ggml-vulkan.cpp 606 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #if defined(_MSC_VER)
  25. # define NOMINMAX 1
  26. # include <windows.h>
  27. # define YIELD() YieldProcessor()
  28. #elif defined(__clang__) || defined(__GNUC__)
  29. # if defined(__x86_64__) ||defined(__i386__)
  30. # include <immintrin.h>
  31. # define YIELD() _mm_pause()
  32. # elif defined(__arm__) || defined(__aarch64__)
  33. # if defined(__clang__)
  34. # include <arm_acle.h>
  35. # define YIELD() __yield()
  36. # else
  37. # define YIELD() asm volatile("yield")
  38. # endif
  39. # endif
  40. #endif
  41. #if !defined(YIELD)
  42. #define YIELD()
  43. #endif
  44. #include "ggml-impl.h"
  45. #include "ggml-backend-impl.h"
  46. #include "ggml-vulkan-shaders.hpp"
  47. // remove this once it's more widely available in the SDK
  48. #if !defined(VK_KHR_shader_bfloat16)
  49. #define VK_KHR_shader_bfloat16 1
  50. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  51. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  52. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  53. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  54. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  55. VkStructureType sType;
  56. void* pNext;
  57. VkBool32 shaderBFloat16Type;
  58. VkBool32 shaderBFloat16DotProduct;
  59. VkBool32 shaderBFloat16CooperativeMatrix;
  60. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  61. #endif
  62. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  63. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  64. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  65. #define VK_VENDOR_ID_AMD 0x1002
  66. #define VK_VENDOR_ID_APPLE 0x106b
  67. #define VK_VENDOR_ID_INTEL 0x8086
  68. #define VK_VENDOR_ID_NVIDIA 0x10de
  69. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  70. #define GGML_VK_MAX_NODES 8192
  71. #define MAX_VK_BUFFERS 256
  72. #define VK_CHECK(err, msg) \
  73. do { \
  74. vk::Result err_ = (err); \
  75. if (err_ != vk::Result::eSuccess) { \
  76. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  77. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  78. exit(1); \
  79. } \
  80. } while (0)
  81. #ifdef GGML_VULKAN_DEBUG
  82. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  83. #else
  84. #define VK_LOG_DEBUG(msg) ((void) 0)
  85. #endif // GGML_VULKAN_DEBUG
  86. struct ggml_backend_vk_context;
  87. #define MAX_PARAMETER_COUNT 8
  88. // Max number of adds that can be fused without exceeding MAX_PARAMETER_COUNT.
  89. #define MAX_FUSED_ADDS (MAX_PARAMETER_COUNT - 2)
  90. struct vk_pipeline_struct {
  91. std::string name;
  92. vk::ShaderModule shader_module;
  93. vk::PipelineLayout layout;
  94. vk::Pipeline pipeline;
  95. uint32_t push_constant_size;
  96. uint32_t parameter_count;
  97. std::array<uint32_t, 3> wg_denoms;
  98. uint32_t align;
  99. // set to true to request the pipeline is compiled after the dryrun
  100. bool needed {};
  101. // set to true when the shader has been compiled
  102. bool compiled {};
  103. };
  104. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  105. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  106. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  107. struct vk_matmul_pipeline_struct {
  108. vk_pipeline l, m, s;
  109. vk_pipeline a_l, a_m, a_s;
  110. };
  111. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  112. struct vk_matmul_pipeline2 {
  113. vk_matmul_pipeline2() {
  114. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  115. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  116. }
  117. vk_matmul_pipeline f32acc;
  118. vk_matmul_pipeline f16acc;
  119. };
  120. struct vk_device_struct;
  121. typedef std::shared_ptr<vk_device_struct> vk_device;
  122. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  123. struct vk_buffer_struct;
  124. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  125. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  126. struct ggml_backend_vk_buffer_type_context {
  127. std::string name;
  128. vk_device device;
  129. };
  130. struct vk_queue;
  131. // Stores command pool/buffers. There's an instance of this
  132. // for each (context,queue) pair and for each (device,queue) pair.
  133. struct vk_command_pool {
  134. void init(vk_device& device, vk_queue *q_);
  135. void destroy(vk::Device& device);
  136. vk::CommandPool pool;
  137. uint32_t cmd_buffer_idx;
  138. std::vector<vk::CommandBuffer> cmd_buffers;
  139. vk_queue *q;
  140. };
  141. // Prevent simultaneous submissions to the same queue.
  142. // This could be per vk_queue if we stopped having two vk_queue structures
  143. // sharing the same vk::Queue.
  144. static std::mutex queue_mutex;
  145. struct vk_queue {
  146. uint32_t queue_family_index;
  147. vk::Queue queue;
  148. vk_command_pool cmd_pool;
  149. vk::PipelineStageFlags stage_flags;
  150. bool transfer_only;
  151. // copy everything except the cmd_pool
  152. void copyFrom(vk_queue &other) {
  153. queue_family_index = other.queue_family_index;
  154. queue = other.queue;
  155. stage_flags = other.stage_flags;
  156. transfer_only = other.transfer_only;
  157. }
  158. };
  159. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  160. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  161. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  162. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  163. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  164. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  165. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  166. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  167. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  168. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  169. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  170. /* .is_host = */ NULL,
  171. };
  172. #ifdef GGML_VULKAN_MEMORY_DEBUG
  173. class vk_memory_logger;
  174. #endif
  175. class vk_perf_logger;
  176. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  177. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  178. static constexpr uint32_t p021_max_gqa_ratio = 8;
  179. enum vk_device_architecture {
  180. OTHER,
  181. AMD_GCN,
  182. AMD_RDNA1,
  183. AMD_RDNA2,
  184. AMD_RDNA3,
  185. INTEL_XE2,
  186. NVIDIA_PRE_TURING,
  187. };
  188. // HSK x HSV
  189. enum FaHeadSizes {
  190. FA_HEAD_SIZE_64,
  191. FA_HEAD_SIZE_80,
  192. FA_HEAD_SIZE_96,
  193. FA_HEAD_SIZE_112,
  194. FA_HEAD_SIZE_128,
  195. FA_HEAD_SIZE_192,
  196. FA_HEAD_SIZE_192_128,
  197. FA_HEAD_SIZE_256,
  198. FA_HEAD_SIZE_576_512,
  199. FA_HEAD_SIZE_UNSUPPORTED,
  200. FA_HEAD_SIZE_COUNT = FA_HEAD_SIZE_UNSUPPORTED,
  201. };
  202. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  203. vk::PhysicalDeviceProperties props = device.getProperties();
  204. if (props.vendorID == VK_VENDOR_ID_AMD) {
  205. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  206. bool amd_shader_core_properties = false;
  207. bool integer_dot_product = false;
  208. bool subgroup_size_control = false;
  209. for (const auto& properties : ext_props) {
  210. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  211. amd_shader_core_properties = true;
  212. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  213. integer_dot_product = true;
  214. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  215. subgroup_size_control = true;
  216. }
  217. }
  218. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  219. return vk_device_architecture::OTHER;
  220. }
  221. vk::PhysicalDeviceProperties2 props2;
  222. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  223. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  224. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  225. props2.pNext = &shader_core_props_amd;
  226. shader_core_props_amd.pNext = &integer_dot_props;
  227. integer_dot_props.pNext = &subgroup_size_control_props;
  228. device.getProperties2(&props2);
  229. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  230. return vk_device_architecture::AMD_GCN;
  231. }
  232. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  233. // RDNA
  234. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  235. return vk_device_architecture::AMD_RDNA1;
  236. }
  237. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  238. return vk_device_architecture::AMD_RDNA3;
  239. }
  240. return vk_device_architecture::AMD_RDNA2;
  241. }
  242. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  243. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  244. bool subgroup_size_control = false;
  245. for (const auto& properties : ext_props) {
  246. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  247. subgroup_size_control = true;
  248. }
  249. }
  250. if (!subgroup_size_control) {
  251. return vk_device_architecture::OTHER;
  252. }
  253. vk::PhysicalDeviceProperties2 props2;
  254. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  255. props2.pNext = &subgroup_size_control_props;
  256. device.getProperties2(&props2);
  257. if (subgroup_size_control_props.minSubgroupSize == 16) {
  258. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  259. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  260. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  261. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  262. return vk_device_architecture::INTEL_XE2;
  263. }
  264. } else if (props.vendorID == VK_VENDOR_ID_NVIDIA) {
  265. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  266. bool cooperative_matrix = false;
  267. // Detect "pre-turing" based on lack of coopmat support.
  268. for (const auto& properties : ext_props) {
  269. if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0) {
  270. cooperative_matrix = true;
  271. break;
  272. }
  273. }
  274. if (!cooperative_matrix) {
  275. return vk_device_architecture::NVIDIA_PRE_TURING;
  276. }
  277. }
  278. return vk_device_architecture::OTHER;
  279. }
  280. enum vk_conv_shapes {
  281. CONV_SHAPE_128x128,
  282. CONV_SHAPE_64x32,
  283. CONV_SHAPE_32x256,
  284. CONV_SHAPE_COUNT,
  285. };
  286. enum dmmv_wg_sizes {
  287. DMMV_WG_SIZE_SUBGROUP,
  288. DMMV_WG_SIZE_LARGE,
  289. DMMV_WG_SIZE_COUNT,
  290. };
  291. static constexpr uint32_t num_argsort_pipelines = 11;
  292. static constexpr uint32_t max_argsort_cols = 1 << (num_argsort_pipelines-1);
  293. struct vk_device_struct {
  294. std::recursive_mutex mutex;
  295. vk::PhysicalDevice physical_device;
  296. vk::PhysicalDeviceProperties properties;
  297. std::string name;
  298. uint64_t max_memory_allocation_size;
  299. uint64_t suballocation_block_size;
  300. bool fp16;
  301. bool bf16;
  302. bool pipeline_robustness;
  303. vk::Device device;
  304. uint32_t vendor_id;
  305. vk::DriverId driver_id;
  306. vk_device_architecture architecture;
  307. vk_queue compute_queue;
  308. vk_queue transfer_queue;
  309. bool single_queue;
  310. uint32_t subgroup_size;
  311. uint32_t shader_core_count;
  312. bool uma;
  313. bool prefer_host_memory;
  314. bool float_controls_rte_fp16;
  315. bool subgroup_add;
  316. bool subgroup_shuffle;
  317. bool multi_add;
  318. bool integer_dot_product;
  319. bool subgroup_size_control;
  320. uint32_t subgroup_min_size;
  321. uint32_t subgroup_max_size;
  322. bool subgroup_require_full_support;
  323. bool coopmat_support;
  324. bool coopmat_acc_f32_support {};
  325. bool coopmat_acc_f16_support {};
  326. bool coopmat_bf16_support {};
  327. bool coopmat_support_16x16x16_f16acc {};
  328. bool coopmat_support_16x16x16_f32acc {};
  329. bool coopmat1_fa_support {};
  330. uint32_t coopmat_m;
  331. uint32_t coopmat_n;
  332. uint32_t coopmat_k;
  333. bool coopmat_int_support;
  334. uint32_t coopmat_int_m;
  335. uint32_t coopmat_int_n;
  336. uint32_t coopmat_int_k;
  337. bool coopmat2;
  338. size_t idx;
  339. bool mul_mat_l[GGML_TYPE_COUNT];
  340. bool mul_mat_m[GGML_TYPE_COUNT];
  341. bool mul_mat_s[GGML_TYPE_COUNT];
  342. bool mul_mat_id_l[GGML_TYPE_COUNT];
  343. bool mul_mat_id_m[GGML_TYPE_COUNT];
  344. bool mul_mat_id_s[GGML_TYPE_COUNT];
  345. // set to true to indicate that some shaders need to be compiled after the dryrun
  346. bool need_compiles {};
  347. vk::DescriptorSetLayout dsl;
  348. vk_matmul_pipeline pipeline_matmul_f32 {};
  349. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  350. vk_matmul_pipeline pipeline_matmul_bf16 {};
  351. vk_matmul_pipeline2 pipeline_matmul_f16;
  352. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  353. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  354. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  355. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  356. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  357. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  358. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  359. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  360. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  361. vk_pipeline pipeline_matmul_split_k_reduce;
  362. vk_pipeline pipeline_quantize_q8_1;
  363. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  364. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  365. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  366. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  367. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  368. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  369. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  370. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  371. vk_pipeline pipeline_acc_f32;
  372. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  373. vk_pipeline pipeline_add[2][2][2];
  374. vk_pipeline pipeline_add_norepeat[2][2][2];
  375. vk_pipeline pipeline_sub[2][2][2];
  376. vk_pipeline pipeline_sub_norepeat[2][2][2];
  377. vk_pipeline pipeline_mul[2][2][2];
  378. vk_pipeline pipeline_mul_norepeat[2][2][2];
  379. vk_pipeline pipeline_div[2][2][2];
  380. vk_pipeline pipeline_div_norepeat[2][2][2];
  381. // indexed by num_additional_fused_ops == num_adds - 1
  382. vk_pipeline pipeline_multi_add[MAX_FUSED_ADDS];
  383. vk_pipeline pipeline_add_id_f32;
  384. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  385. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bilinear_ac_f32;
  386. vk_pipeline pipeline_scale_f32;
  387. vk_pipeline pipeline_sqr_f32;
  388. vk_pipeline pipeline_sqrt_f32;
  389. vk_pipeline pipeline_sin_f32;
  390. vk_pipeline pipeline_cos_f32;
  391. vk_pipeline pipeline_clamp_f32;
  392. vk_pipeline pipeline_pad_f32;
  393. vk_pipeline pipeline_roll_f32;
  394. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  395. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  396. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16;
  397. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  398. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  399. vk_pipeline pipeline_set_rows[GGML_TYPE_COUNT];
  400. vk_pipeline pipeline_norm_f32;
  401. vk_pipeline pipeline_group_norm_f32;
  402. vk_pipeline pipeline_rms_norm_f32;
  403. vk_pipeline pipeline_rms_norm_mul_f32;
  404. vk_pipeline pipeline_rms_norm_back_f32;
  405. vk_pipeline pipeline_l2_norm_f32;
  406. // [src/dst 0=fp32,1=fp16]
  407. vk_pipeline pipeline_gelu[2];
  408. vk_pipeline pipeline_gelu_erf[2];
  409. vk_pipeline pipeline_gelu_quick[2];
  410. vk_pipeline pipeline_silu[2];
  411. vk_pipeline pipeline_relu[2];
  412. vk_pipeline pipeline_tanh[2];
  413. vk_pipeline pipeline_sigmoid[2];
  414. vk_pipeline pipeline_geglu[2];
  415. vk_pipeline pipeline_reglu[2];
  416. vk_pipeline pipeline_swiglu[2];
  417. vk_pipeline pipeline_swiglu_oai[2];
  418. vk_pipeline pipeline_geglu_erf[2];
  419. vk_pipeline pipeline_geglu_quick[2];
  420. vk_pipeline pipeline_leaky_relu_f32;
  421. vk_pipeline pipeline_silu_back_f32;
  422. vk_pipeline pipeline_diag_mask_inf_f32;
  423. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  424. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  425. vk_pipeline pipeline_soft_max_back_f32;
  426. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  427. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  428. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  429. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  430. vk_pipeline pipeline_argsort_f32[num_argsort_pipelines];
  431. vk_pipeline pipeline_sum_rows_f32;
  432. vk_pipeline pipeline_argmax_f32;
  433. vk_pipeline pipeline_count_equal_i32;
  434. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  435. vk_pipeline pipeline_timestep_embedding_f32;
  436. vk_pipeline pipeline_conv_transpose_1d_f32;
  437. vk_pipeline pipeline_pool2d_f32;
  438. vk_pipeline pipeline_rwkv_wkv6_f32;
  439. vk_pipeline pipeline_rwkv_wkv7_f32;
  440. vk_pipeline pipeline_opt_step_adamw_f32;
  441. vk_pipeline pipeline_opt_step_sgd_f32;
  442. vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
  443. vk_pipeline pipeline_conv2d_f16_f32[CONV_SHAPE_COUNT];
  444. vk_pipeline pipeline_conv2d_dw_whcn_f32;
  445. vk_pipeline pipeline_conv2d_dw_cwhn_f32;
  446. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  447. vk_pipeline pipeline_flash_attn_f32_f16_cm2[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  448. vk_pipeline pipeline_flash_attn_f32_f16_cm1[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  449. vk_pipeline pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  450. vk_pipeline pipeline_flash_attn_split_k_reduce;
  451. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  452. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  453. vk::Fence fence;
  454. vk_buffer sync_staging;
  455. ggml_backend_buffer_type buffer_type;
  456. bool disable_fusion;
  457. bool disable_host_visible_vidmem;
  458. #ifdef GGML_VULKAN_MEMORY_DEBUG
  459. std::unique_ptr<vk_memory_logger> memory_logger;
  460. #endif
  461. // for GGML_VK_PERF_LOGGER
  462. std::unique_ptr<vk_perf_logger> perf_logger;
  463. vk::QueryPool query_pool;
  464. int32_t num_queries;
  465. ~vk_device_struct() {
  466. VK_LOG_DEBUG("destroy device " << name);
  467. device.destroyFence(fence);
  468. ggml_vk_destroy_buffer(sync_staging);
  469. compute_queue.cmd_pool.destroy(device);
  470. transfer_queue.cmd_pool.destroy(device);
  471. for (auto& pipeline : pipelines) {
  472. if (pipeline.second.expired()) {
  473. continue;
  474. }
  475. vk_pipeline pl = pipeline.second.lock();
  476. ggml_vk_destroy_pipeline(device, pl);
  477. }
  478. pipelines.clear();
  479. device.destroyDescriptorSetLayout(dsl);
  480. device.destroy();
  481. }
  482. };
  483. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  484. cmd_buffer_idx = 0;
  485. q = q_;
  486. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  487. pool = device->device.createCommandPool(command_pool_create_info);
  488. }
  489. void vk_command_pool::destroy(vk::Device& device) {
  490. device.destroyCommandPool(pool);
  491. pool = nullptr;
  492. cmd_buffers.clear();
  493. }
  494. struct vk_buffer_struct {
  495. vk::Buffer buffer = VK_NULL_HANDLE;
  496. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  497. vk::MemoryPropertyFlags memory_property_flags;
  498. void * ptr;
  499. size_t size = 0;
  500. vk_device device;
  501. ~vk_buffer_struct() {
  502. if (size == 0) {
  503. return;
  504. }
  505. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  506. device->device.freeMemory(device_memory);
  507. device->device.destroyBuffer(buffer);
  508. }
  509. };
  510. struct vk_subbuffer {
  511. vk_buffer buffer;
  512. uint64_t offset;
  513. uint64_t size;
  514. operator vk::DescriptorBufferInfo() const {
  515. return { buffer->buffer, offset, size };
  516. }
  517. };
  518. struct vk_semaphore {
  519. vk::Semaphore s;
  520. uint64_t value;
  521. };
  522. struct vk_submission {
  523. vk::CommandBuffer buffer;
  524. std::vector<vk_semaphore> wait_semaphores;
  525. std::vector<vk_semaphore> signal_semaphores;
  526. };
  527. typedef std::vector<vk_submission> vk_sequence;
  528. struct vk_mat_mat_push_constants {
  529. uint32_t M; uint32_t N; uint32_t K;
  530. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  531. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  532. uint32_t k_split;
  533. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  534. uint32_t padded_N;
  535. };
  536. struct vk_mat_vec_push_constants {
  537. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  538. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  539. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  540. };
  541. struct vk_mat_mat_id_push_constants {
  542. uint32_t M; uint32_t N; uint32_t K;
  543. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  544. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  545. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  546. uint32_t padded_N;
  547. };
  548. struct vk_mat_vec_id_push_constants {
  549. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  550. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  551. uint32_t nei0; uint32_t ne11;
  552. };
  553. struct vk_flash_attn_push_constants {
  554. uint32_t N;
  555. uint32_t KV;
  556. uint32_t ne1;
  557. uint32_t ne2;
  558. uint32_t ne3;
  559. uint32_t neq2;
  560. uint32_t neq3;
  561. uint32_t nek2;
  562. uint32_t nek3;
  563. uint32_t nev2;
  564. uint32_t nev3;
  565. uint32_t nem1;
  566. uint32_t nem2;
  567. uint32_t nem3;
  568. uint32_t nb01;
  569. uint32_t nb02;
  570. uint32_t nb03;
  571. uint32_t nb11;
  572. uint32_t nb12;
  573. uint32_t nb13;
  574. uint32_t nb21;
  575. uint32_t nb22;
  576. uint32_t nb23;
  577. float scale;
  578. float max_bias;
  579. float logit_softcap;
  580. uint32_t mask_n_head_log2;
  581. float m0;
  582. float m1;
  583. uint32_t gqa_ratio;
  584. uint32_t split_kv;
  585. uint32_t k_num;
  586. };
  587. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  588. struct vk_op_push_constants {
  589. uint32_t KX;
  590. uint32_t KY;
  591. float param1;
  592. float param2;
  593. };
  594. struct vk_op_glu_push_constants {
  595. uint32_t N;
  596. uint32_t ne00;
  597. uint32_t ne20;
  598. uint32_t mode; // 0: default, 1: swapped, 2: split
  599. float alpha; // for swiglu_oai
  600. float limit;
  601. };
  602. struct vk_op_unary_push_constants {
  603. uint32_t ne;
  604. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  605. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  606. uint32_t misalign_offsets;
  607. float param1; float param2;
  608. uint32_t ne0_012mp; uint32_t ne0_012L;
  609. uint32_t ne0_01mp; uint32_t ne0_01L;
  610. uint32_t ne0_0mp; uint32_t ne0_0L;
  611. uint32_t ne1_012mp; uint32_t ne1_012L;
  612. uint32_t ne1_01mp; uint32_t ne1_01L;
  613. uint32_t ne1_0mp; uint32_t ne1_0L;
  614. };
  615. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  616. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  617. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  618. ne = ne != 0 ? ne : ggml_nelements(dst);
  619. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  620. vk_op_unary_push_constants p{};
  621. p.ne = (uint32_t)ne;
  622. size_t src0_tsize = ggml_type_size(src0->type);
  623. p.ne00 = (uint32_t)src0->ne[0];
  624. p.ne01 = (uint32_t)src0->ne[1];
  625. p.ne02 = (uint32_t)src0->ne[2];
  626. p.ne03 = (uint32_t)src0->ne[3];
  627. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  628. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  629. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  630. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  631. size_t dst_tsize = ggml_type_size(dst->type);
  632. p.ne10 = (uint32_t)dst->ne[0];
  633. p.ne11 = (uint32_t)dst->ne[1];
  634. p.ne12 = (uint32_t)dst->ne[2];
  635. p.ne13 = (uint32_t)dst->ne[3];
  636. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  637. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  638. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  639. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  640. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  641. }
  642. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  643. // Precompute mp (m' in the paper) and L such that division
  644. // can be computed using a multiply (high 32b of 64b result)
  645. // and a shift:
  646. //
  647. // n/d = (mulhi(n, mp) + n) >> L;
  648. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  649. {
  650. // compute L = ceil(log2(d));
  651. L = 0;
  652. while (L < 32 && (uint32_t{1} << L) < d) {
  653. L++;
  654. }
  655. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  656. }
  657. template <typename T> void init_pushconst_fastdiv(T &p) {
  658. GGML_UNUSED(p);
  659. static_assert(!std::is_const<T>::value, "unexpected type");
  660. }
  661. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  662. // Compute magic values to divide by these six numbers.
  663. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  664. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  665. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  666. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  667. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  668. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  669. }
  670. struct vk_op_binary_push_constants {
  671. uint32_t ne;
  672. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  673. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  674. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  675. uint32_t misalign_offsets;
  676. float param1; float param2; int32_t param3;
  677. };
  678. struct vk_op_multi_add_push_constants {
  679. // shape for dst
  680. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23;
  681. // strides for srcs+dst
  682. uint32_t nb[8][4];
  683. };
  684. struct vk_op_add_id_push_constants {
  685. uint32_t ne0;
  686. uint32_t ne1;
  687. uint32_t s01;
  688. uint32_t s02;
  689. uint32_t s11;
  690. uint32_t s21;
  691. };
  692. struct vk_op_diag_mask_push_constants {
  693. uint32_t ncols;
  694. uint32_t rows_per_channel;
  695. int32_t n_past;
  696. };
  697. struct vk_op_rope_push_constants {
  698. uint32_t ncols;
  699. uint32_t n_dims;
  700. float freq_scale;
  701. uint32_t p_delta_rows;
  702. float freq_base;
  703. float ext_factor;
  704. float attn_factor;
  705. float corr_dims[2];
  706. float theta_scale;
  707. uint32_t has_ff;
  708. uint32_t ne02;
  709. uint32_t s1;
  710. uint32_t s2;
  711. int32_t sections[4];
  712. uint32_t is_back;
  713. };
  714. struct vk_op_soft_max_push_constants {
  715. uint32_t KX;
  716. uint32_t KY;
  717. uint32_t ne00;
  718. uint32_t ne01;
  719. uint32_t ne02;
  720. uint32_t ne12;
  721. uint32_t ne13;
  722. uint32_t nb11;
  723. uint32_t nb12;
  724. uint32_t nb13;
  725. float scale;
  726. float max_bias;
  727. float m0;
  728. float m1;
  729. uint32_t n_head_log2;
  730. uint32_t nrows_x;
  731. uint32_t has_sinks;
  732. };
  733. struct vk_op_argsort_push_constants {
  734. uint32_t ncols;
  735. int32_t order;
  736. };
  737. struct vk_op_im2col_push_constants {
  738. uint32_t batch_offset; uint32_t offset_delta;
  739. uint32_t IC;
  740. uint32_t IW; uint32_t IH;
  741. uint32_t OW; uint32_t OH;
  742. uint32_t KW; uint32_t KH;
  743. uint32_t pelements;
  744. uint32_t CHW;
  745. int32_t s0; int32_t s1;
  746. int32_t p0; int32_t p1;
  747. int32_t d0; int32_t d1;
  748. };
  749. struct vk_op_timestep_embedding_push_constants {
  750. uint32_t nb1;
  751. uint32_t dim;
  752. uint32_t max_period;
  753. };
  754. struct vk_op_conv_transpose_1d_push_constants {
  755. uint32_t Cout;
  756. uint32_t Cin;
  757. uint32_t K;
  758. uint32_t L;
  759. uint32_t KL;
  760. uint32_t nb01;
  761. uint32_t nb02;
  762. uint32_t nb11;
  763. uint32_t nb1;
  764. int32_t s0;
  765. };
  766. struct vk_op_pool2d_push_constants {
  767. uint32_t IW; uint32_t IH;
  768. uint32_t OW; uint32_t OH;
  769. uint32_t OC;
  770. uint32_t pelements;
  771. uint32_t op;
  772. int32_t k0; int32_t k1;
  773. int32_t s0; int32_t s1;
  774. int32_t p0; int32_t p1;
  775. };
  776. struct vk_op_rwkv_wkv6_push_constants {
  777. uint32_t B;
  778. uint32_t T;
  779. uint32_t C;
  780. uint32_t H;
  781. };
  782. struct vk_op_rwkv_wkv7_push_constants {
  783. uint32_t B;
  784. uint32_t T;
  785. uint32_t C;
  786. uint32_t H;
  787. };
  788. struct vk_op_conv2d_push_constants {
  789. uint32_t Cout;
  790. uint32_t Cin;
  791. uint32_t N;
  792. uint32_t KW;
  793. uint32_t KH;
  794. uint32_t W;
  795. uint32_t H;
  796. uint32_t OW;
  797. uint32_t OH;
  798. uint32_t s0;
  799. uint32_t s1;
  800. uint32_t p0;
  801. uint32_t p1;
  802. uint32_t d0;
  803. uint32_t d1;
  804. uint32_t nb01;
  805. uint32_t nb02;
  806. uint32_t nb03;
  807. uint32_t nb11;
  808. uint32_t nb12;
  809. uint32_t nb13;
  810. uint32_t nb1;
  811. uint32_t nb2;
  812. uint32_t nb3;
  813. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH
  814. uint32_t KWmp; uint32_t KWL;
  815. uint32_t KWKHmp; uint32_t KWKHL;
  816. uint32_t OWmp; uint32_t OWL;
  817. uint32_t OWOHmp; uint32_t OWOHL;
  818. };
  819. template <> void init_pushconst_fastdiv(vk_op_conv2d_push_constants &p) {
  820. // Compute magic values to divide by KW, KW*KH, OW, OW*OH
  821. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  822. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  823. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  824. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  825. }
  826. struct vk_op_conv2d_dw_push_constants {
  827. uint32_t ne;
  828. uint32_t batches;
  829. uint32_t channels;
  830. uint32_t dst_w;
  831. uint32_t dst_h;
  832. uint32_t src_w;
  833. uint32_t src_h;
  834. uint32_t knl_w;
  835. uint32_t knl_h;
  836. int32_t stride_x;
  837. int32_t stride_y;
  838. int32_t pad_x;
  839. int32_t pad_y;
  840. int32_t dilation_x;
  841. int32_t dilation_y;
  842. };
  843. struct vk_op_upscale_push_constants {
  844. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  845. uint32_t ne00; uint32_t ne01;
  846. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  847. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  848. float sf0; float sf1; float sf2; float sf3;
  849. };
  850. // Allow pre-recording command buffers
  851. struct vk_staging_memcpy {
  852. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  853. void * dst;
  854. const void * src;
  855. size_t n;
  856. };
  857. struct vk_context_struct {
  858. vk_submission * s;
  859. std::vector<vk_sequence> seqs;
  860. int exit_tensor_idx;
  861. std::vector<vk_staging_memcpy> in_memcpys;
  862. std::vector<vk_staging_memcpy> out_memcpys;
  863. vk_command_pool * p {};
  864. };
  865. typedef std::shared_ptr<vk_context_struct> vk_context;
  866. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  867. struct ggml_vk_garbage_collector {
  868. std::vector<vk_semaphore> tl_semaphores;
  869. std::vector<vk_semaphore> semaphores;
  870. std::vector<vk::Event> events;
  871. std::vector<vk_buffer> temp_buffers;
  872. std::vector<vk_context> contexts;
  873. };
  874. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  875. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  876. static std::string format_size(size_t size) {
  877. const size_t kib = 1024;
  878. const size_t mib = kib * 1024;
  879. const size_t gib = mib * 1024;
  880. std::ostringstream oss;
  881. oss << std::fixed << std::setprecision(2);
  882. if (size >= gib) {
  883. oss << static_cast<double>(size) / gib << " GiB";
  884. } else if (size >= mib) {
  885. oss << static_cast<double>(size) / mib << " MiB";
  886. } else if (size >= kib) {
  887. oss << static_cast<double>(size) / kib << " KiB";
  888. } else {
  889. oss << size << " B";
  890. }
  891. return oss.str();
  892. }
  893. static std::mutex log_mutex;
  894. class vk_memory_logger {
  895. public:
  896. vk_memory_logger(): total_device(0), total_host(0) {}
  897. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  898. void log_deallocation(vk_buffer_ref buf_ref);
  899. private:
  900. std::map<vk::Buffer, size_t> allocations; // Track allocations
  901. size_t total_device;
  902. size_t total_host;
  903. };
  904. #else
  905. #define VK_LOG_MEMORY(msg) ((void) 0)
  906. #endif // GGML_VULKAN_MEMORY_DEBUG
  907. class vk_perf_logger {
  908. public:
  909. void print_timings() {
  910. if (timings.empty()) {
  911. return;
  912. }
  913. uint64_t total_all_op_times = 0;
  914. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  915. for (const auto & t : timings) {
  916. uint64_t total_op_times = 0;
  917. for (const auto & time : t.second) {
  918. total_op_times += time;
  919. }
  920. std::cerr << t.first << ": " << t.second.size() << " x " << (total_op_times / t.second.size() / 1000.0)
  921. << " us";
  922. // If we have as many flops entries as timing entries for the op, then compute and log the flops/S.
  923. auto it = flops.find(t.first);
  924. if (it != flops.end() && (it->second).size() == t.second.size()) {
  925. uint64_t total_op_flops = 0;
  926. for (const auto & elem : it->second) {
  927. total_op_flops += elem;
  928. }
  929. std::cerr << " ("
  930. << (double(total_op_flops) / (1000.0 * 1000.0 * 1000.0)) /
  931. (double(total_op_times) / (1000.0 * 1000.0 * 1000.0))
  932. << " GFLOPS/s)";
  933. }
  934. total_all_op_times += total_op_times;
  935. std::cerr << std::endl;
  936. }
  937. if (timings.size() > 0) {
  938. std::cerr << "Total time: " << total_all_op_times / 1000.0 << " us." << std::endl;
  939. }
  940. timings.clear();
  941. flops.clear();
  942. }
  943. void log_timing(const ggml_tensor * node, uint64_t time) {
  944. if (node->op == GGML_OP_UNARY) {
  945. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  946. return;
  947. }
  948. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  949. const uint64_t m = node->src[0]->ne[1];
  950. const uint64_t n = node->ne[1];
  951. const uint64_t k = node->src[1]->ne[0];
  952. const uint64_t batch = node->src[1]->ne[2] * node->src[1]->ne[3];
  953. std::string name = ggml_op_name(node->op);
  954. if ((node->op == GGML_OP_MUL_MAT && n <= mul_mat_vec_max_cols) ||
  955. (node->op == GGML_OP_MUL_MAT_ID && node->src[2]->ne[1] == 1)) {
  956. name += "_VEC";
  957. }
  958. name += " ";
  959. name += ggml_type_name(node->src[0]->type);
  960. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  961. if (batch > 1) {
  962. name += " batch=" + std::to_string(batch);
  963. }
  964. timings[name].push_back(time);
  965. flops[name].push_back(m * n * (k + (k - 1)) * batch);
  966. return;
  967. }
  968. if (node->op == GGML_OP_CONV_2D) {
  969. std::string name = ggml_op_name(node->op);
  970. ggml_tensor * knl = node->src[0];
  971. uint64_t OW = node->ne[0];
  972. uint64_t OH = node->ne[1];
  973. uint64_t N = node->ne[3];
  974. uint64_t Cout = node->ne[2];
  975. uint64_t KW = knl->ne[0];
  976. uint64_t KH = knl->ne[1];
  977. uint64_t Cin = knl->ne[2];
  978. // KxCRS @ CRSxNPQ = KxNPQ -> M=K, K=CRS, N=NPQ
  979. uint64_t size_M = Cout;
  980. uint64_t size_K = Cin * KW * KH;
  981. uint64_t size_N = N * OW * OH;
  982. uint64_t n_flops = size_M * size_N * (size_K + (size_K - 1));
  983. name += " M=Cout=" + std::to_string(size_M) + ", K=Cin*KW*KH=" + std::to_string(size_K) +
  984. ", N=N*OW*OH=" + std::to_string(size_N);
  985. flops[name].push_back(n_flops);
  986. timings[name].push_back(time);
  987. return;
  988. }
  989. timings[ggml_op_name(node->op)].push_back(time);
  990. }
  991. private:
  992. std::map<std::string, std::vector<uint64_t>> timings;
  993. std::map<std::string, std::vector<uint64_t>> flops;
  994. };
  995. struct ggml_backend_vk_context {
  996. std::string name;
  997. vk_device device;
  998. size_t semaphore_idx, event_idx;
  999. ggml_vk_garbage_collector gc;
  1000. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  1001. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  1002. vk::Fence fence, almost_ready_fence;
  1003. bool almost_ready_fence_pending {};
  1004. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  1005. vk_context_ref compute_ctx;
  1006. vk_context_ref transfer_ctx;
  1007. std::vector<vk_context_ref> tensor_ctxs;
  1008. std::vector<vk::DescriptorPool> descriptor_pools;
  1009. std::vector<vk::DescriptorSet> descriptor_sets;
  1010. uint32_t descriptor_set_idx {};
  1011. uint32_t pipeline_descriptor_set_requirements {};
  1012. vk_command_pool compute_cmd_pool;
  1013. vk_command_pool transfer_cmd_pool;
  1014. // number of additional consecutive nodes that are being fused with the
  1015. // node currently being processed
  1016. int num_additional_fused_ops {};
  1017. };
  1018. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  1019. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  1020. if (tensor->view_src) {
  1021. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  1022. }
  1023. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  1024. }
  1025. struct ggml_backend_vk_buffer_context {
  1026. vk_device_ref device;
  1027. vk_buffer dev_buffer;
  1028. std::string name;
  1029. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  1030. device(device),
  1031. dev_buffer(dev_buffer),
  1032. name(name) {
  1033. }
  1034. ~ggml_backend_vk_buffer_context() {
  1035. ggml_vk_destroy_buffer(dev_buffer);
  1036. }
  1037. };
  1038. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1039. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  1040. std::lock_guard<std::mutex> guard(log_mutex);
  1041. vk_buffer buf = buf_ref.lock();
  1042. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1043. const std::string type = device ? "device" : "host";
  1044. allocations[buf->buffer] = size;
  1045. total_device += device ? size : 0;
  1046. total_host += device ? 0 : size;
  1047. 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));
  1048. }
  1049. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  1050. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  1051. return;
  1052. }
  1053. std::lock_guard<std::mutex> guard(log_mutex);
  1054. vk_buffer buf = buf_ref.lock();
  1055. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1056. std::string type = device ? "device" : "host";
  1057. auto it = allocations.find(buf->buffer);
  1058. total_device -= device ? it->second : 0;
  1059. total_host -= device ? 0 : it->second;
  1060. if (it != allocations.end()) {
  1061. 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));
  1062. allocations.erase(it);
  1063. } else {
  1064. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  1065. }
  1066. }
  1067. #endif // GGML_VULKAN_MEMORY_DEBUG
  1068. struct vk_instance_t {
  1069. vk::Instance instance;
  1070. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  1071. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  1072. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  1073. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  1074. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  1075. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  1076. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  1077. std::vector<size_t> device_indices;
  1078. vk_device devices[GGML_VK_MAX_DEVICES];
  1079. };
  1080. static bool vk_instance_initialized = false;
  1081. static vk_instance_t vk_instance;
  1082. static bool vk_perf_logger_enabled = false;
  1083. #ifdef GGML_VULKAN_CHECK_RESULTS
  1084. static size_t vk_skip_checks;
  1085. static size_t vk_output_tensor;
  1086. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  1087. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1088. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1089. #endif
  1090. 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);
  1091. static void ggml_backend_vk_free(ggml_backend_t backend);
  1092. // Wait for ctx->fence to be signaled.
  1093. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  1094. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  1095. // during this wait.
  1096. if (ctx->almost_ready_fence_pending) {
  1097. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  1098. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  1099. ctx->almost_ready_fence_pending = false;
  1100. }
  1101. // Spin (w/pause) waiting for the graph to finish executing.
  1102. vk::Result result;
  1103. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  1104. if (result != vk::Result::eNotReady) {
  1105. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  1106. exit(1);
  1107. }
  1108. for (uint32_t i = 0; i < 100; ++i) {
  1109. YIELD();
  1110. YIELD();
  1111. YIELD();
  1112. YIELD();
  1113. YIELD();
  1114. YIELD();
  1115. YIELD();
  1116. YIELD();
  1117. YIELD();
  1118. YIELD();
  1119. }
  1120. }
  1121. ctx->device->device.resetFences({ ctx->fence });
  1122. }
  1123. // variables to track number of compiles in progress
  1124. static uint32_t compile_count = 0;
  1125. static std::mutex compile_count_mutex;
  1126. static std::condition_variable compile_count_cond;
  1127. 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,
  1128. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  1129. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  1130. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  1131. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  1132. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  1133. GGML_ASSERT(parameter_count > 0);
  1134. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  1135. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  1136. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  1137. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  1138. vk::PushConstantRange pcr(
  1139. vk::ShaderStageFlagBits::eCompute,
  1140. 0,
  1141. pipeline->push_constant_size
  1142. );
  1143. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  1144. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  1145. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1146. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1147. specialization_entries[i].constantID = i;
  1148. specialization_entries[i].offset = i * sizeof(uint32_t);
  1149. specialization_entries[i].size = sizeof(uint32_t);
  1150. }
  1151. vk::SpecializationInfo specialization_info(
  1152. specialization_entries.size(),
  1153. specialization_entries.data(),
  1154. specialization_constants.size() * sizeof(uint32_t),
  1155. specialization_constants.data()
  1156. );
  1157. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1158. if (device->subgroup_require_full_support && require_full_subgroups) {
  1159. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1160. }
  1161. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1162. pipeline_shader_stage_create_flags,
  1163. vk::ShaderStageFlagBits::eCompute,
  1164. pipeline->shader_module,
  1165. entrypoint.c_str(),
  1166. &specialization_info);
  1167. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1168. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1169. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1170. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1171. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1172. }
  1173. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1174. vk::PipelineCreateFlags{},
  1175. pipeline_shader_create_info,
  1176. pipeline->layout);
  1177. vk::PipelineRobustnessCreateInfoEXT rci;
  1178. if (device->pipeline_robustness && disable_robustness) {
  1179. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1180. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1181. compute_pipeline_create_info.setPNext(&rci);
  1182. }
  1183. try {
  1184. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1185. } catch (const vk::SystemError& e) {
  1186. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1187. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1188. throw e;
  1189. }
  1190. pipeline->compiled = true;
  1191. if (vk_instance.debug_utils_support) {
  1192. vk::DebugUtilsObjectNameInfoEXT duoni;
  1193. duoni.objectType = vk::ObjectType::ePipeline;
  1194. duoni.pObjectName = pipeline->name.c_str();
  1195. duoni.objectHandle = /*reinterpret_cast*/(uint64_t)(static_cast<VkPipeline>(pipeline->pipeline));
  1196. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1197. }
  1198. {
  1199. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1200. device->pipelines.insert({ pipeline->name, pipeline });
  1201. }
  1202. {
  1203. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1204. assert(compile_count > 0);
  1205. compile_count--;
  1206. }
  1207. compile_count_cond.notify_all();
  1208. }
  1209. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1210. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1211. device.destroyPipelineLayout(pipeline->layout);
  1212. device.destroyShaderModule(pipeline->shader_module);
  1213. device.destroyPipeline(pipeline->pipeline);
  1214. }
  1215. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1216. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1217. ctx->pipeline_descriptor_set_requirements += n;
  1218. if (!pipeline->compiled) {
  1219. pipeline->needed = true;
  1220. ctx->device->need_compiles = true;
  1221. }
  1222. }
  1223. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1224. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1225. // Enough descriptors are available
  1226. return;
  1227. }
  1228. vk_device& device = ctx->device;
  1229. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1230. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1231. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1232. while (to_alloc > 0) {
  1233. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1234. to_alloc -= alloc_count;
  1235. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1236. if (pool_idx >= ctx->descriptor_pools.size()) {
  1237. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1238. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1239. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1240. }
  1241. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1242. for (uint32_t i = 0; i < alloc_count; i++) {
  1243. layouts[i] = device->dsl;
  1244. }
  1245. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1246. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1247. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1248. pool_idx++;
  1249. }
  1250. }
  1251. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1252. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1253. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1254. // Reuse command buffer
  1255. return p.cmd_buffers[p.cmd_buffer_idx++];
  1256. }
  1257. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1258. p.pool,
  1259. vk::CommandBufferLevel::ePrimary,
  1260. 1);
  1261. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1262. auto buf = cmd_buffers.front();
  1263. p.cmd_buffers.push_back(buf);
  1264. p.cmd_buffer_idx++;
  1265. return buf;
  1266. }
  1267. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1268. if (ctx->seqs.empty()) {
  1269. if (fence) {
  1270. std::lock_guard<std::mutex> guard(queue_mutex);
  1271. ctx->p->q->queue.submit({}, fence);
  1272. }
  1273. return;
  1274. }
  1275. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1276. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1277. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1278. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1279. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1280. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1281. std::vector<vk::SubmitInfo> submit_infos;
  1282. int idx = -1;
  1283. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1284. size_t reserve = 0;
  1285. for (const auto& sequence : ctx->seqs) {
  1286. reserve += sequence.size();
  1287. }
  1288. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1289. tl_wait_semaphores.reserve(reserve);
  1290. tl_wait_vals.reserve(reserve);
  1291. tl_signal_semaphores.reserve(reserve);
  1292. tl_signal_vals.reserve(reserve);
  1293. tl_submit_infos.reserve(reserve);
  1294. submit_infos.reserve(reserve);
  1295. stage_flags.reserve(reserve);
  1296. for (const auto& sequence : ctx->seqs) {
  1297. for (const auto& submission : sequence) {
  1298. stage_flags.push_back({});
  1299. idx++;
  1300. tl_wait_vals.push_back({});
  1301. tl_wait_semaphores.push_back({});
  1302. tl_signal_vals.push_back({});
  1303. tl_signal_semaphores.push_back({});
  1304. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1305. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1306. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1307. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1308. }
  1309. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1310. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1311. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1312. }
  1313. tl_submit_infos.push_back({
  1314. (uint32_t) submission.wait_semaphores.size(),
  1315. tl_wait_vals[idx].data(),
  1316. (uint32_t) submission.signal_semaphores.size(),
  1317. tl_signal_vals[idx].data(),
  1318. });
  1319. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1320. tl_submit_infos[idx].pNext = nullptr;
  1321. vk::SubmitInfo si{
  1322. (uint32_t) submission.wait_semaphores.size(),
  1323. tl_wait_semaphores[idx].data(),
  1324. stage_flags[idx].data(),
  1325. 1,
  1326. &submission.buffer,
  1327. (uint32_t) submission.signal_semaphores.size(),
  1328. tl_signal_semaphores[idx].data(),
  1329. };
  1330. si.setPNext(&tl_submit_infos[idx]);
  1331. submit_infos.push_back(si);
  1332. }
  1333. }
  1334. std::lock_guard<std::mutex> guard(queue_mutex);
  1335. ctx->p->q->queue.submit(submit_infos, fence);
  1336. ctx->seqs.clear();
  1337. }
  1338. 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) {
  1339. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1340. const uint32_t qfsize = queue_family_props.size();
  1341. // Try with avoid preferences first
  1342. for (uint32_t i = 0; i < qfsize; i++) {
  1343. 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)) {
  1344. return i;
  1345. }
  1346. }
  1347. // Fall back to only required
  1348. for (size_t i = 0; i < qfsize; i++) {
  1349. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1350. return i;
  1351. }
  1352. }
  1353. // Fall back to reusing compute queue
  1354. for (size_t i = 0; i < qfsize; i++) {
  1355. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1356. return i;
  1357. }
  1358. }
  1359. // Fall back to ignoring min_num_queries
  1360. for (size_t i = 0; i < qfsize; i++) {
  1361. if (queue_family_props[i].queueFlags & required) {
  1362. return i;
  1363. }
  1364. }
  1365. // 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.
  1366. // 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.
  1367. if (compute_index >= 0) {
  1368. return compute_index;
  1369. }
  1370. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1371. for(auto &q_family : queue_family_props) {
  1372. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1373. }
  1374. abort();
  1375. }
  1376. 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) {
  1377. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1378. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1379. q.queue_family_index = queue_family_index;
  1380. q.transfer_only = transfer_only;
  1381. q.cmd_pool.init(device, &q);
  1382. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1383. q.stage_flags = stage_flags;
  1384. }
  1385. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1386. vk_context result = std::make_shared<vk_context_struct>();
  1387. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1388. ctx->gc.contexts.emplace_back(result);
  1389. result->p = &p;
  1390. return result;
  1391. }
  1392. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1393. vk_context result = std::make_shared<vk_context_struct>();
  1394. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1395. result->p = &p;
  1396. return result;
  1397. }
  1398. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1399. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1400. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1401. vk::SemaphoreCreateInfo ci{};
  1402. ci.setPNext(&tci);
  1403. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1404. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1405. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1406. }
  1407. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1408. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1409. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1410. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1411. vk::SemaphoreCreateInfo ci{};
  1412. ci.setPNext(&tci);
  1413. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1414. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1415. }
  1416. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1417. }
  1418. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1419. if (ctx->event_idx >= ctx->gc.events.size()) {
  1420. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1421. }
  1422. return ctx->gc.events[ctx->event_idx++];
  1423. }
  1424. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1425. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1426. // Requires command buffers to be done
  1427. device->device.resetCommandPool(p.pool);
  1428. p.cmd_buffer_idx = 0;
  1429. }
  1430. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1431. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1432. // Arbitrary frequency to cleanup/reuse command buffers
  1433. static constexpr uint32_t cleanup_frequency = 10;
  1434. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1435. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1436. }
  1437. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1438. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1439. }
  1440. }
  1441. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1442. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1443. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1444. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1445. (flags & memory_type.propertyFlags) == flags &&
  1446. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1447. return static_cast<int32_t>(i);
  1448. }
  1449. }
  1450. return UINT32_MAX;
  1451. }
  1452. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1453. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1454. if (size > device->max_memory_allocation_size) {
  1455. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1456. }
  1457. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1458. if (size == 0) {
  1459. buf->size = 0;
  1460. return buf;
  1461. }
  1462. vk::BufferCreateInfo buffer_create_info{
  1463. vk::BufferCreateFlags(),
  1464. size,
  1465. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1466. vk::SharingMode::eExclusive,
  1467. 0,
  1468. nullptr,
  1469. };
  1470. buf->buffer = device->device.createBuffer(buffer_create_info);
  1471. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1472. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1473. uint32_t memory_type_index = UINT32_MAX;
  1474. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1475. buf->memory_property_flags = req_flags;
  1476. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1477. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1478. buf->memory_property_flags = fallback_flags;
  1479. }
  1480. if (memory_type_index == UINT32_MAX) {
  1481. device->device.destroyBuffer(buf->buffer);
  1482. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1483. }
  1484. try {
  1485. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1486. } catch (const vk::SystemError& e) {
  1487. if (buf->memory_property_flags != fallback_flags) {
  1488. // Try again with fallback flags
  1489. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1490. buf->memory_property_flags = fallback_flags;
  1491. try {
  1492. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1493. }
  1494. catch (const vk::SystemError& e) {
  1495. device->device.destroyBuffer(buf->buffer);
  1496. throw e;
  1497. }
  1498. } else {
  1499. // Out of Host/Device memory, clean up buffer
  1500. device->device.destroyBuffer(buf->buffer);
  1501. throw e;
  1502. }
  1503. }
  1504. buf->ptr = nullptr;
  1505. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1506. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1507. }
  1508. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1509. buf->device = device;
  1510. buf->size = size;
  1511. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1512. device->memory_logger->log_allocation(buf, size);
  1513. #endif
  1514. return buf;
  1515. }
  1516. 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)) {
  1517. try {
  1518. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1519. } catch (const vk::SystemError& e) {
  1520. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1521. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1522. throw e;
  1523. }
  1524. }
  1525. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1526. vk_buffer buf;
  1527. try {
  1528. if (device->prefer_host_memory) {
  1529. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1530. } else if (device->uma) {
  1531. // Fall back to host memory type
  1532. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1533. } else if (device->disable_host_visible_vidmem) {
  1534. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1535. } else {
  1536. // use rebar if available, otherwise fallback to device only visible memory
  1537. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1538. }
  1539. } catch (const vk::SystemError& e) {
  1540. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1541. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1542. throw e;
  1543. }
  1544. return buf;
  1545. }
  1546. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1547. if (buf == nullptr) {
  1548. return;
  1549. }
  1550. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1551. if (buf->device != nullptr) {
  1552. buf->device->memory_logger->log_deallocation(buf);
  1553. }
  1554. #endif
  1555. buf.reset();
  1556. }
  1557. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1558. return { buf, 0, VK_WHOLE_SIZE };
  1559. }
  1560. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1561. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1562. const bool transfer_queue = ctx->p->q->transfer_only;
  1563. ctx->s->buffer.pipelineBarrier(
  1564. ctx->p->q->stage_flags,
  1565. ctx->p->q->stage_flags,
  1566. {},
  1567. { {
  1568. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1569. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1570. } },
  1571. {},
  1572. {}
  1573. );
  1574. }
  1575. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1576. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1577. if (events.empty()) {
  1578. return;
  1579. }
  1580. ctx->s->buffer.waitEvents(
  1581. events,
  1582. ctx->p->q->stage_flags,
  1583. ctx->p->q->stage_flags,
  1584. {},
  1585. {},
  1586. {}
  1587. );
  1588. }
  1589. enum FaCodePath {
  1590. FA_SCALAR,
  1591. FA_COOPMAT1,
  1592. FA_COOPMAT2,
  1593. };
  1594. static FaHeadSizes fa_get_head_sizes(uint32_t hsk, uint32_t hsv) {
  1595. if (hsk != 192 && hsk != 576 && hsk != hsv) {
  1596. return FA_HEAD_SIZE_UNSUPPORTED;
  1597. }
  1598. switch (hsk) {
  1599. case 64: return FA_HEAD_SIZE_64;
  1600. case 80: return FA_HEAD_SIZE_80;
  1601. case 96: return FA_HEAD_SIZE_96;
  1602. case 112: return FA_HEAD_SIZE_112;
  1603. case 128: return FA_HEAD_SIZE_128;
  1604. case 192:
  1605. if (hsv == 192) {
  1606. return FA_HEAD_SIZE_192;
  1607. } else if (hsv == 128) {
  1608. return FA_HEAD_SIZE_192_128;
  1609. } else {
  1610. return FA_HEAD_SIZE_UNSUPPORTED;
  1611. }
  1612. case 256: return FA_HEAD_SIZE_256;
  1613. case 576:
  1614. if (hsv == 512) {
  1615. return FA_HEAD_SIZE_576_512;
  1616. } else {
  1617. return FA_HEAD_SIZE_UNSUPPORTED;
  1618. }
  1619. default: return FA_HEAD_SIZE_UNSUPPORTED;
  1620. }
  1621. }
  1622. // number of rows/cols for flash attention shader
  1623. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1624. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1625. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1626. if (hsv >= 512) {
  1627. return 2;
  1628. } else {
  1629. return 8;
  1630. }
  1631. }
  1632. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1633. // 128 threads split into four subgroups, each subgroup does 1/4
  1634. // of the Bc dimension.
  1635. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1636. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1637. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1638. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1639. if (path == FA_COOPMAT2) {
  1640. return flash_attention_num_small_rows;
  1641. } else {
  1642. return scalar_flash_attention_num_small_rows;
  1643. }
  1644. }
  1645. static std::array<uint32_t, 2> fa_rows_cols(FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) {
  1646. GGML_UNUSED(clamp);
  1647. GGML_UNUSED(hsv);
  1648. if (path == FA_SCALAR) {
  1649. if (small_rows) {
  1650. return {scalar_flash_attention_num_small_rows, 64};
  1651. } else {
  1652. return {get_fa_scalar_num_large_rows(hsv), 32};
  1653. }
  1654. }
  1655. if (path == FA_COOPMAT1) {
  1656. if (small_rows) {
  1657. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1658. } else {
  1659. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1660. }
  1661. }
  1662. // small rows, large cols
  1663. if (small_rows) {
  1664. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1665. }
  1666. // small cols to reduce register count
  1667. if (ggml_is_quantized(type) || hsk >= 256) {
  1668. if (hsk >= 512) {
  1669. return {32, 32};
  1670. } else {
  1671. return {64, 32};
  1672. }
  1673. }
  1674. return {64, 64};
  1675. }
  1676. 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) {
  1677. uint32_t lut_size = 0;
  1678. switch (src0_type) {
  1679. case GGML_TYPE_IQ1_S:
  1680. case GGML_TYPE_IQ1_M:
  1681. lut_size = 2*2048;
  1682. break;
  1683. case GGML_TYPE_IQ2_XXS:
  1684. lut_size = 8*256;
  1685. break;
  1686. case GGML_TYPE_IQ2_XS:
  1687. lut_size = 8*512;
  1688. break;
  1689. case GGML_TYPE_IQ2_S:
  1690. lut_size = 8*1024;
  1691. break;
  1692. case GGML_TYPE_IQ3_XXS:
  1693. lut_size = 4*256;
  1694. break;
  1695. case GGML_TYPE_IQ3_S:
  1696. lut_size = 4*512;
  1697. break;
  1698. case GGML_TYPE_IQ4_NL:
  1699. case GGML_TYPE_IQ4_XS:
  1700. case GGML_TYPE_MXFP4:
  1701. lut_size = 4*16;
  1702. break;
  1703. default:
  1704. break;
  1705. }
  1706. // Needs to be kept up to date on shader changes
  1707. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1708. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1709. const uint32_t warps = warptile[0] / warptile[10];
  1710. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1711. const uint32_t mmid_row_ids = mul_mat_id ? (4096 * sizeof(uint32_t) + 4/*_ne1*/) : 0;
  1712. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1713. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1714. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1715. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1716. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1717. return supported;
  1718. }
  1719. struct GpuPipelineConfig {
  1720. // GPU architecture identifier.
  1721. // Example: vk_device_architecture::AMD_GCN
  1722. vk_device_architecture arch;
  1723. // Mapping of pipeline names to their specific subgroup sizes.
  1724. // Example: {"soft_max_f32", 64}
  1725. std::unordered_map<std::string, uint32_t> pipelines;
  1726. // Default subgroup size for this GPU.
  1727. // Defaults to 0 if not explicitly provided.
  1728. uint32_t default_subgroup_size = 0;
  1729. };
  1730. // Pipeline configuration for RDNA1 GPUs.
  1731. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1732. {"soft_max", 64}, {"im2col", 64},
  1733. {"argmax", 64}, {"mul_mat_vec", 64},
  1734. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1735. };
  1736. // Pipeline configuration for RDNA2 GPUs.
  1737. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1738. {"soft_max", 64}, {"im2col", 64},
  1739. };
  1740. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1741. // Define configurations for different GPUs.
  1742. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1743. {
  1744. vk_device_architecture::AMD_RDNA1,
  1745. {
  1746. rdna1_pipelines,
  1747. },
  1748. RDNA_DEFAULT_SUBGROUP_SIZE
  1749. },
  1750. {
  1751. vk_device_architecture::AMD_RDNA2,
  1752. {
  1753. rdna2_pipelines,
  1754. },
  1755. RDNA_DEFAULT_SUBGROUP_SIZE
  1756. },
  1757. };
  1758. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1759. for (const auto &config : gpu_pipeline_configs) {
  1760. if (config.arch == arch) {
  1761. auto pipIt = config.pipelines.find(pipeline_name);
  1762. if (pipIt != config.pipelines.end()) {
  1763. return pipIt->second;
  1764. }
  1765. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1766. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1767. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1768. for (const auto &entry : sorted_pipelines) {
  1769. if (pipeline_name.find(entry.first) != std::string::npos) {
  1770. return entry.second;
  1771. }
  1772. }
  1773. return config.default_subgroup_size;
  1774. }
  1775. }
  1776. return 0; // If no matching configuration is found
  1777. }
  1778. static void ggml_vk_load_shaders(vk_device& device) {
  1779. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1780. // some shaders have a minimum subgroup size
  1781. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1782. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1783. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1784. // mulmat
  1785. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1786. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1787. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1788. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1789. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1790. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1791. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1792. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1793. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1794. uint32_t l_align, m_align, s_align;
  1795. if (device->coopmat2) {
  1796. // spec constants and tile sizes for non-quant matmul/matmul_id
  1797. l_warptile = { 256, 128, 256, 64, 1 };
  1798. m_warptile = { 256, 128, 128, 64, 0 };
  1799. s_warptile = { 128, 64, 64, 64, 0 };
  1800. l_wg_denoms = {128, 256, 1 };
  1801. m_wg_denoms = {128, 128, 1 };
  1802. s_wg_denoms = { 64, 64, 1 };
  1803. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1804. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1805. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1806. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1807. l_mmq_wg_denoms = { 128, 256, 1 };
  1808. m_mmq_wg_denoms = { 128, 128, 1 };
  1809. s_mmq_wg_denoms = { 32, 64, 1 };
  1810. // spec constants and tile sizes for quant matmul (Qi_K)
  1811. l_warptile_mmq_k = { 256, 128, 256, 64, 1 };
  1812. m_warptile_mmq_k = { 256, 128, 128, 64, 1 };
  1813. s_warptile_mmq_k = { 256, 32, 64, 128, 0 };
  1814. l_mmq_wg_denoms_k = { 128, 256, 1 };
  1815. m_mmq_wg_denoms_k = { 128, 128, 1 };
  1816. s_mmq_wg_denoms_k = { 32, 64, 1 };
  1817. // spec constants and tile sizes for quant matmul_id
  1818. l_warptile_mmqid = { 256, 128, 128, 16, 0 };
  1819. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1820. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1821. l_mmqid_wg_denoms = { 128, 128, 1 };
  1822. m_mmqid_wg_denoms = { 128, 64, 1 };
  1823. s_mmqid_wg_denoms = { 128, 64, 1 };
  1824. l_align = 128;
  1825. m_align = 64;
  1826. s_align = 32;
  1827. } else {
  1828. // Matrix cores require different warp group sizes
  1829. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1830. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1831. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1832. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1833. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1834. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1835. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1836. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1837. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1838. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1839. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1840. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1841. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1842. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1843. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1844. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1845. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1846. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1847. // chip specific tuning
  1848. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1849. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1850. }
  1851. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1852. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1853. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1854. l_align = 128;
  1855. m_align = 64;
  1856. s_align = 32;
  1857. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1858. ggml_type t = (ggml_type)i;
  1859. // Disable medium and large matrix multiplication if not enough shared memory is available
  1860. // Check mmq warptiles as the largest configuration
  1861. // Throw an error if not enough for any matrix multiplication is available
  1862. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1863. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1864. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1865. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1866. device->mul_mat_m[i] = false;
  1867. device->mul_mat_l[i] = false;
  1868. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1869. device->mul_mat_l[i] = false;
  1870. }
  1871. // Disable mul_mat_id if not enough shared memory is available
  1872. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1873. device->mul_mat_id_s[i] = false;
  1874. device->mul_mat_id_m[i] = false;
  1875. device->mul_mat_id_l[i] = false;
  1876. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1877. device->mul_mat_id_m[i] = false;
  1878. device->mul_mat_id_l[i] = false;
  1879. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1880. device->mul_mat_id_l[i] = false;
  1881. }
  1882. }
  1883. }
  1884. if (!device->pipeline_matmul_f32) {
  1885. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1886. }
  1887. if (!device->pipeline_matmul_f32_f16) {
  1888. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1889. }
  1890. if (!device->pipeline_matmul_id_f32) {
  1891. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1892. }
  1893. if (!device->pipeline_matmul_bf16) {
  1894. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1895. }
  1896. if (!device->pipeline_matmul_id_bf16) {
  1897. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1898. }
  1899. std::vector<std::future<void>> compiles;
  1900. 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,
  1901. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1902. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1903. if (!require_full_subgroups && required_subgroup_size == 0) {
  1904. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1905. }
  1906. if (!pipeline) {
  1907. pipeline = std::make_shared<vk_pipeline_struct>();
  1908. pipeline->name = name;
  1909. pipeline->parameter_count = parameter_count;
  1910. pipeline->push_constant_size = push_constant_size;
  1911. pipeline->wg_denoms = wg_denoms;
  1912. pipeline->align = align;
  1913. }
  1914. if (!pipeline->needed || pipeline->compiled) {
  1915. return;
  1916. }
  1917. {
  1918. // wait until fewer than N compiles are in progress
  1919. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1920. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1921. while (compile_count >= N) {
  1922. compile_count_cond.wait(guard);
  1923. }
  1924. compile_count++;
  1925. }
  1926. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1927. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1928. };
  1929. auto const &fa_wg_denoms = [&](FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  1930. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  1931. };
  1932. auto const &fa_spec_constants = [&](FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  1933. // For large number of rows, 128 invocations seems to work best.
  1934. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1935. // can't use 256 for D==80.
  1936. // For scalar, use 128 (arbitrary)
  1937. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  1938. const uint32_t D = (hsk|hsv);
  1939. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  1940. ? scalar_flash_attention_workgroup_size
  1941. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  1942. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  1943. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  1944. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  1945. const uint32_t D_lsb = D ^ (D & (D-1));
  1946. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  1947. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1948. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1949. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  1950. };
  1951. #define CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, HSK, HSV, HEAD_SIZES) \
  1952. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1953. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1954. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1955. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1956. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1957. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1958. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1959. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1960. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  1961. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 64, 64, 64) \
  1962. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 80, 80, 80) \
  1963. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 96, 96, 96) \
  1964. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 112, 112, 112) \
  1965. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 128, 128, 128) \
  1966. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 192, 192) \
  1967. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 128, 192_128) \
  1968. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 256, 256, 256) \
  1969. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 576, 512, 576_512)
  1970. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  1971. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  1972. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  1973. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1974. if (device->coopmat1_fa_support) {
  1975. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  1976. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  1977. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  1978. }
  1979. #endif
  1980. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1981. if (device->coopmat2) {
  1982. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  1983. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  1984. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  1985. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  1986. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  1987. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  1988. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  1989. }
  1990. #endif
  1991. #undef CREATE_FA2
  1992. #undef CREATE_FA
  1993. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1994. if (device->coopmat2) {
  1995. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1996. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1997. 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); \
  1998. 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); \
  1999. 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); \
  2000. 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); \
  2001. 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); \
  2002. 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); \
  2003. // Create 2 variants, {f16,f32} accumulator
  2004. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2005. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2006. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2007. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2008. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2009. if (device->coopmat_bf16_support) {
  2010. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2011. }
  2012. #endif
  2013. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0], matmul_q4_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2014. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1], matmul_q4_1_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2015. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0], matmul_q5_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2016. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1], matmul_q5_1_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2017. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0], matmul_q8_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2018. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K], matmul_q2_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2019. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K], matmul_q3_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2020. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K], matmul_q4_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2021. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K], matmul_q5_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2022. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K], matmul_q6_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  2023. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S], matmul_iq1_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2024. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M], matmul_iq1_m_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2025. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2026. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2027. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S], matmul_iq2_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2028. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2029. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S], matmul_iq3_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2030. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2031. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2032. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_MXFP4], matmul_mxfp4_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2033. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2034. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2035. if (device->coopmat_bf16_support) {
  2036. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2037. }
  2038. #endif
  2039. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_q4_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2040. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_q4_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2041. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_q5_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2042. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_q5_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2043. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_q8_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2044. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_q2_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2045. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_q3_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2046. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_q4_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2047. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_q5_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2048. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_q6_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2049. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_iq1_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2050. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_iq1_m_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2051. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_iq2_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2052. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_iq2_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2053. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_iq2_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2054. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_iq3_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2055. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_iq3_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2056. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_iq4_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2057. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_iq4_nl_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2058. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_mxfp4_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2059. #undef CREATE_MM
  2060. #undef CREATE_MM2
  2061. } else
  2062. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2063. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2064. if (device->coopmat_support) {
  2065. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2066. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2067. if (device->mul_mat ## ID ## _l[TYPE]) \
  2068. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  2069. if (device->mul_mat ## ID ## _m[TYPE]) \
  2070. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  2071. if (device->mul_mat ## ID ## _s[TYPE]) \
  2072. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  2073. if (device->mul_mat ## ID ## _l[TYPE]) \
  2074. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  2075. if (device->mul_mat ## ID ## _m[TYPE]) \
  2076. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  2077. if (device->mul_mat ## ID ## _s[TYPE]) \
  2078. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  2079. // Create 2 variants, {f16,f32} accumulator
  2080. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2081. if (device->coopmat_acc_f16_support) { \
  2082. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2083. } \
  2084. if (device->coopmat_acc_f32_support) { \
  2085. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2086. } \
  2087. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2088. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2089. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2090. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2091. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2092. if (device->coopmat_bf16_support) {
  2093. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  2094. }
  2095. #endif
  2096. if (device->coopmat_acc_f16_support) {
  2097. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2098. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2099. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2100. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2101. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2102. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2103. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2104. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2105. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2106. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2107. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2108. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2109. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2110. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2111. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2112. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2113. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2114. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2115. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2116. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2117. } else {
  2118. 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, );
  2119. 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, );
  2120. 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, );
  2121. 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, );
  2122. 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, );
  2123. 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, );
  2124. 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, );
  2125. 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, );
  2126. 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, );
  2127. 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, );
  2128. 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, );
  2129. 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, );
  2130. 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, );
  2131. 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, );
  2132. 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, );
  2133. 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, );
  2134. 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, );
  2135. 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, );
  2136. 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, );
  2137. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2138. }
  2139. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2140. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2141. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2142. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2143. if (device->coopmat_bf16_support) {
  2144. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2145. }
  2146. #endif
  2147. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2148. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2149. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2150. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2151. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2152. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2153. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2154. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2155. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2156. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2157. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2158. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2159. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2160. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2161. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2162. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2163. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2164. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2165. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2166. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2167. #undef CREATE_MM2
  2168. #undef CREATE_MM
  2169. } else
  2170. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2171. if (device->fp16) {
  2172. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2173. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2174. if (device->mul_mat ## ID ## _l[TYPE]) \
  2175. 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); \
  2176. if (device->mul_mat ## ID ## _m[TYPE]) \
  2177. 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); \
  2178. if (device->mul_mat ## ID ## _s[TYPE]) \
  2179. 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); \
  2180. if (device->mul_mat ## ID ## _l[TYPE]) \
  2181. 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); \
  2182. if (device->mul_mat ## ID ## _m[TYPE]) \
  2183. 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); \
  2184. if (device->mul_mat ## ID ## _s[TYPE]) \
  2185. 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); \
  2186. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2187. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2188. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->l, #NAMELC "_f16acc_l", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2189. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->l, #NAMELC "_l", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2190. } \
  2191. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2192. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->m, #NAMELC "_f16acc_m", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2193. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->m, #NAMELC "_m", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2194. } \
  2195. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2196. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->s, #NAMELC "_f16acc_s", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2197. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->s, #NAMELC "_s", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2198. } \
  2199. // Create 2 variants, {f16,f32} accumulator
  2200. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2201. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2202. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2203. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2204. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2205. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2206. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2207. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2208. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2209. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2210. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2211. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2212. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2213. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2214. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2215. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2216. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2217. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2218. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2219. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2220. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2221. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2222. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2223. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2224. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2225. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2226. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2227. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2228. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2229. if (device->integer_dot_product) {
  2230. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0], matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2231. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1], matmul_q4_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2232. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0], matmul_q5_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2233. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1], matmul_q5_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2234. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0], matmul_q8_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2235. }
  2236. #endif
  2237. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2238. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2239. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2240. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2241. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2242. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2243. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2244. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2245. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2246. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2247. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2248. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2249. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2250. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2251. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2252. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2253. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2254. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2255. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2256. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2257. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2258. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2259. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2260. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2261. #undef CREATE_MM2
  2262. #undef CREATE_MMQ
  2263. #undef CREATE_MM
  2264. } else {
  2265. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2266. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2267. if (device->mul_mat ## ID ## _l[TYPE]) \
  2268. 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); \
  2269. if (device->mul_mat ## ID ## _m[TYPE]) \
  2270. 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); \
  2271. if (device->mul_mat ## ID ## _s[TYPE]) \
  2272. 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); \
  2273. if (device->mul_mat ## ID ## _l[TYPE]) \
  2274. 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); \
  2275. if (device->mul_mat ## ID ## _m[TYPE]) \
  2276. 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); \
  2277. if (device->mul_mat ## ID ## _s[TYPE]) \
  2278. 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); \
  2279. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2280. if (device->mul_mat ## ID ## _l[TYPE]) \
  2281. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC "_l", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2282. if (device->mul_mat ## ID ## _m[TYPE]) \
  2283. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC "_m", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2284. if (device->mul_mat ## ID ## _s[TYPE]) \
  2285. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC "_s", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2286. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2287. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2288. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2289. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2290. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2291. 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, );
  2292. 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, );
  2293. 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, );
  2294. 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, );
  2295. 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, );
  2296. 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, );
  2297. 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, );
  2298. 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, );
  2299. 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, );
  2300. 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, );
  2301. 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, );
  2302. 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, );
  2303. 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, );
  2304. 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, );
  2305. 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, );
  2306. 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, );
  2307. 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, );
  2308. 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, );
  2309. 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, );
  2310. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2311. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2312. if (device->integer_dot_product) {
  2313. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2314. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2315. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2316. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2317. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2318. }
  2319. #endif
  2320. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2321. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2322. 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);
  2323. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2324. 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);
  2325. 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);
  2326. 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);
  2327. 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);
  2328. 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);
  2329. 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);
  2330. 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);
  2331. 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);
  2332. 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);
  2333. 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);
  2334. 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);
  2335. 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);
  2336. 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);
  2337. 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);
  2338. 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);
  2339. 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);
  2340. 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);
  2341. 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);
  2342. 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);
  2343. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2344. }
  2345. // reusing CREATE_MM from the fp32 path
  2346. if ((device->coopmat2 || device->coopmat_support)
  2347. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2348. && !device->coopmat_bf16_support
  2349. #endif
  2350. ) {
  2351. // use scalar tile sizes
  2352. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2353. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2354. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2355. l_wg_denoms = {128, 128, 1 };
  2356. m_wg_denoms = { 64, 64, 1 };
  2357. s_wg_denoms = { 32, 32, 1 };
  2358. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2359. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2360. }
  2361. #undef CREATE_MM
  2362. // mul mat vec
  2363. // the number of rows computed per shader depends on GPU model and quant
  2364. uint32_t rm_stdq = 1;
  2365. uint32_t rm_kq = 2;
  2366. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2367. if (device->architecture == AMD_GCN) {
  2368. rm_stdq = 2;
  2369. rm_kq = 4;
  2370. }
  2371. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2372. rm_stdq = 2;
  2373. uint32_t rm_iq = 2 * rm_kq;
  2374. for (uint32_t w = 0; w < DMMV_WG_SIZE_COUNT; ++w) {
  2375. uint32_t wg_size_subgroup16 = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size_16 : (subgroup_size_16 * 4);
  2376. uint32_t wg_size_subgroup = (w == DMMV_WG_SIZE_SUBGROUP) ? device->subgroup_size : (device->subgroup_size * 4);
  2377. const bool s = device->subgroup_add && device->architecture != vk_device_architecture::AMD_GCN;
  2378. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2379. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_f32_f32_f32_len[s], arr_dmmv_f32_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1);
  2380. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_f16_f32_f32_len[s], arr_dmmv_f16_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1);
  2381. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_bf16_f32_f32_len[s], arr_dmmv_bf16_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1);
  2382. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q4_0_f32_f32_len[s], arr_dmmv_q4_0_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2383. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q4_1_f32_f32_len[s], arr_dmmv_q4_1_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2384. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q5_0_f32_f32_len[s], arr_dmmv_q5_0_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2385. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q5_1_f32_f32_len[s], arr_dmmv_q5_1_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2386. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q8_0_f32_f32_len[s], arr_dmmv_q8_0_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true);
  2387. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q2_k_f32_f32_len[s], arr_dmmv_q2_k_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2388. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q3_k_f32_f32_len[s], arr_dmmv_q3_k_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2389. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q4_k_f32_f32_len[s], arr_dmmv_q4_k_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2390. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q5_k_f32_f32_len[s], arr_dmmv_q5_k_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2391. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q6_k_f32_f32_len[s], arr_dmmv_q6_k_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2392. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq1_s_f32_f32_len[s], arr_dmmv_iq1_s_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2393. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq1_m_f32_f32_len[s], arr_dmmv_iq1_m_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2394. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq2_xxs_f32_f32_len[s], arr_dmmv_iq2_xxs_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2395. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq2_xs_f32_f32_len[s], arr_dmmv_iq2_xs_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2396. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq2_s_f32_f32_len[s], arr_dmmv_iq2_s_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2397. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq3_xxs_f32_f32_len[s], arr_dmmv_iq3_xxs_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2398. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq3_s_f32_f32_len[s], arr_dmmv_iq3_s_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2399. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq4_xs_f32_f32_len[s], arr_dmmv_iq4_xs_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2400. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq4_nl_f32_f32_len[s], arr_dmmv_iq4_nl_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2401. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f32_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_mxfp4_f32_f32_len[s], arr_dmmv_mxfp4_f32_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2402. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_f32_f16_f32_len[s], arr_dmmv_f32_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1);
  2403. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_f16_f16_f32_len[s], arr_dmmv_f16_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1);
  2404. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_bf16_f16_f32_len[s], arr_dmmv_bf16_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1);
  2405. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q4_0_f16_f32_len[s], arr_dmmv_q4_0_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2406. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q4_1_f16_f32_len[s], arr_dmmv_q4_1_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2407. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q5_0_f16_f32_len[s], arr_dmmv_q5_0_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2408. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q5_1_f16_f32_len[s], arr_dmmv_q5_1_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true);
  2409. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q8_0_f16_f32_len[s], arr_dmmv_q8_0_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true);
  2410. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q2_k_f16_f32_len[s], arr_dmmv_q2_k_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2411. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q3_k_f16_f32_len[s], arr_dmmv_q3_k_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2412. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q4_k_f16_f32_len[s], arr_dmmv_q4_k_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2413. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q5_k_f16_f32_len[s], arr_dmmv_q5_k_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2414. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_q6_k_f16_f32_len[s], arr_dmmv_q6_k_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true);
  2415. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq1_s_f16_f32_len[s], arr_dmmv_iq1_s_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2416. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq1_m_f16_f32_len[s], arr_dmmv_iq1_m_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2417. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq2_xxs_f16_f32_len[s], arr_dmmv_iq2_xxs_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2418. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq2_xs_f16_f32_len[s], arr_dmmv_iq2_xs_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2419. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq2_s_f16_f32_len[s], arr_dmmv_iq2_s_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2420. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq3_xxs_f16_f32_len[s], arr_dmmv_iq3_xxs_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2421. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq3_s_f16_f32_len[s], arr_dmmv_iq3_s_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2422. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq4_xs_f16_f32_len[s], arr_dmmv_iq4_xs_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2423. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_iq4_nl_f16_f32_len[s], arr_dmmv_iq4_nl_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2424. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f16_f32_"+std::to_string(w)+"_"+std::to_string(i+1), arr_dmmv_mxfp4_f16_f32_len[s], arr_dmmv_mxfp4_f16_f32_data[s], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true);
  2425. }
  2426. }
  2427. 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);
  2428. 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);
  2429. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_BF16], "mul_mat_vec_id_bf16_f32", mul_mat_vec_id_bf16_f32_len, mul_mat_vec_id_bf16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2430. 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);
  2431. 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);
  2432. 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);
  2433. 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);
  2434. 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);
  2435. 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);
  2436. 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);
  2437. 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);
  2438. 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);
  2439. 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);
  2440. 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);
  2441. 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);
  2442. 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);
  2443. 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);
  2444. 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);
  2445. 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);
  2446. 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);
  2447. 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);
  2448. 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);
  2449. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_MXFP4], "mul_mat_vec_id_mxfp4_f32", mul_mat_vec_id_mxfp4_f32_len, mul_mat_vec_id_mxfp4_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2450. // dequant shaders
  2451. 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);
  2452. 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);
  2453. 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);
  2454. 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);
  2455. 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);
  2456. 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);
  2457. 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);
  2458. 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);
  2459. 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);
  2460. 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);
  2461. 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);
  2462. 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);
  2463. 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);
  2464. 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);
  2465. 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);
  2466. 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);
  2467. 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);
  2468. 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);
  2469. 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);
  2470. 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);
  2471. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_MXFP4], "dequant_mxfp4", dequant_mxfp4_len, dequant_mxfp4_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2472. // get_rows
  2473. 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);
  2474. 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);
  2475. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_BF16], "get_rows_bf16", get_rows_bf16_len, get_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2476. 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);
  2477. 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);
  2478. 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);
  2479. 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);
  2480. 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);
  2481. 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);
  2482. 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);
  2483. 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);
  2484. 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);
  2485. 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);
  2486. 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);
  2487. 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);
  2488. 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);
  2489. 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);
  2490. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_MXFP4], "get_rows_mxfp4", get_rows_mxfp4_len, get_rows_mxfp4_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2491. 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);
  2492. 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);
  2493. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_BF16], "get_rows_bf16_f32", get_rows_bf16_f32_len, get_rows_bf16_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2494. 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);
  2495. 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);
  2496. 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);
  2497. 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);
  2498. 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);
  2499. 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);
  2500. 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);
  2501. 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);
  2502. 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);
  2503. 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);
  2504. 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);
  2505. 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);
  2506. 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);
  2507. 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);
  2508. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_MXFP4], "get_rows_mxfp4_f32", get_rows_mxfp4_f32_len, get_rows_mxfp4_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2509. 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);
  2510. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_split_k_reduce, "fa_split_k_reduce", fa_split_k_reduce_len, fa_split_k_reduce_data, "main", 3, 5 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  2511. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_len, quantize_q8_1_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  2512. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2513. if (device->subgroup_add && device->subgroup_require_full_support) {
  2514. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  2515. } else {
  2516. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  2517. }
  2518. }
  2519. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_nc_f16_f32, "mul_mat_vec_nc_f16_f32", mul_mat_vec_nc_f16_f32_len, mul_mat_vec_nc_f16_f32_data, "main", 3, 12 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2520. 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);
  2521. 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);
  2522. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1);
  2523. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1);
  2524. 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);
  2525. 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);
  2526. 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);
  2527. 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);
  2528. 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);
  2529. ggml_vk_create_pipeline(device, device->pipeline_cpy_f16_f32, "cpy_f16_f32", cpy_f16_f32_len, cpy_f16_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2530. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_bf16,"cpy_f32_bf16",cpy_f32_bf16_len,cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2531. 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);
  2532. 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);
  2533. 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);
  2534. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f32, "contig_cpy_f16_f32", contig_cpy_f16_f32_len, contig_cpy_f16_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2535. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_bf16,"contig_cpy_f32_bf16",contig_cpy_f32_bf16_len,contig_cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2536. if (device->float_controls_rte_fp16) {
  2537. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2538. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2539. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_rte_len, cpy_f32_q5_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2540. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_rte_len, cpy_f32_q5_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2541. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_rte_len, cpy_f32_q8_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2542. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_rte_len, cpy_f32_iq4_nl_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2543. } else {
  2544. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_len, cpy_f32_q4_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2545. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_len, cpy_f32_q4_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2546. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_len, cpy_f32_q5_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2547. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_len, cpy_f32_q5_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2548. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_len, cpy_f32_q8_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2549. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_len, cpy_f32_iq4_nl_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2550. }
  2551. if (device->float_controls_rte_fp16) {
  2552. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_rte_len, set_rows_f32_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2553. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_rte_len, set_rows_f16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2554. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_rte_len, set_rows_bf16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2555. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_rte_len, set_rows_q4_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2556. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_rte_len, set_rows_q4_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2557. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_rte_len, set_rows_q5_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2558. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_rte_len, set_rows_q5_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2559. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_rte_len, set_rows_q8_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2560. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_rte_len, set_rows_iq4_nl_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2561. } else {
  2562. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_len, set_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2563. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_len, set_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2564. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_len, set_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2565. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_len, set_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2566. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_len, set_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2567. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_len, set_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2568. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_len, set_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2569. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_len, set_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2570. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_len, set_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2571. }
  2572. 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);
  2573. 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);
  2574. 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);
  2575. 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);
  2576. 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);
  2577. 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);
  2578. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2579. std::string s;
  2580. s += std::string(src0_f16 ? "_f16" : "_f32");
  2581. s += std::string(src1_f16 ? "_f16" : "_f32");
  2582. s += std::string(dst_f16 ? "_f16" : "_f32");
  2583. return s;
  2584. };
  2585. bool rte = device->float_controls_rte_fp16;
  2586. #define CREATE_BINARY(name, namemod, spec) \
  2587. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2588. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2589. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d][rte], name ## _data[s0][s1][d][rte], \
  2590. "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2591. CREATE_BINARY(add, , {0})
  2592. CREATE_BINARY(add, _norepeat, {1})
  2593. CREATE_BINARY(sub, , {0})
  2594. CREATE_BINARY(sub, _norepeat, {1})
  2595. CREATE_BINARY(mul, , {0})
  2596. CREATE_BINARY(mul, _norepeat, {1})
  2597. CREATE_BINARY(div, , {0})
  2598. CREATE_BINARY(div, _norepeat, {1})
  2599. #undef CREATE_BINARY
  2600. if (device->multi_add) {
  2601. for (uint32_t i = 0; i < MAX_FUSED_ADDS; ++i) {
  2602. ggml_vk_create_pipeline(device, device->pipeline_multi_add[i], "multi_add_f32_" + std::to_string(i+1), multi_add_f32_len, multi_add_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  2603. }
  2604. }
  2605. ggml_vk_create_pipeline(device, device->pipeline_add_id_f32, "add_id_f32", add_id_f32_len, add_id_f32_data, "main", 4, sizeof(vk_op_add_id_push_constants), {1, 1, 1}, {}, 1);
  2606. 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);
  2607. 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);
  2608. 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);
  2609. 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);
  2610. ggml_vk_create_pipeline(device, device->pipeline_upscale_nearest_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_NEAREST}, 1);
  2611. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR}, 1);
  2612. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_ac_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS}, 1);
  2613. 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);
  2614. 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);
  2615. ggml_vk_create_pipeline(device, device->pipeline_sqrt_f32, "sqrt_f32", sqrt_f32_len, sqrt_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2616. 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);
  2617. 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);
  2618. 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);
  2619. 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);
  2620. ggml_vk_create_pipeline(device, device->pipeline_roll_f32, "roll_f32", roll_f32_len, roll_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2621. 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);
  2622. 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);
  2623. #define CREATE_UNARY(name) \
  2624. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32", name ## _f32_len, name ## _f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  2625. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16", name ## _f16_len, name ## _f16_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2626. CREATE_UNARY(gelu)
  2627. CREATE_UNARY(gelu_erf)
  2628. CREATE_UNARY(gelu_quick)
  2629. CREATE_UNARY(silu)
  2630. CREATE_UNARY(relu)
  2631. CREATE_UNARY(tanh)
  2632. CREATE_UNARY(sigmoid)
  2633. #undef CREATE_UNARY
  2634. #define CREATE_GLU(name) \
  2635. if (device->float_controls_rte_fp16) { \
  2636. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32_rte", name ## _f32_rte_len, name ## _f32_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2637. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16_rte", name ## _f16_rte_len, name ## _f16_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2638. } else { \
  2639. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32", name ## _f32_len, name ## _f32_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2640. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16", name ## _f16_len, name ## _f16_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2641. }
  2642. CREATE_GLU(geglu)
  2643. CREATE_GLU(reglu)
  2644. CREATE_GLU(swiglu)
  2645. CREATE_GLU(swiglu_oai)
  2646. CREATE_GLU(geglu_erf)
  2647. CREATE_GLU(geglu_quick)
  2648. #undef CREATE_GLU
  2649. 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);
  2650. 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);
  2651. 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);
  2652. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2653. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2654. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2655. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2656. 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);
  2657. 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);
  2658. 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);
  2659. 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);
  2660. 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);
  2661. if (device->float_controls_rte_fp16) {
  2662. 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);
  2663. 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);
  2664. 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);
  2665. 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);
  2666. } else {
  2667. 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);
  2668. 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);
  2669. 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);
  2670. 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);
  2671. }
  2672. for (uint32_t i = 0; i < num_argsort_pipelines; ++i) {
  2673. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32[i], "argsort_f32_"+std::to_string(i), argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1u<<i, 1, 1}, {1u<<i, i}, 1, true);
  2674. }
  2675. 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);
  2676. 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);
  2677. 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);
  2678. 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);
  2679. if (device->float_controls_rte_fp16) {
  2680. 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);
  2681. } else {
  2682. 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);
  2683. }
  2684. 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);
  2685. ggml_vk_create_pipeline(device, device->pipeline_conv_transpose_1d_f32, "conv_transpose_1d_f32", conv_transpose_1d_f32_len, conv_transpose_1d_f32_data, "main", 3, sizeof(vk_op_conv_transpose_1d_push_constants), {1, 1, 1}, {}, 1);
  2686. 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);
  2687. 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);
  2688. 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);
  2689. 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);
  2690. ggml_vk_create_pipeline(device, device->pipeline_opt_step_sgd_f32, "opt_step_sgd_f32", opt_step_sgd_f32_len, opt_step_sgd_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2691. // conv2d
  2692. for (uint32_t s = 0; s < CONV_SHAPE_COUNT; ++s) {
  2693. uint32_t conv2d_WG_SIZE = 256;
  2694. uint32_t conv2d_BS_K = 128;
  2695. uint32_t conv2d_BS_CRS = 16;
  2696. uint32_t use_collectives = 0; // Enables subgroup ops for preventing the re-calculation of indices.
  2697. uint32_t conv2d_BS_NPQ = 128;
  2698. uint32_t conv2d_TS_K = 8;
  2699. uint32_t conv2d_SHMEM_PAD = 4;
  2700. bool conv2d_UNROLL = true;
  2701. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2702. if (device->coopmat2) {
  2703. conv2d_SHMEM_PAD = 8; // 8 float16_t
  2704. }
  2705. #endif
  2706. if (device->vendor_id == VK_VENDOR_ID_INTEL) {
  2707. conv2d_SHMEM_PAD = 0;
  2708. conv2d_UNROLL = false;
  2709. } else if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2710. conv2d_SHMEM_PAD = device->architecture == vk_device_architecture::AMD_GCN ? 1 : 4;
  2711. }
  2712. switch (s) {
  2713. default:
  2714. case CONV_SHAPE_128x128:
  2715. conv2d_BS_K = 128;
  2716. conv2d_BS_NPQ = 128;
  2717. conv2d_BS_CRS = 16;
  2718. if (device->vendor_id == VK_VENDOR_ID_AMD && device->architecture != vk_device_architecture::AMD_GCN) {
  2719. conv2d_UNROLL = false;
  2720. }
  2721. break;
  2722. case CONV_SHAPE_64x32:
  2723. conv2d_BS_K = 64;
  2724. conv2d_BS_NPQ = 32;
  2725. conv2d_BS_CRS = 32;
  2726. conv2d_TS_K = 4;
  2727. break;
  2728. case CONV_SHAPE_32x256:
  2729. conv2d_BS_K = 32;
  2730. conv2d_BS_NPQ = 256;
  2731. conv2d_BS_CRS = 16;
  2732. break;
  2733. }
  2734. // Use collectives on pre-Turing NVIDIA GPUs and GCN AMD cards, which had slower integer math.
  2735. bool allow_collectives_nv = device->vendor_id != VK_VENDOR_ID_NVIDIA ||
  2736. device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  2737. bool allow_collectives_amd = device->vendor_id != VK_VENDOR_ID_AMD ||
  2738. device->architecture == vk_device_architecture::AMD_GCN;
  2739. if (device->subgroup_shuffle &&
  2740. device->vendor_id != VK_VENDOR_ID_INTEL && // Do not enable collectives on Intel, see PR 14316.
  2741. allow_collectives_nv &&
  2742. allow_collectives_amd) {
  2743. use_collectives = 1;
  2744. conv2d_BS_CRS = std::min(
  2745. device->subgroup_size,
  2746. conv2d_BS_CRS); // CRS block size should be capped at subgroup size for correctness when shuffle is used.
  2747. }
  2748. uint32_t conv2d_shmem_req =
  2749. (conv2d_BS_K * (conv2d_BS_CRS + conv2d_SHMEM_PAD) + conv2d_BS_CRS * (conv2d_BS_NPQ + conv2d_SHMEM_PAD)) * sizeof(float);
  2750. if (device->properties.limits.maxComputeSharedMemorySize < conv2d_shmem_req) {
  2751. conv2d_BS_CRS = 8;
  2752. if (use_collectives) {
  2753. conv2d_BS_CRS = std::min(device->subgroup_size, conv2d_BS_CRS);
  2754. }
  2755. }
  2756. std::array<uint32_t, 3> wg_denoms = { conv2d_BS_K, conv2d_BS_NPQ, 1 };
  2757. std::vector<uint32_t> spec_constants = { conv2d_WG_SIZE, conv2d_BS_K, conv2d_BS_CRS, conv2d_BS_NPQ, conv2d_TS_K, use_collectives, conv2d_SHMEM_PAD };
  2758. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2759. if (device->coopmat2) {
  2760. ggml_vk_create_pipeline(
  2761. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_cm2_len, conv2d_f32_cm2_data, "main", 3,
  2762. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2763. ggml_vk_create_pipeline(
  2764. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_cm2_len, conv2d_f16_f32_cm2_data, "main", 3,
  2765. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2766. } else
  2767. #endif
  2768. if (conv2d_UNROLL) {
  2769. ggml_vk_create_pipeline(
  2770. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_unroll_len, conv2d_f32_unroll_data, "main", 3,
  2771. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2772. ggml_vk_create_pipeline(
  2773. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_unroll_len, conv2d_f16_f32_unroll_data, "main", 3,
  2774. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2775. } else {
  2776. ggml_vk_create_pipeline(
  2777. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_len, conv2d_f32_data, "main", 3,
  2778. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2779. ggml_vk_create_pipeline(
  2780. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_len, conv2d_f16_f32_data, "main", 3,
  2781. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2782. }
  2783. }
  2784. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f32, "conv2d_dw_whcn_f32", conv2d_dw_whcn_f32_len, conv2d_dw_whcn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2785. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f32, "conv2d_dw_cwhn_f32", conv2d_dw_cwhn_f32_len, conv2d_dw_cwhn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2786. for (auto &c : compiles) {
  2787. c.wait();
  2788. }
  2789. device->need_compiles = false;
  2790. }
  2791. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2792. static vk_device ggml_vk_get_device(size_t idx) {
  2793. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2794. if (vk_instance.devices[idx] == nullptr) {
  2795. VK_LOG_DEBUG("Initializing new vk_device");
  2796. vk_device device = std::make_shared<vk_device_struct>();
  2797. vk_instance.devices[idx] = device;
  2798. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2799. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2800. #endif
  2801. if (vk_perf_logger_enabled) {
  2802. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2803. }
  2804. size_t dev_num = vk_instance.device_indices[idx];
  2805. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2806. if (dev_num >= physical_devices.size()) {
  2807. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2808. throw std::runtime_error("Device not found");
  2809. }
  2810. device->physical_device = physical_devices[dev_num];
  2811. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2812. device->architecture = get_device_architecture(device->physical_device);
  2813. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2814. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2815. const char* GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM = getenv("GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM");
  2816. device->disable_host_visible_vidmem = GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM != nullptr;
  2817. bool fp16_storage = false;
  2818. bool fp16_compute = false;
  2819. bool maintenance4_support = false;
  2820. bool sm_builtins = false;
  2821. bool amd_shader_core_properties2 = false;
  2822. bool pipeline_robustness = false;
  2823. bool coopmat2_support = false;
  2824. device->coopmat_support = false;
  2825. device->integer_dot_product = false;
  2826. bool bfloat16_support = false;
  2827. for (const auto& properties : ext_props) {
  2828. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2829. maintenance4_support = true;
  2830. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2831. fp16_storage = true;
  2832. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2833. fp16_compute = true;
  2834. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2835. sm_builtins = true;
  2836. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2837. amd_shader_core_properties2 = true;
  2838. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2839. pipeline_robustness = true;
  2840. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2841. device->subgroup_size_control = true;
  2842. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2843. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2844. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2845. device->coopmat_support = true;
  2846. device->coopmat_m = 0;
  2847. device->coopmat_n = 0;
  2848. device->coopmat_k = 0;
  2849. #endif
  2850. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2851. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2852. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2853. coopmat2_support = true;
  2854. #endif
  2855. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2856. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2857. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2858. device->integer_dot_product = true;
  2859. #endif
  2860. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2861. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  2862. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2863. bfloat16_support = true;
  2864. #endif
  2865. }
  2866. }
  2867. vk::PhysicalDeviceProperties2 props2;
  2868. vk::PhysicalDeviceMaintenance3Properties props3;
  2869. vk::PhysicalDeviceMaintenance4Properties props4;
  2870. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2871. vk::PhysicalDeviceDriverProperties driver_props;
  2872. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2873. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2874. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2875. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2876. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2877. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2878. props2.pNext = &props3;
  2879. props3.pNext = &subgroup_props;
  2880. subgroup_props.pNext = &driver_props;
  2881. driver_props.pNext = &vk11_props;
  2882. vk11_props.pNext = &vk12_props;
  2883. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2884. if (maintenance4_support) {
  2885. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2886. last_struct = (VkBaseOutStructure *)&props4;
  2887. }
  2888. if (sm_builtins) {
  2889. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2890. last_struct = (VkBaseOutStructure *)&sm_props;
  2891. }
  2892. if (amd_shader_core_properties2) {
  2893. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2894. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2895. }
  2896. if (device->subgroup_size_control) {
  2897. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2898. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2899. }
  2900. #if defined(VK_NV_cooperative_matrix2)
  2901. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2902. if (coopmat2_support) {
  2903. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2904. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2905. }
  2906. #endif
  2907. if (device->integer_dot_product) {
  2908. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2909. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2910. }
  2911. device->physical_device.getProperties2(&props2);
  2912. device->properties = props2.properties;
  2913. device->vendor_id = device->properties.vendorID;
  2914. device->driver_id = driver_props.driverID;
  2915. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2916. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2917. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2918. } else if (maintenance4_support) {
  2919. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2920. } else {
  2921. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2922. }
  2923. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2924. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2925. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2926. } else {
  2927. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2928. device->suballocation_block_size = 1024*1024*1024;
  2929. }
  2930. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2931. device->subgroup_size = subgroup_props.subgroupSize;
  2932. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2933. if (sm_builtins) {
  2934. device->shader_core_count = sm_props.shaderSMCount;
  2935. } else if (amd_shader_core_properties2) {
  2936. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2937. } else {
  2938. device->shader_core_count = 0;
  2939. }
  2940. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2941. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2942. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2943. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2944. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  2945. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2946. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2947. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2948. device->coopmat_support = false;
  2949. }
  2950. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2951. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2952. // Try to find a non-graphics compute queue and transfer-focused queues
  2953. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2954. 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);
  2955. const float priorities[] = { 1.0f, 1.0f };
  2956. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2957. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2958. if (compute_queue_family_index != transfer_queue_family_index) {
  2959. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2960. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2961. } else if(!device->single_queue) {
  2962. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2963. } else {
  2964. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2965. }
  2966. vk::DeviceCreateInfo device_create_info;
  2967. std::vector<const char *> device_extensions;
  2968. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2969. VkPhysicalDeviceFeatures2 device_features2;
  2970. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2971. device_features2.pNext = nullptr;
  2972. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2973. VkPhysicalDeviceVulkan11Features vk11_features;
  2974. vk11_features.pNext = nullptr;
  2975. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2976. device_features2.pNext = &vk11_features;
  2977. VkPhysicalDeviceVulkan12Features vk12_features;
  2978. vk12_features.pNext = nullptr;
  2979. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2980. vk11_features.pNext = &vk12_features;
  2981. last_struct = (VkBaseOutStructure *)&vk12_features;
  2982. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2983. pl_robustness_features.pNext = nullptr;
  2984. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2985. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2986. if (pipeline_robustness) {
  2987. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2988. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2989. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2990. }
  2991. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2992. subgroup_size_control_features.pNext = nullptr;
  2993. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2994. subgroup_size_control_features.computeFullSubgroups = false;
  2995. subgroup_size_control_features.subgroupSizeControl = false;
  2996. if (device->subgroup_size_control) {
  2997. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2998. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2999. }
  3000. #if defined(VK_KHR_cooperative_matrix)
  3001. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3002. coopmat_features.pNext = nullptr;
  3003. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3004. coopmat_features.cooperativeMatrix = VK_FALSE;
  3005. if (device->coopmat_support) {
  3006. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3007. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3008. }
  3009. #endif
  3010. #if defined(VK_NV_cooperative_matrix2)
  3011. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  3012. coopmat2_features.pNext = nullptr;
  3013. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  3014. if (coopmat2_support) {
  3015. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  3016. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  3017. device_extensions.push_back("VK_NV_cooperative_matrix2");
  3018. }
  3019. #endif
  3020. #if defined(VK_KHR_shader_bfloat16)
  3021. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3022. bfloat16_features.pNext = nullptr;
  3023. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3024. if (bfloat16_support) {
  3025. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3026. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3027. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3028. }
  3029. #endif
  3030. VkPhysicalDeviceMaintenance4Features maint4_features {};
  3031. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  3032. if (maintenance4_support) {
  3033. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  3034. last_struct = (VkBaseOutStructure *)&maint4_features;
  3035. device_extensions.push_back("VK_KHR_maintenance4");
  3036. }
  3037. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3038. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3039. if (device->integer_dot_product) {
  3040. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3041. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3042. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  3043. }
  3044. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  3045. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  3046. #if defined(VK_KHR_shader_bfloat16)
  3047. device->bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3048. #else
  3049. device->bf16 = false;
  3050. #endif
  3051. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  3052. device->multi_add = vk12_props.shaderRoundingModeRTEFloat16 &&
  3053. device->properties.limits.maxPushConstantsSize >= sizeof(vk_op_multi_add_push_constants) &&
  3054. vk12_features.runtimeDescriptorArray &&
  3055. device->vendor_id != VK_VENDOR_ID_INTEL &&
  3056. getenv("GGML_VK_DISABLE_MULTI_ADD") == nullptr;
  3057. if (device->subgroup_size_control) {
  3058. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  3059. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  3060. device_extensions.push_back("VK_EXT_subgroup_size_control");
  3061. }
  3062. device->subgroup_size_control = device->subgroup_size_control &&
  3063. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  3064. subgroup_size_control_features.subgroupSizeControl;
  3065. if (device->subgroup_size_control) {
  3066. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  3067. }
  3068. #if defined(VK_KHR_cooperative_matrix)
  3069. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  3070. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  3071. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  3072. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  3073. device->subgroup_max_size >= 32;
  3074. #endif
  3075. if (coopmat2_support) {
  3076. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3077. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  3078. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  3079. coopmat2_features.cooperativeMatrixReductions &&
  3080. coopmat2_features.cooperativeMatrixConversions &&
  3081. coopmat2_features.cooperativeMatrixPerElementOperations &&
  3082. coopmat2_features.cooperativeMatrixTensorAddressing &&
  3083. coopmat2_features.cooperativeMatrixBlockLoads &&
  3084. vk12_features.bufferDeviceAddress) {
  3085. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  3086. uint32_t count = 0;
  3087. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  3088. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  3089. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  3090. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  3091. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  3092. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  3093. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  3094. flexible_dimensions.resize(count, empty_prop);
  3095. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  3096. bool found_fp16_128 = false,
  3097. found_fp16_256 = false,
  3098. found_fp32_128 = false,
  3099. found_fp32_256 = false;
  3100. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  3101. // with 32x16x16 and 256 with 32x32x16.
  3102. for (auto &prop : flexible_dimensions) {
  3103. if (prop.saturatingAccumulation == VK_FALSE &&
  3104. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  3105. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3106. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3107. if (prop.workgroupInvocations == 128 &&
  3108. prop.MGranularity <= 32 &&
  3109. prop.NGranularity <= 16 &&
  3110. prop.KGranularity <= 16) {
  3111. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3112. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3113. found_fp16_128 = true;
  3114. }
  3115. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3116. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3117. found_fp32_128 = true;
  3118. }
  3119. }
  3120. if (prop.workgroupInvocations == 256 &&
  3121. prop.MGranularity <= 32 &&
  3122. prop.NGranularity <= 32 &&
  3123. prop.KGranularity <= 16) {
  3124. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3125. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3126. found_fp16_256 = true;
  3127. }
  3128. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3129. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3130. found_fp32_256 = true;
  3131. }
  3132. }
  3133. }
  3134. }
  3135. if (found_fp16_128 && found_fp16_256 &&
  3136. found_fp32_128 && found_fp32_256 &&
  3137. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  3138. device->coopmat2 = true;
  3139. }
  3140. }
  3141. #endif
  3142. }
  3143. if (!vk11_features.storageBuffer16BitAccess) {
  3144. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  3145. throw std::runtime_error("Unsupported device");
  3146. }
  3147. device_extensions.push_back("VK_KHR_16bit_storage");
  3148. #ifdef GGML_VULKAN_VALIDATE
  3149. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  3150. #endif
  3151. if (device->fp16) {
  3152. device_extensions.push_back("VK_KHR_shader_float16_int8");
  3153. }
  3154. #if defined(VK_KHR_cooperative_matrix)
  3155. if (device->coopmat_support) {
  3156. // Query supported shapes
  3157. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  3158. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  3159. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  3160. uint32_t cm_props_num;
  3161. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  3162. cm_props.resize(cm_props_num);
  3163. for (auto& prop : cm_props) {
  3164. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  3165. }
  3166. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  3167. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  3168. for (auto& prop : cm_props) {
  3169. 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));
  3170. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  3171. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  3172. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3173. ) {
  3174. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  3175. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  3176. // coopmat sizes not set yet
  3177. if (device->coopmat_m == 0) {
  3178. device->coopmat_acc_f32_support = true;
  3179. device->coopmat_m = prop.MSize;
  3180. device->coopmat_n = prop.NSize;
  3181. device->coopmat_k = prop.KSize;
  3182. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3183. // Only enable if shape is identical
  3184. device->coopmat_acc_f32_support = true;
  3185. }
  3186. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3187. device->coopmat_support_16x16x16_f32acc = true;
  3188. }
  3189. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  3190. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  3191. // coopmat sizes not set yet
  3192. if (device->coopmat_m == 0) {
  3193. device->coopmat_acc_f16_support = true;
  3194. device->coopmat_m = prop.MSize;
  3195. device->coopmat_n = prop.NSize;
  3196. device->coopmat_k = prop.KSize;
  3197. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3198. // Only enable if shape is identical
  3199. device->coopmat_acc_f16_support = true;
  3200. }
  3201. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3202. device->coopmat_support_16x16x16_f16acc = true;
  3203. }
  3204. }
  3205. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  3206. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  3207. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  3208. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  3209. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  3210. device->coopmat_int_m == 0
  3211. ) {
  3212. device->coopmat_int_support = true;
  3213. device->coopmat_int_m = prop.MSize;
  3214. device->coopmat_int_n = prop.NSize;
  3215. device->coopmat_int_k = prop.KSize;
  3216. }
  3217. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3218. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3219. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3220. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3221. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3222. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3223. ) {
  3224. // coopmat sizes not set yet
  3225. if (device->coopmat_m == 0) {
  3226. device->coopmat_bf16_support = true;
  3227. device->coopmat_m = prop.MSize;
  3228. device->coopmat_n = prop.NSize;
  3229. device->coopmat_k = prop.KSize;
  3230. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3231. // Only enable if shape is identical
  3232. device->coopmat_bf16_support = true;
  3233. }
  3234. }
  3235. #endif
  3236. }
  3237. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  3238. // No suitable matmul mode found
  3239. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  3240. device->coopmat_support = false;
  3241. }
  3242. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3243. device->coopmat_bf16_support = false;
  3244. }
  3245. }
  3246. if (device->coopmat_support) {
  3247. device_extensions.push_back("VK_KHR_cooperative_matrix");
  3248. }
  3249. #if defined(VK_KHR_shader_bfloat16)
  3250. if (device->coopmat_bf16_support) {
  3251. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3252. }
  3253. #endif
  3254. #endif
  3255. device->name = GGML_VK_NAME + std::to_string(idx);
  3256. device_create_info = {
  3257. vk::DeviceCreateFlags(),
  3258. device_queue_create_infos,
  3259. {},
  3260. device_extensions
  3261. };
  3262. device_create_info.setPNext(&device_features2);
  3263. device->device = device->physical_device.createDevice(device_create_info);
  3264. // Queues
  3265. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  3266. // Shaders
  3267. // Disable matmul tile sizes early if performance low or not supported
  3268. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3269. switch (device->vendor_id) {
  3270. #ifndef GGML_VULKAN_RUN_TESTS
  3271. case VK_VENDOR_ID_AMD:
  3272. case VK_VENDOR_ID_INTEL:
  3273. device->mul_mat_l[i] = false;
  3274. device->mul_mat_m[i] = true;
  3275. device->mul_mat_s[i] = true;
  3276. device->mul_mat_id_l[i] = false;
  3277. device->mul_mat_id_m[i] = true;
  3278. device->mul_mat_id_s[i] = true;
  3279. break;
  3280. case VK_VENDOR_ID_APPLE:
  3281. device->mul_mat_l[i] = false;
  3282. device->mul_mat_m[i] = true;
  3283. device->mul_mat_s[i] = false;
  3284. device->mul_mat_id_l[i] = false;
  3285. device->mul_mat_id_m[i] = true;
  3286. device->mul_mat_id_s[i] = false;
  3287. break;
  3288. #endif
  3289. default:
  3290. device->mul_mat_l[i] = true;
  3291. device->mul_mat_m[i] = true;
  3292. device->mul_mat_s[i] = true;
  3293. device->mul_mat_id_l[i] = true;
  3294. device->mul_mat_id_m[i] = true;
  3295. device->mul_mat_id_s[i] = true;
  3296. break;
  3297. }
  3298. }
  3299. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3300. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3301. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3302. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3303. dsl_binding_flags.push_back({});
  3304. }
  3305. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3306. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3307. {},
  3308. dsl_binding);
  3309. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3310. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3311. ggml_vk_load_shaders(device);
  3312. if (!device->single_queue) {
  3313. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3314. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3315. } else {
  3316. // TODO: Use pointer or reference to avoid copy
  3317. device->transfer_queue.copyFrom(device->compute_queue);
  3318. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3319. }
  3320. device->buffer_type = {
  3321. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3322. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3323. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3324. };
  3325. device->fence = device->device.createFence({});
  3326. device->idx = idx;
  3327. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3328. return device;
  3329. }
  3330. return vk_instance.devices[idx];
  3331. }
  3332. static void ggml_vk_print_gpu_info(size_t idx) {
  3333. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3334. size_t dev_num = vk_instance.device_indices[idx];
  3335. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3336. GGML_ASSERT(vk_instance_initialized);
  3337. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3338. if (dev_num >= devices.size()) {
  3339. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3340. throw std::runtime_error("Device not found");
  3341. }
  3342. vk::PhysicalDevice physical_device = devices[dev_num];
  3343. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3344. bool fp16_storage = false;
  3345. bool fp16_compute = false;
  3346. bool coopmat_support = false;
  3347. bool coopmat2_support = false;
  3348. bool integer_dot_product = false;
  3349. bool bfloat16_support = false;
  3350. for (auto properties : ext_props) {
  3351. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3352. fp16_storage = true;
  3353. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3354. fp16_compute = true;
  3355. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3356. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3357. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3358. coopmat_support = true;
  3359. #endif
  3360. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3361. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3362. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3363. coopmat2_support = true;
  3364. #endif
  3365. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3366. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3367. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3368. integer_dot_product = true;
  3369. #endif
  3370. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3371. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3372. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3373. bfloat16_support = true;
  3374. #endif
  3375. }
  3376. }
  3377. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3378. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3379. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3380. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3381. vk::PhysicalDeviceProperties2 props2;
  3382. vk::PhysicalDeviceMaintenance3Properties props3;
  3383. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3384. vk::PhysicalDeviceDriverProperties driver_props;
  3385. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3386. props2.pNext = &props3;
  3387. props3.pNext = &subgroup_props;
  3388. subgroup_props.pNext = &driver_props;
  3389. // Pointer to the last chain element
  3390. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3391. if (integer_dot_product) {
  3392. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3393. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3394. }
  3395. physical_device.getProperties2(&props2);
  3396. VkPhysicalDeviceFeatures2 device_features2;
  3397. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3398. device_features2.pNext = nullptr;
  3399. VkPhysicalDeviceVulkan11Features vk11_features;
  3400. vk11_features.pNext = nullptr;
  3401. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3402. device_features2.pNext = &vk11_features;
  3403. VkPhysicalDeviceVulkan12Features vk12_features;
  3404. vk12_features.pNext = nullptr;
  3405. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3406. vk11_features.pNext = &vk12_features;
  3407. // Pointer to the last chain element
  3408. last_struct = (VkBaseOutStructure *)&vk12_features;
  3409. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3410. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3411. coopmat_features.pNext = nullptr;
  3412. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3413. coopmat_features.cooperativeMatrix = VK_FALSE;
  3414. if (coopmat_support) {
  3415. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3416. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3417. }
  3418. #endif
  3419. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3420. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3421. if (integer_dot_product) {
  3422. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3423. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3424. }
  3425. #if defined(VK_KHR_shader_bfloat16)
  3426. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3427. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3428. if (bfloat16_support) {
  3429. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3430. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3431. }
  3432. #endif
  3433. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3434. fp16 = fp16 && vk12_features.shaderFloat16;
  3435. #if defined(VK_KHR_shader_bfloat16)
  3436. bool bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3437. #else
  3438. bool bf16 = false;
  3439. #endif
  3440. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3441. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3442. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3443. integer_dot_product = integer_dot_product
  3444. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3445. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3446. coopmat_support = coopmat_support
  3447. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3448. && coopmat_features.cooperativeMatrix
  3449. #endif
  3450. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3451. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3452. std::string device_name = props2.properties.deviceName.data();
  3453. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | bf16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  3454. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, bf16, subgroup_size,
  3455. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3456. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3457. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3458. }
  3459. }
  3460. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3461. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3462. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3463. static void ggml_vk_instance_init() {
  3464. if (vk_instance_initialized) {
  3465. return;
  3466. }
  3467. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3468. uint32_t api_version = vk::enumerateInstanceVersion();
  3469. if (api_version < VK_API_VERSION_1_2) {
  3470. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3471. GGML_ABORT("fatal error");
  3472. }
  3473. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3474. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3475. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3476. #ifdef __APPLE__
  3477. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3478. #endif
  3479. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3480. std::vector<const char*> layers;
  3481. if (validation_ext) {
  3482. layers.push_back("VK_LAYER_KHRONOS_validation");
  3483. }
  3484. std::vector<const char*> extensions;
  3485. if (validation_ext) {
  3486. extensions.push_back("VK_EXT_validation_features");
  3487. }
  3488. #ifdef __APPLE__
  3489. if (portability_enumeration_ext) {
  3490. extensions.push_back("VK_KHR_portability_enumeration");
  3491. }
  3492. #endif
  3493. if (debug_utils_ext) {
  3494. extensions.push_back("VK_EXT_debug_utils");
  3495. }
  3496. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3497. #ifdef __APPLE__
  3498. if (portability_enumeration_ext) {
  3499. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3500. }
  3501. #endif
  3502. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3503. vk::ValidationFeaturesEXT validation_features;
  3504. if (validation_ext) {
  3505. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3506. validation_features = {
  3507. features_enable,
  3508. {},
  3509. };
  3510. validation_features.setPNext(nullptr);
  3511. instance_create_info.setPNext(&validation_features);
  3512. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3513. }
  3514. vk_instance.instance = vk::createInstance(instance_create_info);
  3515. vk_instance_initialized = true;
  3516. if (debug_utils_ext) {
  3517. vk_instance.debug_utils_support = true;
  3518. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3519. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3520. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3521. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3522. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3523. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3524. }
  3525. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3526. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3527. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3528. if (devices_env != nullptr) {
  3529. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  3530. std::string devices(devices_env);
  3531. std::replace(devices.begin(), devices.end(), ',', ' ');
  3532. std::stringstream ss(devices);
  3533. size_t tmp;
  3534. while (ss >> tmp) {
  3535. if(tmp >= num_available_devices) {
  3536. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3537. throw std::runtime_error("Invalid Vulkan device index");
  3538. }
  3539. vk_instance.device_indices.push_back(tmp);
  3540. }
  3541. } else {
  3542. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3543. // If no vulkan devices are found, return early
  3544. if (devices.empty()) {
  3545. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3546. return;
  3547. }
  3548. // Default to using all dedicated GPUs
  3549. for (size_t i = 0; i < devices.size(); i++) {
  3550. vk::PhysicalDeviceProperties2 new_props;
  3551. vk::PhysicalDeviceDriverProperties new_driver;
  3552. vk::PhysicalDeviceIDProperties new_id;
  3553. new_props.pNext = &new_driver;
  3554. new_driver.pNext = &new_id;
  3555. devices[i].getProperties2(&new_props);
  3556. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3557. // Check if there are two physical devices corresponding to the same GPU
  3558. auto old_device = std::find_if(
  3559. vk_instance.device_indices.begin(),
  3560. vk_instance.device_indices.end(),
  3561. [&devices, &new_id](const size_t k){
  3562. vk::PhysicalDeviceProperties2 old_props;
  3563. vk::PhysicalDeviceIDProperties old_id;
  3564. old_props.pNext = &old_id;
  3565. devices[k].getProperties2(&old_props);
  3566. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3567. }
  3568. );
  3569. if (old_device == vk_instance.device_indices.end()) {
  3570. vk_instance.device_indices.push_back(i);
  3571. } else {
  3572. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3573. // This can cause error when splitting layers aross the devices, need to keep only 1
  3574. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3575. vk::PhysicalDeviceProperties2 old_props;
  3576. vk::PhysicalDeviceDriverProperties old_driver;
  3577. old_props.pNext = &old_driver;
  3578. devices[*old_device].getProperties2(&old_props);
  3579. std::map<vk::DriverId, int> driver_priorities {};
  3580. int old_priority = std::numeric_limits<int>::max();
  3581. int new_priority = std::numeric_limits<int>::max();
  3582. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3583. // Smaller number -> higher priority
  3584. switch (old_props.properties.vendorID) {
  3585. case VK_VENDOR_ID_AMD:
  3586. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3587. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3588. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3589. break;
  3590. case VK_VENDOR_ID_INTEL:
  3591. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3592. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3593. break;
  3594. case VK_VENDOR_ID_NVIDIA:
  3595. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3596. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3597. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3598. #endif
  3599. break;
  3600. }
  3601. if (driver_priorities.count(old_driver.driverID)) {
  3602. old_priority = driver_priorities[old_driver.driverID];
  3603. }
  3604. if (driver_priorities.count(new_driver.driverID)) {
  3605. new_priority = driver_priorities[new_driver.driverID];
  3606. }
  3607. if (new_priority < old_priority) {
  3608. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3609. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3610. vk_instance.device_indices.push_back(i);
  3611. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3612. }
  3613. else {
  3614. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3615. }
  3616. }
  3617. }
  3618. }
  3619. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3620. // If only CPU devices are available, return without devices.
  3621. if (vk_instance.device_indices.empty()) {
  3622. for (size_t i = 0; i < devices.size(); i++) {
  3623. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3624. vk_instance.device_indices.push_back(i);
  3625. break;
  3626. }
  3627. }
  3628. }
  3629. if (vk_instance.device_indices.empty()) {
  3630. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3631. return;
  3632. }
  3633. }
  3634. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3635. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3636. ggml_vk_print_gpu_info(i);
  3637. }
  3638. }
  3639. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3640. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3641. ggml_vk_instance_init();
  3642. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3643. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3644. ctx->device = ggml_vk_get_device(idx);
  3645. ctx->semaphore_idx = 0;
  3646. ctx->event_idx = 0;
  3647. ctx->prealloc_size_x = 0;
  3648. ctx->prealloc_size_y = 0;
  3649. ctx->prealloc_size_split_k = 0;
  3650. ctx->fence = ctx->device->device.createFence({});
  3651. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3652. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3653. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3654. #ifdef GGML_VULKAN_CHECK_RESULTS
  3655. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3656. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3657. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3658. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3659. #endif
  3660. }
  3661. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3662. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3663. switch (type) {
  3664. case GGML_TYPE_F32:
  3665. case GGML_TYPE_Q4_0:
  3666. case GGML_TYPE_Q4_1:
  3667. case GGML_TYPE_Q5_0:
  3668. case GGML_TYPE_Q5_1:
  3669. case GGML_TYPE_Q8_0:
  3670. case GGML_TYPE_Q2_K:
  3671. case GGML_TYPE_Q3_K:
  3672. case GGML_TYPE_Q4_K:
  3673. case GGML_TYPE_Q5_K:
  3674. case GGML_TYPE_Q6_K:
  3675. case GGML_TYPE_IQ1_S:
  3676. case GGML_TYPE_IQ1_M:
  3677. case GGML_TYPE_IQ2_XXS:
  3678. case GGML_TYPE_IQ2_XS:
  3679. case GGML_TYPE_IQ2_S:
  3680. case GGML_TYPE_IQ3_XXS:
  3681. case GGML_TYPE_IQ3_S:
  3682. case GGML_TYPE_IQ4_XS:
  3683. case GGML_TYPE_IQ4_NL:
  3684. case GGML_TYPE_MXFP4:
  3685. break;
  3686. default:
  3687. return nullptr;
  3688. }
  3689. return ctx->device->pipeline_dequant[type];
  3690. }
  3691. 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) {
  3692. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3693. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3694. return ctx->device->pipeline_matmul_f32;
  3695. }
  3696. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3697. return ctx->device->pipeline_matmul_f32_f16;
  3698. }
  3699. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3700. return ctx->device->pipeline_matmul_bf16;
  3701. }
  3702. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3703. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3704. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3705. }
  3706. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3707. return ctx->device->pipeline_matmul_f16.f16acc;
  3708. }
  3709. } else {
  3710. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3711. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3712. }
  3713. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3714. return ctx->device->pipeline_matmul_f16.f32acc;
  3715. }
  3716. }
  3717. // MMQ
  3718. if (src1_type == GGML_TYPE_Q8_1) {
  3719. vk_matmul_pipeline pipelines = (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f32acc;
  3720. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3721. return nullptr;
  3722. }
  3723. return pipelines;
  3724. }
  3725. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3726. return nullptr;
  3727. }
  3728. switch (src0_type) {
  3729. case GGML_TYPE_Q4_0:
  3730. case GGML_TYPE_Q4_1:
  3731. case GGML_TYPE_Q5_0:
  3732. case GGML_TYPE_Q5_1:
  3733. case GGML_TYPE_Q8_0:
  3734. case GGML_TYPE_Q2_K:
  3735. case GGML_TYPE_Q3_K:
  3736. case GGML_TYPE_Q4_K:
  3737. case GGML_TYPE_Q5_K:
  3738. case GGML_TYPE_Q6_K:
  3739. case GGML_TYPE_IQ1_S:
  3740. case GGML_TYPE_IQ1_M:
  3741. case GGML_TYPE_IQ2_XXS:
  3742. case GGML_TYPE_IQ2_XS:
  3743. case GGML_TYPE_IQ2_S:
  3744. case GGML_TYPE_IQ3_XXS:
  3745. case GGML_TYPE_IQ3_S:
  3746. case GGML_TYPE_IQ4_XS:
  3747. case GGML_TYPE_IQ4_NL:
  3748. case GGML_TYPE_MXFP4:
  3749. break;
  3750. default:
  3751. return nullptr;
  3752. }
  3753. if (ctx->device->coopmat2) {
  3754. assert(src1_type == GGML_TYPE_F16);
  3755. return prec == GGML_PREC_DEFAULT ? ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f32acc;
  3756. }
  3757. if (ctx->device->coopmat_support) {
  3758. return (ctx->device->fp16 && ctx->device->coopmat_acc_f16_support && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3759. }
  3760. return (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3761. }
  3762. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type, uint32_t num_cols, uint32_t m, uint32_t k) {
  3763. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3764. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3765. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3766. switch (a_type) {
  3767. case GGML_TYPE_F32:
  3768. case GGML_TYPE_F16:
  3769. case GGML_TYPE_BF16:
  3770. case GGML_TYPE_Q4_0:
  3771. case GGML_TYPE_Q4_1:
  3772. case GGML_TYPE_Q5_0:
  3773. case GGML_TYPE_Q5_1:
  3774. case GGML_TYPE_Q8_0:
  3775. case GGML_TYPE_Q2_K:
  3776. case GGML_TYPE_Q3_K:
  3777. case GGML_TYPE_Q4_K:
  3778. case GGML_TYPE_Q5_K:
  3779. case GGML_TYPE_Q6_K:
  3780. case GGML_TYPE_IQ1_S:
  3781. case GGML_TYPE_IQ1_M:
  3782. case GGML_TYPE_IQ2_XXS:
  3783. case GGML_TYPE_IQ2_XS:
  3784. case GGML_TYPE_IQ2_S:
  3785. case GGML_TYPE_IQ3_XXS:
  3786. case GGML_TYPE_IQ3_S:
  3787. case GGML_TYPE_IQ4_XS:
  3788. case GGML_TYPE_IQ4_NL:
  3789. case GGML_TYPE_MXFP4:
  3790. break;
  3791. default:
  3792. return nullptr;
  3793. }
  3794. // heuristic to choose workgroup size
  3795. uint32_t dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  3796. if (ctx->device->vendor_id == VK_VENDOR_ID_NVIDIA || ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  3797. // Prefer larger workgroups when M is small, to spread the work out more
  3798. // and keep more SMs busy.
  3799. // q6_k seems to prefer small workgroup size even for "medium" values of M.
  3800. if (a_type == GGML_TYPE_Q6_K) {
  3801. if (m < 4096 && k >= 1024) {
  3802. dmmv_wg = DMMV_WG_SIZE_LARGE;
  3803. }
  3804. } else {
  3805. if (m <= 8192 && k >= 1024) {
  3806. dmmv_wg = DMMV_WG_SIZE_LARGE;
  3807. }
  3808. }
  3809. }
  3810. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[dmmv_wg][a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[dmmv_wg][a_type][num_cols-1];
  3811. }
  3812. 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) {
  3813. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3814. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3815. return ctx->device->pipeline_matmul_id_f32;
  3816. }
  3817. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3818. return ctx->device->pipeline_matmul_id_bf16;
  3819. }
  3820. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3821. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3822. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3823. }
  3824. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3825. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3826. }
  3827. } else {
  3828. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3829. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3830. }
  3831. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3832. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3833. }
  3834. }
  3835. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3836. switch (src0_type) {
  3837. case GGML_TYPE_Q4_0:
  3838. case GGML_TYPE_Q4_1:
  3839. case GGML_TYPE_Q5_0:
  3840. case GGML_TYPE_Q5_1:
  3841. case GGML_TYPE_Q8_0:
  3842. case GGML_TYPE_Q2_K:
  3843. case GGML_TYPE_Q3_K:
  3844. case GGML_TYPE_Q4_K:
  3845. case GGML_TYPE_Q5_K:
  3846. case GGML_TYPE_Q6_K:
  3847. case GGML_TYPE_IQ1_S:
  3848. case GGML_TYPE_IQ1_M:
  3849. case GGML_TYPE_IQ2_XXS:
  3850. case GGML_TYPE_IQ2_XS:
  3851. case GGML_TYPE_IQ2_S:
  3852. case GGML_TYPE_IQ3_XXS:
  3853. case GGML_TYPE_IQ3_S:
  3854. case GGML_TYPE_IQ4_XS:
  3855. case GGML_TYPE_IQ4_NL:
  3856. case GGML_TYPE_MXFP4:
  3857. break;
  3858. default:
  3859. return nullptr;
  3860. }
  3861. // XXX TODO 'prec' is not actually allowed in mul_mat_id.
  3862. bool prefer_fp16acc = ctx->device->fp16 /*&& prec == GGML_PREC_DEFAULT*/;
  3863. bool support_fp16acc = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc != nullptr;
  3864. bool support_fp32acc = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc != nullptr;
  3865. if (support_fp16acc && (prefer_fp16acc || !support_fp32acc)) {
  3866. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc;
  3867. } else {
  3868. GGML_ASSERT(support_fp32acc);
  3869. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3870. }
  3871. }
  3872. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3873. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3874. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3875. switch (a_type) {
  3876. case GGML_TYPE_F32:
  3877. case GGML_TYPE_F16:
  3878. case GGML_TYPE_BF16:
  3879. case GGML_TYPE_Q4_0:
  3880. case GGML_TYPE_Q4_1:
  3881. case GGML_TYPE_Q5_0:
  3882. case GGML_TYPE_Q5_1:
  3883. case GGML_TYPE_Q8_0:
  3884. case GGML_TYPE_Q2_K:
  3885. case GGML_TYPE_Q3_K:
  3886. case GGML_TYPE_Q4_K:
  3887. case GGML_TYPE_Q5_K:
  3888. case GGML_TYPE_Q6_K:
  3889. case GGML_TYPE_IQ1_S:
  3890. case GGML_TYPE_IQ1_M:
  3891. case GGML_TYPE_IQ2_XXS:
  3892. case GGML_TYPE_IQ2_XS:
  3893. case GGML_TYPE_IQ2_S:
  3894. case GGML_TYPE_IQ3_XXS:
  3895. case GGML_TYPE_IQ3_S:
  3896. case GGML_TYPE_IQ4_XS:
  3897. case GGML_TYPE_IQ4_NL:
  3898. case GGML_TYPE_MXFP4:
  3899. break;
  3900. default:
  3901. return nullptr;
  3902. }
  3903. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3904. }
  3905. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3906. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3907. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3908. int best_i = -1;
  3909. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3910. int worst_i = -1;
  3911. size_t worst_size = 0; //largest unused buffer seen so far
  3912. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3913. vk_buffer &b = ctx->buffer_pool[i];
  3914. if (b != nullptr && b->size >= size && b->size < best_size) {
  3915. best_i = i;
  3916. best_size = b->size;
  3917. }
  3918. if (b != nullptr && b->size > worst_size) {
  3919. worst_i = i;
  3920. worst_size = b->size;
  3921. }
  3922. }
  3923. if(best_i != -1) {
  3924. //found the smallest buffer that fits our needs
  3925. vk_buffer b = ctx->buffer_pool[best_i];
  3926. ctx->buffer_pool[best_i].reset();
  3927. return b;
  3928. }
  3929. if(worst_i != -1) {
  3930. //no buffer that fits our needs, resize largest one to save memory
  3931. vk_buffer& b = ctx->buffer_pool[worst_i];
  3932. ggml_vk_destroy_buffer(b);
  3933. }
  3934. return ggml_vk_create_buffer_device(ctx->device, size);
  3935. }
  3936. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3937. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3938. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3939. vk_buffer& b = ctx->buffer_pool[i];
  3940. if (b == nullptr) {
  3941. b = buffer;
  3942. return;
  3943. }
  3944. }
  3945. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3946. ggml_vk_destroy_buffer(buffer);
  3947. }
  3948. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3949. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3950. // Try to find existing temp buffer with enough capacity
  3951. for (auto& buffer : ctx->gc.temp_buffers) {
  3952. if (buffer->size >= size) {
  3953. return buffer;
  3954. }
  3955. }
  3956. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3957. // Otherwise create new buffer
  3958. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3959. ctx->gc.temp_buffers.push_back(buf);
  3960. return buf;
  3961. }
  3962. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3963. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3964. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3965. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3966. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3967. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3968. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3969. size/1024.0/1024.0);
  3970. device->device.freeMemory(buf->device_memory);
  3971. device->device.destroyBuffer(buf->buffer);
  3972. return nullptr;
  3973. }
  3974. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3975. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3976. return buf->ptr;
  3977. }
  3978. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3979. if (ptr == nullptr) {
  3980. return;
  3981. }
  3982. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3983. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3984. vk_buffer buf;
  3985. size_t index;
  3986. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3987. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3988. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3989. if (ptr >= addr && ptr < endr) {
  3990. buf = std::get<2>(device->pinned_memory[i]);
  3991. index = i;
  3992. break;
  3993. }
  3994. }
  3995. if (buf == nullptr) {
  3996. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3997. return;
  3998. }
  3999. ggml_vk_destroy_buffer(buf);
  4000. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  4001. }
  4002. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  4003. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4004. buf = nullptr;
  4005. buf_offset = 0;
  4006. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4007. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4008. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4009. if (ptr >= addr && ptr < endr) {
  4010. buf = std::get<2>(device->pinned_memory[i]);
  4011. buf_offset = ((const uint8_t *)ptr) - addr;
  4012. break;
  4013. }
  4014. }
  4015. }
  4016. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  4017. vk_submission s;
  4018. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  4019. if (one_time) {
  4020. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  4021. } else {
  4022. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  4023. }
  4024. return s;
  4025. }
  4026. template <typename T> size_t push_constant_size(const T &t) {
  4027. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4028. GGML_UNUSED(t);
  4029. return sizeof(T);
  4030. }
  4031. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  4032. GGML_UNUSED(t);
  4033. return sizeof(T) * t.size();
  4034. }
  4035. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  4036. GGML_UNUSED(t);
  4037. return sizeof(T) * N;
  4038. }
  4039. template <typename T> const T *push_constant_data(const T &t) {
  4040. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4041. return &t;
  4042. }
  4043. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  4044. return t.data();
  4045. }
  4046. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  4047. return t.data();
  4048. }
  4049. template <typename T>
  4050. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context* ctx, vk_context& subctx, vk_pipeline& pipeline, std::initializer_list<vk::DescriptorBufferInfo> const& descriptor_buffer_infos, const T &push_constants, std::array<uint32_t, 3> elements) {
  4051. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  4052. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  4053. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  4054. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  4055. for (auto& buffer : descriptor_buffer_infos) {
  4056. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  4057. }
  4058. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  4059. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  4060. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  4061. GGML_ASSERT(pipeline->parameter_count == descriptor_buffer_infos.size());
  4062. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  4063. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  4064. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  4065. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  4066. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  4067. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  4068. pipeline->layout,
  4069. 0,
  4070. { descriptor_set },
  4071. {});
  4072. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  4073. }
  4074. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  4075. s.buffer.end();
  4076. s.wait_semaphores = std::move(wait_semaphores);
  4077. s.signal_semaphores = std::move(signal_semaphores);
  4078. }
  4079. static void ggml_vk_ctx_end(vk_context& ctx) {
  4080. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  4081. if (ctx->s == nullptr) {
  4082. return;
  4083. }
  4084. ctx->s->buffer.end();
  4085. ctx->s = nullptr;
  4086. }
  4087. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  4088. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  4089. if (subctx->s != nullptr) {
  4090. ggml_vk_ctx_end(subctx);
  4091. }
  4092. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  4093. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  4094. }
  4095. static size_t ggml_vk_align_size(size_t width, size_t align) {
  4096. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  4097. return CEIL_DIV(width, align) * align;
  4098. }
  4099. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  4100. if (memcpys == nullptr) {
  4101. memcpy(dst, src, size);
  4102. } else {
  4103. memcpys->emplace_back(dst, src, size);
  4104. }
  4105. }
  4106. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  4107. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  4108. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  4109. ggml_vk_destroy_buffer(device->sync_staging);
  4110. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  4111. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4112. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4113. }
  4114. }
  4115. 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) {
  4116. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  4117. GGML_ASSERT(!ggml_is_contiguous(tensor));
  4118. // Buffer is already mapped
  4119. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4120. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4121. GGML_ABORT("fatal error");
  4122. }
  4123. // Check if src is pinned memory
  4124. vk_buffer buf = nullptr;
  4125. size_t buf_offset = 0;
  4126. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  4127. const uint64_t ne0 = tensor->ne[0];
  4128. const uint64_t ne1 = tensor->ne[1];
  4129. const uint64_t ne2 = tensor->ne[2];
  4130. const uint64_t ne3 = tensor->ne[3];
  4131. const uint64_t nb0 = tensor->nb[0];
  4132. const uint64_t nb1 = tensor->nb[1];
  4133. const uint64_t nb2 = tensor->nb[2];
  4134. const uint64_t nb3 = tensor->nb[3];
  4135. const ggml_type type = tensor->type;
  4136. const uint64_t ts = ggml_type_size(type);
  4137. const uint64_t bs = ggml_blck_size(type);
  4138. const uint64_t dstnb0 = ts;
  4139. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  4140. const uint64_t dstnb2 = dstnb1*ne1;
  4141. const uint64_t dstnb3 = dstnb2*ne2;
  4142. const uint64_t ne = ggml_nelements(tensor);
  4143. if (buf != nullptr) {
  4144. // Memory is pinned, use as staging buffer
  4145. std::vector<vk::BufferCopy> slices;
  4146. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4147. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4148. // Find longest contiguous slice
  4149. if (ne1*nb1 == dstnb2) {
  4150. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  4151. } else {
  4152. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4153. if (ne0*nb0/bs == dstnb1) {
  4154. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  4155. } else {
  4156. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4157. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4158. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4159. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  4160. }
  4161. }
  4162. }
  4163. }
  4164. }
  4165. }
  4166. ggml_vk_sync_buffers(subctx);
  4167. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4168. return;
  4169. }
  4170. if (!sync_staging) {
  4171. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4172. }
  4173. // Staging buffer required
  4174. vk_buffer& staging = ctx->device->sync_staging;
  4175. const uint64_t copy_size = ts*ne/bs;
  4176. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  4177. VkBufferCopy buf_copy{ 0, offset, copy_size };
  4178. ggml_vk_sync_buffers(subctx);
  4179. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4180. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4181. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4182. // Find longest contiguous slice
  4183. if (ne1*nb1 == dstnb2) {
  4184. 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);
  4185. } else {
  4186. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4187. if (ne0*nb0/bs == dstnb1) {
  4188. 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);
  4189. } else {
  4190. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4191. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4192. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4193. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  4194. }
  4195. }
  4196. }
  4197. }
  4198. }
  4199. }
  4200. }
  4201. 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) {
  4202. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  4203. // Buffer is already mapped
  4204. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4205. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4206. GGML_ABORT("fatal error");
  4207. }
  4208. // Check if src is pinned memory
  4209. vk_buffer buf = nullptr;
  4210. size_t buf_offset = 0;
  4211. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  4212. if (buf != nullptr) {
  4213. // Memory is pinned, use as staging buffer
  4214. std::vector<vk::BufferCopy> slices(1);
  4215. if (width == spitch) {
  4216. // Only do single write if stride is equal
  4217. slices[0].srcOffset = buf_offset;
  4218. slices[0].dstOffset = offset;
  4219. slices[0].size = width * height;
  4220. } else {
  4221. slices.resize(height);
  4222. for (size_t i = 0; i < height; i++) {
  4223. slices[i].srcOffset = buf_offset + i * spitch;
  4224. slices[i].dstOffset = offset + i * width;
  4225. slices[i].size = width;
  4226. }
  4227. }
  4228. ggml_vk_sync_buffers(subctx);
  4229. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4230. return;
  4231. }
  4232. VK_LOG_DEBUG("STAGING");
  4233. if (!sync_staging) {
  4234. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4235. }
  4236. // Staging buffer required
  4237. const size_t copy_size = width*height;
  4238. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  4239. vk_buffer& staging_buffer = dst->device->sync_staging;
  4240. VkBufferCopy buf_copy = {
  4241. 0,
  4242. offset,
  4243. copy_size};
  4244. ggml_vk_sync_buffers(subctx);
  4245. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4246. if (width == spitch) {
  4247. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  4248. } else {
  4249. for (size_t i = 0; i < height; i++) {
  4250. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  4251. }
  4252. }
  4253. }
  4254. 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) {
  4255. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  4256. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  4257. }
  4258. 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) {
  4259. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  4260. // Buffer is already mapped
  4261. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4262. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4263. for (size_t i = 0; i < height; i++) {
  4264. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  4265. }
  4266. } else {
  4267. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4268. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4269. ggml_vk_ctx_begin(dst->device, subctx);
  4270. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  4271. ggml_vk_ctx_end(subctx);
  4272. for (auto& cpy : subctx->in_memcpys) {
  4273. memcpy(cpy.dst, cpy.src, cpy.n);
  4274. }
  4275. ggml_vk_submit(subctx, dst->device->fence);
  4276. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  4277. dst->device->device.resetFences({ dst->device->fence });
  4278. ggml_vk_queue_command_pools_cleanup(dst->device);
  4279. }
  4280. }
  4281. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  4282. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  4283. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  4284. }
  4285. 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) {
  4286. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  4287. GGML_ASSERT(width > 0);
  4288. GGML_ASSERT(height > 0);
  4289. GGML_ASSERT(src != nullptr);
  4290. // TODO: staging_offset is not used
  4291. // Check if dst is pinned memory
  4292. vk_buffer buf = nullptr;
  4293. size_t buf_offset = 0;
  4294. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  4295. std::vector<vk::BufferCopy> slices(1);
  4296. if (width == spitch && width == dpitch) {
  4297. // Only do single write if stride is equal
  4298. slices[0].srcOffset = offset;
  4299. slices[0].dstOffset = buf_offset;
  4300. slices[0].size = width * height;
  4301. } else {
  4302. slices.resize(height);
  4303. for (size_t i = 0; i < height; i++) {
  4304. slices[i].srcOffset = offset + i * spitch;
  4305. slices[i].dstOffset = buf_offset + i * dpitch;
  4306. slices[i].size = width;
  4307. }
  4308. }
  4309. if (buf != nullptr) {
  4310. // Memory is pinned, use as staging buffer
  4311. ggml_vk_sync_buffers(subctx);
  4312. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  4313. return;
  4314. }
  4315. VK_LOG_DEBUG("STAGING");
  4316. if (!sync_staging) {
  4317. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  4318. }
  4319. // Fall back to staging buffer
  4320. const size_t copy_size = dpitch * height;
  4321. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4322. vk_buffer& staging_buffer = src->device->sync_staging;
  4323. ggml_vk_sync_buffers(subctx);
  4324. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4325. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4326. }
  4327. 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) {
  4328. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4329. }
  4330. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4331. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4332. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4333. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4334. // the HW device to host copy path.
  4335. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4336. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4337. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4338. } else {
  4339. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4340. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4341. ggml_vk_ctx_begin(src->device, subctx);
  4342. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4343. ggml_vk_ctx_end(subctx);
  4344. ggml_vk_submit(subctx, src->device->fence);
  4345. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4346. src->device->device.resetFences({ src->device->fence });
  4347. ggml_vk_queue_command_pools_cleanup(src->device);
  4348. for (auto& cpy : subctx->out_memcpys) {
  4349. memcpy(cpy.dst, cpy.src, cpy.n);
  4350. }
  4351. }
  4352. }
  4353. 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) {
  4354. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4355. // Make sure both buffers are on same device
  4356. GGML_ASSERT(src->device == dst->device);
  4357. VkBufferCopy bc{ src_offset, dst_offset, size };
  4358. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4359. }
  4360. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4361. if (src->device == dst->device) {
  4362. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4363. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4364. // Copy within the device
  4365. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4366. ggml_vk_ctx_begin(src->device, subctx);
  4367. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4368. ggml_vk_ctx_end(subctx);
  4369. ggml_vk_submit(subctx, src->device->fence);
  4370. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4371. src->device->device.resetFences({ src->device->fence });
  4372. ggml_vk_queue_command_pools_cleanup(src->device);
  4373. } else {
  4374. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4375. // Copy device to device
  4376. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4377. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4378. // Copy to src staging buffer
  4379. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4380. // memcpy to dst staging buffer
  4381. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4382. // Copy to dst buffer
  4383. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4384. }
  4385. }
  4386. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4387. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4388. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4389. }
  4390. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4391. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4392. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4393. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4394. ggml_vk_ctx_begin(dst->device, subctx);
  4395. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4396. ggml_vk_ctx_end(subctx);
  4397. ggml_vk_submit(subctx, dst->device->fence);
  4398. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4399. dst->device->device.resetFences({ dst->device->fence });
  4400. ggml_vk_queue_command_pools_cleanup(dst->device);
  4401. }
  4402. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, uint32_t m, uint32_t n, uint32_t k, const vk_pipeline& pipeline) {
  4403. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4404. uint32_t split_k = 1;
  4405. if (ctx->device->shader_core_count != 0 && m >= pipeline->wg_denoms[0] && n >= pipeline->wg_denoms[1]) {
  4406. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4407. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4408. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4409. if (k >= 2048) {
  4410. if (m_tiles * n_tiles <= ctx->device->shader_core_count / 2) {
  4411. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4412. } else if (m_tiles * n_tiles <= ctx->device->shader_core_count * 2 / 3) {
  4413. split_k = 3;
  4414. }
  4415. // Cap the split at 8x. Unless k is huge this is a lot of overhead.
  4416. split_k = std::min(split_k, 8u);
  4417. // ggml_vk_matmul will align the splits to be a multiple of 256.
  4418. // If this rounded up size would cause the last split to be empty,
  4419. // then reduce the split count.
  4420. while (true) {
  4421. if (split_k == 1) {
  4422. break;
  4423. }
  4424. uint32_t k_split = CEIL_DIV(k, split_k);
  4425. k_split = ROUNDUP_POW2(k_split, 256);
  4426. if (k_split * (split_k - 1) < k) {
  4427. break;
  4428. }
  4429. split_k--;
  4430. }
  4431. }
  4432. }
  4433. return split_k;
  4434. }
  4435. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type, ggml_type src1_type) {
  4436. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4437. if (ctx->device->coopmat2) {
  4438. const uint32_t shader_core_count = ctx->device->shader_core_count;
  4439. const uint32_t tiles_l = CEIL_DIV(m, mmp->a_l->wg_denoms[0]) * CEIL_DIV(n, mmp->a_l->wg_denoms[1]);
  4440. const uint32_t tiles_m = CEIL_DIV(m, mmp->a_m->wg_denoms[0]) * CEIL_DIV(n, mmp->a_m->wg_denoms[1]);
  4441. // Use large shader when the N dimension is greater than the medium shader's tile size
  4442. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4443. // Prefer large over medium if either:
  4444. // - medium or large tiles would overfill the GPU
  4445. // - large tiles with a split_k==3 fits in the GPU and medium tiles with split_k==2 does not
  4446. // (medium with split_k==2 is probably better if it fits - more workgroups running and less split_k overhead)
  4447. bool prefer_large = tiles_m > shader_core_count || tiles_l > shader_core_count ||
  4448. // split_k==3 with large tiles likely better than medium tiles with no split_k.
  4449. (tiles_l <= shader_core_count / 3 && tiles_m > shader_core_count / 2);
  4450. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large && prefer_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  4451. return aligned ? mmp->a_l : mmp->l;
  4452. }
  4453. // Use medium shader when the N dimension is greater than the small shader's tile size
  4454. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4455. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4456. return aligned ? mmp->a_m : mmp->m;
  4457. }
  4458. return aligned ? mmp->a_s : mmp->s;
  4459. }
  4460. 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])) {
  4461. return aligned ? mmp->a_s : mmp->s;
  4462. }
  4463. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4464. return aligned ? mmp->a_m : mmp->m;
  4465. }
  4466. return aligned ? mmp->a_l : mmp->l;
  4467. GGML_UNUSED(src1_type);
  4468. }
  4469. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type, ggml_type src1_type) {
  4470. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4471. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4472. }
  4473. static void ggml_vk_matmul(
  4474. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4475. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4476. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4477. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4478. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4479. uint32_t padded_n) {
  4480. VK_LOG_DEBUG("ggml_vk_matmul(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), split_k: (" << (split_k_buffer.buffer != nullptr ? split_k_buffer.buffer->buffer : VK_NULL_HANDLE) << ", " << split_k_buffer.offset << ", " << split_k_buffer.size << "), m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", split_k: " << split_k << ", batch: " << batch << ", ne02: " << ne02 << ", ne12: " << ne12 << ", broadcast2: " << broadcast2 << ", broadcast3: " << broadcast3 << ", padded_n: " << padded_n << ")");
  4481. ggml_vk_sync_buffers(subctx);
  4482. if (split_k == 1) {
  4483. 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 };
  4484. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4485. return;
  4486. }
  4487. GGML_ASSERT(batch_stride_d == m * n);
  4488. // Round the split size up to a multiple of 256 (k-quant alignment)
  4489. uint32_t k_split = CEIL_DIV(k, split_k);
  4490. k_split = ROUNDUP_POW2(k_split, 256);
  4491. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, k_split, ne02, ne12, broadcast2, broadcast3, padded_n };
  4492. // Make sure enough workgroups get assigned for split k to work
  4493. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, split_k_buffer }, pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  4494. ggml_vk_sync_buffers(subctx);
  4495. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4496. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4497. }
  4498. static vk_pipeline ggml_vk_guess_matmul_id_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type) {
  4499. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4500. if (ctx->device->coopmat2) {
  4501. // Use large shader when the N dimension is greater than the medium shader's tile size
  4502. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4503. 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])) {
  4504. return aligned ? mmp->a_l : mmp->l;
  4505. }
  4506. // Use medium shader when the N dimension is greater than the small shader's tile size
  4507. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4508. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4509. return aligned ? mmp->a_m : mmp->m;
  4510. }
  4511. return aligned ? mmp->a_s : mmp->s;
  4512. }
  4513. 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])) {
  4514. return aligned ? mmp->a_s : mmp->s;
  4515. }
  4516. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4517. return aligned ? mmp->a_m : mmp->m;
  4518. }
  4519. return aligned ? mmp->a_l : mmp->l;
  4520. }
  4521. 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) {
  4522. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4523. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4524. }
  4525. static void ggml_vk_matmul_id(
  4526. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4527. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4528. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4529. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4530. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4531. uint32_t padded_n) {
  4532. 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 << "), " <<
  4533. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4534. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4535. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4536. ggml_vk_sync_buffers(subctx);
  4537. 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,
  4538. nei0, nei1, nbi1, ne11, padded_n };
  4539. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4540. }
  4541. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4542. return
  4543. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4544. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4545. (tensor->ne[3] == 1 || tensor->nb[3] == tensor->nb[2]*tensor->ne[2]);
  4546. }
  4547. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4548. // Choose "contiguous copy" shader if src/dst are contiguous
  4549. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4550. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4551. if (contig) {
  4552. return ctx->device->pipeline_contig_cpy_f32_f32;
  4553. } else {
  4554. return ctx->device->pipeline_cpy_f32_f32;
  4555. }
  4556. }
  4557. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4558. if (contig) {
  4559. return ctx->device->pipeline_contig_cpy_f32_f16;
  4560. } else {
  4561. return ctx->device->pipeline_cpy_f32_f16;
  4562. }
  4563. }
  4564. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4565. if (contig) {
  4566. return ctx->device->pipeline_contig_cpy_f16_f16;
  4567. } else {
  4568. return ctx->device->pipeline_cpy_f16_f16;
  4569. }
  4570. }
  4571. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4572. if (contig) {
  4573. return ctx->device->pipeline_contig_cpy_f16_f32;
  4574. } else {
  4575. return ctx->device->pipeline_cpy_f16_f32;
  4576. }
  4577. }
  4578. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4579. if (contig) {
  4580. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4581. } else {
  4582. return ctx->device->pipeline_cpy_f32_bf16;
  4583. }
  4584. }
  4585. if (src->type == GGML_TYPE_F32) {
  4586. switch (to) {
  4587. case GGML_TYPE_Q4_0:
  4588. case GGML_TYPE_Q4_1:
  4589. case GGML_TYPE_Q5_0:
  4590. case GGML_TYPE_Q5_1:
  4591. case GGML_TYPE_Q8_0:
  4592. case GGML_TYPE_IQ4_NL:
  4593. return ctx->device->pipeline_cpy_f32_quant[to];
  4594. default:
  4595. break;
  4596. }
  4597. }
  4598. if (to == GGML_TYPE_F32) {
  4599. switch (src->type) {
  4600. case GGML_TYPE_Q4_0:
  4601. case GGML_TYPE_Q4_1:
  4602. case GGML_TYPE_Q5_0:
  4603. case GGML_TYPE_Q5_1:
  4604. case GGML_TYPE_Q8_0:
  4605. case GGML_TYPE_IQ4_NL:
  4606. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4607. default:
  4608. break;
  4609. }
  4610. }
  4611. if (src->type == to) {
  4612. // Copy two or four bytes at a time, depending on block size.
  4613. // For quantized types, we scale by block size/type size. But
  4614. // this path is also used for bf16->bf16 for example, where the
  4615. // type size must be exactly 2 or 4.
  4616. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4617. if ((ggml_type_size(src->type) % 4) == 0) {
  4618. if (contig) {
  4619. return ctx->device->pipeline_contig_cpy_f32_f32;
  4620. } else {
  4621. return ctx->device->pipeline_cpy_f32_f32;
  4622. }
  4623. } else {
  4624. if (contig) {
  4625. return ctx->device->pipeline_contig_cpy_f16_f16;
  4626. } else {
  4627. return ctx->device->pipeline_cpy_f16_f16;
  4628. }
  4629. }
  4630. }
  4631. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4632. GGML_ABORT("fatal error");
  4633. }
  4634. 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) {
  4635. 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] << "), ";
  4636. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4637. const int tensor_type_size = ggml_type_size(tensor->type);
  4638. const uint32_t ne = ggml_nelements(tensor);
  4639. std::array<uint32_t, 3> elements;
  4640. if (ne > 262144) {
  4641. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4642. } else if (ne > 512) {
  4643. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4644. } else {
  4645. elements = { ne, 1, 1 };
  4646. }
  4647. vk_op_unary_push_constants pc = {
  4648. (uint32_t)ne,
  4649. (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,
  4650. (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]),
  4651. 0,
  4652. 0.0f, 0.0f,
  4653. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4654. };
  4655. init_pushconst_fastdiv(pc);
  4656. ggml_vk_sync_buffers(subctx);
  4657. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4658. }
  4659. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  4660. switch(type) {
  4661. case GGML_TYPE_Q8_1:
  4662. return ctx->device->pipeline_quantize_q8_1;
  4663. default:
  4664. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4665. GGML_ABORT("fatal error");
  4666. }
  4667. }
  4668. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  4669. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4670. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4671. ggml_vk_sync_buffers(subctx);
  4672. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4673. }
  4674. 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) {
  4675. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << ggml_type_name(src0->type) << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4676. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << ggml_type_name(src1->type) << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  4677. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << ggml_type_name(dst->type) << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  4678. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4679. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4680. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4681. const uint64_t ne00 = src0->ne[0];
  4682. const uint64_t ne01 = src0->ne[1];
  4683. const uint64_t ne02 = src0->ne[2];
  4684. const uint64_t ne03 = src0->ne[3];
  4685. const uint64_t ne10 = src1->ne[0];
  4686. const uint64_t ne11 = src1->ne[1];
  4687. const uint64_t ne12 = src1->ne[2];
  4688. const uint64_t ne13 = src1->ne[3];
  4689. const uint64_t ne20 = dst->ne[0];
  4690. const uint64_t ne21 = dst->ne[1];
  4691. const uint64_t r2 = ne12 / ne02;
  4692. const uint64_t r3 = ne13 / ne03;
  4693. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4694. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4695. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4696. vk_buffer d_Qx = nullptr;
  4697. size_t qx_buf_offset = 0;
  4698. vk_buffer d_Qy = nullptr;
  4699. size_t qy_buf_offset = 0;
  4700. bool src0_uma = false;
  4701. bool src1_uma = false;
  4702. if (ctx->device->uma) {
  4703. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4704. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4705. src0_uma = d_Qx != nullptr;
  4706. src1_uma = d_Qy != nullptr;
  4707. }
  4708. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4709. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4710. !ggml_vk_dim01_contiguous(src0);
  4711. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4712. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4713. !ggml_vk_dim01_contiguous(src1);
  4714. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4715. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4716. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4717. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4718. // Check for mmq first
  4719. vk_matmul_pipeline mmp = quantize_y ? ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, GGML_TYPE_Q8_1, (ggml_prec)dst->op_params[0]) : nullptr;
  4720. if (mmp == nullptr) {
  4721. // Fall back to f16 dequant mul mat
  4722. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4723. quantize_y = false;
  4724. }
  4725. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4726. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4727. if (qx_needs_dequant) {
  4728. // Fall back to dequant + f16 mulmat
  4729. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, f16_type, y_f32_kernel ? GGML_TYPE_F32 : f16_type, (ggml_prec)dst->op_params[0]);
  4730. }
  4731. // Not implemented
  4732. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4733. const uint32_t kpad = quantize_y ? 0 : ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11, qx_needs_dequant ? f16_type : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type)));
  4734. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4735. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? f16_type : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type));
  4736. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4737. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4738. const int x_ne = ne01 * ne00;
  4739. const int y_ne = padded_n * ne10;
  4740. const int d_ne = ne11 * ne01;
  4741. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4742. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4743. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4744. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4745. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  4746. const uint64_t d_sz = sizeof(float) * d_ne;
  4747. vk_pipeline to_fp16_vk_0 = nullptr;
  4748. vk_pipeline to_fp16_vk_1 = nullptr;
  4749. vk_pipeline to_q8_1 = nullptr;
  4750. if (x_non_contig) {
  4751. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4752. } else {
  4753. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4754. }
  4755. if (y_non_contig) {
  4756. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4757. } else {
  4758. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4759. }
  4760. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4761. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4762. if (quantize_y) {
  4763. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4764. }
  4765. if (dryrun) {
  4766. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4767. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4768. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4769. if (
  4770. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4771. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4772. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4773. GGML_ABORT("Requested preallocation size is too large");
  4774. }
  4775. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4776. ctx->prealloc_size_x = x_sz_upd;
  4777. }
  4778. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  4779. ctx->prealloc_size_y = y_sz_upd;
  4780. }
  4781. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  4782. ctx->prealloc_size_split_k = split_k_size;
  4783. }
  4784. // Request descriptor sets
  4785. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4786. if (qx_needs_dequant) {
  4787. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4788. }
  4789. if (qy_needs_dequant) {
  4790. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4791. }
  4792. if (quantize_y) {
  4793. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  4794. }
  4795. if (split_k > 1) {
  4796. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  4797. }
  4798. return;
  4799. }
  4800. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4801. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4802. GGML_ASSERT(d_D != nullptr);
  4803. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  4804. vk_buffer d_X;
  4805. uint64_t x_buf_offset = 0;
  4806. vk_buffer d_Y;
  4807. uint64_t y_buf_offset = 0;
  4808. if (!src0_uma) {
  4809. d_Qx = src0_buf_ctx->dev_buffer;
  4810. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4811. GGML_ASSERT(d_Qx != nullptr);
  4812. }
  4813. if (!src1_uma) {
  4814. d_Qy = src1_buf_ctx->dev_buffer;
  4815. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4816. GGML_ASSERT(d_Qy != nullptr);
  4817. }
  4818. if (qx_needs_dequant) {
  4819. d_X = ctx->prealloc_x;
  4820. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4821. } else {
  4822. d_X = d_Qx;
  4823. x_buf_offset = qx_buf_offset;
  4824. GGML_ASSERT(qx_sz == x_sz);
  4825. }
  4826. if (qy_needs_dequant) {
  4827. d_Y = ctx->prealloc_y;
  4828. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4829. } else if (quantize_y) {
  4830. d_Y = ctx->prealloc_y;
  4831. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  4832. } else {
  4833. d_Y = d_Qy;
  4834. y_buf_offset = qy_buf_offset;
  4835. GGML_ASSERT(qy_sz == y_sz);
  4836. }
  4837. if (x_non_contig) {
  4838. 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 });
  4839. } else if (qx_needs_dequant) {
  4840. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4841. ggml_vk_sync_buffers(subctx);
  4842. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc, { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  4843. }
  4844. if (y_non_contig) {
  4845. 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 });
  4846. }
  4847. if (quantize_y) {
  4848. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13);
  4849. }
  4850. uint32_t stride_batch_x = ne00*ne01;
  4851. uint32_t stride_batch_y = ne10*ne11;
  4852. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4853. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4854. }
  4855. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  4856. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4857. }
  4858. // compute
  4859. ggml_vk_matmul(
  4860. ctx, subctx, pipeline,
  4861. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4862. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  4863. ne01, ne11, ne10,
  4864. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  4865. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  4866. ); // NOLINT
  4867. }
  4868. 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) {
  4869. 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];
  4870. 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];
  4871. 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];
  4872. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  4873. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4874. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4875. const uint64_t ne00 = src0->ne[0];
  4876. const uint64_t ne01 = src0->ne[1];
  4877. const uint64_t ne02 = src0->ne[2];
  4878. const uint64_t ne03 = src0->ne[3];
  4879. const uint64_t ne10 = src1->ne[0];
  4880. const uint64_t ne11 = src1->ne[1];
  4881. const uint64_t ne12 = src1->ne[2];
  4882. const uint64_t ne13 = src1->ne[3];
  4883. const uint64_t ne20 = dst->ne[0];
  4884. const uint64_t ne21 = dst->ne[1];
  4885. const uint64_t ne22 = dst->ne[2];
  4886. const uint64_t ne23 = dst->ne[3];
  4887. const uint64_t r2 = ne12 / ne02;
  4888. const uint64_t r3 = ne13 / ne03;
  4889. // batch_n indicates that we need to compute a few vector results, and this assumes
  4890. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4891. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4892. bool batch_n = ne11 > 1;
  4893. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4894. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4895. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4896. vk_buffer d_Qx = nullptr;
  4897. size_t qx_buf_offset = 0;
  4898. vk_buffer d_Qy = nullptr;
  4899. size_t qy_buf_offset = 0;
  4900. bool src0_uma = false;
  4901. bool src1_uma = false;
  4902. if (ctx->device->uma) {
  4903. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4904. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4905. src0_uma = d_Qx != nullptr;
  4906. src1_uma = d_Qy != nullptr;
  4907. }
  4908. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4909. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4910. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4911. const bool qx_needs_dequant = x_non_contig;
  4912. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4913. // Not implemented
  4914. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4915. const uint64_t x_ne = ne01 * ne00;
  4916. const uint64_t y_ne = ne11 * ne10;
  4917. const uint64_t d_ne = ne11 * ne01;
  4918. 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);
  4919. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4920. 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;
  4921. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4922. const uint64_t d_sz = sizeof(float) * d_ne;
  4923. vk_pipeline to_fp16_vk_0 = nullptr;
  4924. vk_pipeline to_fp16_vk_1 = nullptr;
  4925. if (x_non_contig) {
  4926. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4927. }
  4928. if (y_non_contig) {
  4929. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4930. } else {
  4931. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4932. }
  4933. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11, ne20, ne00);
  4934. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4935. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4936. GGML_ASSERT(dmmv != nullptr);
  4937. if (dryrun) {
  4938. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4939. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4940. if (
  4941. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4942. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4943. GGML_ABORT("Requested preallocation size is too large");
  4944. }
  4945. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4946. ctx->prealloc_size_x = x_sz_upd;
  4947. }
  4948. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4949. ctx->prealloc_size_y = y_sz_upd;
  4950. }
  4951. // Request descriptor sets
  4952. if (qx_needs_dequant) {
  4953. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4954. }
  4955. if (qy_needs_dequant) {
  4956. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4957. }
  4958. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  4959. return;
  4960. }
  4961. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4962. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4963. GGML_ASSERT(d_D != nullptr);
  4964. vk_buffer d_X;
  4965. uint64_t x_buf_offset = 0;
  4966. vk_buffer d_Y;
  4967. uint64_t y_buf_offset = 0;
  4968. if(!src0_uma) {
  4969. d_Qx = src0_buf_ctx->dev_buffer;
  4970. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4971. GGML_ASSERT(d_Qx != nullptr);
  4972. }
  4973. if(!src1_uma) {
  4974. d_Qy = src1_buf_ctx->dev_buffer;
  4975. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4976. GGML_ASSERT(d_Qy != nullptr);
  4977. }
  4978. if (qx_needs_dequant) {
  4979. d_X = ctx->prealloc_x;
  4980. } else {
  4981. d_X = d_Qx;
  4982. x_buf_offset = qx_buf_offset;
  4983. GGML_ASSERT(qx_sz == x_sz);
  4984. }
  4985. if (qy_needs_dequant) {
  4986. d_Y = ctx->prealloc_y;
  4987. } else {
  4988. d_Y = d_Qy;
  4989. y_buf_offset = qy_buf_offset;
  4990. GGML_ASSERT(qy_sz == y_sz);
  4991. }
  4992. if (x_non_contig) {
  4993. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4994. 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 });
  4995. }
  4996. if (y_non_contig) {
  4997. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4998. 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 });
  4999. }
  5000. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  5001. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  5002. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  5003. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  5004. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5005. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5006. }
  5007. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5008. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5009. }
  5010. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5011. uint32_t groups_x = ne01;
  5012. uint32_t groups_z = 1;
  5013. if (ne01 > max_groups_x) {
  5014. groups_z = 64;
  5015. groups_x = CEIL_DIV(groups_x, groups_z);
  5016. }
  5017. // compute
  5018. const vk_mat_vec_push_constants pc = {
  5019. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5020. stride_batch_x, stride_batch_y, stride_batch_d,
  5021. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  5022. };
  5023. ggml_vk_sync_buffers(subctx);
  5024. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5025. { 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} },
  5026. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  5027. }
  5028. 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) {
  5029. 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];
  5030. 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];
  5031. 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];
  5032. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5033. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  5034. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  5035. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  5036. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5037. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5038. const uint64_t ne00 = src0->ne[0];
  5039. const uint64_t ne01 = src0->ne[1];
  5040. const uint64_t ne02 = src0->ne[2];
  5041. // const uint64_t ne03 = src0->ne[3];
  5042. const uint64_t ne10 = src1->ne[0];
  5043. const uint64_t ne11 = src1->ne[1];
  5044. const uint64_t ne12 = src1->ne[2];
  5045. // const uint64_t ne13 = src1->ne[3];
  5046. GGML_ASSERT(ne11 == 1);
  5047. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5048. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5049. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5050. vk_buffer d_Qy = nullptr;
  5051. size_t qy_buf_offset = 0;
  5052. bool src1_uma = false;
  5053. if (ctx->device->uma) {
  5054. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5055. src1_uma = d_Qy != nullptr;
  5056. }
  5057. const uint64_t x_ne = ne00 * ne01 * ne02;
  5058. const uint64_t y_ne = ne10 * ne11 * ne12;
  5059. const uint64_t d_ne = ne01 * ne11 * ne12;
  5060. 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);
  5061. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5062. const uint64_t d_sz = sizeof(float) * d_ne;
  5063. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  5064. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  5065. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  5066. gqa_ratio = 1;
  5067. }
  5068. if (dryrun) {
  5069. // Request descriptor sets
  5070. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  5071. return;
  5072. }
  5073. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5074. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5075. GGML_ASSERT(d_D != nullptr);
  5076. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5077. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5078. GGML_ASSERT(d_Qx != nullptr);
  5079. if (!src1_uma) {
  5080. d_Qy = src1_buf_ctx->dev_buffer;
  5081. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5082. GGML_ASSERT(d_Qx != nullptr);
  5083. }
  5084. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5085. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5086. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5087. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5088. // compute
  5089. 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)) };
  5090. uint32_t workgroups_z = (uint32_t)ne12;
  5091. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  5092. if (gqa_ratio > 1) {
  5093. workgroups_z /= gqa_ratio;
  5094. }
  5095. ggml_vk_sync_buffers(subctx);
  5096. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, pc, { 1, (uint32_t)ne01, workgroups_z });
  5097. }
  5098. 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) {
  5099. 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];
  5100. 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];
  5101. 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];
  5102. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5103. GGML_ASSERT(!ggml_is_transposed(src0));
  5104. GGML_ASSERT(!ggml_is_transposed(src1));
  5105. GGML_ASSERT(!ggml_is_permuted(src0));
  5106. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5107. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5108. const uint64_t ne00 = src0->ne[0];
  5109. const uint64_t ne01 = src0->ne[1];
  5110. const uint64_t ne02 = src0->ne[2];
  5111. const uint64_t ne03 = src0->ne[3];
  5112. const uint64_t nb01 = src0->nb[1];
  5113. const uint64_t nb02 = src0->nb[2];
  5114. const uint64_t nb12 = src1->nb[2];
  5115. // const uint64_t ne10 = src1->ne[0];
  5116. const uint64_t ne11 = src1->ne[1];
  5117. const uint64_t ne12 = src1->ne[2];
  5118. // const uint64_t ne13 = src1->ne[3];
  5119. const uint32_t nb03 = (uint32_t)(src0->nb[3] / sizeof(ggml_fp16_t));
  5120. const uint32_t nb13 = (uint32_t)(src1->nb[3] / sizeof(float));
  5121. const uint32_t nb23 = (uint32_t)(dst->nb[3] / sizeof(float));
  5122. GGML_ASSERT(ne11 == 1);
  5123. GGML_ASSERT(src0->ne[3] == src1->ne[3]); // checked in supports_op
  5124. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5125. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5126. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5127. vk_buffer d_Qy = nullptr;
  5128. size_t qy_buf_offset = 0;
  5129. bool src1_uma = false;
  5130. if (ctx->device->uma) {
  5131. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5132. src1_uma = d_Qy != nullptr;
  5133. }
  5134. const uint64_t d_ne = ne01 * ne11 * ne12 * ne03;
  5135. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  5136. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  5137. const uint32_t channel_stride_y = nb12 / sizeof(float);
  5138. const uint64_t qx_sz = ggml_nbytes(src0);
  5139. const uint64_t qy_sz = ggml_nbytes(src1);
  5140. const uint64_t d_sz = sizeof(float) * d_ne;
  5141. if (dryrun) {
  5142. // Request descriptor sets
  5143. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  5144. return;
  5145. }
  5146. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5147. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5148. GGML_ASSERT(d_D != nullptr);
  5149. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5150. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5151. GGML_ASSERT(d_Qx != nullptr);
  5152. if (!src1_uma) {
  5153. d_Qy = src1_buf_ctx->dev_buffer;
  5154. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5155. GGML_ASSERT(d_Qx != nullptr);
  5156. }
  5157. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5158. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5159. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5160. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5161. // compute
  5162. const std::array<uint32_t, 12> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, channel_stride_y, (uint32_t)(ne12 / ne02), (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)), nb03, nb13, nb23 };
  5163. ggml_vk_sync_buffers(subctx);
  5164. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  5165. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, pc, { (uint32_t)ne03, (uint32_t)ne01, (uint32_t)ne12 });
  5166. }
  5167. 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) {
  5168. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  5169. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  5170. // detect 0213 permutation, and batch size of 1
  5171. src0->nb[0] <= src0->nb[2] &&
  5172. src0->nb[2] <= src0->nb[1] &&
  5173. src0->nb[1] <= src0->nb[3] &&
  5174. src1->nb[0] <= src1->nb[2] &&
  5175. src1->nb[2] <= src1->nb[1] &&
  5176. src1->nb[1] <= src1->nb[3] &&
  5177. src0->ne[3] == 1 &&
  5178. src1->ne[3] == 1) {
  5179. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5180. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  5181. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  5182. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5183. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  5184. // when ne12 and ne13 are one.
  5185. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  5186. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  5187. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5188. } else {
  5189. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5190. }
  5191. }
  5192. 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) {
  5193. 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];
  5194. 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];
  5195. 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];
  5196. 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] << "),)");
  5197. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5198. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5199. const uint64_t ne00 = src0->ne[0];
  5200. const uint64_t ne01 = src0->ne[1];
  5201. const uint64_t ne02 = src0->ne[2];
  5202. const uint64_t ne03 = src0->ne[3];
  5203. const uint64_t ne10 = src1->ne[0];
  5204. const uint64_t ne11 = src1->ne[1];
  5205. const uint64_t ne12 = src1->ne[2];
  5206. const uint64_t ne13 = src1->ne[3];
  5207. const uint64_t nei0 = ids->ne[0];
  5208. const uint64_t nei1 = ids->ne[1];
  5209. GGML_ASSERT(nei0 * nei1 <= 4096);
  5210. const uint32_t nbi1 = ids->nb[1];
  5211. const uint32_t nbi2 = ids->nb[2];
  5212. const uint64_t ne20 = dst->ne[0];
  5213. const uint64_t ne21 = dst->ne[1];
  5214. const uint64_t ne22 = dst->ne[2];
  5215. const uint64_t ne23 = dst->ne[3];
  5216. const uint64_t n_as = ne02;
  5217. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5218. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5219. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5220. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5221. vk_buffer d_Qx = nullptr;
  5222. size_t qx_buf_offset = 0;
  5223. vk_buffer d_Qy = nullptr;
  5224. size_t qy_buf_offset = 0;
  5225. vk_buffer d_ids = nullptr;
  5226. size_t ids_buf_offset = 0;
  5227. bool src0_uma = false;
  5228. bool src1_uma = false;
  5229. bool ids_uma = false;
  5230. if (ctx->device->uma) {
  5231. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5232. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5233. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5234. src0_uma = d_Qx != nullptr;
  5235. src1_uma = d_Qy != nullptr;
  5236. ids_uma = d_ids != nullptr;
  5237. }
  5238. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  5239. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  5240. !ggml_vk_dim01_contiguous(src0);
  5241. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  5242. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  5243. !ggml_vk_dim01_contiguous(src1);
  5244. // If src0 is BF16, try to use a BF16 x BF16 multiply
  5245. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  5246. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  5247. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  5248. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  5249. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  5250. if (qx_needs_dequant) {
  5251. // Fall back to dequant + f16 mulmat
  5252. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, f16_type, y_f32_kernel ? GGML_TYPE_F32 : f16_type, (ggml_prec)dst->op_params[0]);
  5253. }
  5254. // Not implemented
  5255. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5256. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? f16_type : src0->type));
  5257. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  5258. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  5259. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  5260. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  5261. const uint64_t x_ne = ne01 * ne00;
  5262. const uint64_t y_ne = padded_n * ne10;
  5263. const uint64_t d_ne = ne21 * ne20;
  5264. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  5265. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5266. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  5267. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5268. const uint64_t ids_sz = nbi2;
  5269. const uint64_t d_sz = sizeof(float) * d_ne;
  5270. vk_pipeline to_fp16_vk_0 = nullptr;
  5271. vk_pipeline to_fp16_vk_1 = nullptr;
  5272. if (x_non_contig) {
  5273. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  5274. } else {
  5275. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  5276. }
  5277. if (y_non_contig) {
  5278. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  5279. } else {
  5280. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5281. }
  5282. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5283. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5284. if (dryrun) {
  5285. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5286. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5287. if (
  5288. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5289. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5290. GGML_ABORT("Requested preallocation size is too large");
  5291. }
  5292. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5293. ctx->prealloc_size_x = x_sz_upd;
  5294. }
  5295. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5296. ctx->prealloc_size_y = y_sz_upd;
  5297. }
  5298. // Request descriptor sets
  5299. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5300. if (qx_needs_dequant) {
  5301. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5302. }
  5303. if (qy_needs_dequant) {
  5304. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5305. }
  5306. return;
  5307. }
  5308. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5309. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5310. GGML_ASSERT(d_D != nullptr);
  5311. vk_buffer d_X;
  5312. uint64_t x_buf_offset = 0;
  5313. vk_buffer d_Y;
  5314. uint64_t y_buf_offset = 0;
  5315. if (!src0_uma) {
  5316. d_Qx = src0_buf_ctx->dev_buffer;
  5317. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5318. GGML_ASSERT(d_Qx != nullptr);
  5319. }
  5320. if (!src1_uma) {
  5321. d_Qy = src1_buf_ctx->dev_buffer;
  5322. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5323. GGML_ASSERT(d_Qy != nullptr);
  5324. }
  5325. if (!ids_uma) {
  5326. d_ids = ids_buf_ctx->dev_buffer;
  5327. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5328. GGML_ASSERT(d_ids != nullptr);
  5329. }
  5330. if (qx_needs_dequant) {
  5331. d_X = ctx->prealloc_x;
  5332. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  5333. } else {
  5334. d_X = d_Qx;
  5335. x_buf_offset = qx_buf_offset;
  5336. GGML_ASSERT(qx_sz == x_sz);
  5337. }
  5338. if (qy_needs_dequant) {
  5339. d_Y = ctx->prealloc_y;
  5340. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5341. } else {
  5342. d_Y = d_Qy;
  5343. y_buf_offset = qy_buf_offset;
  5344. GGML_ASSERT(qy_sz == y_sz);
  5345. }
  5346. if (x_non_contig) {
  5347. 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 });
  5348. } else if (qx_needs_dequant) {
  5349. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5350. ggml_vk_sync_buffers(subctx);
  5351. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  5352. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc, { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  5353. }
  5354. if (y_non_contig) {
  5355. 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 });
  5356. }
  5357. uint32_t stride_batch_x = ne00*ne01;
  5358. uint32_t stride_batch_y = ne10*ne11;
  5359. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5360. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5361. }
  5362. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5363. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5364. }
  5365. // compute
  5366. ggml_vk_matmul_id(
  5367. ctx, subctx, pipeline,
  5368. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  5369. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  5370. ne01, ne21, ne10, ne10, ne10, ne01,
  5371. stride_batch_x, stride_batch_y, ne20*ne21,
  5372. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  5373. ); // NOLINT
  5374. }
  5375. 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) {
  5376. 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];
  5377. 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];
  5378. 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];
  5379. 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];
  5380. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5381. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5382. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5383. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5384. const uint64_t ne00 = src0->ne[0];
  5385. const uint64_t ne01 = src0->ne[1];
  5386. const uint64_t ne02 = src0->ne[2];
  5387. const uint64_t ne03 = src0->ne[3];
  5388. const uint64_t ne10 = src1->ne[0];
  5389. const uint64_t ne11 = src1->ne[1];
  5390. const uint64_t ne12 = src1->ne[2];
  5391. const uint64_t ne13 = src1->ne[3];
  5392. const uint64_t nei0 = ids->ne[0];
  5393. const uint64_t nei1 = ids->ne[1];
  5394. const uint64_t nbi2 = ids->nb[2];
  5395. GGML_ASSERT(nei1 == 1);
  5396. const uint64_t ne20 = dst->ne[0];
  5397. const uint64_t ne21 = dst->ne[1];
  5398. const uint64_t ne22 = dst->ne[2];
  5399. const uint64_t ne23 = dst->ne[3];
  5400. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5401. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5402. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5403. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5404. vk_buffer d_Qx = nullptr;
  5405. size_t qx_buf_offset = 0;
  5406. vk_buffer d_Qy = nullptr;
  5407. size_t qy_buf_offset = 0;
  5408. vk_buffer d_ids = nullptr;
  5409. size_t ids_buf_offset = 0;
  5410. bool src0_uma = false;
  5411. bool src1_uma = false;
  5412. bool ids_uma = false;
  5413. if (ctx->device->uma) {
  5414. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5415. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5416. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5417. src0_uma = d_Qx != nullptr;
  5418. src1_uma = d_Qy != nullptr;
  5419. ids_uma = d_ids != nullptr;
  5420. }
  5421. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5422. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5423. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5424. const bool qx_needs_dequant = x_non_contig;
  5425. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5426. // Not implemented
  5427. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5428. const uint64_t x_ne = ne01 * ne00;
  5429. const uint64_t y_ne = ne11 * ne10;
  5430. const uint64_t d_ne = ne21 * ne20;
  5431. 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);
  5432. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5433. 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;
  5434. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5435. const uint64_t ids_sz = nbi2;
  5436. const uint64_t d_sz = sizeof(float) * d_ne;
  5437. vk_pipeline to_fp16_vk_0 = nullptr;
  5438. vk_pipeline to_fp16_vk_1 = nullptr;
  5439. if (x_non_contig) {
  5440. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5441. }
  5442. if (y_non_contig) {
  5443. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5444. } else {
  5445. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5446. }
  5447. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5448. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5449. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5450. GGML_ASSERT(dmmv != nullptr);
  5451. if (dryrun) {
  5452. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5453. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5454. if (
  5455. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5456. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5457. GGML_ABORT("Requested preallocation size is too large");
  5458. }
  5459. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5460. ctx->prealloc_size_x = x_sz_upd;
  5461. }
  5462. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5463. ctx->prealloc_size_y = y_sz_upd;
  5464. }
  5465. // Request descriptor sets
  5466. if (qx_needs_dequant) {
  5467. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5468. }
  5469. if (qy_needs_dequant) {
  5470. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5471. }
  5472. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5473. return;
  5474. }
  5475. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5476. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5477. GGML_ASSERT(d_D != nullptr);
  5478. vk_buffer d_X;
  5479. uint64_t x_buf_offset = 0;
  5480. vk_buffer d_Y;
  5481. uint64_t y_buf_offset = 0;
  5482. if(!src0_uma) {
  5483. d_Qx = src0_buf_ctx->dev_buffer;
  5484. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5485. GGML_ASSERT(d_Qx != nullptr);
  5486. }
  5487. if(!src1_uma) {
  5488. d_Qy = src1_buf_ctx->dev_buffer;
  5489. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5490. GGML_ASSERT(d_Qy != nullptr);
  5491. }
  5492. if(!ids_uma) {
  5493. d_ids = ids_buf_ctx->dev_buffer;
  5494. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5495. GGML_ASSERT(d_ids != nullptr);
  5496. }
  5497. if (qx_needs_dequant) {
  5498. d_X = ctx->prealloc_x;
  5499. } else {
  5500. d_X = d_Qx;
  5501. x_buf_offset = qx_buf_offset;
  5502. GGML_ASSERT(qx_sz == x_sz);
  5503. }
  5504. if (qy_needs_dequant) {
  5505. d_Y = ctx->prealloc_y;
  5506. } else {
  5507. d_Y = d_Qy;
  5508. y_buf_offset = qy_buf_offset;
  5509. GGML_ASSERT(qy_sz == y_sz);
  5510. }
  5511. if (x_non_contig) {
  5512. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5513. 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 });
  5514. }
  5515. if (y_non_contig) {
  5516. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5517. 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 });
  5518. }
  5519. uint32_t stride_batch_y = ne10*ne11;
  5520. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5521. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5522. }
  5523. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5524. uint32_t groups_x = ne01;
  5525. uint32_t groups_z = 1;
  5526. if (ne01 > max_groups_x) {
  5527. groups_z = 64;
  5528. groups_x = CEIL_DIV(groups_x, groups_z);
  5529. }
  5530. // compute
  5531. const vk_mat_vec_id_push_constants pc = {
  5532. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5533. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5534. (uint32_t)nei0, (uint32_t)ne11,
  5535. };
  5536. ggml_vk_sync_buffers(subctx);
  5537. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5538. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5539. 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 } },
  5540. pc, { groups_x, (uint32_t)nei0, groups_z });
  5541. }
  5542. 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) {
  5543. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5544. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5545. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5546. } else {
  5547. // Split based on number of ids, to fit in shared memory
  5548. const uint32_t nei0 = (uint32_t)src2->ne[0];
  5549. const uint32_t nei1 = (uint32_t)src2->ne[1];
  5550. GGML_ASSERT(nei0 <= 4096);
  5551. const uint32_t split_size = std::min(nei1, 4096u / nei0);
  5552. ggml_tensor src1_copy = *src1;
  5553. ggml_tensor src2_copy = *src2;
  5554. ggml_tensor dst_copy = *dst;
  5555. for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
  5556. const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
  5557. src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
  5558. src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
  5559. dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
  5560. src1_copy.ne[2] = n_tokens;
  5561. src2_copy.ne[1] = n_tokens;
  5562. dst_copy.ne[2] = n_tokens;
  5563. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
  5564. }
  5565. }
  5566. }
  5567. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5568. // Needs to be kept up to date on shader changes
  5569. GGML_UNUSED(hsv);
  5570. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5571. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  5572. const uint32_t Bc = scalar_flash_attention_Bc;
  5573. const uint32_t tmpsh = wg_size * sizeof(float);
  5574. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5575. const uint32_t masksh = Bc * Br * sizeof(float);
  5576. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5577. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5578. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5579. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5580. return supported;
  5581. }
  5582. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5583. // Needs to be kept up to date on shader changes
  5584. GGML_UNUSED(hsv);
  5585. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5586. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5587. const uint32_t Bc = scalar_flash_attention_Bc;
  5588. const uint32_t acctype = f32acc ? 4 : 2;
  5589. const uint32_t f16vec4 = 8;
  5590. const uint32_t tmpsh = wg_size * sizeof(float);
  5591. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5592. const uint32_t Qf = Br * (hsk / 4 + 2) * f16vec4;
  5593. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5594. const uint32_t sfsh = Bc * sfshstride * acctype;
  5595. const uint32_t kshstride = hsk / 4 + 2;
  5596. const uint32_t ksh = Bc * kshstride * f16vec4;
  5597. const uint32_t slope = Br * sizeof(float);
  5598. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5599. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5600. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5601. return supported;
  5602. }
  5603. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, const ggml_tensor * sinks, ggml_tensor * dst, bool dryrun = false) {
  5604. 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];
  5605. 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];
  5606. 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];
  5607. 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];
  5608. if (sinks) {
  5609. std::cerr << "), (" << sinks << ", name=" << sinks->name << ", type=" << sinks->type << ", ne0=" << sinks->ne[0] << ", ne1=" << sinks->ne[1] << ", ne2=" << sinks->ne[2] << ", ne3=" << sinks->ne[3] << ", nb0=" << sinks->nb[0] << ", nb1=" << sinks->nb[1] << ", nb2=" << sinks->nb[2] << ", nb3=" << sinks->nb[3];
  5610. }
  5611. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5612. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5613. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5614. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5615. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5616. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5617. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5618. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5619. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5620. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5621. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5622. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  5623. const uint32_t HSK = nek0;
  5624. const uint32_t HSV = nev0;
  5625. uint32_t N = neq1;
  5626. const uint32_t KV = nek1;
  5627. GGML_ASSERT(ne0 == HSV);
  5628. GGML_ASSERT(ne2 == N);
  5629. // input tensor rows must be contiguous
  5630. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  5631. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  5632. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  5633. GGML_ASSERT(neq0 == HSK);
  5634. GGML_ASSERT(neq1 == N);
  5635. GGML_ASSERT(nev1 == nek1);
  5636. // dst cannot be transposed or permuted
  5637. GGML_ASSERT(nb0 == sizeof(float));
  5638. GGML_ASSERT(nb0 <= nb1);
  5639. GGML_ASSERT(nb1 <= nb2);
  5640. GGML_ASSERT(nb2 <= nb3);
  5641. assert(dst->type == GGML_TYPE_F32);
  5642. assert(q->type == GGML_TYPE_F32);
  5643. assert(k->type == v->type);
  5644. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  5645. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  5646. if (path == FA_COOPMAT1) {
  5647. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  5648. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  5649. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  5650. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  5651. path = FA_SCALAR;
  5652. }
  5653. }
  5654. uint32_t gqa_ratio = 1;
  5655. uint32_t qk_ratio = neq2 / nek2;
  5656. uint32_t workgroups_x = (uint32_t)neq1;
  5657. uint32_t workgroups_y = (uint32_t)neq2;
  5658. uint32_t workgroups_z = (uint32_t)neq3;
  5659. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  5660. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  5661. uint32_t max_gqa;
  5662. switch (path) {
  5663. case FA_SCALAR:
  5664. case FA_COOPMAT1:
  5665. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  5666. max_gqa = get_fa_scalar_num_large_rows(HSV);
  5667. break;
  5668. case FA_COOPMAT2:
  5669. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  5670. break;
  5671. default:
  5672. GGML_ASSERT(0);
  5673. }
  5674. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  5675. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  5676. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  5677. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  5678. // and change addressing calculations to index Q's dimension 2.
  5679. gqa_ratio = qk_ratio;
  5680. N = gqa_ratio;
  5681. workgroups_y /= N;
  5682. }
  5683. vk_pipeline *pipelines;
  5684. bool small_rows = N <= get_fa_num_small_rows(path);
  5685. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  5686. // So use scalar instead.
  5687. if (small_rows && path == FA_COOPMAT1) {
  5688. path = FA_SCALAR;
  5689. }
  5690. // scalar is faster than coopmat2 when N==1
  5691. if (N == 1 && path == FA_COOPMAT2) {
  5692. path = FA_SCALAR;
  5693. }
  5694. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  5695. if (path == FA_SCALAR &&
  5696. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  5697. small_rows = true;
  5698. }
  5699. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  5700. FaHeadSizes head_sizes = fa_get_head_sizes(k->ne[0], v->ne[0]);
  5701. switch (path) {
  5702. case FA_SCALAR:
  5703. pipelines = &ctx->device->pipeline_flash_attn_f32_f16[k->type][head_sizes][f32acc][small_rows][0];
  5704. break;
  5705. case FA_COOPMAT1:
  5706. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm1[k->type][head_sizes][f32acc][small_rows][0];
  5707. break;
  5708. case FA_COOPMAT2:
  5709. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm2[k->type][head_sizes][f32acc][small_rows][0];
  5710. break;
  5711. default:
  5712. GGML_ASSERT(0);
  5713. }
  5714. assert(pipelines);
  5715. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  5716. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  5717. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  5718. bool aligned = (KV % pipelines[1]->align) == 0 &&
  5719. // the "aligned" shader variant will forcibly align strides, for performance
  5720. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  5721. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  5722. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  5723. vk_pipeline pipeline = pipelines[aligned];
  5724. assert(pipeline);
  5725. uint32_t split_kv = KV;
  5726. uint32_t split_k = 1;
  5727. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  5728. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  5729. // Try to use split_k when KV is large enough to be worth the overhead
  5730. if (workgroups_x == 1 && shader_core_count > 0) {
  5731. // Try to run two workgroups per SM.
  5732. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  5733. if (split_k > 1) {
  5734. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  5735. // of "align", so recompute split_k based on that.
  5736. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), pipelines[1]->align);
  5737. split_k = CEIL_DIV(KV, split_kv);
  5738. workgroups_x = split_k;
  5739. }
  5740. }
  5741. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  5742. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  5743. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  5744. if (split_k_size > ctx->device->max_memory_allocation_size) {
  5745. GGML_ABORT("Requested preallocation size is too large");
  5746. }
  5747. if (ctx->prealloc_size_split_k < split_k_size) {
  5748. ctx->prealloc_size_split_k = split_k_size;
  5749. }
  5750. if (dryrun) {
  5751. // Request descriptor sets
  5752. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5753. if (split_k > 1) {
  5754. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  5755. }
  5756. return;
  5757. }
  5758. float scale = 1.0f;
  5759. float max_bias = 0.0f;
  5760. float logit_softcap = 0.0f;
  5761. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  5762. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  5763. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  5764. if (logit_softcap != 0) {
  5765. scale /= logit_softcap;
  5766. }
  5767. const uint32_t n_head_kv = neq2;
  5768. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5769. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5770. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5771. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr, d_S = nullptr;
  5772. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0, s_buf_offset = 0;
  5773. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false, S_uma = false;
  5774. if (ctx->device->uma) {
  5775. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  5776. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  5777. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  5778. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  5779. Q_uma = d_Q != nullptr;
  5780. K_uma = d_K != nullptr;
  5781. V_uma = d_V != nullptr;
  5782. D_uma = d_D != nullptr;
  5783. if (mask) {
  5784. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  5785. M_uma = d_M != nullptr;
  5786. }
  5787. if (sinks) {
  5788. ggml_vk_host_get(ctx->device, sinks->data, d_S, s_buf_offset);
  5789. S_uma = d_S != nullptr;
  5790. }
  5791. }
  5792. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5793. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  5794. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  5795. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  5796. if (!Q_uma) {
  5797. d_Q = q_buf_ctx->dev_buffer;
  5798. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  5799. }
  5800. if (!K_uma) {
  5801. d_K = k_buf_ctx->dev_buffer;
  5802. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  5803. }
  5804. if (!V_uma) {
  5805. d_V = v_buf_ctx->dev_buffer;
  5806. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  5807. }
  5808. if (!D_uma) {
  5809. d_D = d_buf_ctx->dev_buffer;
  5810. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5811. }
  5812. if (!M_uma) {
  5813. d_M = d_Q;
  5814. m_buf_offset = q_buf_offset;
  5815. if (mask) {
  5816. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  5817. d_M = m_buf_ctx->dev_buffer;
  5818. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  5819. }
  5820. }
  5821. if (!S_uma) {
  5822. d_S = d_Q;
  5823. s_buf_offset = q_buf_offset;
  5824. if (sinks) {
  5825. ggml_backend_vk_buffer_context * s_buf_ctx = (ggml_backend_vk_buffer_context*)sinks->buffer->context;
  5826. d_S = s_buf_ctx->dev_buffer;
  5827. s_buf_offset = vk_tensor_offset(sinks) + sinks->view_offs;
  5828. }
  5829. }
  5830. uint32_t mask_n_head_log2 = ((sinks != nullptr) << 24) | ((mask != nullptr) << 16) | n_head_log2;
  5831. const vk_flash_attn_push_constants pc = { N, KV,
  5832. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  5833. (uint32_t)neq2, (uint32_t)neq3,
  5834. (uint32_t)nek2, (uint32_t)nek3,
  5835. (uint32_t)nev2, (uint32_t)nev3,
  5836. nem1, nem2, nem3,
  5837. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  5838. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  5839. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  5840. scale, max_bias, logit_softcap,
  5841. mask_n_head_log2, m0, m1,
  5842. gqa_ratio, split_kv, split_k };
  5843. ggml_vk_sync_buffers(subctx);
  5844. if (split_k > 1) {
  5845. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5846. {
  5847. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5848. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5849. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5850. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5851. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  5852. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5853. },
  5854. // We only use split_k when group query attention is enabled, which means
  5855. // there's no more than one tile of rows (i.e. workgroups_x would have been
  5856. // one). We reuse workgroups_x to mean the number of splits, so we need to
  5857. // cancel out the divide by wg_denoms[0].
  5858. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  5859. ggml_vk_sync_buffers(subctx);
  5860. const std::array<uint32_t, 5> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k, (sinks != nullptr) };
  5861. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  5862. {
  5863. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5864. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  5865. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5866. },
  5867. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  5868. } else {
  5869. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5870. {
  5871. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5872. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5873. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5874. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5875. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  5876. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5877. },
  5878. pc, { workgroups_x, workgroups_y, workgroups_z });
  5879. }
  5880. }
  5881. static std::array<uint32_t, 3> ggml_vk_get_conv_elements(const ggml_tensor *dst) {
  5882. const ggml_tensor *src0 = dst->src[0];
  5883. const ggml_tensor *src1 = dst->src[1];
  5884. // src0 - kernel: [KW, KH, Cin, Cout]
  5885. // src1 - input: [W, H, Cin, N]
  5886. // dst - result: [OW, OH, Cout, N]
  5887. // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d)
  5888. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  5889. return (ins + 2 * p - d * (ks - 1) - 1) / s + 1;
  5890. };
  5891. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  5892. int64_t W = src1->ne[0];
  5893. int64_t H = src1->ne[1];
  5894. int64_t KW = src0->ne[0];
  5895. int64_t KH = src0->ne[1];
  5896. int64_t Cout = src0->ne[3];
  5897. int64_t N = src1->ne[3];
  5898. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[1], dst->op_params[3], dst->op_params[5]);
  5899. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], dst->op_params[2], dst->op_params[4]);
  5900. int64_t NPQ = N * OW * OH;
  5901. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  5902. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  5903. return elements;
  5904. }
  5905. 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) {
  5906. switch (op) {
  5907. case GGML_OP_GET_ROWS:
  5908. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5909. if (dst->type == GGML_TYPE_F16) {
  5910. return ctx->device->pipeline_get_rows[src0->type];
  5911. }
  5912. if (dst->type == GGML_TYPE_F32) {
  5913. return ctx->device->pipeline_get_rows_f32[src0->type];
  5914. }
  5915. return nullptr;
  5916. case GGML_OP_ACC:
  5917. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5918. return ctx->device->pipeline_acc_f32;
  5919. }
  5920. return nullptr;
  5921. case GGML_OP_ADD:
  5922. case GGML_OP_SUB:
  5923. case GGML_OP_MUL:
  5924. case GGML_OP_DIV:
  5925. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5926. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  5927. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  5928. return nullptr;
  5929. }
  5930. switch (op) {
  5931. case GGML_OP_ADD:
  5932. {
  5933. if (ctx->num_additional_fused_ops > 0) {
  5934. return ctx->device->pipeline_multi_add[ctx->num_additional_fused_ops];
  5935. }
  5936. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  5937. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5938. }
  5939. case GGML_OP_SUB:
  5940. {
  5941. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  5942. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5943. }
  5944. case GGML_OP_MUL:
  5945. {
  5946. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  5947. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5948. }
  5949. case GGML_OP_DIV:
  5950. {
  5951. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  5952. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5953. }
  5954. default:
  5955. break;
  5956. }
  5957. return nullptr;
  5958. case GGML_OP_ADD_ID:
  5959. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && src2->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32) {
  5960. return ctx->device->pipeline_add_id_f32;
  5961. }
  5962. return nullptr;
  5963. case GGML_OP_CONCAT:
  5964. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5965. return ctx->device->pipeline_concat_f32;
  5966. }
  5967. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5968. return ctx->device->pipeline_concat_f16;
  5969. }
  5970. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  5971. return ctx->device->pipeline_concat_i32;
  5972. }
  5973. return nullptr;
  5974. case GGML_OP_UPSCALE:
  5975. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5976. int mode = ggml_get_op_params_i32(dst, 0);
  5977. switch (mode) {
  5978. case GGML_SCALE_MODE_NEAREST:
  5979. return ctx->device->pipeline_upscale_nearest_f32;
  5980. case GGML_SCALE_MODE_BILINEAR:
  5981. return ctx->device->pipeline_upscale_bilinear_f32;
  5982. case GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS:
  5983. return ctx->device->pipeline_upscale_bilinear_ac_f32;
  5984. }
  5985. }
  5986. return nullptr;
  5987. case GGML_OP_SCALE:
  5988. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5989. return ctx->device->pipeline_scale_f32;
  5990. }
  5991. return nullptr;
  5992. case GGML_OP_SQR:
  5993. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5994. return ctx->device->pipeline_sqr_f32;
  5995. }
  5996. return nullptr;
  5997. case GGML_OP_SQRT:
  5998. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5999. return ctx->device->pipeline_sqrt_f32;
  6000. }
  6001. return nullptr;
  6002. case GGML_OP_SIN:
  6003. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6004. return ctx->device->pipeline_sin_f32;
  6005. }
  6006. return nullptr;
  6007. case GGML_OP_COS:
  6008. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6009. return ctx->device->pipeline_cos_f32;
  6010. }
  6011. return nullptr;
  6012. case GGML_OP_CLAMP:
  6013. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6014. return ctx->device->pipeline_clamp_f32;
  6015. }
  6016. return nullptr;
  6017. case GGML_OP_PAD:
  6018. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6019. return ctx->device->pipeline_pad_f32;
  6020. }
  6021. return nullptr;
  6022. case GGML_OP_ROLL:
  6023. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6024. return ctx->device->pipeline_roll_f32;
  6025. }
  6026. return nullptr;
  6027. case GGML_OP_REPEAT:
  6028. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  6029. return ctx->device->pipeline_repeat_f32;
  6030. }
  6031. return nullptr;
  6032. case GGML_OP_REPEAT_BACK:
  6033. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6034. return ctx->device->pipeline_repeat_back_f32;
  6035. }
  6036. return nullptr;
  6037. case GGML_OP_CPY:
  6038. case GGML_OP_CONT:
  6039. case GGML_OP_DUP:
  6040. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  6041. case GGML_OP_SET_ROWS:
  6042. return ctx->device->pipeline_set_rows[dst->type];
  6043. case GGML_OP_SILU_BACK:
  6044. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6045. return ctx->device->pipeline_silu_back_f32;
  6046. }
  6047. return nullptr;
  6048. case GGML_OP_NORM:
  6049. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6050. return ctx->device->pipeline_norm_f32;
  6051. }
  6052. return nullptr;
  6053. case GGML_OP_GROUP_NORM:
  6054. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6055. return ctx->device->pipeline_group_norm_f32;
  6056. }
  6057. return nullptr;
  6058. case GGML_OP_RMS_NORM:
  6059. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6060. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  6061. }
  6062. return nullptr;
  6063. case GGML_OP_RMS_NORM_BACK:
  6064. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6065. return ctx->device->pipeline_rms_norm_back_f32;
  6066. }
  6067. return nullptr;
  6068. case GGML_OP_L2_NORM:
  6069. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6070. return ctx->device->pipeline_l2_norm_f32;
  6071. }
  6072. return nullptr;
  6073. case GGML_OP_UNARY:
  6074. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6075. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6076. (src0->type != dst->type)) {
  6077. return nullptr;
  6078. }
  6079. switch (ggml_get_unary_op(dst)) {
  6080. case GGML_UNARY_OP_SILU:
  6081. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  6082. case GGML_UNARY_OP_GELU:
  6083. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  6084. case GGML_UNARY_OP_GELU_ERF:
  6085. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  6086. case GGML_UNARY_OP_GELU_QUICK:
  6087. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  6088. case GGML_UNARY_OP_RELU:
  6089. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  6090. case GGML_UNARY_OP_TANH:
  6091. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  6092. case GGML_UNARY_OP_SIGMOID:
  6093. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  6094. default:
  6095. break;
  6096. }
  6097. return nullptr;
  6098. case GGML_OP_GLU:
  6099. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6100. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6101. (src0->type != dst->type)) {
  6102. return nullptr;
  6103. }
  6104. switch (ggml_get_glu_op(dst)) {
  6105. case GGML_GLU_OP_GEGLU:
  6106. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  6107. case GGML_GLU_OP_REGLU:
  6108. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  6109. case GGML_GLU_OP_SWIGLU:
  6110. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  6111. case GGML_GLU_OP_SWIGLU_OAI:
  6112. return ctx->device->pipeline_swiglu_oai[dst->type == GGML_TYPE_F16];
  6113. case GGML_GLU_OP_GEGLU_ERF:
  6114. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  6115. case GGML_GLU_OP_GEGLU_QUICK:
  6116. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  6117. default:
  6118. break;
  6119. }
  6120. return nullptr;
  6121. case GGML_OP_DIAG_MASK_INF:
  6122. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6123. return ctx->device->pipeline_diag_mask_inf_f32;
  6124. }
  6125. return nullptr;
  6126. case GGML_OP_SOFT_MAX:
  6127. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  6128. GGML_ASSERT(!src2 || src2->type == GGML_TYPE_F32);
  6129. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  6130. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  6131. }
  6132. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6133. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  6134. }
  6135. return nullptr;
  6136. case GGML_OP_SOFT_MAX_BACK:
  6137. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6138. return ctx->device->pipeline_soft_max_back_f32;
  6139. }
  6140. return nullptr;
  6141. case GGML_OP_ROPE:
  6142. case GGML_OP_ROPE_BACK:
  6143. {
  6144. const int mode = ((const int32_t *) dst->op_params)[2];
  6145. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  6146. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  6147. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  6148. if (is_neox) {
  6149. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6150. return ctx->device->pipeline_rope_neox_f32;
  6151. }
  6152. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6153. return ctx->device->pipeline_rope_neox_f16;
  6154. }
  6155. } else if (is_mrope && !is_vision) {
  6156. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6157. return ctx->device->pipeline_rope_multi_f32;
  6158. }
  6159. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6160. return ctx->device->pipeline_rope_multi_f16;
  6161. }
  6162. } else if (is_vision) {
  6163. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6164. return ctx->device->pipeline_rope_vision_f32;
  6165. }
  6166. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6167. return ctx->device->pipeline_rope_vision_f16;
  6168. }
  6169. } else {
  6170. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6171. return ctx->device->pipeline_rope_norm_f32;
  6172. }
  6173. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6174. return ctx->device->pipeline_rope_norm_f16;
  6175. }
  6176. }
  6177. return nullptr;
  6178. }
  6179. case GGML_OP_ARGSORT:
  6180. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6181. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  6182. return ctx->device->pipeline_argsort_f32[idx];
  6183. }
  6184. return nullptr;
  6185. case GGML_OP_SUM:
  6186. case GGML_OP_SUM_ROWS:
  6187. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6188. return ctx->device->pipeline_sum_rows_f32;
  6189. }
  6190. return nullptr;
  6191. case GGML_OP_ARGMAX:
  6192. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6193. return ctx->device->pipeline_argmax_f32;
  6194. }
  6195. return nullptr;
  6196. case GGML_OP_COUNT_EQUAL:
  6197. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  6198. return ctx->device->pipeline_count_equal_i32;
  6199. }
  6200. return nullptr;
  6201. case GGML_OP_IM2COL:
  6202. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6203. return ctx->device->pipeline_im2col_f32;
  6204. }
  6205. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  6206. return ctx->device->pipeline_im2col_f32_f16;
  6207. }
  6208. return nullptr;
  6209. case GGML_OP_TIMESTEP_EMBEDDING:
  6210. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6211. return ctx->device->pipeline_timestep_embedding_f32;
  6212. }
  6213. return nullptr;
  6214. case GGML_OP_CONV_TRANSPOSE_1D:
  6215. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6216. return ctx->device->pipeline_conv_transpose_1d_f32;
  6217. }
  6218. return nullptr;
  6219. case GGML_OP_POOL_2D:
  6220. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6221. return ctx->device->pipeline_pool2d_f32;
  6222. }
  6223. return nullptr;
  6224. case GGML_OP_RWKV_WKV6:
  6225. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6226. return ctx->device->pipeline_rwkv_wkv6_f32;
  6227. }
  6228. return nullptr;
  6229. case GGML_OP_RWKV_WKV7:
  6230. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6231. return ctx->device->pipeline_rwkv_wkv7_f32;
  6232. }
  6233. return nullptr;
  6234. case GGML_OP_OPT_STEP_ADAMW:
  6235. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6236. return ctx->device->pipeline_opt_step_adamw_f32;
  6237. }
  6238. return nullptr;
  6239. case GGML_OP_OPT_STEP_SGD:
  6240. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6241. return ctx->device->pipeline_opt_step_sgd_f32;
  6242. }
  6243. return nullptr;
  6244. case GGML_OP_LEAKY_RELU:
  6245. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6246. return ctx->device->pipeline_leaky_relu_f32;
  6247. }
  6248. return nullptr;
  6249. case GGML_OP_CONV_2D:
  6250. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
  6251. ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  6252. auto elements = ggml_vk_get_conv_elements(dst);
  6253. vk_conv_shapes shape;
  6254. uint32_t tiles[CONV_SHAPE_COUNT];
  6255. for (uint32_t i = 0; i < CONV_SHAPE_COUNT; ++i) {
  6256. tiles[i] = CEIL_DIV(elements[0], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[0]) * CEIL_DIV(elements[1], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[1]);
  6257. }
  6258. // We can't query number of shader cores on Intel, use 32 as a placeholder
  6259. // so small convolutions will still choose a smaller tile.
  6260. const uint32_t shader_core_count = ctx->device->shader_core_count > 0 ? ctx->device->shader_core_count : 32;
  6261. if (elements[0] > 64 && tiles[CONV_SHAPE_128x128] >= shader_core_count * 2) {
  6262. shape = CONV_SHAPE_128x128;
  6263. } else if (elements[0] <= 32 && tiles[CONV_SHAPE_32x256] >= shader_core_count * 2) {
  6264. shape = CONV_SHAPE_32x256;
  6265. } else {
  6266. shape = CONV_SHAPE_64x32;
  6267. }
  6268. if (src0->type == GGML_TYPE_F32) {
  6269. return ctx->device->pipeline_conv2d_f32[shape];
  6270. } else if (src0->type == GGML_TYPE_F16) {
  6271. return ctx->device->pipeline_conv2d_f16_f32[shape];
  6272. }
  6273. }
  6274. return nullptr;
  6275. case GGML_OP_CONV_2D_DW:
  6276. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6277. if (ggml_is_contiguous(src1)) {
  6278. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  6279. } else if (ggml_is_contiguous_channels(src1)) {
  6280. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  6281. }
  6282. }
  6283. return nullptr;
  6284. default:
  6285. return nullptr;
  6286. }
  6287. GGML_UNUSED(src2);
  6288. }
  6289. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  6290. switch (op) {
  6291. case GGML_OP_CPY:
  6292. case GGML_OP_GET_ROWS:
  6293. case GGML_OP_ADD:
  6294. case GGML_OP_SUB:
  6295. case GGML_OP_MUL:
  6296. case GGML_OP_DIV:
  6297. case GGML_OP_ADD_ID:
  6298. case GGML_OP_CONCAT:
  6299. case GGML_OP_UPSCALE:
  6300. case GGML_OP_SQR:
  6301. case GGML_OP_SQRT:
  6302. case GGML_OP_SIN:
  6303. case GGML_OP_COS:
  6304. case GGML_OP_CLAMP:
  6305. case GGML_OP_PAD:
  6306. case GGML_OP_REPEAT:
  6307. case GGML_OP_REPEAT_BACK:
  6308. case GGML_OP_ROPE:
  6309. case GGML_OP_RMS_NORM:
  6310. case GGML_OP_CONV_2D_DW:
  6311. case GGML_OP_IM2COL:
  6312. case GGML_OP_SET_ROWS:
  6313. return true;
  6314. default:
  6315. return false;
  6316. }
  6317. }
  6318. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  6319. {
  6320. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  6321. }
  6322. 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) {
  6323. GGML_UNUSED(p);
  6324. GGML_UNUSED(src0);
  6325. GGML_UNUSED(src1);
  6326. GGML_UNUSED(src2);
  6327. GGML_UNUSED(dst);
  6328. static_assert(!std::is_const<T>::value, "unexpected type");
  6329. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  6330. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  6331. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  6332. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  6333. }
  6334. 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) {
  6335. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6336. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6337. p.misalign_offsets = (a_offset << 16) | d_offset;
  6338. GGML_UNUSED(src1);
  6339. GGML_UNUSED(src2);
  6340. }
  6341. 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) {
  6342. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6343. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  6344. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6345. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  6346. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  6347. GGML_UNUSED(src2);
  6348. }
  6349. 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) {
  6350. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6351. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6352. p.a_offset = a_offset;
  6353. p.d_offset = d_offset;
  6354. GGML_UNUSED(src1);
  6355. GGML_UNUSED(src2);
  6356. }
  6357. template<typename PC>
  6358. 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) {
  6359. 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];
  6360. if (src1 != nullptr) {
  6361. 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];
  6362. }
  6363. if (src2 != nullptr) {
  6364. 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];
  6365. }
  6366. 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];
  6367. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  6368. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  6369. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  6370. GGML_ASSERT(dst->buffer != nullptr);
  6371. const uint64_t ne00 = src0->ne[0];
  6372. const uint64_t ne01 = src0->ne[1];
  6373. const uint64_t ne02 = src0->ne[2];
  6374. const uint64_t ne03 = src0->ne[3];
  6375. const uint64_t ne0 = ne00 * ne01;
  6376. const bool use_src1 = src1 != nullptr;
  6377. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  6378. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  6379. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  6380. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  6381. const uint64_t ne1 = ne10 * ne11;
  6382. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  6383. const bool use_src2 = src2 != nullptr;
  6384. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  6385. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  6386. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  6387. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  6388. const uint64_t ne2 = ne20 * ne21;
  6389. const uint64_t ned0 = dst->ne[0];
  6390. const uint64_t ned1 = dst->ne[1];
  6391. const uint64_t ned2 = dst->ne[2];
  6392. const uint64_t ned3 = dst->ne[3];
  6393. const uint64_t ned = ned0 * ned1;
  6394. init_pushconst_fastdiv(pc);
  6395. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  6396. if (pipeline == nullptr) {
  6397. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  6398. if (src1 != nullptr) {
  6399. std::cerr << " and " << ggml_type_name(src1->type);
  6400. }
  6401. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  6402. GGML_ABORT("fatal error");
  6403. }
  6404. if (dryrun) {
  6405. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6406. return;
  6407. }
  6408. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  6409. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6410. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  6411. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  6412. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  6413. vk_buffer d_X = nullptr;
  6414. size_t x_buf_offset = 0;
  6415. vk_buffer d_Y = nullptr;
  6416. size_t y_buf_offset = 0;
  6417. vk_buffer d_Z = nullptr;
  6418. size_t z_buf_offset = 0;
  6419. bool src0_uma = false;
  6420. bool src1_uma = false;
  6421. bool src2_uma = false;
  6422. if (ctx->device->uma) {
  6423. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  6424. src0_uma = d_X != nullptr;
  6425. if (use_src1) {
  6426. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  6427. src1_uma = d_Y != nullptr;
  6428. }
  6429. if (use_src2) {
  6430. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  6431. src2_uma = d_Z != nullptr;
  6432. }
  6433. }
  6434. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  6435. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  6436. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  6437. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  6438. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6439. // Workaround for tiny tensor inputs on ROPE
  6440. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  6441. y_sz = VK_WHOLE_SIZE;
  6442. }
  6443. GGML_ASSERT(d_D != nullptr);
  6444. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6445. if(!src0_uma) {
  6446. d_X = src0_buf_ctx->dev_buffer;
  6447. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6448. GGML_ASSERT(d_X != nullptr);
  6449. }
  6450. if (use_src1 && !src1_uma) {
  6451. d_Y = src1_buf_ctx->dev_buffer;
  6452. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6453. GGML_ASSERT(d_Y != nullptr);
  6454. }
  6455. if (use_src2 && !src2_uma) {
  6456. d_Z = src2_buf_ctx->dev_buffer;
  6457. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  6458. GGML_ASSERT(d_Z != nullptr);
  6459. }
  6460. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  6461. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  6462. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6463. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6464. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6465. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6466. if (op_supports_incontiguous) {
  6467. x_sz = ggml_nbytes(src0);
  6468. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  6469. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  6470. d_sz = ggml_nbytes(dst);
  6471. if (x_buf_offset + x_sz >= d_X->size) {
  6472. x_sz = VK_WHOLE_SIZE;
  6473. }
  6474. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  6475. y_sz = VK_WHOLE_SIZE;
  6476. }
  6477. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  6478. z_sz = VK_WHOLE_SIZE;
  6479. }
  6480. if (d_buf_offset + d_sz >= d_D->size) {
  6481. d_sz = VK_WHOLE_SIZE;
  6482. }
  6483. }
  6484. std::array<uint32_t, 3> elements;
  6485. // Single call if dimension 2 is contiguous
  6486. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  6487. switch (op) {
  6488. case GGML_OP_NORM:
  6489. case GGML_OP_RMS_NORM_BACK:
  6490. case GGML_OP_L2_NORM:
  6491. case GGML_OP_SOFT_MAX:
  6492. case GGML_OP_SOFT_MAX_BACK:
  6493. case GGML_OP_SUM_ROWS:
  6494. case GGML_OP_ARGMAX:
  6495. {
  6496. const uint32_t nr = ggml_nrows(src0);
  6497. if (nr > 262144) {
  6498. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  6499. } else if (nr > 512) {
  6500. elements = { 512, CEIL_DIV(nr, 512), 1 };
  6501. } else {
  6502. elements = { nr, 1, 1 };
  6503. }
  6504. } break;
  6505. case GGML_OP_RMS_NORM:
  6506. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  6507. break;
  6508. case GGML_OP_SUM:
  6509. // We use GGML_OP_SUM_ROWS with 1 row.
  6510. elements = { 1, 1, 1 };
  6511. break;
  6512. case GGML_OP_GROUP_NORM:
  6513. {
  6514. const uint32_t num_groups = dst->op_params[0];
  6515. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  6516. } break;
  6517. case GGML_OP_DIAG_MASK_INF:
  6518. case GGML_OP_ROPE:
  6519. case GGML_OP_ROPE_BACK:
  6520. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6521. break;
  6522. case GGML_OP_GET_ROWS:
  6523. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6524. break;
  6525. case GGML_OP_ARGSORT:
  6526. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6527. break;
  6528. case GGML_OP_IM2COL:
  6529. {
  6530. const bool is_2D = dst->op_params[6] == 1;
  6531. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6532. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6533. const uint32_t KW = src0->ne[0];
  6534. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6535. const uint32_t OW = dst->ne[1];
  6536. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6537. elements = { OW * KW * KH, OH, batch * IC };
  6538. } break;
  6539. case GGML_OP_TIMESTEP_EMBEDDING:
  6540. {
  6541. const uint32_t dim = dst->op_params[0];
  6542. uint32_t half_ceil = (dim + 1) / 2;
  6543. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6544. } break;
  6545. case GGML_OP_CONV_TRANSPOSE_1D:
  6546. {
  6547. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6548. } break;
  6549. case GGML_OP_POOL_2D:
  6550. {
  6551. const uint32_t N = dst->ne[3];
  6552. const uint32_t OC = dst->ne[2];
  6553. const uint32_t OH = dst->ne[1];
  6554. const uint32_t OW = dst->ne[0];
  6555. elements = { N * OC * OH * OW, 1, 1};
  6556. } break;
  6557. case GGML_OP_CONV_2D:
  6558. {
  6559. elements = ggml_vk_get_conv_elements(dst);
  6560. } break;
  6561. case GGML_OP_ADD:
  6562. case GGML_OP_SUB:
  6563. case GGML_OP_DIV:
  6564. case GGML_OP_MUL:
  6565. case GGML_OP_SCALE:
  6566. case GGML_OP_SQR:
  6567. case GGML_OP_SQRT:
  6568. case GGML_OP_SIN:
  6569. case GGML_OP_COS:
  6570. case GGML_OP_CLAMP:
  6571. case GGML_OP_PAD:
  6572. case GGML_OP_ROLL:
  6573. case GGML_OP_REPEAT:
  6574. case GGML_OP_REPEAT_BACK:
  6575. case GGML_OP_CPY:
  6576. case GGML_OP_CONCAT:
  6577. case GGML_OP_UPSCALE:
  6578. case GGML_OP_UNARY:
  6579. case GGML_OP_GLU:
  6580. case GGML_OP_CONV_2D_DW:
  6581. {
  6582. uint32_t ne = ggml_nelements(dst);
  6583. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6584. // Convert from number of logical elements to 2- or 4-byte units.
  6585. ne /= ggml_blck_size(src0->type);
  6586. if ((ggml_type_size(src0->type) % 4) == 0) {
  6587. ne *= ggml_type_size(src0->type) / 4;
  6588. } else {
  6589. ne *= ggml_type_size(src0->type) / 2;
  6590. }
  6591. }
  6592. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  6593. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  6594. // So divide by block size here before splitting into 512x512 groups.
  6595. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6596. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  6597. }
  6598. if (ne > 262144) {
  6599. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6600. } else if (ne > 512) {
  6601. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6602. } else {
  6603. elements = { ne, 1, 1 };
  6604. }
  6605. } break;
  6606. case GGML_OP_ADD_ID:
  6607. {
  6608. elements = { (uint32_t)ne01, (uint32_t)ne02, 1 };
  6609. } break;
  6610. case GGML_OP_SET_ROWS:
  6611. {
  6612. uint32_t ne = ggml_nelements(src0);
  6613. if (ggml_is_quantized(dst->type)) {
  6614. // quants run 32 threads each doing QUANT_K elements
  6615. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  6616. } else {
  6617. // scalar types do one element per thread, running 512 threads
  6618. ne = CEIL_DIV(ne, 512);
  6619. }
  6620. if (ne > 262144) {
  6621. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6622. } else if (ne > 512) {
  6623. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6624. } else {
  6625. elements = { ne, 1, 1 };
  6626. }
  6627. }
  6628. break;
  6629. default:
  6630. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  6631. break;
  6632. }
  6633. if (!op_supports_incontiguous) {
  6634. if (x_sz != VK_WHOLE_SIZE) {
  6635. x_sz *= ne02 * ne03;
  6636. }
  6637. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  6638. y_sz *= ne12 * ne13;
  6639. }
  6640. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  6641. z_sz *= ne22 * ne23;
  6642. }
  6643. if (d_sz != VK_WHOLE_SIZE) {
  6644. d_sz *= ned2 * ned3;
  6645. }
  6646. }
  6647. if (op == GGML_OP_GLU) {
  6648. // Empty src1 is possible in glu, but the shader needs a buffer
  6649. vk_subbuffer subbuf_y;
  6650. if (use_src1) {
  6651. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6652. } else {
  6653. subbuf_y = { d_X, 0, x_sz };
  6654. }
  6655. ggml_vk_sync_buffers(subctx);
  6656. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6657. } else if (op == GGML_OP_SOFT_MAX) {
  6658. // Empty src1 and src2 is possible in soft_max, but the shader needs a buffer
  6659. vk_subbuffer subbuf_y;
  6660. if (use_src1) {
  6661. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6662. } else {
  6663. subbuf_y = { d_X, 0, x_sz };
  6664. }
  6665. vk_subbuffer subbuf_z;
  6666. if (use_src2) {
  6667. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6668. } else {
  6669. subbuf_z = { d_X, 0, x_sz };
  6670. }
  6671. ggml_vk_sync_buffers(subctx);
  6672. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6673. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  6674. // Empty src2 is possible in rope, but the shader needs a buffer
  6675. vk_subbuffer subbuf_z;
  6676. if (use_src2) {
  6677. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6678. } else {
  6679. subbuf_z = { d_X, 0, x_sz };
  6680. }
  6681. ggml_vk_sync_buffers(subctx);
  6682. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6683. } else if (op == GGML_OP_IM2COL) {
  6684. // im2col uses only src1 and dst buffers
  6685. ggml_vk_sync_buffers(subctx);
  6686. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6687. } else if (op == GGML_OP_COUNT_EQUAL) {
  6688. ggml_vk_sync_buffers(subctx);
  6689. // count_equal assumes that destination buffer is initialized with zeroes
  6690. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  6691. ggml_vk_sync_buffers(subctx);
  6692. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6693. } else if (op == GGML_OP_OPT_STEP_SGD) {
  6694. // OPT_STEP_SGD works on src0, it does not need dst
  6695. ggml_vk_sync_buffers(subctx);
  6696. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz } }, pc, elements);
  6697. } else if (use_src2) {
  6698. ggml_vk_sync_buffers(subctx);
  6699. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6700. } else if (use_src1) {
  6701. ggml_vk_sync_buffers(subctx);
  6702. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6703. } else {
  6704. ggml_vk_sync_buffers(subctx);
  6705. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6706. }
  6707. }
  6708. 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) {
  6709. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6710. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6711. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6712. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  6713. (uint32_t)ggml_nelements(src0),
  6714. (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,
  6715. (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,
  6716. (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,
  6717. 0,
  6718. 0.0f, 0.0f, 0,
  6719. }, dryrun);
  6720. }
  6721. 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) {
  6722. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6723. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6724. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6725. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6726. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6727. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6728. int offset = dst->op_params[3] / 4; // offset in bytes
  6729. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  6730. (uint32_t)ggml_nelements(src0),
  6731. (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,
  6732. (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,
  6733. (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,
  6734. 0,
  6735. 0.0f, 0.0f, offset,
  6736. }, dryrun);
  6737. }
  6738. static void ggml_vk_multi_add(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cgraph * cgraph, int node_idx, bool dryrun = false) {
  6739. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  6740. const ggml_tensor *dst = cgraph->nodes[node_idx + ctx->num_additional_fused_ops];
  6741. // Make a list of all the tensors used by the op.
  6742. // Last element of the list is the dest tensor.
  6743. const ggml_tensor *tensors[MAX_PARAMETER_COUNT];
  6744. uint32_t num_srcs = ctx->num_additional_fused_ops + 2;
  6745. uint32_t num_tensors = num_srcs + 1;
  6746. GGML_ASSERT(num_tensors <= MAX_PARAMETER_COUNT);
  6747. tensors[0] = first_node->src[0];
  6748. tensors[1] = first_node->src[1];
  6749. for (int32_t i = 0; i < ctx->num_additional_fused_ops; ++i) {
  6750. // check whether the previous result is src[0] or src[1]
  6751. if (cgraph->nodes[node_idx + i] == cgraph->nodes[node_idx + i + 1]->src[0]) {
  6752. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[1];
  6753. } else {
  6754. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[0];
  6755. }
  6756. }
  6757. tensors[num_srcs] = dst;
  6758. vk_op_multi_add_push_constants pc;
  6759. pc.ne20 = (uint32_t)dst->ne[0];
  6760. pc.ne21 = (uint32_t)dst->ne[1];
  6761. pc.ne22 = (uint32_t)dst->ne[2];
  6762. pc.ne23 = (uint32_t)dst->ne[3];
  6763. for (uint32_t i = 0; i < num_tensors; ++i) {
  6764. const ggml_tensor *t = tensors[i];
  6765. pc.nb[i][0] = (uint32_t)t->nb[0] / sizeof(float);
  6766. pc.nb[i][1] = (uint32_t)t->nb[1] / sizeof(float);
  6767. pc.nb[i][2] = (uint32_t)t->nb[2] / sizeof(float);
  6768. pc.nb[i][3] = (uint32_t)t->nb[3] / sizeof(float);
  6769. }
  6770. vk_pipeline pipeline = ctx->device->pipeline_multi_add[ctx->num_additional_fused_ops];
  6771. if (pipeline == nullptr) {
  6772. std::cerr << "ggml_vulkan: Error: Missing multi_add";
  6773. GGML_ABORT("fatal error");
  6774. }
  6775. if (dryrun) {
  6776. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6777. return;
  6778. }
  6779. ggml_backend_vk_buffer_context * buf_ctx[MAX_PARAMETER_COUNT];
  6780. vk_buffer buf[MAX_PARAMETER_COUNT];
  6781. size_t offset[MAX_PARAMETER_COUNT];
  6782. bool uma[MAX_PARAMETER_COUNT];
  6783. for (uint32_t i = 0; i < num_tensors; ++i) {
  6784. buf_ctx[i] = (ggml_backend_vk_buffer_context *)tensors[i]->buffer->context;
  6785. buf[i] = nullptr;
  6786. offset[i] = 0;
  6787. uma[i] = false;
  6788. if (ctx->device->uma) {
  6789. ggml_vk_host_get(ctx->device, tensors[i]->data, buf[i], offset[i]);
  6790. uma[i] = buf[i] != nullptr;
  6791. }
  6792. if (!uma[i]) {
  6793. buf[i] = buf_ctx[i]->dev_buffer;
  6794. offset[i] = vk_tensor_offset(tensors[i]) + tensors[i]->view_offs;
  6795. }
  6796. GGML_ASSERT(buf[i] != nullptr);
  6797. }
  6798. // If any remaining descriptors are unused, just point them at src[0]
  6799. for (uint32_t i = num_tensors; i < MAX_PARAMETER_COUNT; ++i) {
  6800. buf[i] = buf[0];
  6801. offset[i] = 0;
  6802. }
  6803. std::array<uint32_t, 3> elements;
  6804. uint32_t ne = ggml_nelements(dst);
  6805. if (ne > 262144) {
  6806. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6807. } else if (ne > 512) {
  6808. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6809. } else {
  6810. elements = { ne, 1, 1 };
  6811. }
  6812. ggml_vk_sync_buffers(subctx);
  6813. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6814. {
  6815. vk_subbuffer{ buf[0], offset[0], VK_WHOLE_SIZE },
  6816. vk_subbuffer{ buf[1], offset[1], VK_WHOLE_SIZE },
  6817. vk_subbuffer{ buf[2], offset[2], VK_WHOLE_SIZE },
  6818. vk_subbuffer{ buf[3], offset[3], VK_WHOLE_SIZE },
  6819. vk_subbuffer{ buf[4], offset[4], VK_WHOLE_SIZE },
  6820. vk_subbuffer{ buf[5], offset[5], VK_WHOLE_SIZE },
  6821. vk_subbuffer{ buf[6], offset[6], VK_WHOLE_SIZE },
  6822. vk_subbuffer{ buf[7], offset[7], VK_WHOLE_SIZE },
  6823. }, pc, elements);
  6824. }
  6825. 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) {
  6826. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6827. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6828. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6829. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  6830. (uint32_t)ggml_nelements(src0),
  6831. (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,
  6832. (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,
  6833. (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,
  6834. 0,
  6835. 0.0f, 0.0f, 0,
  6836. }, dryrun);
  6837. }
  6838. 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) {
  6839. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6840. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6841. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6842. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  6843. (uint32_t)ggml_nelements(src0),
  6844. (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,
  6845. (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,
  6846. (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,
  6847. 0,
  6848. 0.0f, 0.0f, 0,
  6849. }, dryrun);
  6850. }
  6851. 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) {
  6852. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6853. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6854. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6855. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  6856. (uint32_t)ggml_nelements(src0),
  6857. (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,
  6858. (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,
  6859. (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,
  6860. 0,
  6861. 0.0f, 0.0f, 0,
  6862. }, dryrun);
  6863. }
  6864. 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) {
  6865. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6866. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6867. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6868. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  6869. (uint32_t)ggml_nelements(src0),
  6870. (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,
  6871. (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,
  6872. (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,
  6873. 0,
  6874. 0.0f, 0.0f, 0,
  6875. }, dryrun);
  6876. }
  6877. static void ggml_vk_add_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  6878. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6879. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6880. const uint32_t src2_type_size = ggml_type_size(src2->type);
  6881. ggml_vk_op_f32<vk_op_add_id_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ADD_ID, {
  6882. (uint32_t)dst->ne[0],
  6883. (uint32_t)dst->ne[1],
  6884. (uint32_t)src0->nb[1] / src0_type_size,
  6885. (uint32_t)src0->nb[2] / src0_type_size,
  6886. (uint32_t)src1->nb[1] / src1_type_size,
  6887. (uint32_t)src2->nb[1] / src2_type_size,
  6888. }, dryrun);
  6889. }
  6890. 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) {
  6891. GGML_ASSERT(version == 6 || version == 7);
  6892. int num_srcs = version == 6 ? 6 : 7;
  6893. for (int i = 0; i < num_srcs; i++) {
  6894. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  6895. }
  6896. GGML_ASSERT(dst->buffer != nullptr);
  6897. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  6898. GGML_ASSERT(pipeline != nullptr);
  6899. if (dryrun) {
  6900. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6901. return;
  6902. }
  6903. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6904. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6905. for (int i = 0; i < num_srcs; i++) {
  6906. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  6907. }
  6908. ggml_vk_sync_buffers(subctx);
  6909. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6910. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6911. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  6912. if (ctx->device->uma) {
  6913. for (int i = 0; i < num_srcs; i++) {
  6914. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  6915. srcs_uma[i] = d_srcs[i] != nullptr;
  6916. }
  6917. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  6918. dst_uma = d_D != nullptr;
  6919. }
  6920. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6921. for (int i = 0; i < num_srcs; i++) {
  6922. src_sizes[i] = ggml_nbytes(dst->src[i]);
  6923. if (!srcs_uma[i]) {
  6924. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  6925. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  6926. }
  6927. }
  6928. const uint64_t dst_size = ggml_nbytes(dst);
  6929. if (!dst_uma) {
  6930. d_D = dst_buf_ctx->dev_buffer;
  6931. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  6932. }
  6933. std::array<uint32_t, 3> elements = {
  6934. (uint32_t)(pc.B * pc.H),
  6935. 1,
  6936. 1
  6937. };
  6938. if (version == 6) {
  6939. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6940. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6941. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6942. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6943. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6944. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6945. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6946. vk_subbuffer{ d_D, dst_offset, dst_size }
  6947. }, pc, elements);
  6948. } else if (version == 7) {
  6949. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6950. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6951. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6952. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6953. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6954. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6955. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6956. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  6957. vk_subbuffer{ d_D, dst_offset, dst_size }
  6958. }, pc, elements);
  6959. } else {
  6960. // shouldn't happen
  6961. GGML_ASSERT(false);
  6962. }
  6963. }
  6964. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6965. const size_t seq_length = dst->src[0]->ne[2];
  6966. const size_t n_embed = dst->ne[0];
  6967. const size_t n_heads = dst->src[0]->ne[1];
  6968. const size_t n_seqs = dst->src[5]->ne[1];
  6969. ggml_vk_op_f32_wkv(
  6970. ctx, subctx, dst,
  6971. {
  6972. (uint32_t)n_seqs,
  6973. (uint32_t)seq_length,
  6974. (uint32_t)n_embed,
  6975. (uint32_t)n_heads,
  6976. },
  6977. 6,
  6978. dryrun
  6979. );
  6980. }
  6981. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6982. const size_t seq_length = dst->src[0]->ne[2];
  6983. const size_t n_embed = dst->ne[0];
  6984. const size_t n_heads = dst->src[0]->ne[1];
  6985. const size_t n_seqs = dst->src[6]->ne[1];
  6986. ggml_vk_op_f32_wkv(
  6987. ctx, subctx, dst,
  6988. {
  6989. (uint32_t)n_seqs,
  6990. (uint32_t)seq_length,
  6991. (uint32_t)n_embed,
  6992. (uint32_t)n_heads,
  6993. },
  6994. 7,
  6995. dryrun
  6996. );
  6997. }
  6998. 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) {
  6999. const ggml_tensor * x = dst->src[0];
  7000. const ggml_tensor * g = dst->src[1];
  7001. const ggml_tensor * gm = dst->src[2];
  7002. const ggml_tensor * gv = dst->src[3];
  7003. const ggml_tensor * p = dst->src[4];
  7004. GGML_ASSERT(x->type == GGML_TYPE_F32);
  7005. GGML_ASSERT(g->type == GGML_TYPE_F32);
  7006. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  7007. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  7008. GGML_ASSERT(p->type == GGML_TYPE_F32);
  7009. GGML_ASSERT(dst->buffer != nullptr);
  7010. GGML_ASSERT(ggml_is_contiguous(x));
  7011. GGML_ASSERT(ggml_is_contiguous(g));
  7012. GGML_ASSERT(ggml_is_contiguous(gm));
  7013. GGML_ASSERT(ggml_is_contiguous(gv));
  7014. GGML_ASSERT(ggml_is_contiguous(p));
  7015. GGML_ASSERT(ggml_are_same_shape(x, g));
  7016. GGML_ASSERT(ggml_are_same_shape(x, gm));
  7017. GGML_ASSERT(ggml_are_same_shape(x, gv));
  7018. GGML_ASSERT(ggml_nelements(p) == 7);
  7019. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  7020. GGML_ASSERT(pipeline != nullptr);
  7021. if (dryrun) {
  7022. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7023. return;
  7024. }
  7025. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  7026. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  7027. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  7028. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  7029. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  7030. ggml_vk_sync_buffers(subctx);
  7031. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  7032. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  7033. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  7034. if (ctx->device->uma) {
  7035. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  7036. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  7037. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  7038. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  7039. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  7040. X_uma = d_X != nullptr;
  7041. G_uma = d_G != nullptr;
  7042. GM_uma = d_GM != nullptr;
  7043. GV_uma = d_GV != nullptr;
  7044. P_uma = d_P != nullptr;
  7045. }
  7046. if (!X_uma) {
  7047. d_X = x_buf_ctx->dev_buffer;
  7048. x_offset = vk_tensor_offset(x) + x->view_offs;
  7049. }
  7050. if (!G_uma) {
  7051. d_G = g_buf_ctx->dev_buffer;
  7052. g_offset = vk_tensor_offset(g) + g->view_offs;
  7053. }
  7054. if (!GM_uma) {
  7055. d_GM = gm_buf_ctx->dev_buffer;
  7056. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  7057. }
  7058. if (!GV_uma) {
  7059. d_GV = gv_buf_ctx->dev_buffer;
  7060. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  7061. }
  7062. if (!P_uma) {
  7063. d_P = p_buf_ctx->dev_buffer;
  7064. p_offset = vk_tensor_offset(p) + p->view_offs;
  7065. }
  7066. const uint64_t x_size = ggml_nbytes(x);
  7067. const uint64_t g_size = ggml_nbytes(g);
  7068. const uint64_t gm_size = ggml_nbytes(gm);
  7069. const uint64_t gv_size = ggml_nbytes(gv);
  7070. const uint64_t p_size = ggml_nbytes(p);
  7071. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  7072. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7073. vk_subbuffer{ d_X, x_offset, x_size },
  7074. vk_subbuffer{ d_G, g_offset, g_size },
  7075. vk_subbuffer{ d_GM, gm_offset, gm_size },
  7076. vk_subbuffer{ d_GV, gv_offset, gv_size },
  7077. vk_subbuffer{ d_P, p_offset, p_size },
  7078. }, pc, elements);
  7079. }
  7080. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7081. const size_t n = ggml_nelements(dst->src[0]);
  7082. ggml_vk_op_f32_opt_step_adamw(
  7083. ctx, subctx, dst,
  7084. { (uint32_t)n, 0, 0.0f, 0.0f },
  7085. dryrun
  7086. );
  7087. }
  7088. static void ggml_vk_opt_step_sgd(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  7089. const size_t n = ggml_nelements(dst->src[0]);
  7090. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_OPT_STEP_SGD, { (uint32_t)n, 0, 0.0f, 0.0f }, dryrun);
  7091. }
  7092. 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) {
  7093. int * op_params = (int *)dst->op_params;
  7094. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7095. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7096. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7097. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  7098. (uint32_t)ggml_nelements(dst),
  7099. (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,
  7100. (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,
  7101. (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,
  7102. 0,
  7103. 0.0f, 0.0f, op_params[0],
  7104. }, dryrun);
  7105. }
  7106. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7107. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7108. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  7109. float sf0 = (float)dst->ne[0] / src0->ne[0];
  7110. float sf1 = (float)dst->ne[1] / src0->ne[1];
  7111. float sf2 = (float)dst->ne[2] / src0->ne[2];
  7112. float sf3 = (float)dst->ne[3] / src0->ne[3];
  7113. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  7114. sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
  7115. sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
  7116. }
  7117. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  7118. (uint32_t)ggml_nelements(dst), 0, 0,
  7119. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1],
  7120. (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,
  7121. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  7122. sf0, sf1, sf2, sf3,
  7123. }, dryrun);
  7124. }
  7125. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7126. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7127. p.param1 = ggml_get_op_params_f32(dst, 0);
  7128. p.param2 = ggml_get_op_params_f32(dst, 1);
  7129. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  7130. }
  7131. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7132. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7133. }
  7134. static void ggml_vk_sqrt(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7135. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQRT, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7136. }
  7137. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7138. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7139. }
  7140. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7141. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  7142. }
  7143. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7144. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7145. p.param1 = ggml_get_op_params_f32(dst, 0);
  7146. p.param2 = ggml_get_op_params_f32(dst, 1);
  7147. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  7148. }
  7149. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7150. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7151. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  7152. }
  7153. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7154. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  7155. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  7156. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  7157. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  7158. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  7159. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  7160. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7161. memcpy(&p.param1, &s01_packed, sizeof(float));
  7162. memcpy(&p.param2, &s23_packed, sizeof(float));
  7163. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  7164. }
  7165. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7166. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7167. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  7168. }
  7169. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7170. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7171. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  7172. }
  7173. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7174. uint32_t ne = (uint32_t)ggml_nelements(src0);
  7175. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7176. // Convert from number of logical elements to 2- or 4-byte units.
  7177. ne /= ggml_blck_size(src0->type);
  7178. if ((ggml_type_size(src0->type) % 4) == 0) {
  7179. ne *= ggml_type_size(src0->type) / 4;
  7180. } else {
  7181. ne *= ggml_type_size(src0->type) / 2;
  7182. }
  7183. }
  7184. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  7185. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  7186. }
  7187. static void ggml_vk_set_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7188. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7189. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7190. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7191. // Skip empty skip_rows operations. For most ops the empty check at the start
  7192. // of ggml_vk_build_graph is sufficient, but set_rows can have a nonempty dst
  7193. // with empty srcs.
  7194. if (ggml_is_empty(src0) || ggml_is_empty(src1)) {
  7195. return;
  7196. }
  7197. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  7198. (uint32_t)ggml_nelements(src0),
  7199. (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,
  7200. (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,
  7201. (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,
  7202. 0,
  7203. 0.0f, 0.0f, 0,
  7204. }, dryrun);
  7205. }
  7206. 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) {
  7207. 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);
  7208. }
  7209. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7210. float * op_params = (float *)dst->op_params;
  7211. 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);
  7212. }
  7213. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7214. const int * int_op_params = (const int *)dst->op_params;
  7215. const float * float_op_params = (const float *)dst->op_params;
  7216. const uint32_t num_groups = int_op_params[0];
  7217. const float eps = float_op_params[1];
  7218. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  7219. 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);
  7220. }
  7221. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, float * op_params, bool dryrun = false) {
  7222. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7223. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7224. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7225. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  7226. (uint32_t)ggml_nelements(src0),
  7227. (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,
  7228. (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,
  7229. (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,
  7230. 0,
  7231. op_params[0], 0.0f, 0,
  7232. }, dryrun);
  7233. }
  7234. 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) {
  7235. float * op_params = (float *)dst->op_params;
  7236. 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);
  7237. }
  7238. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7239. float * op_params = (float *)dst->op_params;
  7240. 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);
  7241. }
  7242. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7243. 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);
  7244. }
  7245. static void ggml_vk_glu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7246. const float * op_params_f = (const float *)dst->op_params;
  7247. const bool swapped = (bool)dst->op_params[1];
  7248. const bool split = src1 != nullptr;
  7249. const float alpha = op_params_f[2];
  7250. const float limit = op_params_f[3];
  7251. GGML_ASSERT(ggml_is_contiguous(src0));
  7252. if (!split) {
  7253. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  7254. } else {
  7255. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  7256. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  7257. GGML_ASSERT(src0->type == src1->type);
  7258. }
  7259. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  7260. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU,
  7261. {
  7262. (uint32_t)ggml_nelements(dst),
  7263. (uint32_t)src0->ne[0],
  7264. (uint32_t)dst->ne[0],
  7265. mode,
  7266. alpha,
  7267. limit
  7268. }, dryrun);
  7269. }
  7270. 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) {
  7271. int32_t * op_params = (int32_t *)dst->op_params;
  7272. 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);
  7273. }
  7274. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  7275. float * op_params = (float *)dst->op_params;
  7276. float scale = op_params[0];
  7277. float max_bias = op_params[1];
  7278. const uint32_t ncols = (uint32_t)src0->ne[0];
  7279. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  7280. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  7281. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  7282. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  7283. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  7284. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  7285. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  7286. const uint32_t n_head_kv = src0->ne[2];
  7287. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  7288. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7289. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7290. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_SOFT_MAX, {
  7291. ncols,
  7292. src1 != nullptr ? nrows_y : (uint32_t)0,
  7293. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  7294. ne12, ne13,
  7295. nb11, nb12, nb13,
  7296. scale, max_bias,
  7297. m0, m1,
  7298. n_head_log2,
  7299. nrows_x,
  7300. src2 != nullptr
  7301. }, dryrun);
  7302. }
  7303. 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) {
  7304. float * op_params = (float *)dst->op_params;
  7305. 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);
  7306. }
  7307. 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) {
  7308. const int n_dims = ((int32_t *) dst->op_params)[1];
  7309. const int mode = ((int32_t *) dst->op_params)[2];
  7310. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  7311. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7312. const float freq_base = ((float *) dst->op_params)[5];
  7313. const float freq_scale = ((float *) dst->op_params)[6];
  7314. const float ext_factor = ((float *) dst->op_params)[7];
  7315. const float attn_factor = ((float *) dst->op_params)[8];
  7316. const float beta_fast = ((float *) dst->op_params)[9];
  7317. const float beta_slow = ((float *) dst->op_params)[10];
  7318. int sections[4] {};
  7319. if (mode & GGML_ROPE_TYPE_MROPE) {
  7320. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7321. }
  7322. float corr_dims[2];
  7323. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7324. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7325. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  7326. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  7327. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  7328. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  7329. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  7330. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  7331. { sections[0], sections[1], sections[2], sections[3] }, backprop
  7332. }, dryrun);
  7333. }
  7334. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7335. int32_t * op_params = (int32_t *)dst->op_params;
  7336. uint32_t ncols = src0->ne[0];
  7337. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  7338. ncols,
  7339. op_params[0],
  7340. }, dryrun);
  7341. }
  7342. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7343. 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);
  7344. }
  7345. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7346. 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);
  7347. }
  7348. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7349. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], 0.0f, 0.0f }, dryrun);
  7350. }
  7351. 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) {
  7352. 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);
  7353. }
  7354. 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) {
  7355. const int32_t s0 = dst->op_params[0];
  7356. const int32_t s1 = dst->op_params[1];
  7357. const int32_t p0 = dst->op_params[2];
  7358. const int32_t p1 = dst->op_params[3];
  7359. const int32_t d0 = dst->op_params[4];
  7360. const int32_t d1 = dst->op_params[5];
  7361. const bool is_2D = dst->op_params[6] == 1;
  7362. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  7363. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  7364. const uint32_t IW = src1->ne[0];
  7365. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  7366. const uint32_t KW = src0->ne[0];
  7367. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  7368. const uint32_t OW = dst->ne[1];
  7369. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7370. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  7371. const uint32_t pelements = OW * KW * KH;
  7372. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  7373. batch_offset, offset_delta,
  7374. IC, IW, IH, OW, OH, KW, KH,
  7375. pelements,
  7376. IC * KH * KW,
  7377. s0, s1, p0, p1, d0, d1,
  7378. }, dryrun);
  7379. }
  7380. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7381. const uint32_t dim = dst->op_params[0];
  7382. const uint32_t max_period = dst->op_params[1];
  7383. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  7384. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  7385. nb1, dim, max_period,
  7386. }, dryrun);
  7387. }
  7388. static void ggml_vk_conv_transpose_1d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7389. // src0: (K, Cout, Cin, 1) -- kernel
  7390. // src1: (L, Cin, 1, 1) -- input
  7391. // dst: (*, Cout, 1, 1)
  7392. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7393. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7394. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7395. GGML_TENSOR_BINARY_OP_LOCALS
  7396. GGML_ASSERT(nb00 == sizeof(float));
  7397. GGML_ASSERT(nb10 == sizeof(float));
  7398. const int32_t s0 = dst->op_params[0];
  7399. vk_op_conv_transpose_1d_push_constants p{};
  7400. p.Cout = static_cast<uint32_t>(ne01);
  7401. p.Cin = static_cast<uint32_t>(ne02);
  7402. p.K = static_cast<uint32_t>(ne00);
  7403. p.L = static_cast<uint32_t>(ne10);
  7404. p.KL = static_cast<uint32_t>(ne0);
  7405. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  7406. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  7407. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  7408. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  7409. p.s0 = static_cast<uint32_t>(s0);
  7410. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  7411. }
  7412. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7413. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  7414. const int32_t k1 = dst->op_params[1];
  7415. const int32_t k0 = dst->op_params[2];
  7416. const int32_t s1 = dst->op_params[3];
  7417. const int32_t s0 = dst->op_params[4];
  7418. const int32_t p1 = dst->op_params[5];
  7419. const int32_t p0 = dst->op_params[6];
  7420. const uint32_t IH = src0->ne[1];
  7421. const uint32_t IW = src0->ne[0];
  7422. const uint32_t N = dst->ne[3];
  7423. const uint32_t OC = dst->ne[2];
  7424. const uint32_t OH = dst->ne[1];
  7425. const uint32_t OW = dst->ne[0];
  7426. const uint32_t parallel_elements = N * OC * OH * OW;
  7427. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  7428. IW, IH, OW, OH, OC,
  7429. parallel_elements,
  7430. op,
  7431. k0, k1, s0, s1, p0, p1,
  7432. }, dryrun);
  7433. }
  7434. static void ggml_vk_conv_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  7435. const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7436. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7437. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7438. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7439. GGML_TENSOR_BINARY_OP_LOCALS
  7440. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  7441. GGML_ASSERT(nb10 == sizeof(float));
  7442. GGML_ASSERT(nb0 == sizeof(float));
  7443. vk_op_conv2d_push_constants p{};
  7444. p.Cout = static_cast<uint32_t>(ne03);
  7445. p.Cin = static_cast<uint32_t>(ne02);
  7446. p.N = static_cast<uint32_t>(ne13);
  7447. p.KW = static_cast<uint32_t>(ne00);
  7448. p.KH = static_cast<uint32_t>(ne01);
  7449. p.W = static_cast<uint32_t>(ne10);
  7450. p.H = static_cast<uint32_t>(ne11);
  7451. p.OW = static_cast<uint32_t>(ne0);
  7452. p.OH = static_cast<uint32_t>(ne1);
  7453. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  7454. p.s1 = static_cast<uint32_t>(dst->op_params[1]);
  7455. p.p0 = static_cast<uint32_t>(dst->op_params[2]);
  7456. p.p1 = static_cast<uint32_t>(dst->op_params[3]);
  7457. p.d0 = static_cast<uint32_t>(dst->op_params[4]);
  7458. p.d1 = static_cast<uint32_t>(dst->op_params[5]);
  7459. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  7460. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  7461. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  7462. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  7463. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  7464. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  7465. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  7466. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  7467. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  7468. GGML_ASSERT(ne03 == ne2);
  7469. GGML_ASSERT(ne02 == ne12);
  7470. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D, std::move(p), dryrun);
  7471. }
  7472. static void ggml_vk_conv_2d_dw(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7473. vk_op_conv2d_dw_push_constants p{};
  7474. p.ne = ggml_nelements(dst);
  7475. p.channels = dst->ne[2];
  7476. p.batches = dst->ne[3];
  7477. p.dst_w = dst->ne[0];
  7478. p.dst_h = dst->ne[1];
  7479. p.src_w = src1->ne[0];
  7480. p.src_h = src1->ne[1];
  7481. p.knl_w = src0->ne[0];
  7482. p.knl_h = src0->ne[1];
  7483. p.stride_x = dst->op_params[0];
  7484. p.stride_y = dst->op_params[1];
  7485. p.pad_x = dst->op_params[2];
  7486. p.pad_y = dst->op_params[3];
  7487. p.dilation_x = dst->op_params[4];
  7488. p.dilation_y = dst->op_params[5];
  7489. GGML_ASSERT(src0->ne[3] == p.channels);
  7490. GGML_ASSERT(src1->ne[3] == p.batches);
  7491. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  7492. }
  7493. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7494. const float * op_params = (const float *)dst->op_params;
  7495. 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);
  7496. }
  7497. #ifdef GGML_VULKAN_RUN_TESTS
  7498. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  7499. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  7500. return;
  7501. }
  7502. i0 = std::max(i0, 5);
  7503. i1 = std::max(i1, 5);
  7504. i2 = std::max(i2, 0);
  7505. fprintf(stderr, " ");
  7506. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7507. fprintf(stderr, "%7d ", idx1);
  7508. }
  7509. fprintf(stderr, "\n");
  7510. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7511. fprintf(stderr, "%7d: ", idx0);
  7512. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7513. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  7514. float val;
  7515. if (type == GGML_TYPE_F32) {
  7516. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  7517. } else if (type == GGML_TYPE_F16) {
  7518. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  7519. } else {
  7520. GGML_ABORT("fatal error");
  7521. }
  7522. fprintf(stderr, "% 7.2f ", val);
  7523. } else {
  7524. fprintf(stderr, " ");
  7525. }
  7526. }
  7527. fprintf(stderr, "\n");
  7528. }
  7529. }
  7530. template <typename X_TYPE, typename Y_TYPE>
  7531. 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) {
  7532. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  7533. const size_t x_ne = m * k * batch;
  7534. const size_t y_ne = k * n * batch;
  7535. const size_t d_ne = m * n * batch;
  7536. vk_pipeline p;
  7537. std::string shname;
  7538. if (shader_size == 0) {
  7539. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7540. p = ctx->device->pipeline_matmul_f32->a_s;
  7541. shname = "F32_ALIGNED_S";
  7542. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7543. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  7544. shname = "F32_F16_ALIGNED_S";
  7545. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7546. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  7547. shname = "F16_F32_ALIGNED_S";
  7548. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7549. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  7550. shname = "F16_ALIGNED_S";
  7551. } else {
  7552. GGML_ABORT("fatal error");
  7553. }
  7554. } else if (shader_size == 1) {
  7555. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7556. p = ctx->device->pipeline_matmul_f32->a_m;
  7557. shname = "F32_ALIGNED_M";
  7558. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7559. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  7560. shname = "F32_F16_ALIGNED_M";
  7561. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7562. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  7563. shname = "F16_F32_ALIGNED_M";
  7564. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7565. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  7566. shname = "F16_ALIGNED_M";
  7567. } else {
  7568. GGML_ABORT("fatal error");
  7569. }
  7570. } else if (shader_size == 2) {
  7571. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7572. p = ctx->device->pipeline_matmul_f32->a_l;
  7573. shname = "F32_ALIGNED_L";
  7574. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7575. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  7576. shname = "F32_F16_ALIGNED_L";
  7577. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7578. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  7579. shname = "F16_F32_ALIGNED_L";
  7580. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7581. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  7582. shname = "F16_ALIGNED_L";
  7583. } else {
  7584. GGML_ABORT("fatal error");
  7585. }
  7586. } else {
  7587. GGML_ASSERT(0);
  7588. }
  7589. const size_t kpad = ggml_vk_align_size(k, p->align);
  7590. if (k != kpad) {
  7591. if (shader_size == 0) {
  7592. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7593. p = ctx->device->pipeline_matmul_f32->s;
  7594. shname = "F32_S";
  7595. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7596. p = ctx->device->pipeline_matmul_f32_f16->s;
  7597. shname = "F32_F16_S";
  7598. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7599. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  7600. shname = "F16_F32_S";
  7601. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7602. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  7603. shname = "F16_S";
  7604. }
  7605. } else if (shader_size == 1) {
  7606. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7607. p = ctx->device->pipeline_matmul_f32->m;
  7608. shname = "F32_M";
  7609. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7610. p = ctx->device->pipeline_matmul_f32_f16->m;
  7611. shname = "F32_F16_M";
  7612. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7613. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  7614. shname = "F16_F32_M";
  7615. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7616. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  7617. shname = "F16_M";
  7618. }
  7619. } else if (shader_size == 2) {
  7620. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7621. p = ctx->device->pipeline_matmul_f32->l;
  7622. shname = "F32_L";
  7623. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7624. p = ctx->device->pipeline_matmul_f32_f16->l;
  7625. shname = "F32_F16_L";
  7626. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7627. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  7628. shname = "F16_F32_L";
  7629. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7630. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  7631. shname = "F16_L";
  7632. }
  7633. }
  7634. }
  7635. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7636. if (split_k > 1) {
  7637. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7638. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7639. // Resize buffer
  7640. if (ctx->prealloc_split_k != nullptr) {
  7641. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7642. }
  7643. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7644. }
  7645. }
  7646. if (ctx->device->need_compiles) {
  7647. ggml_vk_load_shaders(ctx->device);
  7648. }
  7649. ggml_pipeline_allocate_descriptor_sets(ctx);
  7650. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7651. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7652. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7653. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  7654. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  7655. float* d = (float *) malloc(sizeof(float) * d_ne);
  7656. for (size_t i = 0; i < x_ne; i++) {
  7657. if (std::is_same<float, X_TYPE>()) {
  7658. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7659. // x[i] = 1.0f;
  7660. // x[i] = i + 1;
  7661. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7662. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7663. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7664. // x[i] = ggml_fp32_to_fp16(1.0f);
  7665. // x[i] = ggml_fp32_to_fp16(i + 1);
  7666. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7667. } else {
  7668. GGML_ABORT("fatal error");
  7669. }
  7670. }
  7671. for (size_t i = 0; i < y_ne; i++) {
  7672. if (std::is_same<float, Y_TYPE>()) {
  7673. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7674. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7675. // y[i] = i + 1;
  7676. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7677. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7678. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7679. // y[i] = ggml_fp32_to_fp16(i + 1);
  7680. } else {
  7681. GGML_ABORT("fatal error");
  7682. }
  7683. }
  7684. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  7685. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  7686. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7687. ggml_vk_ctx_begin(ctx->device, subctx);
  7688. for (size_t i = 0; i < num_it; i++) {
  7689. ggml_vk_matmul(
  7690. 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),
  7691. m, n, k,
  7692. k, k, m, k*m, k*n, m*n,
  7693. split_k, batch, batch, batch, 1, 1, n
  7694. );
  7695. }
  7696. ggml_vk_ctx_end(subctx);
  7697. auto begin = std::chrono::high_resolution_clock::now();
  7698. ggml_vk_submit(subctx, ctx->fence);
  7699. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  7700. ctx->device->device.resetFences({ ctx->fence });
  7701. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7702. auto end = std::chrono::high_resolution_clock::now();
  7703. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7704. // copy dst to host
  7705. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  7706. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  7707. ggml_init_params iparams = {
  7708. /*.mem_size =*/ 1024*1024*1024,
  7709. /*.mem_buffer =*/ NULL,
  7710. /*.no_alloc =*/ true,
  7711. };
  7712. ggml_context * ggml_ctx = ggml_init(iparams);
  7713. ggml_type src0_type;
  7714. ggml_type src1_type;
  7715. if (std::is_same<float, X_TYPE>()) {
  7716. src0_type = GGML_TYPE_F32;
  7717. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7718. src0_type = GGML_TYPE_F16;
  7719. } else {
  7720. GGML_ABORT("fatal error");
  7721. }
  7722. if (std::is_same<float, Y_TYPE>()) {
  7723. src1_type = GGML_TYPE_F32;
  7724. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7725. src1_type = GGML_TYPE_F16;
  7726. } else {
  7727. GGML_ABORT("fatal error");
  7728. }
  7729. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  7730. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  7731. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7732. src0_ggml->data = x;
  7733. src1_ggml->data = y;
  7734. tensor_ggml->data = d_chk;
  7735. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7736. ggml_build_forward_expand(cgraph, tensor_ggml);
  7737. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7738. ggml_free(ggml_ctx);
  7739. double avg_err = 0.0;
  7740. int first_err_n = -1;
  7741. int first_err_m = -1;
  7742. int first_err_b = -1;
  7743. for (size_t i = 0; i < m*n*batch; i++) {
  7744. double err = std::fabs(d[i] - d_chk[i]);
  7745. avg_err += err;
  7746. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7747. first_err_b = i / (m * n);
  7748. first_err_n = (i % (m * n)) / m;
  7749. first_err_m = (i % (m * n)) % m;
  7750. }
  7751. }
  7752. avg_err /= m * n;
  7753. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7754. 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;
  7755. if (avg_err > 0.1 || std::isnan(avg_err)) {
  7756. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7757. std::cerr << "Actual result: " << std::endl << std::endl;
  7758. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7759. std::cerr << "Expected result: " << std::endl << std::endl;
  7760. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7761. if (split_k > 1) {
  7762. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7763. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7764. std::cerr << "d_buf0: " << std::endl << std::endl;
  7765. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7766. std::cerr << "d_buf1: " << std::endl << std::endl;
  7767. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7768. std::cerr << "d_buf2: " << std::endl << std::endl;
  7769. 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);
  7770. std::cerr << "d_buf3: " << std::endl << std::endl;
  7771. 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);
  7772. free(split_k_buf);
  7773. }
  7774. }
  7775. free(d_chk);
  7776. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  7777. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  7778. ggml_vk_destroy_buffer(d_X);
  7779. ggml_vk_destroy_buffer(d_Y);
  7780. ggml_vk_destroy_buffer(d_D);
  7781. free(x);
  7782. free(y);
  7783. free(d);
  7784. }
  7785. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  7786. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  7787. return;
  7788. }
  7789. i0 = std::max(i0, 5);
  7790. i1 = std::max(i1, 5);
  7791. i2 = std::max(i2, 0);
  7792. i3 = std::max(i3, 0);
  7793. fprintf(stderr, " ");
  7794. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7795. fprintf(stderr, "%7d ", idx1);
  7796. }
  7797. fprintf(stderr, "\n");
  7798. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7799. fprintf(stderr, "%7d: ", idx0);
  7800. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7801. 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]) {
  7802. float val;
  7803. if (tensor->type == GGML_TYPE_F32) {
  7804. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7805. } else if (tensor->type == GGML_TYPE_F16) {
  7806. 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]));
  7807. } else {
  7808. GGML_ABORT("fatal error");
  7809. }
  7810. fprintf(stderr, "% 7.2f ", val);
  7811. } else {
  7812. fprintf(stderr, " ");
  7813. }
  7814. }
  7815. fprintf(stderr, "\n");
  7816. }
  7817. }
  7818. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  7819. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  7820. }
  7821. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  7822. if (quant == GGML_TYPE_F32) {
  7823. memcpy(to, from, sizeof(float) * ne);
  7824. return;
  7825. }
  7826. const auto * tt = ggml_get_type_traits(quant);
  7827. ggml_to_float_t dequant_fn = tt->to_float;
  7828. dequant_fn(from, to, ne);
  7829. }
  7830. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7831. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  7832. const size_t x_sz = sizeof(float) * ne;
  7833. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  7834. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7835. float * x = (float *) malloc(x_sz);
  7836. void * qx = malloc(qx_sz);
  7837. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7838. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7839. float * x_ref = (float *) malloc(x_sz);
  7840. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  7841. for (size_t i = 0; i < ne; i++) {
  7842. x[i] = rand() / (float)RAND_MAX;
  7843. }
  7844. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  7845. ggml_vk_quantize_data(x, qx, ne, quant);
  7846. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  7847. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7848. if (ctx->device->need_compiles) {
  7849. ggml_vk_load_shaders(ctx->device);
  7850. }
  7851. ggml_pipeline_allocate_descriptor_sets(ctx);
  7852. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7853. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7854. ggml_vk_ctx_begin(ctx->device, subctx);
  7855. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  7856. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc, { (uint32_t)ne, 1, 1});
  7857. ggml_vk_ctx_end(subctx);
  7858. auto begin = std::chrono::high_resolution_clock::now();
  7859. ggml_vk_submit(subctx, ctx->fence);
  7860. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7861. ctx->device->device.resetFences({ ctx->fence });
  7862. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7863. auto end = std::chrono::high_resolution_clock::now();
  7864. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7865. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  7866. int first_err = -1;
  7867. double avg_err = 0.0;
  7868. for (size_t i = 0; i < ne; i++) {
  7869. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  7870. avg_err += error;
  7871. if (first_err < 0 && error > 0.05) {
  7872. first_err = i;
  7873. }
  7874. }
  7875. avg_err /= ne;
  7876. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  7877. if (avg_err > 0.1) {
  7878. std::cerr << "first_error = " << first_err << std::endl;
  7879. std::cerr << "Actual result: " << std::endl << std::endl;
  7880. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7881. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  7882. }
  7883. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  7884. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7885. std::cerr << x_ref[i] << ", ";
  7886. }
  7887. std::cerr << std::endl;
  7888. }
  7889. ggml_vk_destroy_buffer(x_buf);
  7890. ggml_vk_destroy_buffer(qx_buf);
  7891. free(x);
  7892. free(qx);
  7893. free(x_ref);
  7894. free(x_chk);
  7895. }
  7896. // This does not work without ggml q8_1 quantization support
  7897. //
  7898. // typedef uint16_t ggml_half;
  7899. // typedef uint32_t ggml_half2;
  7900. //
  7901. // #define QK8_1 32
  7902. // typedef struct {
  7903. // union {
  7904. // struct {
  7905. // ggml_half d; // delta
  7906. // ggml_half s; // d * sum(qs[i])
  7907. // } GGML_COMMON_AGGR_S;
  7908. // ggml_half2 ds;
  7909. // } GGML_COMMON_AGGR_U;
  7910. // int8_t qs[QK8_1]; // quants
  7911. // } block_q8_1;
  7912. //
  7913. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7914. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  7915. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  7916. //
  7917. // const size_t x_sz = sizeof(float) * ne;
  7918. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7919. // float * x = (float *) malloc(x_sz);
  7920. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  7921. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  7922. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7923. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7924. //
  7925. // for (size_t i = 0; i < ne; i++) {
  7926. // x[i] = rand() / (float)RAND_MAX;
  7927. // }
  7928. //
  7929. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  7930. //
  7931. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7932. //
  7933. // if (ctx->device->need_compiles) {
  7934. // ggml_vk_load_shaders(ctx->device);
  7935. // }
  7936. //
  7937. // ggml_pipeline_allocate_descriptor_sets(ctx);
  7938. //
  7939. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  7940. //
  7941. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7942. // ggml_vk_ctx_begin(ctx->device, subctx);
  7943. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  7944. // ggml_vk_ctx_end(subctx);
  7945. //
  7946. // auto begin = std::chrono::high_resolution_clock::now();
  7947. //
  7948. // ggml_vk_submit(subctx, ctx->fence);
  7949. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  7950. // ctx->device->device.resetFences({ ctx->fence });
  7951. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  7952. //
  7953. // auto end = std::chrono::high_resolution_clock::now();
  7954. //
  7955. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7956. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  7957. //
  7958. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  7959. //
  7960. // int first_err = -1;
  7961. //
  7962. // for (size_t i = 0; i < ne / 32; i++) {
  7963. // double error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d));
  7964. //
  7965. // if (first_err < 0 && error > 0.1) {
  7966. // first_err = i;
  7967. // }
  7968. //
  7969. // error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s));
  7970. //
  7971. // if (first_err < 0 && error > 0.1) {
  7972. // first_err = i;
  7973. // }
  7974. //
  7975. // for (size_t j = 0; j < 32; j++) {
  7976. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  7977. //
  7978. // if (first_err < 0 && error > 1) {
  7979. // first_err = i;
  7980. // }
  7981. // }
  7982. // }
  7983. //
  7984. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  7985. //
  7986. // if (first_err != -1) {
  7987. // std::cerr << "first_error = " << first_err << std::endl;
  7988. // std::cerr << "Actual result: " << std::endl << std::endl;
  7989. // std::cout << "d=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  7990. // for (size_t j = 0; j < 32; j++) {
  7991. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  7992. // }
  7993. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  7994. // std::cout << "d=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  7995. // for (size_t j = 0; j < 32; j++) {
  7996. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  7997. // }
  7998. // std::cerr << std::endl;
  7999. // }
  8000. //
  8001. // ggml_vk_destroy_buffer(x_buf);
  8002. // ggml_vk_destroy_buffer(qx_buf);
  8003. //
  8004. // free(x);
  8005. // free(qx);
  8006. // free(qx_res);
  8007. // }
  8008. static void ggml_vk_test_dequant_matmul(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, size_t split_k, size_t shader_size, ggml_type quant, bool mmq = false) {
  8009. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  8010. const size_t x_ne = m * k * batch;
  8011. const size_t y_ne = k * n * batch;
  8012. const size_t d_ne = m * n * batch;
  8013. vk_matmul_pipeline2 * pipelines;
  8014. if (mmq) {
  8015. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  8016. } else {
  8017. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  8018. }
  8019. const bool fp16acc = ctx->device->fp16;
  8020. vk_pipeline p;
  8021. std::string shname;
  8022. if (shader_size == 0) {
  8023. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  8024. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  8025. } else if (shader_size == 1) {
  8026. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  8027. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  8028. } else if (shader_size == 2) {
  8029. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  8030. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  8031. } else {
  8032. GGML_ASSERT(0);
  8033. }
  8034. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  8035. if (mmq || k != kpad) {
  8036. if (shader_size == 0) {
  8037. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  8038. shname = std::string(ggml_type_name(quant)) + "_S";
  8039. } else if (shader_size == 1) {
  8040. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  8041. shname = std::string(ggml_type_name(quant)) + "_M";
  8042. } else if (shader_size == 2) {
  8043. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  8044. shname = std::string(ggml_type_name(quant)) + "_L";
  8045. } else {
  8046. GGML_ASSERT(0);
  8047. }
  8048. }
  8049. if (p == nullptr) {
  8050. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  8051. return;
  8052. }
  8053. const size_t x_sz = sizeof(float) * x_ne;
  8054. const size_t y_sz = sizeof(float) * y_ne;
  8055. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  8056. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  8057. const size_t d_sz = sizeof(float) * d_ne;
  8058. float * x = (float *) malloc(x_sz);
  8059. float * y = (float *) malloc(y_sz);
  8060. void * qx = malloc(qx_sz);
  8061. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  8062. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  8063. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  8064. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  8065. float * d = (float *) malloc(d_sz);
  8066. float * d_chk = (float *) malloc(d_sz);
  8067. for (size_t i = 0; i < x_ne; i++) {
  8068. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8069. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8070. // x[i] = i % k;
  8071. }
  8072. ggml_vk_quantize_data(x, qx, x_ne, quant);
  8073. for (size_t i = 0; i < y_ne; i++) {
  8074. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8075. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8076. // y[i] = i % k;
  8077. }
  8078. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  8079. if (split_k > 1) {
  8080. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  8081. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  8082. // Resize buffer
  8083. if (ctx->prealloc_split_k != nullptr) {
  8084. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8085. }
  8086. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  8087. }
  8088. }
  8089. if (mmq) {
  8090. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  8091. }
  8092. if (ctx->device->need_compiles) {
  8093. ggml_vk_load_shaders(ctx->device);
  8094. }
  8095. ggml_pipeline_allocate_descriptor_sets(ctx);
  8096. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  8097. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  8098. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8099. ggml_vk_ctx_begin(ctx->device, subctx);
  8100. if (mmq) {
  8101. for (size_t i = 0; i < num_it; i++) {
  8102. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  8103. ggml_vk_matmul(
  8104. ctx, subctx, p, { qx_buf, 0, qx_sz }, { qy_buf, 0, qy_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  8105. m, n, k,
  8106. k, k, m, k*m, k*n, m*n,
  8107. split_k, batch, batch, batch, 1, 1, n
  8108. );
  8109. }
  8110. } else {
  8111. for (size_t i = 0; i < num_it; i++) {
  8112. ggml_vk_matmul(
  8113. ctx, subctx, p, { qx_buf, 0, qx_sz }, { y_buf, 0, y_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  8114. m, n, k,
  8115. k, k, m, k*m, k*n, m*n,
  8116. split_k, batch, batch, batch, 1, 1, n
  8117. );
  8118. }
  8119. }
  8120. ggml_vk_ctx_end(subctx);
  8121. auto begin = std::chrono::high_resolution_clock::now();
  8122. ggml_vk_submit(subctx, ctx->fence);
  8123. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  8124. ctx->device->device.resetFences({ ctx->fence });
  8125. ggml_vk_queue_command_pools_cleanup(ctx->device);
  8126. auto end = std::chrono::high_resolution_clock::now();
  8127. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  8128. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  8129. ggml_init_params iparams = {
  8130. /*.mem_size =*/ 1024*1024*1024,
  8131. /*.mem_buffer =*/ NULL,
  8132. /*.no_alloc =*/ true,
  8133. };
  8134. ggml_context * ggml_ctx = ggml_init(iparams);
  8135. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  8136. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  8137. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  8138. src0_ggml->data = qx;
  8139. src1_ggml->data = y;
  8140. tensor_ggml->data = d_chk;
  8141. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8142. ggml_build_forward_expand(cgraph, tensor_ggml);
  8143. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  8144. ggml_free(ggml_ctx);
  8145. double avg_err = 0.0;
  8146. int first_err_n = -1;
  8147. int first_err_m = -1;
  8148. int first_err_b = -1;
  8149. for (size_t i = 0; i < m*n*batch; i++) {
  8150. double err = std::fabs(d[i] - d_chk[i]);
  8151. avg_err += err;
  8152. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  8153. first_err_b = i / (m * n);
  8154. first_err_n = (i % (m * n)) / m;
  8155. first_err_m = (i % (m * n)) % m;
  8156. }
  8157. }
  8158. avg_err /= m * n;
  8159. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  8160. std::cerr << "TEST dequant matmul " << shname;
  8161. if (mmq) {
  8162. std::cerr << " mmq";
  8163. }
  8164. std::cerr << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  8165. if (avg_err > 0.01 || std::isnan(avg_err)) {
  8166. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  8167. std::cerr << "Actual result: " << std::endl << std::endl;
  8168. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8169. std::cerr << std::endl;
  8170. std::cerr << "Expected result: " << std::endl << std::endl;
  8171. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8172. std::cerr << "src0: " << std::endl << std::endl;
  8173. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  8174. std::cerr << std::endl;
  8175. std::cerr << "src1: " << std::endl << std::endl;
  8176. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  8177. if (split_k > 1) {
  8178. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  8179. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  8180. std::cerr << "d_buf0: " << std::endl << std::endl;
  8181. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8182. std::cerr << "d_buf1: " << std::endl << std::endl;
  8183. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8184. std::cerr << "d_buf2: " << std::endl << std::endl;
  8185. 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);
  8186. std::cerr << "d_buf3: " << std::endl << std::endl;
  8187. 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);
  8188. free(split_k_buf);
  8189. }
  8190. }
  8191. ggml_vk_destroy_buffer(qx_buf);
  8192. ggml_vk_destroy_buffer(y_buf);
  8193. ggml_vk_destroy_buffer(qy_buf);
  8194. ggml_vk_destroy_buffer(d_buf);
  8195. free(x);
  8196. free(qx);
  8197. free(y);
  8198. free(d);
  8199. free(d_chk);
  8200. }
  8201. #endif
  8202. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  8203. #if defined(GGML_VULKAN_RUN_TESTS)
  8204. const std::vector<size_t> vals {
  8205. 512, 512, 128,
  8206. 128, 512, 512,
  8207. 4096, 512, 4096,
  8208. 11008, 512, 4096,
  8209. 4096, 512, 11008,
  8210. 32000, 512, 4096,
  8211. 8, 8, 8,
  8212. 100, 46, 576,
  8213. 623, 111, 128,
  8214. 100, 46, 558,
  8215. 512, 1, 256,
  8216. 128, 110, 622,
  8217. 511, 511, 127,
  8218. 511, 511, 7,
  8219. 511, 511, 17,
  8220. 49, 49, 128,
  8221. 128, 49, 49,
  8222. 4096, 49, 4096,
  8223. };
  8224. const size_t num_it = 100;
  8225. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  8226. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  8227. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  8228. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  8229. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  8230. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  8231. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  8232. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  8233. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  8234. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  8235. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  8236. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  8237. abort();
  8238. for (size_t i = 0; i < vals.size(); i += 3) {
  8239. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  8240. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  8241. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  8242. std::cerr << '\n';
  8243. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  8244. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  8245. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  8246. std::cerr << '\n';
  8247. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  8248. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  8249. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  8250. std::cerr << '\n' << std::endl;
  8251. if (vals[i + 2] % 32 == 0) {
  8252. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  8253. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  8254. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  8255. std::cerr << '\n';
  8256. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  8257. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  8258. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  8259. std::cerr << '\n';
  8260. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  8261. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  8262. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  8263. std::cerr << '\n' << std::endl;
  8264. }
  8265. if (vals[i + 2] % 256 == 0) {
  8266. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  8267. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  8268. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  8269. std::cerr << '\n';
  8270. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  8271. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  8272. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  8273. std::cerr << '\n';
  8274. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  8275. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  8276. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  8277. std::cerr << '\n' << std::endl;
  8278. }
  8279. }
  8280. GGML_ABORT("fatal error");
  8281. #endif
  8282. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  8283. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  8284. // Resize buffer
  8285. if (ctx->prealloc_x != nullptr) {
  8286. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8287. }
  8288. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  8289. }
  8290. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  8291. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  8292. // Resize buffer
  8293. if (ctx->prealloc_y != nullptr) {
  8294. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8295. }
  8296. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  8297. }
  8298. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  8299. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  8300. // Resize buffer
  8301. if (ctx->prealloc_split_k != nullptr) {
  8302. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8303. }
  8304. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  8305. }
  8306. }
  8307. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_cgraph * cgraph, ggml_tensor* tensor, int tensor_idx, bool use_fence, bool almost_ready);
  8308. // Returns true if node has enqueued work into the queue, false otherwise
  8309. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  8310. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
  8311. ggml_tensor * node = cgraph->nodes[node_idx];
  8312. if (ggml_is_empty(node) || !node->buffer) {
  8313. return false;
  8314. }
  8315. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  8316. ctx->semaphore_idx = 0;
  8317. const ggml_tensor * src0 = node->src[0];
  8318. const ggml_tensor * src1 = node->src[1];
  8319. const ggml_tensor * src2 = node->src[2];
  8320. const ggml_tensor * src3 = node->src[3];
  8321. switch (node->op) {
  8322. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  8323. case GGML_OP_RESHAPE:
  8324. case GGML_OP_VIEW:
  8325. case GGML_OP_PERMUTE:
  8326. case GGML_OP_TRANSPOSE:
  8327. case GGML_OP_NONE:
  8328. return false;
  8329. case GGML_OP_UNARY:
  8330. switch (ggml_get_unary_op(node)) {
  8331. case GGML_UNARY_OP_SILU:
  8332. case GGML_UNARY_OP_GELU:
  8333. case GGML_UNARY_OP_GELU_ERF:
  8334. case GGML_UNARY_OP_GELU_QUICK:
  8335. case GGML_UNARY_OP_RELU:
  8336. case GGML_UNARY_OP_TANH:
  8337. case GGML_UNARY_OP_SIGMOID:
  8338. break;
  8339. default:
  8340. return false;
  8341. }
  8342. break;
  8343. case GGML_OP_GLU:
  8344. switch (ggml_get_glu_op(node)) {
  8345. case GGML_GLU_OP_GEGLU:
  8346. case GGML_GLU_OP_REGLU:
  8347. case GGML_GLU_OP_SWIGLU:
  8348. case GGML_GLU_OP_SWIGLU_OAI:
  8349. case GGML_GLU_OP_GEGLU_ERF:
  8350. case GGML_GLU_OP_GEGLU_QUICK:
  8351. break;
  8352. default:
  8353. return false;
  8354. }
  8355. break;
  8356. case GGML_OP_REPEAT:
  8357. case GGML_OP_REPEAT_BACK:
  8358. case GGML_OP_GET_ROWS:
  8359. case GGML_OP_ADD:
  8360. case GGML_OP_ADD_ID:
  8361. case GGML_OP_ACC:
  8362. case GGML_OP_SUB:
  8363. case GGML_OP_MUL:
  8364. case GGML_OP_DIV:
  8365. case GGML_OP_CONCAT:
  8366. case GGML_OP_UPSCALE:
  8367. case GGML_OP_SCALE:
  8368. case GGML_OP_SQR:
  8369. case GGML_OP_SQRT:
  8370. case GGML_OP_SIN:
  8371. case GGML_OP_COS:
  8372. case GGML_OP_CLAMP:
  8373. case GGML_OP_PAD:
  8374. case GGML_OP_ROLL:
  8375. case GGML_OP_CPY:
  8376. case GGML_OP_SET_ROWS:
  8377. case GGML_OP_CONT:
  8378. case GGML_OP_DUP:
  8379. case GGML_OP_SILU_BACK:
  8380. case GGML_OP_NORM:
  8381. case GGML_OP_GROUP_NORM:
  8382. case GGML_OP_RMS_NORM:
  8383. case GGML_OP_RMS_NORM_BACK:
  8384. case GGML_OP_L2_NORM:
  8385. case GGML_OP_DIAG_MASK_INF:
  8386. case GGML_OP_SOFT_MAX:
  8387. case GGML_OP_SOFT_MAX_BACK:
  8388. case GGML_OP_ROPE:
  8389. case GGML_OP_ROPE_BACK:
  8390. case GGML_OP_MUL_MAT:
  8391. case GGML_OP_MUL_MAT_ID:
  8392. case GGML_OP_ARGSORT:
  8393. case GGML_OP_SUM:
  8394. case GGML_OP_SUM_ROWS:
  8395. case GGML_OP_ARGMAX:
  8396. case GGML_OP_COUNT_EQUAL:
  8397. case GGML_OP_IM2COL:
  8398. case GGML_OP_TIMESTEP_EMBEDDING:
  8399. case GGML_OP_CONV_TRANSPOSE_1D:
  8400. case GGML_OP_POOL_2D:
  8401. case GGML_OP_CONV_2D:
  8402. case GGML_OP_CONV_2D_DW:
  8403. case GGML_OP_RWKV_WKV6:
  8404. case GGML_OP_RWKV_WKV7:
  8405. case GGML_OP_LEAKY_RELU:
  8406. case GGML_OP_FLASH_ATTN_EXT:
  8407. case GGML_OP_OPT_STEP_ADAMW:
  8408. case GGML_OP_OPT_STEP_SGD:
  8409. break;
  8410. default:
  8411. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  8412. GGML_ABORT("fatal error");
  8413. }
  8414. vk_context compute_ctx;
  8415. if (!dryrun) {
  8416. if (ctx->compute_ctx.expired()) {
  8417. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8418. ctx->compute_ctx = compute_ctx;
  8419. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8420. } else {
  8421. compute_ctx = ctx->compute_ctx.lock();
  8422. }
  8423. } else {
  8424. switch (node->op) {
  8425. case GGML_OP_REPEAT:
  8426. case GGML_OP_REPEAT_BACK:
  8427. case GGML_OP_ACC:
  8428. case GGML_OP_GET_ROWS:
  8429. case GGML_OP_ADD:
  8430. case GGML_OP_SUB:
  8431. case GGML_OP_MUL:
  8432. case GGML_OP_DIV:
  8433. case GGML_OP_CONCAT:
  8434. case GGML_OP_UPSCALE:
  8435. case GGML_OP_SCALE:
  8436. case GGML_OP_SQR:
  8437. case GGML_OP_SQRT:
  8438. case GGML_OP_SIN:
  8439. case GGML_OP_COS:
  8440. case GGML_OP_CLAMP:
  8441. case GGML_OP_PAD:
  8442. case GGML_OP_CPY:
  8443. case GGML_OP_SET_ROWS:
  8444. case GGML_OP_CONT:
  8445. case GGML_OP_DUP:
  8446. case GGML_OP_SILU_BACK:
  8447. case GGML_OP_NORM:
  8448. case GGML_OP_GROUP_NORM:
  8449. case GGML_OP_RMS_NORM:
  8450. case GGML_OP_RMS_NORM_BACK:
  8451. case GGML_OP_L2_NORM:
  8452. case GGML_OP_UNARY:
  8453. case GGML_OP_GLU:
  8454. case GGML_OP_DIAG_MASK_INF:
  8455. case GGML_OP_SOFT_MAX:
  8456. case GGML_OP_SOFT_MAX_BACK:
  8457. case GGML_OP_ROPE:
  8458. case GGML_OP_ROPE_BACK:
  8459. case GGML_OP_ARGSORT:
  8460. case GGML_OP_SUM:
  8461. case GGML_OP_SUM_ROWS:
  8462. case GGML_OP_ARGMAX:
  8463. case GGML_OP_COUNT_EQUAL:
  8464. case GGML_OP_IM2COL:
  8465. case GGML_OP_TIMESTEP_EMBEDDING:
  8466. case GGML_OP_CONV_TRANSPOSE_1D:
  8467. case GGML_OP_POOL_2D:
  8468. case GGML_OP_CONV_2D:
  8469. case GGML_OP_CONV_2D_DW:
  8470. case GGML_OP_LEAKY_RELU:
  8471. case GGML_OP_OPT_STEP_SGD:
  8472. {
  8473. // These operations all go through ggml_vk_op_f32, so short-circuit and
  8474. // do the only thing needed for the dryrun.
  8475. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  8476. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8477. return false;
  8478. }
  8479. default:
  8480. break;
  8481. }
  8482. }
  8483. switch (node->op) {
  8484. case GGML_OP_REPEAT:
  8485. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  8486. break;
  8487. case GGML_OP_REPEAT_BACK:
  8488. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  8489. break;
  8490. case GGML_OP_ACC:
  8491. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  8492. break;
  8493. case GGML_OP_GET_ROWS:
  8494. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  8495. break;
  8496. case GGML_OP_ADD:
  8497. if (ctx->num_additional_fused_ops) {
  8498. ggml_vk_multi_add(ctx, compute_ctx, cgraph, node_idx, dryrun);
  8499. } else {
  8500. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  8501. }
  8502. break;
  8503. case GGML_OP_SUB:
  8504. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  8505. break;
  8506. case GGML_OP_MUL:
  8507. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  8508. break;
  8509. case GGML_OP_DIV:
  8510. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  8511. break;
  8512. case GGML_OP_ADD_ID:
  8513. ggml_vk_add_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8514. break;
  8515. case GGML_OP_CONCAT:
  8516. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  8517. break;
  8518. case GGML_OP_UPSCALE:
  8519. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  8520. break;
  8521. case GGML_OP_SCALE:
  8522. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  8523. break;
  8524. case GGML_OP_SQR:
  8525. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  8526. break;
  8527. case GGML_OP_SQRT:
  8528. ggml_vk_sqrt(ctx, compute_ctx, src0, node, dryrun);
  8529. break;
  8530. case GGML_OP_SIN:
  8531. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  8532. break;
  8533. case GGML_OP_COS:
  8534. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  8535. break;
  8536. case GGML_OP_CLAMP:
  8537. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  8538. break;
  8539. case GGML_OP_PAD:
  8540. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  8541. break;
  8542. case GGML_OP_ROLL:
  8543. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  8544. break;
  8545. case GGML_OP_CPY:
  8546. case GGML_OP_CONT:
  8547. case GGML_OP_DUP:
  8548. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  8549. break;
  8550. case GGML_OP_SET_ROWS:
  8551. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  8552. break;
  8553. case GGML_OP_SILU_BACK:
  8554. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  8555. break;
  8556. case GGML_OP_NORM:
  8557. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  8558. break;
  8559. case GGML_OP_GROUP_NORM:
  8560. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  8561. break;
  8562. case GGML_OP_RMS_NORM:
  8563. if (ctx->num_additional_fused_ops > 0) {
  8564. // fused rms_norm + mul
  8565. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  8566. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  8567. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  8568. } else {
  8569. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  8570. }
  8571. break;
  8572. case GGML_OP_RMS_NORM_BACK:
  8573. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  8574. break;
  8575. case GGML_OP_L2_NORM:
  8576. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  8577. break;
  8578. case GGML_OP_UNARY:
  8579. switch (ggml_get_unary_op(node)) {
  8580. case GGML_UNARY_OP_SILU:
  8581. case GGML_UNARY_OP_GELU:
  8582. case GGML_UNARY_OP_GELU_ERF:
  8583. case GGML_UNARY_OP_GELU_QUICK:
  8584. case GGML_UNARY_OP_RELU:
  8585. case GGML_UNARY_OP_TANH:
  8586. case GGML_UNARY_OP_SIGMOID:
  8587. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  8588. break;
  8589. default:
  8590. return false;
  8591. }
  8592. break;
  8593. case GGML_OP_GLU:
  8594. switch (ggml_get_glu_op(node)) {
  8595. case GGML_GLU_OP_GEGLU:
  8596. case GGML_GLU_OP_REGLU:
  8597. case GGML_GLU_OP_SWIGLU:
  8598. case GGML_GLU_OP_SWIGLU_OAI:
  8599. case GGML_GLU_OP_GEGLU_ERF:
  8600. case GGML_GLU_OP_GEGLU_QUICK:
  8601. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  8602. break;
  8603. default:
  8604. return false;
  8605. }
  8606. break;
  8607. case GGML_OP_DIAG_MASK_INF:
  8608. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  8609. break;
  8610. case GGML_OP_SOFT_MAX:
  8611. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8612. break;
  8613. case GGML_OP_SOFT_MAX_BACK:
  8614. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  8615. break;
  8616. case GGML_OP_ROPE:
  8617. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  8618. break;
  8619. case GGML_OP_ROPE_BACK:
  8620. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  8621. break;
  8622. case GGML_OP_ARGSORT:
  8623. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  8624. break;
  8625. case GGML_OP_SUM:
  8626. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  8627. break;
  8628. case GGML_OP_SUM_ROWS:
  8629. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  8630. break;
  8631. case GGML_OP_ARGMAX:
  8632. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  8633. break;
  8634. case GGML_OP_COUNT_EQUAL:
  8635. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  8636. break;
  8637. case GGML_OP_IM2COL:
  8638. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  8639. break;
  8640. case GGML_OP_TIMESTEP_EMBEDDING:
  8641. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  8642. break;
  8643. case GGML_OP_CONV_TRANSPOSE_1D:
  8644. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  8645. break;
  8646. case GGML_OP_POOL_2D:
  8647. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  8648. break;
  8649. case GGML_OP_CONV_2D:
  8650. ggml_vk_conv_2d(ctx, compute_ctx, src0, src1, node, dryrun);
  8651. break;
  8652. case GGML_OP_CONV_2D_DW:
  8653. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  8654. break;
  8655. case GGML_OP_LEAKY_RELU:
  8656. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  8657. break;
  8658. case GGML_OP_MUL_MAT:
  8659. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  8660. break;
  8661. case GGML_OP_MUL_MAT_ID:
  8662. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8663. break;
  8664. case GGML_OP_FLASH_ATTN_EXT:
  8665. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node->src[4], node, dryrun);
  8666. break;
  8667. case GGML_OP_RWKV_WKV6:
  8668. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  8669. break;
  8670. case GGML_OP_RWKV_WKV7:
  8671. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  8672. break;
  8673. case GGML_OP_OPT_STEP_ADAMW:
  8674. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  8675. break;
  8676. case GGML_OP_OPT_STEP_SGD:
  8677. ggml_vk_opt_step_sgd(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8678. break;
  8679. default:
  8680. return false;
  8681. }
  8682. if (dryrun) {
  8683. return false;
  8684. }
  8685. ctx->tensor_ctxs[node_idx] = compute_ctx;
  8686. #if defined(GGML_VULKAN_CHECK_RESULTS)
  8687. // Force context reset on each node so that each tensor ends up in its own context
  8688. // and can be run and compared to its CPU equivalent separately
  8689. last_node = true;
  8690. #endif
  8691. if (submit || last_node) {
  8692. ggml_vk_ctx_end(compute_ctx);
  8693. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  8694. if (last_node) {
  8695. compute_ctx->exit_tensor_idx = node_idx_begin;
  8696. }
  8697. else {
  8698. compute_ctx->exit_tensor_idx = -1;
  8699. }
  8700. ctx->compute_ctx.reset();
  8701. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  8702. if (!ok) {
  8703. if (node->op == GGML_OP_UNARY) {
  8704. 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;
  8705. } else if (node->op == GGML_OP_GLU) {
  8706. std::cerr << __func__ << ": error: op not supported GLU " << node->name << " (" << ggml_glu_op_name(static_cast<ggml_glu_op>(node->op_params[0])) << ")" << std::endl;
  8707. } else {
  8708. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  8709. }
  8710. }
  8711. }
  8712. return true;
  8713. }
  8714. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, ggml_tensor * tensor, int tensor_idx, bool use_fence = true, bool almost_ready = false) {
  8715. GGML_UNUSED(cgraph);
  8716. ggml_backend_buffer * buf = nullptr;
  8717. switch (tensor->op) {
  8718. case GGML_OP_ADD:
  8719. case GGML_OP_ACC:
  8720. case GGML_OP_GET_ROWS:
  8721. case GGML_OP_SUB:
  8722. case GGML_OP_MUL:
  8723. case GGML_OP_DIV:
  8724. case GGML_OP_ADD_ID:
  8725. case GGML_OP_CONCAT:
  8726. case GGML_OP_UPSCALE:
  8727. case GGML_OP_SCALE:
  8728. case GGML_OP_SQR:
  8729. case GGML_OP_SQRT:
  8730. case GGML_OP_SIN:
  8731. case GGML_OP_COS:
  8732. case GGML_OP_CLAMP:
  8733. case GGML_OP_PAD:
  8734. case GGML_OP_ROLL:
  8735. case GGML_OP_CPY:
  8736. case GGML_OP_SET_ROWS:
  8737. case GGML_OP_CONT:
  8738. case GGML_OP_DUP:
  8739. case GGML_OP_SILU_BACK:
  8740. case GGML_OP_NORM:
  8741. case GGML_OP_GROUP_NORM:
  8742. case GGML_OP_RMS_NORM:
  8743. case GGML_OP_RMS_NORM_BACK:
  8744. case GGML_OP_L2_NORM:
  8745. case GGML_OP_DIAG_MASK_INF:
  8746. case GGML_OP_SOFT_MAX:
  8747. case GGML_OP_SOFT_MAX_BACK:
  8748. case GGML_OP_ROPE:
  8749. case GGML_OP_ROPE_BACK:
  8750. case GGML_OP_RESHAPE:
  8751. case GGML_OP_VIEW:
  8752. case GGML_OP_PERMUTE:
  8753. case GGML_OP_TRANSPOSE:
  8754. case GGML_OP_NONE:
  8755. case GGML_OP_ARGSORT:
  8756. case GGML_OP_SUM:
  8757. case GGML_OP_SUM_ROWS:
  8758. case GGML_OP_ARGMAX:
  8759. case GGML_OP_COUNT_EQUAL:
  8760. case GGML_OP_IM2COL:
  8761. case GGML_OP_TIMESTEP_EMBEDDING:
  8762. case GGML_OP_CONV_TRANSPOSE_1D:
  8763. case GGML_OP_POOL_2D:
  8764. case GGML_OP_CONV_2D:
  8765. case GGML_OP_CONV_2D_DW:
  8766. case GGML_OP_RWKV_WKV6:
  8767. case GGML_OP_RWKV_WKV7:
  8768. case GGML_OP_LEAKY_RELU:
  8769. case GGML_OP_REPEAT:
  8770. case GGML_OP_REPEAT_BACK:
  8771. case GGML_OP_OPT_STEP_ADAMW:
  8772. case GGML_OP_OPT_STEP_SGD:
  8773. buf = tensor->buffer;
  8774. break;
  8775. case GGML_OP_UNARY:
  8776. switch (ggml_get_unary_op(tensor)) {
  8777. case GGML_UNARY_OP_SILU:
  8778. case GGML_UNARY_OP_GELU:
  8779. case GGML_UNARY_OP_GELU_ERF:
  8780. case GGML_UNARY_OP_GELU_QUICK:
  8781. case GGML_UNARY_OP_RELU:
  8782. case GGML_UNARY_OP_TANH:
  8783. case GGML_UNARY_OP_SIGMOID:
  8784. buf = tensor->buffer;
  8785. break;
  8786. default:
  8787. return false;
  8788. }
  8789. break;
  8790. case GGML_OP_GLU:
  8791. switch (ggml_get_glu_op(tensor)) {
  8792. case GGML_GLU_OP_GEGLU:
  8793. case GGML_GLU_OP_REGLU:
  8794. case GGML_GLU_OP_SWIGLU:
  8795. case GGML_GLU_OP_SWIGLU_OAI:
  8796. case GGML_GLU_OP_GEGLU_ERF:
  8797. case GGML_GLU_OP_GEGLU_QUICK:
  8798. buf = tensor->buffer;
  8799. break;
  8800. default:
  8801. return false;
  8802. }
  8803. break;
  8804. case GGML_OP_MUL_MAT:
  8805. case GGML_OP_MUL_MAT_ID:
  8806. case GGML_OP_FLASH_ATTN_EXT:
  8807. buf = tensor->buffer;
  8808. break;
  8809. default:
  8810. return false;
  8811. }
  8812. if (buf == nullptr) {
  8813. return false;
  8814. }
  8815. 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 << ")");
  8816. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  8817. // always wait for the GPU work to be done for the last submit
  8818. if (tensor_idx == subctx->exit_tensor_idx) {
  8819. use_fence = true;
  8820. }
  8821. // Only run if ctx hasn't been submitted yet
  8822. if (!subctx->seqs.empty()) {
  8823. #ifdef GGML_VULKAN_CHECK_RESULTS
  8824. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  8825. use_fence = true;
  8826. #endif
  8827. // Do staging buffer copies
  8828. for (auto& cpy : subctx->in_memcpys) {
  8829. memcpy(cpy.dst, cpy.src, cpy.n);
  8830. }
  8831. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  8832. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  8833. ctx->almost_ready_fence_pending = true;
  8834. } else {
  8835. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  8836. }
  8837. if (use_fence) {
  8838. ggml_vk_wait_for_fence(ctx);
  8839. }
  8840. #ifdef GGML_VULKAN_CHECK_RESULTS
  8841. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  8842. #endif
  8843. }
  8844. if (tensor_idx == subctx->exit_tensor_idx) {
  8845. // Do staging buffer copies
  8846. for (auto& cpy : subctx->out_memcpys) {
  8847. memcpy(cpy.dst, cpy.src, cpy.n);
  8848. }
  8849. subctx->in_memcpys.clear();
  8850. subctx->out_memcpys.clear();
  8851. }
  8852. return true;
  8853. }
  8854. // Clean up after graph processing is done
  8855. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  8856. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  8857. for (auto& buffer : ctx->gc.temp_buffers) {
  8858. ggml_vk_pool_free(ctx, buffer);
  8859. }
  8860. ctx->gc.temp_buffers.clear();
  8861. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8862. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8863. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  8864. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  8865. }
  8866. ctx->gc.semaphores.clear();
  8867. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  8868. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  8869. }
  8870. ctx->gc.tl_semaphores.clear();
  8871. ctx->semaphore_idx = 0;
  8872. ctx->event_idx = 0;
  8873. for (auto& event : ctx->gc.events) {
  8874. ctx->device->device.resetEvent(event);
  8875. }
  8876. ctx->tensor_ctxs.clear();
  8877. ctx->gc.contexts.clear();
  8878. ctx->pipeline_descriptor_set_requirements = 0;
  8879. ctx->descriptor_set_idx = 0;
  8880. }
  8881. // Clean up on backend free
  8882. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  8883. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  8884. ggml_vk_graph_cleanup(ctx);
  8885. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8886. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8887. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8888. for (auto& buffer : ctx->buffer_pool) {
  8889. ggml_vk_destroy_buffer(buffer);
  8890. }
  8891. ctx->prealloc_size_x = 0;
  8892. ctx->prealloc_size_y = 0;
  8893. ctx->prealloc_size_split_k = 0;
  8894. for (auto& event : ctx->gc.events) {
  8895. ctx->device->device.destroyEvent(event);
  8896. }
  8897. ctx->gc.events.clear();
  8898. ctx->device->device.destroyFence(ctx->fence);
  8899. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  8900. for (auto& pool : ctx->descriptor_pools) {
  8901. ctx->device->device.destroyDescriptorPool(pool);
  8902. }
  8903. ctx->descriptor_pools.clear();
  8904. ctx->descriptor_sets.clear();
  8905. ctx->compute_cmd_pool.destroy(ctx->device->device);
  8906. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  8907. }
  8908. static int ggml_vk_get_device_count() {
  8909. ggml_vk_instance_init();
  8910. return vk_instance.device_indices.size();
  8911. }
  8912. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  8913. ggml_vk_instance_init();
  8914. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  8915. vk::PhysicalDeviceProperties props;
  8916. devices[device].getProperties(&props);
  8917. snprintf(description, description_size, "%s", props.deviceName.data());
  8918. }
  8919. // backend interface
  8920. #define UNUSED GGML_UNUSED
  8921. // device backend
  8922. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  8923. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  8924. }
  8925. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8926. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  8927. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8928. ggml_vk_destroy_buffer(ctx->dev_buffer);
  8929. delete ctx;
  8930. }
  8931. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  8932. return vk_ptr_base;
  8933. UNUSED(buffer);
  8934. }
  8935. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8936. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  8937. if (tensor->view_src != nullptr) {
  8938. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  8939. }
  8940. return GGML_STATUS_SUCCESS;
  8941. }
  8942. 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) {
  8943. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  8944. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8945. vk_buffer buf = buf_ctx->dev_buffer;
  8946. uint32_t val32 = (uint32_t)value * 0x01010101;
  8947. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  8948. }
  8949. 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) {
  8950. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8951. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8952. vk_buffer buf = buf_ctx->dev_buffer;
  8953. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8954. }
  8955. 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) {
  8956. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8957. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8958. vk_buffer buf = buf_ctx->dev_buffer;
  8959. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8960. }
  8961. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8962. if (ggml_backend_buffer_is_vk(src->buffer)) {
  8963. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8964. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8965. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8966. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8967. 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));
  8968. return true;
  8969. }
  8970. return false;
  8971. UNUSED(buffer);
  8972. }
  8973. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8974. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8975. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  8976. }
  8977. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  8978. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  8979. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  8980. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  8981. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  8982. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  8983. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  8984. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  8985. /* .clear = */ ggml_backend_vk_buffer_clear,
  8986. /* .reset = */ NULL,
  8987. };
  8988. // vk buffer type
  8989. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8990. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8991. return ctx->name.c_str();
  8992. }
  8993. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8994. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  8995. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8996. vk_buffer dev_buffer = nullptr;
  8997. try {
  8998. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  8999. } catch (const vk::SystemError& e) {
  9000. return nullptr;
  9001. }
  9002. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  9003. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  9004. }
  9005. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9006. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  9007. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  9008. }
  9009. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  9010. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  9011. return ctx->device->suballocation_block_size;
  9012. }
  9013. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9014. return ggml_nbytes(tensor);
  9015. UNUSED(buft);
  9016. }
  9017. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  9018. ggml_vk_instance_init();
  9019. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  9020. vk_device dev = ggml_vk_get_device(dev_num);
  9021. return &dev->buffer_type;
  9022. }
  9023. // host buffer type
  9024. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9025. return GGML_VK_NAME "_Host";
  9026. UNUSED(buft);
  9027. }
  9028. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  9029. return GGML_VK_NAME "_Host";
  9030. UNUSED(buffer);
  9031. }
  9032. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9033. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  9034. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  9035. }
  9036. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9037. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  9038. size += 32; // Behave like the CPU buffer type
  9039. void * ptr = nullptr;
  9040. try {
  9041. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  9042. } catch (vk::SystemError& e) {
  9043. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  9044. // fallback to cpu buffer
  9045. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9046. }
  9047. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9048. buffer->buft = buft;
  9049. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  9050. return buffer;
  9051. UNUSED(buft);
  9052. }
  9053. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9054. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  9055. UNUSED(buft);
  9056. }
  9057. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  9058. return vk_instance.devices[0]->suballocation_block_size;
  9059. UNUSED(buft);
  9060. }
  9061. // Should be changed to return device-specific host buffer type
  9062. // but that probably requires changes in llama.cpp
  9063. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  9064. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  9065. /* .iface = */ {
  9066. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  9067. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  9068. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  9069. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  9070. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9071. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9072. },
  9073. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  9074. /* .context = */ nullptr,
  9075. };
  9076. // Make sure device 0 is initialized
  9077. ggml_vk_instance_init();
  9078. ggml_vk_get_device(0);
  9079. return &ggml_backend_vk_buffer_type_host;
  9080. }
  9081. // backend
  9082. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  9083. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9084. return ctx->name.c_str();
  9085. }
  9086. static void ggml_backend_vk_free(ggml_backend_t backend) {
  9087. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9088. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  9089. ggml_vk_cleanup(ctx);
  9090. delete ctx;
  9091. delete backend;
  9092. }
  9093. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  9094. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9095. return &ctx->device->buffer_type;
  9096. }
  9097. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9098. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  9099. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9100. 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");
  9101. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9102. vk_context transfer_ctx;
  9103. if (ctx->transfer_ctx.expired()) {
  9104. // Initialize new transfer context
  9105. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  9106. ctx->transfer_ctx = transfer_ctx;
  9107. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  9108. } else {
  9109. transfer_ctx = ctx->transfer_ctx.lock();
  9110. }
  9111. vk_buffer buf = buf_ctx->dev_buffer;
  9112. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  9113. }
  9114. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9115. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  9116. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9117. 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");
  9118. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9119. vk_context transfer_ctx;
  9120. if (ctx->transfer_ctx.expired()) {
  9121. // Initialize new transfer context
  9122. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  9123. ctx->transfer_ctx = transfer_ctx;
  9124. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  9125. } else {
  9126. transfer_ctx = ctx->transfer_ctx.lock();
  9127. }
  9128. vk_buffer buf = buf_ctx->dev_buffer;
  9129. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  9130. }
  9131. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9132. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  9133. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9134. 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)) {
  9135. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  9136. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  9137. vk_context transfer_ctx;
  9138. if (ctx->transfer_ctx.expired()) {
  9139. // Initialize new transfer context
  9140. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  9141. ctx->transfer_ctx = transfer_ctx;
  9142. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  9143. } else {
  9144. transfer_ctx = ctx->transfer_ctx.lock();
  9145. }
  9146. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  9147. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  9148. 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));
  9149. return true;
  9150. }
  9151. return false;
  9152. }
  9153. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  9154. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  9155. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9156. if(ctx->transfer_ctx.expired()) {
  9157. return;
  9158. }
  9159. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  9160. ggml_vk_ctx_end(transfer_ctx);
  9161. for (auto& cpy : transfer_ctx->in_memcpys) {
  9162. memcpy(cpy.dst, cpy.src, cpy.n);
  9163. }
  9164. ggml_vk_submit(transfer_ctx, ctx->fence);
  9165. ggml_vk_wait_for_fence(ctx);
  9166. for (auto& cpy : transfer_ctx->out_memcpys) {
  9167. memcpy(cpy.dst, cpy.src, cpy.n);
  9168. }
  9169. ctx->transfer_ctx.reset();
  9170. }
  9171. static bool ggml_vk_is_empty(ggml_tensor * node) {
  9172. 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;
  9173. }
  9174. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  9175. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  9176. return false;
  9177. }
  9178. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  9179. // additional constraints specific to this fusion
  9180. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  9181. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  9182. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  9183. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  9184. // rms_norm only supports f32
  9185. if (mul->src[0]->type != GGML_TYPE_F32 ||
  9186. mul->src[1]->type != GGML_TYPE_F32 ||
  9187. mul->type != GGML_TYPE_F32) {
  9188. return false;
  9189. }
  9190. // if rms_norm is the B operand, then we don't handle broadcast
  9191. if (rms_norm == mul->src[1] &&
  9192. !ggml_are_same_shape(mul->src[0], rms_norm)) {
  9193. return false;
  9194. }
  9195. // rms_norm shader assumes contiguous rows
  9196. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  9197. return false;
  9198. }
  9199. }
  9200. return true;
  9201. }
  9202. static uint32_t ggml_vk_fuse_multi_add(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph, int node_idx) {
  9203. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  9204. if (first_node->op != GGML_OP_ADD) {
  9205. return 0;
  9206. }
  9207. if (!ctx->device->multi_add) {
  9208. return 0;
  9209. }
  9210. int32_t num_adds = 1;
  9211. while (node_idx + num_adds < cgraph->n_nodes &&
  9212. cgraph->nodes[node_idx + num_adds]->op == GGML_OP_ADD &&
  9213. num_adds < MAX_FUSED_ADDS) {
  9214. num_adds++;
  9215. }
  9216. // The shader currently requires same shapes (but different strides are allowed),
  9217. // everything f32, and no misalignment
  9218. for (int32_t i = 0; i < num_adds; ++i) {
  9219. const ggml_tensor *next_node = cgraph->nodes[node_idx + i];
  9220. if (!ggml_are_same_shape(first_node, next_node->src[0]) ||
  9221. !ggml_are_same_shape(first_node, next_node->src[1]) ||
  9222. next_node->type != GGML_TYPE_F32 ||
  9223. next_node->src[0]->type != GGML_TYPE_F32 ||
  9224. next_node->src[1]->type != GGML_TYPE_F32 ||
  9225. get_misalign_bytes(ctx, next_node) ||
  9226. get_misalign_bytes(ctx, next_node->src[0]) ||
  9227. get_misalign_bytes(ctx, next_node->src[1])) {
  9228. num_adds = i;
  9229. }
  9230. }
  9231. // Verify we can fuse these
  9232. ggml_op adds[MAX_FUSED_ADDS];
  9233. for (int32_t i = 0; i < num_adds; ++i) {
  9234. adds[i] = GGML_OP_ADD;
  9235. }
  9236. // decrease num_adds if they can't all be fused
  9237. while (num_adds > 1 && !ggml_can_fuse(cgraph, node_idx, adds, num_adds)) {
  9238. num_adds--;
  9239. }
  9240. // a single add is not "fused", so just return zero
  9241. if (num_adds == 1) {
  9242. return 0;
  9243. }
  9244. return num_adds;
  9245. }
  9246. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9247. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  9248. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9249. if (vk_instance.debug_utils_support) {
  9250. vk::DebugUtilsLabelEXT dul = {};
  9251. dul.pLabelName = "ggml_backend_vk_graph_compute";
  9252. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  9253. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  9254. }
  9255. uint64_t total_mat_mul_bytes = 0;
  9256. for (int i = 0; i < cgraph->n_nodes; i++) {
  9257. if (!ctx->device->disable_fusion) {
  9258. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  9259. if (num_adds) {
  9260. ctx->num_additional_fused_ops = num_adds - 1;
  9261. } else if (ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  9262. ctx->num_additional_fused_ops = 1;
  9263. }
  9264. }
  9265. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  9266. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  9267. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  9268. } else if (cgraph->nodes[i]->op == GGML_OP_CONV_2D) {
  9269. // Return CRSxNPQxsizeof(*) to account as many bytes as mul_mat has in im2col->mul_mat mode.
  9270. auto CRS_size =
  9271. cgraph->nodes[i]->src[0]->ne[0] * cgraph->nodes[i]->src[0]->ne[1] * cgraph->nodes[i]->src[0]->ne[2];
  9272. auto NPQ_size = cgraph->nodes[i]->ne[0] * cgraph->nodes[i]->ne[1] * cgraph->nodes[i]->ne[3];
  9273. total_mat_mul_bytes += NPQ_size * CRS_size * ggml_type_size(cgraph->nodes[i]->type);
  9274. }
  9275. i += ctx->num_additional_fused_ops;
  9276. ctx->num_additional_fused_ops = 0;
  9277. }
  9278. if (ctx->device->need_compiles) {
  9279. ggml_vk_load_shaders(ctx->device);
  9280. }
  9281. ggml_vk_preallocate_buffers(ctx);
  9282. ggml_pipeline_allocate_descriptor_sets(ctx);
  9283. int last_node = cgraph->n_nodes - 1;
  9284. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  9285. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  9286. last_node -= 1;
  9287. }
  9288. // Reserve tensor context space for all nodes
  9289. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  9290. bool first_node_in_batch = true; // true if next node will be first node in a batch
  9291. int submit_node_idx = 0; // index to first node in a batch
  9292. vk_context compute_ctx;
  9293. if (vk_perf_logger_enabled) {
  9294. // allocate/resize the query pool
  9295. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  9296. if (ctx->device->query_pool) {
  9297. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  9298. }
  9299. vk::QueryPoolCreateInfo query_create_info;
  9300. query_create_info.queryType = vk::QueryType::eTimestamp;
  9301. query_create_info.queryCount = cgraph->n_nodes + 100;
  9302. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  9303. ctx->device->num_queries = query_create_info.queryCount;
  9304. }
  9305. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  9306. GGML_ASSERT(ctx->compute_ctx.expired());
  9307. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9308. ctx->compute_ctx = compute_ctx;
  9309. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9310. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  9311. }
  9312. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  9313. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  9314. // (and scaled down based on model size, so smaller models submit earlier).
  9315. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  9316. int nodes_per_submit = 100;
  9317. int submitted_nodes = 0;
  9318. int submit_count = 0;
  9319. uint64_t mul_mat_bytes = 0;
  9320. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  9321. for (int i = 0; i < cgraph->n_nodes; i++) {
  9322. if (first_node_in_batch) {
  9323. submit_node_idx = i;
  9324. }
  9325. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  9326. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  9327. }
  9328. if (!ctx->device->disable_fusion) {
  9329. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  9330. if (num_adds) {
  9331. ctx->num_additional_fused_ops = num_adds - 1;
  9332. } else if (ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  9333. ctx->num_additional_fused_ops = 1;
  9334. }
  9335. }
  9336. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  9337. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  9338. bool submit = (submitted_nodes >= nodes_per_submit) ||
  9339. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  9340. (i + ctx->num_additional_fused_ops == last_node) ||
  9341. (almost_ready && !ctx->almost_ready_fence_pending);
  9342. bool enqueued = ggml_vk_build_graph(ctx, cgraph, i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i + ctx->num_additional_fused_ops == last_node, almost_ready, submit);
  9343. if (vk_perf_logger_enabled) {
  9344. if (ctx->compute_ctx.expired()) {
  9345. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9346. ctx->compute_ctx = compute_ctx;
  9347. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9348. } else {
  9349. compute_ctx = ctx->compute_ctx.lock();
  9350. }
  9351. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  9352. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  9353. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  9354. }
  9355. }
  9356. if (enqueued) {
  9357. ++submitted_nodes;
  9358. #ifndef GGML_VULKAN_CHECK_RESULTS
  9359. if (first_node_in_batch) {
  9360. first_node_in_batch = false;
  9361. }
  9362. #endif
  9363. }
  9364. if (submit && enqueued) {
  9365. first_node_in_batch = true;
  9366. submitted_nodes = 0;
  9367. mul_mat_bytes = 0;
  9368. if (submit_count < 3) {
  9369. mul_mat_bytes_per_submit *= 2;
  9370. }
  9371. submit_count++;
  9372. }
  9373. i += ctx->num_additional_fused_ops;
  9374. ctx->num_additional_fused_ops = 0;
  9375. }
  9376. if (vk_perf_logger_enabled) {
  9377. // End the command buffer and submit/wait
  9378. GGML_ASSERT(!ctx->compute_ctx.expired());
  9379. compute_ctx = ctx->compute_ctx.lock();
  9380. ggml_vk_ctx_end(compute_ctx);
  9381. ggml_vk_submit(compute_ctx, ctx->device->fence);
  9382. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  9383. ctx->device->device.resetFences({ ctx->device->fence });
  9384. // Get the results and pass them to the logger
  9385. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  9386. VK_CHECK(ctx->device->device.getQueryPoolResults(ctx->device->query_pool, 0, cgraph->n_nodes + 1, (cgraph->n_nodes + 1)*sizeof(uint64_t), timestamps.data(), sizeof(uint64_t), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait), "get timestamp results");
  9387. for (int i = 0; i < cgraph->n_nodes; i++) {
  9388. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  9389. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  9390. }
  9391. }
  9392. ctx->device->perf_logger->print_timings();
  9393. }
  9394. ggml_vk_graph_cleanup(ctx);
  9395. return GGML_STATUS_SUCCESS;
  9396. UNUSED(backend);
  9397. }
  9398. // TODO: enable async and synchronize
  9399. static ggml_backend_i ggml_backend_vk_interface = {
  9400. /* .get_name = */ ggml_backend_vk_name,
  9401. /* .free = */ ggml_backend_vk_free,
  9402. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  9403. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  9404. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  9405. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  9406. /* .graph_plan_create = */ NULL,
  9407. /* .graph_plan_free = */ NULL,
  9408. /* .graph_plan_update = */ NULL,
  9409. /* .graph_plan_compute = */ NULL,
  9410. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  9411. /* .event_record = */ NULL,
  9412. /* .event_wait = */ NULL,
  9413. };
  9414. static ggml_guid_t ggml_backend_vk_guid() {
  9415. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  9416. return &guid;
  9417. }
  9418. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  9419. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  9420. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  9421. ggml_vk_init(ctx, dev_num);
  9422. ggml_backend_t vk_backend = new ggml_backend {
  9423. /* .guid = */ ggml_backend_vk_guid(),
  9424. /* .iface = */ ggml_backend_vk_interface,
  9425. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  9426. /* .context = */ ctx,
  9427. };
  9428. return vk_backend;
  9429. }
  9430. bool ggml_backend_is_vk(ggml_backend_t backend) {
  9431. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  9432. }
  9433. int ggml_backend_vk_get_device_count() {
  9434. return ggml_vk_get_device_count();
  9435. }
  9436. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  9437. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  9438. int dev_idx = vk_instance.device_indices[device];
  9439. ggml_vk_get_device_description(dev_idx, description, description_size);
  9440. }
  9441. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  9442. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  9443. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  9444. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  9445. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  9446. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  9447. *total = heap.size;
  9448. *free = heap.size;
  9449. break;
  9450. }
  9451. }
  9452. }
  9453. //////////////////////////
  9454. struct ggml_backend_vk_device_context {
  9455. size_t device;
  9456. std::string name;
  9457. std::string description;
  9458. };
  9459. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  9460. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9461. return ctx->name.c_str();
  9462. }
  9463. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  9464. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9465. return ctx->description.c_str();
  9466. }
  9467. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  9468. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  9469. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  9470. }
  9471. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  9472. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9473. return ggml_backend_vk_buffer_type(ctx->device);
  9474. }
  9475. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  9476. UNUSED(dev);
  9477. return ggml_backend_vk_host_buffer_type();
  9478. }
  9479. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  9480. UNUSED(dev);
  9481. return GGML_BACKEND_DEVICE_TYPE_GPU;
  9482. }
  9483. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  9484. props->name = ggml_backend_vk_device_get_name(dev);
  9485. props->description = ggml_backend_vk_device_get_description(dev);
  9486. props->type = ggml_backend_vk_device_get_type(dev);
  9487. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  9488. props->caps = {
  9489. /* .async = */ false,
  9490. /* .host_buffer = */ true,
  9491. /* .buffer_from_host_ptr = */ false,
  9492. /* .events = */ false,
  9493. };
  9494. }
  9495. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  9496. UNUSED(params);
  9497. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9498. return ggml_backend_vk_init(ctx->device);
  9499. }
  9500. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9501. switch (op->op) {
  9502. case GGML_OP_UNARY:
  9503. switch (ggml_get_unary_op(op)) {
  9504. case GGML_UNARY_OP_GELU:
  9505. case GGML_UNARY_OP_GELU_ERF:
  9506. case GGML_UNARY_OP_GELU_QUICK:
  9507. case GGML_UNARY_OP_SILU:
  9508. case GGML_UNARY_OP_RELU:
  9509. case GGML_UNARY_OP_TANH:
  9510. case GGML_UNARY_OP_SIGMOID:
  9511. return ggml_is_contiguous(op->src[0]) &&
  9512. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9513. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  9514. (op->src[0]->type == op->type);
  9515. default:
  9516. return false;
  9517. }
  9518. case GGML_OP_GLU:
  9519. switch (ggml_get_glu_op(op)) {
  9520. case GGML_GLU_OP_GEGLU:
  9521. case GGML_GLU_OP_REGLU:
  9522. case GGML_GLU_OP_SWIGLU:
  9523. case GGML_GLU_OP_SWIGLU_OAI:
  9524. case GGML_GLU_OP_GEGLU_ERF:
  9525. case GGML_GLU_OP_GEGLU_QUICK:
  9526. return ggml_is_contiguous(op->src[0]) &&
  9527. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9528. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  9529. (op->src[0]->type == op->type);
  9530. default:
  9531. return false;
  9532. }
  9533. case GGML_OP_MUL_MAT:
  9534. case GGML_OP_MUL_MAT_ID:
  9535. {
  9536. ggml_type src0_type = op->src[0]->type;
  9537. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9538. const vk_device& device = ggml_vk_get_device(ctx->device);
  9539. if (op->op == GGML_OP_MUL_MAT_ID) {
  9540. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  9541. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  9542. return false;
  9543. }
  9544. }
  9545. switch (src0_type) {
  9546. case GGML_TYPE_F32:
  9547. case GGML_TYPE_F16:
  9548. case GGML_TYPE_BF16:
  9549. case GGML_TYPE_Q4_0:
  9550. case GGML_TYPE_Q4_1:
  9551. case GGML_TYPE_Q5_0:
  9552. case GGML_TYPE_Q5_1:
  9553. case GGML_TYPE_Q8_0:
  9554. case GGML_TYPE_Q2_K:
  9555. case GGML_TYPE_Q3_K:
  9556. case GGML_TYPE_Q4_K:
  9557. case GGML_TYPE_Q5_K:
  9558. case GGML_TYPE_Q6_K:
  9559. case GGML_TYPE_IQ1_S:
  9560. case GGML_TYPE_IQ1_M:
  9561. case GGML_TYPE_IQ2_XXS:
  9562. case GGML_TYPE_IQ2_XS:
  9563. case GGML_TYPE_IQ2_S:
  9564. case GGML_TYPE_IQ3_XXS:
  9565. case GGML_TYPE_IQ3_S:
  9566. case GGML_TYPE_IQ4_XS:
  9567. case GGML_TYPE_IQ4_NL:
  9568. case GGML_TYPE_MXFP4:
  9569. break;
  9570. default:
  9571. return false;
  9572. }
  9573. struct ggml_tensor * a;
  9574. struct ggml_tensor * b;
  9575. if (op->op == GGML_OP_MUL_MAT) {
  9576. a = op->src[0];
  9577. b = op->src[1];
  9578. } else {
  9579. a = op->src[2];
  9580. b = op->src[1];
  9581. }
  9582. if (a->ne[3] != b->ne[3]) {
  9583. return false;
  9584. }
  9585. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16) ||
  9586. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  9587. return false;
  9588. }
  9589. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  9590. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  9591. // So don't support this combination for now.
  9592. return false;
  9593. }
  9594. return true;
  9595. }
  9596. case GGML_OP_FLASH_ATTN_EXT:
  9597. {
  9598. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9599. auto device = ggml_vk_get_device(ctx->device);
  9600. bool coopmat2 = device->coopmat2;
  9601. FaHeadSizes head_sizes = fa_get_head_sizes(op->src[1]->ne[0], op->src[2]->ne[0]);
  9602. if (head_sizes == FA_HEAD_SIZE_UNSUPPORTED) {
  9603. return false;
  9604. }
  9605. if (op->src[4] && op->src[4]->type != GGML_TYPE_F32) {
  9606. return false;
  9607. }
  9608. if (op->src[0]->type != GGML_TYPE_F32) {
  9609. return false;
  9610. }
  9611. if (op->type != GGML_TYPE_F32) {
  9612. return false;
  9613. }
  9614. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  9615. return false;
  9616. }
  9617. // It's straightforward to support different K/V dequant, but would
  9618. // significantly increase the number of pipelines
  9619. if (op->src[1]->type != op->src[2]->type) {
  9620. return false;
  9621. }
  9622. switch (op->src[1]->type) {
  9623. case GGML_TYPE_F16:
  9624. case GGML_TYPE_Q4_0:
  9625. case GGML_TYPE_Q8_0:
  9626. // supported in scalar and coopmat2 paths
  9627. break;
  9628. case GGML_TYPE_Q4_1:
  9629. case GGML_TYPE_Q5_0:
  9630. case GGML_TYPE_Q5_1:
  9631. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  9632. //case GGML_TYPE_Q2_K:
  9633. //case GGML_TYPE_Q3_K:
  9634. //case GGML_TYPE_Q4_K:
  9635. //case GGML_TYPE_Q5_K:
  9636. //case GGML_TYPE_Q6_K:
  9637. //case GGML_TYPE_IQ1_S:
  9638. //case GGML_TYPE_IQ1_M:
  9639. //case GGML_TYPE_IQ2_XXS:
  9640. //case GGML_TYPE_IQ2_XS:
  9641. //case GGML_TYPE_IQ2_S:
  9642. //case GGML_TYPE_IQ3_XXS:
  9643. //case GGML_TYPE_IQ3_S:
  9644. //case GGML_TYPE_IQ4_XS:
  9645. case GGML_TYPE_IQ4_NL:
  9646. // currently supported only in coopmat2 path
  9647. if (!coopmat2) {
  9648. return false;
  9649. }
  9650. break;
  9651. default:
  9652. return false;
  9653. }
  9654. if (!coopmat2 && !device->subgroup_shuffle) {
  9655. // scalar FA uses subgroupShuffle
  9656. return false;
  9657. }
  9658. return true;
  9659. }
  9660. case GGML_OP_GET_ROWS:
  9661. {
  9662. switch (op->src[0]->type) {
  9663. case GGML_TYPE_F32:
  9664. case GGML_TYPE_F16:
  9665. case GGML_TYPE_BF16:
  9666. case GGML_TYPE_Q4_0:
  9667. case GGML_TYPE_Q4_1:
  9668. case GGML_TYPE_Q5_0:
  9669. case GGML_TYPE_Q5_1:
  9670. case GGML_TYPE_Q8_0:
  9671. case GGML_TYPE_IQ1_S:
  9672. case GGML_TYPE_IQ1_M:
  9673. case GGML_TYPE_IQ2_XXS:
  9674. case GGML_TYPE_IQ2_XS:
  9675. case GGML_TYPE_IQ2_S:
  9676. case GGML_TYPE_IQ3_XXS:
  9677. case GGML_TYPE_IQ3_S:
  9678. case GGML_TYPE_IQ4_XS:
  9679. case GGML_TYPE_IQ4_NL:
  9680. case GGML_TYPE_MXFP4:
  9681. return true;
  9682. default:
  9683. return false;
  9684. }
  9685. }
  9686. case GGML_OP_SET_ROWS:
  9687. {
  9688. switch (op->type) {
  9689. case GGML_TYPE_F32:
  9690. case GGML_TYPE_F16:
  9691. case GGML_TYPE_BF16:
  9692. case GGML_TYPE_Q4_0:
  9693. case GGML_TYPE_Q4_1:
  9694. case GGML_TYPE_Q5_0:
  9695. case GGML_TYPE_Q5_1:
  9696. case GGML_TYPE_Q8_0:
  9697. case GGML_TYPE_IQ4_NL:
  9698. return true;
  9699. default:
  9700. return false;
  9701. }
  9702. }
  9703. case GGML_OP_CONT:
  9704. case GGML_OP_CPY:
  9705. case GGML_OP_DUP:
  9706. {
  9707. ggml_type src0_type = op->src[0]->type;
  9708. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  9709. if (src0_type == GGML_TYPE_F32) {
  9710. switch (src1_type) {
  9711. case GGML_TYPE_F32:
  9712. case GGML_TYPE_F16:
  9713. case GGML_TYPE_BF16:
  9714. case GGML_TYPE_Q4_0:
  9715. case GGML_TYPE_Q4_1:
  9716. case GGML_TYPE_Q5_0:
  9717. case GGML_TYPE_Q5_1:
  9718. case GGML_TYPE_Q8_0:
  9719. case GGML_TYPE_IQ4_NL:
  9720. return true;
  9721. default:
  9722. break;
  9723. }
  9724. }
  9725. if (src1_type == GGML_TYPE_F32) {
  9726. switch (src0_type) {
  9727. case GGML_TYPE_F16:
  9728. case GGML_TYPE_Q4_0:
  9729. case GGML_TYPE_Q4_1:
  9730. case GGML_TYPE_Q5_0:
  9731. case GGML_TYPE_Q5_1:
  9732. case GGML_TYPE_Q8_0:
  9733. case GGML_TYPE_IQ4_NL:
  9734. return true;
  9735. default:
  9736. break;
  9737. }
  9738. }
  9739. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9740. return true;
  9741. }
  9742. // We can handle copying from a type to the same type if it's
  9743. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  9744. // so the type/block size must be a multiple of 4.
  9745. if (src0_type == src1_type &&
  9746. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  9747. (ggml_type_size(src0_type) % 2) == 0) {
  9748. return true;
  9749. }
  9750. return false;
  9751. }
  9752. case GGML_OP_REPEAT:
  9753. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  9754. case GGML_OP_REPEAT_BACK:
  9755. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  9756. case GGML_OP_ROPE:
  9757. case GGML_OP_ROPE_BACK:
  9758. case GGML_OP_NONE:
  9759. case GGML_OP_RESHAPE:
  9760. case GGML_OP_VIEW:
  9761. case GGML_OP_PERMUTE:
  9762. case GGML_OP_TRANSPOSE:
  9763. case GGML_OP_RMS_NORM:
  9764. return true;
  9765. case GGML_OP_NORM:
  9766. case GGML_OP_GROUP_NORM:
  9767. case GGML_OP_L2_NORM:
  9768. return ggml_is_contiguous(op->src[0]);
  9769. case GGML_OP_ADD:
  9770. case GGML_OP_SUB:
  9771. case GGML_OP_MUL:
  9772. case GGML_OP_DIV:
  9773. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9774. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  9775. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  9776. case GGML_OP_ADD_ID:
  9777. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32 && op->src[2]->type == GGML_TYPE_I32 &&
  9778. op->type == GGML_TYPE_F32;
  9779. case GGML_OP_SILU_BACK:
  9780. case GGML_OP_RMS_NORM_BACK:
  9781. case GGML_OP_SQR:
  9782. case GGML_OP_SQRT:
  9783. case GGML_OP_SIN:
  9784. case GGML_OP_COS:
  9785. case GGML_OP_CLAMP:
  9786. case GGML_OP_LEAKY_RELU:
  9787. case GGML_OP_OPT_STEP_ADAMW:
  9788. case GGML_OP_OPT_STEP_SGD:
  9789. return op->src[0]->type == GGML_TYPE_F32;
  9790. case GGML_OP_ARGSORT:
  9791. return op->ne[0] <= max_argsort_cols;
  9792. case GGML_OP_UPSCALE:
  9793. case GGML_OP_ACC:
  9794. case GGML_OP_CONCAT:
  9795. case GGML_OP_SCALE:
  9796. case GGML_OP_PAD:
  9797. case GGML_OP_ROLL:
  9798. case GGML_OP_DIAG_MASK_INF:
  9799. case GGML_OP_SOFT_MAX:
  9800. case GGML_OP_SOFT_MAX_BACK:
  9801. case GGML_OP_SUM:
  9802. case GGML_OP_SUM_ROWS:
  9803. case GGML_OP_ARGMAX:
  9804. case GGML_OP_COUNT_EQUAL:
  9805. case GGML_OP_IM2COL:
  9806. case GGML_OP_TIMESTEP_EMBEDDING:
  9807. case GGML_OP_CONV_2D_DW:
  9808. case GGML_OP_POOL_2D:
  9809. case GGML_OP_RWKV_WKV6:
  9810. case GGML_OP_RWKV_WKV7:
  9811. return true;
  9812. case GGML_OP_CONV_TRANSPOSE_1D:
  9813. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  9814. case GGML_OP_CONV_2D:
  9815. {
  9816. // Op is disabled for Apple because it segfaults at pipeline create time on MoltenVK
  9817. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9818. const vk_device& device = ggml_vk_get_device(ctx->device);
  9819. bool is_Apple = ggml_vk_get_device(ctx->device)->vendor_id == VK_VENDOR_ID_APPLE;
  9820. // Channel-contiguous format is not supported yet.
  9821. return ((op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9822. op->src[1]->type == GGML_TYPE_F32 &&
  9823. op->type == GGML_TYPE_F32 &&
  9824. ggml_is_contiguous(op->src[0]) &&
  9825. ggml_is_contiguous(op->src[1]) &&
  9826. ggml_is_contiguous(op)) && !is_Apple;
  9827. }
  9828. default:
  9829. return false;
  9830. }
  9831. UNUSED(dev);
  9832. }
  9833. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  9834. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  9835. return false;
  9836. }
  9837. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9838. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  9839. return buft_ctx->device->idx == ctx->device;
  9840. }
  9841. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9842. const int min_batch_size = 32;
  9843. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  9844. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  9845. UNUSED(dev);
  9846. }
  9847. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  9848. /* .get_name = */ ggml_backend_vk_device_get_name,
  9849. /* .get_description = */ ggml_backend_vk_device_get_description,
  9850. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  9851. /* .get_type = */ ggml_backend_vk_device_get_type,
  9852. /* .get_props = */ ggml_backend_vk_device_get_props,
  9853. /* .init_backend = */ ggml_backend_vk_device_init,
  9854. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  9855. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  9856. /* .buffer_from_host_ptr = */ NULL,
  9857. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  9858. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  9859. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  9860. /* .event_new = */ NULL,
  9861. /* .event_free = */ NULL,
  9862. /* .event_synchronize = */ NULL,
  9863. };
  9864. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  9865. UNUSED(reg);
  9866. return GGML_VK_NAME;
  9867. }
  9868. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  9869. UNUSED(reg);
  9870. return ggml_backend_vk_get_device_count();
  9871. }
  9872. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  9873. static std::vector<ggml_backend_dev_t> devices;
  9874. static bool initialized = false;
  9875. {
  9876. static std::mutex mutex;
  9877. std::lock_guard<std::mutex> lock(mutex);
  9878. if (!initialized) {
  9879. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  9880. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  9881. char desc[256];
  9882. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  9883. ctx->device = i;
  9884. ctx->name = GGML_VK_NAME + std::to_string(i);
  9885. ctx->description = desc;
  9886. devices.push_back(new ggml_backend_device {
  9887. /* .iface = */ ggml_backend_vk_device_i,
  9888. /* .reg = */ reg,
  9889. /* .context = */ ctx,
  9890. });
  9891. }
  9892. initialized = true;
  9893. }
  9894. }
  9895. GGML_ASSERT(device < devices.size());
  9896. return devices[device];
  9897. }
  9898. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  9899. /* .get_name = */ ggml_backend_vk_reg_get_name,
  9900. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  9901. /* .get_device = */ ggml_backend_vk_reg_get_device,
  9902. /* .get_proc_address = */ NULL,
  9903. };
  9904. ggml_backend_reg_t ggml_backend_vk_reg() {
  9905. static ggml_backend_reg reg = {
  9906. /* .api_version = */ GGML_BACKEND_API_VERSION,
  9907. /* .iface = */ ggml_backend_vk_reg_i,
  9908. /* .context = */ nullptr,
  9909. };
  9910. try {
  9911. ggml_vk_instance_init();
  9912. return &reg;
  9913. } catch (const vk::SystemError& e) {
  9914. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  9915. return nullptr;
  9916. }
  9917. }
  9918. // Extension availability
  9919. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9920. #ifdef GGML_VULKAN_VALIDATE
  9921. bool portability_enumeration_ext = false;
  9922. // Check for portability enumeration extension for MoltenVK support
  9923. for (const auto& properties : instance_extensions) {
  9924. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9925. return true;
  9926. }
  9927. }
  9928. if (!portability_enumeration_ext) {
  9929. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9930. }
  9931. #endif
  9932. return false;
  9933. UNUSED(instance_extensions);
  9934. }
  9935. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9936. #ifdef __APPLE__
  9937. bool portability_enumeration_ext = false;
  9938. // Check for portability enumeration extension for MoltenVK support
  9939. for (const auto& properties : instance_extensions) {
  9940. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9941. return true;
  9942. }
  9943. }
  9944. if (!portability_enumeration_ext) {
  9945. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9946. }
  9947. #endif
  9948. return false;
  9949. UNUSED(instance_extensions);
  9950. }
  9951. // Extension availability
  9952. static bool ggml_vk_instance_debug_utils_ext_available(
  9953. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  9954. // Check for portability enumeration extension for MoltenVK support
  9955. for (const auto & properties : instance_extensions) {
  9956. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  9957. return true;
  9958. }
  9959. }
  9960. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  9961. return false;
  9962. UNUSED(instance_extensions);
  9963. }
  9964. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  9965. switch (props.vendorID) {
  9966. case VK_VENDOR_ID_INTEL:
  9967. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  9968. // while some older hardware (ex. Arc A770) has performance regressions
  9969. return arch == vk_device_architecture::INTEL_XE2;
  9970. case VK_VENDOR_ID_AMD:
  9971. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  9972. // Workaround for AMD proprietary driver reporting support on all GPUs
  9973. return arch == vk_device_architecture::AMD_RDNA3;
  9974. }
  9975. return true;
  9976. default:
  9977. return true;
  9978. }
  9979. }
  9980. // checks
  9981. #ifdef GGML_VULKAN_CHECK_RESULTS
  9982. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  9983. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  9984. return;
  9985. }
  9986. for (int j = 0; j < level; j++) {
  9987. std::cerr << " ";
  9988. }
  9989. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  9990. done.push_back(tensor);
  9991. for (int i = 0; i < GGML_MAX_SRC; i++) {
  9992. if (tensor->src[i] != nullptr) {
  9993. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  9994. }
  9995. }
  9996. }
  9997. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  9998. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  9999. return;
  10000. }
  10001. i0 = std::max(i0, 5);
  10002. i1 = std::max(i1, 5);
  10003. i2 = std::max(i2, 0);
  10004. i3 = std::max(i3, 0);
  10005. fprintf(stderr, " ");
  10006. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  10007. fprintf(stderr, "%7d ", idx1);
  10008. }
  10009. fprintf(stderr, "\n");
  10010. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  10011. fprintf(stderr, "%7d: ", idx0);
  10012. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  10013. 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]) {
  10014. float val;
  10015. if (tensor->type == GGML_TYPE_F32) {
  10016. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  10017. } else if (tensor->type == GGML_TYPE_F16) {
  10018. 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]));
  10019. } else if (tensor->type == GGML_TYPE_I32) {
  10020. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  10021. } else {
  10022. GGML_ABORT("fatal error");
  10023. }
  10024. fprintf(stderr, "% 7.2f ", val);
  10025. } else {
  10026. fprintf(stderr, " ");
  10027. }
  10028. }
  10029. fprintf(stderr, "\n");
  10030. }
  10031. }
  10032. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  10033. void * tensor_data = tensor->data;
  10034. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  10035. if (is_gpu) {
  10036. const size_t tensor_size = ggml_nbytes(tensor);
  10037. tensor_data = malloc(tensor_size);
  10038. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10039. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  10040. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  10041. }
  10042. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  10043. 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;
  10044. if (tensor->src[0] != nullptr) {
  10045. 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;
  10046. }
  10047. if (tensor->src[1] != nullptr) {
  10048. 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;
  10049. }
  10050. std::cerr << std::endl << "Result:" << std::endl;
  10051. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  10052. std::cerr << std::endl;
  10053. std::vector<const ggml_tensor *> done;
  10054. ggml_vk_print_graph_origin(tensor, done);
  10055. if (is_gpu) {
  10056. free(tensor_data);
  10057. }
  10058. }
  10059. void * comp_result;
  10060. size_t comp_size;
  10061. size_t comp_nb[GGML_MAX_DIMS];
  10062. size_t check_counter = 0;
  10063. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  10064. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  10065. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  10066. return;
  10067. }
  10068. bool fused_rms_norm_mul = false;
  10069. int rms_norm_idx = -1;
  10070. if (ctx->num_additional_fused_ops == 1 &&
  10071. tensor->op == GGML_OP_RMS_NORM &&
  10072. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  10073. fused_rms_norm_mul = true;
  10074. tensor = cgraph->nodes[tensor_idx + 1];
  10075. }
  10076. check_counter++;
  10077. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  10078. return;
  10079. }
  10080. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  10081. ggml_tensor * src0 = tensor->src[0];
  10082. ggml_tensor * src1 = tensor->src[1];
  10083. struct ggml_init_params iparams = {
  10084. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  10085. /*.mem_buffer =*/ NULL,
  10086. /*.no_alloc =*/ false,
  10087. };
  10088. struct ggml_context * ggml_ctx = ggml_init(iparams);
  10089. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  10090. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  10091. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  10092. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  10093. struct ggml_tensor * tensor_clone = nullptr;
  10094. for (int i = 0; i < 6; i++) {
  10095. ggml_tensor * srci = tensor->src[i];
  10096. if (fused_rms_norm_mul) {
  10097. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  10098. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  10099. switch (i) {
  10100. case 0: srci = rms_norm->src[0]; break;
  10101. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  10102. default: continue;
  10103. }
  10104. }
  10105. if (srci == nullptr) {
  10106. continue;
  10107. }
  10108. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  10109. size_t srci_size = ggml_nbytes(srci);
  10110. src_clone[i] = srci_clone;
  10111. src_size[i] = ggml_nbytes(srci);
  10112. src_buffer[i] = malloc(srci_size);
  10113. srci_clone->data = src_buffer[i];
  10114. if (ggml_backend_buffer_is_host(srci->buffer)) {
  10115. memcpy(srci_clone->data, srci->data, srci_size);
  10116. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10117. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  10118. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  10119. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  10120. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  10121. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  10122. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  10123. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  10124. const int idx = i3*srci->ne[2] + i2;
  10125. 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]);
  10126. }
  10127. }
  10128. srci_clone->nb[0] = srci->nb[0];
  10129. srci_clone->nb[1] = srci->nb[1];
  10130. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  10131. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  10132. }
  10133. } else {
  10134. if (offset + srci_size >= buffer_gpu->size) {
  10135. srci_size = buffer_gpu->size - offset;
  10136. }
  10137. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  10138. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10139. }
  10140. } else {
  10141. GGML_ABORT("fatal error");
  10142. }
  10143. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10144. ggml_vk_print_tensor(srci, srci_name[i]);
  10145. }
  10146. }
  10147. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  10148. const float * params = (const float *)tensor->op_params;
  10149. 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]);
  10150. if (src_clone[4]) {
  10151. ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]);
  10152. }
  10153. } else if (tensor->op == GGML_OP_MUL_MAT) {
  10154. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  10155. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  10156. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  10157. } else if (tensor->op == GGML_OP_SUB) {
  10158. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  10159. } else if (tensor->op == GGML_OP_MUL) {
  10160. if (fused_rms_norm_mul) {
  10161. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  10162. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  10163. } else {
  10164. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  10165. }
  10166. } else if (tensor->op == GGML_OP_DIV) {
  10167. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  10168. } else if (tensor->op == GGML_OP_CONCAT) {
  10169. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  10170. } else if (tensor->op == GGML_OP_UPSCALE) {
  10171. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  10172. } else if (tensor->op == GGML_OP_SCALE) {
  10173. const float * params = (const float *)tensor->op_params;
  10174. tensor_clone = ggml_scale_bias(ggml_ctx, src_clone[0], params[0], params[1]);
  10175. } else if (tensor->op == GGML_OP_SQR) {
  10176. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  10177. } else if (tensor->op == GGML_OP_SQRT) {
  10178. tensor_clone = ggml_sqrt(ggml_ctx, src_clone[0]);
  10179. } else if (tensor->op == GGML_OP_SIN) {
  10180. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  10181. } else if (tensor->op == GGML_OP_COS) {
  10182. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  10183. } else if (tensor->op == GGML_OP_CLAMP) {
  10184. const float * params = (const float *)tensor->op_params;
  10185. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  10186. } else if (tensor->op == GGML_OP_PAD) {
  10187. 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]);
  10188. } else if (tensor->op == GGML_OP_REPEAT) {
  10189. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  10190. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  10191. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  10192. } else if (tensor->op == GGML_OP_ADD) {
  10193. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  10194. } else if (tensor->op == GGML_OP_ACC) {
  10195. 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]);
  10196. } else if (tensor->op == GGML_OP_NORM) {
  10197. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  10198. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  10199. const float * float_params = (const float *)tensor->op_params;
  10200. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  10201. } else if (tensor->op == GGML_OP_RMS_NORM) {
  10202. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  10203. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  10204. const float eps = ((float *) tensor->op_params)[0];
  10205. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  10206. } else if (tensor->op == GGML_OP_SILU_BACK) {
  10207. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  10208. } else if (tensor->op == GGML_OP_L2_NORM) {
  10209. const float eps = ((float *) tensor->op_params)[0];
  10210. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  10211. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  10212. if (src1 != nullptr) {
  10213. const float * params = (const float *)tensor->op_params;
  10214. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  10215. } else {
  10216. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  10217. }
  10218. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  10219. 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]);
  10220. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  10221. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  10222. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  10223. const int n_dims = ((int32_t *) tensor->op_params)[1];
  10224. const int mode = ((int32_t *) tensor->op_params)[2];
  10225. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  10226. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  10227. const float freq_base = ((float *) tensor->op_params)[5];
  10228. const float freq_scale = ((float *) tensor->op_params)[6];
  10229. const float ext_factor = ((float *) tensor->op_params)[7];
  10230. const float attn_factor = ((float *) tensor->op_params)[8];
  10231. const float beta_fast = ((float *) tensor->op_params)[9];
  10232. const float beta_slow = ((float *) tensor->op_params)[10];
  10233. if (mode & GGML_ROPE_TYPE_MROPE) {
  10234. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  10235. if (tensor->op == GGML_OP_ROPE) {
  10236. 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);
  10237. } else {
  10238. 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);
  10239. }
  10240. } else {
  10241. if (tensor->op == GGML_OP_ROPE) {
  10242. 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);
  10243. } else {
  10244. 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);
  10245. }
  10246. }
  10247. } else if (tensor->op == GGML_OP_UNARY) {
  10248. switch (ggml_get_unary_op(tensor)) {
  10249. case GGML_UNARY_OP_SILU:
  10250. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  10251. break;
  10252. case GGML_UNARY_OP_GELU:
  10253. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  10254. break;
  10255. case GGML_UNARY_OP_GELU_ERF:
  10256. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  10257. break;
  10258. case GGML_UNARY_OP_GELU_QUICK:
  10259. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  10260. break;
  10261. case GGML_UNARY_OP_RELU:
  10262. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  10263. break;
  10264. case GGML_UNARY_OP_TANH:
  10265. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  10266. break;
  10267. case GGML_UNARY_OP_SIGMOID:
  10268. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  10269. break;
  10270. default:
  10271. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  10272. GGML_ABORT("fatal error");
  10273. }
  10274. } else if (tensor->op == GGML_OP_GLU) {
  10275. if (src_clone[1] == nullptr) {
  10276. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  10277. } else {
  10278. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  10279. }
  10280. ggml_set_op_params_i32(tensor_clone, 2, ggml_get_op_params_i32(tensor, 2));
  10281. ggml_set_op_params_i32(tensor_clone, 3, ggml_get_op_params_i32(tensor, 3));
  10282. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  10283. if (src1 == nullptr) {
  10284. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  10285. tensor_clone->type = tensor->type;
  10286. } else {
  10287. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  10288. }
  10289. } else if (tensor->op == GGML_OP_CONT) {
  10290. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  10291. } else if (tensor->op == GGML_OP_RESHAPE) {
  10292. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  10293. } else if (tensor->op == GGML_OP_VIEW) {
  10294. 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]);
  10295. } else if (tensor->op == GGML_OP_PERMUTE) {
  10296. int32_t * params = (int32_t *)tensor->op_params;
  10297. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  10298. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  10299. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  10300. } else if (tensor->op == GGML_OP_GET_ROWS) {
  10301. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  10302. } else if (tensor->op == GGML_OP_ARGSORT) {
  10303. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  10304. } else if (tensor->op == GGML_OP_SUM) {
  10305. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  10306. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  10307. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  10308. } else if (tensor->op == GGML_OP_ARGMAX) {
  10309. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  10310. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  10311. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  10312. } else if (tensor->op == GGML_OP_IM2COL) {
  10313. const int32_t s0 = tensor->op_params[0];
  10314. const int32_t s1 = tensor->op_params[1];
  10315. const int32_t p0 = tensor->op_params[2];
  10316. const int32_t p1 = tensor->op_params[3];
  10317. const int32_t d0 = tensor->op_params[4];
  10318. const int32_t d1 = tensor->op_params[5];
  10319. const bool is_2D = tensor->op_params[6] == 1;
  10320. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  10321. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  10322. const int32_t dim = tensor->op_params[0];
  10323. const int32_t max_period = tensor->op_params[1];
  10324. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  10325. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  10326. const int32_t s0 = tensor->op_params[0];
  10327. const int32_t p0 = tensor->op_params[1];
  10328. const int32_t d0 = tensor->op_params[2];
  10329. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  10330. } else if (tensor->op == GGML_OP_POOL_2D) {
  10331. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  10332. const int32_t k0 = tensor->op_params[1];
  10333. const int32_t k1 = tensor->op_params[2];
  10334. const int32_t s0 = tensor->op_params[3];
  10335. const int32_t s1 = tensor->op_params[4];
  10336. const int32_t p0 = tensor->op_params[5];
  10337. const int32_t p1 = tensor->op_params[6];
  10338. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  10339. } else if (tensor->op == GGML_OP_CONV_2D) {
  10340. const int32_t s0 = tensor->op_params[0];
  10341. const int32_t s1 = tensor->op_params[1];
  10342. const int32_t p0 = tensor->op_params[2];
  10343. const int32_t p1 = tensor->op_params[3];
  10344. const int32_t d0 = tensor->op_params[4];
  10345. const int32_t d1 = tensor->op_params[5];
  10346. tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1);
  10347. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  10348. const float * op_params = (const float *)tensor->op_params;
  10349. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  10350. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  10351. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  10352. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  10353. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  10354. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  10355. src_clone[4], src_clone[5], src_clone[6]);
  10356. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  10357. src_clone[0]->flags = src0->flags;
  10358. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  10359. src_clone[2], src_clone[3], src_clone[4]);
  10360. } else if (tensor->op == GGML_OP_OPT_STEP_SGD) {
  10361. src_clone[0]->flags = src0->flags;
  10362. tensor_clone = ggml_opt_step_sgd(ggml_ctx, src_clone[0], src_clone[1],
  10363. src_clone[2]);
  10364. } else if (tensor->op == GGML_OP_ADD_ID) {
  10365. tensor_clone = ggml_add_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  10366. }
  10367. else {
  10368. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  10369. GGML_ABORT("fatal error");
  10370. }
  10371. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  10372. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  10373. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  10374. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10375. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  10376. }
  10377. comp_size = ggml_nbytes(tensor_clone);
  10378. comp_result = malloc(comp_size);
  10379. memcpy(comp_result, tensor_clone->data, comp_size);
  10380. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10381. for (int i = 0; i < 6; i++) {
  10382. if (src_buffer[i] != nullptr) {
  10383. free(src_buffer[i]);
  10384. }
  10385. }
  10386. ggml_free(ggml_ctx);
  10387. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  10388. }
  10389. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  10390. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  10391. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  10392. return;
  10393. }
  10394. bool fused_rms_norm_mul = false;
  10395. if (ctx->num_additional_fused_ops == 1 &&
  10396. tensor->op == GGML_OP_RMS_NORM &&
  10397. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  10398. fused_rms_norm_mul = true;
  10399. tensor = cgraph->nodes[tensor_idx + 1];
  10400. }
  10401. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  10402. return;
  10403. }
  10404. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  10405. ggml_tensor * src0 = tensor->src[0];
  10406. ggml_tensor * src1 = tensor->src[1];
  10407. ggml_tensor * src2 = tensor->src[2];
  10408. ggml_tensor * src3 = tensor->src[3];
  10409. void * tensor_data = tensor->data;
  10410. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  10411. size_t tensor_size = ggml_nbytes(tensor);
  10412. tensor_data = malloc(tensor_size);
  10413. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10414. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  10415. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  10416. if (offset + tensor_size >= buffer_gpu->size) {
  10417. tensor_size = buffer_gpu->size - offset;
  10418. }
  10419. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  10420. }
  10421. float first_error_result = -1.0f;
  10422. float first_error_correct = -1.0f;
  10423. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  10424. double avg_err = 0.0;
  10425. size_t counter = 0;
  10426. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  10427. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  10428. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  10429. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  10430. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  10431. float correct = 0.0f;
  10432. float result = 0.0f;
  10433. if (buffer_size_fit) {
  10434. if (tensor->type == GGML_TYPE_F32) {
  10435. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  10436. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  10437. } else if (tensor->type == GGML_TYPE_F16) {
  10438. 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]));
  10439. 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]));
  10440. } else if (tensor->type == GGML_TYPE_BF16) {
  10441. correct = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]));
  10442. result = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]));
  10443. } else if (tensor->type == GGML_TYPE_I32) {
  10444. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  10445. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  10446. } else if (tensor->type == GGML_TYPE_I64) {
  10447. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  10448. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  10449. } else {
  10450. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  10451. }
  10452. } else {
  10453. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  10454. GGML_ABORT("fatal error");
  10455. }
  10456. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  10457. 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;
  10458. 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;
  10459. if (src0 != nullptr) {
  10460. 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;
  10461. }
  10462. if (src1 != nullptr) {
  10463. 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;
  10464. }
  10465. if (src2 != nullptr) {
  10466. 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;
  10467. }
  10468. if (src3 != nullptr) {
  10469. 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;
  10470. }
  10471. 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;
  10472. std::cerr << std::endl << "Result:" << std::endl;
  10473. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  10474. std::cerr << std::endl << "Correct:" << std::endl;
  10475. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  10476. std::cerr << std::endl;
  10477. std::vector<const ggml_tensor *> done;
  10478. ggml_vk_print_graph_origin(tensor, done);
  10479. GGML_ABORT("fatal error");
  10480. }
  10481. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  10482. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  10483. first_error[0] = i0;
  10484. first_error[1] = i1;
  10485. first_error[2] = i2;
  10486. first_error[3] = i3;
  10487. first_error_result = result;
  10488. first_error_correct = correct;
  10489. }
  10490. // Special case, value is infinite, avoid NaN result in avg_err
  10491. // NaN also appears in results, if both are nan error is 0
  10492. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  10493. avg_err += std::fabs(correct - result) / denom;
  10494. }
  10495. counter++;
  10496. }
  10497. }
  10498. }
  10499. }
  10500. avg_err /= counter;
  10501. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10502. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  10503. 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;
  10504. if (src0 != nullptr) {
  10505. 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;
  10506. }
  10507. if (src1 != nullptr) {
  10508. 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;
  10509. }
  10510. if (src2 != nullptr) {
  10511. 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;
  10512. }
  10513. if (src3 != nullptr) {
  10514. 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;
  10515. }
  10516. 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;
  10517. std::cerr << std::endl << "Result:" << std::endl;
  10518. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  10519. std::cerr << std::endl << "Correct:" << std::endl;
  10520. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  10521. std::cerr << std::endl;
  10522. std::vector<const ggml_tensor *> done;
  10523. ggml_vk_print_graph_origin(tensor, done);
  10524. }
  10525. if (avg_err > 0.5 || std::isnan(avg_err)) {
  10526. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  10527. 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;
  10528. if (src0 != nullptr) {
  10529. 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;
  10530. }
  10531. if (src1 != nullptr) {
  10532. 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;
  10533. }
  10534. if (src2 != nullptr) {
  10535. 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;
  10536. }
  10537. if (src3 != nullptr) {
  10538. 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;
  10539. }
  10540. 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;
  10541. std::cerr << std::endl << "Result:" << std::endl;
  10542. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  10543. std::cerr << std::endl << "Correct:" << std::endl;
  10544. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  10545. std::cerr << std::endl;
  10546. std::vector<const ggml_tensor *> done;
  10547. ggml_vk_print_graph_origin(tensor, done);
  10548. GGML_ABORT("fatal error");
  10549. } else {
  10550. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  10551. }
  10552. free(comp_result);
  10553. comp_result = nullptr;
  10554. comp_size = 0;
  10555. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  10556. free(tensor_data);
  10557. }
  10558. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  10559. }
  10560. #endif
  10561. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)