ggml-vulkan.cpp 489 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_PERF) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #include "ggml-impl.h"
  25. #include "ggml-backend-impl.h"
  26. #include "ggml-vulkan-shaders.hpp"
  27. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  28. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  29. #define VK_VENDOR_ID_AMD 0x1002
  30. #define VK_VENDOR_ID_APPLE 0x106b
  31. #define VK_VENDOR_ID_INTEL 0x8086
  32. #define VK_VENDOR_ID_NVIDIA 0x10de
  33. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 32
  34. #define GGML_VK_MAX_NODES 8192
  35. #define MAX_VK_BUFFERS 256
  36. #define VK_CHECK(err, msg) \
  37. do { \
  38. vk::Result err_ = (err); \
  39. if (err_ != vk::Result::eSuccess) { \
  40. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  41. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  42. exit(1); \
  43. } \
  44. } while (0)
  45. #ifdef GGML_VULKAN_DEBUG
  46. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  47. #else
  48. #define VK_LOG_DEBUG(msg) ((void) 0)
  49. #endif // GGML_VULKAN_DEBUG
  50. struct ggml_backend_vk_context;
  51. struct vk_queue {
  52. uint32_t queue_family_index;
  53. vk::Queue queue;
  54. vk::CommandPool pool;
  55. uint32_t cmd_buffer_idx;
  56. std::vector<vk::CommandBuffer> cmd_buffers;
  57. vk::PipelineStageFlags stage_flags;
  58. bool transfer_only;
  59. };
  60. struct vk_pipeline_struct {
  61. std::string name;
  62. vk::ShaderModule shader_module;
  63. vk::DescriptorSetLayout dsl;
  64. std::vector<vk::DescriptorPool> descriptor_pools;
  65. std::vector<vk::DescriptorSet> descriptor_sets;
  66. uint32_t descriptor_set_idx;
  67. vk::PipelineLayout layout;
  68. vk::Pipeline pipeline;
  69. uint32_t push_constant_size;
  70. uint32_t parameter_count;
  71. std::array<uint32_t, 3> wg_denoms;
  72. uint32_t align;
  73. // set to true to request the pipeline is compiled after the dryrun
  74. bool needed {};
  75. // set to true when the shader has been compiled
  76. bool compiled {};
  77. };
  78. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  79. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  80. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  81. struct vk_matmul_pipeline_struct {
  82. vk_pipeline l, m, s;
  83. vk_pipeline a_l, a_m, a_s;
  84. };
  85. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  86. struct vk_matmul_pipeline2 {
  87. vk_matmul_pipeline2() {
  88. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  89. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  90. }
  91. vk_matmul_pipeline f32acc;
  92. vk_matmul_pipeline f16acc;
  93. };
  94. struct vk_device_struct;
  95. typedef std::shared_ptr<vk_device_struct> vk_device;
  96. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  97. struct vk_buffer_struct;
  98. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  99. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  100. struct ggml_backend_vk_buffer_type_context {
  101. std::string name;
  102. vk_device device;
  103. };
  104. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  105. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  106. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  107. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  108. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  109. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  110. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  111. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  112. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  113. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  114. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  115. /* .is_host = */ NULL,
  116. };
  117. #ifdef GGML_VULKAN_MEMORY_DEBUG
  118. class vk_memory_logger;
  119. #endif
  120. #ifdef GGML_VULKAN_PERF
  121. class vk_perf_logger;
  122. #endif
  123. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  124. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  125. enum vk_device_architecture {
  126. OTHER,
  127. AMD_GCN,
  128. AMD_RDNA1,
  129. AMD_RDNA2,
  130. AMD_RDNA3,
  131. };
  132. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  133. vk::PhysicalDeviceProperties props = device.getProperties();
  134. if (props.vendorID == VK_VENDOR_ID_AMD) {
  135. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  136. bool amd_shader_core_properties = false;
  137. bool integer_dot_product = false;
  138. bool subgroup_size_control = false;
  139. for (const auto& properties : ext_props) {
  140. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  141. amd_shader_core_properties = true;
  142. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  143. integer_dot_product = true;
  144. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  145. subgroup_size_control = true;
  146. }
  147. }
  148. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  149. return vk_device_architecture::OTHER;
  150. }
  151. vk::PhysicalDeviceProperties2 props2;
  152. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  153. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  154. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  155. props2.pNext = &shader_core_props_amd;
  156. shader_core_props_amd.pNext = &integer_dot_props;
  157. integer_dot_props.pNext = &subgroup_size_control_props;
  158. device.getProperties2(&props2);
  159. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  160. return vk_device_architecture::AMD_GCN;
  161. }
  162. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  163. // RDNA
  164. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  165. return vk_device_architecture::AMD_RDNA1;
  166. }
  167. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  168. return vk_device_architecture::AMD_RDNA3;
  169. }
  170. return vk_device_architecture::AMD_RDNA2;
  171. }
  172. }
  173. return vk_device_architecture::OTHER;
  174. }
  175. struct vk_device_struct {
  176. std::mutex mutex;
  177. vk::PhysicalDevice physical_device;
  178. vk::PhysicalDeviceProperties properties;
  179. std::string name;
  180. uint64_t max_memory_allocation_size;
  181. uint64_t suballocation_block_size;
  182. bool fp16;
  183. bool pipeline_robustness;
  184. vk::Device device;
  185. uint32_t vendor_id;
  186. vk_device_architecture architecture;
  187. vk_queue compute_queue;
  188. vk_queue transfer_queue;
  189. bool single_queue;
  190. uint32_t subgroup_size;
  191. uint32_t shader_core_count;
  192. bool uma;
  193. bool prefer_host_memory;
  194. bool float_controls_rte_fp16;
  195. bool subgroup_size_control;
  196. uint32_t subgroup_min_size;
  197. uint32_t subgroup_max_size;
  198. bool subgroup_require_full_support;
  199. bool coopmat_support;
  200. bool coopmat_acc_f32_support;
  201. bool coopmat_acc_f16_support;
  202. uint32_t coopmat_m;
  203. uint32_t coopmat_n;
  204. uint32_t coopmat_k;
  205. bool coopmat2;
  206. size_t idx;
  207. bool mul_mat_l[GGML_TYPE_COUNT];
  208. bool mul_mat_m[GGML_TYPE_COUNT];
  209. bool mul_mat_s[GGML_TYPE_COUNT];
  210. bool mul_mat_id_l[GGML_TYPE_COUNT];
  211. bool mul_mat_id_m[GGML_TYPE_COUNT];
  212. bool mul_mat_id_s[GGML_TYPE_COUNT];
  213. // set to true to indicate that some shaders need to be compiled after the dryrun
  214. bool need_compiles {};
  215. vk_matmul_pipeline pipeline_matmul_f32 {};
  216. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  217. vk_matmul_pipeline2 pipeline_matmul_f16;
  218. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  219. vk_pipeline pipeline_matmul_split_k_reduce;
  220. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  221. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  222. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  223. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  224. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  225. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  226. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  227. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  228. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  229. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  230. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32;
  231. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  232. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  233. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  234. vk_pipeline pipeline_acc_f32;
  235. vk_pipeline pipeline_add_f32, pipeline_add_f32_norepeat;
  236. vk_pipeline pipeline_add_f16_f32_f16, pipeline_add_f16_f32_f16_norepeat;
  237. vk_pipeline pipeline_sub_f32, pipeline_sub_f32_norepeat;
  238. vk_pipeline pipeline_mul_f32, pipeline_mul_f32_norepeat;
  239. vk_pipeline pipeline_div_f32, pipeline_div_f32_norepeat;
  240. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  241. vk_pipeline pipeline_upscale_f32;
  242. vk_pipeline pipeline_scale_f32;
  243. vk_pipeline pipeline_sqr_f32;
  244. vk_pipeline pipeline_sin_f32;
  245. vk_pipeline pipeline_cos_f32;
  246. vk_pipeline pipeline_clamp_f32;
  247. vk_pipeline pipeline_pad_f32;
  248. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  249. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16;
  250. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16;
  251. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  252. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  253. vk_pipeline pipeline_norm_f32;
  254. vk_pipeline pipeline_group_norm_f32;
  255. vk_pipeline pipeline_rms_norm_f32;
  256. vk_pipeline pipeline_rms_norm_back_f32;
  257. vk_pipeline pipeline_l2_norm_f32;
  258. vk_pipeline pipeline_gelu_f32;
  259. vk_pipeline pipeline_gelu_quick_f32;
  260. vk_pipeline pipeline_silu_f32;
  261. vk_pipeline pipeline_silu_back_f32;
  262. vk_pipeline pipeline_relu_f32;
  263. vk_pipeline pipeline_leaky_relu_f32;
  264. vk_pipeline pipeline_tanh_f32;
  265. vk_pipeline pipeline_sigmoid_f32;
  266. vk_pipeline pipeline_diag_mask_inf_f32;
  267. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  268. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  269. vk_pipeline pipeline_soft_max_back_f32;
  270. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  271. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  272. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  273. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  274. vk_pipeline pipeline_argsort_f32;
  275. vk_pipeline pipeline_sum_rows_f32;
  276. vk_pipeline pipeline_argmax_f32;
  277. vk_pipeline pipeline_count_equal_i32;
  278. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  279. vk_pipeline pipeline_timestep_embedding_f32;
  280. vk_pipeline pipeline_pool2d_f32;
  281. vk_pipeline pipeline_rwkv_wkv6_f32;
  282. vk_pipeline pipeline_rwkv_wkv7_f32;
  283. vk_pipeline pipeline_opt_step_adamw_f32;
  284. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  285. vk_pipeline pipeline_flash_attn_f32_f16_D64[GGML_TYPE_COUNT][2][2][2];
  286. vk_pipeline pipeline_flash_attn_f32_f16_D80[GGML_TYPE_COUNT][2][2][2];
  287. vk_pipeline pipeline_flash_attn_f32_f16_D96[GGML_TYPE_COUNT][2][2][2];
  288. vk_pipeline pipeline_flash_attn_f32_f16_D112[GGML_TYPE_COUNT][2][2][2];
  289. vk_pipeline pipeline_flash_attn_f32_f16_D128[GGML_TYPE_COUNT][2][2][2];
  290. vk_pipeline pipeline_flash_attn_f32_f16_D256[GGML_TYPE_COUNT][2][2][2];
  291. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  292. std::unordered_map<std::string, uint64_t> pipeline_descriptor_set_requirements;
  293. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  294. vk::Fence fence;
  295. vk_buffer sync_staging;
  296. ggml_backend_buffer_type buffer_type;
  297. #ifdef GGML_VULKAN_MEMORY_DEBUG
  298. std::unique_ptr<vk_memory_logger> memory_logger;
  299. #endif
  300. #ifdef GGML_VULKAN_PERF
  301. std::unique_ptr<vk_perf_logger> perf_logger;
  302. #endif
  303. ~vk_device_struct() {
  304. VK_LOG_DEBUG("destroy device " << name);
  305. device.destroyFence(fence);
  306. ggml_vk_destroy_buffer(sync_staging);
  307. device.destroyCommandPool(compute_queue.pool);
  308. if (!single_queue) {
  309. device.destroyCommandPool(transfer_queue.pool);
  310. }
  311. for (auto& pipeline : pipelines) {
  312. if (pipeline.second.expired()) {
  313. continue;
  314. }
  315. vk_pipeline pl = pipeline.second.lock();
  316. ggml_vk_destroy_pipeline(device, pl);
  317. }
  318. pipelines.clear();
  319. device.destroy();
  320. }
  321. };
  322. struct vk_buffer_struct {
  323. vk::Buffer buffer = VK_NULL_HANDLE;
  324. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  325. vk::MemoryPropertyFlags memory_property_flags;
  326. void * ptr;
  327. size_t size = 0;
  328. vk_device device;
  329. ~vk_buffer_struct() {
  330. if (size == 0) {
  331. return;
  332. }
  333. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  334. device->device.freeMemory(device_memory);
  335. device->device.destroyBuffer(buffer);
  336. }
  337. };
  338. struct vk_subbuffer {
  339. vk_buffer buffer;
  340. uint64_t offset;
  341. uint64_t size;
  342. operator vk::DescriptorBufferInfo() const {
  343. return { buffer->buffer, offset, size };
  344. }
  345. };
  346. struct vk_semaphore {
  347. vk::Semaphore s;
  348. uint64_t value;
  349. };
  350. struct vk_submission {
  351. vk::CommandBuffer buffer;
  352. std::vector<vk_semaphore> wait_semaphores;
  353. std::vector<vk_semaphore> signal_semaphores;
  354. };
  355. typedef std::vector<vk_submission> vk_sequence;
  356. struct vk_mat_mat_push_constants {
  357. uint32_t M; uint32_t N; uint32_t K;
  358. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  359. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  360. uint32_t k_split;
  361. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  362. uint32_t padded_N;
  363. };
  364. struct vk_mat_vec_push_constants {
  365. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  366. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  367. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  368. };
  369. struct vk_mat_mat_id_push_constants {
  370. uint32_t M; uint32_t N; uint32_t K;
  371. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  372. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  373. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  374. uint32_t padded_N;
  375. };
  376. struct vk_mat_vec_id_push_constants {
  377. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  378. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  379. uint32_t nei0; uint32_t ne11;
  380. };
  381. struct vk_flash_attn_push_constants {
  382. uint32_t N;
  383. uint32_t KV;
  384. uint32_t ne1;
  385. uint32_t ne2;
  386. uint32_t ne3;
  387. uint32_t neq2;
  388. uint32_t neq3;
  389. uint32_t nek2;
  390. uint32_t nek3;
  391. uint32_t nev2;
  392. uint32_t nev3;
  393. uint32_t nem1;
  394. uint32_t nb01;
  395. uint32_t nb02;
  396. uint32_t nb03;
  397. uint32_t nb11;
  398. uint32_t nb12;
  399. uint32_t nb13;
  400. uint32_t nb21;
  401. uint32_t nb22;
  402. uint32_t nb23;
  403. uint32_t nb31;
  404. float scale;
  405. float max_bias;
  406. float logit_softcap;
  407. uint32_t mask;
  408. uint32_t n_head_log2;
  409. float m0;
  410. float m1;
  411. };
  412. struct vk_op_push_constants {
  413. uint32_t KX;
  414. uint32_t KY;
  415. float param1;
  416. float param2;
  417. };
  418. struct vk_op_unary_push_constants {
  419. uint32_t ne;
  420. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  421. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  422. uint32_t misalign_offsets;
  423. float param1; float param2;
  424. uint32_t ne0_012mp; uint32_t ne0_012L;
  425. uint32_t ne0_01mp; uint32_t ne0_01L;
  426. uint32_t ne0_0mp; uint32_t ne0_0L;
  427. uint32_t ne1_012mp; uint32_t ne1_012L;
  428. uint32_t ne1_01mp; uint32_t ne1_01L;
  429. uint32_t ne1_0mp; uint32_t ne1_0L;
  430. };
  431. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  432. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  433. // Precompute mp (m' in the paper) and L such that division
  434. // can be computed using a multiply (high 32b of 64b result)
  435. // and a shift:
  436. //
  437. // n/d = (mulhi(n, mp) + n) >> L;
  438. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  439. {
  440. // compute L = ceil(log2(d));
  441. L = 0;
  442. while (L < 32 && (uint32_t{1} << L) < d) {
  443. L++;
  444. }
  445. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  446. }
  447. template <typename T> void init_pushconst_fastdiv(T &p) {
  448. GGML_UNUSED(p);
  449. static_assert(!std::is_const<T>::value, "unexpected type");
  450. }
  451. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  452. // Compute magic values to divide by these six numbers.
  453. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  454. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  455. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  456. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  457. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  458. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  459. }
  460. struct vk_op_binary_push_constants {
  461. uint32_t ne;
  462. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  463. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  464. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  465. uint32_t misalign_offsets;
  466. float param1; float param2; int32_t param3;
  467. };
  468. struct vk_op_diag_mask_push_constants {
  469. uint32_t ncols;
  470. uint32_t rows_per_channel;
  471. int32_t n_past;
  472. };
  473. struct vk_op_rope_push_constants {
  474. uint32_t ncols;
  475. uint32_t n_dims;
  476. float freq_scale;
  477. uint32_t p_delta_rows;
  478. float freq_base;
  479. float ext_factor;
  480. float attn_factor;
  481. float corr_dims[2];
  482. float theta_scale;
  483. uint32_t has_ff;
  484. uint32_t ne02;
  485. uint32_t s1;
  486. uint32_t s2;
  487. int32_t sections[4];
  488. uint32_t is_back;
  489. };
  490. struct vk_op_soft_max_push_constants {
  491. uint32_t KX;
  492. uint32_t KY;
  493. float scale;
  494. float max_bias;
  495. float m0;
  496. float m1;
  497. uint32_t n_head_log2;
  498. uint32_t nrows_x;
  499. };
  500. struct vk_op_argsort_push_constants {
  501. uint32_t ncols;
  502. uint32_t ncols_pad;
  503. int32_t order;
  504. };
  505. struct vk_op_im2col_push_constants {
  506. uint32_t batch_offset; uint32_t offset_delta;
  507. uint32_t IC;
  508. uint32_t IW; uint32_t IH;
  509. uint32_t OW; uint32_t OH;
  510. uint32_t KW; uint32_t KH;
  511. uint32_t pelements;
  512. uint32_t CHW;
  513. int32_t s0; int32_t s1;
  514. int32_t p0; int32_t p1;
  515. int32_t d0; int32_t d1;
  516. };
  517. struct vk_op_timestep_embedding_push_constants {
  518. uint32_t nb1;
  519. uint32_t dim;
  520. uint32_t max_period;
  521. };
  522. struct vk_op_pool2d_push_constants {
  523. uint32_t IW; uint32_t IH;
  524. uint32_t OW; uint32_t OH;
  525. uint32_t OC;
  526. uint32_t pelements;
  527. uint32_t op;
  528. int32_t k0; int32_t k1;
  529. int32_t s0; int32_t s1;
  530. int32_t p0; int32_t p1;
  531. };
  532. struct vk_op_rwkv_wkv6_push_constants {
  533. uint32_t B;
  534. uint32_t T;
  535. uint32_t C;
  536. uint32_t H;
  537. };
  538. struct vk_op_rwkv_wkv7_push_constants {
  539. uint32_t B;
  540. uint32_t T;
  541. uint32_t C;
  542. uint32_t H;
  543. };
  544. // Allow pre-recording command buffers
  545. struct vk_staging_memcpy {
  546. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  547. void * dst;
  548. const void * src;
  549. size_t n;
  550. };
  551. struct vk_op_upscale_push_constants {
  552. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  553. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  554. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  555. float sf0; float sf1; float sf2; float sf3;
  556. };
  557. struct vk_context_struct {
  558. vk_submission * s;
  559. std::vector<vk_sequence> seqs;
  560. int exit_tensor_idx;
  561. std::vector<vk_staging_memcpy> in_memcpys;
  562. std::vector<vk_staging_memcpy> out_memcpys;
  563. vk_queue * q;
  564. };
  565. typedef std::shared_ptr<vk_context_struct> vk_context;
  566. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  567. struct ggml_vk_garbage_collector {
  568. std::vector<vk_semaphore> tl_semaphores;
  569. std::vector<vk_semaphore> semaphores;
  570. std::vector<vk::Event> events;
  571. std::vector<vk_buffer> temp_buffers;
  572. std::vector<vk_context> contexts;
  573. };
  574. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  575. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  576. static std::string format_size(size_t size) {
  577. const size_t kib = 1024;
  578. const size_t mib = kib * 1024;
  579. const size_t gib = mib * 1024;
  580. std::ostringstream oss;
  581. oss << std::fixed << std::setprecision(2);
  582. if (size >= gib) {
  583. oss << static_cast<double>(size) / gib << " GiB";
  584. } else if (size >= mib) {
  585. oss << static_cast<double>(size) / mib << " MiB";
  586. } else if (size >= kib) {
  587. oss << static_cast<double>(size) / kib << " KiB";
  588. } else {
  589. oss << size << " B";
  590. }
  591. return oss.str();
  592. }
  593. static std::mutex log_mutex;
  594. class vk_memory_logger {
  595. public:
  596. vk_memory_logger(): total_device(0), total_host(0) {}
  597. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  598. void log_deallocation(vk_buffer_ref buf_ref);
  599. private:
  600. std::map<vk::Buffer, size_t> allocations; // Track allocations
  601. size_t total_device;
  602. size_t total_host;
  603. };
  604. #else
  605. #define VK_LOG_MEMORY(msg) ((void) 0)
  606. #endif // GGML_VULKAN_MEMORY_DEBUG
  607. #if defined(GGML_VULKAN_PERF)
  608. class vk_perf_logger {
  609. public:
  610. void print_timings() {
  611. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  612. for (const auto& t : timings) {
  613. uint64_t total = 0;
  614. for (const auto& time : t.second) {
  615. total += time;
  616. }
  617. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " ms" << std::endl;
  618. }
  619. timings.clear();
  620. }
  621. void log_timing(const ggml_tensor * node, uint64_t time) {
  622. if (node->op == GGML_OP_UNARY) {
  623. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  624. return;
  625. }
  626. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  627. const uint64_t m = node->src[0]->ne[1];
  628. const uint64_t n = node->src[1]->ne[1];
  629. const uint64_t k = node->src[1]->ne[0];
  630. std::string name = ggml_op_name(node->op);
  631. if (n == 1) {
  632. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  633. } else {
  634. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  635. }
  636. timings[name].push_back(time);
  637. return;
  638. }
  639. timings[ggml_op_name(node->op)].push_back(time);
  640. }
  641. private:
  642. std::map<std::string, std::vector<uint64_t>> timings;
  643. };
  644. #endif // GGML_VULKAN_PERF
  645. struct ggml_backend_vk_context {
  646. std::string name;
  647. vk_device device;
  648. size_t semaphore_idx, event_idx;
  649. ggml_vk_garbage_collector gc;
  650. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  651. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  652. vk::Fence fence;
  653. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  654. vk_context_ref compute_ctx;
  655. vk_context_ref transfer_ctx;
  656. std::vector<vk_context_ref> tensor_ctxs;
  657. };
  658. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  659. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  660. if (tensor->view_src) {
  661. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  662. }
  663. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  664. }
  665. struct ggml_backend_vk_buffer_context {
  666. vk_device_ref device;
  667. vk_buffer dev_buffer;
  668. std::string name;
  669. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  670. device(device),
  671. dev_buffer(dev_buffer),
  672. name(name) {
  673. }
  674. ~ggml_backend_vk_buffer_context() {
  675. ggml_vk_destroy_buffer(dev_buffer);
  676. }
  677. };
  678. #ifdef GGML_VULKAN_MEMORY_DEBUG
  679. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  680. std::lock_guard<std::mutex> guard(log_mutex);
  681. vk_buffer buf = buf_ref.lock();
  682. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  683. const std::string type = device ? "device" : "host";
  684. allocations[buf->buffer] = size;
  685. total_device += device ? size : 0;
  686. total_host += device ? 0 : size;
  687. VK_LOG_MEMORY(buf->device->name << ": +" << format_size(size) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host));
  688. }
  689. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  690. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  691. return;
  692. }
  693. std::lock_guard<std::mutex> guard(log_mutex);
  694. vk_buffer buf = buf_ref.lock();
  695. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  696. std::string type = device ? "device" : "host";
  697. auto it = allocations.find(buf->buffer);
  698. total_device -= device ? it->second : 0;
  699. total_host -= device ? 0 : it->second;
  700. if (it != allocations.end()) {
  701. VK_LOG_MEMORY(buf->device->name << ": -" << format_size(it->second) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host));
  702. allocations.erase(it);
  703. } else {
  704. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  705. }
  706. }
  707. #endif // GGML_VULKAN_MEMORY_DEBUG
  708. struct vk_instance_t {
  709. vk::Instance instance;
  710. std::vector<size_t> device_indices;
  711. vk_device devices[GGML_VK_MAX_DEVICES];
  712. };
  713. static bool vk_instance_initialized = false;
  714. static vk_instance_t vk_instance;
  715. #ifdef GGML_VULKAN_CHECK_RESULTS
  716. static size_t vk_skip_checks;
  717. static size_t vk_output_tensor;
  718. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  719. static void ggml_vk_check_results_0(ggml_tensor * tensor);
  720. static void ggml_vk_check_results_1(ggml_tensor * tensor);
  721. #endif
  722. 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);
  723. static void ggml_backend_vk_free(ggml_backend_t backend);
  724. // variables to track number of compiles in progress
  725. static uint32_t compile_count = 0;
  726. static std::mutex compile_count_mutex;
  727. static std::condition_variable compile_count_cond;
  728. static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipeline, size_t spv_size, const void* spv_data, const std::string entrypoint,
  729. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  730. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  731. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  732. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  733. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  734. GGML_ASSERT(parameter_count > 0);
  735. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  736. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  737. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  738. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  739. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  740. for (uint32_t i = 0; i < parameter_count; i++) {
  741. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  742. dsl_binding_flags.push_back({});
  743. }
  744. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  745. vk::PushConstantRange pcr(
  746. vk::ShaderStageFlagBits::eCompute,
  747. 0,
  748. pipeline->push_constant_size
  749. );
  750. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  751. {},
  752. dsl_binding);
  753. descriptor_set_layout_create_info.setPNext(&dslbfci);
  754. pipeline->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  755. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  756. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  757. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  758. pipeline->descriptor_set_idx = 0;
  759. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), pipeline->dsl, pcr);
  760. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  761. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  762. for (size_t i = 0; i < specialization_constants.size(); i++) {
  763. specialization_entries[i].constantID = i;
  764. specialization_entries[i].offset = i * sizeof(uint32_t);
  765. specialization_entries[i].size = sizeof(uint32_t);
  766. }
  767. vk::SpecializationInfo specialization_info(
  768. specialization_entries.size(),
  769. specialization_entries.data(),
  770. specialization_constants.size() * sizeof(uint32_t),
  771. specialization_constants.data()
  772. );
  773. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  774. if (device->subgroup_require_full_support && require_full_subgroups) {
  775. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  776. }
  777. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  778. pipeline_shader_stage_create_flags,
  779. vk::ShaderStageFlagBits::eCompute,
  780. pipeline->shader_module,
  781. entrypoint.c_str(),
  782. &specialization_info);
  783. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  784. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  785. if (device->subgroup_size_control && required_subgroup_size > 0) {
  786. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  787. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  788. }
  789. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  790. vk::PipelineCreateFlags{},
  791. pipeline_shader_create_info,
  792. pipeline->layout);
  793. vk::PipelineRobustnessCreateInfoEXT rci;
  794. if (device->pipeline_robustness && disable_robustness) {
  795. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  796. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  797. compute_pipeline_create_info.setPNext(&rci);
  798. }
  799. try {
  800. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  801. } catch (const vk::SystemError& e) {
  802. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  803. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  804. throw e;
  805. }
  806. pipeline->compiled = true;
  807. {
  808. std::lock_guard<std::mutex> guard(device->mutex);
  809. device->pipelines.insert({ pipeline->name, pipeline });
  810. }
  811. {
  812. std::lock_guard<std::mutex> guard(compile_count_mutex);
  813. assert(compile_count > 0);
  814. compile_count--;
  815. }
  816. compile_count_cond.notify_all();
  817. }
  818. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  819. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  820. for (auto& pool : pipeline->descriptor_pools) {
  821. device.destroyDescriptorPool(pool);
  822. }
  823. pipeline->descriptor_pools.clear();
  824. pipeline->descriptor_sets.clear();
  825. pipeline->descriptor_set_idx = 0;
  826. device.destroyDescriptorSetLayout(pipeline->dsl);
  827. device.destroyPipelineLayout(pipeline->layout);
  828. device.destroyShaderModule(pipeline->shader_module);
  829. device.destroyPipeline(pipeline->pipeline);
  830. }
  831. static void ggml_pipeline_request_descriptor_sets(vk_device& device, vk_pipeline& pipeline, uint32_t n) {
  832. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  833. device->pipeline_descriptor_set_requirements[pipeline->name] += n;
  834. if (!pipeline->compiled) {
  835. pipeline->needed = true;
  836. device->need_compiles = true;
  837. }
  838. }
  839. static void ggml_pipeline_allocate_descriptor_sets(vk_device& device) {
  840. std::lock_guard<std::mutex> guard(device->mutex);
  841. for (auto& pair : device->pipeline_descriptor_set_requirements) {
  842. vk_pipeline pipeline = device->pipelines.at(pair.first).lock();
  843. const uint64_t n = pair.second;
  844. VK_LOG_DEBUG("ggml_pipeline_allocate_descriptor_sets(" << pipeline->name << ", " << n << ")");
  845. if (pipeline->descriptor_sets.size() >= pipeline->descriptor_set_idx + n) {
  846. // Enough descriptors are available
  847. continue;
  848. }
  849. uint32_t to_alloc = pipeline->descriptor_set_idx + n - pipeline->descriptor_sets.size();
  850. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - pipeline->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  851. uint32_t pool_idx = pipeline->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  852. while (to_alloc > 0) {
  853. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  854. to_alloc -= alloc_count;
  855. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  856. if (pool_idx >= pipeline->descriptor_pools.size()) {
  857. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  858. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  859. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  860. }
  861. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  862. for (uint32_t i = 0; i < alloc_count; i++) {
  863. layouts[i] = pipeline->dsl;
  864. }
  865. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[pool_idx], alloc_count, layouts.data());
  866. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  867. pipeline->descriptor_sets.insert(pipeline->descriptor_sets.end(), sets.begin(), sets.end());
  868. pool_idx++;
  869. }
  870. }
  871. }
  872. static void ggml_pipeline_cleanup(vk_pipeline& pipeline) {
  873. VK_LOG_DEBUG("ggml_pipeline_cleanup(" << pipeline->name << ")");
  874. pipeline->descriptor_set_idx = 0;
  875. }
  876. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_queue& q) {
  877. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  878. std::lock_guard<std::mutex> guard(device->mutex);
  879. if (q.cmd_buffers.size() > q.cmd_buffer_idx) {
  880. // Reuse command buffer
  881. return q.cmd_buffers[q.cmd_buffer_idx++];
  882. }
  883. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  884. q.pool,
  885. vk::CommandBufferLevel::ePrimary,
  886. 1);
  887. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  888. auto buf = cmd_buffers.front();
  889. q.cmd_buffers.push_back(buf);
  890. q.cmd_buffer_idx++;
  891. return buf;
  892. }
  893. static vk_submission ggml_vk_create_submission(vk_device& device, vk_queue& q, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  894. VK_LOG_DEBUG("ggml_vk_create_submission()");
  895. vk_submission s;
  896. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  897. s.wait_semaphores = std::move(wait_semaphores);
  898. s.signal_semaphores = std::move(signal_semaphores);
  899. return s;
  900. }
  901. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  902. if (ctx->seqs.empty()) {
  903. if (fence) {
  904. ctx->q->queue.submit({}, fence);
  905. }
  906. return;
  907. }
  908. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  909. std::vector<std::vector<uint64_t>> tl_wait_vals;
  910. std::vector<std::vector<uint64_t>> tl_signal_vals;
  911. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  912. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  913. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  914. std::vector<vk::SubmitInfo> submit_infos;
  915. int idx = -1;
  916. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  917. size_t reserve = 0;
  918. for (const auto& sequence : ctx->seqs) {
  919. reserve += sequence.size();
  920. }
  921. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  922. tl_wait_semaphores.reserve(reserve);
  923. tl_wait_vals.reserve(reserve);
  924. tl_signal_semaphores.reserve(reserve);
  925. tl_signal_vals.reserve(reserve);
  926. tl_submit_infos.reserve(reserve);
  927. submit_infos.reserve(reserve);
  928. stage_flags.reserve(reserve);
  929. for (const auto& sequence : ctx->seqs) {
  930. for (const auto& submission : sequence) {
  931. stage_flags.push_back({});
  932. idx++;
  933. tl_wait_vals.push_back({});
  934. tl_wait_semaphores.push_back({});
  935. tl_signal_vals.push_back({});
  936. tl_signal_semaphores.push_back({});
  937. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  938. stage_flags[idx].push_back(ctx->q->stage_flags);
  939. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  940. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  941. }
  942. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  943. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  944. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  945. }
  946. tl_submit_infos.push_back({
  947. (uint32_t) submission.wait_semaphores.size(),
  948. tl_wait_vals[idx].data(),
  949. (uint32_t) submission.signal_semaphores.size(),
  950. tl_signal_vals[idx].data(),
  951. });
  952. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  953. tl_submit_infos[idx].pNext = nullptr;
  954. vk::SubmitInfo si{
  955. (uint32_t) submission.wait_semaphores.size(),
  956. tl_wait_semaphores[idx].data(),
  957. stage_flags[idx].data(),
  958. 1,
  959. &submission.buffer,
  960. (uint32_t) submission.signal_semaphores.size(),
  961. tl_signal_semaphores[idx].data(),
  962. };
  963. si.setPNext(&tl_submit_infos[idx]);
  964. submit_infos.push_back(si);
  965. }
  966. }
  967. ctx->q->queue.submit(submit_infos, fence);
  968. ctx->seqs.clear();
  969. }
  970. 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) {
  971. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  972. const uint32_t qfsize = queue_family_props.size();
  973. // Try with avoid preferences first
  974. for (uint32_t i = 0; i < qfsize; i++) {
  975. 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)) {
  976. return i;
  977. }
  978. }
  979. // Fall back to only required
  980. for (size_t i = 0; i < qfsize; i++) {
  981. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  982. return i;
  983. }
  984. }
  985. // Fall back to reusing compute queue
  986. for (size_t i = 0; i < qfsize; i++) {
  987. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  988. return i;
  989. }
  990. }
  991. // Fall back to ignoring min_num_queries
  992. for (size_t i = 0; i < qfsize; i++) {
  993. if (queue_family_props[i].queueFlags & required) {
  994. return i;
  995. }
  996. }
  997. // 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.
  998. // 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.
  999. if (compute_index >= 0) {
  1000. return compute_index;
  1001. }
  1002. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1003. for(auto &q_family : queue_family_props) {
  1004. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1005. }
  1006. abort();
  1007. }
  1008. static void ggml_vk_create_queue(vk_device& device, vk_queue& q, uint32_t queue_family_index, uint32_t queue_index, vk::PipelineStageFlags&& stage_flags, bool transfer_only) {
  1009. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1010. std::lock_guard<std::mutex> guard(device->mutex);
  1011. q.queue_family_index = queue_family_index;
  1012. q.transfer_only = transfer_only;
  1013. vk::CommandPoolCreateInfo command_pool_create_info_compute(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), queue_family_index);
  1014. q.pool = device->device.createCommandPool(command_pool_create_info_compute);
  1015. q.cmd_buffer_idx = 0;
  1016. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1017. q.stage_flags = stage_flags;
  1018. }
  1019. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_queue& q) {
  1020. vk_context result = std::make_shared<vk_context_struct>();
  1021. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1022. ctx->gc.contexts.emplace_back(result);
  1023. result->q = &q;
  1024. return result;
  1025. }
  1026. static vk_context ggml_vk_create_temporary_context(vk_queue& q) {
  1027. vk_context result = std::make_shared<vk_context_struct>();
  1028. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1029. result->q = &q;
  1030. return result;
  1031. }
  1032. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1033. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1034. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1035. vk::SemaphoreCreateInfo ci{};
  1036. ci.setPNext(&tci);
  1037. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1038. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1039. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1040. }
  1041. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1042. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1043. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1044. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1045. vk::SemaphoreCreateInfo ci{};
  1046. ci.setPNext(&tci);
  1047. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1048. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1049. }
  1050. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1051. }
  1052. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1053. if (ctx->event_idx >= ctx->gc.events.size()) {
  1054. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1055. }
  1056. return ctx->gc.events[ctx->event_idx++];
  1057. }
  1058. static void ggml_vk_queue_cleanup(vk_device& device, vk_queue& q) {
  1059. VK_LOG_DEBUG("ggml_vk_queue_cleanup()");
  1060. std::lock_guard<std::mutex> guard(device->mutex);
  1061. // Requires command buffers to be done
  1062. device->device.resetCommandPool(q.pool);
  1063. q.cmd_buffer_idx = 0;
  1064. }
  1065. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1066. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1067. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1068. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1069. (flags & memory_type.propertyFlags) == flags &&
  1070. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1071. return static_cast<int32_t>(i);
  1072. }
  1073. }
  1074. return UINT32_MAX;
  1075. }
  1076. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1077. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1078. if (size > device->max_memory_allocation_size) {
  1079. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1080. }
  1081. std::lock_guard<std::mutex> guard(device->mutex);
  1082. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1083. if (size == 0) {
  1084. buf->size = 0;
  1085. return buf;
  1086. }
  1087. vk::BufferCreateInfo buffer_create_info{
  1088. vk::BufferCreateFlags(),
  1089. size,
  1090. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1091. vk::SharingMode::eExclusive,
  1092. 0,
  1093. nullptr,
  1094. };
  1095. buf->buffer = device->device.createBuffer(buffer_create_info);
  1096. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1097. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1098. uint32_t memory_type_index = UINT32_MAX;
  1099. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1100. buf->memory_property_flags = req_flags;
  1101. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1102. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1103. buf->memory_property_flags = fallback_flags;
  1104. }
  1105. if (memory_type_index == UINT32_MAX) {
  1106. device->device.destroyBuffer(buf->buffer);
  1107. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1108. }
  1109. try {
  1110. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1111. } catch (const vk::SystemError& e) {
  1112. if (buf->memory_property_flags != fallback_flags) {
  1113. // Try again with fallback flags
  1114. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1115. buf->memory_property_flags = fallback_flags;
  1116. try {
  1117. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1118. }
  1119. catch (const vk::SystemError& e) {
  1120. device->device.destroyBuffer(buf->buffer);
  1121. throw e;
  1122. }
  1123. } else {
  1124. // Out of Host/Device memory, clean up buffer
  1125. device->device.destroyBuffer(buf->buffer);
  1126. throw e;
  1127. }
  1128. }
  1129. buf->ptr = nullptr;
  1130. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1131. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1132. }
  1133. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1134. buf->device = device;
  1135. buf->size = size;
  1136. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1137. device->memory_logger->log_allocation(buf, size);
  1138. #endif
  1139. return buf;
  1140. }
  1141. static vk_buffer ggml_vk_create_buffer_check(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1142. try {
  1143. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1144. } catch (const vk::SystemError& e) {
  1145. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1146. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1147. throw e;
  1148. }
  1149. }
  1150. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1151. vk_buffer buf;
  1152. try {
  1153. if (device->prefer_host_memory) {
  1154. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1155. } else if (device->uma) {
  1156. // Fall back to host memory type
  1157. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1158. } else {
  1159. // use rebar if available, otherwise fallback to device only visible memory
  1160. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1161. }
  1162. } catch (const vk::SystemError& e) {
  1163. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1164. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1165. throw e;
  1166. }
  1167. return buf;
  1168. }
  1169. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1170. if (buf == nullptr) {
  1171. return;
  1172. }
  1173. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1174. if (buf->device != nullptr) {
  1175. buf->device->memory_logger->log_deallocation(buf);
  1176. }
  1177. #endif
  1178. buf.reset();
  1179. }
  1180. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1181. return { buf, 0, VK_WHOLE_SIZE };
  1182. }
  1183. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1184. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1185. const bool transfer_queue = ctx->q->transfer_only;
  1186. ctx->s->buffer.pipelineBarrier(
  1187. ctx->q->stage_flags,
  1188. ctx->q->stage_flags,
  1189. {},
  1190. { {
  1191. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1192. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1193. } },
  1194. {},
  1195. {}
  1196. );
  1197. }
  1198. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1199. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1200. if (events.empty()) {
  1201. return;
  1202. }
  1203. ctx->s->buffer.waitEvents(
  1204. events,
  1205. ctx->q->stage_flags,
  1206. ctx->q->stage_flags,
  1207. {},
  1208. {},
  1209. {}
  1210. );
  1211. }
  1212. // number of rows/cols for flash attention shader
  1213. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1214. static std::array<uint32_t, 2> fa_rows_cols(uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) {
  1215. GGML_UNUSED(clamp);
  1216. // small rows, large cols
  1217. if (small_rows) {
  1218. return {flash_attention_num_small_rows, 128};
  1219. }
  1220. // small cols to reduce register count
  1221. if (ggml_is_quantized(type) || D == 256) {
  1222. return {64, 32};
  1223. }
  1224. return {64, 64};
  1225. };
  1226. static bool ggml_vk_matmul_shmem_support(const vk_device& device, const std::vector<uint32_t>& warptile, bool mul_mat_id, ggml_type src0_type) {
  1227. uint32_t lut_size = 0;
  1228. switch (src0_type) {
  1229. case GGML_TYPE_IQ1_S:
  1230. case GGML_TYPE_IQ1_M:
  1231. lut_size = 2*2048;
  1232. break;
  1233. case GGML_TYPE_IQ2_XXS:
  1234. lut_size = 8*256;
  1235. break;
  1236. case GGML_TYPE_IQ2_XS:
  1237. lut_size = 8*512;
  1238. break;
  1239. case GGML_TYPE_IQ2_S:
  1240. lut_size = 8*1024;
  1241. break;
  1242. case GGML_TYPE_IQ3_XXS:
  1243. lut_size = 4*256;
  1244. break;
  1245. case GGML_TYPE_IQ3_S:
  1246. lut_size = 4*512;
  1247. break;
  1248. case GGML_TYPE_IQ4_NL:
  1249. case GGML_TYPE_IQ4_XS:
  1250. lut_size = 4*16;
  1251. break;
  1252. default:
  1253. break;
  1254. }
  1255. // Needs to be kept up to date on shader changes
  1256. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1257. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1258. const uint32_t warps = warptile[0] / warptile[10];
  1259. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1260. const uint32_t mmid_row_ids = mul_mat_id ? 3072 * sizeof(uint32_t) : 0;
  1261. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1262. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1263. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1264. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1265. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1266. return supported;
  1267. }
  1268. struct GpuPipelineConfig {
  1269. // GPU architecture identifier.
  1270. // Example: vk_device_architecture::AMD_GCN
  1271. vk_device_architecture arch;
  1272. // Mapping of pipeline names to their specific subgroup sizes.
  1273. // Example: {"soft_max_f32", 64}
  1274. std::unordered_map<std::string, uint32_t> pipelines;
  1275. // Default subgroup size for this GPU.
  1276. // Defaults to 0 if not explicitly provided.
  1277. uint32_t default_subgroup_size = 0;
  1278. };
  1279. // Pipeline configuration for RDNA1 GPUs.
  1280. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1281. {"soft_max", 64}, {"im2col", 64},
  1282. {"argmax", 64}, {"mul_mat_vec", 64},
  1283. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1284. };
  1285. // Pipeline configuration for RDNA2 GPUs.
  1286. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1287. {"soft_max", 64}, {"im2col", 64},
  1288. };
  1289. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1290. // Define configurations for different GPUs.
  1291. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1292. {
  1293. vk_device_architecture::AMD_RDNA1,
  1294. {
  1295. rdna1_pipelines,
  1296. },
  1297. RDNA_DEFAULT_SUBGROUP_SIZE
  1298. },
  1299. {
  1300. vk_device_architecture::AMD_RDNA2,
  1301. {
  1302. rdna2_pipelines,
  1303. },
  1304. RDNA_DEFAULT_SUBGROUP_SIZE
  1305. },
  1306. };
  1307. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1308. for (const auto &config : gpu_pipeline_configs) {
  1309. if (config.arch == arch) {
  1310. auto pipIt = config.pipelines.find(pipeline_name);
  1311. if (pipIt != config.pipelines.end()) {
  1312. return pipIt->second;
  1313. }
  1314. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1315. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1316. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1317. for (const auto &entry : sorted_pipelines) {
  1318. if (pipeline_name.find(entry.first) != std::string::npos) {
  1319. return entry.second;
  1320. }
  1321. }
  1322. return config.default_subgroup_size;
  1323. }
  1324. }
  1325. return 0; // If no matching configuration is found
  1326. }
  1327. static void ggml_vk_load_shaders(vk_device& device) {
  1328. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1329. // some shaders have a minimum subgroup size
  1330. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1331. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1332. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1333. // mulmat
  1334. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1335. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1336. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1337. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1338. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1339. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1340. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1341. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1342. uint32_t l_align, m_align, s_align;
  1343. if (device->coopmat2) {
  1344. // spec constants and tile sizes for non-quant matmul/matmul_id
  1345. l_warptile = { 256, 128, 256, 64, 1 };
  1346. m_warptile = { 256, 128, 128, 64, 0 };
  1347. s_warptile = { 128, 64, 64, 64, 0 };
  1348. l_wg_denoms = {128, 256, 1 };
  1349. m_wg_denoms = {128, 128, 1 };
  1350. s_wg_denoms = { 64, 64, 1 };
  1351. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1352. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1353. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1354. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1355. l_mmq_wg_denoms = { 128, 256, 1 };
  1356. m_mmq_wg_denoms = { 128, 128, 1 };
  1357. s_mmq_wg_denoms = { 32, 64, 1 };
  1358. // spec constants and tile sizes for quant matmul (Qi_K)
  1359. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1360. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1361. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1362. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1363. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1364. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1365. // spec constants and tile sizes for quant matmul_id
  1366. l_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1367. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1368. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1369. l_mmqid_wg_denoms = { 128, 64, 1 };
  1370. m_mmqid_wg_denoms = { 128, 64, 1 };
  1371. s_mmqid_wg_denoms = { 128, 64, 1 };
  1372. l_align = 128;
  1373. m_align = 64;
  1374. s_align = 32;
  1375. } else {
  1376. // Matrix cores require different warp group sizes
  1377. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1378. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1379. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1380. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1381. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1382. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1383. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1384. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1385. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1386. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1387. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1388. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1389. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1390. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1391. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1392. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1393. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1394. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1395. l_align = 128;
  1396. m_align = 64;
  1397. s_align = 32;
  1398. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1399. ggml_type t = (ggml_type)i;
  1400. // Disable medium and large matrix multiplication if not enough shared memory is available
  1401. // Check mmq warptiles as the largest configuration
  1402. // Throw an error if not enough for any matrix multiplication is available
  1403. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1404. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1405. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1406. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1407. device->mul_mat_m[i] = false;
  1408. device->mul_mat_l[i] = false;
  1409. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1410. device->mul_mat_l[i] = false;
  1411. }
  1412. // Disable mul_mat_id if not enough shared memory is available
  1413. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1414. device->mul_mat_id_s[i] = false;
  1415. device->mul_mat_id_m[i] = false;
  1416. device->mul_mat_id_l[i] = false;
  1417. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1418. device->mul_mat_id_m[i] = false;
  1419. device->mul_mat_id_l[i] = false;
  1420. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1421. device->mul_mat_id_l[i] = false;
  1422. }
  1423. }
  1424. }
  1425. if (!device->pipeline_matmul_f32) {
  1426. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1427. }
  1428. if (!device->pipeline_matmul_f32_f16) {
  1429. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1430. }
  1431. if (!device->pipeline_matmul_id_f32) {
  1432. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1433. }
  1434. std::vector<std::future<void>> compiles;
  1435. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const std::string &entrypoint,
  1436. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1437. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1438. if (!require_full_subgroups && required_subgroup_size == 0) {
  1439. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1440. }
  1441. if (!pipeline) {
  1442. pipeline = std::make_shared<vk_pipeline_struct>();
  1443. pipeline->name = name;
  1444. pipeline->parameter_count = parameter_count;
  1445. pipeline->push_constant_size = push_constant_size;
  1446. pipeline->wg_denoms = wg_denoms;
  1447. pipeline->align = align;
  1448. }
  1449. if (!pipeline->needed || pipeline->compiled) {
  1450. return;
  1451. }
  1452. {
  1453. // wait until fewer than N compiles are in progress
  1454. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1455. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1456. while (compile_count >= N) {
  1457. compile_count_cond.wait(guard);
  1458. }
  1459. compile_count++;
  1460. }
  1461. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1462. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1463. };
  1464. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1465. if (device->coopmat2) {
  1466. auto const &fa_wg_denoms = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  1467. return {fa_rows_cols(D, clamp, type, small_rows)[0], 1, 1};
  1468. };
  1469. auto const &fa_spec_constants = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  1470. // For large number of rows, 128 invocations seems to work best.
  1471. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1472. // can't use 256 for D==80.
  1473. uint32_t wg_size = (small_rows && (D % 32) == 0) ? 256 : 128;
  1474. auto rows_cols = fa_rows_cols(D, clamp, type, small_rows);
  1475. return {wg_size, rows_cols[0], rows_cols[1], (D), clamp};
  1476. };
  1477. #define CREATE_FA2(TYPE, NAMELC, D) \
  1478. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][0], "flash_attn_f32_f16_D" #D "_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1479. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1480. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][0], "flash_attn_f32_f16_D" #D "_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1481. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1482. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][0], "flash_attn_f32_f16_D" #D "_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1483. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1484. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][0], "flash_attn_f32_f16_D" #D "_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1485. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1486. #define CREATE_FA(TYPE, NAMELC) \
  1487. CREATE_FA2(TYPE, NAMELC, 64) \
  1488. CREATE_FA2(TYPE, NAMELC, 80) \
  1489. CREATE_FA2(TYPE, NAMELC, 96) \
  1490. CREATE_FA2(TYPE, NAMELC, 112) \
  1491. CREATE_FA2(TYPE, NAMELC, 128) \
  1492. CREATE_FA2(TYPE, NAMELC, 256)
  1493. CREATE_FA(GGML_TYPE_F16, f16)
  1494. CREATE_FA(GGML_TYPE_Q4_0, q4_0)
  1495. CREATE_FA(GGML_TYPE_Q4_1, q4_1)
  1496. CREATE_FA(GGML_TYPE_Q5_0, q5_0)
  1497. CREATE_FA(GGML_TYPE_Q5_1, q5_1)
  1498. CREATE_FA(GGML_TYPE_Q8_0, q8_0)
  1499. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  1500. //CREATE_FA(GGML_TYPE_Q2_K, q2_k)
  1501. //CREATE_FA(GGML_TYPE_Q3_K, q3_k)
  1502. //CREATE_FA(GGML_TYPE_Q4_K, q4_k)
  1503. //CREATE_FA(GGML_TYPE_Q5_K, q5_k)
  1504. //CREATE_FA(GGML_TYPE_Q6_K, q6_k)
  1505. //CREATE_FA(GGML_TYPE_IQ1_S, iq1_s)
  1506. //CREATE_FA(GGML_TYPE_IQ1_M, iq1_m)
  1507. //CREATE_FA(GGML_TYPE_IQ2_XXS, iq2_xxs)
  1508. //CREATE_FA(GGML_TYPE_IQ2_XS, iq2_xs)
  1509. //CREATE_FA(GGML_TYPE_IQ2_S, iq2_s)
  1510. //CREATE_FA(GGML_TYPE_IQ3_XXS, iq3_xxs)
  1511. //CREATE_FA(GGML_TYPE_IQ3_S, iq3_s)
  1512. //CREATE_FA(GGML_TYPE_IQ4_XS, iq4_xs)
  1513. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl)
  1514. #undef CREATE_FA
  1515. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1516. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1517. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1518. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1519. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1520. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1521. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1522. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1523. // Create 2 variants, {f16,f32} accumulator
  1524. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1525. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1526. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1527. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1528. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1529. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1530. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1531. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1532. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1533. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1534. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1535. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1536. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1537. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1538. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1539. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1540. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1541. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1542. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1543. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1544. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1545. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1546. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1547. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1548. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1549. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1550. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1551. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1552. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1553. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1554. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1555. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1556. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1557. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1558. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1559. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1560. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1561. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1562. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1563. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1564. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1565. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1566. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1567. #undef CREATE_MM
  1568. #undef CREATE_MM2
  1569. } else
  1570. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1571. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1572. if (device->coopmat_support) {
  1573. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1574. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1575. if (device->mul_mat ## ID ## _l[TYPE]) \
  1576. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  1577. if (device->mul_mat ## ID ## _m[TYPE]) \
  1578. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  1579. if (device->mul_mat ## ID ## _s[TYPE]) \
  1580. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  1581. if (device->mul_mat ## ID ## _l[TYPE]) \
  1582. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  1583. if (device->mul_mat ## ID ## _m[TYPE]) \
  1584. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  1585. if (device->mul_mat ## ID ## _s[TYPE]) \
  1586. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  1587. // Create 2 variants, {f16,f32} accumulator
  1588. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1589. if (device->coopmat_acc_f16_support) { \
  1590. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1591. } \
  1592. if (device->coopmat_acc_f32_support) { \
  1593. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1594. } \
  1595. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1596. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1597. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1598. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1599. if (device->coopmat_acc_f16_support) {
  1600. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1601. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1602. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1603. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1604. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1605. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1606. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1607. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1608. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1609. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1610. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1611. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1612. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1613. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1614. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1615. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1616. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1617. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1618. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1619. } else {
  1620. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1621. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1622. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1623. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1624. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1625. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1626. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1627. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1628. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1629. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1630. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1631. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1632. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1633. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1634. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1635. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1636. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1637. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1638. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1639. }
  1640. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1641. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1642. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1643. if (device->coopmat_acc_f16_support) {
  1644. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1645. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1646. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1647. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1648. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1649. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1650. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1651. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1652. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1653. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1654. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1655. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1656. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1657. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1658. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1659. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1660. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1661. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1662. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1663. } else {
  1664. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1665. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1666. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1667. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1668. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1669. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1670. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1671. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1672. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1673. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1674. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1675. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1676. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1677. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1678. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1679. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1680. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1681. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1682. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1683. }
  1684. #undef CREATE_MM2
  1685. #undef CREATE_MM
  1686. } else
  1687. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1688. if (device->fp16) {
  1689. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1690. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1691. if (device->mul_mat ## ID ## _l[TYPE]) \
  1692. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1693. if (device->mul_mat ## ID ## _m[TYPE]) \
  1694. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1695. if (device->mul_mat ## ID ## _s[TYPE]) \
  1696. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1697. if (device->mul_mat ## ID ## _l[TYPE]) \
  1698. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1699. if (device->mul_mat ## ID ## _m[TYPE]) \
  1700. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1701. if (device->mul_mat ## ID ## _s[TYPE]) \
  1702. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1703. // Create 2 variants, {f16,f32} accumulator
  1704. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1705. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1706. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1707. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1708. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1709. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1710. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1711. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1712. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1713. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1714. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1715. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1716. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1717. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1718. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1719. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1720. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1721. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1722. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1723. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1724. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1725. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1726. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1727. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1728. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1729. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1730. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1731. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1732. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1733. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1734. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1735. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1736. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1737. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1738. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1739. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1740. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1741. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1742. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1743. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1744. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1745. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1746. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1747. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1748. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1749. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1750. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1751. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1752. #undef CREATE_MM2
  1753. #undef CREATE_MM
  1754. } else {
  1755. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1756. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1757. if (device->mul_mat ## ID ## _l[TYPE]) \
  1758. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1759. if (device->mul_mat ## ID ## _m[TYPE]) \
  1760. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1761. if (device->mul_mat ## ID ## _s[TYPE]) \
  1762. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1763. if (device->mul_mat ## ID ## _l[TYPE]) \
  1764. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1765. if (device->mul_mat ## ID ## _m[TYPE]) \
  1766. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1767. if (device->mul_mat ## ID ## _s[TYPE]) \
  1768. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1769. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1770. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1771. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1772. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1773. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1774. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1775. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1776. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1777. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1778. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1779. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1780. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1781. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1782. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1783. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1784. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1785. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1786. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1787. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1788. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1789. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1790. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1791. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1792. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1793. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1794. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1795. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1796. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1797. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1798. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1799. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1800. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1801. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1802. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1803. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1804. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1805. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1806. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1807. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1808. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1809. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1810. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1811. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1812. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1813. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1814. #undef CREATE_MM
  1815. }
  1816. // mul mat vec
  1817. // the number of rows computed per shader depends on GPU model and quant
  1818. uint32_t rm_stdq = 1;
  1819. uint32_t rm_kq = 2;
  1820. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  1821. if (device->subgroup_min_size == 64 && device->subgroup_max_size == 64) { // GCN
  1822. rm_stdq = 2;
  1823. rm_kq = 4;
  1824. }
  1825. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  1826. rm_stdq = 2;
  1827. uint32_t rm_iq = 2 * rm_kq;
  1828. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  1829. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1830. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1831. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1832. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1833. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1834. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1835. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f32_f32_len, mul_mat_vec_q8_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  1836. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1837. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1838. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1839. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1840. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1841. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1842. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1843. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1844. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1845. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1846. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1847. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1848. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1849. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1850. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1851. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1852. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1853. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1854. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1855. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1856. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f16_f32_len, mul_mat_vec_q8_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  1857. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1858. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1859. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1860. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1861. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1862. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1863. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1864. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1865. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1866. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1867. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1868. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1869. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1870. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1871. }
  1872. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F32 ], "mul_mat_vec_id_f32_f32", mul_mat_vec_id_f32_f32_len, mul_mat_vec_id_f32_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  1873. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F16 ], "mul_mat_vec_id_f16_f32", mul_mat_vec_id_f16_f32_len, mul_mat_vec_id_f16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  1874. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_0], "mul_mat_vec_id_q4_0_f32", mul_mat_vec_id_q4_0_f32_len, mul_mat_vec_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1875. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_1], "mul_mat_vec_id_q4_1_f32", mul_mat_vec_id_q4_1_f32_len, mul_mat_vec_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1876. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_0], "mul_mat_vec_id_q5_0_f32", mul_mat_vec_id_q5_0_f32_len, mul_mat_vec_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1877. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_1], "mul_mat_vec_id_q5_1_f32", mul_mat_vec_id_q5_1_f32_len, mul_mat_vec_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1878. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q8_0], "mul_mat_vec_id_q8_0_f32", mul_mat_vec_id_q8_0_f32_len, mul_mat_vec_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq}, 1, true);
  1879. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q2_K], "mul_mat_vec_id_q2_k_f32", mul_mat_vec_id_q2_k_f32_len, mul_mat_vec_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1880. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q3_K], "mul_mat_vec_id_q3_k_f32", mul_mat_vec_id_q3_k_f32_len, mul_mat_vec_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1881. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_K], "mul_mat_vec_id_q4_k_f32", mul_mat_vec_id_q4_k_f32_len, mul_mat_vec_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1882. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_K], "mul_mat_vec_id_q5_k_f32", mul_mat_vec_id_q5_k_f32_len, mul_mat_vec_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1883. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q6_K], "mul_mat_vec_id_q6_k_f32", mul_mat_vec_id_q6_k_f32_len, mul_mat_vec_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1884. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_S], "mul_mat_vec_id_iq1_s_f32", mul_mat_vec_id_iq1_s_f32_len, mul_mat_vec_id_iq1_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1885. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_M], "mul_mat_vec_id_iq1_m_f32", mul_mat_vec_id_iq1_m_f32_len, mul_mat_vec_id_iq1_m_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1886. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XXS], "mul_mat_vec_id_iq2_xxs_f32", mul_mat_vec_id_iq2_xxs_f32_len, mul_mat_vec_id_iq2_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1887. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XS], "mul_mat_vec_id_iq2_xs_f32", mul_mat_vec_id_iq2_xs_f32_len, mul_mat_vec_id_iq2_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1888. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_S], "mul_mat_vec_id_iq2_s_f32", mul_mat_vec_id_iq2_s_f32_len, mul_mat_vec_id_iq2_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1889. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_XXS], "mul_mat_vec_id_iq3_xxs_f32", mul_mat_vec_id_iq3_xxs_f32_len, mul_mat_vec_id_iq3_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1890. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_S], "mul_mat_vec_id_iq3_s_f32", mul_mat_vec_id_iq3_s_f32_len, mul_mat_vec_id_iq3_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1891. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_XS], "mul_mat_vec_id_iq4_xs_f32", mul_mat_vec_id_iq4_xs_f32_len, mul_mat_vec_id_iq4_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1892. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_NL], "mul_mat_vec_id_iq4_nl_f32", mul_mat_vec_id_iq4_nl_f32_len, mul_mat_vec_id_iq4_nl_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1893. // dequant shaders
  1894. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_F32 ], "f32_to_f16", dequant_f32_len, dequant_f32_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1895. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_0], "dequant_q4_0", dequant_q4_0_len, dequant_q4_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1896. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_1], "dequant_q4_1", dequant_q4_1_len, dequant_q4_1_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1897. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_0], "dequant_q5_0", dequant_q5_0_len, dequant_q5_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1898. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_1], "dequant_q5_1", dequant_q5_1_len, dequant_q5_1_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1899. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q8_0], "dequant_q8_0", dequant_q8_0_len, dequant_q8_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1900. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q2_K], "dequant_q2_k", dequant_q2_k_len, dequant_q2_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1901. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q3_K], "dequant_q3_k", dequant_q3_k_len, dequant_q3_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1902. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_K], "dequant_q4_k", dequant_q4_k_len, dequant_q4_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1903. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_K], "dequant_q5_k", dequant_q5_k_len, dequant_q5_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1904. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q6_K], "dequant_q6_k", dequant_q6_k_len, dequant_q6_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1905. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_S], "dequant_iq1_s", dequant_iq1_s_len, dequant_iq1_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1906. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_M], "dequant_iq1_m", dequant_iq1_m_len, dequant_iq1_m_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1907. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XXS], "dequant_iq2_xxs", dequant_iq2_xxs_len, dequant_iq2_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1908. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XS], "dequant_iq2_xs", dequant_iq2_xs_len, dequant_iq2_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1909. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_S], "dequant_iq2_s", dequant_iq2_s_len, dequant_iq2_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1910. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_XXS], "dequant_iq3_xxs", dequant_iq3_xxs_len, dequant_iq3_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1911. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_S], "dequant_iq3_s", dequant_iq3_s_len, dequant_iq3_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1912. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_XS], "dequant_iq4_xs", dequant_iq4_xs_len, dequant_iq4_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1913. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_NL], "dequant_iq4_nl", dequant_iq4_nl_len, dequant_iq4_nl_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1914. // get_rows
  1915. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F32 ], "get_rows_f32", get_rows_f32_len, get_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1916. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F16 ], "get_rows_f16", get_rows_f16_len, get_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1917. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_0], "get_rows_q4_0", get_rows_q4_0_len, get_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1918. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_1], "get_rows_q4_1", get_rows_q4_1_len, get_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1919. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_0], "get_rows_q5_0", get_rows_q5_0_len, get_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1920. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_1], "get_rows_q5_1", get_rows_q5_1_len, get_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1921. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q8_0], "get_rows_q8_0", get_rows_q8_0_len, get_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1922. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_S], "get_rows_iq1_s", get_rows_iq1_s_len, get_rows_iq1_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1923. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_M], "get_rows_iq1_m", get_rows_iq1_m_len, get_rows_iq1_m_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1924. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs", get_rows_iq2_xxs_len, get_rows_iq2_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1925. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs", get_rows_iq2_xs_len, get_rows_iq2_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1926. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_S], "get_rows_iq2_s", get_rows_iq2_s_len, get_rows_iq2_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1927. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs", get_rows_iq3_xxs_len, get_rows_iq3_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1928. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_S], "get_rows_iq3_s", get_rows_iq3_s_len, get_rows_iq3_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1929. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs", get_rows_iq4_xs_len, get_rows_iq4_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1930. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl", get_rows_iq4_nl_len, get_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1931. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F32 ], "get_rows_f32_f32", get_rows_f32_f32_len, get_rows_f32_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1932. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F16 ], "get_rows_f16_f32", get_rows_f16_f32_len, get_rows_f16_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1933. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_0], "get_rows_q4_0_f32", get_rows_q4_0_f32_len, get_rows_q4_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1934. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_1], "get_rows_q4_1_f32", get_rows_q4_1_f32_len, get_rows_q4_1_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1935. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_0], "get_rows_q5_0_f32", get_rows_q5_0_f32_len, get_rows_q5_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1936. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_1], "get_rows_q5_1_f32", get_rows_q5_1_f32_len, get_rows_q5_1_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1937. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q8_0], "get_rows_q8_0_f32", get_rows_q8_0_f32_len, get_rows_q8_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1938. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_S], "get_rows_iq1_s_f32", get_rows_iq1_s_f32_len, get_rows_iq1_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1939. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_M], "get_rows_iq1_m_f32", get_rows_iq1_m_f32_len, get_rows_iq1_m_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1940. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs_f32", get_rows_iq2_xxs_f32_len, get_rows_iq2_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1941. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs_f32", get_rows_iq2_xs_f32_len, get_rows_iq2_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1942. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_S], "get_rows_iq2_s_f32", get_rows_iq2_s_f32_len, get_rows_iq2_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1943. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs_f32", get_rows_iq3_xxs_f32_len, get_rows_iq3_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1944. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_S], "get_rows_iq3_s_f32", get_rows_iq3_s_f32_len, get_rows_iq3_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1945. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs_f32", get_rows_iq4_xs_f32_len, get_rows_iq4_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1946. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl_f32", get_rows_iq4_nl_f32_len, get_rows_iq4_nl_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1947. ggml_vk_create_pipeline(device, device->pipeline_matmul_split_k_reduce, "split_k_reduce", split_k_reduce_len, split_k_reduce_data, "main", 2, 2 * sizeof(uint32_t), {256 * 4, 1, 1}, {}, 1);
  1948. ggml_vk_create_pipeline(device, 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);
  1949. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_nc_f16_f32, "mul_mat_vec_nc_f16_f32", mul_mat_vec_nc_f16_f32_len, mul_mat_vec_nc_f16_f32_data, "main", 3, 7 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  1950. ggml_vk_create_pipeline(device, device->pipeline_norm_f32, "norm_f32", norm_f32_len, norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1951. ggml_vk_create_pipeline(device, device->pipeline_group_norm_f32, "group_norm_f32", group_norm_f32_len, group_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1952. ggml_vk_create_pipeline(device, 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);
  1953. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_back_f32, "rms_norm_back_f32", rms_norm_back_f32_len, rms_norm_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1954. ggml_vk_create_pipeline(device, device->pipeline_l2_norm_f32, "l2_norm_f32", l2_norm_f32_len, l2_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1955. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_f32, "cpy_f32_f32", cpy_f32_f32_len, cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1956. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_f16, "cpy_f32_f16", cpy_f32_f16_len, cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1957. ggml_vk_create_pipeline(device, device->pipeline_cpy_f16_f16, "cpy_f16_f16", cpy_f16_f16_len, cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1958. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f32, "contig_cpy_f32_f32", contig_cpy_f32_f32_len, contig_cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1959. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f16, "contig_cpy_f32_f16", contig_cpy_f32_f16_len, contig_cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1960. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f16, "contig_cpy_f16_f16", contig_cpy_f16_f16_len, contig_cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1961. if (device->float_controls_rte_fp16) {
  1962. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  1963. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  1964. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_rte_len, cpy_f32_q5_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  1965. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_rte_len, cpy_f32_q5_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  1966. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_rte_len, cpy_f32_q8_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  1967. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_rte_len, cpy_f32_iq4_nl_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  1968. } else {
  1969. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_len, cpy_f32_q4_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  1970. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_len, cpy_f32_q4_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  1971. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_len, cpy_f32_q5_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  1972. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_len, cpy_f32_q5_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  1973. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_len, cpy_f32_q8_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  1974. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_len, cpy_f32_iq4_nl_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  1975. }
  1976. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_0], "cpy_q4_0_f32", cpy_q4_0_f32_len, cpy_q4_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  1977. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_1], "cpy_q4_1_f32", cpy_q4_1_f32_len, cpy_q4_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  1978. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_0], "cpy_q5_0_f32", cpy_q5_0_f32_len, cpy_q5_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  1979. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_1], "cpy_q5_1_f32", cpy_q5_1_f32_len, cpy_q5_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  1980. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q8_0], "cpy_q8_0_f32", cpy_q8_0_f32_len, cpy_q8_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  1981. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_IQ4_NL], "cpy_iq4_nl_f32", cpy_iq4_nl_f32_len, cpy_iq4_nl_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  1982. ggml_vk_create_pipeline(device, device->pipeline_add_f32, "add_f32", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1983. ggml_vk_create_pipeline(device, device->pipeline_add_f32_norepeat, "add_f32_norepeat", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1984. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16, "add_f16_f32_f16", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1985. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16_norepeat, "add_f16_f32_f16_norepeat", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1986. ggml_vk_create_pipeline(device, device->pipeline_acc_f32, "acc_f32", acc_f32_len, acc_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1987. ggml_vk_create_pipeline(device, device->pipeline_sub_f32, "sub_f32", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1988. ggml_vk_create_pipeline(device, device->pipeline_sub_f32_norepeat, "sub_f32_norepeat", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1989. ggml_vk_create_pipeline(device, device->pipeline_mul_f32, "mul_f32", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1990. ggml_vk_create_pipeline(device, device->pipeline_mul_f32_norepeat, "mul_f32_norepeat", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1991. ggml_vk_create_pipeline(device, device->pipeline_div_f32, "div_f32", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1992. ggml_vk_create_pipeline(device, device->pipeline_div_f32_norepeat, "div_f32_norepeat", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1993. ggml_vk_create_pipeline(device, device->pipeline_concat_f32, "concat_f32", concat_f32_len, concat_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1994. ggml_vk_create_pipeline(device, device->pipeline_concat_f16, "concat_f16", concat_f16_len, concat_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1995. ggml_vk_create_pipeline(device, device->pipeline_concat_i32, "concat_i32", concat_i32_len, concat_i32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1996. ggml_vk_create_pipeline(device, device->pipeline_upscale_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {}, 1);
  1997. ggml_vk_create_pipeline(device, device->pipeline_scale_f32, "scale_f32", scale_f32_len, scale_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1998. ggml_vk_create_pipeline(device, device->pipeline_sqr_f32, "sqr_f32", sqr_f32_len, sqr_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1999. ggml_vk_create_pipeline(device, device->pipeline_sin_f32, "sin_f32", sin_f32_len, sin_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2000. ggml_vk_create_pipeline(device, device->pipeline_cos_f32, "cos_f32", cos_f32_len, cos_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2001. ggml_vk_create_pipeline(device, device->pipeline_clamp_f32, "clamp_f32", clamp_f32_len, clamp_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2002. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2003. ggml_vk_create_pipeline(device, device->pipeline_repeat_f32, "repeat_f32", repeat_f32_len, repeat_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2004. ggml_vk_create_pipeline(device, device->pipeline_repeat_back_f32, "repeat_back_f32", repeat_back_f32_len, repeat_back_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2005. ggml_vk_create_pipeline(device, device->pipeline_gelu_f32, "gelu_f32", gelu_f32_len, gelu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2006. ggml_vk_create_pipeline(device, device->pipeline_gelu_quick_f32, "gelu_quick_f32", gelu_quick_f32_len, gelu_quick_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2007. ggml_vk_create_pipeline(device, device->pipeline_silu_f32, "silu_f32", silu_f32_len, silu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2008. ggml_vk_create_pipeline(device, device->pipeline_silu_back_f32, "silu_back_f32", silu_back_f32_len, silu_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2009. ggml_vk_create_pipeline(device, device->pipeline_relu_f32, "relu_f32", relu_f32_len, relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2010. ggml_vk_create_pipeline(device, device->pipeline_leaky_relu_f32, "leaky_relu_f32", leaky_relu_f32_len, leaky_relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2011. ggml_vk_create_pipeline(device, device->pipeline_tanh_f32, "tanh_f32", tanh_f32_len, tanh_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2012. ggml_vk_create_pipeline(device, device->pipeline_sigmoid_f32, "sigmoid_f32", sigmoid_f32_len, sigmoid_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2013. ggml_vk_create_pipeline(device, device->pipeline_diag_mask_inf_f32, "diag_mask_inf_f32", diag_mask_inf_f32_len, diag_mask_inf_f32_data, "main", 2, sizeof(vk_op_diag_mask_push_constants), {1, 512, 1}, {}, 1, true);
  2014. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2015. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2016. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2017. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2018. ggml_vk_create_pipeline(device, device->pipeline_soft_max_back_f32, "soft_max_back_f32", soft_max_back_f32_len, soft_max_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2019. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2020. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32, "rope_neox_f32", rope_neox_f32_len, rope_neox_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2021. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f32, "rope_multi_f32", rope_multi_f32_len, rope_multi_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2022. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f32, "rope_vision_f32", rope_vision_f32_len, rope_vision_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2023. if (device->float_controls_rte_fp16) {
  2024. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_rte_len, rope_norm_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2025. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_rte_len, rope_neox_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2026. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_rte_len, rope_multi_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2027. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_rte_len, rope_vision_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2028. } else {
  2029. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2030. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_len, rope_neox_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2031. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_len, rope_multi_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2032. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_len, rope_vision_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2033. }
  2034. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  2035. ggml_vk_create_pipeline(device, device->pipeline_argmax_f32, "argmax_f32", argmax_f32_len, argmax_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2036. ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2037. ggml_vk_create_pipeline(device, device->pipeline_count_equal_i32, "count_equal_i32", count_equal_i32_len, count_equal_i32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, { device->subgroup_size }, 1);
  2038. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2039. if (device->float_controls_rte_fp16) {
  2040. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2041. } else {
  2042. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2043. }
  2044. ggml_vk_create_pipeline(device, device->pipeline_timestep_embedding_f32, "timestep_embedding_f32", timestep_embedding_f32_len, timestep_embedding_f32_data, "main", 2, sizeof(vk_op_timestep_embedding_push_constants), {256, 1, 1}, {}, 1);
  2045. ggml_vk_create_pipeline(device, device->pipeline_pool2d_f32, "pool2d_f32", pool2d_f32_len, pool2d_f32_data, "main", 2, sizeof(vk_op_pool2d_push_constants), {512, 1, 1}, {}, 1);
  2046. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv6_f32, "rwkv_wkv6_f32", rwkv_wkv6_f32_len, rwkv_wkv6_f32_data, "main", 7, sizeof(vk_op_rwkv_wkv6_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2047. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv7_f32, "rwkv_wkv7_f32", rwkv_wkv7_f32_len, rwkv_wkv7_f32_data, "main", 8, sizeof(vk_op_rwkv_wkv7_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2048. ggml_vk_create_pipeline(device, device->pipeline_opt_step_adamw_f32, "opt_step_adamw_f32", opt_step_adamw_f32_len, opt_step_adamw_f32_data, "main", 5, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2049. for (auto &c : compiles) {
  2050. c.wait();
  2051. }
  2052. device->need_compiles = false;
  2053. }
  2054. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2055. static vk_device ggml_vk_get_device(size_t idx) {
  2056. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2057. if (vk_instance.devices[idx] == nullptr) {
  2058. VK_LOG_DEBUG("Initializing new vk_device");
  2059. vk_device device = std::make_shared<vk_device_struct>();
  2060. vk_instance.devices[idx] = device;
  2061. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2062. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2063. #endif
  2064. #ifdef GGML_VULKAN_PERF
  2065. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2066. #endif
  2067. size_t dev_num = vk_instance.device_indices[idx];
  2068. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2069. if (dev_num >= physical_devices.size()) {
  2070. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2071. throw std::runtime_error("Device not found");
  2072. }
  2073. device->physical_device = physical_devices[dev_num];
  2074. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2075. device->architecture = get_device_architecture(device->physical_device);
  2076. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2077. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2078. bool fp16_storage = false;
  2079. bool fp16_compute = false;
  2080. bool maintenance4_support = false;
  2081. bool sm_builtins = false;
  2082. bool amd_shader_core_properties2 = false;
  2083. bool pipeline_robustness = false;
  2084. bool coopmat2_support = false;
  2085. device->coopmat_support = false;
  2086. for (const auto& properties : ext_props) {
  2087. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2088. maintenance4_support = true;
  2089. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2090. fp16_storage = true;
  2091. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2092. fp16_compute = true;
  2093. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2094. sm_builtins = true;
  2095. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2096. amd_shader_core_properties2 = true;
  2097. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2098. pipeline_robustness = true;
  2099. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2100. device->subgroup_size_control = true;
  2101. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2102. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2103. device->coopmat_support = true;
  2104. device->coopmat_m = 0;
  2105. device->coopmat_n = 0;
  2106. device->coopmat_k = 0;
  2107. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2108. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2109. coopmat2_support = true;
  2110. }
  2111. }
  2112. vk::PhysicalDeviceProperties2 props2;
  2113. vk::PhysicalDeviceMaintenance3Properties props3;
  2114. vk::PhysicalDeviceMaintenance4Properties props4;
  2115. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2116. vk::PhysicalDeviceDriverProperties driver_props;
  2117. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2118. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2119. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2120. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2121. props2.pNext = &props3;
  2122. props3.pNext = &subgroup_props;
  2123. subgroup_props.pNext = &driver_props;
  2124. driver_props.pNext = &vk12_props;
  2125. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2126. if (maintenance4_support) {
  2127. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2128. last_struct = (VkBaseOutStructure *)&props4;
  2129. }
  2130. if (sm_builtins) {
  2131. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2132. last_struct = (VkBaseOutStructure *)&sm_props;
  2133. }
  2134. if (amd_shader_core_properties2) {
  2135. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2136. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2137. }
  2138. if (device->subgroup_size_control) {
  2139. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2140. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2141. }
  2142. #if defined(VK_NV_cooperative_matrix2)
  2143. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2144. if (coopmat2_support) {
  2145. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2146. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2147. }
  2148. #endif
  2149. device->physical_device.getProperties2(&props2);
  2150. device->properties = props2.properties;
  2151. device->vendor_id = device->properties.vendorID;
  2152. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2153. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2154. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2155. } else if (maintenance4_support) {
  2156. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2157. } else {
  2158. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2159. }
  2160. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2161. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2162. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2163. } else {
  2164. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2165. device->suballocation_block_size = 1024*1024*1024;
  2166. }
  2167. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2168. device->subgroup_size = subgroup_props.subgroupSize;
  2169. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2170. if (sm_builtins) {
  2171. device->shader_core_count = sm_props.shaderSMCount;
  2172. } else if (amd_shader_core_properties2) {
  2173. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2174. } else {
  2175. device->shader_core_count = 0;
  2176. }
  2177. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2178. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2179. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2180. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2181. device->coopmat_support = false;
  2182. }
  2183. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2184. // Try to find a non-graphics compute queue and transfer-focused queues
  2185. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2186. 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);
  2187. const float priorities[] = { 1.0f, 1.0f };
  2188. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2189. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2190. if (compute_queue_family_index != transfer_queue_family_index) {
  2191. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2192. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2193. } else if(!device->single_queue) {
  2194. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2195. } else {
  2196. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2197. }
  2198. vk::DeviceCreateInfo device_create_info;
  2199. std::vector<const char *> device_extensions;
  2200. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2201. VkPhysicalDeviceFeatures2 device_features2;
  2202. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2203. device_features2.pNext = nullptr;
  2204. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2205. VkPhysicalDeviceVulkan11Features vk11_features;
  2206. vk11_features.pNext = nullptr;
  2207. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2208. device_features2.pNext = &vk11_features;
  2209. VkPhysicalDeviceVulkan12Features vk12_features;
  2210. vk12_features.pNext = nullptr;
  2211. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2212. vk11_features.pNext = &vk12_features;
  2213. last_struct = (VkBaseOutStructure *)&vk12_features;
  2214. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2215. pl_robustness_features.pNext = nullptr;
  2216. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2217. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2218. if (pipeline_robustness) {
  2219. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2220. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2221. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2222. }
  2223. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2224. subgroup_size_control_features.pNext = nullptr;
  2225. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2226. subgroup_size_control_features.computeFullSubgroups = false;
  2227. subgroup_size_control_features.subgroupSizeControl = false;
  2228. if (device->subgroup_size_control) {
  2229. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2230. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2231. }
  2232. #if defined(VK_KHR_cooperative_matrix)
  2233. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2234. coopmat_features.pNext = nullptr;
  2235. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2236. coopmat_features.cooperativeMatrix = VK_FALSE;
  2237. if (device->coopmat_support) {
  2238. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2239. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2240. }
  2241. #endif
  2242. #if defined(VK_NV_cooperative_matrix2)
  2243. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2244. coopmat2_features.pNext = nullptr;
  2245. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2246. if (coopmat2_support) {
  2247. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2248. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2249. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2250. }
  2251. #endif
  2252. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2253. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2254. if (maintenance4_support) {
  2255. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2256. last_struct = (VkBaseOutStructure *)&maint4_features;
  2257. device_extensions.push_back("VK_KHR_maintenance4");
  2258. }
  2259. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2260. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2261. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2262. if (device->subgroup_size_control) {
  2263. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2264. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2265. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2266. }
  2267. device->subgroup_size_control = device->subgroup_size_control &&
  2268. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2269. subgroup_size_control_features.subgroupSizeControl;
  2270. if (device->subgroup_size_control) {
  2271. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2272. }
  2273. #if defined(VK_KHR_cooperative_matrix)
  2274. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2275. #endif
  2276. if (coopmat2_support) {
  2277. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2278. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2279. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2280. coopmat2_features.cooperativeMatrixReductions &&
  2281. coopmat2_features.cooperativeMatrixConversions &&
  2282. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2283. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2284. coopmat2_features.cooperativeMatrixBlockLoads &&
  2285. vk12_features.bufferDeviceAddress) {
  2286. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2287. uint32_t count = 0;
  2288. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2289. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2290. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2291. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2292. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2293. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2294. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2295. flexible_dimensions.resize(count, empty_prop);
  2296. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2297. bool found_fp16_128 = false,
  2298. found_fp16_256 = false,
  2299. found_fp32_128 = false,
  2300. found_fp32_256 = false;
  2301. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2302. // with 32x16x16 and 256 with 32x32x16.
  2303. for (auto &prop : flexible_dimensions) {
  2304. if (prop.saturatingAccumulation == VK_FALSE &&
  2305. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2306. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2307. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2308. if (prop.workgroupInvocations == 128 &&
  2309. prop.MGranularity <= 32 &&
  2310. prop.NGranularity <= 16 &&
  2311. prop.KGranularity <= 16) {
  2312. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2313. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2314. found_fp16_128 = true;
  2315. }
  2316. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2317. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2318. found_fp32_128 = true;
  2319. }
  2320. }
  2321. if (prop.workgroupInvocations == 256 &&
  2322. prop.MGranularity <= 32 &&
  2323. prop.NGranularity <= 32 &&
  2324. prop.KGranularity <= 16) {
  2325. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2326. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2327. found_fp16_256 = true;
  2328. }
  2329. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2330. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2331. found_fp32_256 = true;
  2332. }
  2333. }
  2334. }
  2335. }
  2336. if (found_fp16_128 && found_fp16_256 &&
  2337. found_fp32_128 && found_fp32_256 &&
  2338. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2339. device->coopmat2 = true;
  2340. }
  2341. }
  2342. #endif
  2343. }
  2344. if (!vk11_features.storageBuffer16BitAccess) {
  2345. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2346. throw std::runtime_error("Unsupported device");
  2347. }
  2348. device_extensions.push_back("VK_KHR_16bit_storage");
  2349. #ifdef GGML_VULKAN_VALIDATE
  2350. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2351. #endif
  2352. if (device->fp16) {
  2353. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2354. }
  2355. #if defined(VK_KHR_cooperative_matrix)
  2356. if (device->coopmat_support) {
  2357. // Query supported shapes
  2358. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2359. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2360. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2361. uint32_t cm_props_num;
  2362. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2363. cm_props.resize(cm_props_num);
  2364. for (auto& prop : cm_props) {
  2365. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2366. }
  2367. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2368. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2369. for (auto& prop : cm_props) {
  2370. VK_LOG_DEBUG("ggml_vulkan: M: " << prop.MSize << " N: " << prop.NSize << " K: " << prop.KSize << " A: " << vk::to_string((vk::ComponentTypeKHR)prop.AType) << " B: " << vk::to_string((vk::ComponentTypeKHR)prop.BType) << " C: " << vk::to_string((vk::ComponentTypeKHR)prop.CType) << " Result: " << vk::to_string((vk::ComponentTypeKHR)prop.ResultType) << " saturatingAccumulation: " << prop.saturatingAccumulation << " scope: " << vk::to_string((vk::ScopeKHR)prop.scope));
  2371. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2372. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2373. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2374. ) {
  2375. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2376. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2377. // coopmat sizes not set yet
  2378. if (device->coopmat_m == 0) {
  2379. device->coopmat_acc_f32_support = true;
  2380. device->coopmat_m = prop.MSize;
  2381. device->coopmat_n = prop.NSize;
  2382. device->coopmat_k = prop.KSize;
  2383. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2384. // Only enable if shape is identical
  2385. device->coopmat_acc_f32_support = true;
  2386. }
  2387. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2388. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2389. // coopmat sizes not set yet
  2390. if (device->coopmat_m == 0) {
  2391. device->coopmat_acc_f16_support = true;
  2392. device->coopmat_m = prop.MSize;
  2393. device->coopmat_n = prop.NSize;
  2394. device->coopmat_k = prop.KSize;
  2395. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2396. // Only enable if shape is identical
  2397. device->coopmat_acc_f16_support = true;
  2398. }
  2399. }
  2400. }
  2401. }
  2402. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2403. // No suitable matmul mode found
  2404. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2405. device->coopmat_support = false;
  2406. }
  2407. }
  2408. if (device->coopmat_support) {
  2409. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2410. }
  2411. #endif
  2412. device->name = GGML_VK_NAME + std::to_string(idx);
  2413. device_create_info = {
  2414. vk::DeviceCreateFlags(),
  2415. device_queue_create_infos,
  2416. {},
  2417. device_extensions
  2418. };
  2419. device_create_info.setPNext(&device_features2);
  2420. device->device = device->physical_device.createDevice(device_create_info);
  2421. // Queues
  2422. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2423. // Shaders
  2424. // Disable matmul tile sizes early if performance low or not supported
  2425. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2426. switch (device->vendor_id) {
  2427. #ifndef GGML_VULKAN_RUN_TESTS
  2428. case VK_VENDOR_ID_AMD:
  2429. case VK_VENDOR_ID_INTEL:
  2430. device->mul_mat_l[i] = false;
  2431. device->mul_mat_m[i] = true;
  2432. device->mul_mat_s[i] = true;
  2433. device->mul_mat_id_l[i] = false;
  2434. device->mul_mat_id_m[i] = true;
  2435. device->mul_mat_id_s[i] = true;
  2436. break;
  2437. case VK_VENDOR_ID_APPLE:
  2438. device->mul_mat_l[i] = false;
  2439. device->mul_mat_m[i] = true;
  2440. device->mul_mat_s[i] = false;
  2441. device->mul_mat_id_l[i] = false;
  2442. device->mul_mat_id_m[i] = true;
  2443. device->mul_mat_id_s[i] = false;
  2444. break;
  2445. #endif
  2446. default:
  2447. device->mul_mat_l[i] = true;
  2448. device->mul_mat_m[i] = true;
  2449. device->mul_mat_s[i] = true;
  2450. device->mul_mat_id_l[i] = true;
  2451. device->mul_mat_id_m[i] = true;
  2452. device->mul_mat_id_s[i] = true;
  2453. break;
  2454. }
  2455. }
  2456. ggml_vk_load_shaders(device);
  2457. if (!device->single_queue) {
  2458. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  2459. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  2460. } else {
  2461. // TODO: Use pointer or reference to avoid copy
  2462. device->transfer_queue = device->compute_queue;
  2463. }
  2464. device->buffer_type = {
  2465. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  2466. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  2467. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  2468. };
  2469. device->fence = device->device.createFence({});
  2470. device->idx = idx;
  2471. return device;
  2472. }
  2473. return vk_instance.devices[idx];
  2474. }
  2475. static void ggml_vk_print_gpu_info(size_t idx) {
  2476. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2477. size_t dev_num = vk_instance.device_indices[idx];
  2478. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  2479. GGML_ASSERT(vk_instance_initialized);
  2480. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2481. if (dev_num >= devices.size()) {
  2482. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2483. throw std::runtime_error("Device not found");
  2484. }
  2485. vk::PhysicalDevice physical_device = devices[dev_num];
  2486. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  2487. vk::PhysicalDeviceProperties2 props2;
  2488. vk::PhysicalDeviceMaintenance3Properties props3;
  2489. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2490. vk::PhysicalDeviceDriverProperties driver_props;
  2491. props2.pNext = &props3;
  2492. props3.pNext = &subgroup_props;
  2493. subgroup_props.pNext = &driver_props;
  2494. physical_device.getProperties2(&props2);
  2495. vk_device_architecture arch = get_device_architecture(physical_device);
  2496. uint32_t default_subgroup_size = get_subgroup_size("", arch);
  2497. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  2498. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2499. bool fp16_storage = false;
  2500. bool fp16_compute = false;
  2501. bool coopmat_support = false;
  2502. bool coopmat2_support = false;
  2503. for (auto properties : ext_props) {
  2504. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2505. fp16_storage = true;
  2506. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2507. fp16_compute = true;
  2508. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2509. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2510. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2511. coopmat_support = true;
  2512. #endif
  2513. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2514. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2515. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2516. coopmat2_support = true;
  2517. #endif
  2518. }
  2519. }
  2520. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  2521. if (!ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture)) {
  2522. coopmat_support = false;
  2523. }
  2524. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  2525. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  2526. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2527. vk::PhysicalDeviceFeatures device_features = physical_device.getFeatures();
  2528. VkPhysicalDeviceFeatures2 device_features2;
  2529. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2530. device_features2.pNext = nullptr;
  2531. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2532. VkPhysicalDeviceVulkan11Features vk11_features;
  2533. vk11_features.pNext = nullptr;
  2534. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2535. device_features2.pNext = &vk11_features;
  2536. VkPhysicalDeviceVulkan12Features vk12_features;
  2537. vk12_features.pNext = nullptr;
  2538. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2539. vk11_features.pNext = &vk12_features;
  2540. // Pointer to the last chain element
  2541. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_features;
  2542. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2543. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2544. coopmat_features.pNext = nullptr;
  2545. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2546. coopmat_features.cooperativeMatrix = VK_FALSE;
  2547. if (coopmat_support) {
  2548. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2549. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2550. }
  2551. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  2552. fp16 = fp16 && vk12_features.shaderFloat16;
  2553. coopmat_support = coopmat_support && coopmat_features.cooperativeMatrix;
  2554. #endif
  2555. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  2556. std::string device_name = props2.properties.deviceName.data();
  2557. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | matrix cores: %s\n",
  2558. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  2559. props2.properties.limits.maxComputeSharedMemorySize, matrix_cores.c_str());
  2560. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  2561. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  2562. }
  2563. }
  2564. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2565. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2566. static void ggml_vk_instance_init() {
  2567. if (vk_instance_initialized) {
  2568. return;
  2569. }
  2570. VK_LOG_DEBUG("ggml_vk_instance_init()");
  2571. uint32_t api_version = vk::enumerateInstanceVersion();
  2572. if (api_version < VK_API_VERSION_1_2) {
  2573. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  2574. GGML_ABORT("fatal error");
  2575. }
  2576. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  2577. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  2578. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  2579. #ifdef __APPLE__
  2580. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  2581. #endif
  2582. std::vector<const char*> layers;
  2583. if (validation_ext) {
  2584. layers.push_back("VK_LAYER_KHRONOS_validation");
  2585. }
  2586. std::vector<const char*> extensions;
  2587. if (validation_ext) {
  2588. extensions.push_back("VK_EXT_validation_features");
  2589. }
  2590. #ifdef __APPLE__
  2591. if (portability_enumeration_ext) {
  2592. extensions.push_back("VK_KHR_portability_enumeration");
  2593. }
  2594. #endif
  2595. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  2596. #ifdef __APPLE__
  2597. if (portability_enumeration_ext) {
  2598. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  2599. }
  2600. #endif
  2601. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  2602. vk::ValidationFeaturesEXT validation_features;
  2603. if (validation_ext) {
  2604. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  2605. validation_features = {
  2606. features_enable,
  2607. {},
  2608. };
  2609. validation_features.setPNext(nullptr);
  2610. instance_create_info.setPNext(&validation_features);
  2611. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  2612. }
  2613. vk_instance.instance = vk::createInstance(instance_create_info);
  2614. vk_instance_initialized = true;
  2615. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  2616. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  2617. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  2618. if (devices_env != nullptr) {
  2619. std::string devices(devices_env);
  2620. std::replace(devices.begin(), devices.end(), ',', ' ');
  2621. std::stringstream ss(devices);
  2622. size_t tmp;
  2623. while (ss >> tmp) {
  2624. if(tmp >= num_available_devices) {
  2625. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  2626. throw std::runtime_error("Invalid Vulkan device index");
  2627. }
  2628. vk_instance.device_indices.push_back(tmp);
  2629. }
  2630. } else {
  2631. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2632. // Make sure at least one device exists
  2633. if (devices.empty()) {
  2634. std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
  2635. return;
  2636. }
  2637. // Default to using all dedicated GPUs
  2638. for (size_t i = 0; i < devices.size(); i++) {
  2639. vk::PhysicalDeviceProperties2 new_props;
  2640. vk::PhysicalDeviceDriverProperties new_driver;
  2641. vk::PhysicalDeviceIDProperties new_id;
  2642. new_props.pNext = &new_driver;
  2643. new_driver.pNext = &new_id;
  2644. devices[i].getProperties2(&new_props);
  2645. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  2646. // Check if there are two physical devices corresponding to the same GPU
  2647. auto old_device = std::find_if(
  2648. vk_instance.device_indices.begin(),
  2649. vk_instance.device_indices.end(),
  2650. [&devices, &new_id](const size_t k){
  2651. vk::PhysicalDeviceProperties2 old_props;
  2652. vk::PhysicalDeviceIDProperties old_id;
  2653. old_props.pNext = &old_id;
  2654. devices[k].getProperties2(&old_props);
  2655. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  2656. }
  2657. );
  2658. if (old_device == vk_instance.device_indices.end()) {
  2659. vk_instance.device_indices.push_back(i);
  2660. } else {
  2661. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  2662. // This can cause error when splitting layers aross the devices, need to keep only 1
  2663. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  2664. vk::PhysicalDeviceProperties2 old_props;
  2665. vk::PhysicalDeviceDriverProperties old_driver;
  2666. old_props.pNext = &old_driver;
  2667. devices[*old_device].getProperties2(&old_props);
  2668. std::map<vk::DriverId, int> driver_priorities {};
  2669. int old_priority = std::numeric_limits<int>::max();
  2670. int new_priority = std::numeric_limits<int>::max();
  2671. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  2672. // Smaller number -> higher priority
  2673. switch (old_props.properties.vendorID) {
  2674. case VK_VENDOR_ID_AMD:
  2675. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  2676. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  2677. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  2678. break;
  2679. case VK_VENDOR_ID_INTEL:
  2680. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  2681. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  2682. break;
  2683. case VK_VENDOR_ID_NVIDIA:
  2684. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  2685. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  2686. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  2687. #endif
  2688. break;
  2689. }
  2690. if (driver_priorities.count(old_driver.driverID)) {
  2691. old_priority = driver_priorities[old_driver.driverID];
  2692. }
  2693. if (driver_priorities.count(new_driver.driverID)) {
  2694. new_priority = driver_priorities[new_driver.driverID];
  2695. }
  2696. if (new_priority < old_priority) {
  2697. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  2698. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  2699. vk_instance.device_indices.push_back(i);
  2700. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  2701. }
  2702. else {
  2703. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  2704. }
  2705. }
  2706. }
  2707. }
  2708. // If no dedicated GPUs found, fall back to GPU 0
  2709. if (vk_instance.device_indices.empty()) {
  2710. vk_instance.device_indices.push_back(0);
  2711. }
  2712. }
  2713. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  2714. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  2715. ggml_vk_print_gpu_info(i);
  2716. }
  2717. }
  2718. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  2719. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  2720. ggml_vk_instance_init();
  2721. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2722. ctx->name = GGML_VK_NAME + std::to_string(idx);
  2723. ctx->device = ggml_vk_get_device(idx);
  2724. ctx->semaphore_idx = 0;
  2725. ctx->event_idx = 0;
  2726. ctx->prealloc_size_x = 0;
  2727. ctx->prealloc_size_y = 0;
  2728. ctx->prealloc_size_split_k = 0;
  2729. ctx->fence = ctx->device->device.createFence({});
  2730. #ifdef GGML_VULKAN_CHECK_RESULTS
  2731. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  2732. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  2733. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  2734. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  2735. #endif
  2736. }
  2737. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  2738. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  2739. switch (type) {
  2740. case GGML_TYPE_F32:
  2741. case GGML_TYPE_Q4_0:
  2742. case GGML_TYPE_Q4_1:
  2743. case GGML_TYPE_Q5_0:
  2744. case GGML_TYPE_Q5_1:
  2745. case GGML_TYPE_Q8_0:
  2746. case GGML_TYPE_Q2_K:
  2747. case GGML_TYPE_Q3_K:
  2748. case GGML_TYPE_Q4_K:
  2749. case GGML_TYPE_Q5_K:
  2750. case GGML_TYPE_Q6_K:
  2751. case GGML_TYPE_IQ1_S:
  2752. case GGML_TYPE_IQ1_M:
  2753. case GGML_TYPE_IQ2_XXS:
  2754. case GGML_TYPE_IQ2_XS:
  2755. case GGML_TYPE_IQ2_S:
  2756. case GGML_TYPE_IQ3_XXS:
  2757. case GGML_TYPE_IQ3_S:
  2758. case GGML_TYPE_IQ4_XS:
  2759. case GGML_TYPE_IQ4_NL:
  2760. break;
  2761. default:
  2762. return nullptr;
  2763. }
  2764. return ctx->device->pipeline_dequant[type];
  2765. }
  2766. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  2767. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  2768. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2769. return ctx->device->pipeline_matmul_f32;
  2770. }
  2771. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  2772. return ctx->device->pipeline_matmul_f32_f16;
  2773. }
  2774. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2775. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2776. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  2777. }
  2778. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2779. return ctx->device->pipeline_matmul_f16.f16acc;
  2780. }
  2781. } else {
  2782. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2783. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  2784. }
  2785. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2786. return ctx->device->pipeline_matmul_f16.f32acc;
  2787. }
  2788. }
  2789. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  2790. return nullptr;
  2791. }
  2792. switch (src0_type) {
  2793. case GGML_TYPE_Q4_0:
  2794. case GGML_TYPE_Q4_1:
  2795. case GGML_TYPE_Q5_0:
  2796. case GGML_TYPE_Q5_1:
  2797. case GGML_TYPE_Q8_0:
  2798. case GGML_TYPE_Q2_K:
  2799. case GGML_TYPE_Q3_K:
  2800. case GGML_TYPE_Q4_K:
  2801. case GGML_TYPE_Q5_K:
  2802. case GGML_TYPE_Q6_K:
  2803. case GGML_TYPE_IQ1_S:
  2804. case GGML_TYPE_IQ1_M:
  2805. case GGML_TYPE_IQ2_XXS:
  2806. case GGML_TYPE_IQ2_XS:
  2807. case GGML_TYPE_IQ2_S:
  2808. case GGML_TYPE_IQ3_XXS:
  2809. case GGML_TYPE_IQ3_S:
  2810. case GGML_TYPE_IQ4_XS:
  2811. case GGML_TYPE_IQ4_NL:
  2812. break;
  2813. default:
  2814. return nullptr;
  2815. }
  2816. if (ctx->device->coopmat2) {
  2817. assert(src1_type == GGML_TYPE_F16);
  2818. return ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc;
  2819. }
  2820. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  2821. }
  2822. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type, uint32_t num_cols) {
  2823. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  2824. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  2825. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  2826. switch (a_type) {
  2827. case GGML_TYPE_F32:
  2828. case GGML_TYPE_F16:
  2829. case GGML_TYPE_Q4_0:
  2830. case GGML_TYPE_Q4_1:
  2831. case GGML_TYPE_Q5_0:
  2832. case GGML_TYPE_Q5_1:
  2833. case GGML_TYPE_Q8_0:
  2834. case GGML_TYPE_Q2_K:
  2835. case GGML_TYPE_Q3_K:
  2836. case GGML_TYPE_Q4_K:
  2837. case GGML_TYPE_Q5_K:
  2838. case GGML_TYPE_Q6_K:
  2839. case GGML_TYPE_IQ1_S:
  2840. case GGML_TYPE_IQ1_M:
  2841. case GGML_TYPE_IQ2_XXS:
  2842. case GGML_TYPE_IQ2_XS:
  2843. case GGML_TYPE_IQ2_S:
  2844. case GGML_TYPE_IQ3_XXS:
  2845. case GGML_TYPE_IQ3_S:
  2846. case GGML_TYPE_IQ4_XS:
  2847. case GGML_TYPE_IQ4_NL:
  2848. break;
  2849. default:
  2850. return nullptr;
  2851. }
  2852. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  2853. }
  2854. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_id_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  2855. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  2856. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2857. return ctx->device->pipeline_matmul_id_f32;
  2858. }
  2859. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2860. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2861. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  2862. }
  2863. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2864. return ctx->device->pipeline_matmul_id_f16.f16acc;
  2865. }
  2866. } else {
  2867. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2868. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  2869. }
  2870. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2871. return ctx->device->pipeline_matmul_id_f16.f32acc;
  2872. }
  2873. }
  2874. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  2875. switch (src0_type) {
  2876. case GGML_TYPE_Q4_0:
  2877. case GGML_TYPE_Q4_1:
  2878. case GGML_TYPE_Q5_0:
  2879. case GGML_TYPE_Q5_1:
  2880. case GGML_TYPE_Q8_0:
  2881. case GGML_TYPE_Q2_K:
  2882. case GGML_TYPE_Q3_K:
  2883. case GGML_TYPE_Q4_K:
  2884. case GGML_TYPE_Q5_K:
  2885. case GGML_TYPE_Q6_K:
  2886. case GGML_TYPE_IQ1_S:
  2887. case GGML_TYPE_IQ1_M:
  2888. case GGML_TYPE_IQ2_XXS:
  2889. case GGML_TYPE_IQ2_XS:
  2890. case GGML_TYPE_IQ2_S:
  2891. case GGML_TYPE_IQ3_XXS:
  2892. case GGML_TYPE_IQ3_S:
  2893. case GGML_TYPE_IQ4_XS:
  2894. case GGML_TYPE_IQ4_NL:
  2895. break;
  2896. default:
  2897. return nullptr;
  2898. }
  2899. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  2900. }
  2901. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  2902. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  2903. GGML_ASSERT(b_type == GGML_TYPE_F32);
  2904. switch (a_type) {
  2905. case GGML_TYPE_F32:
  2906. case GGML_TYPE_F16:
  2907. case GGML_TYPE_Q4_0:
  2908. case GGML_TYPE_Q4_1:
  2909. case GGML_TYPE_Q5_0:
  2910. case GGML_TYPE_Q5_1:
  2911. case GGML_TYPE_Q8_0:
  2912. case GGML_TYPE_Q2_K:
  2913. case GGML_TYPE_Q3_K:
  2914. case GGML_TYPE_Q4_K:
  2915. case GGML_TYPE_Q5_K:
  2916. case GGML_TYPE_Q6_K:
  2917. case GGML_TYPE_IQ1_S:
  2918. case GGML_TYPE_IQ1_M:
  2919. case GGML_TYPE_IQ2_XXS:
  2920. case GGML_TYPE_IQ2_XS:
  2921. case GGML_TYPE_IQ2_S:
  2922. case GGML_TYPE_IQ3_XXS:
  2923. case GGML_TYPE_IQ3_S:
  2924. case GGML_TYPE_IQ4_XS:
  2925. case GGML_TYPE_IQ4_NL:
  2926. break;
  2927. default:
  2928. return nullptr;
  2929. }
  2930. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  2931. }
  2932. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  2933. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  2934. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  2935. int best_i = -1;
  2936. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  2937. int worst_i = -1;
  2938. size_t worst_size = 0; //largest unused buffer seen so far
  2939. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  2940. vk_buffer &b = ctx->buffer_pool[i];
  2941. if (b != nullptr && b->size >= size && b->size < best_size) {
  2942. best_i = i;
  2943. best_size = b->size;
  2944. }
  2945. if (b != nullptr && b->size > worst_size) {
  2946. worst_i = i;
  2947. worst_size = b->size;
  2948. }
  2949. }
  2950. if(best_i != -1) {
  2951. //found the smallest buffer that fits our needs
  2952. vk_buffer b = ctx->buffer_pool[best_i];
  2953. ctx->buffer_pool[best_i].reset();
  2954. return b;
  2955. }
  2956. if(worst_i != -1) {
  2957. //no buffer that fits our needs, resize largest one to save memory
  2958. vk_buffer& b = ctx->buffer_pool[worst_i];
  2959. ggml_vk_destroy_buffer(b);
  2960. }
  2961. return ggml_vk_create_buffer_device(ctx->device, size);
  2962. }
  2963. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  2964. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  2965. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  2966. vk_buffer& b = ctx->buffer_pool[i];
  2967. if (b == nullptr) {
  2968. b = buffer;
  2969. return;
  2970. }
  2971. }
  2972. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  2973. ggml_vk_destroy_buffer(buffer);
  2974. }
  2975. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  2976. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  2977. // Try to find existing temp buffer with enough capacity
  2978. for (auto& buffer : ctx->gc.temp_buffers) {
  2979. if (buffer->size >= size) {
  2980. return buffer;
  2981. }
  2982. }
  2983. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  2984. // Otherwise create new buffer
  2985. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  2986. ctx->gc.temp_buffers.push_back(buf);
  2987. return buf;
  2988. }
  2989. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  2990. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  2991. vk_buffer buf = ggml_vk_create_buffer(device, size,
  2992. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  2993. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  2994. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  2995. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  2996. size/1024.0/1024.0);
  2997. device->device.freeMemory(buf->device_memory);
  2998. device->device.destroyBuffer(buf->buffer);
  2999. return nullptr;
  3000. }
  3001. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3002. return buf->ptr;
  3003. }
  3004. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3005. if (ptr == nullptr) {
  3006. return;
  3007. }
  3008. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3009. vk_buffer buf;
  3010. size_t index;
  3011. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3012. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3013. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3014. if (ptr >= addr && ptr < endr) {
  3015. buf = std::get<2>(device->pinned_memory[i]);
  3016. index = i;
  3017. break;
  3018. }
  3019. }
  3020. if (buf == nullptr) {
  3021. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3022. return;
  3023. }
  3024. ggml_vk_destroy_buffer(buf);
  3025. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3026. }
  3027. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3028. buf = nullptr;
  3029. buf_offset = 0;
  3030. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3031. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3032. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3033. if (ptr >= addr && ptr < endr) {
  3034. buf = std::get<2>(device->pinned_memory[i]);
  3035. buf_offset = ((const uint8_t *)ptr) - addr;
  3036. break;
  3037. }
  3038. }
  3039. }
  3040. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_queue& q, bool one_time = true) {
  3041. vk_submission s;
  3042. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  3043. if (one_time) {
  3044. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3045. } else {
  3046. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3047. }
  3048. return s;
  3049. }
  3050. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context* ctx, vk_context& subctx, vk_pipeline& pipeline, std::initializer_list<vk::DescriptorBufferInfo> const& descriptor_buffer_infos, size_t push_constant_size, const void* push_constants, std::array<uint32_t, 3> elements) {
  3051. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3052. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3053. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3054. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3055. for (auto& buffer : descriptor_buffer_infos) {
  3056. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3057. }
  3058. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3059. GGML_ASSERT(pipeline->descriptor_set_idx < pipeline->descriptor_sets.size());
  3060. GGML_ASSERT(descriptor_buffer_infos.size() == pipeline->parameter_count);
  3061. vk::DescriptorSet& descriptor_set = pipeline->descriptor_sets[pipeline->descriptor_set_idx++];
  3062. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3063. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3064. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size, push_constants);
  3065. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3066. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3067. pipeline->layout,
  3068. 0,
  3069. { descriptor_set },
  3070. {});
  3071. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3072. }
  3073. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3074. s.buffer.end();
  3075. s.wait_semaphores = std::move(wait_semaphores);
  3076. s.signal_semaphores = std::move(signal_semaphores);
  3077. }
  3078. static void ggml_vk_ctx_end(vk_context& ctx) {
  3079. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3080. if (ctx->s == nullptr) {
  3081. return;
  3082. }
  3083. ctx->s->buffer.end();
  3084. ctx->s = nullptr;
  3085. }
  3086. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3087. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3088. if (subctx->s != nullptr) {
  3089. ggml_vk_ctx_end(subctx);
  3090. }
  3091. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->q) });
  3092. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3093. }
  3094. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3095. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3096. return CEIL_DIV(width, align) * align;
  3097. }
  3098. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3099. if (memcpys == nullptr) {
  3100. memcpy(dst, src, size);
  3101. } else {
  3102. memcpys->emplace_back(dst, src, size);
  3103. }
  3104. }
  3105. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3106. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3107. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3108. ggml_vk_destroy_buffer(device->sync_staging);
  3109. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3110. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3111. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3112. }
  3113. }
  3114. 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) {
  3115. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3116. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3117. // Buffer is already mapped
  3118. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3119. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3120. GGML_ABORT("fatal error");
  3121. }
  3122. // Check if src is pinned memory
  3123. vk_buffer buf = nullptr;
  3124. size_t buf_offset = 0;
  3125. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3126. const uint64_t ne0 = tensor->ne[0];
  3127. const uint64_t ne1 = tensor->ne[1];
  3128. const uint64_t ne2 = tensor->ne[2];
  3129. const uint64_t ne3 = tensor->ne[3];
  3130. const uint64_t nb0 = tensor->nb[0];
  3131. const uint64_t nb1 = tensor->nb[1];
  3132. const uint64_t nb2 = tensor->nb[2];
  3133. const uint64_t nb3 = tensor->nb[3];
  3134. const ggml_type type = tensor->type;
  3135. const uint64_t ts = ggml_type_size(type);
  3136. const uint64_t bs = ggml_blck_size(type);
  3137. const uint64_t dstnb0 = ts;
  3138. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3139. const uint64_t dstnb2 = dstnb1*ne1;
  3140. const uint64_t dstnb3 = dstnb2*ne2;
  3141. const uint64_t ne = ggml_nelements(tensor);
  3142. if (buf != nullptr) {
  3143. // Memory is pinned, use as staging buffer
  3144. std::vector<vk::BufferCopy> slices;
  3145. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3146. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3147. // Find longest contiguous slice
  3148. if (ne1*nb1 == dstnb2) {
  3149. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3150. } else {
  3151. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3152. if (ne0*nb0/bs == dstnb1) {
  3153. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3154. } else {
  3155. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3156. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3157. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3158. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3159. }
  3160. }
  3161. }
  3162. }
  3163. }
  3164. }
  3165. ggml_vk_sync_buffers(subctx);
  3166. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3167. return;
  3168. }
  3169. if (!sync_staging) {
  3170. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3171. }
  3172. // Staging buffer required
  3173. vk_buffer& staging = ctx->device->sync_staging;
  3174. const uint64_t copy_size = ts*ne/bs;
  3175. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3176. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3177. ggml_vk_sync_buffers(subctx);
  3178. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3179. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3180. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3181. // Find longest contiguous slice
  3182. if (ne1*nb1 == dstnb2) {
  3183. deferred_memcpy((uint8_t *)staging->ptr + i3*dstnb3 + i2*dstnb2, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2, dstnb2, &subctx->in_memcpys);
  3184. } else {
  3185. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3186. if (ne0*nb0/bs == dstnb1) {
  3187. deferred_memcpy((uint8_t *)staging->ptr + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2 + i1*nb1, dstnb1, &subctx->in_memcpys);
  3188. } else {
  3189. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3190. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3191. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3192. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3193. }
  3194. }
  3195. }
  3196. }
  3197. }
  3198. }
  3199. }
  3200. static void ggml_vk_buffer_write_2d_async(vk_context subctx, vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height, bool sync_staging = false) {
  3201. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3202. // Buffer is already mapped
  3203. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3204. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3205. GGML_ABORT("fatal error");
  3206. }
  3207. // Check if src is pinned memory
  3208. vk_buffer buf = nullptr;
  3209. size_t buf_offset = 0;
  3210. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3211. if (buf != nullptr) {
  3212. // Memory is pinned, use as staging buffer
  3213. std::vector<vk::BufferCopy> slices(1);
  3214. if (width == spitch) {
  3215. // Only do single write if stride is equal
  3216. slices[0].srcOffset = buf_offset;
  3217. slices[0].dstOffset = offset;
  3218. slices[0].size = width * height;
  3219. } else {
  3220. slices.resize(height);
  3221. for (size_t i = 0; i < height; i++) {
  3222. slices[i].srcOffset = buf_offset + i * spitch;
  3223. slices[i].dstOffset = offset + i * width;
  3224. slices[i].size = width;
  3225. }
  3226. }
  3227. ggml_vk_sync_buffers(subctx);
  3228. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3229. return;
  3230. }
  3231. VK_LOG_DEBUG("STAGING");
  3232. if (!sync_staging) {
  3233. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3234. }
  3235. // Staging buffer required
  3236. const size_t copy_size = width*height;
  3237. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3238. vk_buffer& staging_buffer = dst->device->sync_staging;
  3239. VkBufferCopy buf_copy = {
  3240. 0,
  3241. offset,
  3242. copy_size};
  3243. ggml_vk_sync_buffers(subctx);
  3244. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3245. if (width == spitch) {
  3246. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3247. } else {
  3248. for (size_t i = 0; i < height; i++) {
  3249. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3250. }
  3251. }
  3252. }
  3253. static void ggml_vk_buffer_write_async(vk_context subctx, vk_buffer& dst, size_t offset, const void * src, size_t size, bool sync_staging = false) {
  3254. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3255. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3256. }
  3257. static void ggml_vk_buffer_write_2d(vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height) {
  3258. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3259. // Buffer is already mapped
  3260. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3261. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3262. for (size_t i = 0; i < height; i++) {
  3263. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3264. }
  3265. } else {
  3266. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3267. ggml_vk_ctx_begin(dst->device, subctx);
  3268. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3269. ggml_vk_ctx_end(subctx);
  3270. for (auto& cpy : subctx->in_memcpys) {
  3271. memcpy(cpy.dst, cpy.src, cpy.n);
  3272. }
  3273. ggml_vk_submit(subctx, dst->device->fence);
  3274. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3275. dst->device->device.resetFences({ dst->device->fence });
  3276. }
  3277. }
  3278. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3279. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3280. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3281. }
  3282. static void ggml_vk_buffer_read_2d_async(vk_context subctx, vk_buffer& src, size_t offset, void * dst, size_t spitch, size_t dpitch, size_t width, size_t height, bool sync_staging = false) {
  3283. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3284. GGML_ASSERT(width > 0);
  3285. GGML_ASSERT(height > 0);
  3286. GGML_ASSERT(src != nullptr);
  3287. // TODO: staging_offset is not used
  3288. // Check if dst is pinned memory
  3289. vk_buffer buf = nullptr;
  3290. size_t buf_offset = 0;
  3291. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3292. std::vector<vk::BufferCopy> slices(1);
  3293. if (width == spitch && width == dpitch) {
  3294. // Only do single write if stride is equal
  3295. slices[0].srcOffset = offset;
  3296. slices[0].dstOffset = buf_offset;
  3297. slices[0].size = width * height;
  3298. } else {
  3299. slices.resize(height);
  3300. for (size_t i = 0; i < height; i++) {
  3301. slices[i].srcOffset = offset + i * spitch;
  3302. slices[i].dstOffset = buf_offset + i * dpitch;
  3303. slices[i].size = width;
  3304. }
  3305. }
  3306. if (buf != nullptr) {
  3307. // Memory is pinned, use as staging buffer
  3308. ggml_vk_sync_buffers(subctx);
  3309. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3310. return;
  3311. }
  3312. VK_LOG_DEBUG("STAGING");
  3313. if (!sync_staging) {
  3314. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3315. }
  3316. // Fall back to staging buffer
  3317. const size_t copy_size = dpitch * height;
  3318. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  3319. vk_buffer& staging_buffer = src->device->sync_staging;
  3320. ggml_vk_sync_buffers(subctx);
  3321. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  3322. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  3323. }
  3324. static void ggml_vk_buffer_read_async(vk_context subctx, vk_buffer& src, size_t offset, void * dst, size_t size, bool sync_staging = false) {
  3325. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  3326. }
  3327. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  3328. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  3329. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  3330. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  3331. // the HW device to host copy path.
  3332. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  3333. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3334. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  3335. } else {
  3336. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3337. ggml_vk_ctx_begin(src->device, subctx);
  3338. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  3339. ggml_vk_ctx_end(subctx);
  3340. ggml_vk_submit(subctx, src->device->fence);
  3341. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  3342. src->device->device.resetFences({ src->device->fence });
  3343. for (auto& cpy : subctx->out_memcpys) {
  3344. memcpy(cpy.dst, cpy.src, cpy.n);
  3345. }
  3346. }
  3347. }
  3348. 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) {
  3349. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  3350. // Make sure both buffers are on same device
  3351. GGML_ASSERT(src->device == dst->device);
  3352. VkBufferCopy bc{ src_offset, dst_offset, size };
  3353. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  3354. }
  3355. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  3356. if (src->device == dst->device) {
  3357. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  3358. // Copy within the device
  3359. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3360. ggml_vk_ctx_begin(src->device, subctx);
  3361. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  3362. ggml_vk_ctx_end(subctx);
  3363. ggml_vk_submit(subctx, src->device->fence);
  3364. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  3365. src->device->device.resetFences({ src->device->fence });
  3366. } else {
  3367. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  3368. // Copy device to device
  3369. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  3370. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  3371. // Copy to src staging buffer
  3372. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  3373. // memcpy to dst staging buffer
  3374. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  3375. // Copy to dst buffer
  3376. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  3377. }
  3378. }
  3379. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3380. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  3381. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3382. }
  3383. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3384. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  3385. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3386. ggml_vk_ctx_begin(dst->device, subctx);
  3387. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3388. ggml_vk_ctx_end(subctx);
  3389. ggml_vk_submit(subctx, dst->device->fence);
  3390. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  3391. dst->device->device.resetFences({ dst->device->fence });
  3392. }
  3393. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  3394. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  3395. uint32_t split_k = 1;
  3396. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  3397. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  3398. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  3399. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  3400. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  3401. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  3402. // Clamp to 2 or 4
  3403. split_k = std::min(split_k, 4u);
  3404. if (split_k == 3) {
  3405. split_k = 2;
  3406. }
  3407. }
  3408. }
  3409. return split_k;
  3410. }
  3411. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned, ggml_type src0_type) {
  3412. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  3413. if (ctx->device->coopmat2) {
  3414. // Use large shader when the N dimension is greater than the medium shader's tile size
  3415. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3416. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  3417. return aligned ? mmp->a_l : mmp->l;
  3418. }
  3419. // Use medium shader when the N dimension is greater than the small shader's tile size
  3420. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3421. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  3422. return aligned ? mmp->a_m : mmp->m;
  3423. }
  3424. return aligned ? mmp->a_s : mmp->s;
  3425. }
  3426. if ((ctx->device->mul_mat_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_l[src0_type])) {
  3427. return aligned ? mmp->a_s : mmp->s;
  3428. }
  3429. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  3430. return aligned ? mmp->a_m : mmp->m;
  3431. }
  3432. return aligned ? mmp->a_l : mmp->l;
  3433. }
  3434. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type) {
  3435. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  3436. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  3437. }
  3438. static void ggml_vk_matmul(
  3439. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3440. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  3441. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3442. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3443. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  3444. uint32_t padded_n) {
  3445. VK_LOG_DEBUG("ggml_vk_matmul(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), split_k: (" << (split_k_buffer.buffer != nullptr ? split_k_buffer.buffer->buffer : VK_NULL_HANDLE) << ", " << split_k_buffer.offset << ", " << split_k_buffer.size << "), m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", split_k: " << split_k << ", batch: " << batch << ", ne02: " << ne02 << ", ne12: " << ne12 << ", broadcast2: " << broadcast2 << ", broadcast3: " << broadcast3 << ")");
  3446. ggml_vk_sync_buffers(subctx);
  3447. if (split_k == 1) {
  3448. const vk_mat_mat_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, k, ne02, ne12, broadcast2, broadcast3, padded_n };
  3449. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  3450. return;
  3451. }
  3452. GGML_ASSERT(batch_stride_d == m * n);
  3453. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, padded_n };
  3454. // Make sure enough workgroups get assigned for split k to work
  3455. 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 });
  3456. ggml_vk_sync_buffers(subctx);
  3457. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  3458. 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 });
  3459. }
  3460. static vk_pipeline ggml_vk_guess_matmul_id_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned, ggml_type src0_type) {
  3461. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  3462. if (ctx->device->coopmat2) {
  3463. // Use large shader when the N dimension is greater than the medium shader's tile size
  3464. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3465. if ((ctx->device->mul_mat_id_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_s[src0_type])) {
  3466. return aligned ? mmp->a_l : mmp->l;
  3467. }
  3468. // Use medium shader when the N dimension is greater than the small shader's tile size
  3469. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3470. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  3471. return aligned ? mmp->a_m : mmp->m;
  3472. }
  3473. return aligned ? mmp->a_s : mmp->s;
  3474. }
  3475. if ((ctx->device->mul_mat_id_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_l[src0_type])) {
  3476. return aligned ? mmp->a_s : mmp->s;
  3477. }
  3478. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  3479. return aligned ? mmp->a_m : mmp->m;
  3480. }
  3481. return aligned ? mmp->a_l : mmp->l;
  3482. }
  3483. static uint32_t ggml_vk_guess_matmul_id_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type) {
  3484. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  3485. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  3486. }
  3487. static void ggml_vk_matmul_id(
  3488. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3489. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  3490. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3491. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3492. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  3493. uint32_t padded_n) {
  3494. VK_LOG_DEBUG("ggml_vk_matmul_id(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), ids: (" << ids.buffer->buffer << ", " << ids.offset << ", " << ids.size << "), " <<
  3495. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  3496. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  3497. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  3498. ggml_vk_sync_buffers(subctx);
  3499. const vk_mat_mat_id_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d,
  3500. nei0, nei1, nbi1, ne11, padded_n };
  3501. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, sizeof(vk_mat_mat_id_push_constants), &pc, { m, nei1, n_as });
  3502. }
  3503. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  3504. return
  3505. tensor->nb[0] == ggml_type_size(tensor->type) &&
  3506. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  3507. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  3508. }
  3509. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  3510. // Choose "contiguous copy" shader if src/dst are contiguous
  3511. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  3512. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  3513. if (contig) {
  3514. return ctx->device->pipeline_contig_cpy_f32_f32;
  3515. } else {
  3516. return ctx->device->pipeline_cpy_f32_f32;
  3517. }
  3518. }
  3519. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  3520. if (contig) {
  3521. return ctx->device->pipeline_contig_cpy_f32_f16;
  3522. } else {
  3523. return ctx->device->pipeline_cpy_f32_f16;
  3524. }
  3525. }
  3526. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  3527. if (contig) {
  3528. return ctx->device->pipeline_contig_cpy_f16_f16;
  3529. } else {
  3530. return ctx->device->pipeline_cpy_f16_f16;
  3531. }
  3532. }
  3533. if (src->type == GGML_TYPE_F32) {
  3534. switch (to) {
  3535. case GGML_TYPE_Q4_0:
  3536. case GGML_TYPE_Q4_1:
  3537. case GGML_TYPE_Q5_0:
  3538. case GGML_TYPE_Q5_1:
  3539. case GGML_TYPE_Q8_0:
  3540. case GGML_TYPE_IQ4_NL:
  3541. return ctx->device->pipeline_cpy_f32_quant[to];
  3542. default:
  3543. break;
  3544. }
  3545. }
  3546. if (to == GGML_TYPE_F32) {
  3547. switch (src->type) {
  3548. case GGML_TYPE_Q4_0:
  3549. case GGML_TYPE_Q4_1:
  3550. case GGML_TYPE_Q5_0:
  3551. case GGML_TYPE_Q5_1:
  3552. case GGML_TYPE_Q8_0:
  3553. case GGML_TYPE_IQ4_NL:
  3554. return ctx->device->pipeline_cpy_quant_f32[src->type];
  3555. default:
  3556. break;
  3557. }
  3558. }
  3559. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  3560. GGML_ABORT("fatal error");
  3561. }
  3562. 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) {
  3563. VK_LOG_DEBUG("ggml_vk_cpy_to_contiguous((" << tensor << ", type=" << tensor->type << ", ne0=" << tensor->ne[0] << ", ne1=" << tensor->ne[1] << ", ne2=" << tensor->ne[2] << ", ne3=" << tensor->ne[3] << ", nb0=" << tensor->nb[0] << ", nb1=" << tensor->nb[1] << ", nb2=" << tensor->nb[2] << ", nb3=" << tensor->nb[3] << "), ";
  3564. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  3565. const int tensor_type_size = ggml_type_size(tensor->type);
  3566. const uint32_t ne = ggml_nelements(tensor);
  3567. std::array<uint32_t, 3> elements;
  3568. if (ne > 262144) {
  3569. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  3570. } else if (ne > 512) {
  3571. elements = { 512, CEIL_DIV(ne, 512), 1 };
  3572. } else {
  3573. elements = { ne, 1, 1 };
  3574. }
  3575. vk_op_unary_push_constants pc = {
  3576. (uint32_t)ne,
  3577. (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,
  3578. (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]),
  3579. 0,
  3580. 0.0f, 0.0f,
  3581. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3582. };
  3583. init_pushconst_fastdiv(pc);
  3584. ggml_vk_sync_buffers(subctx);
  3585. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(vk_op_unary_push_constants), &pc, elements);
  3586. }
  3587. static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  3588. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3589. 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];
  3590. 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];
  3591. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3592. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3593. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3594. const uint64_t ne00 = src0->ne[0];
  3595. const uint64_t ne01 = src0->ne[1];
  3596. const uint64_t ne02 = src0->ne[2];
  3597. const uint64_t ne03 = src0->ne[3];
  3598. const uint64_t ne10 = src1->ne[0];
  3599. const uint64_t ne11 = src1->ne[1];
  3600. const uint64_t ne12 = src1->ne[2];
  3601. const uint64_t ne13 = src1->ne[3];
  3602. const uint64_t ne20 = dst->ne[0];
  3603. const uint64_t ne21 = dst->ne[1];
  3604. const uint64_t r2 = ne12 / ne02;
  3605. const uint64_t r3 = ne13 / ne03;
  3606. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3607. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3608. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3609. vk_buffer d_Qx = nullptr;
  3610. size_t qx_buf_offset = 0;
  3611. vk_buffer d_Qy = nullptr;
  3612. size_t qy_buf_offset = 0;
  3613. bool src0_uma = false;
  3614. bool src1_uma = false;
  3615. if (ctx->device->uma) {
  3616. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3617. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3618. src0_uma = d_Qx != nullptr;
  3619. src1_uma = d_Qy != nullptr;
  3620. }
  3621. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  3622. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  3623. !ggml_vk_dim01_contiguous(src0);
  3624. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  3625. !ggml_vk_dim01_contiguous(src1);
  3626. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  3627. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  3628. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  3629. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  3630. if (qx_needs_dequant) {
  3631. // Fall back to dequant + f16 mulmat
  3632. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  3633. }
  3634. // Not implemented
  3635. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3636. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11, qx_needs_dequant ? GGML_TYPE_F16 : src0->type));
  3637. const bool aligned = ne10 == kpad && ne01 > 8 && ne11 > 8;
  3638. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type);
  3639. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  3640. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  3641. const int x_ne = ne01 * ne00;
  3642. const int y_ne = padded_n * ne10;
  3643. const int d_ne = ne11 * ne01;
  3644. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  3645. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  3646. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3647. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  3648. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  3649. const uint64_t d_sz = sizeof(float) * d_ne;
  3650. vk_pipeline to_fp16_vk_0 = nullptr;
  3651. vk_pipeline to_fp16_vk_1 = nullptr;
  3652. if (x_non_contig) {
  3653. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  3654. } else {
  3655. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  3656. }
  3657. if (y_non_contig) {
  3658. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  3659. } else {
  3660. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3661. }
  3662. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3663. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3664. if (dryrun) {
  3665. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3666. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3667. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  3668. if (
  3669. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3670. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  3671. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  3672. GGML_ABORT("Requested preallocation size is too large");
  3673. }
  3674. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3675. ctx->prealloc_size_x = x_sz_upd;
  3676. }
  3677. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  3678. ctx->prealloc_size_y = y_sz_upd;
  3679. }
  3680. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  3681. ctx->prealloc_size_split_k = split_k_size;
  3682. }
  3683. // Request descriptor sets
  3684. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  3685. if (qx_needs_dequant) {
  3686. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  3687. }
  3688. if (qy_needs_dequant) {
  3689. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  3690. }
  3691. if (split_k > 1) {
  3692. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, 1);
  3693. }
  3694. return;
  3695. }
  3696. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3697. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3698. GGML_ASSERT(d_D != nullptr);
  3699. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  3700. vk_buffer d_X;
  3701. uint64_t x_buf_offset = 0;
  3702. vk_buffer d_Y;
  3703. uint64_t y_buf_offset = 0;
  3704. if (!src0_uma) {
  3705. d_Qx = src0_buf_ctx->dev_buffer;
  3706. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3707. GGML_ASSERT(d_Qx != nullptr);
  3708. }
  3709. if (!src1_uma) {
  3710. d_Qy = src1_buf_ctx->dev_buffer;
  3711. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3712. GGML_ASSERT(d_Qy != nullptr);
  3713. }
  3714. if (qx_needs_dequant) {
  3715. d_X = ctx->prealloc_x;
  3716. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  3717. } else {
  3718. d_X = d_Qx;
  3719. x_buf_offset = qx_buf_offset;
  3720. GGML_ASSERT(qx_sz == x_sz);
  3721. }
  3722. if (qy_needs_dequant) {
  3723. d_Y = ctx->prealloc_y;
  3724. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  3725. } else {
  3726. d_Y = d_Qy;
  3727. y_buf_offset = qy_buf_offset;
  3728. GGML_ASSERT(qy_sz == y_sz);
  3729. }
  3730. if (x_non_contig) {
  3731. 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 });
  3732. } else if (qx_needs_dequant) {
  3733. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  3734. ggml_vk_sync_buffers(subctx);
  3735. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  3736. }
  3737. if (y_non_contig) {
  3738. 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 });
  3739. }
  3740. uint32_t stride_batch_x = ne00*ne01;
  3741. uint32_t stride_batch_y = ne10*ne11;
  3742. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3743. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3744. }
  3745. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  3746. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3747. }
  3748. // compute
  3749. ggml_vk_matmul(
  3750. ctx, subctx, pipeline,
  3751. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  3752. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  3753. ne01, ne11, ne10,
  3754. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  3755. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  3756. ); // NOLINT
  3757. }
  3758. static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  3759. VK_LOG_DEBUG("ggml_vk_mul_mat_vec_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3760. 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];
  3761. 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];
  3762. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  3763. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3764. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3765. const uint64_t ne00 = src0->ne[0];
  3766. const uint64_t ne01 = src0->ne[1];
  3767. const uint64_t ne02 = src0->ne[2];
  3768. const uint64_t ne03 = src0->ne[3];
  3769. const uint64_t ne10 = src1->ne[0];
  3770. const uint64_t ne11 = src1->ne[1];
  3771. const uint64_t ne12 = src1->ne[2];
  3772. const uint64_t ne13 = src1->ne[3];
  3773. const uint64_t ne20 = dst->ne[0];
  3774. const uint64_t ne21 = dst->ne[1];
  3775. const uint64_t ne22 = dst->ne[2];
  3776. const uint64_t ne23 = dst->ne[3];
  3777. const uint64_t r2 = ne12 / ne02;
  3778. const uint64_t r3 = ne13 / ne03;
  3779. // batch_n indicates that we need to compute a few vector results, and this assumes
  3780. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  3781. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  3782. bool batch_n = ne11 > 1;
  3783. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3784. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3785. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3786. vk_buffer d_Qx = nullptr;
  3787. size_t qx_buf_offset = 0;
  3788. vk_buffer d_Qy = nullptr;
  3789. size_t qy_buf_offset = 0;
  3790. bool src0_uma = false;
  3791. bool src1_uma = false;
  3792. if (ctx->device->uma) {
  3793. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3794. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3795. src0_uma = d_Qx != nullptr;
  3796. src1_uma = d_Qy != nullptr;
  3797. }
  3798. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  3799. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  3800. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  3801. const bool qx_needs_dequant = x_non_contig;
  3802. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  3803. // Not implemented
  3804. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3805. const uint64_t x_ne = ne01 * ne00;
  3806. const uint64_t y_ne = ne11 * ne10;
  3807. const uint64_t d_ne = ne11 * ne01;
  3808. 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);
  3809. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3810. 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;
  3811. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  3812. const uint64_t d_sz = sizeof(float) * d_ne;
  3813. vk_pipeline to_fp16_vk_0 = nullptr;
  3814. vk_pipeline to_fp16_vk_1 = nullptr;
  3815. if (x_non_contig) {
  3816. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  3817. }
  3818. if (y_non_contig) {
  3819. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  3820. } else {
  3821. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3822. }
  3823. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  3824. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3825. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3826. GGML_ASSERT(dmmv != nullptr);
  3827. if (dryrun) {
  3828. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3829. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3830. if (
  3831. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3832. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  3833. GGML_ABORT("Requested preallocation size is too large");
  3834. }
  3835. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3836. ctx->prealloc_size_x = x_sz_upd;
  3837. }
  3838. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  3839. ctx->prealloc_size_y = y_sz_upd;
  3840. }
  3841. // Request descriptor sets
  3842. if (qx_needs_dequant) {
  3843. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  3844. }
  3845. if (qy_needs_dequant) {
  3846. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  3847. }
  3848. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  3849. return;
  3850. }
  3851. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3852. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3853. GGML_ASSERT(d_D != nullptr);
  3854. vk_buffer d_X;
  3855. uint64_t x_buf_offset = 0;
  3856. vk_buffer d_Y;
  3857. uint64_t y_buf_offset = 0;
  3858. if(!src0_uma) {
  3859. d_Qx = src0_buf_ctx->dev_buffer;
  3860. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3861. GGML_ASSERT(d_Qx != nullptr);
  3862. }
  3863. if(!src1_uma) {
  3864. d_Qy = src1_buf_ctx->dev_buffer;
  3865. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3866. GGML_ASSERT(d_Qy != nullptr);
  3867. }
  3868. if (qx_needs_dequant) {
  3869. d_X = ctx->prealloc_x;
  3870. } else {
  3871. d_X = d_Qx;
  3872. x_buf_offset = qx_buf_offset;
  3873. GGML_ASSERT(qx_sz == x_sz);
  3874. }
  3875. if (qy_needs_dequant) {
  3876. d_Y = ctx->prealloc_y;
  3877. } else {
  3878. d_Y = d_Qy;
  3879. y_buf_offset = qy_buf_offset;
  3880. GGML_ASSERT(qy_sz == y_sz);
  3881. }
  3882. if (x_non_contig) {
  3883. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  3884. 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 });
  3885. }
  3886. if (y_non_contig) {
  3887. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  3888. 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 });
  3889. }
  3890. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  3891. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  3892. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  3893. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  3894. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3895. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3896. }
  3897. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  3898. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3899. }
  3900. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  3901. uint32_t groups_x = ne01;
  3902. uint32_t groups_z = 1;
  3903. if (ne01 > max_groups_x) {
  3904. groups_z = 64;
  3905. groups_x = CEIL_DIV(groups_x, groups_z);
  3906. }
  3907. // compute
  3908. const vk_mat_vec_push_constants pc = {
  3909. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  3910. stride_batch_x, stride_batch_y, stride_batch_d,
  3911. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  3912. };
  3913. ggml_vk_sync_buffers(subctx);
  3914. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  3915. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  3916. sizeof(vk_mat_vec_push_constants), &pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  3917. }
  3918. static void ggml_vk_mul_mat_vec_p021_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  3919. VK_LOG_DEBUG("ggml_vk_mul_mat_p021_f16_f32(" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3920. 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];
  3921. 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];
  3922. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3923. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  3924. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  3925. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  3926. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3927. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3928. const uint64_t ne00 = src0->ne[0];
  3929. const uint64_t ne01 = src0->ne[1];
  3930. const uint64_t ne02 = src0->ne[2];
  3931. // const uint64_t ne03 = src0->ne[3];
  3932. const uint64_t ne10 = src1->ne[0];
  3933. const uint64_t ne11 = src1->ne[1];
  3934. const uint64_t ne12 = src1->ne[2];
  3935. // const uint64_t ne13 = src1->ne[3];
  3936. GGML_ASSERT(ne11 == 1);
  3937. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3938. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3939. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3940. vk_buffer d_Qy = nullptr;
  3941. size_t qy_buf_offset = 0;
  3942. bool src1_uma = false;
  3943. if (ctx->device->uma) {
  3944. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3945. src1_uma = d_Qy != nullptr;
  3946. }
  3947. const uint64_t x_ne = ne00 * ne01 * ne02;
  3948. const uint64_t y_ne = ne10 * ne11 * ne12;
  3949. const uint64_t d_ne = ne01 * ne11 * ne12;
  3950. 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);
  3951. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3952. const uint64_t d_sz = sizeof(float) * d_ne;
  3953. if (dryrun) {
  3954. // Request descriptor sets
  3955. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, 1);
  3956. return;
  3957. }
  3958. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3959. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3960. GGML_ASSERT(d_D != nullptr);
  3961. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  3962. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3963. GGML_ASSERT(d_Qx != nullptr);
  3964. if (!src1_uma) {
  3965. d_Qy = src1_buf_ctx->dev_buffer;
  3966. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3967. GGML_ASSERT(d_Qx != nullptr);
  3968. }
  3969. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  3970. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  3971. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  3972. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  3973. // compute
  3974. 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)) };
  3975. ggml_vk_sync_buffers(subctx);
  3976. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, 6 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  3977. }
  3978. static void ggml_vk_mul_mat_vec_nc_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  3979. VK_LOG_DEBUG("ggml_vk_mul_mat_nc_f16_f32((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3980. 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];
  3981. 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];
  3982. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3983. GGML_ASSERT(!ggml_is_transposed(src0));
  3984. GGML_ASSERT(!ggml_is_transposed(src1));
  3985. GGML_ASSERT(!ggml_is_permuted(src0));
  3986. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3987. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3988. const uint64_t ne00 = src0->ne[0];
  3989. const uint64_t ne01 = src0->ne[1];
  3990. const uint64_t ne02 = src0->ne[2];
  3991. // const uint64_t ne03 = src0->ne[3];
  3992. const uint64_t nb01 = src0->nb[1];
  3993. const uint64_t nb02 = src0->nb[2];
  3994. // const uint64_t ne10 = src1->ne[0];
  3995. const uint64_t ne11 = src1->ne[1];
  3996. const uint64_t ne12 = src1->ne[2];
  3997. // const uint64_t ne13 = src1->ne[3];
  3998. GGML_ASSERT(ne11 == 1);
  3999. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4000. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4001. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4002. vk_buffer d_Qy = nullptr;
  4003. size_t qy_buf_offset = 0;
  4004. bool src1_uma = false;
  4005. if (ctx->device->uma) {
  4006. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4007. src1_uma = d_Qy != nullptr;
  4008. }
  4009. const uint64_t d_ne = ne01 * ne11 * ne12;
  4010. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4011. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4012. const uint64_t qx_sz = ggml_nbytes(src0);
  4013. const uint64_t qy_sz = ggml_nbytes(src1);
  4014. const uint64_t d_sz = sizeof(float) * d_ne;
  4015. if (dryrun) {
  4016. // Request descriptor sets
  4017. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4018. return;
  4019. }
  4020. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4021. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4022. GGML_ASSERT(d_D != nullptr);
  4023. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4024. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4025. GGML_ASSERT(d_Qx != nullptr);
  4026. if (!src1_uma) {
  4027. d_Qy = src1_buf_ctx->dev_buffer;
  4028. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4029. GGML_ASSERT(d_Qx != nullptr);
  4030. }
  4031. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4032. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4033. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4034. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4035. // compute
  4036. 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)) };
  4037. ggml_vk_sync_buffers(subctx);
  4038. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4039. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, 7 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  4040. }
  4041. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4042. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4043. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4044. // detect 0213 permutation, and batch size of 1
  4045. src0->nb[0] <= src0->nb[2] &&
  4046. src0->nb[2] <= src0->nb[1] &&
  4047. src0->nb[1] <= src0->nb[3] &&
  4048. src1->nb[0] <= src1->nb[2] &&
  4049. src1->nb[2] <= src1->nb[1] &&
  4050. src1->nb[1] <= src1->nb[3] &&
  4051. src0->ne[3] == 1 &&
  4052. src1->ne[3] == 1) {
  4053. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4054. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4055. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4056. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4057. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4058. // when ne12 and ne13 are one.
  4059. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4060. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4061. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4062. } else {
  4063. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4064. }
  4065. }
  4066. static void ggml_vk_mul_mat_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  4067. VK_LOG_DEBUG("ggml_vk_mul_mat_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4068. 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];
  4069. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
  4070. 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] << "),)");
  4071. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4072. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4073. const uint64_t ne00 = src0->ne[0];
  4074. const uint64_t ne01 = src0->ne[1];
  4075. const uint64_t ne02 = src0->ne[2];
  4076. const uint64_t ne03 = src0->ne[3];
  4077. const uint64_t ne10 = src1->ne[0];
  4078. const uint64_t ne11 = src1->ne[1];
  4079. const uint64_t ne12 = src1->ne[2];
  4080. const uint64_t ne13 = src1->ne[3];
  4081. const uint64_t nei0 = ids->ne[0];
  4082. const uint64_t nei1 = ids->ne[1];
  4083. GGML_ASSERT(nei0 * nei1 <= 3072);
  4084. const uint32_t nbi1 = ids->nb[1];
  4085. const uint32_t nbi2 = ids->nb[2];
  4086. const uint64_t ne20 = dst->ne[0];
  4087. const uint64_t ne21 = dst->ne[1];
  4088. const uint64_t ne22 = dst->ne[2];
  4089. const uint64_t ne23 = dst->ne[3];
  4090. const uint64_t n_as = ne02;
  4091. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4092. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4093. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4094. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4095. vk_buffer d_Qx = nullptr;
  4096. size_t qx_buf_offset = 0;
  4097. vk_buffer d_Qy = nullptr;
  4098. size_t qy_buf_offset = 0;
  4099. vk_buffer d_ids = nullptr;
  4100. size_t ids_buf_offset = 0;
  4101. bool src0_uma = false;
  4102. bool src1_uma = false;
  4103. bool ids_uma = false;
  4104. if (ctx->device->uma) {
  4105. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4106. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4107. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4108. src0_uma = d_Qx != nullptr;
  4109. src1_uma = d_Qy != nullptr;
  4110. ids_uma = d_ids != nullptr;
  4111. }
  4112. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4113. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4114. !ggml_vk_dim01_contiguous(src0);
  4115. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4116. !ggml_vk_dim01_contiguous(src1);
  4117. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4118. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  4119. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4120. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  4121. if (qx_needs_dequant) {
  4122. // Fall back to dequant + f16 mulmat
  4123. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  4124. }
  4125. // Not implemented
  4126. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4127. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? GGML_TYPE_F16 : src0->type));
  4128. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4129. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type);
  4130. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4131. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4132. const uint64_t x_ne = ne01 * ne00;
  4133. const uint64_t y_ne = padded_n * ne10;
  4134. const uint64_t d_ne = ne21 * ne20;
  4135. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4136. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4137. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4138. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4139. const uint64_t ids_sz = nbi2;
  4140. const uint64_t d_sz = sizeof(float) * d_ne;
  4141. vk_pipeline to_fp16_vk_0 = nullptr;
  4142. vk_pipeline to_fp16_vk_1 = nullptr;
  4143. if (x_non_contig) {
  4144. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  4145. } else {
  4146. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4147. }
  4148. if (y_non_contig) {
  4149. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  4150. } else {
  4151. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4152. }
  4153. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4154. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4155. if (dryrun) {
  4156. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4157. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4158. if (
  4159. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4160. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4161. GGML_ABORT("Requested preallocation size is too large");
  4162. }
  4163. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4164. ctx->prealloc_size_x = x_sz_upd;
  4165. }
  4166. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4167. ctx->prealloc_size_y = y_sz_upd;
  4168. }
  4169. // Request descriptor sets
  4170. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4171. if (qx_needs_dequant) {
  4172. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4173. }
  4174. if (qy_needs_dequant) {
  4175. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4176. }
  4177. return;
  4178. }
  4179. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4180. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4181. GGML_ASSERT(d_D != nullptr);
  4182. vk_buffer d_X;
  4183. uint64_t x_buf_offset = 0;
  4184. vk_buffer d_Y;
  4185. uint64_t y_buf_offset = 0;
  4186. if (!src0_uma) {
  4187. d_Qx = src0_buf_ctx->dev_buffer;
  4188. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4189. GGML_ASSERT(d_Qx != nullptr);
  4190. }
  4191. if (!src1_uma) {
  4192. d_Qy = src1_buf_ctx->dev_buffer;
  4193. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4194. GGML_ASSERT(d_Qy != nullptr);
  4195. }
  4196. if (!ids_uma) {
  4197. d_ids = ids_buf_ctx->dev_buffer;
  4198. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4199. GGML_ASSERT(d_ids != nullptr);
  4200. }
  4201. if (qx_needs_dequant) {
  4202. d_X = ctx->prealloc_x;
  4203. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4204. } else {
  4205. d_X = d_Qx;
  4206. x_buf_offset = qx_buf_offset;
  4207. GGML_ASSERT(qx_sz == x_sz);
  4208. }
  4209. if (qy_needs_dequant) {
  4210. d_Y = ctx->prealloc_y;
  4211. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4212. } else {
  4213. d_Y = d_Qy;
  4214. y_buf_offset = qy_buf_offset;
  4215. GGML_ASSERT(qy_sz == y_sz);
  4216. }
  4217. if (x_non_contig) {
  4218. 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 });
  4219. } else if (qx_needs_dequant) {
  4220. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4221. ggml_vk_sync_buffers(subctx);
  4222. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  4223. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  4224. }
  4225. if (y_non_contig) {
  4226. 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 });
  4227. }
  4228. uint32_t stride_batch_x = ne00*ne01;
  4229. uint32_t stride_batch_y = ne10*ne11;
  4230. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4231. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4232. }
  4233. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4234. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4235. }
  4236. // compute
  4237. ggml_vk_matmul_id(
  4238. ctx, subctx, pipeline,
  4239. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4240. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  4241. ne01, ne21, ne10, ne10, ne10, ne01,
  4242. stride_batch_x, stride_batch_y, ne20*ne21,
  4243. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  4244. ); // NOLINT
  4245. }
  4246. static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  4247. VK_LOG_DEBUG("ggml_vk_mul_mat_vec_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4248. 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];
  4249. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
  4250. 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];
  4251. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4252. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  4253. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4254. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4255. const uint64_t ne00 = src0->ne[0];
  4256. const uint64_t ne01 = src0->ne[1];
  4257. const uint64_t ne02 = src0->ne[2];
  4258. const uint64_t ne03 = src0->ne[3];
  4259. const uint64_t ne10 = src1->ne[0];
  4260. const uint64_t ne11 = src1->ne[1];
  4261. const uint64_t ne12 = src1->ne[2];
  4262. const uint64_t ne13 = src1->ne[3];
  4263. const uint64_t nei0 = ids->ne[0];
  4264. const uint64_t nei1 = ids->ne[1];
  4265. const uint64_t nbi2 = ids->nb[2];
  4266. GGML_ASSERT(nei1 == 1);
  4267. const uint64_t ne20 = dst->ne[0];
  4268. const uint64_t ne21 = dst->ne[1];
  4269. const uint64_t ne22 = dst->ne[2];
  4270. const uint64_t ne23 = dst->ne[3];
  4271. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4272. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4273. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4274. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4275. vk_buffer d_Qx = nullptr;
  4276. size_t qx_buf_offset = 0;
  4277. vk_buffer d_Qy = nullptr;
  4278. size_t qy_buf_offset = 0;
  4279. vk_buffer d_ids = nullptr;
  4280. size_t ids_buf_offset = 0;
  4281. bool src0_uma = false;
  4282. bool src1_uma = false;
  4283. bool ids_uma = false;
  4284. if (ctx->device->uma) {
  4285. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4286. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4287. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4288. src0_uma = d_Qx != nullptr;
  4289. src1_uma = d_Qy != nullptr;
  4290. ids_uma = d_ids != nullptr;
  4291. }
  4292. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4293. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4294. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4295. const bool qx_needs_dequant = x_non_contig;
  4296. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4297. // Not implemented
  4298. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4299. const uint64_t x_ne = ne01 * ne00;
  4300. const uint64_t y_ne = ne11 * ne10;
  4301. const uint64_t d_ne = ne21 * ne20;
  4302. 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);
  4303. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4304. 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;
  4305. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4306. const uint64_t ids_sz = nbi2;
  4307. const uint64_t d_sz = sizeof(float) * d_ne;
  4308. vk_pipeline to_fp16_vk_0 = nullptr;
  4309. vk_pipeline to_fp16_vk_1 = nullptr;
  4310. if (x_non_contig) {
  4311. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4312. }
  4313. if (y_non_contig) {
  4314. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4315. } else {
  4316. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4317. }
  4318. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  4319. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4320. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4321. GGML_ASSERT(dmmv != nullptr);
  4322. if (dryrun) {
  4323. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4324. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4325. if (
  4326. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4327. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4328. GGML_ABORT("Requested preallocation size is too large");
  4329. }
  4330. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4331. ctx->prealloc_size_x = x_sz_upd;
  4332. }
  4333. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4334. ctx->prealloc_size_y = y_sz_upd;
  4335. }
  4336. // Request descriptor sets
  4337. if (qx_needs_dequant) {
  4338. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4339. }
  4340. if (qy_needs_dequant) {
  4341. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4342. }
  4343. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  4344. return;
  4345. }
  4346. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4347. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4348. GGML_ASSERT(d_D != nullptr);
  4349. vk_buffer d_X;
  4350. uint64_t x_buf_offset = 0;
  4351. vk_buffer d_Y;
  4352. uint64_t y_buf_offset = 0;
  4353. if(!src0_uma) {
  4354. d_Qx = src0_buf_ctx->dev_buffer;
  4355. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4356. GGML_ASSERT(d_Qx != nullptr);
  4357. }
  4358. if(!src1_uma) {
  4359. d_Qy = src1_buf_ctx->dev_buffer;
  4360. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4361. GGML_ASSERT(d_Qy != nullptr);
  4362. }
  4363. if(!ids_uma) {
  4364. d_ids = ids_buf_ctx->dev_buffer;
  4365. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4366. GGML_ASSERT(d_ids != nullptr);
  4367. }
  4368. if (qx_needs_dequant) {
  4369. d_X = ctx->prealloc_x;
  4370. } else {
  4371. d_X = d_Qx;
  4372. x_buf_offset = qx_buf_offset;
  4373. GGML_ASSERT(qx_sz == x_sz);
  4374. }
  4375. if (qy_needs_dequant) {
  4376. d_Y = ctx->prealloc_y;
  4377. } else {
  4378. d_Y = d_Qy;
  4379. y_buf_offset = qy_buf_offset;
  4380. GGML_ASSERT(qy_sz == y_sz);
  4381. }
  4382. if (x_non_contig) {
  4383. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4384. 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 });
  4385. }
  4386. if (y_non_contig) {
  4387. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4388. 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 });
  4389. }
  4390. uint32_t stride_batch_y = ne10*ne11;
  4391. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4392. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4393. }
  4394. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4395. uint32_t groups_x = ne01;
  4396. uint32_t groups_z = 1;
  4397. if (ne01 > max_groups_x) {
  4398. groups_z = 64;
  4399. groups_x = CEIL_DIV(groups_x, groups_z);
  4400. }
  4401. // compute
  4402. const vk_mat_vec_id_push_constants pc = {
  4403. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4404. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  4405. (uint32_t)nei0, (uint32_t)ne11,
  4406. };
  4407. ggml_vk_sync_buffers(subctx);
  4408. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4409. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  4410. vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23}, vk_subbuffer{ d_ids, ids_buf_offset, ids_sz } },
  4411. sizeof(vk_mat_vec_id_push_constants), &pc, { groups_x, (uint32_t)nei0, groups_z });
  4412. }
  4413. static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  4414. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  4415. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4416. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4417. } else {
  4418. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4419. }
  4420. }
  4421. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, ggml_tensor * dst, bool dryrun = false) {
  4422. VK_LOG_DEBUG("ggml_vk_flash_attn((" << q << ", name=" << q->name << ", type=" << q->type << ", ne0=" << q->ne[0] << ", ne1=" << q->ne[1] << ", ne2=" << q->ne[2] << ", ne3=" << q->ne[3] << ", nb0=" << q->nb[0] << ", nb1=" << q->nb[1] << ", nb2=" << q->nb[2] << ", nb3=" << q->nb[3];
  4423. std::cerr << "), (" << k << ", name=" << k->name << ", type=" << k->type << ", ne0=" << k->ne[0] << ", ne1=" << k->ne[1] << ", ne2=" << k->ne[2] << ", ne3=" << k->ne[3] << ", nb0=" << k->nb[0] << ", nb1=" << k->nb[1] << ", nb2=" << k->nb[2] << ", nb3=" << k->nb[3];
  4424. std::cerr << "), (" << v << ", name=" << v->name << ", type=" << v->type << ", ne0=" << v->ne[0] << ", ne1=" << v->ne[1] << ", ne2=" << v->ne[2] << ", ne3=" << v->ne[3] << ", nb0=" << v->nb[0] << ", nb1=" << v->nb[1] << ", nb2=" << v->nb[2] << ", nb3=" << v->nb[3];
  4425. 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];
  4426. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4427. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  4428. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  4429. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  4430. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  4431. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  4432. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  4433. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  4434. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  4435. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  4436. const uint32_t nbm1 = mask ? mask->nb[1] : 0;
  4437. const uint32_t D = neq0;
  4438. const uint32_t N = neq1;
  4439. const uint32_t KV = nek1;
  4440. GGML_ASSERT(ne0 == D);
  4441. GGML_ASSERT(ne2 == N);
  4442. // input tensor rows must be contiguous
  4443. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  4444. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  4445. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  4446. GGML_ASSERT(neq0 == D);
  4447. GGML_ASSERT(nek0 == D);
  4448. GGML_ASSERT(nev0 == D);
  4449. GGML_ASSERT(neq1 == N);
  4450. GGML_ASSERT(nev0 == D);
  4451. GGML_ASSERT(nev1 == nek1);
  4452. // dst cannot be transposed or permuted
  4453. GGML_ASSERT(nb0 == sizeof(float));
  4454. GGML_ASSERT(nb0 <= nb1);
  4455. GGML_ASSERT(nb1 <= nb2);
  4456. GGML_ASSERT(nb2 <= nb3);
  4457. assert(dst->type == GGML_TYPE_F32);
  4458. assert(q->type == GGML_TYPE_F32);
  4459. assert(k->type == v->type);
  4460. vk_pipeline *pipelines;
  4461. // XXX TODO other backends may be changing accumulator precision to default to f32 soon
  4462. bool f32acc = dst->op_params[3] == GGML_PREC_F32;
  4463. bool small_rows = N <= flash_attention_num_small_rows;
  4464. switch (D) {
  4465. case 64: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D64[k->type][f32acc][small_rows][0]; break;
  4466. case 80: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D80[k->type][f32acc][small_rows][0]; break;
  4467. case 96: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D96[k->type][f32acc][small_rows][0]; break;
  4468. case 112: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D112[k->type][f32acc][small_rows][0]; break;
  4469. case 128: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D128[k->type][f32acc][small_rows][0]; break;
  4470. case 256: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D256[k->type][f32acc][small_rows][0]; break;
  4471. default:
  4472. assert(!"unsupported D value");
  4473. return;
  4474. }
  4475. assert(pipelines);
  4476. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  4477. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  4478. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  4479. bool aligned = (KV % pipelines[1]->align) == 0 &&
  4480. // the "aligned" shader variant will forcibly align strides, for performance
  4481. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  4482. vk_pipeline pipeline = pipelines[aligned];
  4483. assert(pipeline);
  4484. if (dryrun) {
  4485. // Request descriptor sets
  4486. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4487. return;
  4488. }
  4489. float scale = 1.0f;
  4490. float max_bias = 0.0f;
  4491. float logit_softcap = 0.0f;
  4492. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  4493. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  4494. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  4495. if (logit_softcap != 0) {
  4496. scale /= logit_softcap;
  4497. }
  4498. const uint32_t n_head_kv = neq2;
  4499. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  4500. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  4501. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  4502. ggml_vk_sync_buffers(subctx);
  4503. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  4504. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  4505. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  4506. if (ctx->device->uma) {
  4507. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  4508. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  4509. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  4510. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  4511. Q_uma = d_Q != nullptr;
  4512. K_uma = d_K != nullptr;
  4513. V_uma = d_V != nullptr;
  4514. D_uma = d_D != nullptr;
  4515. if (mask) {
  4516. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  4517. M_uma = d_M != nullptr;
  4518. }
  4519. }
  4520. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4521. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  4522. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  4523. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  4524. if (!Q_uma) {
  4525. d_Q = q_buf_ctx->dev_buffer;
  4526. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  4527. }
  4528. if (!K_uma) {
  4529. d_K = k_buf_ctx->dev_buffer;
  4530. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  4531. }
  4532. if (!V_uma) {
  4533. d_V = v_buf_ctx->dev_buffer;
  4534. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  4535. }
  4536. if (!D_uma) {
  4537. d_D = d_buf_ctx->dev_buffer;
  4538. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4539. }
  4540. if (!M_uma) {
  4541. d_M = d_Q;
  4542. m_buf_offset = q_buf_offset;
  4543. if (mask) {
  4544. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  4545. d_M = m_buf_ctx->dev_buffer;
  4546. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  4547. }
  4548. }
  4549. const vk_flash_attn_push_constants pc = { N, KV,
  4550. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  4551. (uint32_t)neq2, (uint32_t)neq3,
  4552. (uint32_t)nek2, (uint32_t)nek3,
  4553. (uint32_t)nev2, (uint32_t)nev3,
  4554. nem1,
  4555. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  4556. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  4557. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  4558. nbm1,
  4559. scale, max_bias, logit_softcap,
  4560. mask != nullptr, n_head_log2, m0, m1 };
  4561. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  4562. {
  4563. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  4564. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  4565. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  4566. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  4567. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  4568. },
  4569. sizeof(vk_flash_attn_push_constants), &pc, { (uint32_t)neq1, (uint32_t)neq2, (uint32_t)neq3 });
  4570. }
  4571. static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op) {
  4572. switch (op) {
  4573. case GGML_OP_GET_ROWS:
  4574. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4575. if (dst->type == GGML_TYPE_F16) {
  4576. return ctx->device->pipeline_get_rows[src0->type];
  4577. }
  4578. if (dst->type == GGML_TYPE_F32) {
  4579. return ctx->device->pipeline_get_rows_f32[src0->type];
  4580. }
  4581. return nullptr;
  4582. case GGML_OP_ACC:
  4583. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4584. return ctx->device->pipeline_acc_f32;
  4585. }
  4586. return nullptr;
  4587. case GGML_OP_ADD:
  4588. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4589. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f32_norepeat : ctx->device->pipeline_add_f32;
  4590. }
  4591. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4592. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f16_f32_f16_norepeat : ctx->device->pipeline_add_f16_f32_f16;
  4593. }
  4594. return nullptr;
  4595. case GGML_OP_SUB:
  4596. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4597. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_f32_norepeat : ctx->device->pipeline_sub_f32;
  4598. }
  4599. return nullptr;
  4600. case GGML_OP_MUL:
  4601. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4602. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_f32_norepeat : ctx->device->pipeline_mul_f32;
  4603. }
  4604. return nullptr;
  4605. case GGML_OP_DIV:
  4606. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4607. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_f32_norepeat : ctx->device->pipeline_div_f32;
  4608. }
  4609. return nullptr;
  4610. case GGML_OP_CONCAT:
  4611. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4612. return ctx->device->pipeline_concat_f32;
  4613. }
  4614. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4615. return ctx->device->pipeline_concat_f16;
  4616. }
  4617. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  4618. return ctx->device->pipeline_concat_i32;
  4619. }
  4620. return nullptr;
  4621. case GGML_OP_UPSCALE:
  4622. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4623. return ctx->device->pipeline_upscale_f32;
  4624. }
  4625. return nullptr;
  4626. case GGML_OP_SCALE:
  4627. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4628. return ctx->device->pipeline_scale_f32;
  4629. }
  4630. return nullptr;
  4631. case GGML_OP_SQR:
  4632. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4633. return ctx->device->pipeline_sqr_f32;
  4634. }
  4635. return nullptr;
  4636. case GGML_OP_SIN:
  4637. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4638. return ctx->device->pipeline_sin_f32;
  4639. }
  4640. return nullptr;
  4641. case GGML_OP_COS:
  4642. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4643. return ctx->device->pipeline_cos_f32;
  4644. }
  4645. return nullptr;
  4646. case GGML_OP_CLAMP:
  4647. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4648. return ctx->device->pipeline_clamp_f32;
  4649. }
  4650. return nullptr;
  4651. case GGML_OP_PAD:
  4652. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4653. return ctx->device->pipeline_pad_f32;
  4654. }
  4655. return nullptr;
  4656. case GGML_OP_REPEAT:
  4657. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  4658. return ctx->device->pipeline_repeat_f32;
  4659. }
  4660. return nullptr;
  4661. case GGML_OP_REPEAT_BACK:
  4662. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4663. return ctx->device->pipeline_repeat_back_f32;
  4664. }
  4665. return nullptr;
  4666. case GGML_OP_CPY:
  4667. case GGML_OP_CONT:
  4668. case GGML_OP_DUP:
  4669. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  4670. case GGML_OP_SILU_BACK:
  4671. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4672. return ctx->device->pipeline_silu_back_f32;
  4673. }
  4674. return nullptr;
  4675. case GGML_OP_NORM:
  4676. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4677. return ctx->device->pipeline_norm_f32;
  4678. }
  4679. return nullptr;
  4680. case GGML_OP_GROUP_NORM:
  4681. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4682. return ctx->device->pipeline_group_norm_f32;
  4683. }
  4684. return nullptr;
  4685. case GGML_OP_RMS_NORM:
  4686. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4687. return ctx->device->pipeline_rms_norm_f32;
  4688. }
  4689. return nullptr;
  4690. case GGML_OP_RMS_NORM_BACK:
  4691. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4692. return ctx->device->pipeline_rms_norm_back_f32;
  4693. }
  4694. return nullptr;
  4695. case GGML_OP_L2_NORM:
  4696. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4697. return ctx->device->pipeline_l2_norm_f32;
  4698. }
  4699. return nullptr;
  4700. case GGML_OP_UNARY:
  4701. switch (ggml_get_unary_op(dst)) {
  4702. case GGML_UNARY_OP_SILU:
  4703. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4704. return ctx->device->pipeline_silu_f32;
  4705. }
  4706. break;
  4707. case GGML_UNARY_OP_GELU:
  4708. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4709. return ctx->device->pipeline_gelu_f32;
  4710. }
  4711. break;
  4712. case GGML_UNARY_OP_GELU_QUICK:
  4713. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4714. return ctx->device->pipeline_gelu_quick_f32;
  4715. }
  4716. break;
  4717. case GGML_UNARY_OP_RELU:
  4718. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4719. return ctx->device->pipeline_relu_f32;
  4720. }
  4721. break;
  4722. case GGML_UNARY_OP_TANH:
  4723. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4724. return ctx->device->pipeline_tanh_f32;
  4725. }
  4726. break;
  4727. case GGML_UNARY_OP_SIGMOID:
  4728. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4729. return ctx->device->pipeline_sigmoid_f32;
  4730. }
  4731. break;
  4732. default:
  4733. break;
  4734. }
  4735. return nullptr;
  4736. case GGML_OP_DIAG_MASK_INF:
  4737. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4738. return ctx->device->pipeline_diag_mask_inf_f32;
  4739. }
  4740. return nullptr;
  4741. case GGML_OP_SOFT_MAX:
  4742. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  4743. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  4744. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  4745. }
  4746. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  4747. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  4748. }
  4749. return nullptr;
  4750. case GGML_OP_SOFT_MAX_BACK:
  4751. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4752. return ctx->device->pipeline_soft_max_back_f32;
  4753. }
  4754. return nullptr;
  4755. case GGML_OP_ROPE:
  4756. case GGML_OP_ROPE_BACK:
  4757. {
  4758. const int mode = ((const int32_t *) dst->op_params)[2];
  4759. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  4760. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  4761. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  4762. if (is_neox) {
  4763. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4764. return ctx->device->pipeline_rope_neox_f32;
  4765. }
  4766. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4767. return ctx->device->pipeline_rope_neox_f16;
  4768. }
  4769. } else if (is_mrope && !is_vision) {
  4770. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4771. return ctx->device->pipeline_rope_multi_f32;
  4772. }
  4773. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4774. return ctx->device->pipeline_rope_multi_f16;
  4775. }
  4776. } else if (is_vision) {
  4777. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4778. return ctx->device->pipeline_rope_vision_f32;
  4779. }
  4780. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4781. return ctx->device->pipeline_rope_vision_f16;
  4782. }
  4783. } else {
  4784. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4785. return ctx->device->pipeline_rope_norm_f32;
  4786. }
  4787. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4788. return ctx->device->pipeline_rope_norm_f16;
  4789. }
  4790. }
  4791. return nullptr;
  4792. }
  4793. case GGML_OP_ARGSORT:
  4794. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  4795. return ctx->device->pipeline_argsort_f32;
  4796. }
  4797. return nullptr;
  4798. case GGML_OP_SUM:
  4799. case GGML_OP_SUM_ROWS:
  4800. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4801. return ctx->device->pipeline_sum_rows_f32;
  4802. }
  4803. return nullptr;
  4804. case GGML_OP_ARGMAX:
  4805. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  4806. return ctx->device->pipeline_argmax_f32;
  4807. }
  4808. return nullptr;
  4809. case GGML_OP_COUNT_EQUAL:
  4810. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  4811. return ctx->device->pipeline_count_equal_i32;
  4812. }
  4813. return nullptr;
  4814. case GGML_OP_IM2COL:
  4815. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4816. return ctx->device->pipeline_im2col_f32;
  4817. }
  4818. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4819. return ctx->device->pipeline_im2col_f32_f16;
  4820. }
  4821. return nullptr;
  4822. case GGML_OP_TIMESTEP_EMBEDDING:
  4823. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4824. return ctx->device->pipeline_timestep_embedding_f32;
  4825. }
  4826. return nullptr;
  4827. case GGML_OP_POOL_2D:
  4828. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4829. return ctx->device->pipeline_pool2d_f32;
  4830. }
  4831. return nullptr;
  4832. case GGML_OP_RWKV_WKV6:
  4833. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4834. return ctx->device->pipeline_rwkv_wkv6_f32;
  4835. }
  4836. return nullptr;
  4837. case GGML_OP_RWKV_WKV7:
  4838. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4839. return ctx->device->pipeline_rwkv_wkv7_f32;
  4840. }
  4841. return nullptr;
  4842. case GGML_OP_OPT_STEP_ADAMW:
  4843. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4844. return ctx->device->pipeline_opt_step_adamw_f32;
  4845. }
  4846. return nullptr;
  4847. case GGML_OP_LEAKY_RELU:
  4848. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4849. return ctx->device->pipeline_leaky_relu_f32;
  4850. }
  4851. return nullptr;
  4852. default:
  4853. return nullptr;
  4854. }
  4855. GGML_UNUSED(src2);
  4856. }
  4857. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  4858. switch (op) {
  4859. case GGML_OP_CPY:
  4860. case GGML_OP_GET_ROWS:
  4861. case GGML_OP_ADD:
  4862. case GGML_OP_SUB:
  4863. case GGML_OP_MUL:
  4864. case GGML_OP_DIV:
  4865. case GGML_OP_CONCAT:
  4866. case GGML_OP_UPSCALE:
  4867. case GGML_OP_SQR:
  4868. case GGML_OP_SIN:
  4869. case GGML_OP_COS:
  4870. case GGML_OP_CLAMP:
  4871. case GGML_OP_PAD:
  4872. case GGML_OP_REPEAT:
  4873. case GGML_OP_REPEAT_BACK:
  4874. case GGML_OP_ROPE:
  4875. return true;
  4876. default:
  4877. return false;
  4878. }
  4879. }
  4880. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  4881. {
  4882. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  4883. }
  4884. template <typename T> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, T &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  4885. GGML_UNUSED(p);
  4886. GGML_UNUSED(src0);
  4887. GGML_UNUSED(src1);
  4888. GGML_UNUSED(src2);
  4889. GGML_UNUSED(dst);
  4890. static_assert(!std::is_const<T>::value, "unexpected type");
  4891. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  4892. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  4893. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  4894. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  4895. }
  4896. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_unary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  4897. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  4898. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  4899. p.misalign_offsets = (a_offset << 16) | d_offset;
  4900. GGML_UNUSED(src1);
  4901. GGML_UNUSED(src2);
  4902. }
  4903. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_binary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  4904. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  4905. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  4906. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  4907. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  4908. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  4909. GGML_UNUSED(src2);
  4910. }
  4911. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_upscale_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  4912. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  4913. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  4914. p.a_offset = a_offset;
  4915. p.d_offset = d_offset;
  4916. GGML_UNUSED(src1);
  4917. GGML_UNUSED(src2);
  4918. }
  4919. template<typename PC>
  4920. static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op, PC&& pc, bool dryrun = false) {
  4921. VK_LOG_DEBUG("ggml_vk_op_f32((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4922. if (src1 != nullptr) {
  4923. 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];
  4924. }
  4925. if (src2 != nullptr) {
  4926. std::cerr << "), (" << src2 << ", name=" << src2->name << ", type=" << src2->type << ", ne0=" << src2->ne[0] << ", ne1=" << src2->ne[1] << ", ne2=" << src2->ne[2] << ", ne3=" << src2->ne[3] << ", nb0=" << src2->nb[0] << ", nb1=" << src2->nb[1] << ", nb2=" << src2->nb[2] << ", nb3=" << src2->nb[3];
  4927. }
  4928. 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];
  4929. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  4930. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  4931. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  4932. GGML_ASSERT(dst->buffer != nullptr);
  4933. const uint64_t ne00 = src0->ne[0];
  4934. const uint64_t ne01 = src0->ne[1];
  4935. const uint64_t ne02 = src0->ne[2];
  4936. const uint64_t ne03 = src0->ne[3];
  4937. const uint64_t ne0 = ne00 * ne01;
  4938. const bool use_src1 = src1 != nullptr;
  4939. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  4940. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  4941. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  4942. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  4943. const uint64_t ne1 = ne10 * ne11;
  4944. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  4945. const bool use_src2 = src2 != nullptr;
  4946. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  4947. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  4948. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  4949. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  4950. const uint64_t ne2 = ne20 * ne21;
  4951. const uint64_t ned0 = dst->ne[0];
  4952. const uint64_t ned1 = dst->ne[1];
  4953. const uint64_t ned2 = dst->ne[2];
  4954. const uint64_t ned3 = dst->ne[3];
  4955. const uint64_t ned = ned0 * ned1;
  4956. init_pushconst_fastdiv(pc);
  4957. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  4958. if (pipeline == nullptr) {
  4959. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  4960. if (src1 != nullptr) {
  4961. std::cerr << " and " << ggml_type_name(src1->type);
  4962. }
  4963. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  4964. GGML_ABORT("fatal error");
  4965. }
  4966. if (dryrun) {
  4967. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4968. return;
  4969. }
  4970. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  4971. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4972. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4973. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  4974. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  4975. vk_buffer d_X = nullptr;
  4976. size_t x_buf_offset = 0;
  4977. vk_buffer d_Y = nullptr;
  4978. size_t y_buf_offset = 0;
  4979. vk_buffer d_Z = nullptr;
  4980. size_t z_buf_offset = 0;
  4981. bool src0_uma = false;
  4982. bool src1_uma = false;
  4983. bool src2_uma = false;
  4984. if (ctx->device->uma) {
  4985. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  4986. src0_uma = d_X != nullptr;
  4987. if (use_src1) {
  4988. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  4989. src1_uma = d_Y != nullptr;
  4990. }
  4991. if (use_src2) {
  4992. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  4993. src2_uma = d_Z != nullptr;
  4994. }
  4995. }
  4996. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  4997. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  4998. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  4999. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  5000. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5001. // Workaround for tiny tensor inputs on ROPE
  5002. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  5003. y_sz = VK_WHOLE_SIZE;
  5004. }
  5005. GGML_ASSERT(d_D != nullptr);
  5006. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5007. if(!src0_uma) {
  5008. d_X = src0_buf_ctx->dev_buffer;
  5009. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5010. GGML_ASSERT(d_X != nullptr);
  5011. }
  5012. if (use_src1 && !src1_uma) {
  5013. d_Y = src1_buf_ctx->dev_buffer;
  5014. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5015. GGML_ASSERT(d_Y != nullptr);
  5016. }
  5017. if (use_src2 && !src2_uma) {
  5018. d_Z = src2_buf_ctx->dev_buffer;
  5019. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  5020. GGML_ASSERT(d_Z != nullptr);
  5021. }
  5022. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  5023. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  5024. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5025. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5026. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5027. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5028. if (op_supports_incontiguous) {
  5029. x_sz = ggml_nbytes(src0);
  5030. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  5031. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  5032. d_sz = ggml_nbytes(dst);
  5033. if (x_buf_offset + x_sz >= d_X->size) {
  5034. x_sz = VK_WHOLE_SIZE;
  5035. }
  5036. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  5037. y_sz = VK_WHOLE_SIZE;
  5038. }
  5039. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  5040. z_sz = VK_WHOLE_SIZE;
  5041. }
  5042. if (d_buf_offset + d_sz >= d_D->size) {
  5043. d_sz = VK_WHOLE_SIZE;
  5044. }
  5045. }
  5046. std::array<uint32_t, 3> elements;
  5047. // Single call if dimension 2 is contiguous
  5048. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  5049. switch (op) {
  5050. case GGML_OP_NORM:
  5051. case GGML_OP_RMS_NORM:
  5052. case GGML_OP_RMS_NORM_BACK:
  5053. case GGML_OP_L2_NORM:
  5054. case GGML_OP_SOFT_MAX:
  5055. case GGML_OP_SOFT_MAX_BACK:
  5056. case GGML_OP_SUM_ROWS:
  5057. case GGML_OP_ARGMAX:
  5058. {
  5059. const uint32_t nr = ggml_nrows(src0);
  5060. if (nr > 262144) {
  5061. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  5062. } else if (nr > 512) {
  5063. elements = { 512, CEIL_DIV(nr, 512), 1 };
  5064. } else {
  5065. elements = { nr, 1, 1 };
  5066. }
  5067. } break;
  5068. case GGML_OP_SUM:
  5069. // We use GGML_OP_SUM_ROWS with 1 row.
  5070. elements = { 1, 1, 1 };
  5071. break;
  5072. case GGML_OP_GROUP_NORM:
  5073. {
  5074. const uint32_t num_groups = dst->op_params[0];
  5075. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  5076. } break;
  5077. case GGML_OP_DIAG_MASK_INF:
  5078. case GGML_OP_ROPE:
  5079. case GGML_OP_ROPE_BACK:
  5080. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  5081. break;
  5082. case GGML_OP_GET_ROWS:
  5083. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  5084. break;
  5085. case GGML_OP_ARGSORT:
  5086. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  5087. break;
  5088. case GGML_OP_IM2COL:
  5089. {
  5090. const bool is_2D = dst->op_params[6] == 1;
  5091. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5092. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5093. const uint32_t KW = src0->ne[0];
  5094. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5095. const uint32_t OW = dst->ne[1];
  5096. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  5097. elements = { OW * KW * KH, OH, batch * IC };
  5098. } break;
  5099. case GGML_OP_TIMESTEP_EMBEDDING:
  5100. {
  5101. const uint32_t dim = dst->op_params[0];
  5102. uint32_t half_ceil = (dim + 1) / 2;
  5103. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  5104. } break;
  5105. case GGML_OP_POOL_2D:
  5106. {
  5107. const uint32_t N = dst->ne[3];
  5108. const uint32_t OC = dst->ne[2];
  5109. const uint32_t OH = dst->ne[1];
  5110. const uint32_t OW = dst->ne[0];
  5111. elements = { N * OC * OH * OW, 1, 1};
  5112. } break;
  5113. case GGML_OP_ADD:
  5114. case GGML_OP_SUB:
  5115. case GGML_OP_DIV:
  5116. case GGML_OP_MUL:
  5117. case GGML_OP_SCALE:
  5118. case GGML_OP_SQR:
  5119. case GGML_OP_SIN:
  5120. case GGML_OP_COS:
  5121. case GGML_OP_CLAMP:
  5122. case GGML_OP_PAD:
  5123. case GGML_OP_REPEAT:
  5124. case GGML_OP_REPEAT_BACK:
  5125. case GGML_OP_CPY:
  5126. case GGML_OP_CONCAT:
  5127. case GGML_OP_UPSCALE:
  5128. case GGML_OP_UNARY:
  5129. {
  5130. const uint32_t ne = ggml_nelements(dst);
  5131. if (ne > 262144) {
  5132. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  5133. } else if (ne > 512) {
  5134. elements = { 512, CEIL_DIV(ne, 512), 1 };
  5135. } else {
  5136. elements = { ne, 1, 1 };
  5137. }
  5138. } break;
  5139. default:
  5140. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  5141. break;
  5142. }
  5143. if (!op_supports_incontiguous) {
  5144. if (x_sz != VK_WHOLE_SIZE) {
  5145. x_sz *= ne02 * ne03;
  5146. }
  5147. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  5148. y_sz *= ne12 * ne13;
  5149. }
  5150. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  5151. z_sz *= ne22 * ne23;
  5152. }
  5153. if (d_sz != VK_WHOLE_SIZE) {
  5154. d_sz *= ned2 * ned3;
  5155. }
  5156. }
  5157. if (op == GGML_OP_SOFT_MAX) {
  5158. // Empty src1 is possible in soft_max, but the shader needs a buffer
  5159. vk_subbuffer subbuf_y;
  5160. if (use_src1) {
  5161. subbuf_y = { d_Y, y_buf_offset, y_sz };
  5162. } else {
  5163. subbuf_y = { d_X, 0, x_sz };
  5164. }
  5165. ggml_vk_sync_buffers(subctx);
  5166. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5167. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  5168. // Empty src2 is possible in rope, but the shader needs a buffer
  5169. vk_subbuffer subbuf_z;
  5170. if (use_src2) {
  5171. subbuf_z = { d_Z, z_buf_offset, z_sz };
  5172. } else {
  5173. subbuf_z = { d_X, 0, x_sz };
  5174. }
  5175. ggml_vk_sync_buffers(subctx);
  5176. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5177. } else if (op == GGML_OP_IM2COL) {
  5178. // im2col uses only src1 and dst buffers
  5179. ggml_vk_sync_buffers(subctx);
  5180. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5181. } else if (op == GGML_OP_COUNT_EQUAL) {
  5182. ggml_vk_sync_buffers(subctx);
  5183. // count_equal assumes that destination buffer is initialized with zeroes
  5184. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  5185. ggml_vk_sync_buffers(subctx);
  5186. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5187. } else if (use_src2) {
  5188. ggml_vk_sync_buffers(subctx);
  5189. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5190. } else if (use_src1) {
  5191. ggml_vk_sync_buffers(subctx);
  5192. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5193. } else {
  5194. ggml_vk_sync_buffers(subctx);
  5195. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5196. }
  5197. }
  5198. static void ggml_vk_get_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5199. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5200. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5201. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5202. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  5203. (uint32_t)ggml_nelements(src0),
  5204. (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,
  5205. (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,
  5206. (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,
  5207. 0,
  5208. 0.0f, 0.0f, 0,
  5209. }, dryrun);
  5210. }
  5211. static void ggml_vk_acc(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5212. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5213. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5214. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5215. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5216. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5217. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5218. int offset = dst->op_params[3] / 4; // offset in bytes
  5219. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  5220. (uint32_t)ggml_nelements(src0),
  5221. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)nb1, (uint32_t)nb2, (uint32_t)src0->nb[3] / src0_type_size,
  5222. (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,
  5223. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t)nb1, (uint32_t)nb2, (uint32_t) dst->nb[3] / dst_type_size,
  5224. 0,
  5225. 0.0f, 0.0f, offset,
  5226. }, dryrun);
  5227. }
  5228. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5229. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5230. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5231. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5232. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  5233. (uint32_t)ggml_nelements(src0),
  5234. (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,
  5235. (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,
  5236. (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,
  5237. 0,
  5238. 0.0f, 0.0f, 0,
  5239. }, dryrun);
  5240. }
  5241. static void ggml_vk_sub(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5242. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5243. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5244. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5245. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  5246. (uint32_t)ggml_nelements(src0),
  5247. (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,
  5248. (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,
  5249. (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,
  5250. 0,
  5251. 0.0f, 0.0f, 0,
  5252. }, dryrun);
  5253. }
  5254. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5255. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5256. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5257. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5258. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  5259. (uint32_t)ggml_nelements(src0),
  5260. (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,
  5261. (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,
  5262. (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,
  5263. 0,
  5264. 0.0f, 0.0f, 0,
  5265. }, dryrun);
  5266. }
  5267. static void ggml_vk_div(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5268. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5269. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5270. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5271. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  5272. (uint32_t)ggml_nelements(src0),
  5273. (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,
  5274. (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,
  5275. (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,
  5276. 0,
  5277. 0.0f, 0.0f, 0,
  5278. }, dryrun);
  5279. }
  5280. static void ggml_vk_op_f32_wkv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_rwkv_wkv6_push_constants&& pc, int version, bool dryrun = false) {
  5281. GGML_ASSERT(version == 6 || version == 7);
  5282. int num_srcs = version == 6 ? 6 : 7;
  5283. for (int i = 0; i < num_srcs; i++) {
  5284. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  5285. }
  5286. GGML_ASSERT(dst->buffer != nullptr);
  5287. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  5288. GGML_ASSERT(pipeline != nullptr);
  5289. if (dryrun) {
  5290. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5291. return;
  5292. }
  5293. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5294. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5295. for (int i = 0; i < num_srcs; i++) {
  5296. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  5297. }
  5298. ggml_vk_sync_buffers(subctx);
  5299. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5300. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5301. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  5302. if (ctx->device->uma) {
  5303. for (int i = 0; i < num_srcs; i++) {
  5304. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  5305. srcs_uma[i] = d_srcs[i] != nullptr;
  5306. }
  5307. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  5308. dst_uma = d_D != nullptr;
  5309. }
  5310. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5311. for (int i = 0; i < num_srcs; i++) {
  5312. src_sizes[i] = ggml_nbytes(dst->src[i]);
  5313. if (!srcs_uma[i]) {
  5314. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  5315. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  5316. }
  5317. }
  5318. const uint64_t dst_size = ggml_nbytes(dst);
  5319. if (!dst_uma) {
  5320. d_D = dst_buf_ctx->dev_buffer;
  5321. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  5322. }
  5323. std::array<uint32_t, 3> elements = {
  5324. (uint32_t)(pc.B * pc.H),
  5325. 1,
  5326. 1
  5327. };
  5328. if (version == 6) {
  5329. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5330. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5331. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5332. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5333. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5334. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5335. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5336. vk_subbuffer{ d_D, dst_offset, dst_size }
  5337. }, sizeof(vk_op_rwkv_wkv6_push_constants), &pc, elements);
  5338. } else if (version == 7) {
  5339. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5340. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5341. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5342. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5343. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5344. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5345. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5346. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  5347. vk_subbuffer{ d_D, dst_offset, dst_size }
  5348. }, sizeof(vk_op_rwkv_wkv7_push_constants), &pc, elements);
  5349. } else {
  5350. // shouldn't happen
  5351. GGML_ASSERT(false);
  5352. }
  5353. }
  5354. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5355. const size_t seq_length = dst->src[0]->ne[2];
  5356. const size_t n_embed = dst->ne[0];
  5357. const size_t n_heads = dst->src[0]->ne[1];
  5358. const size_t n_seqs = dst->src[5]->ne[1];
  5359. ggml_vk_op_f32_wkv(
  5360. ctx, subctx, dst,
  5361. {
  5362. (uint32_t)n_seqs,
  5363. (uint32_t)seq_length,
  5364. (uint32_t)n_embed,
  5365. (uint32_t)n_heads,
  5366. },
  5367. 6,
  5368. dryrun
  5369. );
  5370. }
  5371. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5372. const size_t seq_length = dst->src[0]->ne[2];
  5373. const size_t n_embed = dst->ne[0];
  5374. const size_t n_heads = dst->src[0]->ne[1];
  5375. const size_t n_seqs = dst->src[6]->ne[1];
  5376. ggml_vk_op_f32_wkv(
  5377. ctx, subctx, dst,
  5378. {
  5379. (uint32_t)n_seqs,
  5380. (uint32_t)seq_length,
  5381. (uint32_t)n_embed,
  5382. (uint32_t)n_heads,
  5383. },
  5384. 7,
  5385. dryrun
  5386. );
  5387. }
  5388. static void ggml_vk_op_f32_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_push_constants&& pc, bool dryrun = false) {
  5389. const ggml_tensor * x = dst->src[0];
  5390. const ggml_tensor * g = dst->src[1];
  5391. const ggml_tensor * gm = dst->src[2];
  5392. const ggml_tensor * gv = dst->src[3];
  5393. const ggml_tensor * p = dst->src[4];
  5394. GGML_ASSERT(x->type == GGML_TYPE_F32);
  5395. GGML_ASSERT(g->type == GGML_TYPE_F32);
  5396. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  5397. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  5398. GGML_ASSERT(p->type == GGML_TYPE_F32);
  5399. GGML_ASSERT(dst->buffer != nullptr);
  5400. GGML_ASSERT(ggml_is_contiguous(x));
  5401. GGML_ASSERT(ggml_is_contiguous(g));
  5402. GGML_ASSERT(ggml_is_contiguous(gm));
  5403. GGML_ASSERT(ggml_is_contiguous(gv));
  5404. GGML_ASSERT(ggml_is_contiguous(p));
  5405. GGML_ASSERT(ggml_are_same_shape(x, g));
  5406. GGML_ASSERT(ggml_are_same_shape(x, gm));
  5407. GGML_ASSERT(ggml_are_same_shape(x, gv));
  5408. GGML_ASSERT(ggml_nelements(p) == 7);
  5409. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  5410. GGML_ASSERT(pipeline != nullptr);
  5411. if (dryrun) {
  5412. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5413. return;
  5414. }
  5415. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  5416. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  5417. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  5418. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  5419. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  5420. ggml_vk_sync_buffers(subctx);
  5421. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  5422. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  5423. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  5424. if (ctx->device->uma) {
  5425. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  5426. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  5427. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  5428. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  5429. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  5430. X_uma = d_X != nullptr;
  5431. G_uma = d_G != nullptr;
  5432. GM_uma = d_GM != nullptr;
  5433. GV_uma = d_GV != nullptr;
  5434. P_uma = d_P != nullptr;
  5435. }
  5436. if (!X_uma) {
  5437. d_X = x_buf_ctx->dev_buffer;
  5438. x_offset = vk_tensor_offset(x) + x->view_offs;
  5439. }
  5440. if (!G_uma) {
  5441. d_G = g_buf_ctx->dev_buffer;
  5442. g_offset = vk_tensor_offset(g) + g->view_offs;
  5443. }
  5444. if (!GM_uma) {
  5445. d_GM = gm_buf_ctx->dev_buffer;
  5446. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  5447. }
  5448. if (!GV_uma) {
  5449. d_GV = gv_buf_ctx->dev_buffer;
  5450. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  5451. }
  5452. if (!P_uma) {
  5453. d_P = p_buf_ctx->dev_buffer;
  5454. p_offset = vk_tensor_offset(p) + p->view_offs;
  5455. }
  5456. const uint64_t x_size = ggml_nbytes(x);
  5457. const uint64_t g_size = ggml_nbytes(g);
  5458. const uint64_t gm_size = ggml_nbytes(gm);
  5459. const uint64_t gv_size = ggml_nbytes(gv);
  5460. const uint64_t p_size = ggml_nbytes(p);
  5461. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  5462. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5463. vk_subbuffer{ d_X, x_offset, x_size },
  5464. vk_subbuffer{ d_G, g_offset, g_size },
  5465. vk_subbuffer{ d_GM, gm_offset, gm_size },
  5466. vk_subbuffer{ d_GV, gv_offset, gv_size },
  5467. vk_subbuffer{ d_P, p_offset, p_size },
  5468. }, sizeof(vk_op_push_constants), &pc, elements);
  5469. }
  5470. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5471. const size_t n = ggml_nelements(dst->src[0]);
  5472. ggml_vk_op_f32_opt_step_adamw(
  5473. ctx, subctx, dst,
  5474. { (uint32_t)n, 0, 0.0f, 0.0f },
  5475. dryrun
  5476. );
  5477. }
  5478. static void ggml_vk_concat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5479. int * op_params = (int *)dst->op_params;
  5480. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5481. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5482. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5483. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  5484. (uint32_t)ggml_nelements(dst),
  5485. (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,
  5486. (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,
  5487. (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,
  5488. 0,
  5489. 0.0f, 0.0f, op_params[0],
  5490. }, dryrun);
  5491. }
  5492. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5493. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5494. const float sf0 = (float)dst->ne[0] / src0->ne[0];
  5495. const float sf1 = (float)dst->ne[1] / src0->ne[1];
  5496. const float sf2 = (float)dst->ne[2] / src0->ne[2];
  5497. const float sf3 = (float)dst->ne[3] / src0->ne[3];
  5498. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  5499. (uint32_t)ggml_nelements(dst), 0, 0,
  5500. (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,
  5501. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  5502. sf0, sf1, sf2, sf3,
  5503. }, dryrun);
  5504. }
  5505. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5506. float * op_params = (float *)dst->op_params;
  5507. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5508. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5509. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  5510. (uint32_t)ggml_nelements(src0),
  5511. (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,
  5512. (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,
  5513. 0,
  5514. op_params[0], 0.0f,
  5515. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5516. }, dryrun);
  5517. }
  5518. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5519. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5520. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5521. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  5522. (uint32_t)ggml_nelements(src0),
  5523. (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,
  5524. (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,
  5525. 0,
  5526. 0.0f, 0.0f,
  5527. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5528. }, dryrun);
  5529. }
  5530. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5531. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5532. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5533. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, {
  5534. (uint32_t)ggml_nelements(src0),
  5535. (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,
  5536. (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,
  5537. 0,
  5538. 0.0f, 0.0f,
  5539. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5540. }, dryrun);
  5541. }
  5542. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5543. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5544. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5545. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, {
  5546. (uint32_t)ggml_nelements(src0),
  5547. (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,
  5548. (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,
  5549. 0,
  5550. 0.0f, 0.0f,
  5551. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5552. }, dryrun);
  5553. }
  5554. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5555. float * op_params = (float *)dst->op_params;
  5556. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5557. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5558. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  5559. (uint32_t)ggml_nelements(src0),
  5560. (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,
  5561. (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,
  5562. 0,
  5563. op_params[0], op_params[1],
  5564. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5565. }, dryrun);
  5566. }
  5567. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5568. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5569. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5570. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, {
  5571. (uint32_t)ggml_nelements(dst),
  5572. (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,
  5573. (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,
  5574. 0,
  5575. 0.0f, 0.0f,
  5576. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5577. }, dryrun);
  5578. }
  5579. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5580. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5581. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5582. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, {
  5583. (uint32_t)ggml_nelements(dst),
  5584. (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,
  5585. (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,
  5586. 0,
  5587. 0.0f, 0.0f,
  5588. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5589. }, dryrun);
  5590. }
  5591. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5592. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5593. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5594. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, {
  5595. (uint32_t)ggml_nelements(dst),
  5596. (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,
  5597. (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,
  5598. 0,
  5599. 0.0f, 0.0f,
  5600. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5601. }, dryrun);
  5602. }
  5603. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5604. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5605. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5606. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  5607. (uint32_t)ggml_nelements(src0),
  5608. (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,
  5609. (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,
  5610. 0,
  5611. 0.0f, 0.0f,
  5612. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5613. }, dryrun);
  5614. }
  5615. static void ggml_vk_silu_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5616. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SILU_BACK, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5617. }
  5618. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5619. float * op_params = (float *)dst->op_params;
  5620. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5621. }
  5622. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5623. const int * int_op_params = (const int *)dst->op_params;
  5624. const float * float_op_params = (const float *)dst->op_params;
  5625. const uint32_t num_groups = int_op_params[0];
  5626. const float eps = float_op_params[1];
  5627. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5628. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_GROUP_NORM, { group_size, 0, eps, 0.0f }, dryrun);
  5629. }
  5630. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5631. float * op_params = (float *)dst->op_params;
  5632. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_RMS_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5633. }
  5634. static void ggml_vk_rms_norm_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5635. float * op_params = (float *)dst->op_params;
  5636. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5637. }
  5638. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5639. float * op_params = (float *)dst->op_params;
  5640. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_L2_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5641. }
  5642. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5643. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UNARY, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5644. }
  5645. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5646. int32_t * op_params = (int32_t *)dst->op_params;
  5647. ggml_vk_op_f32<vk_op_diag_mask_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_DIAG_MASK_INF, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0] }, dryrun);
  5648. }
  5649. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5650. float * op_params = (float *)dst->op_params;
  5651. float scale = op_params[0];
  5652. float max_bias = op_params[1];
  5653. const uint32_t ncols = (uint32_t)src0->ne[0];
  5654. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  5655. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  5656. const uint32_t n_head_kv = nrows_x/nrows_y;
  5657. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5658. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5659. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5660. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  5661. ncols,
  5662. src1 != nullptr ? nrows_y : (uint32_t)0,
  5663. scale, max_bias,
  5664. m0, m1,
  5665. n_head_log2,
  5666. nrows_x,
  5667. }, dryrun);
  5668. }
  5669. static void ggml_vk_soft_max_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5670. float * op_params = (float *)dst->op_params;
  5671. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], op_params[1] }, dryrun);
  5672. }
  5673. static void ggml_vk_rope(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool backprop, bool dryrun = false) {
  5674. const int n_dims = ((int32_t *) dst->op_params)[1];
  5675. const int mode = ((int32_t *) dst->op_params)[2];
  5676. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  5677. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  5678. const float freq_base = ((float *) dst->op_params)[5];
  5679. const float freq_scale = ((float *) dst->op_params)[6];
  5680. const float ext_factor = ((float *) dst->op_params)[7];
  5681. const float attn_factor = ((float *) dst->op_params)[8];
  5682. const float beta_fast = ((float *) dst->op_params)[9];
  5683. const float beta_slow = ((float *) dst->op_params)[10];
  5684. int sections[4] {};
  5685. if (mode & GGML_ROPE_TYPE_MROPE) {
  5686. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  5687. }
  5688. float corr_dims[2];
  5689. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  5690. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5691. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  5692. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  5693. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  5694. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  5695. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  5696. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  5697. sections[0], sections[1], sections[2], sections[3], backprop
  5698. }, dryrun);
  5699. }
  5700. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5701. int32_t * op_params = (int32_t *)dst->op_params;
  5702. uint32_t ncols = src0->ne[0];
  5703. uint32_t ncols_pad = 1;
  5704. while (ncols_pad < ncols) {
  5705. ncols_pad *= 2;
  5706. }
  5707. GGML_ASSERT(ncols_pad <= 1024);
  5708. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  5709. ncols,
  5710. ncols_pad,
  5711. op_params[0],
  5712. }, dryrun);
  5713. }
  5714. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5715. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5716. }
  5717. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5718. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  5719. }
  5720. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5721. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  5722. }
  5723. static void ggml_vk_count_equal(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5724. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_COUNT_EQUAL, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5725. }
  5726. static void ggml_vk_im2col(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5727. const int32_t s0 = dst->op_params[0];
  5728. const int32_t s1 = dst->op_params[1];
  5729. const int32_t p0 = dst->op_params[2];
  5730. const int32_t p1 = dst->op_params[3];
  5731. const int32_t d0 = dst->op_params[4];
  5732. const int32_t d1 = dst->op_params[5];
  5733. const bool is_2D = dst->op_params[6] == 1;
  5734. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5735. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  5736. const uint32_t IW = src1->ne[0];
  5737. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5738. const uint32_t KW = src0->ne[0];
  5739. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5740. const uint32_t OW = dst->ne[1];
  5741. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  5742. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  5743. const uint32_t pelements = OW * KW * KH;
  5744. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  5745. batch_offset, offset_delta,
  5746. IC, IW, IH, OW, OH, KW, KH,
  5747. pelements,
  5748. IC * KH * KW,
  5749. s0, s1, p0, p1, d0, d1,
  5750. }, dryrun);
  5751. }
  5752. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5753. const uint32_t dim = dst->op_params[0];
  5754. const uint32_t max_period = dst->op_params[1];
  5755. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  5756. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  5757. nb1, dim, max_period,
  5758. }, dryrun);
  5759. }
  5760. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5761. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  5762. const int32_t k1 = dst->op_params[1];
  5763. const int32_t k0 = dst->op_params[2];
  5764. const int32_t s1 = dst->op_params[3];
  5765. const int32_t s0 = dst->op_params[4];
  5766. const int32_t p1 = dst->op_params[5];
  5767. const int32_t p0 = dst->op_params[6];
  5768. const uint32_t IH = src0->ne[1];
  5769. const uint32_t IW = src0->ne[0];
  5770. const uint32_t N = dst->ne[3];
  5771. const uint32_t OC = dst->ne[2];
  5772. const uint32_t OH = dst->ne[1];
  5773. const uint32_t OW = dst->ne[0];
  5774. const uint32_t parallel_elements = N * OC * OH * OW;
  5775. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  5776. IW, IH, OW, OH, OC,
  5777. parallel_elements,
  5778. op,
  5779. k0, k1, s0, s1, p0, p1,
  5780. }, dryrun);
  5781. }
  5782. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5783. const float * op_params = (const float *)dst->op_params;
  5784. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_LEAKY_RELU, { (uint32_t)ggml_nelements(src0), 0, op_params[0], 0.0f }, dryrun);
  5785. }
  5786. #ifdef GGML_VULKAN_RUN_TESTS
  5787. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  5788. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  5789. return;
  5790. }
  5791. i0 = std::max(i0, 5);
  5792. i1 = std::max(i1, 5);
  5793. i2 = std::max(i2, 0);
  5794. fprintf(stderr, " ");
  5795. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5796. fprintf(stderr, "%7d ", idx1);
  5797. }
  5798. fprintf(stderr, "\n");
  5799. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  5800. fprintf(stderr, "%7d: ", idx0);
  5801. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5802. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  5803. float val;
  5804. if (type == GGML_TYPE_F32) {
  5805. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  5806. } else if (type == GGML_TYPE_F16) {
  5807. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  5808. } else {
  5809. GGML_ABORT("fatal error");
  5810. }
  5811. fprintf(stderr, "% 7.2f ", val);
  5812. } else {
  5813. fprintf(stderr, " ");
  5814. }
  5815. }
  5816. fprintf(stderr, "\n");
  5817. }
  5818. }
  5819. template <typename X_TYPE, typename Y_TYPE>
  5820. 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) {
  5821. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  5822. const size_t x_ne = m * k * batch;
  5823. const size_t y_ne = k * n * batch;
  5824. const size_t d_ne = m * n * batch;
  5825. vk_pipeline p;
  5826. std::string shname;
  5827. if (shader_size == 0) {
  5828. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5829. p = ctx->device->pipeline_matmul_f32->a_s;
  5830. shname = "F32_ALIGNED_S";
  5831. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5832. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  5833. shname = "F32_F16_ALIGNED_S";
  5834. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5835. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  5836. shname = "F16_F32_ALIGNED_S";
  5837. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5838. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  5839. shname = "F16_ALIGNED_S";
  5840. } else {
  5841. GGML_ABORT("fatal error");
  5842. }
  5843. } else if (shader_size == 1) {
  5844. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5845. p = ctx->device->pipeline_matmul_f32->a_m;
  5846. shname = "F32_ALIGNED_M";
  5847. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5848. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  5849. shname = "F32_F16_ALIGNED_M";
  5850. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5851. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  5852. shname = "F16_F32_ALIGNED_M";
  5853. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5854. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  5855. shname = "F16_ALIGNED_M";
  5856. } else {
  5857. GGML_ABORT("fatal error");
  5858. }
  5859. } else if (shader_size == 2) {
  5860. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5861. p = ctx->device->pipeline_matmul_f32->a_l;
  5862. shname = "F32_ALIGNED_L";
  5863. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5864. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  5865. shname = "F32_F16_ALIGNED_L";
  5866. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5867. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  5868. shname = "F16_F32_ALIGNED_L";
  5869. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5870. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  5871. shname = "F16_ALIGNED_L";
  5872. } else {
  5873. GGML_ABORT("fatal error");
  5874. }
  5875. } else {
  5876. GGML_ASSERT(0);
  5877. }
  5878. const size_t kpad = ggml_vk_align_size(k, p->align);
  5879. if (k != kpad) {
  5880. if (shader_size == 0) {
  5881. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5882. p = ctx->device->pipeline_matmul_f32->s;
  5883. shname = "F32_S";
  5884. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5885. p = ctx->device->pipeline_matmul_f32_f16->s;
  5886. shname = "F32_F16_S";
  5887. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5888. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  5889. shname = "F16_F32_S";
  5890. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5891. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  5892. shname = "F16_S";
  5893. }
  5894. } else if (shader_size == 1) {
  5895. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5896. p = ctx->device->pipeline_matmul_f32->m;
  5897. shname = "F32_M";
  5898. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5899. p = ctx->device->pipeline_matmul_f32_f16->m;
  5900. shname = "F32_F16_M";
  5901. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5902. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  5903. shname = "F16_F32_M";
  5904. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5905. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  5906. shname = "F16_M";
  5907. }
  5908. } else if (shader_size == 2) {
  5909. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5910. p = ctx->device->pipeline_matmul_f32->l;
  5911. shname = "F32_L";
  5912. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5913. p = ctx->device->pipeline_matmul_f32_f16->l;
  5914. shname = "F32_F16_L";
  5915. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5916. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  5917. shname = "F16_F32_L";
  5918. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5919. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  5920. shname = "F16_L";
  5921. }
  5922. }
  5923. }
  5924. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  5925. if (split_k > 1) {
  5926. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  5927. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  5928. // Resize buffer
  5929. if (ctx->prealloc_split_k != nullptr) {
  5930. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  5931. }
  5932. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5933. }
  5934. }
  5935. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  5936. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5937. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5938. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5939. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  5940. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  5941. float* d = (float *) malloc(sizeof(float) * d_ne);
  5942. for (size_t i = 0; i < x_ne; i++) {
  5943. if (std::is_same<float, X_TYPE>()) {
  5944. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  5945. // x[i] = 1.0f;
  5946. // x[i] = i + 1;
  5947. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  5948. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  5949. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  5950. // x[i] = ggml_fp32_to_fp16(1.0f);
  5951. // x[i] = ggml_fp32_to_fp16(i + 1);
  5952. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  5953. } else {
  5954. GGML_ABORT("fatal error");
  5955. }
  5956. }
  5957. for (size_t i = 0; i < y_ne; i++) {
  5958. if (std::is_same<float, Y_TYPE>()) {
  5959. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  5960. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  5961. // y[i] = i + 1;
  5962. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5963. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  5964. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  5965. // y[i] = ggml_fp32_to_fp16(i + 1);
  5966. } else {
  5967. GGML_ABORT("fatal error");
  5968. }
  5969. }
  5970. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  5971. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  5972. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  5973. ggml_vk_ctx_begin(ctx->device, subctx);
  5974. for (size_t i = 0; i < num_it; i++) {
  5975. ggml_vk_matmul(
  5976. 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),
  5977. m, n, k,
  5978. k, k, m, k*m, k*n, m*n,
  5979. split_k, batch, batch, batch, 1, 1, n
  5980. );
  5981. }
  5982. ggml_vk_ctx_end(subctx);
  5983. auto begin = std::chrono::high_resolution_clock::now();
  5984. ggml_vk_submit(subctx, ctx->fence);
  5985. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  5986. ctx->device->device.resetFences({ ctx->fence });
  5987. auto end = std::chrono::high_resolution_clock::now();
  5988. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  5989. // copy dst to host
  5990. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  5991. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  5992. ggml_init_params iparams = {
  5993. /*.mem_size =*/ 1024*1024*1024,
  5994. /*.mem_buffer =*/ NULL,
  5995. /*.no_alloc =*/ true,
  5996. };
  5997. ggml_context * ggml_ctx = ggml_init(iparams);
  5998. ggml_type src0_type;
  5999. ggml_type src1_type;
  6000. if (std::is_same<float, X_TYPE>()) {
  6001. src0_type = GGML_TYPE_F32;
  6002. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  6003. src0_type = GGML_TYPE_F16;
  6004. } else {
  6005. GGML_ABORT("fatal error");
  6006. }
  6007. if (std::is_same<float, Y_TYPE>()) {
  6008. src1_type = GGML_TYPE_F32;
  6009. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6010. src1_type = GGML_TYPE_F16;
  6011. } else {
  6012. GGML_ABORT("fatal error");
  6013. }
  6014. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  6015. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  6016. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6017. src0_ggml->data = x;
  6018. src1_ggml->data = y;
  6019. tensor_ggml->data = d_chk;
  6020. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6021. ggml_build_forward_expand(cgraph, tensor_ggml);
  6022. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6023. ggml_free(ggml_ctx);
  6024. double avg_err = 0.0;
  6025. int first_err_n = -1;
  6026. int first_err_m = -1;
  6027. int first_err_b = -1;
  6028. for (size_t i = 0; i < m*n*batch; i++) {
  6029. double err = std::fabs(d[i] - d_chk[i]);
  6030. avg_err += err;
  6031. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6032. first_err_b = i / (m * n);
  6033. first_err_n = (i % (m * n)) / m;
  6034. first_err_m = (i % (m * n)) % m;
  6035. }
  6036. }
  6037. avg_err /= m * n;
  6038. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6039. std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  6040. if (avg_err > 0.1 || std::isnan(avg_err)) {
  6041. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6042. std::cerr << "Actual result: " << std::endl << std::endl;
  6043. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6044. std::cerr << "Expected result: " << std::endl << std::endl;
  6045. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6046. if (split_k > 1) {
  6047. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6048. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6049. std::cerr << "d_buf0: " << std::endl << std::endl;
  6050. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6051. std::cerr << "d_buf1: " << std::endl << std::endl;
  6052. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6053. std::cerr << "d_buf2: " << std::endl << std::endl;
  6054. 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);
  6055. std::cerr << "d_buf3: " << std::endl << std::endl;
  6056. 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);
  6057. free(split_k_buf);
  6058. }
  6059. }
  6060. free(d_chk);
  6061. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  6062. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  6063. ggml_vk_destroy_buffer(d_X);
  6064. ggml_vk_destroy_buffer(d_Y);
  6065. ggml_vk_destroy_buffer(d_D);
  6066. ggml_pipeline_cleanup(p);
  6067. ggml_pipeline_cleanup(ctx->device->pipeline_matmul_split_k_reduce);
  6068. free(x);
  6069. free(y);
  6070. free(d);
  6071. }
  6072. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  6073. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  6074. return;
  6075. }
  6076. i0 = std::max(i0, 5);
  6077. i1 = std::max(i1, 5);
  6078. i2 = std::max(i2, 0);
  6079. i3 = std::max(i3, 0);
  6080. fprintf(stderr, " ");
  6081. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6082. fprintf(stderr, "%7d ", idx1);
  6083. }
  6084. fprintf(stderr, "\n");
  6085. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6086. fprintf(stderr, "%7d: ", idx0);
  6087. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6088. 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]) {
  6089. float val;
  6090. if (tensor->type == GGML_TYPE_F32) {
  6091. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  6092. } else if (tensor->type == GGML_TYPE_F16) {
  6093. 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]));
  6094. } else {
  6095. GGML_ABORT("fatal error");
  6096. }
  6097. fprintf(stderr, "% 7.2f ", val);
  6098. } else {
  6099. fprintf(stderr, " ");
  6100. }
  6101. }
  6102. fprintf(stderr, "\n");
  6103. }
  6104. }
  6105. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  6106. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  6107. }
  6108. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  6109. if (quant == GGML_TYPE_F32) {
  6110. memcpy(to, from, sizeof(float) * ne);
  6111. return;
  6112. }
  6113. const auto * tt = ggml_get_type_traits(quant);
  6114. ggml_to_float_t dequant_fn = tt->to_float;
  6115. dequant_fn(from, to, ne);
  6116. }
  6117. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  6118. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  6119. const size_t x_sz = sizeof(float) * ne;
  6120. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  6121. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6122. float * x = (float *) malloc(x_sz);
  6123. void * qx = malloc(qx_sz);
  6124. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6125. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6126. float * x_ref = (float *) malloc(x_sz);
  6127. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  6128. for (size_t i = 0; i < ne; i++) {
  6129. x[i] = rand() / (float)RAND_MAX;
  6130. }
  6131. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  6132. ggml_vk_quantize_data(x, qx, ne, quant);
  6133. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  6134. ggml_pipeline_request_descriptor_sets(ctx->device, p, 1);
  6135. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6136. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6137. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6138. ggml_vk_ctx_begin(ctx->device, subctx);
  6139. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  6140. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc.size() * sizeof(int), pc.data(), { (uint32_t)ne, 1, 1});
  6141. ggml_vk_ctx_end(subctx);
  6142. auto begin = std::chrono::high_resolution_clock::now();
  6143. ggml_vk_submit(subctx, ctx->fence);
  6144. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6145. ctx->device->device.resetFences({ ctx->fence });
  6146. auto end = std::chrono::high_resolution_clock::now();
  6147. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6148. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  6149. int first_err = -1;
  6150. double avg_err = 0.0;
  6151. for (size_t i = 0; i < ne; i++) {
  6152. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  6153. avg_err += error;
  6154. if (first_err < 0 && error > 0.05) {
  6155. first_err = i;
  6156. }
  6157. }
  6158. avg_err /= ne;
  6159. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  6160. if (avg_err > 0.1) {
  6161. std::cerr << "first_error = " << first_err << std::endl;
  6162. std::cerr << "Actual result: " << std::endl << std::endl;
  6163. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6164. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  6165. }
  6166. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  6167. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6168. std::cerr << x_ref[i] << ", ";
  6169. }
  6170. std::cerr << std::endl;
  6171. }
  6172. ggml_vk_destroy_buffer(x_buf);
  6173. ggml_vk_destroy_buffer(qx_buf);
  6174. free(x);
  6175. free(qx);
  6176. free(x_ref);
  6177. free(x_chk);
  6178. }
  6179. 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) {
  6180. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  6181. const size_t x_ne = m * k * batch;
  6182. const size_t y_ne = k * n * batch;
  6183. const size_t d_ne = m * n * batch;
  6184. vk_pipeline p;
  6185. std::string shname;
  6186. if (shader_size == 0) {
  6187. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->a_s : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->a_s;
  6188. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  6189. } else if (shader_size == 1) {
  6190. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->a_m : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->a_m;
  6191. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  6192. } else if (shader_size == 2) {
  6193. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->a_l : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->a_l;
  6194. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  6195. } else {
  6196. GGML_ASSERT(0);
  6197. }
  6198. const size_t kpad = ggml_vk_align_size(k, p->align);
  6199. if (k != kpad) {
  6200. if (shader_size == 0) {
  6201. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->s : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->s;
  6202. shname = std::string(ggml_type_name(quant)) + "_S";
  6203. } else if (shader_size == 1) {
  6204. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->m : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->m;
  6205. shname = std::string(ggml_type_name(quant)) + "_M";
  6206. } else if (shader_size == 2) {
  6207. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->l : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->l;
  6208. shname = std::string(ggml_type_name(quant)) + "_L";
  6209. } else {
  6210. GGML_ASSERT(0);
  6211. }
  6212. }
  6213. const size_t x_sz = sizeof(float) * x_ne;
  6214. const size_t y_sz = sizeof(float) * y_ne;
  6215. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6216. const size_t d_sz = sizeof(float) * d_ne;
  6217. float * x = (float *) malloc(x_sz);
  6218. float * y = (float *) malloc(y_sz);
  6219. void * qx = malloc(qx_sz);
  6220. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6221. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6222. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6223. float * d = (float *) malloc(d_sz);
  6224. float * d_chk = (float *) malloc(d_sz);
  6225. for (size_t i = 0; i < x_ne; i++) {
  6226. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6227. }
  6228. ggml_vk_quantize_data(x, qx, x_ne, quant);
  6229. for (size_t i = 0; i < y_ne; i++) {
  6230. // y[i] = rand() / (float)RAND_MAX;
  6231. y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6232. }
  6233. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  6234. if (split_k > 1) {
  6235. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6236. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6237. // Resize buffer
  6238. if (ctx->prealloc_split_k != nullptr) {
  6239. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6240. }
  6241. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6242. }
  6243. }
  6244. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6245. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6246. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  6247. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6248. ggml_vk_ctx_begin(ctx->device, subctx);
  6249. for (size_t i = 0; i < num_it; i++) {
  6250. ggml_vk_matmul(
  6251. 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),
  6252. m, n, k,
  6253. k, k, m, k*m, k*n, m*n,
  6254. split_k, batch, batch, batch, 1, 1, n
  6255. );
  6256. }
  6257. ggml_vk_ctx_end(subctx);
  6258. auto begin = std::chrono::high_resolution_clock::now();
  6259. ggml_vk_submit(subctx, ctx->fence);
  6260. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6261. ctx->device->device.resetFences({ ctx->fence });
  6262. auto end = std::chrono::high_resolution_clock::now();
  6263. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6264. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  6265. ggml_init_params iparams = {
  6266. /*.mem_size =*/ 1024*1024*1024,
  6267. /*.mem_buffer =*/ NULL,
  6268. /*.no_alloc =*/ true,
  6269. };
  6270. ggml_context * ggml_ctx = ggml_init(iparams);
  6271. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  6272. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  6273. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6274. src0_ggml->data = qx;
  6275. src1_ggml->data = y;
  6276. tensor_ggml->data = d_chk;
  6277. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6278. ggml_build_forward_expand(cgraph, tensor_ggml);
  6279. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6280. ggml_free(ggml_ctx);
  6281. double avg_err = 0.0;
  6282. int first_err_n = -1;
  6283. int first_err_m = -1;
  6284. int first_err_b = -1;
  6285. for (size_t i = 0; i < m*n*batch; i++) {
  6286. double err = std::fabs(d[i] - d_chk[i]);
  6287. avg_err += err;
  6288. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6289. first_err_b = i / (m * n);
  6290. first_err_n = (i % (m * n)) / m;
  6291. first_err_m = (i % (m * n)) % m;
  6292. }
  6293. }
  6294. avg_err /= m * n;
  6295. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6296. std::cerr << "TEST MMQ " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  6297. if (avg_err > 0.01 || std::isnan(avg_err)) {
  6298. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6299. std::cerr << "Actual result: " << std::endl << std::endl;
  6300. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6301. std::cerr << std::endl;
  6302. std::cerr << "Expected result: " << std::endl << std::endl;
  6303. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6304. if (split_k > 1) {
  6305. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6306. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6307. std::cerr << "d_buf0: " << std::endl << std::endl;
  6308. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6309. std::cerr << "d_buf1: " << std::endl << std::endl;
  6310. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6311. std::cerr << "d_buf2: " << std::endl << std::endl;
  6312. 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);
  6313. std::cerr << "d_buf3: " << std::endl << std::endl;
  6314. 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);
  6315. free(split_k_buf);
  6316. }
  6317. }
  6318. ggml_vk_destroy_buffer(qx_buf);
  6319. ggml_vk_destroy_buffer(y_buf);
  6320. ggml_vk_destroy_buffer(d_buf);
  6321. free(x);
  6322. free(qx);
  6323. free(y);
  6324. free(d);
  6325. free(d_chk);
  6326. }
  6327. #endif
  6328. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  6329. #if defined(GGML_VULKAN_RUN_TESTS)
  6330. const std::vector<size_t> vals {
  6331. 512, 512, 128,
  6332. 128, 512, 512,
  6333. 4096, 512, 4096,
  6334. 11008, 512, 4096,
  6335. 4096, 512, 11008,
  6336. 32000, 512, 4096,
  6337. 8, 8, 8,
  6338. 100, 46, 576,
  6339. 623, 111, 128,
  6340. 100, 46, 558,
  6341. 512, 1, 256,
  6342. 128, 110, 622,
  6343. 511, 511, 127,
  6344. 511, 511, 7,
  6345. 511, 511, 17,
  6346. 49, 49, 128,
  6347. 128, 49, 49,
  6348. 4096, 49, 4096,
  6349. };
  6350. const size_t num_it = 100;
  6351. for (size_t i = 0; i < vals.size(); i += 3) {
  6352. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  6353. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  6354. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  6355. std::cerr << '\n';
  6356. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  6357. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  6358. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  6359. std::cerr << '\n';
  6360. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  6361. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  6362. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  6363. std::cerr << '\n' << std::endl;
  6364. if (vals[i + 2] % 32 == 0) {
  6365. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  6366. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  6367. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  6368. std::cerr << '\n';
  6369. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  6370. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  6371. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  6372. std::cerr << '\n';
  6373. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  6374. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  6375. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  6376. std::cerr << '\n' << std::endl;
  6377. }
  6378. if (vals[i + 2] % 256 == 0) {
  6379. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  6380. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  6381. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  6382. std::cerr << '\n';
  6383. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  6384. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  6385. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  6386. std::cerr << '\n';
  6387. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  6388. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  6389. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  6390. std::cerr << '\n' << std::endl;
  6391. }
  6392. }
  6393. GGML_ABORT("fatal error");
  6394. #endif
  6395. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  6396. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  6397. // Resize buffer
  6398. if (ctx->prealloc_x != nullptr) {
  6399. ggml_vk_destroy_buffer(ctx->prealloc_x);
  6400. }
  6401. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  6402. }
  6403. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  6404. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  6405. // Resize buffer
  6406. if (ctx->prealloc_y != nullptr) {
  6407. ggml_vk_destroy_buffer(ctx->prealloc_y);
  6408. }
  6409. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  6410. }
  6411. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  6412. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  6413. // Resize buffer
  6414. if (ctx->prealloc_split_k != nullptr) {
  6415. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6416. }
  6417. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  6418. }
  6419. }
  6420. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* tensor, int tensor_idx, bool use_fence);
  6421. // Returns true if node has enqueued work into the queue, false otherwise
  6422. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  6423. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool submit){
  6424. if (ggml_is_empty(node) || !node->buffer) {
  6425. return false;
  6426. }
  6427. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  6428. ctx->semaphore_idx = 0;
  6429. const ggml_tensor * src0 = node->src[0];
  6430. const ggml_tensor * src1 = node->src[1];
  6431. const ggml_tensor * src2 = node->src[2];
  6432. const ggml_tensor * src3 = node->src[3];
  6433. switch (node->op) {
  6434. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  6435. case GGML_OP_RESHAPE:
  6436. case GGML_OP_VIEW:
  6437. case GGML_OP_PERMUTE:
  6438. case GGML_OP_TRANSPOSE:
  6439. case GGML_OP_NONE:
  6440. return false;
  6441. case GGML_OP_UNARY:
  6442. switch (ggml_get_unary_op(node)) {
  6443. case GGML_UNARY_OP_SILU:
  6444. case GGML_UNARY_OP_GELU:
  6445. case GGML_UNARY_OP_GELU_QUICK:
  6446. case GGML_UNARY_OP_RELU:
  6447. case GGML_UNARY_OP_TANH:
  6448. case GGML_UNARY_OP_SIGMOID:
  6449. break;
  6450. default:
  6451. return false;
  6452. }
  6453. break;
  6454. case GGML_OP_REPEAT:
  6455. case GGML_OP_REPEAT_BACK:
  6456. case GGML_OP_GET_ROWS:
  6457. case GGML_OP_ADD:
  6458. case GGML_OP_ACC:
  6459. case GGML_OP_SUB:
  6460. case GGML_OP_MUL:
  6461. case GGML_OP_DIV:
  6462. case GGML_OP_CONCAT:
  6463. case GGML_OP_UPSCALE:
  6464. case GGML_OP_SCALE:
  6465. case GGML_OP_SQR:
  6466. case GGML_OP_SIN:
  6467. case GGML_OP_COS:
  6468. case GGML_OP_CLAMP:
  6469. case GGML_OP_PAD:
  6470. case GGML_OP_CPY:
  6471. case GGML_OP_CONT:
  6472. case GGML_OP_DUP:
  6473. case GGML_OP_SILU_BACK:
  6474. case GGML_OP_NORM:
  6475. case GGML_OP_GROUP_NORM:
  6476. case GGML_OP_RMS_NORM:
  6477. case GGML_OP_RMS_NORM_BACK:
  6478. case GGML_OP_L2_NORM:
  6479. case GGML_OP_DIAG_MASK_INF:
  6480. case GGML_OP_SOFT_MAX:
  6481. case GGML_OP_SOFT_MAX_BACK:
  6482. case GGML_OP_ROPE:
  6483. case GGML_OP_ROPE_BACK:
  6484. case GGML_OP_MUL_MAT:
  6485. case GGML_OP_MUL_MAT_ID:
  6486. case GGML_OP_ARGSORT:
  6487. case GGML_OP_SUM:
  6488. case GGML_OP_SUM_ROWS:
  6489. case GGML_OP_ARGMAX:
  6490. case GGML_OP_COUNT_EQUAL:
  6491. case GGML_OP_IM2COL:
  6492. case GGML_OP_TIMESTEP_EMBEDDING:
  6493. case GGML_OP_POOL_2D:
  6494. case GGML_OP_RWKV_WKV6:
  6495. case GGML_OP_RWKV_WKV7:
  6496. case GGML_OP_LEAKY_RELU:
  6497. case GGML_OP_FLASH_ATTN_EXT:
  6498. case GGML_OP_OPT_STEP_ADAMW:
  6499. break;
  6500. default:
  6501. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  6502. GGML_ABORT("fatal error");
  6503. return false;
  6504. }
  6505. vk_context compute_ctx;
  6506. if (!dryrun) {
  6507. if (ctx->compute_ctx.expired()) {
  6508. compute_ctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6509. ctx->compute_ctx = compute_ctx;
  6510. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  6511. } else {
  6512. compute_ctx = ctx->compute_ctx.lock();
  6513. }
  6514. } else {
  6515. switch (node->op) {
  6516. case GGML_OP_REPEAT:
  6517. case GGML_OP_REPEAT_BACK:
  6518. case GGML_OP_ACC:
  6519. case GGML_OP_GET_ROWS:
  6520. case GGML_OP_ADD:
  6521. case GGML_OP_SUB:
  6522. case GGML_OP_MUL:
  6523. case GGML_OP_DIV:
  6524. case GGML_OP_CONCAT:
  6525. case GGML_OP_UPSCALE:
  6526. case GGML_OP_SCALE:
  6527. case GGML_OP_SQR:
  6528. case GGML_OP_SIN:
  6529. case GGML_OP_COS:
  6530. case GGML_OP_CLAMP:
  6531. case GGML_OP_PAD:
  6532. case GGML_OP_CPY:
  6533. case GGML_OP_CONT:
  6534. case GGML_OP_DUP:
  6535. case GGML_OP_SILU_BACK:
  6536. case GGML_OP_NORM:
  6537. case GGML_OP_GROUP_NORM:
  6538. case GGML_OP_RMS_NORM:
  6539. case GGML_OP_RMS_NORM_BACK:
  6540. case GGML_OP_L2_NORM:
  6541. case GGML_OP_UNARY:
  6542. case GGML_OP_DIAG_MASK_INF:
  6543. case GGML_OP_SOFT_MAX:
  6544. case GGML_OP_SOFT_MAX_BACK:
  6545. case GGML_OP_ROPE:
  6546. case GGML_OP_ROPE_BACK:
  6547. case GGML_OP_ARGSORT:
  6548. case GGML_OP_SUM:
  6549. case GGML_OP_SUM_ROWS:
  6550. case GGML_OP_ARGMAX:
  6551. case GGML_OP_COUNT_EQUAL:
  6552. case GGML_OP_IM2COL:
  6553. case GGML_OP_TIMESTEP_EMBEDDING:
  6554. case GGML_OP_POOL_2D:
  6555. case GGML_OP_LEAKY_RELU:
  6556. {
  6557. // These operations all go through ggml_vk_op_f32, so short-circuit and
  6558. // do the only thing needed for the dryrun.
  6559. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  6560. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  6561. return false;
  6562. }
  6563. default:
  6564. break;
  6565. }
  6566. }
  6567. switch (node->op) {
  6568. case GGML_OP_REPEAT:
  6569. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  6570. break;
  6571. case GGML_OP_REPEAT_BACK:
  6572. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  6573. break;
  6574. case GGML_OP_ACC:
  6575. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  6576. break;
  6577. case GGML_OP_GET_ROWS:
  6578. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  6579. break;
  6580. case GGML_OP_ADD:
  6581. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  6582. break;
  6583. case GGML_OP_SUB:
  6584. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  6585. break;
  6586. case GGML_OP_MUL:
  6587. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  6588. break;
  6589. case GGML_OP_DIV:
  6590. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  6591. break;
  6592. case GGML_OP_CONCAT:
  6593. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  6594. break;
  6595. case GGML_OP_UPSCALE:
  6596. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  6597. break;
  6598. case GGML_OP_SCALE:
  6599. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  6600. break;
  6601. case GGML_OP_SQR:
  6602. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  6603. break;
  6604. case GGML_OP_SIN:
  6605. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  6606. break;
  6607. case GGML_OP_COS:
  6608. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  6609. break;
  6610. case GGML_OP_CLAMP:
  6611. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  6612. break;
  6613. case GGML_OP_PAD:
  6614. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  6615. break;
  6616. case GGML_OP_CPY:
  6617. case GGML_OP_CONT:
  6618. case GGML_OP_DUP:
  6619. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  6620. break;
  6621. case GGML_OP_SILU_BACK:
  6622. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6623. break;
  6624. case GGML_OP_NORM:
  6625. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  6626. break;
  6627. case GGML_OP_GROUP_NORM:
  6628. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  6629. break;
  6630. case GGML_OP_RMS_NORM:
  6631. ggml_vk_rms_norm(ctx, compute_ctx, src0, node, dryrun);
  6632. break;
  6633. case GGML_OP_RMS_NORM_BACK:
  6634. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6635. break;
  6636. case GGML_OP_L2_NORM:
  6637. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  6638. break;
  6639. case GGML_OP_UNARY:
  6640. switch (ggml_get_unary_op(node)) {
  6641. case GGML_UNARY_OP_SILU:
  6642. case GGML_UNARY_OP_GELU:
  6643. case GGML_UNARY_OP_GELU_QUICK:
  6644. case GGML_UNARY_OP_RELU:
  6645. case GGML_UNARY_OP_TANH:
  6646. case GGML_UNARY_OP_SIGMOID:
  6647. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  6648. break;
  6649. default:
  6650. return false;
  6651. }
  6652. break;
  6653. case GGML_OP_DIAG_MASK_INF:
  6654. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  6655. break;
  6656. case GGML_OP_SOFT_MAX:
  6657. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  6658. break;
  6659. case GGML_OP_SOFT_MAX_BACK:
  6660. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6661. break;
  6662. case GGML_OP_ROPE:
  6663. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  6664. break;
  6665. case GGML_OP_ROPE_BACK:
  6666. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  6667. break;
  6668. case GGML_OP_ARGSORT:
  6669. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  6670. break;
  6671. case GGML_OP_SUM:
  6672. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  6673. break;
  6674. case GGML_OP_SUM_ROWS:
  6675. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  6676. break;
  6677. case GGML_OP_ARGMAX:
  6678. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  6679. break;
  6680. case GGML_OP_COUNT_EQUAL:
  6681. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  6682. break;
  6683. case GGML_OP_IM2COL:
  6684. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  6685. break;
  6686. case GGML_OP_TIMESTEP_EMBEDDING:
  6687. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  6688. break;
  6689. case GGML_OP_POOL_2D:
  6690. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  6691. break;
  6692. case GGML_OP_LEAKY_RELU:
  6693. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  6694. break;
  6695. case GGML_OP_MUL_MAT:
  6696. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  6697. break;
  6698. case GGML_OP_MUL_MAT_ID:
  6699. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  6700. break;
  6701. case GGML_OP_FLASH_ATTN_EXT:
  6702. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  6703. break;
  6704. case GGML_OP_RWKV_WKV6:
  6705. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  6706. break;
  6707. case GGML_OP_RWKV_WKV7:
  6708. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  6709. break;
  6710. case GGML_OP_OPT_STEP_ADAMW:
  6711. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  6712. break;
  6713. default:
  6714. return false;
  6715. }
  6716. if (dryrun) {
  6717. return false;
  6718. }
  6719. ctx->tensor_ctxs[node_idx] = compute_ctx;
  6720. #if defined(GGML_VULKAN_CHECK_RESULTS) || defined(GGML_VULKAN_PERF)
  6721. // Force context reset on each node so that each tensor ends up in its own context
  6722. // and can be run and compared to its CPU equivalent separately
  6723. last_node = true;
  6724. #endif
  6725. if (submit || last_node) {
  6726. ggml_vk_ctx_end(compute_ctx);
  6727. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  6728. if (last_node) {
  6729. compute_ctx->exit_tensor_idx = node_idx_begin;
  6730. }
  6731. else {
  6732. compute_ctx->exit_tensor_idx = -1;
  6733. }
  6734. ctx->compute_ctx.reset();
  6735. bool ok = ggml_vk_compute_forward(ctx, node_begin, node_idx_begin, false);
  6736. if (!ok) {
  6737. if (node->op == GGML_OP_UNARY) {
  6738. std::cerr << __func__ << ": error: op not supported UNARY " << node->name << " (" << ggml_unary_op_name(static_cast<ggml_unary_op>(node->op_params[0])) << ")" << std::endl;
  6739. }
  6740. else {
  6741. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  6742. }
  6743. }
  6744. }
  6745. return true;
  6746. }
  6747. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_tensor * tensor, int tensor_idx, bool use_fence = true){
  6748. ggml_backend_buffer * buf = nullptr;
  6749. switch (tensor->op) {
  6750. case GGML_OP_ADD:
  6751. case GGML_OP_ACC:
  6752. case GGML_OP_GET_ROWS:
  6753. case GGML_OP_SUB:
  6754. case GGML_OP_MUL:
  6755. case GGML_OP_DIV:
  6756. case GGML_OP_CONCAT:
  6757. case GGML_OP_UPSCALE:
  6758. case GGML_OP_SCALE:
  6759. case GGML_OP_SQR:
  6760. case GGML_OP_SIN:
  6761. case GGML_OP_COS:
  6762. case GGML_OP_CLAMP:
  6763. case GGML_OP_PAD:
  6764. case GGML_OP_CPY:
  6765. case GGML_OP_CONT:
  6766. case GGML_OP_DUP:
  6767. case GGML_OP_SILU_BACK:
  6768. case GGML_OP_NORM:
  6769. case GGML_OP_GROUP_NORM:
  6770. case GGML_OP_RMS_NORM:
  6771. case GGML_OP_RMS_NORM_BACK:
  6772. case GGML_OP_L2_NORM:
  6773. case GGML_OP_DIAG_MASK_INF:
  6774. case GGML_OP_SOFT_MAX:
  6775. case GGML_OP_SOFT_MAX_BACK:
  6776. case GGML_OP_ROPE:
  6777. case GGML_OP_ROPE_BACK:
  6778. case GGML_OP_RESHAPE:
  6779. case GGML_OP_VIEW:
  6780. case GGML_OP_PERMUTE:
  6781. case GGML_OP_TRANSPOSE:
  6782. case GGML_OP_NONE:
  6783. case GGML_OP_ARGSORT:
  6784. case GGML_OP_SUM:
  6785. case GGML_OP_SUM_ROWS:
  6786. case GGML_OP_ARGMAX:
  6787. case GGML_OP_COUNT_EQUAL:
  6788. case GGML_OP_IM2COL:
  6789. case GGML_OP_TIMESTEP_EMBEDDING:
  6790. case GGML_OP_POOL_2D:
  6791. case GGML_OP_RWKV_WKV6:
  6792. case GGML_OP_RWKV_WKV7:
  6793. case GGML_OP_LEAKY_RELU:
  6794. case GGML_OP_REPEAT:
  6795. case GGML_OP_REPEAT_BACK:
  6796. case GGML_OP_OPT_STEP_ADAMW:
  6797. buf = tensor->buffer;
  6798. break;
  6799. case GGML_OP_UNARY:
  6800. switch (ggml_get_unary_op(tensor)) {
  6801. case GGML_UNARY_OP_SILU:
  6802. case GGML_UNARY_OP_GELU:
  6803. case GGML_UNARY_OP_GELU_QUICK:
  6804. case GGML_UNARY_OP_RELU:
  6805. case GGML_UNARY_OP_TANH:
  6806. case GGML_UNARY_OP_SIGMOID:
  6807. buf = tensor->buffer;
  6808. break;
  6809. default:
  6810. return false;
  6811. }
  6812. break;
  6813. case GGML_OP_MUL_MAT:
  6814. case GGML_OP_MUL_MAT_ID:
  6815. case GGML_OP_FLASH_ATTN_EXT:
  6816. buf = tensor->buffer;
  6817. break;
  6818. default:
  6819. return false;
  6820. }
  6821. if (buf == nullptr) {
  6822. return false;
  6823. }
  6824. VK_LOG_DEBUG("ggml_vk_compute_forward(" << tensor << ", name=" << tensor->name << ", op=" << ggml_op_name(tensor->op) << ", type=" << tensor->type << ", ne0=" << tensor->ne[0] << ", ne1=" << tensor->ne[1] << ", ne2=" << tensor->ne[2] << ", ne3=" << tensor->ne[3] << ", nb0=" << tensor->nb[0] << ", nb1=" << tensor->nb[1] << ", nb2=" << tensor->nb[2] << ", nb3=" << tensor->nb[3] << ", view_src=" << tensor->view_src << ", view_offs=" << tensor->view_offs << ")");
  6825. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  6826. // always wait for the GPU work to be done for the last submit
  6827. if (tensor_idx == subctx->exit_tensor_idx) {
  6828. use_fence = true;
  6829. }
  6830. // Only run if ctx hasn't been submitted yet
  6831. if (!subctx->seqs.empty()) {
  6832. #ifdef GGML_VULKAN_CHECK_RESULTS
  6833. ggml_vk_check_results_0(tensor);
  6834. use_fence = true;
  6835. #endif
  6836. // Do staging buffer copies
  6837. for (auto& cpy : subctx->in_memcpys) {
  6838. memcpy(cpy.dst, cpy.src, cpy.n);
  6839. }
  6840. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  6841. if (use_fence) {
  6842. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_compute_forward waitForFences");
  6843. ctx->device->device.resetFences({ ctx->fence });
  6844. }
  6845. #ifdef GGML_VULKAN_CHECK_RESULTS
  6846. ggml_vk_check_results_1(tensor);
  6847. #endif
  6848. }
  6849. if (tensor_idx == subctx->exit_tensor_idx) {
  6850. // Do staging buffer copies
  6851. for (auto& cpy : subctx->out_memcpys) {
  6852. memcpy(cpy.dst, cpy.src, cpy.n);
  6853. }
  6854. subctx->in_memcpys.clear();
  6855. subctx->out_memcpys.clear();
  6856. }
  6857. return true;
  6858. }
  6859. // Clean up after graph processing is done
  6860. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  6861. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  6862. for (auto& buffer : ctx->gc.temp_buffers) {
  6863. ggml_vk_pool_free(ctx, buffer);
  6864. }
  6865. ctx->gc.temp_buffers.clear();
  6866. for (auto& dsr : ctx->device->pipeline_descriptor_set_requirements) {
  6867. vk_pipeline_ref plr = ctx->device->pipelines[dsr.first];
  6868. if (plr.expired()) {
  6869. continue;
  6870. }
  6871. vk_pipeline pl = plr.lock();
  6872. ggml_pipeline_cleanup(pl);
  6873. }
  6874. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  6875. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  6876. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  6877. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  6878. }
  6879. ctx->gc.semaphores.clear();
  6880. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  6881. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  6882. }
  6883. ctx->gc.tl_semaphores.clear();
  6884. ctx->semaphore_idx = 0;
  6885. ctx->event_idx = 0;
  6886. for (auto& event : ctx->gc.events) {
  6887. ctx->device->device.resetEvent(event);
  6888. }
  6889. ctx->tensor_ctxs.clear();
  6890. ctx->gc.contexts.clear();
  6891. ctx->device->pipeline_descriptor_set_requirements.clear();
  6892. }
  6893. // Clean up on backend free
  6894. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  6895. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  6896. ggml_vk_graph_cleanup(ctx);
  6897. ggml_vk_destroy_buffer(ctx->prealloc_x);
  6898. ggml_vk_destroy_buffer(ctx->prealloc_y);
  6899. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6900. for (auto& buffer : ctx->buffer_pool) {
  6901. ggml_vk_destroy_buffer(buffer);
  6902. }
  6903. ctx->prealloc_size_x = 0;
  6904. ctx->prealloc_size_y = 0;
  6905. ctx->prealloc_size_split_k = 0;
  6906. for (auto& event : ctx->gc.events) {
  6907. ctx->device->device.destroyEvent(event);
  6908. }
  6909. ctx->gc.events.clear();
  6910. ctx->device->device.destroyFence(ctx->fence);
  6911. }
  6912. static int ggml_vk_get_device_count() {
  6913. ggml_vk_instance_init();
  6914. return vk_instance.device_indices.size();
  6915. }
  6916. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  6917. ggml_vk_instance_init();
  6918. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  6919. vk::PhysicalDeviceProperties props;
  6920. devices[device].getProperties(&props);
  6921. snprintf(description, description_size, "%s", props.deviceName.data());
  6922. }
  6923. // backend interface
  6924. #define UNUSED GGML_UNUSED
  6925. // device backend
  6926. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  6927. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  6928. }
  6929. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  6930. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  6931. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6932. ggml_vk_destroy_buffer(ctx->dev_buffer);
  6933. delete ctx;
  6934. }
  6935. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  6936. return vk_ptr_base;
  6937. UNUSED(buffer);
  6938. }
  6939. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  6940. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  6941. if (tensor->view_src != nullptr) {
  6942. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  6943. }
  6944. return GGML_STATUS_SUCCESS;
  6945. }
  6946. static void ggml_backend_vk_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
  6947. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  6948. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6949. vk_buffer buf = buf_ctx->dev_buffer;
  6950. uint32_t val32 = (uint32_t)value * 0x01010101;
  6951. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  6952. }
  6953. 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) {
  6954. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  6955. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6956. vk_buffer buf = buf_ctx->dev_buffer;
  6957. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  6958. }
  6959. 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) {
  6960. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  6961. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6962. vk_buffer buf = buf_ctx->dev_buffer;
  6963. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  6964. }
  6965. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  6966. if (ggml_backend_buffer_is_vk(src->buffer)) {
  6967. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  6968. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6969. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  6970. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  6971. ggml_vk_buffer_copy(dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  6972. return true;
  6973. }
  6974. return false;
  6975. UNUSED(buffer);
  6976. }
  6977. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  6978. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6979. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  6980. }
  6981. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  6982. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  6983. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  6984. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  6985. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  6986. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  6987. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  6988. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  6989. /* .clear = */ ggml_backend_vk_buffer_clear,
  6990. /* .reset = */ NULL,
  6991. };
  6992. // vk buffer type
  6993. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  6994. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  6995. return ctx->name.c_str();
  6996. }
  6997. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  6998. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  6999. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7000. vk_buffer dev_buffer = nullptr;
  7001. try {
  7002. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  7003. } catch (const vk::SystemError& e) {
  7004. return nullptr;
  7005. }
  7006. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  7007. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  7008. }
  7009. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7010. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7011. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  7012. }
  7013. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  7014. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7015. return ctx->device->suballocation_block_size;
  7016. }
  7017. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  7018. return ggml_nbytes(tensor);
  7019. UNUSED(buft);
  7020. }
  7021. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  7022. ggml_vk_instance_init();
  7023. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  7024. vk_device dev = ggml_vk_get_device(dev_num);
  7025. return &dev->buffer_type;
  7026. }
  7027. // host buffer type
  7028. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  7029. return GGML_VK_NAME "_Host";
  7030. UNUSED(buft);
  7031. }
  7032. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  7033. return GGML_VK_NAME "_Host";
  7034. UNUSED(buffer);
  7035. }
  7036. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7037. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  7038. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  7039. }
  7040. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7041. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  7042. size += 32; // Behave like the CPU buffer type
  7043. void * ptr = nullptr;
  7044. try {
  7045. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  7046. } catch (vk::SystemError& e) {
  7047. std::cerr << "ggml_vulkan: Failed to allocate pinned memory." << std::endl;
  7048. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  7049. // fallback to cpu buffer
  7050. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7051. }
  7052. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7053. buffer->buft = buft;
  7054. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  7055. return buffer;
  7056. UNUSED(buft);
  7057. }
  7058. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7059. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  7060. UNUSED(buft);
  7061. }
  7062. // Should be changed to return device-specific host buffer type
  7063. // but that probably requires changes in llama.cpp
  7064. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  7065. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  7066. /* .iface = */ {
  7067. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  7068. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  7069. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  7070. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  7071. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7072. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7073. },
  7074. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  7075. /* .context = */ nullptr,
  7076. };
  7077. // Make sure device 0 is initialized
  7078. ggml_vk_instance_init();
  7079. ggml_vk_get_device(0);
  7080. return &ggml_backend_vk_buffer_type_host;
  7081. }
  7082. // backend
  7083. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  7084. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7085. return ctx->name.c_str();
  7086. }
  7087. static void ggml_backend_vk_free(ggml_backend_t backend) {
  7088. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7089. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  7090. ggml_vk_cleanup(ctx);
  7091. delete ctx;
  7092. delete backend;
  7093. }
  7094. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  7095. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7096. return &ctx->device->buffer_type;
  7097. }
  7098. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7099. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  7100. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7101. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  7102. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7103. vk_context transfer_ctx;
  7104. if (ctx->transfer_ctx.expired()) {
  7105. // Initialize new transfer context
  7106. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7107. ctx->transfer_ctx = transfer_ctx;
  7108. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7109. } else {
  7110. transfer_ctx = ctx->transfer_ctx.lock();
  7111. }
  7112. vk_buffer buf = buf_ctx->dev_buffer;
  7113. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7114. }
  7115. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7116. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  7117. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7118. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  7119. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7120. vk_context transfer_ctx;
  7121. if (ctx->transfer_ctx.expired()) {
  7122. // Initialize new transfer context
  7123. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7124. ctx->transfer_ctx = transfer_ctx;
  7125. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7126. } else {
  7127. transfer_ctx = ctx->transfer_ctx.lock();
  7128. }
  7129. vk_buffer buf = buf_ctx->dev_buffer;
  7130. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7131. }
  7132. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  7133. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  7134. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7135. if ((dst->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || dst->buffer->buft == ggml_backend_vk_host_buffer_type()) && ggml_backend_buffer_is_vk(src->buffer)) {
  7136. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  7137. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7138. vk_context transfer_ctx;
  7139. if (ctx->transfer_ctx.expired()) {
  7140. // Initialize new transfer context
  7141. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7142. ctx->transfer_ctx = transfer_ctx;
  7143. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7144. } else {
  7145. transfer_ctx = ctx->transfer_ctx.lock();
  7146. }
  7147. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  7148. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  7149. ggml_vk_buffer_copy_async(transfer_ctx, dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  7150. return true;
  7151. }
  7152. return false;
  7153. }
  7154. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  7155. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  7156. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7157. if(ctx->transfer_ctx.expired()) {
  7158. return;
  7159. }
  7160. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  7161. ggml_vk_ctx_end(transfer_ctx);
  7162. for (auto& cpy : transfer_ctx->in_memcpys) {
  7163. memcpy(cpy.dst, cpy.src, cpy.n);
  7164. }
  7165. ggml_vk_submit(transfer_ctx, ctx->fence);
  7166. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_backend_vk_synchronize waitForFences");
  7167. ctx->device->device.resetFences({ ctx->fence });
  7168. for (auto& cpy : transfer_ctx->out_memcpys) {
  7169. memcpy(cpy.dst, cpy.src, cpy.n);
  7170. }
  7171. ctx->transfer_ctx.reset();
  7172. }
  7173. static bool ggml_vk_is_empty(ggml_tensor * node) {
  7174. 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;
  7175. }
  7176. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7177. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  7178. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7179. uint64_t total_mat_mul_bytes = 0;
  7180. for (int i = 0; i < cgraph->n_nodes; i++) {
  7181. ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false);
  7182. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  7183. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  7184. }
  7185. }
  7186. if (ctx->device->need_compiles) {
  7187. ggml_vk_load_shaders(ctx->device);
  7188. }
  7189. ggml_vk_preallocate_buffers(ctx);
  7190. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  7191. int last_node = cgraph->n_nodes - 1;
  7192. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  7193. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  7194. last_node -= 1;
  7195. }
  7196. // Reserve tensor context space for all nodes
  7197. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  7198. bool first_node_in_batch = true; // true if next node will be first node in a batch
  7199. int submit_node_idx = 0; // index to first node in a batch
  7200. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  7201. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  7202. // (and scaled down based on model size, so smaller models submit earlier).
  7203. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  7204. int nodes_per_submit = 100;
  7205. int submitted_nodes = 0;
  7206. int submit_count = 0;
  7207. uint64_t mul_mat_bytes = 0;
  7208. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  7209. for (int i = 0; i < cgraph->n_nodes; i++) {
  7210. if (first_node_in_batch) {
  7211. submit_node_idx = i;
  7212. }
  7213. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  7214. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  7215. }
  7216. bool submit = (submitted_nodes >= nodes_per_submit) ||
  7217. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  7218. (i == last_node);
  7219. bool enqueued = ggml_vk_build_graph(ctx, cgraph->nodes[i], i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i == last_node, submit);
  7220. if (enqueued) {
  7221. ++submitted_nodes;
  7222. #ifndef GGML_VULKAN_CHECK_RESULTS
  7223. if (first_node_in_batch) {
  7224. first_node_in_batch = false;
  7225. }
  7226. #endif
  7227. }
  7228. if (submit) {
  7229. first_node_in_batch = true;
  7230. submitted_nodes = 0;
  7231. mul_mat_bytes = 0;
  7232. if (submit_count < 3) {
  7233. mul_mat_bytes_per_submit *= 2;
  7234. }
  7235. submit_count++;
  7236. }
  7237. }
  7238. #ifdef GGML_VULKAN_PERF
  7239. ctx->device->perf_logger->print_timings();
  7240. #endif
  7241. ggml_vk_graph_cleanup(ctx);
  7242. return GGML_STATUS_SUCCESS;
  7243. UNUSED(backend);
  7244. }
  7245. // TODO: enable async and synchronize
  7246. static ggml_backend_i ggml_backend_vk_interface = {
  7247. /* .get_name = */ ggml_backend_vk_name,
  7248. /* .free = */ ggml_backend_vk_free,
  7249. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  7250. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  7251. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  7252. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  7253. /* .graph_plan_create = */ NULL,
  7254. /* .graph_plan_free = */ NULL,
  7255. /* .graph_plan_update = */ NULL,
  7256. /* .graph_plan_compute = */ NULL,
  7257. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  7258. /* .event_record = */ NULL,
  7259. /* .event_wait = */ NULL,
  7260. };
  7261. static ggml_guid_t ggml_backend_vk_guid() {
  7262. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  7263. return &guid;
  7264. }
  7265. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  7266. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  7267. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  7268. ggml_vk_init(ctx, dev_num);
  7269. ggml_backend_t vk_backend = new ggml_backend {
  7270. /* .guid = */ ggml_backend_vk_guid(),
  7271. /* .interface = */ ggml_backend_vk_interface,
  7272. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  7273. /* .context = */ ctx,
  7274. };
  7275. return vk_backend;
  7276. }
  7277. bool ggml_backend_is_vk(ggml_backend_t backend) {
  7278. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  7279. }
  7280. int ggml_backend_vk_get_device_count() {
  7281. return ggml_vk_get_device_count();
  7282. }
  7283. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  7284. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7285. int dev_idx = vk_instance.device_indices[device];
  7286. ggml_vk_get_device_description(dev_idx, description, description_size);
  7287. }
  7288. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  7289. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7290. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  7291. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  7292. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  7293. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  7294. *total = heap.size;
  7295. *free = heap.size;
  7296. break;
  7297. }
  7298. }
  7299. }
  7300. //////////////////////////
  7301. struct ggml_backend_vk_device_context {
  7302. size_t device;
  7303. std::string name;
  7304. std::string description;
  7305. };
  7306. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  7307. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7308. return ctx->name.c_str();
  7309. }
  7310. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  7311. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7312. return ctx->description.c_str();
  7313. }
  7314. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  7315. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  7316. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  7317. }
  7318. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  7319. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7320. return ggml_backend_vk_buffer_type(ctx->device);
  7321. }
  7322. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  7323. UNUSED(dev);
  7324. return ggml_backend_vk_host_buffer_type();
  7325. }
  7326. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  7327. UNUSED(dev);
  7328. return GGML_BACKEND_DEVICE_TYPE_GPU;
  7329. }
  7330. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  7331. props->name = ggml_backend_vk_device_get_name(dev);
  7332. props->description = ggml_backend_vk_device_get_description(dev);
  7333. props->type = ggml_backend_vk_device_get_type(dev);
  7334. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  7335. props->caps = {
  7336. /* .async = */ false,
  7337. /* .host_buffer = */ true,
  7338. /* .buffer_from_host_ptr = */ false,
  7339. /* .events = */ false,
  7340. };
  7341. }
  7342. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  7343. UNUSED(params);
  7344. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7345. return ggml_backend_vk_init(ctx->device);
  7346. }
  7347. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7348. switch (op->op) {
  7349. case GGML_OP_UNARY:
  7350. switch (ggml_get_unary_op(op)) {
  7351. case GGML_UNARY_OP_GELU:
  7352. case GGML_UNARY_OP_GELU_QUICK:
  7353. case GGML_UNARY_OP_SILU:
  7354. case GGML_UNARY_OP_RELU:
  7355. case GGML_UNARY_OP_TANH:
  7356. case GGML_UNARY_OP_SIGMOID:
  7357. return ggml_is_contiguous(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
  7358. default:
  7359. return false;
  7360. }
  7361. break;
  7362. case GGML_OP_MUL_MAT:
  7363. case GGML_OP_MUL_MAT_ID:
  7364. {
  7365. ggml_type src0_type = op->src[0]->type;
  7366. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7367. const vk_device& device = ggml_vk_get_device(ctx->device);
  7368. if (op->op == GGML_OP_MUL_MAT_ID && !device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  7369. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  7370. return false;
  7371. }
  7372. switch (src0_type) {
  7373. case GGML_TYPE_F32:
  7374. case GGML_TYPE_F16:
  7375. case GGML_TYPE_Q4_0:
  7376. case GGML_TYPE_Q4_1:
  7377. case GGML_TYPE_Q5_0:
  7378. case GGML_TYPE_Q5_1:
  7379. case GGML_TYPE_Q8_0:
  7380. case GGML_TYPE_Q2_K:
  7381. case GGML_TYPE_Q3_K:
  7382. case GGML_TYPE_Q4_K:
  7383. case GGML_TYPE_Q5_K:
  7384. case GGML_TYPE_Q6_K:
  7385. case GGML_TYPE_IQ1_S:
  7386. case GGML_TYPE_IQ1_M:
  7387. case GGML_TYPE_IQ2_XXS:
  7388. case GGML_TYPE_IQ2_XS:
  7389. case GGML_TYPE_IQ2_S:
  7390. case GGML_TYPE_IQ3_XXS:
  7391. case GGML_TYPE_IQ3_S:
  7392. case GGML_TYPE_IQ4_XS:
  7393. case GGML_TYPE_IQ4_NL:
  7394. break;
  7395. default:
  7396. return false;
  7397. }
  7398. struct ggml_tensor * a;
  7399. struct ggml_tensor * b;
  7400. if (op->op == GGML_OP_MUL_MAT) {
  7401. a = op->src[0];
  7402. b = op->src[1];
  7403. } else {
  7404. a = op->src[2];
  7405. b = op->src[1];
  7406. }
  7407. if (a->ne[3] != b->ne[3]) {
  7408. return false;
  7409. }
  7410. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) ||
  7411. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  7412. return false;
  7413. }
  7414. return true;
  7415. } break;
  7416. case GGML_OP_FLASH_ATTN_EXT:
  7417. {
  7418. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7419. if (!ggml_vk_get_device(ctx->device)->coopmat2) {
  7420. return false;
  7421. }
  7422. switch (op->src[0]->ne[0]) {
  7423. case 64:
  7424. case 80:
  7425. case 96:
  7426. case 112:
  7427. case 128:
  7428. case 256:
  7429. break;
  7430. default:
  7431. return false;
  7432. }
  7433. if (op->src[0]->type != GGML_TYPE_F32) {
  7434. return false;
  7435. }
  7436. if (op->type != GGML_TYPE_F32) {
  7437. return false;
  7438. }
  7439. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  7440. return false;
  7441. }
  7442. // It's straightforward to support different K/V dequant, but would
  7443. // significantly increase the number of pipelines
  7444. if (op->src[1]->type != op->src[2]->type) {
  7445. return false;
  7446. }
  7447. switch (op->src[1]->type) {
  7448. case GGML_TYPE_F16:
  7449. case GGML_TYPE_Q4_0:
  7450. case GGML_TYPE_Q4_1:
  7451. case GGML_TYPE_Q5_0:
  7452. case GGML_TYPE_Q5_1:
  7453. case GGML_TYPE_Q8_0:
  7454. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  7455. //case GGML_TYPE_Q2_K:
  7456. //case GGML_TYPE_Q3_K:
  7457. //case GGML_TYPE_Q4_K:
  7458. //case GGML_TYPE_Q5_K:
  7459. //case GGML_TYPE_Q6_K:
  7460. //case GGML_TYPE_IQ1_S:
  7461. //case GGML_TYPE_IQ1_M:
  7462. //case GGML_TYPE_IQ2_XXS:
  7463. //case GGML_TYPE_IQ2_XS:
  7464. //case GGML_TYPE_IQ2_S:
  7465. //case GGML_TYPE_IQ3_XXS:
  7466. //case GGML_TYPE_IQ3_S:
  7467. //case GGML_TYPE_IQ4_XS:
  7468. case GGML_TYPE_IQ4_NL:
  7469. break;
  7470. default:
  7471. return false;
  7472. }
  7473. return true;
  7474. }
  7475. case GGML_OP_GET_ROWS:
  7476. {
  7477. switch (op->src[0]->type) {
  7478. case GGML_TYPE_F32:
  7479. case GGML_TYPE_F16:
  7480. case GGML_TYPE_Q4_0:
  7481. case GGML_TYPE_Q4_1:
  7482. case GGML_TYPE_Q5_0:
  7483. case GGML_TYPE_Q5_1:
  7484. case GGML_TYPE_Q8_0:
  7485. case GGML_TYPE_IQ1_S:
  7486. case GGML_TYPE_IQ1_M:
  7487. case GGML_TYPE_IQ2_XXS:
  7488. case GGML_TYPE_IQ2_XS:
  7489. case GGML_TYPE_IQ2_S:
  7490. case GGML_TYPE_IQ3_XXS:
  7491. case GGML_TYPE_IQ3_S:
  7492. case GGML_TYPE_IQ4_XS:
  7493. case GGML_TYPE_IQ4_NL:
  7494. return true;
  7495. default:
  7496. return false;
  7497. }
  7498. } break;
  7499. case GGML_OP_CONT:
  7500. case GGML_OP_CPY:
  7501. case GGML_OP_DUP:
  7502. {
  7503. ggml_type src0_type = op->src[0]->type;
  7504. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  7505. if (src0_type == GGML_TYPE_F32) {
  7506. switch (src1_type) {
  7507. case GGML_TYPE_F32:
  7508. case GGML_TYPE_F16:
  7509. case GGML_TYPE_Q4_0:
  7510. case GGML_TYPE_Q4_1:
  7511. case GGML_TYPE_Q5_0:
  7512. case GGML_TYPE_Q5_1:
  7513. case GGML_TYPE_Q8_0:
  7514. case GGML_TYPE_IQ4_NL:
  7515. return true;
  7516. default:
  7517. break;
  7518. }
  7519. }
  7520. if (src1_type == GGML_TYPE_F32) {
  7521. switch (src0_type) {
  7522. case GGML_TYPE_Q4_0:
  7523. case GGML_TYPE_Q4_1:
  7524. case GGML_TYPE_Q5_0:
  7525. case GGML_TYPE_Q5_1:
  7526. case GGML_TYPE_Q8_0:
  7527. case GGML_TYPE_IQ4_NL:
  7528. return true;
  7529. default:
  7530. break;
  7531. }
  7532. }
  7533. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  7534. return true;
  7535. }
  7536. return false;
  7537. } break;
  7538. case GGML_OP_REPEAT:
  7539. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  7540. case GGML_OP_REPEAT_BACK:
  7541. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  7542. case GGML_OP_ROPE:
  7543. case GGML_OP_ROPE_BACK:
  7544. case GGML_OP_NONE:
  7545. case GGML_OP_RESHAPE:
  7546. case GGML_OP_VIEW:
  7547. case GGML_OP_PERMUTE:
  7548. case GGML_OP_TRANSPOSE:
  7549. return true;
  7550. case GGML_OP_NORM:
  7551. case GGML_OP_GROUP_NORM:
  7552. case GGML_OP_RMS_NORM:
  7553. case GGML_OP_L2_NORM:
  7554. return ggml_is_contiguous(op->src[0]);
  7555. case GGML_OP_ADD:
  7556. case GGML_OP_SUB:
  7557. case GGML_OP_MUL:
  7558. case GGML_OP_DIV:
  7559. case GGML_OP_SILU_BACK:
  7560. case GGML_OP_RMS_NORM_BACK:
  7561. case GGML_OP_SQR:
  7562. case GGML_OP_SIN:
  7563. case GGML_OP_COS:
  7564. case GGML_OP_CLAMP:
  7565. return op->src[0]->type == GGML_TYPE_F32;
  7566. case GGML_OP_ACC:
  7567. case GGML_OP_CONCAT:
  7568. case GGML_OP_UPSCALE:
  7569. case GGML_OP_SCALE:
  7570. case GGML_OP_PAD:
  7571. case GGML_OP_DIAG_MASK_INF:
  7572. case GGML_OP_SOFT_MAX:
  7573. case GGML_OP_SOFT_MAX_BACK:
  7574. case GGML_OP_ARGSORT:
  7575. case GGML_OP_SUM:
  7576. case GGML_OP_SUM_ROWS:
  7577. case GGML_OP_ARGMAX:
  7578. case GGML_OP_COUNT_EQUAL:
  7579. case GGML_OP_IM2COL:
  7580. case GGML_OP_TIMESTEP_EMBEDDING:
  7581. case GGML_OP_POOL_2D:
  7582. case GGML_OP_RWKV_WKV6:
  7583. case GGML_OP_RWKV_WKV7:
  7584. case GGML_OP_LEAKY_RELU:
  7585. case GGML_OP_OPT_STEP_ADAMW:
  7586. return true;
  7587. default:
  7588. return false;
  7589. }
  7590. UNUSED(dev);
  7591. }
  7592. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  7593. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  7594. return false;
  7595. }
  7596. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7597. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  7598. return buft_ctx->device->idx == ctx->device;
  7599. }
  7600. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7601. const int min_batch_size = 32;
  7602. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  7603. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  7604. UNUSED(dev);
  7605. }
  7606. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  7607. /* .get_name = */ ggml_backend_vk_device_get_name,
  7608. /* .get_description = */ ggml_backend_vk_device_get_description,
  7609. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  7610. /* .get_type = */ ggml_backend_vk_device_get_type,
  7611. /* .get_props = */ ggml_backend_vk_device_get_props,
  7612. /* .init_backend = */ ggml_backend_vk_device_init,
  7613. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  7614. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  7615. /* .buffer_from_host_ptr = */ NULL,
  7616. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  7617. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  7618. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  7619. /* .event_new = */ NULL,
  7620. /* .event_free = */ NULL,
  7621. /* .event_synchronize = */ NULL,
  7622. };
  7623. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  7624. UNUSED(reg);
  7625. return GGML_VK_NAME;
  7626. }
  7627. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  7628. UNUSED(reg);
  7629. return ggml_backend_vk_get_device_count();
  7630. }
  7631. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  7632. static std::vector<ggml_backend_dev_t> devices;
  7633. static bool initialized = false;
  7634. {
  7635. static std::mutex mutex;
  7636. std::lock_guard<std::mutex> lock(mutex);
  7637. if (!initialized) {
  7638. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  7639. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  7640. char desc[256];
  7641. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  7642. ctx->device = i;
  7643. ctx->name = GGML_VK_NAME + std::to_string(i);
  7644. ctx->description = desc;
  7645. devices.push_back(new ggml_backend_device {
  7646. /* .iface = */ ggml_backend_vk_device_i,
  7647. /* .reg = */ reg,
  7648. /* .context = */ ctx,
  7649. });
  7650. }
  7651. initialized = true;
  7652. }
  7653. }
  7654. GGML_ASSERT(device < devices.size());
  7655. return devices[device];
  7656. }
  7657. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  7658. /* .get_name = */ ggml_backend_vk_reg_get_name,
  7659. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  7660. /* .get_device = */ ggml_backend_vk_reg_get_device,
  7661. /* .get_proc_address = */ NULL,
  7662. };
  7663. ggml_backend_reg_t ggml_backend_vk_reg() {
  7664. static ggml_backend_reg reg = {
  7665. /* .api_version = */ GGML_BACKEND_API_VERSION,
  7666. /* .iface = */ ggml_backend_vk_reg_i,
  7667. /* .context = */ nullptr,
  7668. };
  7669. try {
  7670. ggml_vk_instance_init();
  7671. return &reg;
  7672. } catch (const vk::SystemError& e) {
  7673. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  7674. return nullptr;
  7675. }
  7676. }
  7677. // Extension availability
  7678. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  7679. #ifdef GGML_VULKAN_VALIDATE
  7680. bool portability_enumeration_ext = false;
  7681. // Check for portability enumeration extension for MoltenVK support
  7682. for (const auto& properties : instance_extensions) {
  7683. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  7684. return true;
  7685. }
  7686. }
  7687. if (!portability_enumeration_ext) {
  7688. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  7689. }
  7690. #endif
  7691. return false;
  7692. UNUSED(instance_extensions);
  7693. }
  7694. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  7695. #ifdef __APPLE__
  7696. bool portability_enumeration_ext = false;
  7697. // Check for portability enumeration extension for MoltenVK support
  7698. for (const auto& properties : instance_extensions) {
  7699. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  7700. return true;
  7701. }
  7702. }
  7703. if (!portability_enumeration_ext) {
  7704. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  7705. }
  7706. #endif
  7707. return false;
  7708. UNUSED(instance_extensions);
  7709. }
  7710. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  7711. switch (props.vendorID) {
  7712. case VK_VENDOR_ID_INTEL:
  7713. // Intel drivers don't support coopmat properly yet
  7714. return false;
  7715. case VK_VENDOR_ID_AMD:
  7716. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  7717. // Workaround for AMD proprietary driver reporting support on all GPUs
  7718. return arch == vk_device_architecture::AMD_RDNA3;
  7719. }
  7720. return true;
  7721. default:
  7722. return true;
  7723. }
  7724. }
  7725. // checks
  7726. #ifdef GGML_VULKAN_CHECK_RESULTS
  7727. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  7728. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  7729. return;
  7730. }
  7731. for (int j = 0; j < level; j++) {
  7732. std::cerr << " ";
  7733. }
  7734. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  7735. done.push_back(tensor);
  7736. for (int i = 0; i < GGML_MAX_SRC; i++) {
  7737. if (tensor->src[i] != nullptr) {
  7738. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  7739. }
  7740. }
  7741. }
  7742. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  7743. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  7744. return;
  7745. }
  7746. i0 = std::max(i0, 5);
  7747. i1 = std::max(i1, 5);
  7748. i2 = std::max(i2, 0);
  7749. i3 = std::max(i3, 0);
  7750. fprintf(stderr, " ");
  7751. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7752. fprintf(stderr, "%7d ", idx1);
  7753. }
  7754. fprintf(stderr, "\n");
  7755. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7756. fprintf(stderr, "%7d: ", idx0);
  7757. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7758. 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]) {
  7759. float val;
  7760. if (tensor->type == GGML_TYPE_F32) {
  7761. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7762. } else if (tensor->type == GGML_TYPE_F16) {
  7763. 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]));
  7764. } else if (tensor->type == GGML_TYPE_I32) {
  7765. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7766. } else {
  7767. GGML_ABORT("fatal error");
  7768. }
  7769. fprintf(stderr, "% 7.2f ", val);
  7770. } else {
  7771. fprintf(stderr, " ");
  7772. }
  7773. }
  7774. fprintf(stderr, "\n");
  7775. }
  7776. }
  7777. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  7778. void * tensor_data = tensor->data;
  7779. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  7780. if (is_gpu) {
  7781. const size_t tensor_size = ggml_nbytes(tensor);
  7782. tensor_data = malloc(tensor_size);
  7783. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7784. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  7785. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  7786. }
  7787. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  7788. std::cerr << "tensor=" << tensor << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << std::endl;
  7789. if (tensor->src[0] != nullptr) {
  7790. std::cerr << "tensor->src[0]=" << tensor->src[0] << " name=" << tensor->src[0]->name << " op=" << ggml_op_name(tensor->src[0]->op) << " type=" << ggml_type_name(tensor->src[0]->type) << " ne0=" << tensor->src[0]->ne[0] << " nb0=" << tensor->src[0]->nb[0] << " ne1=" << tensor->src[0]->ne[1] << " nb1=" << tensor->src[0]->nb[1] << " ne2=" << tensor->src[0]->ne[2] << " nb2=" << tensor->src[0]->nb[2] << " ne3=" << tensor->src[0]->ne[3] << " nb3=" << tensor->src[0]->nb[3] << std::endl;
  7791. }
  7792. if (tensor->src[1] != nullptr) {
  7793. std::cerr << "tensor->src[1]=" << tensor->src[1] << " name=" << tensor->src[1]->name << " op=" << ggml_op_name(tensor->src[1]->op) << " type=" << ggml_type_name(tensor->src[1]->type) << " ne0=" << tensor->src[1]->ne[0] << " nb0=" << tensor->src[1]->nb[0] << " ne1=" << tensor->src[1]->ne[1] << " nb1=" << tensor->src[1]->nb[1] << " ne2=" << tensor->src[1]->ne[2] << " nb2=" << tensor->src[1]->nb[2] << " ne3=" << tensor->src[1]->ne[3] << " nb3=" << tensor->src[1]->nb[3] << std::endl;
  7794. }
  7795. std::cerr << std::endl << "Result:" << std::endl;
  7796. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  7797. std::cerr << std::endl;
  7798. std::vector<const ggml_tensor *> done;
  7799. ggml_vk_print_graph_origin(tensor, done);
  7800. if (is_gpu) {
  7801. free(tensor_data);
  7802. }
  7803. }
  7804. void * comp_result;
  7805. size_t comp_size;
  7806. size_t comp_nb[GGML_MAX_DIMS];
  7807. size_t check_counter = 0;
  7808. static void ggml_vk_check_results_0(ggml_tensor * tensor) {
  7809. if (tensor->op == GGML_OP_TRANSPOSE) {
  7810. return;
  7811. }
  7812. check_counter++;
  7813. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  7814. return;
  7815. }
  7816. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  7817. ggml_tensor * src0 = tensor->src[0];
  7818. ggml_tensor * src1 = tensor->src[1];
  7819. struct ggml_init_params iparams = {
  7820. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  7821. /*.mem_buffer =*/ NULL,
  7822. /*.no_alloc =*/ false,
  7823. };
  7824. struct ggml_context * ggml_ctx = ggml_init(iparams);
  7825. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  7826. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  7827. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  7828. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  7829. struct ggml_tensor * tensor_clone = nullptr;
  7830. for (int i = 0; i < 6; i++) {
  7831. ggml_tensor * srci = tensor->src[i];
  7832. if (srci == nullptr) {
  7833. continue;
  7834. }
  7835. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  7836. size_t srci_size = ggml_nbytes(srci);
  7837. src_clone[i] = srci_clone;
  7838. src_size[i] = ggml_nbytes(srci);
  7839. src_buffer[i] = malloc(srci_size);
  7840. srci_clone->data = src_buffer[i];
  7841. if (ggml_backend_buffer_is_host(srci->buffer)) {
  7842. memcpy(srci_clone->data, srci->data, srci_size);
  7843. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  7844. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  7845. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  7846. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  7847. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  7848. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  7849. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  7850. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  7851. const int idx = i3*srci->ne[2] + i2;
  7852. ggml_vk_buffer_read(buffer_gpu, offset + idx * srci->nb[2], ((char *)srci_clone->data + idx * srci_clone->nb[2]), srci->ne[1] * srci->nb[1]);
  7853. }
  7854. }
  7855. srci_clone->nb[0] = srci->nb[0];
  7856. srci_clone->nb[1] = srci->nb[1];
  7857. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  7858. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  7859. }
  7860. } else {
  7861. if (offset + srci_size >= buffer_gpu->size) {
  7862. srci_size = buffer_gpu->size - offset;
  7863. }
  7864. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  7865. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  7866. }
  7867. } else {
  7868. GGML_ABORT("fatal error");
  7869. }
  7870. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  7871. ggml_vk_print_tensor(srci, srci_name[i]);
  7872. }
  7873. }
  7874. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  7875. const float *params = (const float *)tensor->op_params;
  7876. tensor_clone = ggml_flash_attn_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], params[0], params[1], params[2]);
  7877. } else if (tensor->op == GGML_OP_MUL_MAT) {
  7878. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  7879. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  7880. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  7881. } else if (tensor->op == GGML_OP_SUB) {
  7882. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  7883. } else if (tensor->op == GGML_OP_MUL) {
  7884. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  7885. } else if (tensor->op == GGML_OP_DIV) {
  7886. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  7887. } else if (tensor->op == GGML_OP_CONCAT) {
  7888. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  7889. } else if (tensor->op == GGML_OP_UPSCALE) {
  7890. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  7891. } else if (tensor->op == GGML_OP_SCALE) {
  7892. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], ((float *)tensor->op_params)[0]);
  7893. } else if (tensor->op == GGML_OP_SQR) {
  7894. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  7895. } else if (tensor->op == GGML_OP_SIN) {
  7896. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  7897. } else if (tensor->op == GGML_OP_COS) {
  7898. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  7899. } else if (tensor->op == GGML_OP_CLAMP) {
  7900. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  7901. } else if (tensor->op == GGML_OP_PAD) {
  7902. tensor_clone = ggml_pad(ggml_ctx, src_clone[0], tensor->ne[0] - src_clone[0]->ne[0], tensor->ne[1] - src_clone[0]->ne[1], tensor->ne[2] - src_clone[0]->ne[2], tensor->ne[3] - src_clone[0]->ne[3]);
  7903. } else if (tensor->op == GGML_OP_REPEAT) {
  7904. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  7905. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  7906. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  7907. } else if (tensor->op == GGML_OP_ADD) {
  7908. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  7909. } else if (tensor->op == GGML_OP_ACC) {
  7910. tensor_clone = ggml_acc(ggml_ctx, src_clone[0], src_clone[1], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3]);
  7911. } else if (tensor->op == GGML_OP_NORM) {
  7912. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  7913. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  7914. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], *(int *)tensor->op_params, ((float *)tensor->op_params)[1]);
  7915. } else if (tensor->op == GGML_OP_RMS_NORM) {
  7916. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  7917. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  7918. const float eps = ((float *) tensor->op_params)[0];
  7919. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  7920. } else if (tensor->op == GGML_OP_SILU_BACK) {
  7921. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  7922. } else if (tensor->op == GGML_OP_L2_NORM) {
  7923. const float eps = ((float *) tensor->op_params)[0];
  7924. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  7925. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  7926. if (src1 != nullptr) {
  7927. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  7928. } else {
  7929. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  7930. }
  7931. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  7932. tensor_clone = ggml_soft_max_ext_back(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  7933. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  7934. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], *(int *)tensor->op_params);
  7935. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  7936. const int n_dims = ((int32_t *) tensor->op_params)[1];
  7937. const int mode = ((int32_t *) tensor->op_params)[2];
  7938. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  7939. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  7940. const float freq_base = ((float *) tensor->op_params)[5];
  7941. const float freq_scale = ((float *) tensor->op_params)[6];
  7942. const float ext_factor = ((float *) tensor->op_params)[7];
  7943. const float attn_factor = ((float *) tensor->op_params)[8];
  7944. const float beta_fast = ((float *) tensor->op_params)[9];
  7945. const float beta_slow = ((float *) tensor->op_params)[10];
  7946. if (mode & GGML_ROPE_TYPE_MROPE) {
  7947. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  7948. if (tensor->op == GGML_OP_ROPE) {
  7949. tensor_clone = ggml_rope_multi(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  7950. } else {
  7951. tensor_clone = ggml_rope_multi_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  7952. }
  7953. } else {
  7954. if (tensor->op == GGML_OP_ROPE) {
  7955. tensor_clone = ggml_rope_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  7956. } else {
  7957. tensor_clone = ggml_rope_ext_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  7958. }
  7959. }
  7960. } else if (tensor->op == GGML_OP_UNARY) {
  7961. switch (ggml_get_unary_op(tensor)) {
  7962. case GGML_UNARY_OP_SILU:
  7963. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  7964. break;
  7965. case GGML_UNARY_OP_GELU:
  7966. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  7967. break;
  7968. case GGML_UNARY_OP_GELU_QUICK:
  7969. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  7970. break;
  7971. case GGML_UNARY_OP_RELU:
  7972. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  7973. break;
  7974. case GGML_UNARY_OP_TANH:
  7975. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  7976. break;
  7977. case GGML_UNARY_OP_SIGMOID:
  7978. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  7979. break;
  7980. default:
  7981. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  7982. GGML_ABORT("fatal error");
  7983. }
  7984. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  7985. if (src1 == nullptr) {
  7986. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  7987. tensor_clone->type = tensor->type;
  7988. } else {
  7989. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  7990. }
  7991. } else if (tensor->op == GGML_OP_CONT) {
  7992. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  7993. } else if (tensor->op == GGML_OP_RESHAPE) {
  7994. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  7995. } else if (tensor->op == GGML_OP_VIEW) {
  7996. tensor_clone = ggml_view_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], tensor->nb[1], tensor->nb[2], tensor->nb[3], ((int32_t *) tensor->op_params)[0]);
  7997. } else if (tensor->op == GGML_OP_PERMUTE) {
  7998. int32_t * params = (int32_t *)tensor->op_params;
  7999. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  8000. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  8001. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  8002. } else if (tensor->op == GGML_OP_GET_ROWS) {
  8003. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  8004. } else if (tensor->op == GGML_OP_ARGSORT) {
  8005. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  8006. } else if (tensor->op == GGML_OP_SUM) {
  8007. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  8008. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  8009. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  8010. } else if (tensor->op == GGML_OP_ARGMAX) {
  8011. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  8012. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  8013. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  8014. } else if (tensor->op == GGML_OP_IM2COL) {
  8015. const int32_t s0 = tensor->op_params[0];
  8016. const int32_t s1 = tensor->op_params[1];
  8017. const int32_t p0 = tensor->op_params[2];
  8018. const int32_t p1 = tensor->op_params[3];
  8019. const int32_t d0 = tensor->op_params[4];
  8020. const int32_t d1 = tensor->op_params[5];
  8021. const bool is_2D = tensor->op_params[6] == 1;
  8022. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  8023. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  8024. const int32_t dim = tensor->op_params[0];
  8025. const int32_t max_period = tensor->op_params[1];
  8026. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  8027. } else if (tensor->op == GGML_OP_POOL_2D) {
  8028. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  8029. const int32_t k0 = tensor->op_params[1];
  8030. const int32_t k1 = tensor->op_params[2];
  8031. const int32_t s0 = tensor->op_params[3];
  8032. const int32_t s1 = tensor->op_params[4];
  8033. const int32_t p0 = tensor->op_params[5];
  8034. const int32_t p1 = tensor->op_params[6];
  8035. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  8036. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  8037. const float * op_params = (const float *)tensor->op_params;
  8038. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  8039. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  8040. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  8041. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  8042. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  8043. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  8044. src_clone[4], src_clone[5], src_clone[6]);
  8045. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  8046. src_clone[0]->flags = src0->flags;
  8047. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  8048. src_clone[2], src_clone[3], src_clone[4]);
  8049. }
  8050. else {
  8051. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  8052. GGML_ABORT("fatal error");
  8053. }
  8054. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8055. ggml_build_forward_expand(cgraph, tensor_clone);
  8056. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  8057. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8058. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  8059. }
  8060. comp_size = ggml_nbytes(tensor_clone);
  8061. comp_result = malloc(comp_size);
  8062. memcpy(comp_result, tensor_clone->data, comp_size);
  8063. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8064. for (int i = 0; i < 6; i++) {
  8065. if (src_buffer[i] != nullptr) {
  8066. free(src_buffer[i]);
  8067. }
  8068. }
  8069. ggml_free(ggml_ctx);
  8070. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  8071. }
  8072. static void ggml_vk_check_results_1(ggml_tensor * tensor) {
  8073. if (tensor->op == GGML_OP_TRANSPOSE) {
  8074. return;
  8075. }
  8076. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  8077. return;
  8078. }
  8079. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  8080. ggml_tensor * src0 = tensor->src[0];
  8081. ggml_tensor * src1 = tensor->src[1];
  8082. ggml_tensor * src2 = tensor->src[2];
  8083. ggml_tensor * src3 = tensor->src[3];
  8084. void * tensor_data = tensor->data;
  8085. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8086. size_t tensor_size = ggml_nbytes(tensor);
  8087. tensor_data = malloc(tensor_size);
  8088. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8089. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  8090. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  8091. if (offset + tensor_size >= buffer_gpu->size) {
  8092. tensor_size = buffer_gpu->size - offset;
  8093. }
  8094. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  8095. }
  8096. float first_error_result = -1.0f;
  8097. float first_error_correct = -1.0f;
  8098. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  8099. double avg_err = 0.0;
  8100. size_t counter = 0;
  8101. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  8102. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  8103. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  8104. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  8105. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  8106. float correct = 0.0f;
  8107. float result = 0.0f;
  8108. if (buffer_size_fit) {
  8109. if (tensor->type == GGML_TYPE_F32) {
  8110. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8111. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8112. } else if (tensor->type == GGML_TYPE_F16) {
  8113. 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]));
  8114. 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]));
  8115. } else if (tensor->type == GGML_TYPE_I32) {
  8116. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8117. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8118. } else if (tensor->type == GGML_TYPE_I64) {
  8119. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8120. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8121. } else {
  8122. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  8123. }
  8124. } else {
  8125. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  8126. GGML_ABORT("fatal error");
  8127. }
  8128. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  8129. 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;
  8130. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  8131. if (src0 != nullptr) {
  8132. std::cerr << "src0=" << src0 << " src0->name=" << src0->name << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  8133. }
  8134. if (src1 != nullptr) {
  8135. std::cerr << "src1=" << src1 << " src1->name=" << src1->name << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  8136. }
  8137. if (src2 != nullptr) {
  8138. std::cerr << "src2=" << src2 << " src2->name=" << src2->name << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  8139. }
  8140. if (src3 != nullptr) {
  8141. std::cerr << "src3=" << src3 << " src3->name=" << src3->name << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  8142. }
  8143. 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;
  8144. std::cerr << std::endl << "Result:" << std::endl;
  8145. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  8146. std::cerr << std::endl << "Correct:" << std::endl;
  8147. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  8148. std::cerr << std::endl;
  8149. std::vector<const ggml_tensor *> done;
  8150. ggml_vk_print_graph_origin(tensor, done);
  8151. GGML_ABORT("fatal error");
  8152. }
  8153. if (first_error[0] == -1 && std::fabs(correct - result) > 0.1f) {
  8154. first_error[0] = i0;
  8155. first_error[1] = i1;
  8156. first_error[2] = i2;
  8157. first_error[3] = i3;
  8158. first_error_result = result;
  8159. first_error_correct = correct;
  8160. }
  8161. // Special case, value is infinite, avoid NaN result in avg_err
  8162. // NaN also appears in results, if both are nan error is 0
  8163. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  8164. avg_err += std::fabs(correct - result);
  8165. }
  8166. counter++;
  8167. }
  8168. }
  8169. }
  8170. }
  8171. avg_err /= counter;
  8172. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8173. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8174. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  8175. if (src0 != nullptr) {
  8176. std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  8177. }
  8178. if (src1 != nullptr) {
  8179. std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  8180. }
  8181. if (src2 != nullptr) {
  8182. std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  8183. }
  8184. if (src3 != nullptr) {
  8185. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  8186. }
  8187. 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;
  8188. std::cerr << std::endl << "Result:" << std::endl;
  8189. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  8190. std::cerr << std::endl << "Correct:" << std::endl;
  8191. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  8192. std::cerr << std::endl;
  8193. std::vector<const ggml_tensor *> done;
  8194. ggml_vk_print_graph_origin(tensor, done);
  8195. }
  8196. if (avg_err > 0.05 || std::isnan(avg_err)) {
  8197. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8198. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  8199. if (src0 != nullptr) {
  8200. std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  8201. }
  8202. if (src1 != nullptr) {
  8203. std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  8204. }
  8205. if (src2 != nullptr) {
  8206. std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  8207. }
  8208. if (src3 != nullptr) {
  8209. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  8210. }
  8211. 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;
  8212. std::cerr << std::endl << "Result:" << std::endl;
  8213. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  8214. std::cerr << std::endl << "Correct:" << std::endl;
  8215. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  8216. std::cerr << std::endl;
  8217. std::vector<const ggml_tensor *> done;
  8218. ggml_vk_print_graph_origin(tensor, done);
  8219. GGML_ABORT("fatal error");
  8220. } else {
  8221. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  8222. }
  8223. free(comp_result);
  8224. comp_result = nullptr;
  8225. comp_size = 0;
  8226. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8227. free(tensor_data);
  8228. }
  8229. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  8230. }
  8231. #endif
  8232. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)