ggml-vulkan.cpp 517 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_PERF) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #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. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  48. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  49. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  50. #define VK_VENDOR_ID_AMD 0x1002
  51. #define VK_VENDOR_ID_APPLE 0x106b
  52. #define VK_VENDOR_ID_INTEL 0x8086
  53. #define VK_VENDOR_ID_NVIDIA 0x10de
  54. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 32
  55. #define GGML_VK_MAX_NODES 8192
  56. #define MAX_VK_BUFFERS 256
  57. #define VK_CHECK(err, msg) \
  58. do { \
  59. vk::Result err_ = (err); \
  60. if (err_ != vk::Result::eSuccess) { \
  61. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  62. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  63. exit(1); \
  64. } \
  65. } while (0)
  66. #ifdef GGML_VULKAN_DEBUG
  67. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  68. #else
  69. #define VK_LOG_DEBUG(msg) ((void) 0)
  70. #endif // GGML_VULKAN_DEBUG
  71. struct ggml_backend_vk_context;
  72. struct vk_queue {
  73. uint32_t queue_family_index;
  74. vk::Queue queue;
  75. vk::CommandPool pool;
  76. uint32_t cmd_buffer_idx;
  77. std::vector<vk::CommandBuffer> cmd_buffers;
  78. vk::PipelineStageFlags stage_flags;
  79. bool transfer_only;
  80. };
  81. struct vk_pipeline_struct {
  82. std::string name;
  83. vk::ShaderModule shader_module;
  84. vk::DescriptorSetLayout dsl;
  85. std::vector<vk::DescriptorPool> descriptor_pools;
  86. std::vector<vk::DescriptorSet> descriptor_sets;
  87. uint32_t descriptor_set_idx;
  88. vk::PipelineLayout layout;
  89. vk::Pipeline pipeline;
  90. uint32_t push_constant_size;
  91. uint32_t parameter_count;
  92. std::array<uint32_t, 3> wg_denoms;
  93. uint32_t align;
  94. // set to true to request the pipeline is compiled after the dryrun
  95. bool needed {};
  96. // set to true when the shader has been compiled
  97. bool compiled {};
  98. };
  99. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  100. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  101. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  102. struct vk_matmul_pipeline_struct {
  103. vk_pipeline l, m, s;
  104. vk_pipeline a_l, a_m, a_s;
  105. };
  106. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  107. struct vk_matmul_pipeline2 {
  108. vk_matmul_pipeline2() {
  109. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  110. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  111. }
  112. vk_matmul_pipeline f32acc;
  113. vk_matmul_pipeline f16acc;
  114. };
  115. struct vk_device_struct;
  116. typedef std::shared_ptr<vk_device_struct> vk_device;
  117. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  118. struct vk_buffer_struct;
  119. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  120. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  121. struct ggml_backend_vk_buffer_type_context {
  122. std::string name;
  123. vk_device device;
  124. };
  125. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  126. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  127. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  128. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  129. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  130. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  131. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  132. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  133. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  134. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  135. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  136. /* .is_host = */ NULL,
  137. };
  138. #ifdef GGML_VULKAN_MEMORY_DEBUG
  139. class vk_memory_logger;
  140. #endif
  141. #ifdef GGML_VULKAN_PERF
  142. class vk_perf_logger;
  143. #endif
  144. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  145. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  146. static constexpr uint32_t p021_max_gqa_ratio = 8;
  147. enum vk_device_architecture {
  148. OTHER,
  149. AMD_GCN,
  150. AMD_RDNA1,
  151. AMD_RDNA2,
  152. AMD_RDNA3,
  153. };
  154. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  155. vk::PhysicalDeviceProperties props = device.getProperties();
  156. if (props.vendorID == VK_VENDOR_ID_AMD) {
  157. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  158. bool amd_shader_core_properties = false;
  159. bool integer_dot_product = false;
  160. bool subgroup_size_control = false;
  161. for (const auto& properties : ext_props) {
  162. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  163. amd_shader_core_properties = true;
  164. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  165. integer_dot_product = true;
  166. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  167. subgroup_size_control = true;
  168. }
  169. }
  170. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  171. return vk_device_architecture::OTHER;
  172. }
  173. vk::PhysicalDeviceProperties2 props2;
  174. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  175. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  176. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  177. props2.pNext = &shader_core_props_amd;
  178. shader_core_props_amd.pNext = &integer_dot_props;
  179. integer_dot_props.pNext = &subgroup_size_control_props;
  180. device.getProperties2(&props2);
  181. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  182. return vk_device_architecture::AMD_GCN;
  183. }
  184. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  185. // RDNA
  186. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  187. return vk_device_architecture::AMD_RDNA1;
  188. }
  189. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  190. return vk_device_architecture::AMD_RDNA3;
  191. }
  192. return vk_device_architecture::AMD_RDNA2;
  193. }
  194. }
  195. return vk_device_architecture::OTHER;
  196. }
  197. struct vk_device_struct {
  198. std::mutex mutex;
  199. vk::PhysicalDevice physical_device;
  200. vk::PhysicalDeviceProperties properties;
  201. std::string name;
  202. uint64_t max_memory_allocation_size;
  203. uint64_t suballocation_block_size;
  204. bool fp16;
  205. bool pipeline_robustness;
  206. vk::Device device;
  207. uint32_t vendor_id;
  208. vk::DriverId driver_id;
  209. vk_device_architecture architecture;
  210. vk_queue compute_queue;
  211. vk_queue transfer_queue;
  212. bool single_queue;
  213. uint32_t subgroup_size;
  214. uint32_t shader_core_count;
  215. bool uma;
  216. bool prefer_host_memory;
  217. bool float_controls_rte_fp16;
  218. bool subgroup_add;
  219. bool integer_dot_product;
  220. bool subgroup_size_control;
  221. uint32_t subgroup_min_size;
  222. uint32_t subgroup_max_size;
  223. bool subgroup_require_full_support;
  224. bool coopmat_support;
  225. bool coopmat_acc_f32_support;
  226. bool coopmat_acc_f16_support;
  227. uint32_t coopmat_m;
  228. uint32_t coopmat_n;
  229. uint32_t coopmat_k;
  230. bool coopmat_int_support;
  231. uint32_t coopmat_int_m;
  232. uint32_t coopmat_int_n;
  233. uint32_t coopmat_int_k;
  234. bool coopmat2;
  235. size_t idx;
  236. bool mul_mat_l[GGML_TYPE_COUNT];
  237. bool mul_mat_m[GGML_TYPE_COUNT];
  238. bool mul_mat_s[GGML_TYPE_COUNT];
  239. bool mul_mat_id_l[GGML_TYPE_COUNT];
  240. bool mul_mat_id_m[GGML_TYPE_COUNT];
  241. bool mul_mat_id_s[GGML_TYPE_COUNT];
  242. // set to true to indicate that some shaders need to be compiled after the dryrun
  243. bool need_compiles {};
  244. vk_matmul_pipeline pipeline_matmul_f32 {};
  245. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  246. vk_matmul_pipeline2 pipeline_matmul_f16;
  247. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  248. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  249. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  250. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  251. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  252. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  253. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  254. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  255. vk_pipeline pipeline_matmul_split_k_reduce;
  256. vk_pipeline pipeline_quantize_q8_1;
  257. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  258. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  259. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  260. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  261. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  262. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  263. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  264. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  265. vk_pipeline pipeline_acc_f32;
  266. vk_pipeline pipeline_add_f32, pipeline_add_f32_norepeat;
  267. vk_pipeline pipeline_add_f16_f32_f16, pipeline_add_f16_f32_f16_norepeat;
  268. vk_pipeline pipeline_sub_f32, pipeline_sub_f32_norepeat;
  269. vk_pipeline pipeline_mul_f32, pipeline_mul_f32_norepeat;
  270. vk_pipeline pipeline_div_f32, pipeline_div_f32_norepeat;
  271. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  272. vk_pipeline pipeline_upscale_f32;
  273. vk_pipeline pipeline_scale_f32;
  274. vk_pipeline pipeline_sqr_f32;
  275. vk_pipeline pipeline_sin_f32;
  276. vk_pipeline pipeline_cos_f32;
  277. vk_pipeline pipeline_clamp_f32;
  278. vk_pipeline pipeline_pad_f32;
  279. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  280. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16;
  281. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16;
  282. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  283. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  284. vk_pipeline pipeline_norm_f32;
  285. vk_pipeline pipeline_group_norm_f32;
  286. vk_pipeline pipeline_rms_norm_f32;
  287. vk_pipeline pipeline_rms_norm_back_f32;
  288. vk_pipeline pipeline_l2_norm_f32;
  289. vk_pipeline pipeline_gelu_f32;
  290. vk_pipeline pipeline_gelu_quick_f32;
  291. vk_pipeline pipeline_silu_f32;
  292. vk_pipeline pipeline_silu_back_f32;
  293. vk_pipeline pipeline_relu_f32;
  294. vk_pipeline pipeline_leaky_relu_f32;
  295. vk_pipeline pipeline_tanh_f32;
  296. vk_pipeline pipeline_sigmoid_f32;
  297. vk_pipeline pipeline_diag_mask_inf_f32;
  298. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  299. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  300. vk_pipeline pipeline_soft_max_back_f32;
  301. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  302. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  303. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  304. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  305. vk_pipeline pipeline_argsort_f32;
  306. vk_pipeline pipeline_sum_rows_f32;
  307. vk_pipeline pipeline_argmax_f32;
  308. vk_pipeline pipeline_count_equal_i32;
  309. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  310. vk_pipeline pipeline_timestep_embedding_f32;
  311. vk_pipeline pipeline_pool2d_f32;
  312. vk_pipeline pipeline_rwkv_wkv6_f32;
  313. vk_pipeline pipeline_rwkv_wkv7_f32;
  314. vk_pipeline pipeline_opt_step_adamw_f32;
  315. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  316. vk_pipeline pipeline_flash_attn_f32_f16_D64[GGML_TYPE_COUNT][2][2][2];
  317. vk_pipeline pipeline_flash_attn_f32_f16_D80[GGML_TYPE_COUNT][2][2][2];
  318. vk_pipeline pipeline_flash_attn_f32_f16_D96[GGML_TYPE_COUNT][2][2][2];
  319. vk_pipeline pipeline_flash_attn_f32_f16_D112[GGML_TYPE_COUNT][2][2][2];
  320. vk_pipeline pipeline_flash_attn_f32_f16_D128[GGML_TYPE_COUNT][2][2][2];
  321. vk_pipeline pipeline_flash_attn_f32_f16_D256[GGML_TYPE_COUNT][2][2][2];
  322. vk_pipeline pipeline_flash_attn_split_k_reduce;
  323. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  324. std::unordered_map<std::string, uint64_t> pipeline_descriptor_set_requirements;
  325. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  326. vk::Fence fence;
  327. vk_buffer sync_staging;
  328. ggml_backend_buffer_type buffer_type;
  329. #ifdef GGML_VULKAN_MEMORY_DEBUG
  330. std::unique_ptr<vk_memory_logger> memory_logger;
  331. #endif
  332. #ifdef GGML_VULKAN_PERF
  333. std::unique_ptr<vk_perf_logger> perf_logger;
  334. #endif
  335. ~vk_device_struct() {
  336. VK_LOG_DEBUG("destroy device " << name);
  337. device.destroyFence(fence);
  338. ggml_vk_destroy_buffer(sync_staging);
  339. device.destroyCommandPool(compute_queue.pool);
  340. if (!single_queue) {
  341. device.destroyCommandPool(transfer_queue.pool);
  342. }
  343. for (auto& pipeline : pipelines) {
  344. if (pipeline.second.expired()) {
  345. continue;
  346. }
  347. vk_pipeline pl = pipeline.second.lock();
  348. ggml_vk_destroy_pipeline(device, pl);
  349. }
  350. pipelines.clear();
  351. device.destroy();
  352. }
  353. };
  354. struct vk_buffer_struct {
  355. vk::Buffer buffer = VK_NULL_HANDLE;
  356. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  357. vk::MemoryPropertyFlags memory_property_flags;
  358. void * ptr;
  359. size_t size = 0;
  360. vk_device device;
  361. ~vk_buffer_struct() {
  362. if (size == 0) {
  363. return;
  364. }
  365. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  366. device->device.freeMemory(device_memory);
  367. device->device.destroyBuffer(buffer);
  368. }
  369. };
  370. struct vk_subbuffer {
  371. vk_buffer buffer;
  372. uint64_t offset;
  373. uint64_t size;
  374. operator vk::DescriptorBufferInfo() const {
  375. return { buffer->buffer, offset, size };
  376. }
  377. };
  378. struct vk_semaphore {
  379. vk::Semaphore s;
  380. uint64_t value;
  381. };
  382. struct vk_submission {
  383. vk::CommandBuffer buffer;
  384. std::vector<vk_semaphore> wait_semaphores;
  385. std::vector<vk_semaphore> signal_semaphores;
  386. };
  387. typedef std::vector<vk_submission> vk_sequence;
  388. struct vk_mat_mat_push_constants {
  389. uint32_t M; uint32_t N; uint32_t K;
  390. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  391. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  392. uint32_t k_split;
  393. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  394. uint32_t padded_N;
  395. };
  396. struct vk_mat_vec_push_constants {
  397. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  398. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  399. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  400. };
  401. struct vk_mat_mat_id_push_constants {
  402. uint32_t M; uint32_t N; uint32_t K;
  403. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  404. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  405. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  406. uint32_t padded_N;
  407. };
  408. struct vk_mat_vec_id_push_constants {
  409. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  410. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  411. uint32_t nei0; uint32_t ne11;
  412. };
  413. struct vk_flash_attn_push_constants {
  414. uint32_t N;
  415. uint32_t KV;
  416. uint32_t ne1;
  417. uint32_t ne2;
  418. uint32_t ne3;
  419. uint32_t neq2;
  420. uint32_t neq3;
  421. uint32_t nek2;
  422. uint32_t nek3;
  423. uint32_t nev2;
  424. uint32_t nev3;
  425. uint32_t nem1;
  426. uint32_t nb01;
  427. uint32_t nb02;
  428. uint32_t nb03;
  429. uint32_t nb11;
  430. uint32_t nb12;
  431. uint32_t nb13;
  432. uint32_t nb21;
  433. uint32_t nb22;
  434. uint32_t nb23;
  435. uint32_t nb31;
  436. float scale;
  437. float max_bias;
  438. float logit_softcap;
  439. uint32_t mask;
  440. uint32_t n_head_log2;
  441. float m0;
  442. float m1;
  443. uint32_t gqa_ratio;
  444. uint32_t split_kv;
  445. uint32_t k_num;
  446. };
  447. struct vk_op_push_constants {
  448. uint32_t KX;
  449. uint32_t KY;
  450. float param1;
  451. float param2;
  452. };
  453. struct vk_op_unary_push_constants {
  454. uint32_t ne;
  455. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  456. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  457. uint32_t misalign_offsets;
  458. float param1; float param2;
  459. uint32_t ne0_012mp; uint32_t ne0_012L;
  460. uint32_t ne0_01mp; uint32_t ne0_01L;
  461. uint32_t ne0_0mp; uint32_t ne0_0L;
  462. uint32_t ne1_012mp; uint32_t ne1_012L;
  463. uint32_t ne1_01mp; uint32_t ne1_01L;
  464. uint32_t ne1_0mp; uint32_t ne1_0L;
  465. };
  466. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  467. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  468. // Precompute mp (m' in the paper) and L such that division
  469. // can be computed using a multiply (high 32b of 64b result)
  470. // and a shift:
  471. //
  472. // n/d = (mulhi(n, mp) + n) >> L;
  473. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  474. {
  475. // compute L = ceil(log2(d));
  476. L = 0;
  477. while (L < 32 && (uint32_t{1} << L) < d) {
  478. L++;
  479. }
  480. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  481. }
  482. template <typename T> void init_pushconst_fastdiv(T &p) {
  483. GGML_UNUSED(p);
  484. static_assert(!std::is_const<T>::value, "unexpected type");
  485. }
  486. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  487. // Compute magic values to divide by these six numbers.
  488. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  489. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  490. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  491. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  492. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  493. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  494. }
  495. struct vk_op_binary_push_constants {
  496. uint32_t ne;
  497. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  498. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  499. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  500. uint32_t misalign_offsets;
  501. float param1; float param2; int32_t param3;
  502. };
  503. struct vk_op_diag_mask_push_constants {
  504. uint32_t ncols;
  505. uint32_t rows_per_channel;
  506. int32_t n_past;
  507. };
  508. struct vk_op_rope_push_constants {
  509. uint32_t ncols;
  510. uint32_t n_dims;
  511. float freq_scale;
  512. uint32_t p_delta_rows;
  513. float freq_base;
  514. float ext_factor;
  515. float attn_factor;
  516. float corr_dims[2];
  517. float theta_scale;
  518. uint32_t has_ff;
  519. uint32_t ne02;
  520. uint32_t s1;
  521. uint32_t s2;
  522. int32_t sections[4];
  523. uint32_t is_back;
  524. };
  525. struct vk_op_soft_max_push_constants {
  526. uint32_t KX;
  527. uint32_t KY;
  528. float scale;
  529. float max_bias;
  530. float m0;
  531. float m1;
  532. uint32_t n_head_log2;
  533. uint32_t nrows_x;
  534. };
  535. struct vk_op_argsort_push_constants {
  536. uint32_t ncols;
  537. uint32_t ncols_pad;
  538. int32_t order;
  539. };
  540. struct vk_op_im2col_push_constants {
  541. uint32_t batch_offset; uint32_t offset_delta;
  542. uint32_t IC;
  543. uint32_t IW; uint32_t IH;
  544. uint32_t OW; uint32_t OH;
  545. uint32_t KW; uint32_t KH;
  546. uint32_t pelements;
  547. uint32_t CHW;
  548. int32_t s0; int32_t s1;
  549. int32_t p0; int32_t p1;
  550. int32_t d0; int32_t d1;
  551. };
  552. struct vk_op_timestep_embedding_push_constants {
  553. uint32_t nb1;
  554. uint32_t dim;
  555. uint32_t max_period;
  556. };
  557. struct vk_op_pool2d_push_constants {
  558. uint32_t IW; uint32_t IH;
  559. uint32_t OW; uint32_t OH;
  560. uint32_t OC;
  561. uint32_t pelements;
  562. uint32_t op;
  563. int32_t k0; int32_t k1;
  564. int32_t s0; int32_t s1;
  565. int32_t p0; int32_t p1;
  566. };
  567. struct vk_op_rwkv_wkv6_push_constants {
  568. uint32_t B;
  569. uint32_t T;
  570. uint32_t C;
  571. uint32_t H;
  572. };
  573. struct vk_op_rwkv_wkv7_push_constants {
  574. uint32_t B;
  575. uint32_t T;
  576. uint32_t C;
  577. uint32_t H;
  578. };
  579. struct vk_op_upscale_push_constants {
  580. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  581. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  582. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  583. float sf0; float sf1; float sf2; float sf3;
  584. };
  585. // Allow pre-recording command buffers
  586. struct vk_staging_memcpy {
  587. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  588. void * dst;
  589. const void * src;
  590. size_t n;
  591. };
  592. struct vk_context_struct {
  593. vk_submission * s;
  594. std::vector<vk_sequence> seqs;
  595. int exit_tensor_idx;
  596. std::vector<vk_staging_memcpy> in_memcpys;
  597. std::vector<vk_staging_memcpy> out_memcpys;
  598. vk_queue * q;
  599. };
  600. typedef std::shared_ptr<vk_context_struct> vk_context;
  601. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  602. struct ggml_vk_garbage_collector {
  603. std::vector<vk_semaphore> tl_semaphores;
  604. std::vector<vk_semaphore> semaphores;
  605. std::vector<vk::Event> events;
  606. std::vector<vk_buffer> temp_buffers;
  607. std::vector<vk_context> contexts;
  608. };
  609. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  610. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  611. static std::string format_size(size_t size) {
  612. const size_t kib = 1024;
  613. const size_t mib = kib * 1024;
  614. const size_t gib = mib * 1024;
  615. std::ostringstream oss;
  616. oss << std::fixed << std::setprecision(2);
  617. if (size >= gib) {
  618. oss << static_cast<double>(size) / gib << " GiB";
  619. } else if (size >= mib) {
  620. oss << static_cast<double>(size) / mib << " MiB";
  621. } else if (size >= kib) {
  622. oss << static_cast<double>(size) / kib << " KiB";
  623. } else {
  624. oss << size << " B";
  625. }
  626. return oss.str();
  627. }
  628. static std::mutex log_mutex;
  629. class vk_memory_logger {
  630. public:
  631. vk_memory_logger(): total_device(0), total_host(0) {}
  632. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  633. void log_deallocation(vk_buffer_ref buf_ref);
  634. private:
  635. std::map<vk::Buffer, size_t> allocations; // Track allocations
  636. size_t total_device;
  637. size_t total_host;
  638. };
  639. #else
  640. #define VK_LOG_MEMORY(msg) ((void) 0)
  641. #endif // GGML_VULKAN_MEMORY_DEBUG
  642. #if defined(GGML_VULKAN_PERF)
  643. class vk_perf_logger {
  644. public:
  645. void print_timings() {
  646. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  647. for (const auto& t : timings) {
  648. uint64_t total = 0;
  649. for (const auto& time : t.second) {
  650. total += time;
  651. }
  652. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " ms" << std::endl;
  653. }
  654. timings.clear();
  655. }
  656. void log_timing(const ggml_tensor * node, uint64_t time) {
  657. if (node->op == GGML_OP_UNARY) {
  658. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  659. return;
  660. }
  661. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  662. const uint64_t m = node->src[0]->ne[1];
  663. const uint64_t n = node->src[1]->ne[1];
  664. const uint64_t k = node->src[1]->ne[0];
  665. std::string name = ggml_op_name(node->op);
  666. if (n == 1) {
  667. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  668. } else {
  669. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  670. }
  671. timings[name].push_back(time);
  672. return;
  673. }
  674. timings[ggml_op_name(node->op)].push_back(time);
  675. }
  676. private:
  677. std::map<std::string, std::vector<uint64_t>> timings;
  678. };
  679. #endif // GGML_VULKAN_PERF
  680. struct ggml_backend_vk_context {
  681. std::string name;
  682. vk_device device;
  683. size_t semaphore_idx, event_idx;
  684. ggml_vk_garbage_collector gc;
  685. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  686. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  687. vk::Fence fence, almost_ready_fence;
  688. bool almost_ready_fence_pending {};
  689. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  690. vk_context_ref compute_ctx;
  691. vk_context_ref transfer_ctx;
  692. std::vector<vk_context_ref> tensor_ctxs;
  693. };
  694. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  695. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  696. if (tensor->view_src) {
  697. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  698. }
  699. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  700. }
  701. struct ggml_backend_vk_buffer_context {
  702. vk_device_ref device;
  703. vk_buffer dev_buffer;
  704. std::string name;
  705. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  706. device(device),
  707. dev_buffer(dev_buffer),
  708. name(name) {
  709. }
  710. ~ggml_backend_vk_buffer_context() {
  711. ggml_vk_destroy_buffer(dev_buffer);
  712. }
  713. };
  714. #ifdef GGML_VULKAN_MEMORY_DEBUG
  715. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  716. std::lock_guard<std::mutex> guard(log_mutex);
  717. vk_buffer buf = buf_ref.lock();
  718. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  719. const std::string type = device ? "device" : "host";
  720. allocations[buf->buffer] = size;
  721. total_device += device ? size : 0;
  722. total_host += device ? 0 : size;
  723. 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));
  724. }
  725. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  726. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  727. return;
  728. }
  729. std::lock_guard<std::mutex> guard(log_mutex);
  730. vk_buffer buf = buf_ref.lock();
  731. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  732. std::string type = device ? "device" : "host";
  733. auto it = allocations.find(buf->buffer);
  734. total_device -= device ? it->second : 0;
  735. total_host -= device ? 0 : it->second;
  736. if (it != allocations.end()) {
  737. 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));
  738. allocations.erase(it);
  739. } else {
  740. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  741. }
  742. }
  743. #endif // GGML_VULKAN_MEMORY_DEBUG
  744. struct vk_instance_t {
  745. vk::Instance instance;
  746. std::vector<size_t> device_indices;
  747. vk_device devices[GGML_VK_MAX_DEVICES];
  748. };
  749. static bool vk_instance_initialized = false;
  750. static vk_instance_t vk_instance;
  751. #ifdef GGML_VULKAN_CHECK_RESULTS
  752. static size_t vk_skip_checks;
  753. static size_t vk_output_tensor;
  754. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  755. static void ggml_vk_check_results_0(ggml_tensor * tensor);
  756. static void ggml_vk_check_results_1(ggml_tensor * tensor);
  757. #endif
  758. 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);
  759. static void ggml_backend_vk_free(ggml_backend_t backend);
  760. // Wait for ctx->fence to be signaled.
  761. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  762. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  763. // during this wait.
  764. if (ctx->almost_ready_fence_pending) {
  765. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  766. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  767. ctx->almost_ready_fence_pending = false;
  768. }
  769. // Spin (w/pause) waiting for the graph to finish executing.
  770. vk::Result result;
  771. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  772. if (result != vk::Result::eNotReady) {
  773. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  774. exit(1);
  775. }
  776. for (uint32_t i = 0; i < 100; ++i) {
  777. YIELD();
  778. YIELD();
  779. YIELD();
  780. YIELD();
  781. YIELD();
  782. YIELD();
  783. YIELD();
  784. YIELD();
  785. YIELD();
  786. YIELD();
  787. }
  788. }
  789. ctx->device->device.resetFences({ ctx->fence });
  790. }
  791. // variables to track number of compiles in progress
  792. static uint32_t compile_count = 0;
  793. static std::mutex compile_count_mutex;
  794. static std::condition_variable compile_count_cond;
  795. 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,
  796. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  797. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  798. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  799. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  800. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  801. GGML_ASSERT(parameter_count > 0);
  802. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  803. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  804. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  805. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  806. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  807. for (uint32_t i = 0; i < parameter_count; i++) {
  808. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  809. dsl_binding_flags.push_back({});
  810. }
  811. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  812. vk::PushConstantRange pcr(
  813. vk::ShaderStageFlagBits::eCompute,
  814. 0,
  815. pipeline->push_constant_size
  816. );
  817. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  818. {},
  819. dsl_binding);
  820. descriptor_set_layout_create_info.setPNext(&dslbfci);
  821. pipeline->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  822. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  823. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  824. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  825. pipeline->descriptor_set_idx = 0;
  826. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), pipeline->dsl, pcr);
  827. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  828. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  829. for (size_t i = 0; i < specialization_constants.size(); i++) {
  830. specialization_entries[i].constantID = i;
  831. specialization_entries[i].offset = i * sizeof(uint32_t);
  832. specialization_entries[i].size = sizeof(uint32_t);
  833. }
  834. vk::SpecializationInfo specialization_info(
  835. specialization_entries.size(),
  836. specialization_entries.data(),
  837. specialization_constants.size() * sizeof(uint32_t),
  838. specialization_constants.data()
  839. );
  840. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  841. if (device->subgroup_require_full_support && require_full_subgroups) {
  842. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  843. }
  844. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  845. pipeline_shader_stage_create_flags,
  846. vk::ShaderStageFlagBits::eCompute,
  847. pipeline->shader_module,
  848. entrypoint.c_str(),
  849. &specialization_info);
  850. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  851. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  852. if (device->subgroup_size_control && required_subgroup_size > 0) {
  853. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  854. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  855. }
  856. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  857. vk::PipelineCreateFlags{},
  858. pipeline_shader_create_info,
  859. pipeline->layout);
  860. vk::PipelineRobustnessCreateInfoEXT rci;
  861. if (device->pipeline_robustness && disable_robustness) {
  862. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  863. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  864. compute_pipeline_create_info.setPNext(&rci);
  865. }
  866. try {
  867. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  868. } catch (const vk::SystemError& e) {
  869. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  870. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  871. throw e;
  872. }
  873. pipeline->compiled = true;
  874. {
  875. std::lock_guard<std::mutex> guard(device->mutex);
  876. device->pipelines.insert({ pipeline->name, pipeline });
  877. }
  878. {
  879. std::lock_guard<std::mutex> guard(compile_count_mutex);
  880. assert(compile_count > 0);
  881. compile_count--;
  882. }
  883. compile_count_cond.notify_all();
  884. }
  885. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  886. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  887. for (auto& pool : pipeline->descriptor_pools) {
  888. device.destroyDescriptorPool(pool);
  889. }
  890. pipeline->descriptor_pools.clear();
  891. pipeline->descriptor_sets.clear();
  892. pipeline->descriptor_set_idx = 0;
  893. device.destroyDescriptorSetLayout(pipeline->dsl);
  894. device.destroyPipelineLayout(pipeline->layout);
  895. device.destroyShaderModule(pipeline->shader_module);
  896. device.destroyPipeline(pipeline->pipeline);
  897. }
  898. static void ggml_pipeline_request_descriptor_sets(vk_device& device, vk_pipeline& pipeline, uint32_t n) {
  899. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  900. device->pipeline_descriptor_set_requirements[pipeline->name] += n;
  901. if (!pipeline->compiled) {
  902. pipeline->needed = true;
  903. device->need_compiles = true;
  904. }
  905. }
  906. static void ggml_pipeline_allocate_descriptor_sets(vk_device& device) {
  907. std::lock_guard<std::mutex> guard(device->mutex);
  908. for (auto& pair : device->pipeline_descriptor_set_requirements) {
  909. vk_pipeline pipeline = device->pipelines.at(pair.first).lock();
  910. const uint64_t n = pair.second;
  911. VK_LOG_DEBUG("ggml_pipeline_allocate_descriptor_sets(" << pipeline->name << ", " << n << ")");
  912. if (pipeline->descriptor_sets.size() >= pipeline->descriptor_set_idx + n) {
  913. // Enough descriptors are available
  914. continue;
  915. }
  916. uint32_t to_alloc = pipeline->descriptor_set_idx + n - pipeline->descriptor_sets.size();
  917. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - pipeline->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  918. uint32_t pool_idx = pipeline->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  919. while (to_alloc > 0) {
  920. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  921. to_alloc -= alloc_count;
  922. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  923. if (pool_idx >= pipeline->descriptor_pools.size()) {
  924. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  925. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  926. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  927. }
  928. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  929. for (uint32_t i = 0; i < alloc_count; i++) {
  930. layouts[i] = pipeline->dsl;
  931. }
  932. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[pool_idx], alloc_count, layouts.data());
  933. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  934. pipeline->descriptor_sets.insert(pipeline->descriptor_sets.end(), sets.begin(), sets.end());
  935. pool_idx++;
  936. }
  937. }
  938. }
  939. static void ggml_pipeline_cleanup(vk_pipeline& pipeline) {
  940. VK_LOG_DEBUG("ggml_pipeline_cleanup(" << pipeline->name << ")");
  941. pipeline->descriptor_set_idx = 0;
  942. }
  943. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_queue& q) {
  944. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  945. std::lock_guard<std::mutex> guard(device->mutex);
  946. if (q.cmd_buffers.size() > q.cmd_buffer_idx) {
  947. // Reuse command buffer
  948. return q.cmd_buffers[q.cmd_buffer_idx++];
  949. }
  950. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  951. q.pool,
  952. vk::CommandBufferLevel::ePrimary,
  953. 1);
  954. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  955. auto buf = cmd_buffers.front();
  956. q.cmd_buffers.push_back(buf);
  957. q.cmd_buffer_idx++;
  958. return buf;
  959. }
  960. static vk_submission ggml_vk_create_submission(vk_device& device, vk_queue& q, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  961. VK_LOG_DEBUG("ggml_vk_create_submission()");
  962. vk_submission s;
  963. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  964. s.wait_semaphores = std::move(wait_semaphores);
  965. s.signal_semaphores = std::move(signal_semaphores);
  966. return s;
  967. }
  968. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  969. if (ctx->seqs.empty()) {
  970. if (fence) {
  971. ctx->q->queue.submit({}, fence);
  972. }
  973. return;
  974. }
  975. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  976. std::vector<std::vector<uint64_t>> tl_wait_vals;
  977. std::vector<std::vector<uint64_t>> tl_signal_vals;
  978. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  979. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  980. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  981. std::vector<vk::SubmitInfo> submit_infos;
  982. int idx = -1;
  983. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  984. size_t reserve = 0;
  985. for (const auto& sequence : ctx->seqs) {
  986. reserve += sequence.size();
  987. }
  988. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  989. tl_wait_semaphores.reserve(reserve);
  990. tl_wait_vals.reserve(reserve);
  991. tl_signal_semaphores.reserve(reserve);
  992. tl_signal_vals.reserve(reserve);
  993. tl_submit_infos.reserve(reserve);
  994. submit_infos.reserve(reserve);
  995. stage_flags.reserve(reserve);
  996. for (const auto& sequence : ctx->seqs) {
  997. for (const auto& submission : sequence) {
  998. stage_flags.push_back({});
  999. idx++;
  1000. tl_wait_vals.push_back({});
  1001. tl_wait_semaphores.push_back({});
  1002. tl_signal_vals.push_back({});
  1003. tl_signal_semaphores.push_back({});
  1004. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1005. stage_flags[idx].push_back(ctx->q->stage_flags);
  1006. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1007. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1008. }
  1009. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1010. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1011. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1012. }
  1013. tl_submit_infos.push_back({
  1014. (uint32_t) submission.wait_semaphores.size(),
  1015. tl_wait_vals[idx].data(),
  1016. (uint32_t) submission.signal_semaphores.size(),
  1017. tl_signal_vals[idx].data(),
  1018. });
  1019. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1020. tl_submit_infos[idx].pNext = nullptr;
  1021. vk::SubmitInfo si{
  1022. (uint32_t) submission.wait_semaphores.size(),
  1023. tl_wait_semaphores[idx].data(),
  1024. stage_flags[idx].data(),
  1025. 1,
  1026. &submission.buffer,
  1027. (uint32_t) submission.signal_semaphores.size(),
  1028. tl_signal_semaphores[idx].data(),
  1029. };
  1030. si.setPNext(&tl_submit_infos[idx]);
  1031. submit_infos.push_back(si);
  1032. }
  1033. }
  1034. ctx->q->queue.submit(submit_infos, fence);
  1035. ctx->seqs.clear();
  1036. }
  1037. 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) {
  1038. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1039. const uint32_t qfsize = queue_family_props.size();
  1040. // Try with avoid preferences first
  1041. for (uint32_t i = 0; i < qfsize; i++) {
  1042. 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)) {
  1043. return i;
  1044. }
  1045. }
  1046. // Fall back to only required
  1047. for (size_t i = 0; i < qfsize; i++) {
  1048. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1049. return i;
  1050. }
  1051. }
  1052. // Fall back to reusing compute queue
  1053. for (size_t i = 0; i < qfsize; i++) {
  1054. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1055. return i;
  1056. }
  1057. }
  1058. // Fall back to ignoring min_num_queries
  1059. for (size_t i = 0; i < qfsize; i++) {
  1060. if (queue_family_props[i].queueFlags & required) {
  1061. return i;
  1062. }
  1063. }
  1064. // 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.
  1065. // 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.
  1066. if (compute_index >= 0) {
  1067. return compute_index;
  1068. }
  1069. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1070. for(auto &q_family : queue_family_props) {
  1071. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1072. }
  1073. abort();
  1074. }
  1075. 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) {
  1076. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1077. std::lock_guard<std::mutex> guard(device->mutex);
  1078. q.queue_family_index = queue_family_index;
  1079. q.transfer_only = transfer_only;
  1080. vk::CommandPoolCreateInfo command_pool_create_info_compute(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), queue_family_index);
  1081. q.pool = device->device.createCommandPool(command_pool_create_info_compute);
  1082. q.cmd_buffer_idx = 0;
  1083. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1084. q.stage_flags = stage_flags;
  1085. }
  1086. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_queue& q) {
  1087. vk_context result = std::make_shared<vk_context_struct>();
  1088. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1089. ctx->gc.contexts.emplace_back(result);
  1090. result->q = &q;
  1091. return result;
  1092. }
  1093. static vk_context ggml_vk_create_temporary_context(vk_queue& q) {
  1094. vk_context result = std::make_shared<vk_context_struct>();
  1095. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1096. result->q = &q;
  1097. return result;
  1098. }
  1099. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1100. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1101. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1102. vk::SemaphoreCreateInfo ci{};
  1103. ci.setPNext(&tci);
  1104. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1105. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1106. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1107. }
  1108. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1109. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1110. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1111. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1112. vk::SemaphoreCreateInfo ci{};
  1113. ci.setPNext(&tci);
  1114. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1115. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1116. }
  1117. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1118. }
  1119. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1120. if (ctx->event_idx >= ctx->gc.events.size()) {
  1121. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1122. }
  1123. return ctx->gc.events[ctx->event_idx++];
  1124. }
  1125. static void ggml_vk_queue_cleanup(vk_device& device, vk_queue& q) {
  1126. VK_LOG_DEBUG("ggml_vk_queue_cleanup()");
  1127. std::lock_guard<std::mutex> guard(device->mutex);
  1128. // Requires command buffers to be done
  1129. device->device.resetCommandPool(q.pool);
  1130. q.cmd_buffer_idx = 0;
  1131. }
  1132. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1133. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1134. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1135. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1136. (flags & memory_type.propertyFlags) == flags &&
  1137. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1138. return static_cast<int32_t>(i);
  1139. }
  1140. }
  1141. return UINT32_MAX;
  1142. }
  1143. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1144. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1145. if (size > device->max_memory_allocation_size) {
  1146. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1147. }
  1148. std::lock_guard<std::mutex> guard(device->mutex);
  1149. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1150. if (size == 0) {
  1151. buf->size = 0;
  1152. return buf;
  1153. }
  1154. vk::BufferCreateInfo buffer_create_info{
  1155. vk::BufferCreateFlags(),
  1156. size,
  1157. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1158. vk::SharingMode::eExclusive,
  1159. 0,
  1160. nullptr,
  1161. };
  1162. buf->buffer = device->device.createBuffer(buffer_create_info);
  1163. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1164. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1165. uint32_t memory_type_index = UINT32_MAX;
  1166. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1167. buf->memory_property_flags = req_flags;
  1168. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1169. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1170. buf->memory_property_flags = fallback_flags;
  1171. }
  1172. if (memory_type_index == UINT32_MAX) {
  1173. device->device.destroyBuffer(buf->buffer);
  1174. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1175. }
  1176. try {
  1177. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1178. } catch (const vk::SystemError& e) {
  1179. if (buf->memory_property_flags != fallback_flags) {
  1180. // Try again with fallback flags
  1181. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1182. buf->memory_property_flags = fallback_flags;
  1183. try {
  1184. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1185. }
  1186. catch (const vk::SystemError& e) {
  1187. device->device.destroyBuffer(buf->buffer);
  1188. throw e;
  1189. }
  1190. } else {
  1191. // Out of Host/Device memory, clean up buffer
  1192. device->device.destroyBuffer(buf->buffer);
  1193. throw e;
  1194. }
  1195. }
  1196. buf->ptr = nullptr;
  1197. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1198. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1199. }
  1200. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1201. buf->device = device;
  1202. buf->size = size;
  1203. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1204. device->memory_logger->log_allocation(buf, size);
  1205. #endif
  1206. return buf;
  1207. }
  1208. 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)) {
  1209. try {
  1210. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1211. } catch (const vk::SystemError& e) {
  1212. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1213. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1214. throw e;
  1215. }
  1216. }
  1217. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1218. vk_buffer buf;
  1219. try {
  1220. if (device->prefer_host_memory) {
  1221. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1222. } else if (device->uma) {
  1223. // Fall back to host memory type
  1224. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1225. } else {
  1226. // use rebar if available, otherwise fallback to device only visible memory
  1227. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1228. }
  1229. } catch (const vk::SystemError& e) {
  1230. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1231. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1232. throw e;
  1233. }
  1234. return buf;
  1235. }
  1236. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1237. if (buf == nullptr) {
  1238. return;
  1239. }
  1240. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1241. if (buf->device != nullptr) {
  1242. buf->device->memory_logger->log_deallocation(buf);
  1243. }
  1244. #endif
  1245. buf.reset();
  1246. }
  1247. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1248. return { buf, 0, VK_WHOLE_SIZE };
  1249. }
  1250. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1251. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1252. const bool transfer_queue = ctx->q->transfer_only;
  1253. ctx->s->buffer.pipelineBarrier(
  1254. ctx->q->stage_flags,
  1255. ctx->q->stage_flags,
  1256. {},
  1257. { {
  1258. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1259. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1260. } },
  1261. {},
  1262. {}
  1263. );
  1264. }
  1265. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1266. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1267. if (events.empty()) {
  1268. return;
  1269. }
  1270. ctx->s->buffer.waitEvents(
  1271. events,
  1272. ctx->q->stage_flags,
  1273. ctx->q->stage_flags,
  1274. {},
  1275. {},
  1276. {}
  1277. );
  1278. }
  1279. // number of rows/cols for flash attention shader
  1280. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1281. static std::array<uint32_t, 2> fa_rows_cols(uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) {
  1282. GGML_UNUSED(clamp);
  1283. // small rows, large cols
  1284. if (small_rows) {
  1285. return {flash_attention_num_small_rows, 64};
  1286. }
  1287. // small cols to reduce register count
  1288. if (ggml_is_quantized(type) || D == 256) {
  1289. return {64, 32};
  1290. }
  1291. return {64, 64};
  1292. };
  1293. 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) {
  1294. uint32_t lut_size = 0;
  1295. switch (src0_type) {
  1296. case GGML_TYPE_IQ1_S:
  1297. case GGML_TYPE_IQ1_M:
  1298. lut_size = 2*2048;
  1299. break;
  1300. case GGML_TYPE_IQ2_XXS:
  1301. lut_size = 8*256;
  1302. break;
  1303. case GGML_TYPE_IQ2_XS:
  1304. lut_size = 8*512;
  1305. break;
  1306. case GGML_TYPE_IQ2_S:
  1307. lut_size = 8*1024;
  1308. break;
  1309. case GGML_TYPE_IQ3_XXS:
  1310. lut_size = 4*256;
  1311. break;
  1312. case GGML_TYPE_IQ3_S:
  1313. lut_size = 4*512;
  1314. break;
  1315. case GGML_TYPE_IQ4_NL:
  1316. case GGML_TYPE_IQ4_XS:
  1317. lut_size = 4*16;
  1318. break;
  1319. default:
  1320. break;
  1321. }
  1322. // Needs to be kept up to date on shader changes
  1323. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1324. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1325. const uint32_t warps = warptile[0] / warptile[10];
  1326. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1327. const uint32_t mmid_row_ids = mul_mat_id ? 3072 * sizeof(uint32_t) : 0;
  1328. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1329. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1330. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1331. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1332. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1333. return supported;
  1334. }
  1335. struct GpuPipelineConfig {
  1336. // GPU architecture identifier.
  1337. // Example: vk_device_architecture::AMD_GCN
  1338. vk_device_architecture arch;
  1339. // Mapping of pipeline names to their specific subgroup sizes.
  1340. // Example: {"soft_max_f32", 64}
  1341. std::unordered_map<std::string, uint32_t> pipelines;
  1342. // Default subgroup size for this GPU.
  1343. // Defaults to 0 if not explicitly provided.
  1344. uint32_t default_subgroup_size = 0;
  1345. };
  1346. // Pipeline configuration for RDNA1 GPUs.
  1347. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1348. {"soft_max", 64}, {"im2col", 64},
  1349. {"argmax", 64}, {"mul_mat_vec", 64},
  1350. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1351. };
  1352. // Pipeline configuration for RDNA2 GPUs.
  1353. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1354. {"soft_max", 64}, {"im2col", 64},
  1355. };
  1356. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1357. // Define configurations for different GPUs.
  1358. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1359. {
  1360. vk_device_architecture::AMD_RDNA1,
  1361. {
  1362. rdna1_pipelines,
  1363. },
  1364. RDNA_DEFAULT_SUBGROUP_SIZE
  1365. },
  1366. {
  1367. vk_device_architecture::AMD_RDNA2,
  1368. {
  1369. rdna2_pipelines,
  1370. },
  1371. RDNA_DEFAULT_SUBGROUP_SIZE
  1372. },
  1373. };
  1374. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1375. for (const auto &config : gpu_pipeline_configs) {
  1376. if (config.arch == arch) {
  1377. auto pipIt = config.pipelines.find(pipeline_name);
  1378. if (pipIt != config.pipelines.end()) {
  1379. return pipIt->second;
  1380. }
  1381. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1382. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1383. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1384. for (const auto &entry : sorted_pipelines) {
  1385. if (pipeline_name.find(entry.first) != std::string::npos) {
  1386. return entry.second;
  1387. }
  1388. }
  1389. return config.default_subgroup_size;
  1390. }
  1391. }
  1392. return 0; // If no matching configuration is found
  1393. }
  1394. static void ggml_vk_load_shaders(vk_device& device) {
  1395. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1396. // some shaders have a minimum subgroup size
  1397. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1398. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1399. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1400. // mulmat
  1401. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1402. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1403. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1404. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1405. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1406. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1407. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1408. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1409. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1410. uint32_t l_align, m_align, s_align;
  1411. if (device->coopmat2) {
  1412. // spec constants and tile sizes for non-quant matmul/matmul_id
  1413. l_warptile = { 256, 128, 256, 64, 1 };
  1414. m_warptile = { 256, 128, 128, 64, 0 };
  1415. s_warptile = { 128, 64, 64, 64, 0 };
  1416. l_wg_denoms = {128, 256, 1 };
  1417. m_wg_denoms = {128, 128, 1 };
  1418. s_wg_denoms = { 64, 64, 1 };
  1419. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1420. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1421. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1422. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1423. l_mmq_wg_denoms = { 128, 256, 1 };
  1424. m_mmq_wg_denoms = { 128, 128, 1 };
  1425. s_mmq_wg_denoms = { 32, 64, 1 };
  1426. // spec constants and tile sizes for quant matmul (Qi_K)
  1427. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1428. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1429. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1430. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1431. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1432. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1433. // spec constants and tile sizes for quant matmul_id
  1434. l_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1435. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1436. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1437. l_mmqid_wg_denoms = { 128, 64, 1 };
  1438. m_mmqid_wg_denoms = { 128, 64, 1 };
  1439. s_mmqid_wg_denoms = { 128, 64, 1 };
  1440. l_align = 128;
  1441. m_align = 64;
  1442. s_align = 32;
  1443. } else {
  1444. // Matrix cores require different warp group sizes
  1445. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1446. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1447. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1448. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1449. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1450. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1451. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1452. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1453. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1454. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1455. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1456. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1457. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1458. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1459. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1460. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1461. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1462. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1463. // chip specific tuning
  1464. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1465. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1466. }
  1467. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1468. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1469. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1470. l_align = 128;
  1471. m_align = 64;
  1472. s_align = 32;
  1473. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1474. ggml_type t = (ggml_type)i;
  1475. // Disable medium and large matrix multiplication if not enough shared memory is available
  1476. // Check mmq warptiles as the largest configuration
  1477. // Throw an error if not enough for any matrix multiplication is available
  1478. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1479. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1480. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1481. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1482. device->mul_mat_m[i] = false;
  1483. device->mul_mat_l[i] = false;
  1484. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1485. device->mul_mat_l[i] = false;
  1486. }
  1487. // Disable mul_mat_id if not enough shared memory is available
  1488. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1489. device->mul_mat_id_s[i] = false;
  1490. device->mul_mat_id_m[i] = false;
  1491. device->mul_mat_id_l[i] = false;
  1492. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1493. device->mul_mat_id_m[i] = false;
  1494. device->mul_mat_id_l[i] = false;
  1495. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1496. device->mul_mat_id_l[i] = false;
  1497. }
  1498. }
  1499. }
  1500. if (!device->pipeline_matmul_f32) {
  1501. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1502. }
  1503. if (!device->pipeline_matmul_f32_f16) {
  1504. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1505. }
  1506. if (!device->pipeline_matmul_id_f32) {
  1507. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1508. }
  1509. std::vector<std::future<void>> compiles;
  1510. 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,
  1511. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1512. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1513. if (!require_full_subgroups && required_subgroup_size == 0) {
  1514. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1515. }
  1516. if (!pipeline) {
  1517. pipeline = std::make_shared<vk_pipeline_struct>();
  1518. pipeline->name = name;
  1519. pipeline->parameter_count = parameter_count;
  1520. pipeline->push_constant_size = push_constant_size;
  1521. pipeline->wg_denoms = wg_denoms;
  1522. pipeline->align = align;
  1523. }
  1524. if (!pipeline->needed || pipeline->compiled) {
  1525. return;
  1526. }
  1527. {
  1528. // wait until fewer than N compiles are in progress
  1529. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1530. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1531. while (compile_count >= N) {
  1532. compile_count_cond.wait(guard);
  1533. }
  1534. compile_count++;
  1535. }
  1536. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1537. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1538. };
  1539. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1540. if (device->coopmat2) {
  1541. auto const &fa_wg_denoms = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  1542. return {fa_rows_cols(D, clamp, type, small_rows)[0], 1, 1};
  1543. };
  1544. auto const &fa_spec_constants = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  1545. // For large number of rows, 128 invocations seems to work best.
  1546. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1547. // can't use 256 for D==80.
  1548. uint32_t wg_size = (small_rows && (D % 32) == 0) ? 256 : 128;
  1549. auto rows_cols = fa_rows_cols(D, clamp, type, small_rows);
  1550. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1551. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1552. return {wg_size, rows_cols[0], rows_cols[1], (D), clamp};
  1553. };
  1554. #define CREATE_FA2(TYPE, NAMELC, D) \
  1555. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][0], "flash_attn_f32_f16_D" #D "_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1556. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1557. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][0], "flash_attn_f32_f16_D" #D "_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1558. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1559. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][0], "flash_attn_f32_f16_D" #D "_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1560. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1561. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][0], "flash_attn_f32_f16_D" #D "_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1562. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1563. #define CREATE_FA(TYPE, NAMELC) \
  1564. CREATE_FA2(TYPE, NAMELC, 64) \
  1565. CREATE_FA2(TYPE, NAMELC, 80) \
  1566. CREATE_FA2(TYPE, NAMELC, 96) \
  1567. CREATE_FA2(TYPE, NAMELC, 112) \
  1568. CREATE_FA2(TYPE, NAMELC, 128) \
  1569. CREATE_FA2(TYPE, NAMELC, 256)
  1570. CREATE_FA(GGML_TYPE_F16, f16)
  1571. CREATE_FA(GGML_TYPE_Q4_0, q4_0)
  1572. CREATE_FA(GGML_TYPE_Q4_1, q4_1)
  1573. CREATE_FA(GGML_TYPE_Q5_0, q5_0)
  1574. CREATE_FA(GGML_TYPE_Q5_1, q5_1)
  1575. CREATE_FA(GGML_TYPE_Q8_0, q8_0)
  1576. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  1577. //CREATE_FA(GGML_TYPE_Q2_K, q2_k)
  1578. //CREATE_FA(GGML_TYPE_Q3_K, q3_k)
  1579. //CREATE_FA(GGML_TYPE_Q4_K, q4_k)
  1580. //CREATE_FA(GGML_TYPE_Q5_K, q5_k)
  1581. //CREATE_FA(GGML_TYPE_Q6_K, q6_k)
  1582. //CREATE_FA(GGML_TYPE_IQ1_S, iq1_s)
  1583. //CREATE_FA(GGML_TYPE_IQ1_M, iq1_m)
  1584. //CREATE_FA(GGML_TYPE_IQ2_XXS, iq2_xxs)
  1585. //CREATE_FA(GGML_TYPE_IQ2_XS, iq2_xs)
  1586. //CREATE_FA(GGML_TYPE_IQ2_S, iq2_s)
  1587. //CREATE_FA(GGML_TYPE_IQ3_XXS, iq3_xxs)
  1588. //CREATE_FA(GGML_TYPE_IQ3_S, iq3_s)
  1589. //CREATE_FA(GGML_TYPE_IQ4_XS, iq4_xs)
  1590. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl)
  1591. #undef CREATE_FA
  1592. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1593. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1594. 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); \
  1595. 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); \
  1596. 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); \
  1597. 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); \
  1598. 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); \
  1599. 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); \
  1600. // Create 2 variants, {f16,f32} accumulator
  1601. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1602. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1603. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1604. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1605. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1606. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1607. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1608. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1609. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1610. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1611. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1612. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1613. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1614. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1615. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1616. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1617. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1618. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1619. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1620. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1621. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1622. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1623. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1624. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1625. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1626. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1627. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1628. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1629. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1630. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1631. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1632. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1633. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1634. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1635. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1636. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1637. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1638. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1639. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1640. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1641. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1642. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1643. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1644. #undef CREATE_MM
  1645. #undef CREATE_MM2
  1646. } else
  1647. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1648. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1649. if (device->coopmat_support) {
  1650. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1651. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1652. if (device->mul_mat ## ID ## _l[TYPE]) \
  1653. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  1654. if (device->mul_mat ## ID ## _m[TYPE]) \
  1655. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  1656. if (device->mul_mat ## ID ## _s[TYPE]) \
  1657. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  1658. if (device->mul_mat ## ID ## _l[TYPE]) \
  1659. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  1660. if (device->mul_mat ## ID ## _m[TYPE]) \
  1661. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  1662. if (device->mul_mat ## ID ## _s[TYPE]) \
  1663. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  1664. // Create 2 variants, {f16,f32} accumulator
  1665. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1666. if (device->coopmat_acc_f16_support) { \
  1667. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1668. } \
  1669. if (device->coopmat_acc_f32_support) { \
  1670. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1671. } \
  1672. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1673. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1674. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1675. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1676. if (device->coopmat_acc_f16_support) {
  1677. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1678. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1679. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1680. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1681. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1682. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1683. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1684. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1685. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1686. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1687. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1688. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1689. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1690. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1691. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1692. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1693. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1694. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1695. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1696. } else {
  1697. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1698. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1699. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1700. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1701. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1702. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1703. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1704. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1705. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1706. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1707. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1708. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1709. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1710. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1711. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1712. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1713. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1714. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1715. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1716. }
  1717. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1718. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1719. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1720. if (device->coopmat_acc_f16_support) {
  1721. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1722. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1723. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1724. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1725. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1726. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1727. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1728. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1729. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1730. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1731. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1732. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1733. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1734. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1735. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1736. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1737. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1738. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1739. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1740. } else {
  1741. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1742. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1743. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1744. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1745. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1746. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1747. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1748. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1749. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1750. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1751. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1752. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1753. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1754. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1755. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1756. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1757. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1758. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1759. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1760. }
  1761. #undef CREATE_MM2
  1762. #undef CREATE_MM
  1763. } else
  1764. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1765. if (device->fp16) {
  1766. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1767. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1768. if (device->mul_mat ## ID ## _l[TYPE]) \
  1769. 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); \
  1770. if (device->mul_mat ## ID ## _m[TYPE]) \
  1771. 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); \
  1772. if (device->mul_mat ## ID ## _s[TYPE]) \
  1773. 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); \
  1774. if (device->mul_mat ## ID ## _l[TYPE]) \
  1775. 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); \
  1776. if (device->mul_mat ## ID ## _m[TYPE]) \
  1777. 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); \
  1778. if (device->mul_mat ## ID ## _s[TYPE]) \
  1779. 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); \
  1780. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1781. if (device->mul_mat ## ID ## _l[TYPE]) \
  1782. 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); \
  1783. if (device->mul_mat ## ID ## _m[TYPE]) \
  1784. 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); \
  1785. if (device->mul_mat ## ID ## _s[TYPE]) \
  1786. 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); \
  1787. // Create 2 variants, {f16,f32} accumulator
  1788. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1789. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1790. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1791. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1792. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1793. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1794. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1795. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1796. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1797. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1798. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1799. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1800. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1801. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1802. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1803. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1804. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1805. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1806. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1807. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1808. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1809. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1810. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1811. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1812. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1813. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1814. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  1815. if (device->integer_dot_product) {
  1816. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1817. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1818. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1819. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1820. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1821. }
  1822. #endif
  1823. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1824. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1825. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1826. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1827. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1828. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1829. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1830. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1831. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1832. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1833. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1834. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1835. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1836. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1837. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1838. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1839. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1840. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1841. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1842. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1843. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1844. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1845. #undef CREATE_MM2
  1846. #undef CREATE_MMQ
  1847. #undef CREATE_MM
  1848. } else {
  1849. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1850. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1851. if (device->mul_mat ## ID ## _l[TYPE]) \
  1852. 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); \
  1853. if (device->mul_mat ## ID ## _m[TYPE]) \
  1854. 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); \
  1855. if (device->mul_mat ## ID ## _s[TYPE]) \
  1856. 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); \
  1857. if (device->mul_mat ## ID ## _l[TYPE]) \
  1858. 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); \
  1859. if (device->mul_mat ## ID ## _m[TYPE]) \
  1860. 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); \
  1861. if (device->mul_mat ## ID ## _s[TYPE]) \
  1862. 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); \
  1863. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1864. if (device->mul_mat ## ID ## _l[TYPE]) \
  1865. 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); \
  1866. if (device->mul_mat ## ID ## _m[TYPE]) \
  1867. 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); \
  1868. if (device->mul_mat ## ID ## _s[TYPE]) \
  1869. 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); \
  1870. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1871. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1872. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1873. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1874. 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, );
  1875. 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, );
  1876. 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, );
  1877. 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, );
  1878. 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, );
  1879. 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, );
  1880. 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, );
  1881. 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, );
  1882. 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, );
  1883. 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, );
  1884. 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, );
  1885. 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, );
  1886. 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, );
  1887. 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, );
  1888. 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, );
  1889. 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, );
  1890. 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, );
  1891. 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, );
  1892. 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, );
  1893. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  1894. if (device->integer_dot_product) {
  1895. 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, );
  1896. 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, );
  1897. 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, );
  1898. 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, );
  1899. 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, );
  1900. }
  1901. #endif
  1902. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1903. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1904. 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);
  1905. 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);
  1906. 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);
  1907. 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);
  1908. 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);
  1909. 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);
  1910. 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);
  1911. 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);
  1912. 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);
  1913. 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);
  1914. 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);
  1915. 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);
  1916. 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);
  1917. 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);
  1918. 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);
  1919. 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);
  1920. 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);
  1921. 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);
  1922. 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);
  1923. 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);
  1924. #undef CREATE_MM
  1925. }
  1926. // mul mat vec
  1927. // the number of rows computed per shader depends on GPU model and quant
  1928. uint32_t rm_stdq = 1;
  1929. uint32_t rm_kq = 2;
  1930. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  1931. if (device->architecture == AMD_GCN) {
  1932. rm_stdq = 2;
  1933. rm_kq = 4;
  1934. }
  1935. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  1936. rm_stdq = 2;
  1937. uint32_t rm_iq = 2 * rm_kq;
  1938. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  1939. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1940. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1941. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1942. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1943. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1944. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1945. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f32_f32_len, mul_mat_vec_q8_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  1946. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1947. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1948. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1949. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1950. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1951. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1952. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1953. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1954. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1955. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1956. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1957. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1958. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1959. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1960. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1961. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1962. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1963. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1964. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1965. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1966. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f16_f32_len, mul_mat_vec_q8_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  1967. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1968. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1969. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1970. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1971. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1972. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1973. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1974. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1975. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1976. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1977. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1978. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1979. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1980. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1981. }
  1982. 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);
  1983. 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);
  1984. 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);
  1985. 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);
  1986. 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);
  1987. 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);
  1988. 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);
  1989. 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);
  1990. 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);
  1991. 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);
  1992. 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);
  1993. 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);
  1994. 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);
  1995. 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);
  1996. 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);
  1997. 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);
  1998. 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);
  1999. 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);
  2000. 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);
  2001. 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);
  2002. 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);
  2003. // dequant shaders
  2004. 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);
  2005. 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);
  2006. 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);
  2007. 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);
  2008. 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);
  2009. 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);
  2010. 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);
  2011. 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);
  2012. 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);
  2013. 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);
  2014. 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);
  2015. 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);
  2016. 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);
  2017. 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);
  2018. 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);
  2019. 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);
  2020. 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);
  2021. 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);
  2022. 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);
  2023. 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);
  2024. // get_rows
  2025. 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);
  2026. 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);
  2027. 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);
  2028. 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);
  2029. 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);
  2030. 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);
  2031. 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);
  2032. 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);
  2033. 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);
  2034. 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);
  2035. 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);
  2036. 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);
  2037. 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);
  2038. 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);
  2039. 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);
  2040. 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);
  2041. 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);
  2042. 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);
  2043. 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);
  2044. 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);
  2045. 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);
  2046. 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);
  2047. 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);
  2048. 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);
  2049. 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);
  2050. 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);
  2051. 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);
  2052. 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);
  2053. 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);
  2054. 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);
  2055. 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);
  2056. 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);
  2057. 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);
  2058. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_split_k_reduce, "fa_split_k_reduce", fa_split_k_reduce_len, fa_split_k_reduce_data, "main", 2, 3 * sizeof(uint32_t), {1, 1, 1}, {}, 1, true);
  2059. 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);
  2060. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2061. if (device->subgroup_add && device->subgroup_require_full_support) {
  2062. 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);
  2063. } else {
  2064. 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);
  2065. }
  2066. }
  2067. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_nc_f16_f32, "mul_mat_vec_nc_f16_f32", mul_mat_vec_nc_f16_f32_len, mul_mat_vec_nc_f16_f32_data, "main", 3, 7 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2068. 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);
  2069. 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);
  2070. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {1, 1, 1}, {}, 1);
  2071. 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);
  2072. 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);
  2073. 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);
  2074. 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);
  2075. 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);
  2076. 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);
  2077. 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);
  2078. 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);
  2079. if (device->float_controls_rte_fp16) {
  2080. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2081. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2082. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_rte_len, cpy_f32_q5_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2083. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_rte_len, cpy_f32_q5_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2084. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_rte_len, cpy_f32_q8_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2085. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_rte_len, cpy_f32_iq4_nl_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2086. } else {
  2087. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_len, cpy_f32_q4_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2088. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_len, cpy_f32_q4_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2089. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_len, cpy_f32_q5_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2090. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_len, cpy_f32_q5_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2091. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_len, cpy_f32_q8_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2092. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_len, cpy_f32_iq4_nl_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2093. }
  2094. 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);
  2095. 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);
  2096. 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);
  2097. 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);
  2098. 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);
  2099. 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);
  2100. ggml_vk_create_pipeline(device, device->pipeline_add_f32, "add_f32", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2101. ggml_vk_create_pipeline(device, device->pipeline_add_f32_norepeat, "add_f32_norepeat", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2102. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16, "add_f16_f32_f16", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2103. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16_norepeat, "add_f16_f32_f16_norepeat", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2104. 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);
  2105. ggml_vk_create_pipeline(device, device->pipeline_sub_f32, "sub_f32", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2106. ggml_vk_create_pipeline(device, device->pipeline_sub_f32_norepeat, "sub_f32_norepeat", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2107. ggml_vk_create_pipeline(device, device->pipeline_mul_f32, "mul_f32", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2108. ggml_vk_create_pipeline(device, device->pipeline_mul_f32_norepeat, "mul_f32_norepeat", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2109. ggml_vk_create_pipeline(device, device->pipeline_div_f32, "div_f32", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2110. ggml_vk_create_pipeline(device, device->pipeline_div_f32_norepeat, "div_f32_norepeat", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2111. 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);
  2112. 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);
  2113. 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);
  2114. ggml_vk_create_pipeline(device, device->pipeline_upscale_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {}, 1);
  2115. 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);
  2116. 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);
  2117. 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);
  2118. 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);
  2119. 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);
  2120. 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);
  2121. 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);
  2122. 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);
  2123. ggml_vk_create_pipeline(device, device->pipeline_gelu_f32, "gelu_f32", gelu_f32_len, gelu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2124. ggml_vk_create_pipeline(device, device->pipeline_gelu_quick_f32, "gelu_quick_f32", gelu_quick_f32_len, gelu_quick_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2125. ggml_vk_create_pipeline(device, device->pipeline_silu_f32, "silu_f32", silu_f32_len, silu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2126. 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);
  2127. ggml_vk_create_pipeline(device, device->pipeline_relu_f32, "relu_f32", relu_f32_len, relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2128. 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);
  2129. ggml_vk_create_pipeline(device, device->pipeline_tanh_f32, "tanh_f32", tanh_f32_len, tanh_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2130. ggml_vk_create_pipeline(device, device->pipeline_sigmoid_f32, "sigmoid_f32", sigmoid_f32_len, sigmoid_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2131. 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);
  2132. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2133. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2134. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2135. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2136. 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);
  2137. 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);
  2138. 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);
  2139. 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);
  2140. 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);
  2141. if (device->float_controls_rte_fp16) {
  2142. 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);
  2143. 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);
  2144. 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);
  2145. 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);
  2146. } else {
  2147. 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);
  2148. 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);
  2149. 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);
  2150. 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);
  2151. }
  2152. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  2153. 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);
  2154. 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);
  2155. 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);
  2156. 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);
  2157. if (device->float_controls_rte_fp16) {
  2158. 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);
  2159. } else {
  2160. 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);
  2161. }
  2162. 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);
  2163. 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);
  2164. 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);
  2165. 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);
  2166. 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);
  2167. for (auto &c : compiles) {
  2168. c.wait();
  2169. }
  2170. device->need_compiles = false;
  2171. }
  2172. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2173. static vk_device ggml_vk_get_device(size_t idx) {
  2174. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2175. if (vk_instance.devices[idx] == nullptr) {
  2176. VK_LOG_DEBUG("Initializing new vk_device");
  2177. vk_device device = std::make_shared<vk_device_struct>();
  2178. vk_instance.devices[idx] = device;
  2179. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2180. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2181. #endif
  2182. #ifdef GGML_VULKAN_PERF
  2183. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2184. #endif
  2185. size_t dev_num = vk_instance.device_indices[idx];
  2186. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2187. if (dev_num >= physical_devices.size()) {
  2188. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2189. throw std::runtime_error("Device not found");
  2190. }
  2191. device->physical_device = physical_devices[dev_num];
  2192. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2193. device->architecture = get_device_architecture(device->physical_device);
  2194. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2195. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2196. bool fp16_storage = false;
  2197. bool fp16_compute = false;
  2198. bool maintenance4_support = false;
  2199. bool sm_builtins = false;
  2200. bool amd_shader_core_properties2 = false;
  2201. bool pipeline_robustness = false;
  2202. bool coopmat2_support = false;
  2203. device->coopmat_support = false;
  2204. device->integer_dot_product = false;
  2205. for (const auto& properties : ext_props) {
  2206. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2207. maintenance4_support = true;
  2208. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2209. fp16_storage = true;
  2210. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2211. fp16_compute = true;
  2212. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2213. sm_builtins = true;
  2214. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2215. amd_shader_core_properties2 = true;
  2216. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2217. pipeline_robustness = true;
  2218. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2219. device->subgroup_size_control = true;
  2220. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2221. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2222. device->coopmat_support = true;
  2223. device->coopmat_m = 0;
  2224. device->coopmat_n = 0;
  2225. device->coopmat_k = 0;
  2226. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2227. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2228. coopmat2_support = true;
  2229. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2230. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2231. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2232. device->integer_dot_product = true;
  2233. #endif
  2234. }
  2235. }
  2236. vk::PhysicalDeviceProperties2 props2;
  2237. vk::PhysicalDeviceMaintenance3Properties props3;
  2238. vk::PhysicalDeviceMaintenance4Properties props4;
  2239. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2240. vk::PhysicalDeviceDriverProperties driver_props;
  2241. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2242. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2243. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2244. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2245. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2246. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2247. props2.pNext = &props3;
  2248. props3.pNext = &subgroup_props;
  2249. subgroup_props.pNext = &driver_props;
  2250. driver_props.pNext = &vk11_props;
  2251. vk11_props.pNext = &vk12_props;
  2252. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2253. if (maintenance4_support) {
  2254. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2255. last_struct = (VkBaseOutStructure *)&props4;
  2256. }
  2257. if (sm_builtins) {
  2258. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2259. last_struct = (VkBaseOutStructure *)&sm_props;
  2260. }
  2261. if (amd_shader_core_properties2) {
  2262. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2263. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2264. }
  2265. if (device->subgroup_size_control) {
  2266. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2267. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2268. }
  2269. #if defined(VK_NV_cooperative_matrix2)
  2270. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2271. if (coopmat2_support) {
  2272. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2273. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2274. }
  2275. #endif
  2276. if (device->integer_dot_product) {
  2277. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2278. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2279. }
  2280. device->physical_device.getProperties2(&props2);
  2281. device->properties = props2.properties;
  2282. device->vendor_id = device->properties.vendorID;
  2283. device->driver_id = driver_props.driverID;
  2284. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2285. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2286. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2287. } else if (maintenance4_support) {
  2288. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2289. } else {
  2290. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2291. }
  2292. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2293. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2294. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2295. } else {
  2296. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2297. device->suballocation_block_size = 1024*1024*1024;
  2298. }
  2299. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2300. device->subgroup_size = subgroup_props.subgroupSize;
  2301. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2302. if (sm_builtins) {
  2303. device->shader_core_count = sm_props.shaderSMCount;
  2304. } else if (amd_shader_core_properties2) {
  2305. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2306. } else {
  2307. device->shader_core_count = 0;
  2308. }
  2309. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2310. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2311. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2312. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2313. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2314. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2315. device->coopmat_support = false;
  2316. }
  2317. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2318. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2319. // Try to find a non-graphics compute queue and transfer-focused queues
  2320. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2321. 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);
  2322. const float priorities[] = { 1.0f, 1.0f };
  2323. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2324. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2325. if (compute_queue_family_index != transfer_queue_family_index) {
  2326. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2327. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2328. } else if(!device->single_queue) {
  2329. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2330. } else {
  2331. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2332. }
  2333. vk::DeviceCreateInfo device_create_info;
  2334. std::vector<const char *> device_extensions;
  2335. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2336. VkPhysicalDeviceFeatures2 device_features2;
  2337. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2338. device_features2.pNext = nullptr;
  2339. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2340. VkPhysicalDeviceVulkan11Features vk11_features;
  2341. vk11_features.pNext = nullptr;
  2342. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2343. device_features2.pNext = &vk11_features;
  2344. VkPhysicalDeviceVulkan12Features vk12_features;
  2345. vk12_features.pNext = nullptr;
  2346. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2347. vk11_features.pNext = &vk12_features;
  2348. last_struct = (VkBaseOutStructure *)&vk12_features;
  2349. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2350. pl_robustness_features.pNext = nullptr;
  2351. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2352. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2353. if (pipeline_robustness) {
  2354. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2355. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2356. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2357. }
  2358. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2359. subgroup_size_control_features.pNext = nullptr;
  2360. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2361. subgroup_size_control_features.computeFullSubgroups = false;
  2362. subgroup_size_control_features.subgroupSizeControl = false;
  2363. if (device->subgroup_size_control) {
  2364. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2365. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2366. }
  2367. #if defined(VK_KHR_cooperative_matrix)
  2368. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2369. coopmat_features.pNext = nullptr;
  2370. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2371. coopmat_features.cooperativeMatrix = VK_FALSE;
  2372. if (device->coopmat_support) {
  2373. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2374. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2375. }
  2376. #endif
  2377. #if defined(VK_NV_cooperative_matrix2)
  2378. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2379. coopmat2_features.pNext = nullptr;
  2380. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2381. if (coopmat2_support) {
  2382. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2383. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2384. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2385. }
  2386. #endif
  2387. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2388. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2389. if (maintenance4_support) {
  2390. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2391. last_struct = (VkBaseOutStructure *)&maint4_features;
  2392. device_extensions.push_back("VK_KHR_maintenance4");
  2393. }
  2394. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2395. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2396. if (device->integer_dot_product) {
  2397. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2398. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2399. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  2400. }
  2401. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2402. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2403. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2404. if (device->subgroup_size_control) {
  2405. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2406. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2407. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2408. }
  2409. device->subgroup_size_control = device->subgroup_size_control &&
  2410. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2411. subgroup_size_control_features.subgroupSizeControl;
  2412. if (device->subgroup_size_control) {
  2413. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2414. }
  2415. #if defined(VK_KHR_cooperative_matrix)
  2416. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2417. #endif
  2418. if (coopmat2_support) {
  2419. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2420. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2421. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2422. coopmat2_features.cooperativeMatrixReductions &&
  2423. coopmat2_features.cooperativeMatrixConversions &&
  2424. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2425. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2426. coopmat2_features.cooperativeMatrixBlockLoads &&
  2427. vk12_features.bufferDeviceAddress) {
  2428. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2429. uint32_t count = 0;
  2430. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2431. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2432. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2433. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2434. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2435. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2436. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2437. flexible_dimensions.resize(count, empty_prop);
  2438. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2439. bool found_fp16_128 = false,
  2440. found_fp16_256 = false,
  2441. found_fp32_128 = false,
  2442. found_fp32_256 = false;
  2443. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2444. // with 32x16x16 and 256 with 32x32x16.
  2445. for (auto &prop : flexible_dimensions) {
  2446. if (prop.saturatingAccumulation == VK_FALSE &&
  2447. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2448. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2449. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2450. if (prop.workgroupInvocations == 128 &&
  2451. prop.MGranularity <= 32 &&
  2452. prop.NGranularity <= 16 &&
  2453. prop.KGranularity <= 16) {
  2454. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2455. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2456. found_fp16_128 = true;
  2457. }
  2458. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2459. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2460. found_fp32_128 = true;
  2461. }
  2462. }
  2463. if (prop.workgroupInvocations == 256 &&
  2464. prop.MGranularity <= 32 &&
  2465. prop.NGranularity <= 32 &&
  2466. prop.KGranularity <= 16) {
  2467. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2468. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2469. found_fp16_256 = true;
  2470. }
  2471. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2472. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2473. found_fp32_256 = true;
  2474. }
  2475. }
  2476. }
  2477. }
  2478. if (found_fp16_128 && found_fp16_256 &&
  2479. found_fp32_128 && found_fp32_256 &&
  2480. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2481. device->coopmat2 = true;
  2482. }
  2483. }
  2484. #endif
  2485. }
  2486. if (!vk11_features.storageBuffer16BitAccess) {
  2487. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2488. throw std::runtime_error("Unsupported device");
  2489. }
  2490. device_extensions.push_back("VK_KHR_16bit_storage");
  2491. #ifdef GGML_VULKAN_VALIDATE
  2492. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2493. #endif
  2494. if (device->fp16) {
  2495. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2496. }
  2497. #if defined(VK_KHR_cooperative_matrix)
  2498. if (device->coopmat_support) {
  2499. // Query supported shapes
  2500. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2501. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2502. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2503. uint32_t cm_props_num;
  2504. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2505. cm_props.resize(cm_props_num);
  2506. for (auto& prop : cm_props) {
  2507. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2508. }
  2509. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2510. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2511. for (auto& prop : cm_props) {
  2512. 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));
  2513. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2514. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2515. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2516. ) {
  2517. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2518. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2519. // coopmat sizes not set yet
  2520. if (device->coopmat_m == 0) {
  2521. device->coopmat_acc_f32_support = true;
  2522. device->coopmat_m = prop.MSize;
  2523. device->coopmat_n = prop.NSize;
  2524. device->coopmat_k = prop.KSize;
  2525. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2526. // Only enable if shape is identical
  2527. device->coopmat_acc_f32_support = true;
  2528. }
  2529. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2530. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2531. // coopmat sizes not set yet
  2532. if (device->coopmat_m == 0) {
  2533. device->coopmat_acc_f16_support = true;
  2534. device->coopmat_m = prop.MSize;
  2535. device->coopmat_n = prop.NSize;
  2536. device->coopmat_k = prop.KSize;
  2537. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2538. // Only enable if shape is identical
  2539. device->coopmat_acc_f16_support = true;
  2540. }
  2541. }
  2542. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  2543. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  2544. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  2545. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  2546. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  2547. device->coopmat_int_m == 0
  2548. ) {
  2549. device->coopmat_int_support = true;
  2550. device->coopmat_int_m = prop.MSize;
  2551. device->coopmat_int_n = prop.NSize;
  2552. device->coopmat_int_k = prop.KSize;
  2553. }
  2554. }
  2555. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2556. // No suitable matmul mode found
  2557. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2558. device->coopmat_support = false;
  2559. }
  2560. }
  2561. if (device->coopmat_support) {
  2562. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2563. }
  2564. #endif
  2565. device->name = GGML_VK_NAME + std::to_string(idx);
  2566. device_create_info = {
  2567. vk::DeviceCreateFlags(),
  2568. device_queue_create_infos,
  2569. {},
  2570. device_extensions
  2571. };
  2572. device_create_info.setPNext(&device_features2);
  2573. device->device = device->physical_device.createDevice(device_create_info);
  2574. // Queues
  2575. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2576. // Shaders
  2577. // Disable matmul tile sizes early if performance low or not supported
  2578. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2579. switch (device->vendor_id) {
  2580. #ifndef GGML_VULKAN_RUN_TESTS
  2581. case VK_VENDOR_ID_AMD:
  2582. case VK_VENDOR_ID_INTEL:
  2583. device->mul_mat_l[i] = false;
  2584. device->mul_mat_m[i] = true;
  2585. device->mul_mat_s[i] = true;
  2586. device->mul_mat_id_l[i] = false;
  2587. device->mul_mat_id_m[i] = true;
  2588. device->mul_mat_id_s[i] = true;
  2589. break;
  2590. case VK_VENDOR_ID_APPLE:
  2591. device->mul_mat_l[i] = false;
  2592. device->mul_mat_m[i] = true;
  2593. device->mul_mat_s[i] = false;
  2594. device->mul_mat_id_l[i] = false;
  2595. device->mul_mat_id_m[i] = true;
  2596. device->mul_mat_id_s[i] = false;
  2597. break;
  2598. #endif
  2599. default:
  2600. device->mul_mat_l[i] = true;
  2601. device->mul_mat_m[i] = true;
  2602. device->mul_mat_s[i] = true;
  2603. device->mul_mat_id_l[i] = true;
  2604. device->mul_mat_id_m[i] = true;
  2605. device->mul_mat_id_s[i] = true;
  2606. break;
  2607. }
  2608. }
  2609. ggml_vk_load_shaders(device);
  2610. if (!device->single_queue) {
  2611. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  2612. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  2613. } else {
  2614. // TODO: Use pointer or reference to avoid copy
  2615. device->transfer_queue = device->compute_queue;
  2616. }
  2617. device->buffer_type = {
  2618. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  2619. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  2620. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  2621. };
  2622. device->fence = device->device.createFence({});
  2623. device->idx = idx;
  2624. return device;
  2625. }
  2626. return vk_instance.devices[idx];
  2627. }
  2628. static void ggml_vk_print_gpu_info(size_t idx) {
  2629. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2630. size_t dev_num = vk_instance.device_indices[idx];
  2631. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  2632. GGML_ASSERT(vk_instance_initialized);
  2633. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2634. if (dev_num >= devices.size()) {
  2635. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2636. throw std::runtime_error("Device not found");
  2637. }
  2638. vk::PhysicalDevice physical_device = devices[dev_num];
  2639. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  2640. bool fp16_storage = false;
  2641. bool fp16_compute = false;
  2642. bool coopmat_support = false;
  2643. bool coopmat2_support = false;
  2644. bool integer_dot_product = false;
  2645. for (auto properties : ext_props) {
  2646. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2647. fp16_storage = true;
  2648. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2649. fp16_compute = true;
  2650. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2651. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2652. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2653. coopmat_support = true;
  2654. #endif
  2655. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2656. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2657. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2658. coopmat2_support = true;
  2659. #endif
  2660. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2661. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2662. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2663. integer_dot_product = true;
  2664. #endif
  2665. }
  2666. }
  2667. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  2668. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  2669. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  2670. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2671. vk::PhysicalDeviceProperties2 props2;
  2672. vk::PhysicalDeviceMaintenance3Properties props3;
  2673. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2674. vk::PhysicalDeviceDriverProperties driver_props;
  2675. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2676. props2.pNext = &props3;
  2677. props3.pNext = &subgroup_props;
  2678. subgroup_props.pNext = &driver_props;
  2679. // Pointer to the last chain element
  2680. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  2681. if (integer_dot_product) {
  2682. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2683. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2684. }
  2685. physical_device.getProperties2(&props2);
  2686. VkPhysicalDeviceFeatures2 device_features2;
  2687. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2688. device_features2.pNext = nullptr;
  2689. VkPhysicalDeviceVulkan11Features vk11_features;
  2690. vk11_features.pNext = nullptr;
  2691. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2692. device_features2.pNext = &vk11_features;
  2693. VkPhysicalDeviceVulkan12Features vk12_features;
  2694. vk12_features.pNext = nullptr;
  2695. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2696. vk11_features.pNext = &vk12_features;
  2697. // Pointer to the last chain element
  2698. last_struct = (VkBaseOutStructure *)&vk12_features;
  2699. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2700. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2701. coopmat_features.pNext = nullptr;
  2702. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2703. coopmat_features.cooperativeMatrix = VK_FALSE;
  2704. if (coopmat_support) {
  2705. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2706. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2707. }
  2708. #endif
  2709. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2710. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2711. if (integer_dot_product) {
  2712. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2713. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2714. }
  2715. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  2716. fp16 = fp16 && vk12_features.shaderFloat16;
  2717. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  2718. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  2719. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2720. integer_dot_product = integer_dot_product
  2721. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  2722. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  2723. coopmat_support = coopmat_support
  2724. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2725. && coopmat_features.cooperativeMatrix
  2726. #endif
  2727. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  2728. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  2729. std::string device_name = props2.properties.deviceName.data();
  2730. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  2731. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  2732. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  2733. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  2734. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  2735. }
  2736. }
  2737. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2738. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2739. static void ggml_vk_instance_init() {
  2740. if (vk_instance_initialized) {
  2741. return;
  2742. }
  2743. VK_LOG_DEBUG("ggml_vk_instance_init()");
  2744. uint32_t api_version = vk::enumerateInstanceVersion();
  2745. if (api_version < VK_API_VERSION_1_2) {
  2746. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  2747. GGML_ABORT("fatal error");
  2748. }
  2749. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  2750. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  2751. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  2752. #ifdef __APPLE__
  2753. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  2754. #endif
  2755. std::vector<const char*> layers;
  2756. if (validation_ext) {
  2757. layers.push_back("VK_LAYER_KHRONOS_validation");
  2758. }
  2759. std::vector<const char*> extensions;
  2760. if (validation_ext) {
  2761. extensions.push_back("VK_EXT_validation_features");
  2762. }
  2763. #ifdef __APPLE__
  2764. if (portability_enumeration_ext) {
  2765. extensions.push_back("VK_KHR_portability_enumeration");
  2766. }
  2767. #endif
  2768. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  2769. #ifdef __APPLE__
  2770. if (portability_enumeration_ext) {
  2771. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  2772. }
  2773. #endif
  2774. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  2775. vk::ValidationFeaturesEXT validation_features;
  2776. if (validation_ext) {
  2777. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  2778. validation_features = {
  2779. features_enable,
  2780. {},
  2781. };
  2782. validation_features.setPNext(nullptr);
  2783. instance_create_info.setPNext(&validation_features);
  2784. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  2785. }
  2786. vk_instance.instance = vk::createInstance(instance_create_info);
  2787. vk_instance_initialized = true;
  2788. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  2789. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  2790. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  2791. if (devices_env != nullptr) {
  2792. std::string devices(devices_env);
  2793. std::replace(devices.begin(), devices.end(), ',', ' ');
  2794. std::stringstream ss(devices);
  2795. size_t tmp;
  2796. while (ss >> tmp) {
  2797. if(tmp >= num_available_devices) {
  2798. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  2799. throw std::runtime_error("Invalid Vulkan device index");
  2800. }
  2801. vk_instance.device_indices.push_back(tmp);
  2802. }
  2803. } else {
  2804. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2805. // Make sure at least one device exists
  2806. if (devices.empty()) {
  2807. std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
  2808. return;
  2809. }
  2810. // Default to using all dedicated GPUs
  2811. for (size_t i = 0; i < devices.size(); i++) {
  2812. vk::PhysicalDeviceProperties2 new_props;
  2813. vk::PhysicalDeviceDriverProperties new_driver;
  2814. vk::PhysicalDeviceIDProperties new_id;
  2815. new_props.pNext = &new_driver;
  2816. new_driver.pNext = &new_id;
  2817. devices[i].getProperties2(&new_props);
  2818. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  2819. // Check if there are two physical devices corresponding to the same GPU
  2820. auto old_device = std::find_if(
  2821. vk_instance.device_indices.begin(),
  2822. vk_instance.device_indices.end(),
  2823. [&devices, &new_id](const size_t k){
  2824. vk::PhysicalDeviceProperties2 old_props;
  2825. vk::PhysicalDeviceIDProperties old_id;
  2826. old_props.pNext = &old_id;
  2827. devices[k].getProperties2(&old_props);
  2828. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  2829. }
  2830. );
  2831. if (old_device == vk_instance.device_indices.end()) {
  2832. vk_instance.device_indices.push_back(i);
  2833. } else {
  2834. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  2835. // This can cause error when splitting layers aross the devices, need to keep only 1
  2836. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  2837. vk::PhysicalDeviceProperties2 old_props;
  2838. vk::PhysicalDeviceDriverProperties old_driver;
  2839. old_props.pNext = &old_driver;
  2840. devices[*old_device].getProperties2(&old_props);
  2841. std::map<vk::DriverId, int> driver_priorities {};
  2842. int old_priority = std::numeric_limits<int>::max();
  2843. int new_priority = std::numeric_limits<int>::max();
  2844. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  2845. // Smaller number -> higher priority
  2846. switch (old_props.properties.vendorID) {
  2847. case VK_VENDOR_ID_AMD:
  2848. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  2849. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  2850. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  2851. break;
  2852. case VK_VENDOR_ID_INTEL:
  2853. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  2854. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  2855. break;
  2856. case VK_VENDOR_ID_NVIDIA:
  2857. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  2858. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  2859. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  2860. #endif
  2861. break;
  2862. }
  2863. if (driver_priorities.count(old_driver.driverID)) {
  2864. old_priority = driver_priorities[old_driver.driverID];
  2865. }
  2866. if (driver_priorities.count(new_driver.driverID)) {
  2867. new_priority = driver_priorities[new_driver.driverID];
  2868. }
  2869. if (new_priority < old_priority) {
  2870. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  2871. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  2872. vk_instance.device_indices.push_back(i);
  2873. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  2874. }
  2875. else {
  2876. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  2877. }
  2878. }
  2879. }
  2880. }
  2881. // If no dedicated GPUs found, fall back to GPU 0
  2882. if (vk_instance.device_indices.empty()) {
  2883. vk_instance.device_indices.push_back(0);
  2884. }
  2885. }
  2886. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  2887. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  2888. ggml_vk_print_gpu_info(i);
  2889. }
  2890. }
  2891. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  2892. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  2893. ggml_vk_instance_init();
  2894. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2895. ctx->name = GGML_VK_NAME + std::to_string(idx);
  2896. ctx->device = ggml_vk_get_device(idx);
  2897. ctx->semaphore_idx = 0;
  2898. ctx->event_idx = 0;
  2899. ctx->prealloc_size_x = 0;
  2900. ctx->prealloc_size_y = 0;
  2901. ctx->prealloc_size_split_k = 0;
  2902. ctx->fence = ctx->device->device.createFence({});
  2903. ctx->almost_ready_fence = ctx->device->device.createFence({});
  2904. #ifdef GGML_VULKAN_CHECK_RESULTS
  2905. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  2906. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  2907. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  2908. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  2909. #endif
  2910. }
  2911. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  2912. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  2913. switch (type) {
  2914. case GGML_TYPE_F32:
  2915. case GGML_TYPE_Q4_0:
  2916. case GGML_TYPE_Q4_1:
  2917. case GGML_TYPE_Q5_0:
  2918. case GGML_TYPE_Q5_1:
  2919. case GGML_TYPE_Q8_0:
  2920. case GGML_TYPE_Q2_K:
  2921. case GGML_TYPE_Q3_K:
  2922. case GGML_TYPE_Q4_K:
  2923. case GGML_TYPE_Q5_K:
  2924. case GGML_TYPE_Q6_K:
  2925. case GGML_TYPE_IQ1_S:
  2926. case GGML_TYPE_IQ1_M:
  2927. case GGML_TYPE_IQ2_XXS:
  2928. case GGML_TYPE_IQ2_XS:
  2929. case GGML_TYPE_IQ2_S:
  2930. case GGML_TYPE_IQ3_XXS:
  2931. case GGML_TYPE_IQ3_S:
  2932. case GGML_TYPE_IQ4_XS:
  2933. case GGML_TYPE_IQ4_NL:
  2934. break;
  2935. default:
  2936. return nullptr;
  2937. }
  2938. return ctx->device->pipeline_dequant[type];
  2939. }
  2940. 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) {
  2941. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  2942. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2943. return ctx->device->pipeline_matmul_f32;
  2944. }
  2945. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  2946. return ctx->device->pipeline_matmul_f32_f16;
  2947. }
  2948. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2949. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2950. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  2951. }
  2952. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2953. return ctx->device->pipeline_matmul_f16.f16acc;
  2954. }
  2955. } else {
  2956. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2957. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  2958. }
  2959. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2960. return ctx->device->pipeline_matmul_f16.f32acc;
  2961. }
  2962. }
  2963. // MMQ
  2964. if (src1_type == GGML_TYPE_Q8_1) {
  2965. vk_matmul_pipeline pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f16acc;
  2966. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  2967. return nullptr;
  2968. }
  2969. return pipelines;
  2970. }
  2971. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  2972. return nullptr;
  2973. }
  2974. switch (src0_type) {
  2975. case GGML_TYPE_Q4_0:
  2976. case GGML_TYPE_Q4_1:
  2977. case GGML_TYPE_Q5_0:
  2978. case GGML_TYPE_Q5_1:
  2979. case GGML_TYPE_Q8_0:
  2980. case GGML_TYPE_Q2_K:
  2981. case GGML_TYPE_Q3_K:
  2982. case GGML_TYPE_Q4_K:
  2983. case GGML_TYPE_Q5_K:
  2984. case GGML_TYPE_Q6_K:
  2985. case GGML_TYPE_IQ1_S:
  2986. case GGML_TYPE_IQ1_M:
  2987. case GGML_TYPE_IQ2_XXS:
  2988. case GGML_TYPE_IQ2_XS:
  2989. case GGML_TYPE_IQ2_S:
  2990. case GGML_TYPE_IQ3_XXS:
  2991. case GGML_TYPE_IQ3_S:
  2992. case GGML_TYPE_IQ4_XS:
  2993. case GGML_TYPE_IQ4_NL:
  2994. break;
  2995. default:
  2996. return nullptr;
  2997. }
  2998. if (ctx->device->coopmat2) {
  2999. assert(src1_type == GGML_TYPE_F16);
  3000. return ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc;
  3001. }
  3002. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3003. }
  3004. 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) {
  3005. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3006. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3007. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3008. switch (a_type) {
  3009. case GGML_TYPE_F32:
  3010. case GGML_TYPE_F16:
  3011. case GGML_TYPE_Q4_0:
  3012. case GGML_TYPE_Q4_1:
  3013. case GGML_TYPE_Q5_0:
  3014. case GGML_TYPE_Q5_1:
  3015. case GGML_TYPE_Q8_0:
  3016. case GGML_TYPE_Q2_K:
  3017. case GGML_TYPE_Q3_K:
  3018. case GGML_TYPE_Q4_K:
  3019. case GGML_TYPE_Q5_K:
  3020. case GGML_TYPE_Q6_K:
  3021. case GGML_TYPE_IQ1_S:
  3022. case GGML_TYPE_IQ1_M:
  3023. case GGML_TYPE_IQ2_XXS:
  3024. case GGML_TYPE_IQ2_XS:
  3025. case GGML_TYPE_IQ2_S:
  3026. case GGML_TYPE_IQ3_XXS:
  3027. case GGML_TYPE_IQ3_S:
  3028. case GGML_TYPE_IQ4_XS:
  3029. case GGML_TYPE_IQ4_NL:
  3030. break;
  3031. default:
  3032. return nullptr;
  3033. }
  3034. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  3035. }
  3036. 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) {
  3037. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3038. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3039. return ctx->device->pipeline_matmul_id_f32;
  3040. }
  3041. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3042. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3043. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3044. }
  3045. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3046. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3047. }
  3048. } else {
  3049. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3050. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3051. }
  3052. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3053. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3054. }
  3055. }
  3056. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3057. switch (src0_type) {
  3058. case GGML_TYPE_Q4_0:
  3059. case GGML_TYPE_Q4_1:
  3060. case GGML_TYPE_Q5_0:
  3061. case GGML_TYPE_Q5_1:
  3062. case GGML_TYPE_Q8_0:
  3063. case GGML_TYPE_Q2_K:
  3064. case GGML_TYPE_Q3_K:
  3065. case GGML_TYPE_Q4_K:
  3066. case GGML_TYPE_Q5_K:
  3067. case GGML_TYPE_Q6_K:
  3068. case GGML_TYPE_IQ1_S:
  3069. case GGML_TYPE_IQ1_M:
  3070. case GGML_TYPE_IQ2_XXS:
  3071. case GGML_TYPE_IQ2_XS:
  3072. case GGML_TYPE_IQ2_S:
  3073. case GGML_TYPE_IQ3_XXS:
  3074. case GGML_TYPE_IQ3_S:
  3075. case GGML_TYPE_IQ4_XS:
  3076. case GGML_TYPE_IQ4_NL:
  3077. break;
  3078. default:
  3079. return nullptr;
  3080. }
  3081. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3082. }
  3083. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3084. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3085. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3086. switch (a_type) {
  3087. case GGML_TYPE_F32:
  3088. case GGML_TYPE_F16:
  3089. case GGML_TYPE_Q4_0:
  3090. case GGML_TYPE_Q4_1:
  3091. case GGML_TYPE_Q5_0:
  3092. case GGML_TYPE_Q5_1:
  3093. case GGML_TYPE_Q8_0:
  3094. case GGML_TYPE_Q2_K:
  3095. case GGML_TYPE_Q3_K:
  3096. case GGML_TYPE_Q4_K:
  3097. case GGML_TYPE_Q5_K:
  3098. case GGML_TYPE_Q6_K:
  3099. case GGML_TYPE_IQ1_S:
  3100. case GGML_TYPE_IQ1_M:
  3101. case GGML_TYPE_IQ2_XXS:
  3102. case GGML_TYPE_IQ2_XS:
  3103. case GGML_TYPE_IQ2_S:
  3104. case GGML_TYPE_IQ3_XXS:
  3105. case GGML_TYPE_IQ3_S:
  3106. case GGML_TYPE_IQ4_XS:
  3107. case GGML_TYPE_IQ4_NL:
  3108. break;
  3109. default:
  3110. return nullptr;
  3111. }
  3112. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3113. }
  3114. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3115. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3116. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3117. int best_i = -1;
  3118. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3119. int worst_i = -1;
  3120. size_t worst_size = 0; //largest unused buffer seen so far
  3121. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3122. vk_buffer &b = ctx->buffer_pool[i];
  3123. if (b != nullptr && b->size >= size && b->size < best_size) {
  3124. best_i = i;
  3125. best_size = b->size;
  3126. }
  3127. if (b != nullptr && b->size > worst_size) {
  3128. worst_i = i;
  3129. worst_size = b->size;
  3130. }
  3131. }
  3132. if(best_i != -1) {
  3133. //found the smallest buffer that fits our needs
  3134. vk_buffer b = ctx->buffer_pool[best_i];
  3135. ctx->buffer_pool[best_i].reset();
  3136. return b;
  3137. }
  3138. if(worst_i != -1) {
  3139. //no buffer that fits our needs, resize largest one to save memory
  3140. vk_buffer& b = ctx->buffer_pool[worst_i];
  3141. ggml_vk_destroy_buffer(b);
  3142. }
  3143. return ggml_vk_create_buffer_device(ctx->device, size);
  3144. }
  3145. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3146. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3147. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3148. vk_buffer& b = ctx->buffer_pool[i];
  3149. if (b == nullptr) {
  3150. b = buffer;
  3151. return;
  3152. }
  3153. }
  3154. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3155. ggml_vk_destroy_buffer(buffer);
  3156. }
  3157. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3158. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3159. // Try to find existing temp buffer with enough capacity
  3160. for (auto& buffer : ctx->gc.temp_buffers) {
  3161. if (buffer->size >= size) {
  3162. return buffer;
  3163. }
  3164. }
  3165. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3166. // Otherwise create new buffer
  3167. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3168. ctx->gc.temp_buffers.push_back(buf);
  3169. return buf;
  3170. }
  3171. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3172. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3173. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3174. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3175. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3176. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3177. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3178. size/1024.0/1024.0);
  3179. device->device.freeMemory(buf->device_memory);
  3180. device->device.destroyBuffer(buf->buffer);
  3181. return nullptr;
  3182. }
  3183. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3184. return buf->ptr;
  3185. }
  3186. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3187. if (ptr == nullptr) {
  3188. return;
  3189. }
  3190. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3191. vk_buffer buf;
  3192. size_t index;
  3193. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3194. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3195. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3196. if (ptr >= addr && ptr < endr) {
  3197. buf = std::get<2>(device->pinned_memory[i]);
  3198. index = i;
  3199. break;
  3200. }
  3201. }
  3202. if (buf == nullptr) {
  3203. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3204. return;
  3205. }
  3206. ggml_vk_destroy_buffer(buf);
  3207. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3208. }
  3209. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3210. buf = nullptr;
  3211. buf_offset = 0;
  3212. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3213. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3214. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3215. if (ptr >= addr && ptr < endr) {
  3216. buf = std::get<2>(device->pinned_memory[i]);
  3217. buf_offset = ((const uint8_t *)ptr) - addr;
  3218. break;
  3219. }
  3220. }
  3221. }
  3222. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_queue& q, bool one_time = true) {
  3223. vk_submission s;
  3224. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  3225. if (one_time) {
  3226. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3227. } else {
  3228. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3229. }
  3230. return s;
  3231. }
  3232. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context* ctx, vk_context& subctx, vk_pipeline& pipeline, std::initializer_list<vk::DescriptorBufferInfo> const& descriptor_buffer_infos, size_t push_constant_size, const void* push_constants, std::array<uint32_t, 3> elements) {
  3233. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3234. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3235. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3236. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3237. for (auto& buffer : descriptor_buffer_infos) {
  3238. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3239. }
  3240. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3241. GGML_ASSERT(pipeline->descriptor_set_idx < pipeline->descriptor_sets.size());
  3242. GGML_ASSERT(descriptor_buffer_infos.size() == pipeline->parameter_count);
  3243. vk::DescriptorSet& descriptor_set = pipeline->descriptor_sets[pipeline->descriptor_set_idx++];
  3244. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3245. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3246. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size, push_constants);
  3247. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3248. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3249. pipeline->layout,
  3250. 0,
  3251. { descriptor_set },
  3252. {});
  3253. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3254. }
  3255. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3256. s.buffer.end();
  3257. s.wait_semaphores = std::move(wait_semaphores);
  3258. s.signal_semaphores = std::move(signal_semaphores);
  3259. }
  3260. static void ggml_vk_ctx_end(vk_context& ctx) {
  3261. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3262. if (ctx->s == nullptr) {
  3263. return;
  3264. }
  3265. ctx->s->buffer.end();
  3266. ctx->s = nullptr;
  3267. }
  3268. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3269. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3270. if (subctx->s != nullptr) {
  3271. ggml_vk_ctx_end(subctx);
  3272. }
  3273. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->q) });
  3274. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3275. }
  3276. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3277. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3278. return CEIL_DIV(width, align) * align;
  3279. }
  3280. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3281. if (memcpys == nullptr) {
  3282. memcpy(dst, src, size);
  3283. } else {
  3284. memcpys->emplace_back(dst, src, size);
  3285. }
  3286. }
  3287. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3288. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3289. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3290. ggml_vk_destroy_buffer(device->sync_staging);
  3291. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3292. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3293. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3294. }
  3295. }
  3296. 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) {
  3297. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3298. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3299. // Buffer is already mapped
  3300. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3301. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3302. GGML_ABORT("fatal error");
  3303. }
  3304. // Check if src is pinned memory
  3305. vk_buffer buf = nullptr;
  3306. size_t buf_offset = 0;
  3307. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3308. const uint64_t ne0 = tensor->ne[0];
  3309. const uint64_t ne1 = tensor->ne[1];
  3310. const uint64_t ne2 = tensor->ne[2];
  3311. const uint64_t ne3 = tensor->ne[3];
  3312. const uint64_t nb0 = tensor->nb[0];
  3313. const uint64_t nb1 = tensor->nb[1];
  3314. const uint64_t nb2 = tensor->nb[2];
  3315. const uint64_t nb3 = tensor->nb[3];
  3316. const ggml_type type = tensor->type;
  3317. const uint64_t ts = ggml_type_size(type);
  3318. const uint64_t bs = ggml_blck_size(type);
  3319. const uint64_t dstnb0 = ts;
  3320. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3321. const uint64_t dstnb2 = dstnb1*ne1;
  3322. const uint64_t dstnb3 = dstnb2*ne2;
  3323. const uint64_t ne = ggml_nelements(tensor);
  3324. if (buf != nullptr) {
  3325. // Memory is pinned, use as staging buffer
  3326. std::vector<vk::BufferCopy> slices;
  3327. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3328. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3329. // Find longest contiguous slice
  3330. if (ne1*nb1 == dstnb2) {
  3331. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3332. } else {
  3333. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3334. if (ne0*nb0/bs == dstnb1) {
  3335. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3336. } else {
  3337. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3338. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3339. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3340. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3341. }
  3342. }
  3343. }
  3344. }
  3345. }
  3346. }
  3347. ggml_vk_sync_buffers(subctx);
  3348. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3349. return;
  3350. }
  3351. if (!sync_staging) {
  3352. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3353. }
  3354. // Staging buffer required
  3355. vk_buffer& staging = ctx->device->sync_staging;
  3356. const uint64_t copy_size = ts*ne/bs;
  3357. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3358. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3359. ggml_vk_sync_buffers(subctx);
  3360. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3361. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3362. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3363. // Find longest contiguous slice
  3364. if (ne1*nb1 == dstnb2) {
  3365. 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);
  3366. } else {
  3367. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3368. if (ne0*nb0/bs == dstnb1) {
  3369. 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);
  3370. } else {
  3371. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3372. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3373. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3374. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3375. }
  3376. }
  3377. }
  3378. }
  3379. }
  3380. }
  3381. }
  3382. 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) {
  3383. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3384. // Buffer is already mapped
  3385. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3386. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3387. GGML_ABORT("fatal error");
  3388. }
  3389. // Check if src is pinned memory
  3390. vk_buffer buf = nullptr;
  3391. size_t buf_offset = 0;
  3392. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3393. if (buf != nullptr) {
  3394. // Memory is pinned, use as staging buffer
  3395. std::vector<vk::BufferCopy> slices(1);
  3396. if (width == spitch) {
  3397. // Only do single write if stride is equal
  3398. slices[0].srcOffset = buf_offset;
  3399. slices[0].dstOffset = offset;
  3400. slices[0].size = width * height;
  3401. } else {
  3402. slices.resize(height);
  3403. for (size_t i = 0; i < height; i++) {
  3404. slices[i].srcOffset = buf_offset + i * spitch;
  3405. slices[i].dstOffset = offset + i * width;
  3406. slices[i].size = width;
  3407. }
  3408. }
  3409. ggml_vk_sync_buffers(subctx);
  3410. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3411. return;
  3412. }
  3413. VK_LOG_DEBUG("STAGING");
  3414. if (!sync_staging) {
  3415. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3416. }
  3417. // Staging buffer required
  3418. const size_t copy_size = width*height;
  3419. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3420. vk_buffer& staging_buffer = dst->device->sync_staging;
  3421. VkBufferCopy buf_copy = {
  3422. 0,
  3423. offset,
  3424. copy_size};
  3425. ggml_vk_sync_buffers(subctx);
  3426. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3427. if (width == spitch) {
  3428. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3429. } else {
  3430. for (size_t i = 0; i < height; i++) {
  3431. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3432. }
  3433. }
  3434. }
  3435. 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) {
  3436. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3437. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3438. }
  3439. 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) {
  3440. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3441. // Buffer is already mapped
  3442. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3443. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3444. for (size_t i = 0; i < height; i++) {
  3445. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3446. }
  3447. } else {
  3448. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3449. ggml_vk_ctx_begin(dst->device, subctx);
  3450. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3451. ggml_vk_ctx_end(subctx);
  3452. for (auto& cpy : subctx->in_memcpys) {
  3453. memcpy(cpy.dst, cpy.src, cpy.n);
  3454. }
  3455. ggml_vk_submit(subctx, dst->device->fence);
  3456. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3457. dst->device->device.resetFences({ dst->device->fence });
  3458. }
  3459. }
  3460. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3461. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3462. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3463. }
  3464. 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) {
  3465. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3466. GGML_ASSERT(width > 0);
  3467. GGML_ASSERT(height > 0);
  3468. GGML_ASSERT(src != nullptr);
  3469. // TODO: staging_offset is not used
  3470. // Check if dst is pinned memory
  3471. vk_buffer buf = nullptr;
  3472. size_t buf_offset = 0;
  3473. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3474. std::vector<vk::BufferCopy> slices(1);
  3475. if (width == spitch && width == dpitch) {
  3476. // Only do single write if stride is equal
  3477. slices[0].srcOffset = offset;
  3478. slices[0].dstOffset = buf_offset;
  3479. slices[0].size = width * height;
  3480. } else {
  3481. slices.resize(height);
  3482. for (size_t i = 0; i < height; i++) {
  3483. slices[i].srcOffset = offset + i * spitch;
  3484. slices[i].dstOffset = buf_offset + i * dpitch;
  3485. slices[i].size = width;
  3486. }
  3487. }
  3488. if (buf != nullptr) {
  3489. // Memory is pinned, use as staging buffer
  3490. ggml_vk_sync_buffers(subctx);
  3491. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3492. return;
  3493. }
  3494. VK_LOG_DEBUG("STAGING");
  3495. if (!sync_staging) {
  3496. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3497. }
  3498. // Fall back to staging buffer
  3499. const size_t copy_size = dpitch * height;
  3500. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  3501. vk_buffer& staging_buffer = src->device->sync_staging;
  3502. ggml_vk_sync_buffers(subctx);
  3503. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  3504. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  3505. }
  3506. 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) {
  3507. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  3508. }
  3509. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  3510. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  3511. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  3512. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  3513. // the HW device to host copy path.
  3514. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  3515. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3516. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  3517. } else {
  3518. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3519. ggml_vk_ctx_begin(src->device, subctx);
  3520. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  3521. ggml_vk_ctx_end(subctx);
  3522. ggml_vk_submit(subctx, src->device->fence);
  3523. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  3524. src->device->device.resetFences({ src->device->fence });
  3525. for (auto& cpy : subctx->out_memcpys) {
  3526. memcpy(cpy.dst, cpy.src, cpy.n);
  3527. }
  3528. }
  3529. }
  3530. 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) {
  3531. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  3532. // Make sure both buffers are on same device
  3533. GGML_ASSERT(src->device == dst->device);
  3534. VkBufferCopy bc{ src_offset, dst_offset, size };
  3535. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  3536. }
  3537. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  3538. if (src->device == dst->device) {
  3539. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  3540. // Copy within the device
  3541. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3542. ggml_vk_ctx_begin(src->device, subctx);
  3543. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  3544. ggml_vk_ctx_end(subctx);
  3545. ggml_vk_submit(subctx, src->device->fence);
  3546. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  3547. src->device->device.resetFences({ src->device->fence });
  3548. } else {
  3549. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  3550. // Copy device to device
  3551. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  3552. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  3553. // Copy to src staging buffer
  3554. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  3555. // memcpy to dst staging buffer
  3556. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  3557. // Copy to dst buffer
  3558. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  3559. }
  3560. }
  3561. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3562. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  3563. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3564. }
  3565. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3566. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  3567. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3568. ggml_vk_ctx_begin(dst->device, subctx);
  3569. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3570. ggml_vk_ctx_end(subctx);
  3571. ggml_vk_submit(subctx, dst->device->fence);
  3572. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  3573. dst->device->device.resetFences({ dst->device->fence });
  3574. }
  3575. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  3576. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  3577. uint32_t split_k = 1;
  3578. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  3579. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  3580. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  3581. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  3582. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  3583. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  3584. // Clamp to 2 or 4
  3585. split_k = std::min(split_k, 4u);
  3586. if (split_k == 3) {
  3587. split_k = 2;
  3588. }
  3589. if (ctx->device->coopmat2) {
  3590. // coopmat2 shader expects splits to be aligned to 256
  3591. while (split_k > 1 && ((k / split_k) % 256) != 0) {
  3592. split_k /= 2;
  3593. }
  3594. }
  3595. }
  3596. }
  3597. return split_k;
  3598. }
  3599. 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) {
  3600. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  3601. if (ctx->device->coopmat2) {
  3602. // Use large shader when the N dimension is greater than the medium shader's tile size
  3603. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3604. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  3605. return aligned ? mmp->a_l : mmp->l;
  3606. }
  3607. // Use medium shader when the N dimension is greater than the small shader's tile size
  3608. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3609. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  3610. return aligned ? mmp->a_m : mmp->m;
  3611. }
  3612. return aligned ? mmp->a_s : mmp->s;
  3613. }
  3614. 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])) {
  3615. return aligned ? mmp->a_s : mmp->s;
  3616. }
  3617. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  3618. return aligned ? mmp->a_m : mmp->m;
  3619. }
  3620. return aligned ? mmp->a_l : mmp->l;
  3621. }
  3622. 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) {
  3623. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  3624. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  3625. }
  3626. static void ggml_vk_matmul(
  3627. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3628. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  3629. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3630. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3631. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  3632. uint32_t padded_n) {
  3633. 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 << ")");
  3634. ggml_vk_sync_buffers(subctx);
  3635. if (split_k == 1) {
  3636. 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 };
  3637. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  3638. return;
  3639. }
  3640. GGML_ASSERT(batch_stride_d == m * n);
  3641. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, padded_n };
  3642. // Make sure enough workgroups get assigned for split k to work
  3643. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, split_k_buffer }, sizeof(vk_mat_mat_push_constants), &pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  3644. ggml_vk_sync_buffers(subctx);
  3645. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  3646. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2.size() * sizeof(uint32_t), pc2.data(), { m * n * batch, 1, 1 });
  3647. }
  3648. 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) {
  3649. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  3650. if (ctx->device->coopmat2) {
  3651. // Use large shader when the N dimension is greater than the medium shader's tile size
  3652. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3653. 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])) {
  3654. return aligned ? mmp->a_l : mmp->l;
  3655. }
  3656. // Use medium shader when the N dimension is greater than the small shader's tile size
  3657. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3658. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  3659. return aligned ? mmp->a_m : mmp->m;
  3660. }
  3661. return aligned ? mmp->a_s : mmp->s;
  3662. }
  3663. 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])) {
  3664. return aligned ? mmp->a_s : mmp->s;
  3665. }
  3666. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  3667. return aligned ? mmp->a_m : mmp->m;
  3668. }
  3669. return aligned ? mmp->a_l : mmp->l;
  3670. }
  3671. 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) {
  3672. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  3673. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  3674. }
  3675. static void ggml_vk_matmul_id(
  3676. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3677. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  3678. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3679. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3680. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  3681. uint32_t padded_n) {
  3682. 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 << "), " <<
  3683. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  3684. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  3685. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  3686. ggml_vk_sync_buffers(subctx);
  3687. 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,
  3688. nei0, nei1, nbi1, ne11, padded_n };
  3689. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, sizeof(vk_mat_mat_id_push_constants), &pc, { m, nei1, n_as });
  3690. }
  3691. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  3692. return
  3693. tensor->nb[0] == ggml_type_size(tensor->type) &&
  3694. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  3695. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  3696. }
  3697. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  3698. // Choose "contiguous copy" shader if src/dst are contiguous
  3699. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  3700. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  3701. if (contig) {
  3702. return ctx->device->pipeline_contig_cpy_f32_f32;
  3703. } else {
  3704. return ctx->device->pipeline_cpy_f32_f32;
  3705. }
  3706. }
  3707. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  3708. if (contig) {
  3709. return ctx->device->pipeline_contig_cpy_f32_f16;
  3710. } else {
  3711. return ctx->device->pipeline_cpy_f32_f16;
  3712. }
  3713. }
  3714. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  3715. if (contig) {
  3716. return ctx->device->pipeline_contig_cpy_f16_f16;
  3717. } else {
  3718. return ctx->device->pipeline_cpy_f16_f16;
  3719. }
  3720. }
  3721. if (src->type == GGML_TYPE_F32) {
  3722. switch (to) {
  3723. case GGML_TYPE_Q4_0:
  3724. case GGML_TYPE_Q4_1:
  3725. case GGML_TYPE_Q5_0:
  3726. case GGML_TYPE_Q5_1:
  3727. case GGML_TYPE_Q8_0:
  3728. case GGML_TYPE_IQ4_NL:
  3729. return ctx->device->pipeline_cpy_f32_quant[to];
  3730. default:
  3731. break;
  3732. }
  3733. }
  3734. if (to == GGML_TYPE_F32) {
  3735. switch (src->type) {
  3736. case GGML_TYPE_Q4_0:
  3737. case GGML_TYPE_Q4_1:
  3738. case GGML_TYPE_Q5_0:
  3739. case GGML_TYPE_Q5_1:
  3740. case GGML_TYPE_Q8_0:
  3741. case GGML_TYPE_IQ4_NL:
  3742. return ctx->device->pipeline_cpy_quant_f32[src->type];
  3743. default:
  3744. break;
  3745. }
  3746. }
  3747. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  3748. GGML_ABORT("fatal error");
  3749. }
  3750. 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) {
  3751. 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] << "), ";
  3752. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  3753. const int tensor_type_size = ggml_type_size(tensor->type);
  3754. const uint32_t ne = ggml_nelements(tensor);
  3755. std::array<uint32_t, 3> elements;
  3756. if (ne > 262144) {
  3757. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  3758. } else if (ne > 512) {
  3759. elements = { 512, CEIL_DIV(ne, 512), 1 };
  3760. } else {
  3761. elements = { ne, 1, 1 };
  3762. }
  3763. vk_op_unary_push_constants pc = {
  3764. (uint32_t)ne,
  3765. (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,
  3766. (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]),
  3767. 0,
  3768. 0.0f, 0.0f,
  3769. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3770. };
  3771. init_pushconst_fastdiv(pc);
  3772. ggml_vk_sync_buffers(subctx);
  3773. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(vk_op_unary_push_constants), &pc, elements);
  3774. }
  3775. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  3776. switch(type) {
  3777. case GGML_TYPE_Q8_1:
  3778. return ctx->device->pipeline_quantize_q8_1;
  3779. default:
  3780. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  3781. GGML_ABORT("fatal error");
  3782. }
  3783. }
  3784. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  3785. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  3786. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  3787. ggml_vk_sync_buffers(subctx);
  3788. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(uint32_t), &ne, { ne, 1, 1 });
  3789. }
  3790. 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) {
  3791. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3792. 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];
  3793. 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];
  3794. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3795. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3796. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3797. const uint64_t ne00 = src0->ne[0];
  3798. const uint64_t ne01 = src0->ne[1];
  3799. const uint64_t ne02 = src0->ne[2];
  3800. const uint64_t ne03 = src0->ne[3];
  3801. const uint64_t ne10 = src1->ne[0];
  3802. const uint64_t ne11 = src1->ne[1];
  3803. const uint64_t ne12 = src1->ne[2];
  3804. const uint64_t ne13 = src1->ne[3];
  3805. const uint64_t ne20 = dst->ne[0];
  3806. const uint64_t ne21 = dst->ne[1];
  3807. const uint64_t r2 = ne12 / ne02;
  3808. const uint64_t r3 = ne13 / ne03;
  3809. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3810. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3811. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3812. vk_buffer d_Qx = nullptr;
  3813. size_t qx_buf_offset = 0;
  3814. vk_buffer d_Qy = nullptr;
  3815. size_t qy_buf_offset = 0;
  3816. bool src0_uma = false;
  3817. bool src1_uma = false;
  3818. if (ctx->device->uma) {
  3819. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3820. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3821. src0_uma = d_Qx != nullptr;
  3822. src1_uma = d_Qy != nullptr;
  3823. }
  3824. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  3825. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  3826. !ggml_vk_dim01_contiguous(src0);
  3827. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  3828. !ggml_vk_dim01_contiguous(src1);
  3829. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  3830. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  3831. // Check for mmq first
  3832. 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;
  3833. if (mmp == nullptr) {
  3834. // Fall back to f16 dequant mul mat
  3835. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  3836. quantize_y = false;
  3837. }
  3838. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  3839. const bool qy_needs_dequant = !quantize_y && ((src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig);
  3840. if (qx_needs_dequant) {
  3841. // Fall back to dequant + f16 mulmat
  3842. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  3843. }
  3844. // Not implemented
  3845. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3846. 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 ? GGML_TYPE_F16 : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type)));
  3847. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  3848. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type));
  3849. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  3850. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  3851. const int x_ne = ne01 * ne00;
  3852. const int y_ne = padded_n * ne10;
  3853. const int d_ne = ne11 * ne01;
  3854. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  3855. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  3856. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3857. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  3858. 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);
  3859. const uint64_t d_sz = sizeof(float) * d_ne;
  3860. vk_pipeline to_fp16_vk_0 = nullptr;
  3861. vk_pipeline to_fp16_vk_1 = nullptr;
  3862. vk_pipeline to_q8_1 = nullptr;
  3863. if (x_non_contig) {
  3864. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  3865. } else {
  3866. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  3867. }
  3868. if (y_non_contig) {
  3869. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  3870. } else {
  3871. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3872. }
  3873. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3874. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3875. if (quantize_y) {
  3876. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  3877. }
  3878. if (dryrun) {
  3879. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3880. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3881. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  3882. if (
  3883. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3884. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  3885. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  3886. GGML_ABORT("Requested preallocation size is too large");
  3887. }
  3888. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3889. ctx->prealloc_size_x = x_sz_upd;
  3890. }
  3891. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  3892. ctx->prealloc_size_y = y_sz_upd;
  3893. }
  3894. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  3895. ctx->prealloc_size_split_k = split_k_size;
  3896. }
  3897. // Request descriptor sets
  3898. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  3899. if (qx_needs_dequant) {
  3900. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  3901. }
  3902. if (qy_needs_dequant) {
  3903. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  3904. }
  3905. if (quantize_y) {
  3906. ggml_pipeline_request_descriptor_sets(ctx->device, to_q8_1, 1);
  3907. }
  3908. if (split_k > 1) {
  3909. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, 1);
  3910. }
  3911. return;
  3912. }
  3913. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3914. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3915. GGML_ASSERT(d_D != nullptr);
  3916. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  3917. vk_buffer d_X;
  3918. uint64_t x_buf_offset = 0;
  3919. vk_buffer d_Y;
  3920. uint64_t y_buf_offset = 0;
  3921. if (!src0_uma) {
  3922. d_Qx = src0_buf_ctx->dev_buffer;
  3923. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3924. GGML_ASSERT(d_Qx != nullptr);
  3925. }
  3926. if (!src1_uma) {
  3927. d_Qy = src1_buf_ctx->dev_buffer;
  3928. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3929. GGML_ASSERT(d_Qy != nullptr);
  3930. }
  3931. if (qx_needs_dequant) {
  3932. d_X = ctx->prealloc_x;
  3933. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  3934. } else {
  3935. d_X = d_Qx;
  3936. x_buf_offset = qx_buf_offset;
  3937. GGML_ASSERT(qx_sz == x_sz);
  3938. }
  3939. if (qy_needs_dequant) {
  3940. d_Y = ctx->prealloc_y;
  3941. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  3942. } else if (quantize_y) {
  3943. d_Y = ctx->prealloc_y;
  3944. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  3945. } else {
  3946. d_Y = d_Qy;
  3947. y_buf_offset = qy_buf_offset;
  3948. GGML_ASSERT(qy_sz == y_sz);
  3949. }
  3950. if (x_non_contig) {
  3951. 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 });
  3952. } else if (qx_needs_dequant) {
  3953. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  3954. ggml_vk_sync_buffers(subctx);
  3955. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  3956. }
  3957. if (y_non_contig) {
  3958. 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 });
  3959. }
  3960. if (quantize_y) {
  3961. 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);
  3962. }
  3963. uint32_t stride_batch_x = ne00*ne01;
  3964. uint32_t stride_batch_y = ne10*ne11;
  3965. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3966. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3967. }
  3968. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  3969. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3970. }
  3971. // compute
  3972. ggml_vk_matmul(
  3973. ctx, subctx, pipeline,
  3974. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  3975. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  3976. ne01, ne11, ne10,
  3977. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  3978. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  3979. ); // NOLINT
  3980. }
  3981. 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) {
  3982. 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];
  3983. 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];
  3984. 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];
  3985. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  3986. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3987. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3988. const uint64_t ne00 = src0->ne[0];
  3989. const uint64_t ne01 = src0->ne[1];
  3990. const uint64_t ne02 = src0->ne[2];
  3991. const uint64_t ne03 = src0->ne[3];
  3992. const uint64_t ne10 = src1->ne[0];
  3993. const uint64_t ne11 = src1->ne[1];
  3994. const uint64_t ne12 = src1->ne[2];
  3995. const uint64_t ne13 = src1->ne[3];
  3996. const uint64_t ne20 = dst->ne[0];
  3997. const uint64_t ne21 = dst->ne[1];
  3998. const uint64_t ne22 = dst->ne[2];
  3999. const uint64_t ne23 = dst->ne[3];
  4000. const uint64_t r2 = ne12 / ne02;
  4001. const uint64_t r3 = ne13 / ne03;
  4002. // batch_n indicates that we need to compute a few vector results, and this assumes
  4003. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4004. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4005. bool batch_n = ne11 > 1;
  4006. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4007. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4008. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4009. vk_buffer d_Qx = nullptr;
  4010. size_t qx_buf_offset = 0;
  4011. vk_buffer d_Qy = nullptr;
  4012. size_t qy_buf_offset = 0;
  4013. bool src0_uma = false;
  4014. bool src1_uma = false;
  4015. if (ctx->device->uma) {
  4016. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4017. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4018. src0_uma = d_Qx != nullptr;
  4019. src1_uma = d_Qy != nullptr;
  4020. }
  4021. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4022. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4023. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4024. const bool qx_needs_dequant = x_non_contig;
  4025. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4026. // Not implemented
  4027. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4028. const uint64_t x_ne = ne01 * ne00;
  4029. const uint64_t y_ne = ne11 * ne10;
  4030. const uint64_t d_ne = ne11 * ne01;
  4031. 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);
  4032. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4033. 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;
  4034. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4035. const uint64_t d_sz = sizeof(float) * d_ne;
  4036. vk_pipeline to_fp16_vk_0 = nullptr;
  4037. vk_pipeline to_fp16_vk_1 = nullptr;
  4038. if (x_non_contig) {
  4039. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4040. }
  4041. if (y_non_contig) {
  4042. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4043. } else {
  4044. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4045. }
  4046. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  4047. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4048. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4049. GGML_ASSERT(dmmv != nullptr);
  4050. if (dryrun) {
  4051. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4052. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4053. if (
  4054. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4055. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4056. GGML_ABORT("Requested preallocation size is too large");
  4057. }
  4058. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4059. ctx->prealloc_size_x = x_sz_upd;
  4060. }
  4061. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4062. ctx->prealloc_size_y = y_sz_upd;
  4063. }
  4064. // Request descriptor sets
  4065. if (qx_needs_dequant) {
  4066. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4067. }
  4068. if (qy_needs_dequant) {
  4069. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4070. }
  4071. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  4072. return;
  4073. }
  4074. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4075. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4076. GGML_ASSERT(d_D != nullptr);
  4077. vk_buffer d_X;
  4078. uint64_t x_buf_offset = 0;
  4079. vk_buffer d_Y;
  4080. uint64_t y_buf_offset = 0;
  4081. if(!src0_uma) {
  4082. d_Qx = src0_buf_ctx->dev_buffer;
  4083. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4084. GGML_ASSERT(d_Qx != nullptr);
  4085. }
  4086. if(!src1_uma) {
  4087. d_Qy = src1_buf_ctx->dev_buffer;
  4088. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4089. GGML_ASSERT(d_Qy != nullptr);
  4090. }
  4091. if (qx_needs_dequant) {
  4092. d_X = ctx->prealloc_x;
  4093. } else {
  4094. d_X = d_Qx;
  4095. x_buf_offset = qx_buf_offset;
  4096. GGML_ASSERT(qx_sz == x_sz);
  4097. }
  4098. if (qy_needs_dequant) {
  4099. d_Y = ctx->prealloc_y;
  4100. } else {
  4101. d_Y = d_Qy;
  4102. y_buf_offset = qy_buf_offset;
  4103. GGML_ASSERT(qy_sz == y_sz);
  4104. }
  4105. if (x_non_contig) {
  4106. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4107. 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 });
  4108. }
  4109. if (y_non_contig) {
  4110. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4111. 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 });
  4112. }
  4113. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4114. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4115. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4116. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4117. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4118. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4119. }
  4120. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4121. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4122. }
  4123. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4124. uint32_t groups_x = ne01;
  4125. uint32_t groups_z = 1;
  4126. if (ne01 > max_groups_x) {
  4127. groups_z = 64;
  4128. groups_x = CEIL_DIV(groups_x, groups_z);
  4129. }
  4130. // compute
  4131. const vk_mat_vec_push_constants pc = {
  4132. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4133. stride_batch_x, stride_batch_y, stride_batch_d,
  4134. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4135. };
  4136. ggml_vk_sync_buffers(subctx);
  4137. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4138. { 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} },
  4139. sizeof(vk_mat_vec_push_constants), &pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4140. }
  4141. 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) {
  4142. 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];
  4143. 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];
  4144. 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];
  4145. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4146. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4147. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4148. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4149. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4150. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4151. const uint64_t ne00 = src0->ne[0];
  4152. const uint64_t ne01 = src0->ne[1];
  4153. const uint64_t ne02 = src0->ne[2];
  4154. // const uint64_t ne03 = src0->ne[3];
  4155. const uint64_t ne10 = src1->ne[0];
  4156. const uint64_t ne11 = src1->ne[1];
  4157. const uint64_t ne12 = src1->ne[2];
  4158. // const uint64_t ne13 = src1->ne[3];
  4159. GGML_ASSERT(ne11 == 1);
  4160. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4161. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4162. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4163. vk_buffer d_Qy = nullptr;
  4164. size_t qy_buf_offset = 0;
  4165. bool src1_uma = false;
  4166. if (ctx->device->uma) {
  4167. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4168. src1_uma = d_Qy != nullptr;
  4169. }
  4170. const uint64_t x_ne = ne00 * ne01 * ne02;
  4171. const uint64_t y_ne = ne10 * ne11 * ne12;
  4172. const uint64_t d_ne = ne01 * ne11 * ne12;
  4173. 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);
  4174. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4175. const uint64_t d_sz = sizeof(float) * d_ne;
  4176. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  4177. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  4178. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  4179. gqa_ratio = 1;
  4180. }
  4181. if (dryrun) {
  4182. // Request descriptor sets
  4183. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  4184. return;
  4185. }
  4186. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4187. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4188. GGML_ASSERT(d_D != nullptr);
  4189. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4190. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4191. GGML_ASSERT(d_Qx != nullptr);
  4192. if (!src1_uma) {
  4193. d_Qy = src1_buf_ctx->dev_buffer;
  4194. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4195. GGML_ASSERT(d_Qx != nullptr);
  4196. }
  4197. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4198. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4199. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4200. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4201. // compute
  4202. 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)) };
  4203. uint32_t workgroups_z = (uint32_t)ne12;
  4204. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  4205. if (gqa_ratio > 1) {
  4206. workgroups_z /= gqa_ratio;
  4207. }
  4208. ggml_vk_sync_buffers(subctx);
  4209. 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 } }, 6 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, workgroups_z });
  4210. }
  4211. 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) {
  4212. 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];
  4213. 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];
  4214. 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];
  4215. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4216. GGML_ASSERT(!ggml_is_transposed(src0));
  4217. GGML_ASSERT(!ggml_is_transposed(src1));
  4218. GGML_ASSERT(!ggml_is_permuted(src0));
  4219. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4220. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4221. const uint64_t ne00 = src0->ne[0];
  4222. const uint64_t ne01 = src0->ne[1];
  4223. const uint64_t ne02 = src0->ne[2];
  4224. // const uint64_t ne03 = src0->ne[3];
  4225. const uint64_t nb01 = src0->nb[1];
  4226. const uint64_t nb02 = src0->nb[2];
  4227. // const uint64_t ne10 = src1->ne[0];
  4228. const uint64_t ne11 = src1->ne[1];
  4229. const uint64_t ne12 = src1->ne[2];
  4230. // const uint64_t ne13 = src1->ne[3];
  4231. GGML_ASSERT(ne11 == 1);
  4232. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4233. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4234. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4235. vk_buffer d_Qy = nullptr;
  4236. size_t qy_buf_offset = 0;
  4237. bool src1_uma = false;
  4238. if (ctx->device->uma) {
  4239. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4240. src1_uma = d_Qy != nullptr;
  4241. }
  4242. const uint64_t d_ne = ne01 * ne11 * ne12;
  4243. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4244. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4245. const uint64_t qx_sz = ggml_nbytes(src0);
  4246. const uint64_t qy_sz = ggml_nbytes(src1);
  4247. const uint64_t d_sz = sizeof(float) * d_ne;
  4248. if (dryrun) {
  4249. // Request descriptor sets
  4250. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4251. return;
  4252. }
  4253. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4254. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4255. GGML_ASSERT(d_D != nullptr);
  4256. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4257. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4258. GGML_ASSERT(d_Qx != nullptr);
  4259. if (!src1_uma) {
  4260. d_Qy = src1_buf_ctx->dev_buffer;
  4261. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4262. GGML_ASSERT(d_Qx != nullptr);
  4263. }
  4264. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4265. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4266. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4267. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4268. // compute
  4269. const std::array<uint32_t, 7> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, (uint32_t)(ne12 / ne02), (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)) };
  4270. ggml_vk_sync_buffers(subctx);
  4271. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4272. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, 7 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  4273. }
  4274. 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) {
  4275. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4276. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4277. // detect 0213 permutation, and batch size of 1
  4278. src0->nb[0] <= src0->nb[2] &&
  4279. src0->nb[2] <= src0->nb[1] &&
  4280. src0->nb[1] <= src0->nb[3] &&
  4281. src1->nb[0] <= src1->nb[2] &&
  4282. src1->nb[2] <= src1->nb[1] &&
  4283. src1->nb[1] <= src1->nb[3] &&
  4284. src0->ne[3] == 1 &&
  4285. src1->ne[3] == 1) {
  4286. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4287. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4288. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4289. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4290. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4291. // when ne12 and ne13 are one.
  4292. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4293. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4294. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4295. } else {
  4296. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4297. }
  4298. }
  4299. 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) {
  4300. 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];
  4301. 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];
  4302. 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];
  4303. 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] << "),)");
  4304. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4305. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4306. const uint64_t ne00 = src0->ne[0];
  4307. const uint64_t ne01 = src0->ne[1];
  4308. const uint64_t ne02 = src0->ne[2];
  4309. const uint64_t ne03 = src0->ne[3];
  4310. const uint64_t ne10 = src1->ne[0];
  4311. const uint64_t ne11 = src1->ne[1];
  4312. const uint64_t ne12 = src1->ne[2];
  4313. const uint64_t ne13 = src1->ne[3];
  4314. const uint64_t nei0 = ids->ne[0];
  4315. const uint64_t nei1 = ids->ne[1];
  4316. GGML_ASSERT(nei0 * nei1 <= 3072);
  4317. const uint32_t nbi1 = ids->nb[1];
  4318. const uint32_t nbi2 = ids->nb[2];
  4319. const uint64_t ne20 = dst->ne[0];
  4320. const uint64_t ne21 = dst->ne[1];
  4321. const uint64_t ne22 = dst->ne[2];
  4322. const uint64_t ne23 = dst->ne[3];
  4323. const uint64_t n_as = ne02;
  4324. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4325. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4326. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4327. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4328. vk_buffer d_Qx = nullptr;
  4329. size_t qx_buf_offset = 0;
  4330. vk_buffer d_Qy = nullptr;
  4331. size_t qy_buf_offset = 0;
  4332. vk_buffer d_ids = nullptr;
  4333. size_t ids_buf_offset = 0;
  4334. bool src0_uma = false;
  4335. bool src1_uma = false;
  4336. bool ids_uma = false;
  4337. if (ctx->device->uma) {
  4338. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4339. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4340. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4341. src0_uma = d_Qx != nullptr;
  4342. src1_uma = d_Qy != nullptr;
  4343. ids_uma = d_ids != nullptr;
  4344. }
  4345. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4346. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4347. !ggml_vk_dim01_contiguous(src0);
  4348. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4349. !ggml_vk_dim01_contiguous(src1);
  4350. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4351. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  4352. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4353. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  4354. if (qx_needs_dequant) {
  4355. // Fall back to dequant + f16 mulmat
  4356. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  4357. }
  4358. // Not implemented
  4359. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4360. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? GGML_TYPE_F16 : src0->type));
  4361. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4362. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type);
  4363. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4364. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4365. const uint64_t x_ne = ne01 * ne00;
  4366. const uint64_t y_ne = padded_n * ne10;
  4367. const uint64_t d_ne = ne21 * ne20;
  4368. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4369. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4370. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4371. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4372. const uint64_t ids_sz = nbi2;
  4373. const uint64_t d_sz = sizeof(float) * d_ne;
  4374. vk_pipeline to_fp16_vk_0 = nullptr;
  4375. vk_pipeline to_fp16_vk_1 = nullptr;
  4376. if (x_non_contig) {
  4377. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  4378. } else {
  4379. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4380. }
  4381. if (y_non_contig) {
  4382. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  4383. } else {
  4384. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4385. }
  4386. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4387. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4388. if (dryrun) {
  4389. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4390. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4391. if (
  4392. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4393. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4394. GGML_ABORT("Requested preallocation size is too large");
  4395. }
  4396. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4397. ctx->prealloc_size_x = x_sz_upd;
  4398. }
  4399. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4400. ctx->prealloc_size_y = y_sz_upd;
  4401. }
  4402. // Request descriptor sets
  4403. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4404. if (qx_needs_dequant) {
  4405. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4406. }
  4407. if (qy_needs_dequant) {
  4408. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4409. }
  4410. return;
  4411. }
  4412. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4413. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4414. GGML_ASSERT(d_D != nullptr);
  4415. vk_buffer d_X;
  4416. uint64_t x_buf_offset = 0;
  4417. vk_buffer d_Y;
  4418. uint64_t y_buf_offset = 0;
  4419. if (!src0_uma) {
  4420. d_Qx = src0_buf_ctx->dev_buffer;
  4421. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4422. GGML_ASSERT(d_Qx != nullptr);
  4423. }
  4424. if (!src1_uma) {
  4425. d_Qy = src1_buf_ctx->dev_buffer;
  4426. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4427. GGML_ASSERT(d_Qy != nullptr);
  4428. }
  4429. if (!ids_uma) {
  4430. d_ids = ids_buf_ctx->dev_buffer;
  4431. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4432. GGML_ASSERT(d_ids != nullptr);
  4433. }
  4434. if (qx_needs_dequant) {
  4435. d_X = ctx->prealloc_x;
  4436. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4437. } else {
  4438. d_X = d_Qx;
  4439. x_buf_offset = qx_buf_offset;
  4440. GGML_ASSERT(qx_sz == x_sz);
  4441. }
  4442. if (qy_needs_dequant) {
  4443. d_Y = ctx->prealloc_y;
  4444. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4445. } else {
  4446. d_Y = d_Qy;
  4447. y_buf_offset = qy_buf_offset;
  4448. GGML_ASSERT(qy_sz == y_sz);
  4449. }
  4450. if (x_non_contig) {
  4451. 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 });
  4452. } else if (qx_needs_dequant) {
  4453. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4454. ggml_vk_sync_buffers(subctx);
  4455. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  4456. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  4457. }
  4458. if (y_non_contig) {
  4459. 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 });
  4460. }
  4461. uint32_t stride_batch_x = ne00*ne01;
  4462. uint32_t stride_batch_y = ne10*ne11;
  4463. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4464. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4465. }
  4466. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4467. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4468. }
  4469. // compute
  4470. ggml_vk_matmul_id(
  4471. ctx, subctx, pipeline,
  4472. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4473. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  4474. ne01, ne21, ne10, ne10, ne10, ne01,
  4475. stride_batch_x, stride_batch_y, ne20*ne21,
  4476. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  4477. ); // NOLINT
  4478. }
  4479. 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) {
  4480. 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];
  4481. 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];
  4482. 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];
  4483. 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];
  4484. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4485. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  4486. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4487. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4488. const uint64_t ne00 = src0->ne[0];
  4489. const uint64_t ne01 = src0->ne[1];
  4490. const uint64_t ne02 = src0->ne[2];
  4491. const uint64_t ne03 = src0->ne[3];
  4492. const uint64_t ne10 = src1->ne[0];
  4493. const uint64_t ne11 = src1->ne[1];
  4494. const uint64_t ne12 = src1->ne[2];
  4495. const uint64_t ne13 = src1->ne[3];
  4496. const uint64_t nei0 = ids->ne[0];
  4497. const uint64_t nei1 = ids->ne[1];
  4498. const uint64_t nbi2 = ids->nb[2];
  4499. GGML_ASSERT(nei1 == 1);
  4500. const uint64_t ne20 = dst->ne[0];
  4501. const uint64_t ne21 = dst->ne[1];
  4502. const uint64_t ne22 = dst->ne[2];
  4503. const uint64_t ne23 = dst->ne[3];
  4504. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4505. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4506. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4507. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4508. vk_buffer d_Qx = nullptr;
  4509. size_t qx_buf_offset = 0;
  4510. vk_buffer d_Qy = nullptr;
  4511. size_t qy_buf_offset = 0;
  4512. vk_buffer d_ids = nullptr;
  4513. size_t ids_buf_offset = 0;
  4514. bool src0_uma = false;
  4515. bool src1_uma = false;
  4516. bool ids_uma = false;
  4517. if (ctx->device->uma) {
  4518. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4519. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4520. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4521. src0_uma = d_Qx != nullptr;
  4522. src1_uma = d_Qy != nullptr;
  4523. ids_uma = d_ids != nullptr;
  4524. }
  4525. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4526. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4527. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4528. const bool qx_needs_dequant = x_non_contig;
  4529. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4530. // Not implemented
  4531. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4532. const uint64_t x_ne = ne01 * ne00;
  4533. const uint64_t y_ne = ne11 * ne10;
  4534. const uint64_t d_ne = ne21 * ne20;
  4535. 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);
  4536. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4537. 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;
  4538. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4539. const uint64_t ids_sz = nbi2;
  4540. const uint64_t d_sz = sizeof(float) * d_ne;
  4541. vk_pipeline to_fp16_vk_0 = nullptr;
  4542. vk_pipeline to_fp16_vk_1 = nullptr;
  4543. if (x_non_contig) {
  4544. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4545. }
  4546. if (y_non_contig) {
  4547. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4548. } else {
  4549. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4550. }
  4551. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  4552. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4553. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4554. GGML_ASSERT(dmmv != nullptr);
  4555. if (dryrun) {
  4556. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4557. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4558. if (
  4559. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4560. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4561. GGML_ABORT("Requested preallocation size is too large");
  4562. }
  4563. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4564. ctx->prealloc_size_x = x_sz_upd;
  4565. }
  4566. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4567. ctx->prealloc_size_y = y_sz_upd;
  4568. }
  4569. // Request descriptor sets
  4570. if (qx_needs_dequant) {
  4571. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4572. }
  4573. if (qy_needs_dequant) {
  4574. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4575. }
  4576. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  4577. return;
  4578. }
  4579. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4580. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4581. GGML_ASSERT(d_D != nullptr);
  4582. vk_buffer d_X;
  4583. uint64_t x_buf_offset = 0;
  4584. vk_buffer d_Y;
  4585. uint64_t y_buf_offset = 0;
  4586. if(!src0_uma) {
  4587. d_Qx = src0_buf_ctx->dev_buffer;
  4588. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4589. GGML_ASSERT(d_Qx != nullptr);
  4590. }
  4591. if(!src1_uma) {
  4592. d_Qy = src1_buf_ctx->dev_buffer;
  4593. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4594. GGML_ASSERT(d_Qy != nullptr);
  4595. }
  4596. if(!ids_uma) {
  4597. d_ids = ids_buf_ctx->dev_buffer;
  4598. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4599. GGML_ASSERT(d_ids != nullptr);
  4600. }
  4601. if (qx_needs_dequant) {
  4602. d_X = ctx->prealloc_x;
  4603. } else {
  4604. d_X = d_Qx;
  4605. x_buf_offset = qx_buf_offset;
  4606. GGML_ASSERT(qx_sz == x_sz);
  4607. }
  4608. if (qy_needs_dequant) {
  4609. d_Y = ctx->prealloc_y;
  4610. } else {
  4611. d_Y = d_Qy;
  4612. y_buf_offset = qy_buf_offset;
  4613. GGML_ASSERT(qy_sz == y_sz);
  4614. }
  4615. if (x_non_contig) {
  4616. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4617. 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 });
  4618. }
  4619. if (y_non_contig) {
  4620. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4621. 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 });
  4622. }
  4623. uint32_t stride_batch_y = ne10*ne11;
  4624. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4625. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4626. }
  4627. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4628. uint32_t groups_x = ne01;
  4629. uint32_t groups_z = 1;
  4630. if (ne01 > max_groups_x) {
  4631. groups_z = 64;
  4632. groups_x = CEIL_DIV(groups_x, groups_z);
  4633. }
  4634. // compute
  4635. const vk_mat_vec_id_push_constants pc = {
  4636. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4637. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  4638. (uint32_t)nei0, (uint32_t)ne11,
  4639. };
  4640. ggml_vk_sync_buffers(subctx);
  4641. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4642. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  4643. 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 } },
  4644. sizeof(vk_mat_vec_id_push_constants), &pc, { groups_x, (uint32_t)nei0, groups_z });
  4645. }
  4646. 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) {
  4647. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  4648. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4649. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4650. } else {
  4651. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4652. }
  4653. }
  4654. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, ggml_tensor * dst, bool dryrun = false) {
  4655. 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];
  4656. 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];
  4657. 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];
  4658. 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];
  4659. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4660. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  4661. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  4662. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  4663. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  4664. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  4665. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  4666. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  4667. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  4668. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  4669. const uint32_t nbm1 = mask ? mask->nb[1] : 0;
  4670. const uint32_t D = neq0;
  4671. uint32_t N = neq1;
  4672. const uint32_t KV = nek1;
  4673. GGML_ASSERT(ne0 == D);
  4674. GGML_ASSERT(ne2 == N);
  4675. // input tensor rows must be contiguous
  4676. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  4677. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  4678. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  4679. GGML_ASSERT(neq0 == D);
  4680. GGML_ASSERT(nek0 == D);
  4681. GGML_ASSERT(nev0 == D);
  4682. GGML_ASSERT(neq1 == N);
  4683. GGML_ASSERT(nev0 == D);
  4684. GGML_ASSERT(nev1 == nek1);
  4685. // dst cannot be transposed or permuted
  4686. GGML_ASSERT(nb0 == sizeof(float));
  4687. GGML_ASSERT(nb0 <= nb1);
  4688. GGML_ASSERT(nb1 <= nb2);
  4689. GGML_ASSERT(nb2 <= nb3);
  4690. assert(dst->type == GGML_TYPE_F32);
  4691. assert(q->type == GGML_TYPE_F32);
  4692. assert(k->type == v->type);
  4693. vk_pipeline *pipelines;
  4694. // XXX TODO other backends may be changing accumulator precision to default to f32 soon
  4695. bool f32acc = dst->op_params[3] == GGML_PREC_F32;
  4696. bool small_rows = N <= flash_attention_num_small_rows;
  4697. switch (D) {
  4698. case 64: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D64[k->type][f32acc][small_rows][0]; break;
  4699. case 80: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D80[k->type][f32acc][small_rows][0]; break;
  4700. case 96: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D96[k->type][f32acc][small_rows][0]; break;
  4701. case 112: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D112[k->type][f32acc][small_rows][0]; break;
  4702. case 128: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D128[k->type][f32acc][small_rows][0]; break;
  4703. case 256: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D256[k->type][f32acc][small_rows][0]; break;
  4704. default:
  4705. assert(!"unsupported D value");
  4706. return;
  4707. }
  4708. assert(pipelines);
  4709. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  4710. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  4711. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  4712. bool aligned = (KV % pipelines[1]->align) == 0 &&
  4713. // the "aligned" shader variant will forcibly align strides, for performance
  4714. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  4715. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  4716. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  4717. vk_pipeline pipeline = pipelines[aligned];
  4718. assert(pipeline);
  4719. uint32_t gqa_ratio = 1;
  4720. uint32_t qk_ratio = neq2 / nek2;
  4721. uint32_t workgroups_x = (uint32_t)neq1;
  4722. uint32_t workgroups_y = (uint32_t)neq2;
  4723. uint32_t workgroups_z = (uint32_t)neq3;
  4724. if (N == 1 && qk_ratio > 1 && gqa_ratio <= flash_attention_num_small_rows &&
  4725. qk_ratio * nek2 == neq2 && nek2 == nev2 && neq3 == 1 && nek3 == 1 && nev3 == 1) {
  4726. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  4727. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  4728. // and change addressing calculations to index Q's dimension 2.
  4729. gqa_ratio = qk_ratio;
  4730. N = gqa_ratio;
  4731. workgroups_y /= N;
  4732. }
  4733. uint32_t split_kv = KV;
  4734. uint32_t split_k = 1;
  4735. // Try to use split_k when KV is large enough to be worth the overhead
  4736. if (workgroups_x == 1 && ctx->device->shader_core_count > 0 && KV >= 512) {
  4737. // Try to run two workgroups per SM.
  4738. split_k = ctx->device->shader_core_count * 2 / workgroups_y;
  4739. if (split_k > 1) {
  4740. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  4741. // of "align", so recompute split_k based on that.
  4742. split_kv = ROUNDUP_POW2(KV / split_k, pipelines[1]->align);
  4743. split_k = CEIL_DIV(KV, split_kv);
  4744. workgroups_x = split_k;
  4745. }
  4746. }
  4747. // Reserve space for split_k temporaries. For each split, we need to store the O matrix (D x ne1)
  4748. // and the per-row m and L values (ne1 rows).
  4749. const uint64_t split_k_size = split_k > 1 ? (D * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k : 0;
  4750. if (split_k_size > ctx->device->max_memory_allocation_size) {
  4751. GGML_ABORT("Requested preallocation size is too large");
  4752. }
  4753. if (ctx->prealloc_size_split_k < split_k_size) {
  4754. ctx->prealloc_size_split_k = split_k_size;
  4755. }
  4756. if (dryrun) {
  4757. // Request descriptor sets
  4758. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4759. if (split_k > 1) {
  4760. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  4761. }
  4762. return;
  4763. }
  4764. float scale = 1.0f;
  4765. float max_bias = 0.0f;
  4766. float logit_softcap = 0.0f;
  4767. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  4768. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  4769. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  4770. if (logit_softcap != 0) {
  4771. scale /= logit_softcap;
  4772. }
  4773. const uint32_t n_head_kv = neq2;
  4774. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  4775. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  4776. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  4777. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  4778. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  4779. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  4780. if (ctx->device->uma) {
  4781. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  4782. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  4783. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  4784. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  4785. Q_uma = d_Q != nullptr;
  4786. K_uma = d_K != nullptr;
  4787. V_uma = d_V != nullptr;
  4788. D_uma = d_D != nullptr;
  4789. if (mask) {
  4790. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  4791. M_uma = d_M != nullptr;
  4792. }
  4793. }
  4794. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4795. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  4796. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  4797. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  4798. if (!Q_uma) {
  4799. d_Q = q_buf_ctx->dev_buffer;
  4800. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  4801. }
  4802. if (!K_uma) {
  4803. d_K = k_buf_ctx->dev_buffer;
  4804. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  4805. }
  4806. if (!V_uma) {
  4807. d_V = v_buf_ctx->dev_buffer;
  4808. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  4809. }
  4810. if (!D_uma) {
  4811. d_D = d_buf_ctx->dev_buffer;
  4812. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4813. }
  4814. if (!M_uma) {
  4815. d_M = d_Q;
  4816. m_buf_offset = q_buf_offset;
  4817. if (mask) {
  4818. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  4819. d_M = m_buf_ctx->dev_buffer;
  4820. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  4821. }
  4822. }
  4823. const vk_flash_attn_push_constants pc = { N, KV,
  4824. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  4825. (uint32_t)neq2, (uint32_t)neq3,
  4826. (uint32_t)nek2, (uint32_t)nek3,
  4827. (uint32_t)nev2, (uint32_t)nev3,
  4828. nem1,
  4829. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  4830. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  4831. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  4832. nbm1,
  4833. scale, max_bias, logit_softcap,
  4834. mask != nullptr, n_head_log2, m0, m1,
  4835. gqa_ratio, split_kv, split_k };
  4836. ggml_vk_sync_buffers(subctx);
  4837. if (split_k > 1) {
  4838. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  4839. {
  4840. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  4841. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  4842. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  4843. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  4844. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  4845. },
  4846. // We only use split_k when group query attention is enabled, which means
  4847. // there's no more than one tile of rows (i.e. workgroups_x would have been
  4848. // one). We reuse workgroups_x to mean the number of splits, so we need to
  4849. // cancel out the divide by wg_denoms[0].
  4850. sizeof(vk_flash_attn_push_constants), &pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  4851. ggml_vk_sync_buffers(subctx);
  4852. const std::array<uint32_t, 3> pc2 = { D, (uint32_t)ne1, split_k };
  4853. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  4854. {
  4855. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  4856. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  4857. },
  4858. pc2.size() * uint32_t{sizeof(uint32_t)}, pc2.data(), { (uint32_t)ne1, 1, 1 });
  4859. } else {
  4860. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  4861. {
  4862. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  4863. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  4864. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  4865. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  4866. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  4867. },
  4868. sizeof(vk_flash_attn_push_constants), &pc, { workgroups_x, workgroups_y, workgroups_z });
  4869. }
  4870. }
  4871. 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) {
  4872. switch (op) {
  4873. case GGML_OP_GET_ROWS:
  4874. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4875. if (dst->type == GGML_TYPE_F16) {
  4876. return ctx->device->pipeline_get_rows[src0->type];
  4877. }
  4878. if (dst->type == GGML_TYPE_F32) {
  4879. return ctx->device->pipeline_get_rows_f32[src0->type];
  4880. }
  4881. return nullptr;
  4882. case GGML_OP_ACC:
  4883. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4884. return ctx->device->pipeline_acc_f32;
  4885. }
  4886. return nullptr;
  4887. case GGML_OP_ADD:
  4888. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4889. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f32_norepeat : ctx->device->pipeline_add_f32;
  4890. }
  4891. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4892. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f16_f32_f16_norepeat : ctx->device->pipeline_add_f16_f32_f16;
  4893. }
  4894. return nullptr;
  4895. case GGML_OP_SUB:
  4896. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4897. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_f32_norepeat : ctx->device->pipeline_sub_f32;
  4898. }
  4899. return nullptr;
  4900. case GGML_OP_MUL:
  4901. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4902. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_f32_norepeat : ctx->device->pipeline_mul_f32;
  4903. }
  4904. return nullptr;
  4905. case GGML_OP_DIV:
  4906. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4907. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_f32_norepeat : ctx->device->pipeline_div_f32;
  4908. }
  4909. return nullptr;
  4910. case GGML_OP_CONCAT:
  4911. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4912. return ctx->device->pipeline_concat_f32;
  4913. }
  4914. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4915. return ctx->device->pipeline_concat_f16;
  4916. }
  4917. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  4918. return ctx->device->pipeline_concat_i32;
  4919. }
  4920. return nullptr;
  4921. case GGML_OP_UPSCALE:
  4922. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 && dst->op_params[0] == GGML_SCALE_MODE_NEAREST) {
  4923. return ctx->device->pipeline_upscale_f32;
  4924. }
  4925. return nullptr;
  4926. case GGML_OP_SCALE:
  4927. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4928. return ctx->device->pipeline_scale_f32;
  4929. }
  4930. return nullptr;
  4931. case GGML_OP_SQR:
  4932. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4933. return ctx->device->pipeline_sqr_f32;
  4934. }
  4935. return nullptr;
  4936. case GGML_OP_SIN:
  4937. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4938. return ctx->device->pipeline_sin_f32;
  4939. }
  4940. return nullptr;
  4941. case GGML_OP_COS:
  4942. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4943. return ctx->device->pipeline_cos_f32;
  4944. }
  4945. return nullptr;
  4946. case GGML_OP_CLAMP:
  4947. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4948. return ctx->device->pipeline_clamp_f32;
  4949. }
  4950. return nullptr;
  4951. case GGML_OP_PAD:
  4952. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4953. return ctx->device->pipeline_pad_f32;
  4954. }
  4955. return nullptr;
  4956. case GGML_OP_REPEAT:
  4957. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  4958. return ctx->device->pipeline_repeat_f32;
  4959. }
  4960. return nullptr;
  4961. case GGML_OP_REPEAT_BACK:
  4962. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4963. return ctx->device->pipeline_repeat_back_f32;
  4964. }
  4965. return nullptr;
  4966. case GGML_OP_CPY:
  4967. case GGML_OP_CONT:
  4968. case GGML_OP_DUP:
  4969. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  4970. case GGML_OP_SILU_BACK:
  4971. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4972. return ctx->device->pipeline_silu_back_f32;
  4973. }
  4974. return nullptr;
  4975. case GGML_OP_NORM:
  4976. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4977. return ctx->device->pipeline_norm_f32;
  4978. }
  4979. return nullptr;
  4980. case GGML_OP_GROUP_NORM:
  4981. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4982. return ctx->device->pipeline_group_norm_f32;
  4983. }
  4984. return nullptr;
  4985. case GGML_OP_RMS_NORM:
  4986. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4987. return ctx->device->pipeline_rms_norm_f32;
  4988. }
  4989. return nullptr;
  4990. case GGML_OP_RMS_NORM_BACK:
  4991. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4992. return ctx->device->pipeline_rms_norm_back_f32;
  4993. }
  4994. return nullptr;
  4995. case GGML_OP_L2_NORM:
  4996. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4997. return ctx->device->pipeline_l2_norm_f32;
  4998. }
  4999. return nullptr;
  5000. case GGML_OP_UNARY:
  5001. switch (ggml_get_unary_op(dst)) {
  5002. case GGML_UNARY_OP_SILU:
  5003. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5004. return ctx->device->pipeline_silu_f32;
  5005. }
  5006. break;
  5007. case GGML_UNARY_OP_GELU:
  5008. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5009. return ctx->device->pipeline_gelu_f32;
  5010. }
  5011. break;
  5012. case GGML_UNARY_OP_GELU_QUICK:
  5013. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5014. return ctx->device->pipeline_gelu_quick_f32;
  5015. }
  5016. break;
  5017. case GGML_UNARY_OP_RELU:
  5018. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5019. return ctx->device->pipeline_relu_f32;
  5020. }
  5021. break;
  5022. case GGML_UNARY_OP_TANH:
  5023. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5024. return ctx->device->pipeline_tanh_f32;
  5025. }
  5026. break;
  5027. case GGML_UNARY_OP_SIGMOID:
  5028. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5029. return ctx->device->pipeline_sigmoid_f32;
  5030. }
  5031. break;
  5032. default:
  5033. break;
  5034. }
  5035. return nullptr;
  5036. case GGML_OP_DIAG_MASK_INF:
  5037. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5038. return ctx->device->pipeline_diag_mask_inf_f32;
  5039. }
  5040. return nullptr;
  5041. case GGML_OP_SOFT_MAX:
  5042. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  5043. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  5044. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  5045. }
  5046. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5047. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  5048. }
  5049. return nullptr;
  5050. case GGML_OP_SOFT_MAX_BACK:
  5051. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5052. return ctx->device->pipeline_soft_max_back_f32;
  5053. }
  5054. return nullptr;
  5055. case GGML_OP_ROPE:
  5056. case GGML_OP_ROPE_BACK:
  5057. {
  5058. const int mode = ((const int32_t *) dst->op_params)[2];
  5059. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  5060. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  5061. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  5062. if (is_neox) {
  5063. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5064. return ctx->device->pipeline_rope_neox_f32;
  5065. }
  5066. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5067. return ctx->device->pipeline_rope_neox_f16;
  5068. }
  5069. } else if (is_mrope && !is_vision) {
  5070. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5071. return ctx->device->pipeline_rope_multi_f32;
  5072. }
  5073. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5074. return ctx->device->pipeline_rope_multi_f16;
  5075. }
  5076. } else if (is_vision) {
  5077. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5078. return ctx->device->pipeline_rope_vision_f32;
  5079. }
  5080. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5081. return ctx->device->pipeline_rope_vision_f16;
  5082. }
  5083. } else {
  5084. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5085. return ctx->device->pipeline_rope_norm_f32;
  5086. }
  5087. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5088. return ctx->device->pipeline_rope_norm_f16;
  5089. }
  5090. }
  5091. return nullptr;
  5092. }
  5093. case GGML_OP_ARGSORT:
  5094. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5095. return ctx->device->pipeline_argsort_f32;
  5096. }
  5097. return nullptr;
  5098. case GGML_OP_SUM:
  5099. case GGML_OP_SUM_ROWS:
  5100. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5101. return ctx->device->pipeline_sum_rows_f32;
  5102. }
  5103. return nullptr;
  5104. case GGML_OP_ARGMAX:
  5105. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5106. return ctx->device->pipeline_argmax_f32;
  5107. }
  5108. return nullptr;
  5109. case GGML_OP_COUNT_EQUAL:
  5110. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  5111. return ctx->device->pipeline_count_equal_i32;
  5112. }
  5113. return nullptr;
  5114. case GGML_OP_IM2COL:
  5115. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5116. return ctx->device->pipeline_im2col_f32;
  5117. }
  5118. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  5119. return ctx->device->pipeline_im2col_f32_f16;
  5120. }
  5121. return nullptr;
  5122. case GGML_OP_TIMESTEP_EMBEDDING:
  5123. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5124. return ctx->device->pipeline_timestep_embedding_f32;
  5125. }
  5126. return nullptr;
  5127. case GGML_OP_POOL_2D:
  5128. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5129. return ctx->device->pipeline_pool2d_f32;
  5130. }
  5131. return nullptr;
  5132. case GGML_OP_RWKV_WKV6:
  5133. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5134. return ctx->device->pipeline_rwkv_wkv6_f32;
  5135. }
  5136. return nullptr;
  5137. case GGML_OP_RWKV_WKV7:
  5138. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5139. return ctx->device->pipeline_rwkv_wkv7_f32;
  5140. }
  5141. return nullptr;
  5142. case GGML_OP_OPT_STEP_ADAMW:
  5143. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5144. return ctx->device->pipeline_opt_step_adamw_f32;
  5145. }
  5146. return nullptr;
  5147. case GGML_OP_LEAKY_RELU:
  5148. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5149. return ctx->device->pipeline_leaky_relu_f32;
  5150. }
  5151. return nullptr;
  5152. default:
  5153. return nullptr;
  5154. }
  5155. GGML_UNUSED(src2);
  5156. }
  5157. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  5158. switch (op) {
  5159. case GGML_OP_CPY:
  5160. case GGML_OP_GET_ROWS:
  5161. case GGML_OP_ADD:
  5162. case GGML_OP_SUB:
  5163. case GGML_OP_MUL:
  5164. case GGML_OP_DIV:
  5165. case GGML_OP_CONCAT:
  5166. case GGML_OP_UPSCALE:
  5167. case GGML_OP_SQR:
  5168. case GGML_OP_SIN:
  5169. case GGML_OP_COS:
  5170. case GGML_OP_CLAMP:
  5171. case GGML_OP_PAD:
  5172. case GGML_OP_REPEAT:
  5173. case GGML_OP_REPEAT_BACK:
  5174. case GGML_OP_ROPE:
  5175. case GGML_OP_RMS_NORM:
  5176. return true;
  5177. default:
  5178. return false;
  5179. }
  5180. }
  5181. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  5182. {
  5183. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  5184. }
  5185. 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) {
  5186. GGML_UNUSED(p);
  5187. GGML_UNUSED(src0);
  5188. GGML_UNUSED(src1);
  5189. GGML_UNUSED(src2);
  5190. GGML_UNUSED(dst);
  5191. static_assert(!std::is_const<T>::value, "unexpected type");
  5192. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  5193. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  5194. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  5195. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  5196. }
  5197. 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) {
  5198. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5199. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5200. p.misalign_offsets = (a_offset << 16) | d_offset;
  5201. GGML_UNUSED(src1);
  5202. GGML_UNUSED(src2);
  5203. }
  5204. 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) {
  5205. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5206. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  5207. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5208. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  5209. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  5210. GGML_UNUSED(src2);
  5211. }
  5212. 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) {
  5213. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5214. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5215. p.a_offset = a_offset;
  5216. p.d_offset = d_offset;
  5217. GGML_UNUSED(src1);
  5218. GGML_UNUSED(src2);
  5219. }
  5220. template<typename PC>
  5221. 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) {
  5222. 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];
  5223. if (src1 != nullptr) {
  5224. 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];
  5225. }
  5226. if (src2 != nullptr) {
  5227. 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];
  5228. }
  5229. 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];
  5230. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  5231. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  5232. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  5233. GGML_ASSERT(dst->buffer != nullptr);
  5234. const uint64_t ne00 = src0->ne[0];
  5235. const uint64_t ne01 = src0->ne[1];
  5236. const uint64_t ne02 = src0->ne[2];
  5237. const uint64_t ne03 = src0->ne[3];
  5238. const uint64_t ne0 = ne00 * ne01;
  5239. const bool use_src1 = src1 != nullptr;
  5240. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  5241. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  5242. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  5243. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  5244. const uint64_t ne1 = ne10 * ne11;
  5245. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  5246. const bool use_src2 = src2 != nullptr;
  5247. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  5248. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  5249. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  5250. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  5251. const uint64_t ne2 = ne20 * ne21;
  5252. const uint64_t ned0 = dst->ne[0];
  5253. const uint64_t ned1 = dst->ne[1];
  5254. const uint64_t ned2 = dst->ne[2];
  5255. const uint64_t ned3 = dst->ne[3];
  5256. const uint64_t ned = ned0 * ned1;
  5257. init_pushconst_fastdiv(pc);
  5258. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  5259. if (pipeline == nullptr) {
  5260. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  5261. if (src1 != nullptr) {
  5262. std::cerr << " and " << ggml_type_name(src1->type);
  5263. }
  5264. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  5265. GGML_ABORT("fatal error");
  5266. }
  5267. if (dryrun) {
  5268. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5269. return;
  5270. }
  5271. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  5272. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5273. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5274. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  5275. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  5276. vk_buffer d_X = nullptr;
  5277. size_t x_buf_offset = 0;
  5278. vk_buffer d_Y = nullptr;
  5279. size_t y_buf_offset = 0;
  5280. vk_buffer d_Z = nullptr;
  5281. size_t z_buf_offset = 0;
  5282. bool src0_uma = false;
  5283. bool src1_uma = false;
  5284. bool src2_uma = false;
  5285. if (ctx->device->uma) {
  5286. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  5287. src0_uma = d_X != nullptr;
  5288. if (use_src1) {
  5289. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  5290. src1_uma = d_Y != nullptr;
  5291. }
  5292. if (use_src2) {
  5293. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  5294. src2_uma = d_Z != nullptr;
  5295. }
  5296. }
  5297. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  5298. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  5299. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  5300. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  5301. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5302. // Workaround for tiny tensor inputs on ROPE
  5303. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  5304. y_sz = VK_WHOLE_SIZE;
  5305. }
  5306. GGML_ASSERT(d_D != nullptr);
  5307. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5308. if(!src0_uma) {
  5309. d_X = src0_buf_ctx->dev_buffer;
  5310. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5311. GGML_ASSERT(d_X != nullptr);
  5312. }
  5313. if (use_src1 && !src1_uma) {
  5314. d_Y = src1_buf_ctx->dev_buffer;
  5315. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5316. GGML_ASSERT(d_Y != nullptr);
  5317. }
  5318. if (use_src2 && !src2_uma) {
  5319. d_Z = src2_buf_ctx->dev_buffer;
  5320. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  5321. GGML_ASSERT(d_Z != nullptr);
  5322. }
  5323. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  5324. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  5325. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5326. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5327. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5328. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5329. if (op_supports_incontiguous) {
  5330. x_sz = ggml_nbytes(src0);
  5331. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  5332. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  5333. d_sz = ggml_nbytes(dst);
  5334. if (x_buf_offset + x_sz >= d_X->size) {
  5335. x_sz = VK_WHOLE_SIZE;
  5336. }
  5337. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  5338. y_sz = VK_WHOLE_SIZE;
  5339. }
  5340. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  5341. z_sz = VK_WHOLE_SIZE;
  5342. }
  5343. if (d_buf_offset + d_sz >= d_D->size) {
  5344. d_sz = VK_WHOLE_SIZE;
  5345. }
  5346. }
  5347. std::array<uint32_t, 3> elements;
  5348. // Single call if dimension 2 is contiguous
  5349. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  5350. switch (op) {
  5351. case GGML_OP_NORM:
  5352. case GGML_OP_RMS_NORM_BACK:
  5353. case GGML_OP_L2_NORM:
  5354. case GGML_OP_SOFT_MAX:
  5355. case GGML_OP_SOFT_MAX_BACK:
  5356. case GGML_OP_SUM_ROWS:
  5357. case GGML_OP_ARGMAX:
  5358. {
  5359. const uint32_t nr = ggml_nrows(src0);
  5360. if (nr > 262144) {
  5361. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  5362. } else if (nr > 512) {
  5363. elements = { 512, CEIL_DIV(nr, 512), 1 };
  5364. } else {
  5365. elements = { nr, 1, 1 };
  5366. }
  5367. } break;
  5368. case GGML_OP_RMS_NORM:
  5369. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  5370. break;
  5371. case GGML_OP_SUM:
  5372. // We use GGML_OP_SUM_ROWS with 1 row.
  5373. elements = { 1, 1, 1 };
  5374. break;
  5375. case GGML_OP_GROUP_NORM:
  5376. {
  5377. const uint32_t num_groups = dst->op_params[0];
  5378. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  5379. } break;
  5380. case GGML_OP_DIAG_MASK_INF:
  5381. case GGML_OP_ROPE:
  5382. case GGML_OP_ROPE_BACK:
  5383. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  5384. break;
  5385. case GGML_OP_GET_ROWS:
  5386. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  5387. break;
  5388. case GGML_OP_ARGSORT:
  5389. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  5390. break;
  5391. case GGML_OP_IM2COL:
  5392. {
  5393. const bool is_2D = dst->op_params[6] == 1;
  5394. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5395. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5396. const uint32_t KW = src0->ne[0];
  5397. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5398. const uint32_t OW = dst->ne[1];
  5399. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  5400. elements = { OW * KW * KH, OH, batch * IC };
  5401. } break;
  5402. case GGML_OP_TIMESTEP_EMBEDDING:
  5403. {
  5404. const uint32_t dim = dst->op_params[0];
  5405. uint32_t half_ceil = (dim + 1) / 2;
  5406. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  5407. } break;
  5408. case GGML_OP_POOL_2D:
  5409. {
  5410. const uint32_t N = dst->ne[3];
  5411. const uint32_t OC = dst->ne[2];
  5412. const uint32_t OH = dst->ne[1];
  5413. const uint32_t OW = dst->ne[0];
  5414. elements = { N * OC * OH * OW, 1, 1};
  5415. } break;
  5416. case GGML_OP_ADD:
  5417. case GGML_OP_SUB:
  5418. case GGML_OP_DIV:
  5419. case GGML_OP_MUL:
  5420. case GGML_OP_SCALE:
  5421. case GGML_OP_SQR:
  5422. case GGML_OP_SIN:
  5423. case GGML_OP_COS:
  5424. case GGML_OP_CLAMP:
  5425. case GGML_OP_PAD:
  5426. case GGML_OP_REPEAT:
  5427. case GGML_OP_REPEAT_BACK:
  5428. case GGML_OP_CPY:
  5429. case GGML_OP_CONCAT:
  5430. case GGML_OP_UPSCALE:
  5431. case GGML_OP_UNARY:
  5432. {
  5433. const uint32_t ne = ggml_nelements(dst);
  5434. if (ne > 262144) {
  5435. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  5436. } else if (ne > 512) {
  5437. elements = { 512, CEIL_DIV(ne, 512), 1 };
  5438. } else {
  5439. elements = { ne, 1, 1 };
  5440. }
  5441. } break;
  5442. default:
  5443. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  5444. break;
  5445. }
  5446. if (!op_supports_incontiguous) {
  5447. if (x_sz != VK_WHOLE_SIZE) {
  5448. x_sz *= ne02 * ne03;
  5449. }
  5450. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  5451. y_sz *= ne12 * ne13;
  5452. }
  5453. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  5454. z_sz *= ne22 * ne23;
  5455. }
  5456. if (d_sz != VK_WHOLE_SIZE) {
  5457. d_sz *= ned2 * ned3;
  5458. }
  5459. }
  5460. if (op == GGML_OP_SOFT_MAX) {
  5461. // Empty src1 is possible in soft_max, but the shader needs a buffer
  5462. vk_subbuffer subbuf_y;
  5463. if (use_src1) {
  5464. subbuf_y = { d_Y, y_buf_offset, y_sz };
  5465. } else {
  5466. subbuf_y = { d_X, 0, x_sz };
  5467. }
  5468. ggml_vk_sync_buffers(subctx);
  5469. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5470. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  5471. // Empty src2 is possible in rope, but the shader needs a buffer
  5472. vk_subbuffer subbuf_z;
  5473. if (use_src2) {
  5474. subbuf_z = { d_Z, z_buf_offset, z_sz };
  5475. } else {
  5476. subbuf_z = { d_X, 0, x_sz };
  5477. }
  5478. ggml_vk_sync_buffers(subctx);
  5479. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5480. } else if (op == GGML_OP_IM2COL) {
  5481. // im2col uses only src1 and dst buffers
  5482. ggml_vk_sync_buffers(subctx);
  5483. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5484. } else if (op == GGML_OP_COUNT_EQUAL) {
  5485. ggml_vk_sync_buffers(subctx);
  5486. // count_equal assumes that destination buffer is initialized with zeroes
  5487. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  5488. ggml_vk_sync_buffers(subctx);
  5489. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5490. } else if (use_src2) {
  5491. ggml_vk_sync_buffers(subctx);
  5492. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5493. } else if (use_src1) {
  5494. ggml_vk_sync_buffers(subctx);
  5495. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5496. } else {
  5497. ggml_vk_sync_buffers(subctx);
  5498. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5499. }
  5500. }
  5501. 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) {
  5502. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5503. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5504. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5505. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  5506. (uint32_t)ggml_nelements(src0),
  5507. (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,
  5508. (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,
  5509. (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,
  5510. 0,
  5511. 0.0f, 0.0f, 0,
  5512. }, dryrun);
  5513. }
  5514. 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) {
  5515. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5516. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5517. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5518. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5519. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5520. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5521. int offset = dst->op_params[3] / 4; // offset in bytes
  5522. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  5523. (uint32_t)ggml_nelements(src0),
  5524. (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,
  5525. (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,
  5526. (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,
  5527. 0,
  5528. 0.0f, 0.0f, offset,
  5529. }, dryrun);
  5530. }
  5531. 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) {
  5532. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5533. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5534. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5535. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  5536. (uint32_t)ggml_nelements(src0),
  5537. (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,
  5538. (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,
  5539. (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,
  5540. 0,
  5541. 0.0f, 0.0f, 0,
  5542. }, dryrun);
  5543. }
  5544. 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) {
  5545. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5546. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5547. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5548. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  5549. (uint32_t)ggml_nelements(src0),
  5550. (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,
  5551. (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,
  5552. (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,
  5553. 0,
  5554. 0.0f, 0.0f, 0,
  5555. }, dryrun);
  5556. }
  5557. 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) {
  5558. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5559. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5560. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5561. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  5562. (uint32_t)ggml_nelements(src0),
  5563. (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,
  5564. (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,
  5565. (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,
  5566. 0,
  5567. 0.0f, 0.0f, 0,
  5568. }, dryrun);
  5569. }
  5570. 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) {
  5571. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5572. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5573. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5574. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  5575. (uint32_t)ggml_nelements(src0),
  5576. (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,
  5577. (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,
  5578. (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,
  5579. 0,
  5580. 0.0f, 0.0f, 0,
  5581. }, dryrun);
  5582. }
  5583. 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) {
  5584. GGML_ASSERT(version == 6 || version == 7);
  5585. int num_srcs = version == 6 ? 6 : 7;
  5586. for (int i = 0; i < num_srcs; i++) {
  5587. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  5588. }
  5589. GGML_ASSERT(dst->buffer != nullptr);
  5590. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  5591. GGML_ASSERT(pipeline != nullptr);
  5592. if (dryrun) {
  5593. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5594. return;
  5595. }
  5596. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5597. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5598. for (int i = 0; i < num_srcs; i++) {
  5599. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  5600. }
  5601. ggml_vk_sync_buffers(subctx);
  5602. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5603. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5604. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  5605. if (ctx->device->uma) {
  5606. for (int i = 0; i < num_srcs; i++) {
  5607. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  5608. srcs_uma[i] = d_srcs[i] != nullptr;
  5609. }
  5610. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  5611. dst_uma = d_D != nullptr;
  5612. }
  5613. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5614. for (int i = 0; i < num_srcs; i++) {
  5615. src_sizes[i] = ggml_nbytes(dst->src[i]);
  5616. if (!srcs_uma[i]) {
  5617. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  5618. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  5619. }
  5620. }
  5621. const uint64_t dst_size = ggml_nbytes(dst);
  5622. if (!dst_uma) {
  5623. d_D = dst_buf_ctx->dev_buffer;
  5624. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  5625. }
  5626. std::array<uint32_t, 3> elements = {
  5627. (uint32_t)(pc.B * pc.H),
  5628. 1,
  5629. 1
  5630. };
  5631. if (version == 6) {
  5632. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5633. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5634. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5635. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5636. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5637. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5638. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5639. vk_subbuffer{ d_D, dst_offset, dst_size }
  5640. }, sizeof(vk_op_rwkv_wkv6_push_constants), &pc, elements);
  5641. } else if (version == 7) {
  5642. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5643. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5644. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5645. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5646. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5647. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5648. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5649. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  5650. vk_subbuffer{ d_D, dst_offset, dst_size }
  5651. }, sizeof(vk_op_rwkv_wkv7_push_constants), &pc, elements);
  5652. } else {
  5653. // shouldn't happen
  5654. GGML_ASSERT(false);
  5655. }
  5656. }
  5657. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5658. const size_t seq_length = dst->src[0]->ne[2];
  5659. const size_t n_embed = dst->ne[0];
  5660. const size_t n_heads = dst->src[0]->ne[1];
  5661. const size_t n_seqs = dst->src[5]->ne[1];
  5662. ggml_vk_op_f32_wkv(
  5663. ctx, subctx, dst,
  5664. {
  5665. (uint32_t)n_seqs,
  5666. (uint32_t)seq_length,
  5667. (uint32_t)n_embed,
  5668. (uint32_t)n_heads,
  5669. },
  5670. 6,
  5671. dryrun
  5672. );
  5673. }
  5674. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5675. const size_t seq_length = dst->src[0]->ne[2];
  5676. const size_t n_embed = dst->ne[0];
  5677. const size_t n_heads = dst->src[0]->ne[1];
  5678. const size_t n_seqs = dst->src[6]->ne[1];
  5679. ggml_vk_op_f32_wkv(
  5680. ctx, subctx, dst,
  5681. {
  5682. (uint32_t)n_seqs,
  5683. (uint32_t)seq_length,
  5684. (uint32_t)n_embed,
  5685. (uint32_t)n_heads,
  5686. },
  5687. 7,
  5688. dryrun
  5689. );
  5690. }
  5691. 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) {
  5692. const ggml_tensor * x = dst->src[0];
  5693. const ggml_tensor * g = dst->src[1];
  5694. const ggml_tensor * gm = dst->src[2];
  5695. const ggml_tensor * gv = dst->src[3];
  5696. const ggml_tensor * p = dst->src[4];
  5697. GGML_ASSERT(x->type == GGML_TYPE_F32);
  5698. GGML_ASSERT(g->type == GGML_TYPE_F32);
  5699. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  5700. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  5701. GGML_ASSERT(p->type == GGML_TYPE_F32);
  5702. GGML_ASSERT(dst->buffer != nullptr);
  5703. GGML_ASSERT(ggml_is_contiguous(x));
  5704. GGML_ASSERT(ggml_is_contiguous(g));
  5705. GGML_ASSERT(ggml_is_contiguous(gm));
  5706. GGML_ASSERT(ggml_is_contiguous(gv));
  5707. GGML_ASSERT(ggml_is_contiguous(p));
  5708. GGML_ASSERT(ggml_are_same_shape(x, g));
  5709. GGML_ASSERT(ggml_are_same_shape(x, gm));
  5710. GGML_ASSERT(ggml_are_same_shape(x, gv));
  5711. GGML_ASSERT(ggml_nelements(p) == 7);
  5712. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  5713. GGML_ASSERT(pipeline != nullptr);
  5714. if (dryrun) {
  5715. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5716. return;
  5717. }
  5718. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  5719. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  5720. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  5721. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  5722. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  5723. ggml_vk_sync_buffers(subctx);
  5724. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  5725. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  5726. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  5727. if (ctx->device->uma) {
  5728. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  5729. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  5730. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  5731. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  5732. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  5733. X_uma = d_X != nullptr;
  5734. G_uma = d_G != nullptr;
  5735. GM_uma = d_GM != nullptr;
  5736. GV_uma = d_GV != nullptr;
  5737. P_uma = d_P != nullptr;
  5738. }
  5739. if (!X_uma) {
  5740. d_X = x_buf_ctx->dev_buffer;
  5741. x_offset = vk_tensor_offset(x) + x->view_offs;
  5742. }
  5743. if (!G_uma) {
  5744. d_G = g_buf_ctx->dev_buffer;
  5745. g_offset = vk_tensor_offset(g) + g->view_offs;
  5746. }
  5747. if (!GM_uma) {
  5748. d_GM = gm_buf_ctx->dev_buffer;
  5749. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  5750. }
  5751. if (!GV_uma) {
  5752. d_GV = gv_buf_ctx->dev_buffer;
  5753. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  5754. }
  5755. if (!P_uma) {
  5756. d_P = p_buf_ctx->dev_buffer;
  5757. p_offset = vk_tensor_offset(p) + p->view_offs;
  5758. }
  5759. const uint64_t x_size = ggml_nbytes(x);
  5760. const uint64_t g_size = ggml_nbytes(g);
  5761. const uint64_t gm_size = ggml_nbytes(gm);
  5762. const uint64_t gv_size = ggml_nbytes(gv);
  5763. const uint64_t p_size = ggml_nbytes(p);
  5764. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  5765. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5766. vk_subbuffer{ d_X, x_offset, x_size },
  5767. vk_subbuffer{ d_G, g_offset, g_size },
  5768. vk_subbuffer{ d_GM, gm_offset, gm_size },
  5769. vk_subbuffer{ d_GV, gv_offset, gv_size },
  5770. vk_subbuffer{ d_P, p_offset, p_size },
  5771. }, sizeof(vk_op_push_constants), &pc, elements);
  5772. }
  5773. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5774. const size_t n = ggml_nelements(dst->src[0]);
  5775. ggml_vk_op_f32_opt_step_adamw(
  5776. ctx, subctx, dst,
  5777. { (uint32_t)n, 0, 0.0f, 0.0f },
  5778. dryrun
  5779. );
  5780. }
  5781. 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) {
  5782. int * op_params = (int *)dst->op_params;
  5783. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5784. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5785. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5786. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  5787. (uint32_t)ggml_nelements(dst),
  5788. (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,
  5789. (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,
  5790. (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,
  5791. 0,
  5792. 0.0f, 0.0f, op_params[0],
  5793. }, dryrun);
  5794. }
  5795. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5796. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5797. const float sf0 = (float)dst->ne[0] / src0->ne[0];
  5798. const float sf1 = (float)dst->ne[1] / src0->ne[1];
  5799. const float sf2 = (float)dst->ne[2] / src0->ne[2];
  5800. const float sf3 = (float)dst->ne[3] / src0->ne[3];
  5801. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  5802. (uint32_t)ggml_nelements(dst), 0, 0,
  5803. (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,
  5804. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  5805. sf0, sf1, sf2, sf3,
  5806. }, dryrun);
  5807. }
  5808. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5809. float * op_params = (float *)dst->op_params;
  5810. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5811. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5812. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  5813. (uint32_t)ggml_nelements(src0),
  5814. (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,
  5815. (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,
  5816. 0,
  5817. op_params[0], 0.0f,
  5818. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5819. }, dryrun);
  5820. }
  5821. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5822. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5823. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5824. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  5825. (uint32_t)ggml_nelements(src0),
  5826. (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,
  5827. (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,
  5828. 0,
  5829. 0.0f, 0.0f,
  5830. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5831. }, dryrun);
  5832. }
  5833. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5834. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5835. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5836. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, {
  5837. (uint32_t)ggml_nelements(src0),
  5838. (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,
  5839. (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,
  5840. 0,
  5841. 0.0f, 0.0f,
  5842. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5843. }, dryrun);
  5844. }
  5845. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5846. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5847. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5848. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, {
  5849. (uint32_t)ggml_nelements(src0),
  5850. (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,
  5851. (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,
  5852. 0,
  5853. 0.0f, 0.0f,
  5854. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5855. }, dryrun);
  5856. }
  5857. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5858. float * op_params = (float *)dst->op_params;
  5859. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5860. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5861. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  5862. (uint32_t)ggml_nelements(src0),
  5863. (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,
  5864. (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,
  5865. 0,
  5866. op_params[0], op_params[1],
  5867. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5868. }, dryrun);
  5869. }
  5870. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5871. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5872. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5873. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, {
  5874. (uint32_t)ggml_nelements(dst),
  5875. (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,
  5876. (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,
  5877. 0,
  5878. 0.0f, 0.0f,
  5879. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5880. }, dryrun);
  5881. }
  5882. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5883. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5884. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5885. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, {
  5886. (uint32_t)ggml_nelements(dst),
  5887. (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,
  5888. (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,
  5889. 0,
  5890. 0.0f, 0.0f,
  5891. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5892. }, dryrun);
  5893. }
  5894. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5895. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5896. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5897. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, {
  5898. (uint32_t)ggml_nelements(dst),
  5899. (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,
  5900. (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,
  5901. 0,
  5902. 0.0f, 0.0f,
  5903. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5904. }, dryrun);
  5905. }
  5906. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5907. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5908. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5909. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  5910. (uint32_t)ggml_nelements(src0),
  5911. (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,
  5912. (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,
  5913. 0,
  5914. 0.0f, 0.0f,
  5915. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5916. }, dryrun);
  5917. }
  5918. 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) {
  5919. 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);
  5920. }
  5921. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5922. float * op_params = (float *)dst->op_params;
  5923. 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);
  5924. }
  5925. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5926. const int * int_op_params = (const int *)dst->op_params;
  5927. const float * float_op_params = (const float *)dst->op_params;
  5928. const uint32_t num_groups = int_op_params[0];
  5929. const float eps = float_op_params[1];
  5930. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5931. 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);
  5932. }
  5933. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5934. float * op_params = (float *)dst->op_params;
  5935. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5936. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5937. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_RMS_NORM, {
  5938. (uint32_t)ggml_nelements(src0),
  5939. (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,
  5940. (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,
  5941. 0,
  5942. op_params[0], 0.0f,
  5943. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5944. }, dryrun);
  5945. }
  5946. 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) {
  5947. float * op_params = (float *)dst->op_params;
  5948. 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);
  5949. }
  5950. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5951. float * op_params = (float *)dst->op_params;
  5952. 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);
  5953. }
  5954. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5955. 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);
  5956. }
  5957. 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) {
  5958. int32_t * op_params = (int32_t *)dst->op_params;
  5959. 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);
  5960. }
  5961. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5962. float * op_params = (float *)dst->op_params;
  5963. float scale = op_params[0];
  5964. float max_bias = op_params[1];
  5965. const uint32_t ncols = (uint32_t)src0->ne[0];
  5966. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  5967. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  5968. const uint32_t n_head_kv = nrows_x/nrows_y;
  5969. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5970. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5971. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5972. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  5973. ncols,
  5974. src1 != nullptr ? nrows_y : (uint32_t)0,
  5975. scale, max_bias,
  5976. m0, m1,
  5977. n_head_log2,
  5978. nrows_x,
  5979. }, dryrun);
  5980. }
  5981. 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) {
  5982. float * op_params = (float *)dst->op_params;
  5983. 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);
  5984. }
  5985. 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) {
  5986. const int n_dims = ((int32_t *) dst->op_params)[1];
  5987. const int mode = ((int32_t *) dst->op_params)[2];
  5988. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  5989. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  5990. const float freq_base = ((float *) dst->op_params)[5];
  5991. const float freq_scale = ((float *) dst->op_params)[6];
  5992. const float ext_factor = ((float *) dst->op_params)[7];
  5993. const float attn_factor = ((float *) dst->op_params)[8];
  5994. const float beta_fast = ((float *) dst->op_params)[9];
  5995. const float beta_slow = ((float *) dst->op_params)[10];
  5996. int sections[4] {};
  5997. if (mode & GGML_ROPE_TYPE_MROPE) {
  5998. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  5999. }
  6000. float corr_dims[2];
  6001. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  6002. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6003. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  6004. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  6005. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  6006. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  6007. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  6008. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  6009. sections[0], sections[1], sections[2], sections[3], backprop
  6010. }, dryrun);
  6011. }
  6012. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6013. int32_t * op_params = (int32_t *)dst->op_params;
  6014. uint32_t ncols = src0->ne[0];
  6015. uint32_t ncols_pad = 1;
  6016. while (ncols_pad < ncols) {
  6017. ncols_pad *= 2;
  6018. }
  6019. GGML_ASSERT(ncols_pad <= 1024);
  6020. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  6021. ncols,
  6022. ncols_pad,
  6023. op_params[0],
  6024. }, dryrun);
  6025. }
  6026. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6027. 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);
  6028. }
  6029. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6030. 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);
  6031. }
  6032. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6033. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6034. }
  6035. 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) {
  6036. 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);
  6037. }
  6038. 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) {
  6039. const int32_t s0 = dst->op_params[0];
  6040. const int32_t s1 = dst->op_params[1];
  6041. const int32_t p0 = dst->op_params[2];
  6042. const int32_t p1 = dst->op_params[3];
  6043. const int32_t d0 = dst->op_params[4];
  6044. const int32_t d1 = dst->op_params[5];
  6045. const bool is_2D = dst->op_params[6] == 1;
  6046. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6047. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  6048. const uint32_t IW = src1->ne[0];
  6049. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6050. const uint32_t KW = src0->ne[0];
  6051. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6052. const uint32_t OW = dst->ne[1];
  6053. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6054. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  6055. const uint32_t pelements = OW * KW * KH;
  6056. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  6057. batch_offset, offset_delta,
  6058. IC, IW, IH, OW, OH, KW, KH,
  6059. pelements,
  6060. IC * KH * KW,
  6061. s0, s1, p0, p1, d0, d1,
  6062. }, dryrun);
  6063. }
  6064. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6065. const uint32_t dim = dst->op_params[0];
  6066. const uint32_t max_period = dst->op_params[1];
  6067. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  6068. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  6069. nb1, dim, max_period,
  6070. }, dryrun);
  6071. }
  6072. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6073. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  6074. const int32_t k1 = dst->op_params[1];
  6075. const int32_t k0 = dst->op_params[2];
  6076. const int32_t s1 = dst->op_params[3];
  6077. const int32_t s0 = dst->op_params[4];
  6078. const int32_t p1 = dst->op_params[5];
  6079. const int32_t p0 = dst->op_params[6];
  6080. const uint32_t IH = src0->ne[1];
  6081. const uint32_t IW = src0->ne[0];
  6082. const uint32_t N = dst->ne[3];
  6083. const uint32_t OC = dst->ne[2];
  6084. const uint32_t OH = dst->ne[1];
  6085. const uint32_t OW = dst->ne[0];
  6086. const uint32_t parallel_elements = N * OC * OH * OW;
  6087. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  6088. IW, IH, OW, OH, OC,
  6089. parallel_elements,
  6090. op,
  6091. k0, k1, s0, s1, p0, p1,
  6092. }, dryrun);
  6093. }
  6094. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6095. const float * op_params = (const float *)dst->op_params;
  6096. 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);
  6097. }
  6098. #ifdef GGML_VULKAN_RUN_TESTS
  6099. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  6100. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  6101. return;
  6102. }
  6103. i0 = std::max(i0, 5);
  6104. i1 = std::max(i1, 5);
  6105. i2 = std::max(i2, 0);
  6106. fprintf(stderr, " ");
  6107. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6108. fprintf(stderr, "%7d ", idx1);
  6109. }
  6110. fprintf(stderr, "\n");
  6111. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6112. fprintf(stderr, "%7d: ", idx0);
  6113. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6114. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  6115. float val;
  6116. if (type == GGML_TYPE_F32) {
  6117. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  6118. } else if (type == GGML_TYPE_F16) {
  6119. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  6120. } else {
  6121. GGML_ABORT("fatal error");
  6122. }
  6123. fprintf(stderr, "% 7.2f ", val);
  6124. } else {
  6125. fprintf(stderr, " ");
  6126. }
  6127. }
  6128. fprintf(stderr, "\n");
  6129. }
  6130. }
  6131. template <typename X_TYPE, typename Y_TYPE>
  6132. 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) {
  6133. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  6134. const size_t x_ne = m * k * batch;
  6135. const size_t y_ne = k * n * batch;
  6136. const size_t d_ne = m * n * batch;
  6137. vk_pipeline p;
  6138. std::string shname;
  6139. if (shader_size == 0) {
  6140. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6141. p = ctx->device->pipeline_matmul_f32->a_s;
  6142. shname = "F32_ALIGNED_S";
  6143. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6144. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  6145. shname = "F32_F16_ALIGNED_S";
  6146. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6147. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  6148. shname = "F16_F32_ALIGNED_S";
  6149. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6150. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  6151. shname = "F16_ALIGNED_S";
  6152. } else {
  6153. GGML_ABORT("fatal error");
  6154. }
  6155. } else if (shader_size == 1) {
  6156. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6157. p = ctx->device->pipeline_matmul_f32->a_m;
  6158. shname = "F32_ALIGNED_M";
  6159. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6160. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  6161. shname = "F32_F16_ALIGNED_M";
  6162. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6163. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  6164. shname = "F16_F32_ALIGNED_M";
  6165. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6166. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  6167. shname = "F16_ALIGNED_M";
  6168. } else {
  6169. GGML_ABORT("fatal error");
  6170. }
  6171. } else if (shader_size == 2) {
  6172. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6173. p = ctx->device->pipeline_matmul_f32->a_l;
  6174. shname = "F32_ALIGNED_L";
  6175. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6176. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  6177. shname = "F32_F16_ALIGNED_L";
  6178. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6179. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  6180. shname = "F16_F32_ALIGNED_L";
  6181. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6182. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  6183. shname = "F16_ALIGNED_L";
  6184. } else {
  6185. GGML_ABORT("fatal error");
  6186. }
  6187. } else {
  6188. GGML_ASSERT(0);
  6189. }
  6190. const size_t kpad = ggml_vk_align_size(k, p->align);
  6191. if (k != kpad) {
  6192. if (shader_size == 0) {
  6193. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6194. p = ctx->device->pipeline_matmul_f32->s;
  6195. shname = "F32_S";
  6196. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6197. p = ctx->device->pipeline_matmul_f32_f16->s;
  6198. shname = "F32_F16_S";
  6199. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6200. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  6201. shname = "F16_F32_S";
  6202. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6203. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  6204. shname = "F16_S";
  6205. }
  6206. } else if (shader_size == 1) {
  6207. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6208. p = ctx->device->pipeline_matmul_f32->m;
  6209. shname = "F32_M";
  6210. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6211. p = ctx->device->pipeline_matmul_f32_f16->m;
  6212. shname = "F32_F16_M";
  6213. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6214. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  6215. shname = "F16_F32_M";
  6216. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6217. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  6218. shname = "F16_M";
  6219. }
  6220. } else if (shader_size == 2) {
  6221. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6222. p = ctx->device->pipeline_matmul_f32->l;
  6223. shname = "F32_L";
  6224. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6225. p = ctx->device->pipeline_matmul_f32_f16->l;
  6226. shname = "F32_F16_L";
  6227. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6228. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  6229. shname = "F16_F32_L";
  6230. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6231. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  6232. shname = "F16_L";
  6233. }
  6234. }
  6235. }
  6236. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  6237. if (split_k > 1) {
  6238. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6239. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6240. // Resize buffer
  6241. if (ctx->prealloc_split_k != nullptr) {
  6242. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6243. }
  6244. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6245. }
  6246. }
  6247. if (ctx->device->need_compiles) {
  6248. ggml_vk_load_shaders(ctx->device);
  6249. }
  6250. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6251. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6252. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6253. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6254. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  6255. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  6256. float* d = (float *) malloc(sizeof(float) * d_ne);
  6257. for (size_t i = 0; i < x_ne; i++) {
  6258. if (std::is_same<float, X_TYPE>()) {
  6259. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6260. // x[i] = 1.0f;
  6261. // x[i] = i + 1;
  6262. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6263. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  6264. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  6265. // x[i] = ggml_fp32_to_fp16(1.0f);
  6266. // x[i] = ggml_fp32_to_fp16(i + 1);
  6267. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  6268. } else {
  6269. GGML_ABORT("fatal error");
  6270. }
  6271. }
  6272. for (size_t i = 0; i < y_ne; i++) {
  6273. if (std::is_same<float, Y_TYPE>()) {
  6274. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6275. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6276. // y[i] = i + 1;
  6277. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6278. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  6279. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  6280. // y[i] = ggml_fp32_to_fp16(i + 1);
  6281. } else {
  6282. GGML_ABORT("fatal error");
  6283. }
  6284. }
  6285. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  6286. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  6287. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6288. ggml_vk_ctx_begin(ctx->device, subctx);
  6289. for (size_t i = 0; i < num_it; i++) {
  6290. ggml_vk_matmul(
  6291. 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),
  6292. m, n, k,
  6293. k, k, m, k*m, k*n, m*n,
  6294. split_k, batch, batch, batch, 1, 1, n
  6295. );
  6296. }
  6297. ggml_vk_ctx_end(subctx);
  6298. auto begin = std::chrono::high_resolution_clock::now();
  6299. ggml_vk_submit(subctx, ctx->fence);
  6300. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  6301. ctx->device->device.resetFences({ ctx->fence });
  6302. auto end = std::chrono::high_resolution_clock::now();
  6303. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6304. // copy dst to host
  6305. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  6306. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  6307. ggml_init_params iparams = {
  6308. /*.mem_size =*/ 1024*1024*1024,
  6309. /*.mem_buffer =*/ NULL,
  6310. /*.no_alloc =*/ true,
  6311. };
  6312. ggml_context * ggml_ctx = ggml_init(iparams);
  6313. ggml_type src0_type;
  6314. ggml_type src1_type;
  6315. if (std::is_same<float, X_TYPE>()) {
  6316. src0_type = GGML_TYPE_F32;
  6317. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  6318. src0_type = GGML_TYPE_F16;
  6319. } else {
  6320. GGML_ABORT("fatal error");
  6321. }
  6322. if (std::is_same<float, Y_TYPE>()) {
  6323. src1_type = GGML_TYPE_F32;
  6324. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6325. src1_type = GGML_TYPE_F16;
  6326. } else {
  6327. GGML_ABORT("fatal error");
  6328. }
  6329. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  6330. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  6331. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6332. src0_ggml->data = x;
  6333. src1_ggml->data = y;
  6334. tensor_ggml->data = d_chk;
  6335. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6336. ggml_build_forward_expand(cgraph, tensor_ggml);
  6337. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6338. ggml_free(ggml_ctx);
  6339. double avg_err = 0.0;
  6340. int first_err_n = -1;
  6341. int first_err_m = -1;
  6342. int first_err_b = -1;
  6343. for (size_t i = 0; i < m*n*batch; i++) {
  6344. double err = std::fabs(d[i] - d_chk[i]);
  6345. avg_err += err;
  6346. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6347. first_err_b = i / (m * n);
  6348. first_err_n = (i % (m * n)) / m;
  6349. first_err_m = (i % (m * n)) % m;
  6350. }
  6351. }
  6352. avg_err /= m * n;
  6353. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6354. 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;
  6355. if (avg_err > 0.1 || std::isnan(avg_err)) {
  6356. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6357. std::cerr << "Actual result: " << std::endl << std::endl;
  6358. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6359. std::cerr << "Expected result: " << std::endl << std::endl;
  6360. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6361. if (split_k > 1) {
  6362. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6363. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6364. std::cerr << "d_buf0: " << std::endl << std::endl;
  6365. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6366. std::cerr << "d_buf1: " << std::endl << std::endl;
  6367. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6368. std::cerr << "d_buf2: " << std::endl << std::endl;
  6369. 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);
  6370. std::cerr << "d_buf3: " << std::endl << std::endl;
  6371. 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);
  6372. free(split_k_buf);
  6373. }
  6374. }
  6375. free(d_chk);
  6376. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  6377. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  6378. ggml_vk_destroy_buffer(d_X);
  6379. ggml_vk_destroy_buffer(d_Y);
  6380. ggml_vk_destroy_buffer(d_D);
  6381. ggml_pipeline_cleanup(p);
  6382. ggml_pipeline_cleanup(ctx->device->pipeline_matmul_split_k_reduce);
  6383. free(x);
  6384. free(y);
  6385. free(d);
  6386. }
  6387. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  6388. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  6389. return;
  6390. }
  6391. i0 = std::max(i0, 5);
  6392. i1 = std::max(i1, 5);
  6393. i2 = std::max(i2, 0);
  6394. i3 = std::max(i3, 0);
  6395. fprintf(stderr, " ");
  6396. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6397. fprintf(stderr, "%7d ", idx1);
  6398. }
  6399. fprintf(stderr, "\n");
  6400. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6401. fprintf(stderr, "%7d: ", idx0);
  6402. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6403. 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]) {
  6404. float val;
  6405. if (tensor->type == GGML_TYPE_F32) {
  6406. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  6407. } else if (tensor->type == GGML_TYPE_F16) {
  6408. 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]));
  6409. } else {
  6410. GGML_ABORT("fatal error");
  6411. }
  6412. fprintf(stderr, "% 7.2f ", val);
  6413. } else {
  6414. fprintf(stderr, " ");
  6415. }
  6416. }
  6417. fprintf(stderr, "\n");
  6418. }
  6419. }
  6420. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  6421. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  6422. }
  6423. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  6424. if (quant == GGML_TYPE_F32) {
  6425. memcpy(to, from, sizeof(float) * ne);
  6426. return;
  6427. }
  6428. const auto * tt = ggml_get_type_traits(quant);
  6429. ggml_to_float_t dequant_fn = tt->to_float;
  6430. dequant_fn(from, to, ne);
  6431. }
  6432. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  6433. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  6434. const size_t x_sz = sizeof(float) * ne;
  6435. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  6436. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6437. float * x = (float *) malloc(x_sz);
  6438. void * qx = malloc(qx_sz);
  6439. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6440. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6441. float * x_ref = (float *) malloc(x_sz);
  6442. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  6443. for (size_t i = 0; i < ne; i++) {
  6444. x[i] = rand() / (float)RAND_MAX;
  6445. }
  6446. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  6447. ggml_vk_quantize_data(x, qx, ne, quant);
  6448. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  6449. ggml_pipeline_request_descriptor_sets(ctx->device, p, 1);
  6450. if (ctx->device->need_compiles) {
  6451. ggml_vk_load_shaders(ctx->device);
  6452. }
  6453. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6454. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6455. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6456. ggml_vk_ctx_begin(ctx->device, subctx);
  6457. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  6458. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc.size() * sizeof(int), pc.data(), { (uint32_t)ne, 1, 1});
  6459. ggml_vk_ctx_end(subctx);
  6460. auto begin = std::chrono::high_resolution_clock::now();
  6461. ggml_vk_submit(subctx, ctx->fence);
  6462. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6463. ctx->device->device.resetFences({ ctx->fence });
  6464. auto end = std::chrono::high_resolution_clock::now();
  6465. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6466. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  6467. int first_err = -1;
  6468. double avg_err = 0.0;
  6469. for (size_t i = 0; i < ne; i++) {
  6470. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  6471. avg_err += error;
  6472. if (first_err < 0 && error > 0.05) {
  6473. first_err = i;
  6474. }
  6475. }
  6476. avg_err /= ne;
  6477. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  6478. if (avg_err > 0.1) {
  6479. std::cerr << "first_error = " << first_err << std::endl;
  6480. std::cerr << "Actual result: " << std::endl << std::endl;
  6481. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6482. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  6483. }
  6484. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  6485. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6486. std::cerr << x_ref[i] << ", ";
  6487. }
  6488. std::cerr << std::endl;
  6489. }
  6490. ggml_vk_destroy_buffer(x_buf);
  6491. ggml_vk_destroy_buffer(qx_buf);
  6492. free(x);
  6493. free(qx);
  6494. free(x_ref);
  6495. free(x_chk);
  6496. }
  6497. // This does not work without ggml q8_1 quantization support
  6498. //
  6499. // typedef uint16_t ggml_half;
  6500. // typedef uint32_t ggml_half2;
  6501. //
  6502. // #define QK8_1 32
  6503. // typedef struct {
  6504. // union {
  6505. // struct {
  6506. // ggml_half d; // delta
  6507. // ggml_half s; // d * sum(qs[i])
  6508. // } GGML_COMMON_AGGR_S;
  6509. // ggml_half2 ds;
  6510. // } GGML_COMMON_AGGR_U;
  6511. // int8_t qs[QK8_1]; // quants
  6512. // } block_q8_1;
  6513. //
  6514. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  6515. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  6516. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  6517. //
  6518. // const size_t x_sz = sizeof(float) * ne;
  6519. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6520. // float * x = (float *) malloc(x_sz);
  6521. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  6522. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  6523. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6524. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6525. //
  6526. // for (size_t i = 0; i < ne; i++) {
  6527. // x[i] = rand() / (float)RAND_MAX;
  6528. // }
  6529. //
  6530. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  6531. //
  6532. // ggml_pipeline_request_descriptor_sets(ctx->device, p, 1);
  6533. //
  6534. // if (ctx->device->need_compiles) {
  6535. // ggml_vk_load_shaders(ctx->device);
  6536. // }
  6537. //
  6538. // ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6539. //
  6540. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  6541. //
  6542. // vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6543. // ggml_vk_ctx_begin(ctx->device, subctx);
  6544. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  6545. // ggml_vk_ctx_end(subctx);
  6546. //
  6547. // auto begin = std::chrono::high_resolution_clock::now();
  6548. //
  6549. // ggml_vk_submit(subctx, ctx->fence);
  6550. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  6551. // ctx->device->device.resetFences({ ctx->fence });
  6552. //
  6553. // auto end = std::chrono::high_resolution_clock::now();
  6554. //
  6555. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6556. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  6557. //
  6558. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  6559. //
  6560. // int first_err = -1;
  6561. //
  6562. // for (size_t i = 0; i < ne / 32; i++) {
  6563. // 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));
  6564. //
  6565. // if (first_err < 0 && error > 0.1) {
  6566. // first_err = i;
  6567. // }
  6568. //
  6569. // 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));
  6570. //
  6571. // if (first_err < 0 && error > 0.1) {
  6572. // first_err = i;
  6573. // }
  6574. //
  6575. // for (size_t j = 0; j < 32; j++) {
  6576. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  6577. //
  6578. // if (first_err < 0 && error > 1) {
  6579. // first_err = i;
  6580. // }
  6581. // }
  6582. // }
  6583. //
  6584. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  6585. //
  6586. // if (first_err != -1) {
  6587. // std::cerr << "first_error = " << first_err << std::endl;
  6588. // std::cerr << "Actual result: " << std::endl << std::endl;
  6589. // 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) << " ";
  6590. // for (size_t j = 0; j < 32; j++) {
  6591. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  6592. // }
  6593. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  6594. // 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) << " ";
  6595. // for (size_t j = 0; j < 32; j++) {
  6596. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  6597. // }
  6598. // std::cerr << std::endl;
  6599. // }
  6600. //
  6601. // ggml_vk_destroy_buffer(x_buf);
  6602. // ggml_vk_destroy_buffer(qx_buf);
  6603. //
  6604. // free(x);
  6605. // free(qx);
  6606. // free(qx_res);
  6607. // }
  6608. 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) {
  6609. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  6610. const size_t x_ne = m * k * batch;
  6611. const size_t y_ne = k * n * batch;
  6612. const size_t d_ne = m * n * batch;
  6613. vk_matmul_pipeline2 * pipelines;
  6614. if (mmq) {
  6615. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  6616. } else {
  6617. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  6618. }
  6619. const bool fp16acc = ctx->device->fp16;
  6620. vk_pipeline p;
  6621. std::string shname;
  6622. if (shader_size == 0) {
  6623. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  6624. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  6625. } else if (shader_size == 1) {
  6626. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  6627. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  6628. } else if (shader_size == 2) {
  6629. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  6630. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  6631. } else {
  6632. GGML_ASSERT(0);
  6633. }
  6634. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  6635. if (mmq || k != kpad) {
  6636. if (shader_size == 0) {
  6637. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  6638. shname = std::string(ggml_type_name(quant)) + "_S";
  6639. } else if (shader_size == 1) {
  6640. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  6641. shname = std::string(ggml_type_name(quant)) + "_M";
  6642. } else if (shader_size == 2) {
  6643. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  6644. shname = std::string(ggml_type_name(quant)) + "_L";
  6645. } else {
  6646. GGML_ASSERT(0);
  6647. }
  6648. }
  6649. if (p == nullptr) {
  6650. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  6651. return;
  6652. }
  6653. const size_t x_sz = sizeof(float) * x_ne;
  6654. const size_t y_sz = sizeof(float) * y_ne;
  6655. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6656. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  6657. const size_t d_sz = sizeof(float) * d_ne;
  6658. float * x = (float *) malloc(x_sz);
  6659. float * y = (float *) malloc(y_sz);
  6660. void * qx = malloc(qx_sz);
  6661. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6662. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6663. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6664. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6665. float * d = (float *) malloc(d_sz);
  6666. float * d_chk = (float *) malloc(d_sz);
  6667. for (size_t i = 0; i < x_ne; i++) {
  6668. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6669. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6670. // x[i] = i % k;
  6671. }
  6672. ggml_vk_quantize_data(x, qx, x_ne, quant);
  6673. for (size_t i = 0; i < y_ne; i++) {
  6674. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6675. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6676. // y[i] = i % k;
  6677. }
  6678. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  6679. if (split_k > 1) {
  6680. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6681. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6682. // Resize buffer
  6683. if (ctx->prealloc_split_k != nullptr) {
  6684. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6685. }
  6686. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6687. }
  6688. }
  6689. if (mmq) {
  6690. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_quantize_q8_1, num_it);
  6691. }
  6692. if (ctx->device->need_compiles) {
  6693. ggml_vk_load_shaders(ctx->device);
  6694. }
  6695. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6696. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6697. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  6698. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6699. ggml_vk_ctx_begin(ctx->device, subctx);
  6700. if (mmq) {
  6701. for (size_t i = 0; i < num_it; i++) {
  6702. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  6703. ggml_vk_matmul(
  6704. 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 },
  6705. m, n, k,
  6706. k, k, m, k*m, k*n, m*n,
  6707. split_k, batch, batch, batch, 1, 1, n
  6708. );
  6709. }
  6710. } else {
  6711. for (size_t i = 0; i < num_it; i++) {
  6712. ggml_vk_matmul(
  6713. 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 },
  6714. m, n, k,
  6715. k, k, m, k*m, k*n, m*n,
  6716. split_k, batch, batch, batch, 1, 1, n
  6717. );
  6718. }
  6719. }
  6720. ggml_vk_ctx_end(subctx);
  6721. auto begin = std::chrono::high_resolution_clock::now();
  6722. ggml_vk_submit(subctx, ctx->fence);
  6723. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6724. ctx->device->device.resetFences({ ctx->fence });
  6725. auto end = std::chrono::high_resolution_clock::now();
  6726. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6727. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  6728. ggml_init_params iparams = {
  6729. /*.mem_size =*/ 1024*1024*1024,
  6730. /*.mem_buffer =*/ NULL,
  6731. /*.no_alloc =*/ true,
  6732. };
  6733. ggml_context * ggml_ctx = ggml_init(iparams);
  6734. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  6735. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  6736. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6737. src0_ggml->data = qx;
  6738. src1_ggml->data = y;
  6739. tensor_ggml->data = d_chk;
  6740. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6741. ggml_build_forward_expand(cgraph, tensor_ggml);
  6742. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6743. ggml_free(ggml_ctx);
  6744. double avg_err = 0.0;
  6745. int first_err_n = -1;
  6746. int first_err_m = -1;
  6747. int first_err_b = -1;
  6748. for (size_t i = 0; i < m*n*batch; i++) {
  6749. double err = std::fabs(d[i] - d_chk[i]);
  6750. avg_err += err;
  6751. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6752. first_err_b = i / (m * n);
  6753. first_err_n = (i % (m * n)) / m;
  6754. first_err_m = (i % (m * n)) % m;
  6755. }
  6756. }
  6757. avg_err /= m * n;
  6758. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6759. std::cerr << "TEST dequant matmul " << shname;
  6760. if (mmq) {
  6761. std::cerr << " mmq";
  6762. }
  6763. 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;
  6764. if (avg_err > 0.01 || std::isnan(avg_err)) {
  6765. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6766. std::cerr << "Actual result: " << std::endl << std::endl;
  6767. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6768. std::cerr << std::endl;
  6769. std::cerr << "Expected result: " << std::endl << std::endl;
  6770. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6771. std::cerr << "src0: " << std::endl << std::endl;
  6772. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  6773. std::cerr << std::endl;
  6774. std::cerr << "src1: " << std::endl << std::endl;
  6775. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  6776. if (split_k > 1) {
  6777. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6778. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6779. std::cerr << "d_buf0: " << std::endl << std::endl;
  6780. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6781. std::cerr << "d_buf1: " << std::endl << std::endl;
  6782. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6783. std::cerr << "d_buf2: " << std::endl << std::endl;
  6784. 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);
  6785. std::cerr << "d_buf3: " << std::endl << std::endl;
  6786. 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);
  6787. free(split_k_buf);
  6788. }
  6789. }
  6790. ggml_vk_destroy_buffer(qx_buf);
  6791. ggml_vk_destroy_buffer(y_buf);
  6792. ggml_vk_destroy_buffer(qy_buf);
  6793. ggml_vk_destroy_buffer(d_buf);
  6794. free(x);
  6795. free(qx);
  6796. free(y);
  6797. free(d);
  6798. free(d_chk);
  6799. }
  6800. #endif
  6801. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  6802. #if defined(GGML_VULKAN_RUN_TESTS)
  6803. const std::vector<size_t> vals {
  6804. 512, 512, 128,
  6805. 128, 512, 512,
  6806. 4096, 512, 4096,
  6807. 11008, 512, 4096,
  6808. 4096, 512, 11008,
  6809. 32000, 512, 4096,
  6810. 8, 8, 8,
  6811. 100, 46, 576,
  6812. 623, 111, 128,
  6813. 100, 46, 558,
  6814. 512, 1, 256,
  6815. 128, 110, 622,
  6816. 511, 511, 127,
  6817. 511, 511, 7,
  6818. 511, 511, 17,
  6819. 49, 49, 128,
  6820. 128, 49, 49,
  6821. 4096, 49, 4096,
  6822. };
  6823. const size_t num_it = 100;
  6824. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  6825. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  6826. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  6827. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  6828. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  6829. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  6830. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  6831. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  6832. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  6833. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  6834. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  6835. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  6836. abort();
  6837. for (size_t i = 0; i < vals.size(); i += 3) {
  6838. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  6839. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  6840. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  6841. std::cerr << '\n';
  6842. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  6843. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  6844. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  6845. std::cerr << '\n';
  6846. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  6847. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  6848. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  6849. std::cerr << '\n' << std::endl;
  6850. if (vals[i + 2] % 32 == 0) {
  6851. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  6852. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  6853. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  6854. std::cerr << '\n';
  6855. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  6856. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  6857. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  6858. std::cerr << '\n';
  6859. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  6860. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  6861. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  6862. std::cerr << '\n' << std::endl;
  6863. }
  6864. if (vals[i + 2] % 256 == 0) {
  6865. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  6866. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  6867. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  6868. std::cerr << '\n';
  6869. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  6870. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  6871. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  6872. std::cerr << '\n';
  6873. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  6874. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  6875. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  6876. std::cerr << '\n' << std::endl;
  6877. }
  6878. }
  6879. GGML_ABORT("fatal error");
  6880. #endif
  6881. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  6882. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  6883. // Resize buffer
  6884. if (ctx->prealloc_x != nullptr) {
  6885. ggml_vk_destroy_buffer(ctx->prealloc_x);
  6886. }
  6887. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  6888. }
  6889. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  6890. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  6891. // Resize buffer
  6892. if (ctx->prealloc_y != nullptr) {
  6893. ggml_vk_destroy_buffer(ctx->prealloc_y);
  6894. }
  6895. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  6896. }
  6897. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  6898. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  6899. // Resize buffer
  6900. if (ctx->prealloc_split_k != nullptr) {
  6901. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6902. }
  6903. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  6904. }
  6905. }
  6906. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* tensor, int tensor_idx, bool use_fence, bool almost_ready);
  6907. // Returns true if node has enqueued work into the queue, false otherwise
  6908. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  6909. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
  6910. if (ggml_is_empty(node) || !node->buffer) {
  6911. return false;
  6912. }
  6913. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  6914. ctx->semaphore_idx = 0;
  6915. const ggml_tensor * src0 = node->src[0];
  6916. const ggml_tensor * src1 = node->src[1];
  6917. const ggml_tensor * src2 = node->src[2];
  6918. const ggml_tensor * src3 = node->src[3];
  6919. switch (node->op) {
  6920. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  6921. case GGML_OP_RESHAPE:
  6922. case GGML_OP_VIEW:
  6923. case GGML_OP_PERMUTE:
  6924. case GGML_OP_TRANSPOSE:
  6925. case GGML_OP_NONE:
  6926. return false;
  6927. case GGML_OP_UNARY:
  6928. switch (ggml_get_unary_op(node)) {
  6929. case GGML_UNARY_OP_SILU:
  6930. case GGML_UNARY_OP_GELU:
  6931. case GGML_UNARY_OP_GELU_QUICK:
  6932. case GGML_UNARY_OP_RELU:
  6933. case GGML_UNARY_OP_TANH:
  6934. case GGML_UNARY_OP_SIGMOID:
  6935. break;
  6936. default:
  6937. return false;
  6938. }
  6939. break;
  6940. case GGML_OP_REPEAT:
  6941. case GGML_OP_REPEAT_BACK:
  6942. case GGML_OP_GET_ROWS:
  6943. case GGML_OP_ADD:
  6944. case GGML_OP_ACC:
  6945. case GGML_OP_SUB:
  6946. case GGML_OP_MUL:
  6947. case GGML_OP_DIV:
  6948. case GGML_OP_CONCAT:
  6949. case GGML_OP_UPSCALE:
  6950. case GGML_OP_SCALE:
  6951. case GGML_OP_SQR:
  6952. case GGML_OP_SIN:
  6953. case GGML_OP_COS:
  6954. case GGML_OP_CLAMP:
  6955. case GGML_OP_PAD:
  6956. case GGML_OP_CPY:
  6957. case GGML_OP_CONT:
  6958. case GGML_OP_DUP:
  6959. case GGML_OP_SILU_BACK:
  6960. case GGML_OP_NORM:
  6961. case GGML_OP_GROUP_NORM:
  6962. case GGML_OP_RMS_NORM:
  6963. case GGML_OP_RMS_NORM_BACK:
  6964. case GGML_OP_L2_NORM:
  6965. case GGML_OP_DIAG_MASK_INF:
  6966. case GGML_OP_SOFT_MAX:
  6967. case GGML_OP_SOFT_MAX_BACK:
  6968. case GGML_OP_ROPE:
  6969. case GGML_OP_ROPE_BACK:
  6970. case GGML_OP_MUL_MAT:
  6971. case GGML_OP_MUL_MAT_ID:
  6972. case GGML_OP_ARGSORT:
  6973. case GGML_OP_SUM:
  6974. case GGML_OP_SUM_ROWS:
  6975. case GGML_OP_ARGMAX:
  6976. case GGML_OP_COUNT_EQUAL:
  6977. case GGML_OP_IM2COL:
  6978. case GGML_OP_TIMESTEP_EMBEDDING:
  6979. case GGML_OP_POOL_2D:
  6980. case GGML_OP_RWKV_WKV6:
  6981. case GGML_OP_RWKV_WKV7:
  6982. case GGML_OP_LEAKY_RELU:
  6983. case GGML_OP_FLASH_ATTN_EXT:
  6984. case GGML_OP_OPT_STEP_ADAMW:
  6985. break;
  6986. default:
  6987. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  6988. GGML_ABORT("fatal error");
  6989. return false;
  6990. }
  6991. vk_context compute_ctx;
  6992. if (!dryrun) {
  6993. if (ctx->compute_ctx.expired()) {
  6994. compute_ctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6995. ctx->compute_ctx = compute_ctx;
  6996. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  6997. } else {
  6998. compute_ctx = ctx->compute_ctx.lock();
  6999. }
  7000. } else {
  7001. switch (node->op) {
  7002. case GGML_OP_REPEAT:
  7003. case GGML_OP_REPEAT_BACK:
  7004. case GGML_OP_ACC:
  7005. case GGML_OP_GET_ROWS:
  7006. case GGML_OP_ADD:
  7007. case GGML_OP_SUB:
  7008. case GGML_OP_MUL:
  7009. case GGML_OP_DIV:
  7010. case GGML_OP_CONCAT:
  7011. case GGML_OP_UPSCALE:
  7012. case GGML_OP_SCALE:
  7013. case GGML_OP_SQR:
  7014. case GGML_OP_SIN:
  7015. case GGML_OP_COS:
  7016. case GGML_OP_CLAMP:
  7017. case GGML_OP_PAD:
  7018. case GGML_OP_CPY:
  7019. case GGML_OP_CONT:
  7020. case GGML_OP_DUP:
  7021. case GGML_OP_SILU_BACK:
  7022. case GGML_OP_NORM:
  7023. case GGML_OP_GROUP_NORM:
  7024. case GGML_OP_RMS_NORM:
  7025. case GGML_OP_RMS_NORM_BACK:
  7026. case GGML_OP_L2_NORM:
  7027. case GGML_OP_UNARY:
  7028. case GGML_OP_DIAG_MASK_INF:
  7029. case GGML_OP_SOFT_MAX:
  7030. case GGML_OP_SOFT_MAX_BACK:
  7031. case GGML_OP_ROPE:
  7032. case GGML_OP_ROPE_BACK:
  7033. case GGML_OP_ARGSORT:
  7034. case GGML_OP_SUM:
  7035. case GGML_OP_SUM_ROWS:
  7036. case GGML_OP_ARGMAX:
  7037. case GGML_OP_COUNT_EQUAL:
  7038. case GGML_OP_IM2COL:
  7039. case GGML_OP_TIMESTEP_EMBEDDING:
  7040. case GGML_OP_POOL_2D:
  7041. case GGML_OP_LEAKY_RELU:
  7042. {
  7043. // These operations all go through ggml_vk_op_f32, so short-circuit and
  7044. // do the only thing needed for the dryrun.
  7045. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  7046. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  7047. return false;
  7048. }
  7049. default:
  7050. break;
  7051. }
  7052. }
  7053. switch (node->op) {
  7054. case GGML_OP_REPEAT:
  7055. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  7056. break;
  7057. case GGML_OP_REPEAT_BACK:
  7058. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  7059. break;
  7060. case GGML_OP_ACC:
  7061. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  7062. break;
  7063. case GGML_OP_GET_ROWS:
  7064. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7065. break;
  7066. case GGML_OP_ADD:
  7067. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  7068. break;
  7069. case GGML_OP_SUB:
  7070. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  7071. break;
  7072. case GGML_OP_MUL:
  7073. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  7074. break;
  7075. case GGML_OP_DIV:
  7076. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  7077. break;
  7078. case GGML_OP_CONCAT:
  7079. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  7080. break;
  7081. case GGML_OP_UPSCALE:
  7082. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  7083. break;
  7084. case GGML_OP_SCALE:
  7085. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  7086. break;
  7087. case GGML_OP_SQR:
  7088. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  7089. break;
  7090. case GGML_OP_SIN:
  7091. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  7092. break;
  7093. case GGML_OP_COS:
  7094. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  7095. break;
  7096. case GGML_OP_CLAMP:
  7097. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  7098. break;
  7099. case GGML_OP_PAD:
  7100. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  7101. break;
  7102. case GGML_OP_CPY:
  7103. case GGML_OP_CONT:
  7104. case GGML_OP_DUP:
  7105. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  7106. break;
  7107. case GGML_OP_SILU_BACK:
  7108. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7109. break;
  7110. case GGML_OP_NORM:
  7111. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  7112. break;
  7113. case GGML_OP_GROUP_NORM:
  7114. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  7115. break;
  7116. case GGML_OP_RMS_NORM:
  7117. ggml_vk_rms_norm(ctx, compute_ctx, src0, node, dryrun);
  7118. break;
  7119. case GGML_OP_RMS_NORM_BACK:
  7120. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7121. break;
  7122. case GGML_OP_L2_NORM:
  7123. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  7124. break;
  7125. case GGML_OP_UNARY:
  7126. switch (ggml_get_unary_op(node)) {
  7127. case GGML_UNARY_OP_SILU:
  7128. case GGML_UNARY_OP_GELU:
  7129. case GGML_UNARY_OP_GELU_QUICK:
  7130. case GGML_UNARY_OP_RELU:
  7131. case GGML_UNARY_OP_TANH:
  7132. case GGML_UNARY_OP_SIGMOID:
  7133. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  7134. break;
  7135. default:
  7136. return false;
  7137. }
  7138. break;
  7139. case GGML_OP_DIAG_MASK_INF:
  7140. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  7141. break;
  7142. case GGML_OP_SOFT_MAX:
  7143. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  7144. break;
  7145. case GGML_OP_SOFT_MAX_BACK:
  7146. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7147. break;
  7148. case GGML_OP_ROPE:
  7149. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  7150. break;
  7151. case GGML_OP_ROPE_BACK:
  7152. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  7153. break;
  7154. case GGML_OP_ARGSORT:
  7155. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  7156. break;
  7157. case GGML_OP_SUM:
  7158. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  7159. break;
  7160. case GGML_OP_SUM_ROWS:
  7161. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  7162. break;
  7163. case GGML_OP_ARGMAX:
  7164. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  7165. break;
  7166. case GGML_OP_COUNT_EQUAL:
  7167. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  7168. break;
  7169. case GGML_OP_IM2COL:
  7170. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  7171. break;
  7172. case GGML_OP_TIMESTEP_EMBEDDING:
  7173. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  7174. break;
  7175. case GGML_OP_POOL_2D:
  7176. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  7177. break;
  7178. case GGML_OP_LEAKY_RELU:
  7179. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  7180. break;
  7181. case GGML_OP_MUL_MAT:
  7182. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  7183. break;
  7184. case GGML_OP_MUL_MAT_ID:
  7185. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  7186. break;
  7187. case GGML_OP_FLASH_ATTN_EXT:
  7188. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  7189. break;
  7190. case GGML_OP_RWKV_WKV6:
  7191. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  7192. break;
  7193. case GGML_OP_RWKV_WKV7:
  7194. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  7195. break;
  7196. case GGML_OP_OPT_STEP_ADAMW:
  7197. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  7198. break;
  7199. default:
  7200. return false;
  7201. }
  7202. if (dryrun) {
  7203. return false;
  7204. }
  7205. ctx->tensor_ctxs[node_idx] = compute_ctx;
  7206. #if defined(GGML_VULKAN_CHECK_RESULTS) || defined(GGML_VULKAN_PERF)
  7207. // Force context reset on each node so that each tensor ends up in its own context
  7208. // and can be run and compared to its CPU equivalent separately
  7209. last_node = true;
  7210. #endif
  7211. if (submit || last_node) {
  7212. ggml_vk_ctx_end(compute_ctx);
  7213. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  7214. if (last_node) {
  7215. compute_ctx->exit_tensor_idx = node_idx_begin;
  7216. }
  7217. else {
  7218. compute_ctx->exit_tensor_idx = -1;
  7219. }
  7220. ctx->compute_ctx.reset();
  7221. bool ok = ggml_vk_compute_forward(ctx, node_begin, node_idx_begin, false, almost_ready);
  7222. if (!ok) {
  7223. if (node->op == GGML_OP_UNARY) {
  7224. 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;
  7225. }
  7226. else {
  7227. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  7228. }
  7229. }
  7230. }
  7231. return true;
  7232. }
  7233. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_tensor * tensor, int tensor_idx, bool use_fence = true, bool almost_ready = false) {
  7234. ggml_backend_buffer * buf = nullptr;
  7235. switch (tensor->op) {
  7236. case GGML_OP_ADD:
  7237. case GGML_OP_ACC:
  7238. case GGML_OP_GET_ROWS:
  7239. case GGML_OP_SUB:
  7240. case GGML_OP_MUL:
  7241. case GGML_OP_DIV:
  7242. case GGML_OP_CONCAT:
  7243. case GGML_OP_UPSCALE:
  7244. case GGML_OP_SCALE:
  7245. case GGML_OP_SQR:
  7246. case GGML_OP_SIN:
  7247. case GGML_OP_COS:
  7248. case GGML_OP_CLAMP:
  7249. case GGML_OP_PAD:
  7250. case GGML_OP_CPY:
  7251. case GGML_OP_CONT:
  7252. case GGML_OP_DUP:
  7253. case GGML_OP_SILU_BACK:
  7254. case GGML_OP_NORM:
  7255. case GGML_OP_GROUP_NORM:
  7256. case GGML_OP_RMS_NORM:
  7257. case GGML_OP_RMS_NORM_BACK:
  7258. case GGML_OP_L2_NORM:
  7259. case GGML_OP_DIAG_MASK_INF:
  7260. case GGML_OP_SOFT_MAX:
  7261. case GGML_OP_SOFT_MAX_BACK:
  7262. case GGML_OP_ROPE:
  7263. case GGML_OP_ROPE_BACK:
  7264. case GGML_OP_RESHAPE:
  7265. case GGML_OP_VIEW:
  7266. case GGML_OP_PERMUTE:
  7267. case GGML_OP_TRANSPOSE:
  7268. case GGML_OP_NONE:
  7269. case GGML_OP_ARGSORT:
  7270. case GGML_OP_SUM:
  7271. case GGML_OP_SUM_ROWS:
  7272. case GGML_OP_ARGMAX:
  7273. case GGML_OP_COUNT_EQUAL:
  7274. case GGML_OP_IM2COL:
  7275. case GGML_OP_TIMESTEP_EMBEDDING:
  7276. case GGML_OP_POOL_2D:
  7277. case GGML_OP_RWKV_WKV6:
  7278. case GGML_OP_RWKV_WKV7:
  7279. case GGML_OP_LEAKY_RELU:
  7280. case GGML_OP_REPEAT:
  7281. case GGML_OP_REPEAT_BACK:
  7282. case GGML_OP_OPT_STEP_ADAMW:
  7283. buf = tensor->buffer;
  7284. break;
  7285. case GGML_OP_UNARY:
  7286. switch (ggml_get_unary_op(tensor)) {
  7287. case GGML_UNARY_OP_SILU:
  7288. case GGML_UNARY_OP_GELU:
  7289. case GGML_UNARY_OP_GELU_QUICK:
  7290. case GGML_UNARY_OP_RELU:
  7291. case GGML_UNARY_OP_TANH:
  7292. case GGML_UNARY_OP_SIGMOID:
  7293. buf = tensor->buffer;
  7294. break;
  7295. default:
  7296. return false;
  7297. }
  7298. break;
  7299. case GGML_OP_MUL_MAT:
  7300. case GGML_OP_MUL_MAT_ID:
  7301. case GGML_OP_FLASH_ATTN_EXT:
  7302. buf = tensor->buffer;
  7303. break;
  7304. default:
  7305. return false;
  7306. }
  7307. if (buf == nullptr) {
  7308. return false;
  7309. }
  7310. 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 << ")");
  7311. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  7312. // always wait for the GPU work to be done for the last submit
  7313. if (tensor_idx == subctx->exit_tensor_idx) {
  7314. use_fence = true;
  7315. }
  7316. // Only run if ctx hasn't been submitted yet
  7317. if (!subctx->seqs.empty()) {
  7318. #ifdef GGML_VULKAN_CHECK_RESULTS
  7319. ggml_vk_check_results_0(tensor);
  7320. use_fence = true;
  7321. #endif
  7322. // Do staging buffer copies
  7323. for (auto& cpy : subctx->in_memcpys) {
  7324. memcpy(cpy.dst, cpy.src, cpy.n);
  7325. }
  7326. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  7327. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  7328. ctx->almost_ready_fence_pending = true;
  7329. } else {
  7330. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  7331. }
  7332. if (use_fence) {
  7333. ggml_vk_wait_for_fence(ctx);
  7334. }
  7335. #ifdef GGML_VULKAN_CHECK_RESULTS
  7336. ggml_vk_check_results_1(tensor);
  7337. #endif
  7338. }
  7339. if (tensor_idx == subctx->exit_tensor_idx) {
  7340. // Do staging buffer copies
  7341. for (auto& cpy : subctx->out_memcpys) {
  7342. memcpy(cpy.dst, cpy.src, cpy.n);
  7343. }
  7344. subctx->in_memcpys.clear();
  7345. subctx->out_memcpys.clear();
  7346. }
  7347. return true;
  7348. }
  7349. // Clean up after graph processing is done
  7350. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  7351. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  7352. for (auto& buffer : ctx->gc.temp_buffers) {
  7353. ggml_vk_pool_free(ctx, buffer);
  7354. }
  7355. ctx->gc.temp_buffers.clear();
  7356. for (auto& dsr : ctx->device->pipeline_descriptor_set_requirements) {
  7357. vk_pipeline_ref plr = ctx->device->pipelines[dsr.first];
  7358. if (plr.expired()) {
  7359. continue;
  7360. }
  7361. vk_pipeline pl = plr.lock();
  7362. ggml_pipeline_cleanup(pl);
  7363. }
  7364. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  7365. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  7366. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  7367. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  7368. }
  7369. ctx->gc.semaphores.clear();
  7370. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  7371. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  7372. }
  7373. ctx->gc.tl_semaphores.clear();
  7374. ctx->semaphore_idx = 0;
  7375. ctx->event_idx = 0;
  7376. for (auto& event : ctx->gc.events) {
  7377. ctx->device->device.resetEvent(event);
  7378. }
  7379. ctx->tensor_ctxs.clear();
  7380. ctx->gc.contexts.clear();
  7381. ctx->device->pipeline_descriptor_set_requirements.clear();
  7382. }
  7383. // Clean up on backend free
  7384. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  7385. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  7386. ggml_vk_graph_cleanup(ctx);
  7387. ggml_vk_destroy_buffer(ctx->prealloc_x);
  7388. ggml_vk_destroy_buffer(ctx->prealloc_y);
  7389. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7390. for (auto& buffer : ctx->buffer_pool) {
  7391. ggml_vk_destroy_buffer(buffer);
  7392. }
  7393. ctx->prealloc_size_x = 0;
  7394. ctx->prealloc_size_y = 0;
  7395. ctx->prealloc_size_split_k = 0;
  7396. for (auto& event : ctx->gc.events) {
  7397. ctx->device->device.destroyEvent(event);
  7398. }
  7399. ctx->gc.events.clear();
  7400. ctx->device->device.destroyFence(ctx->fence);
  7401. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  7402. }
  7403. static int ggml_vk_get_device_count() {
  7404. ggml_vk_instance_init();
  7405. return vk_instance.device_indices.size();
  7406. }
  7407. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  7408. ggml_vk_instance_init();
  7409. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  7410. vk::PhysicalDeviceProperties props;
  7411. devices[device].getProperties(&props);
  7412. snprintf(description, description_size, "%s", props.deviceName.data());
  7413. }
  7414. // backend interface
  7415. #define UNUSED GGML_UNUSED
  7416. // device backend
  7417. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  7418. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  7419. }
  7420. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7421. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  7422. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7423. ggml_vk_destroy_buffer(ctx->dev_buffer);
  7424. delete ctx;
  7425. }
  7426. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  7427. return vk_ptr_base;
  7428. UNUSED(buffer);
  7429. }
  7430. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7431. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  7432. if (tensor->view_src != nullptr) {
  7433. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  7434. }
  7435. return GGML_STATUS_SUCCESS;
  7436. }
  7437. 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) {
  7438. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  7439. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7440. vk_buffer buf = buf_ctx->dev_buffer;
  7441. uint32_t val32 = (uint32_t)value * 0x01010101;
  7442. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  7443. }
  7444. 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) {
  7445. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  7446. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7447. vk_buffer buf = buf_ctx->dev_buffer;
  7448. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7449. }
  7450. 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) {
  7451. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  7452. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7453. vk_buffer buf = buf_ctx->dev_buffer;
  7454. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7455. }
  7456. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  7457. if (ggml_backend_buffer_is_vk(src->buffer)) {
  7458. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  7459. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7460. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  7461. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  7462. 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));
  7463. return true;
  7464. }
  7465. return false;
  7466. UNUSED(buffer);
  7467. }
  7468. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7469. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7470. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  7471. }
  7472. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  7473. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  7474. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  7475. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  7476. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  7477. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  7478. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  7479. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  7480. /* .clear = */ ggml_backend_vk_buffer_clear,
  7481. /* .reset = */ NULL,
  7482. };
  7483. // vk buffer type
  7484. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  7485. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  7486. return ctx->name.c_str();
  7487. }
  7488. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7489. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  7490. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7491. vk_buffer dev_buffer = nullptr;
  7492. try {
  7493. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  7494. } catch (const vk::SystemError& e) {
  7495. return nullptr;
  7496. }
  7497. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  7498. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  7499. }
  7500. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7501. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7502. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  7503. }
  7504. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  7505. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7506. return ctx->device->suballocation_block_size;
  7507. }
  7508. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  7509. return ggml_nbytes(tensor);
  7510. UNUSED(buft);
  7511. }
  7512. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  7513. ggml_vk_instance_init();
  7514. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  7515. vk_device dev = ggml_vk_get_device(dev_num);
  7516. return &dev->buffer_type;
  7517. }
  7518. // host buffer type
  7519. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  7520. return GGML_VK_NAME "_Host";
  7521. UNUSED(buft);
  7522. }
  7523. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  7524. return GGML_VK_NAME "_Host";
  7525. UNUSED(buffer);
  7526. }
  7527. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7528. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  7529. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  7530. }
  7531. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7532. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  7533. size += 32; // Behave like the CPU buffer type
  7534. void * ptr = nullptr;
  7535. try {
  7536. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  7537. } catch (vk::SystemError& e) {
  7538. std::cerr << "ggml_vulkan: Failed to allocate pinned memory." << std::endl;
  7539. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  7540. // fallback to cpu buffer
  7541. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7542. }
  7543. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7544. buffer->buft = buft;
  7545. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  7546. return buffer;
  7547. UNUSED(buft);
  7548. }
  7549. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7550. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  7551. UNUSED(buft);
  7552. }
  7553. // Should be changed to return device-specific host buffer type
  7554. // but that probably requires changes in llama.cpp
  7555. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  7556. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  7557. /* .iface = */ {
  7558. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  7559. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  7560. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  7561. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  7562. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7563. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7564. },
  7565. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  7566. /* .context = */ nullptr,
  7567. };
  7568. // Make sure device 0 is initialized
  7569. ggml_vk_instance_init();
  7570. ggml_vk_get_device(0);
  7571. return &ggml_backend_vk_buffer_type_host;
  7572. }
  7573. // backend
  7574. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  7575. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7576. return ctx->name.c_str();
  7577. }
  7578. static void ggml_backend_vk_free(ggml_backend_t backend) {
  7579. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7580. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  7581. ggml_vk_cleanup(ctx);
  7582. delete ctx;
  7583. delete backend;
  7584. }
  7585. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  7586. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7587. return &ctx->device->buffer_type;
  7588. }
  7589. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7590. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  7591. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7592. 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");
  7593. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7594. vk_context transfer_ctx;
  7595. if (ctx->transfer_ctx.expired()) {
  7596. // Initialize new transfer context
  7597. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7598. ctx->transfer_ctx = transfer_ctx;
  7599. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7600. } else {
  7601. transfer_ctx = ctx->transfer_ctx.lock();
  7602. }
  7603. vk_buffer buf = buf_ctx->dev_buffer;
  7604. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7605. }
  7606. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7607. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  7608. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7609. 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");
  7610. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7611. vk_context transfer_ctx;
  7612. if (ctx->transfer_ctx.expired()) {
  7613. // Initialize new transfer context
  7614. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7615. ctx->transfer_ctx = transfer_ctx;
  7616. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7617. } else {
  7618. transfer_ctx = ctx->transfer_ctx.lock();
  7619. }
  7620. vk_buffer buf = buf_ctx->dev_buffer;
  7621. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7622. }
  7623. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  7624. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  7625. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7626. 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)) {
  7627. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  7628. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7629. vk_context transfer_ctx;
  7630. if (ctx->transfer_ctx.expired()) {
  7631. // Initialize new transfer context
  7632. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7633. ctx->transfer_ctx = transfer_ctx;
  7634. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7635. } else {
  7636. transfer_ctx = ctx->transfer_ctx.lock();
  7637. }
  7638. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  7639. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  7640. 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));
  7641. return true;
  7642. }
  7643. return false;
  7644. }
  7645. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  7646. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  7647. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7648. if(ctx->transfer_ctx.expired()) {
  7649. return;
  7650. }
  7651. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  7652. ggml_vk_ctx_end(transfer_ctx);
  7653. for (auto& cpy : transfer_ctx->in_memcpys) {
  7654. memcpy(cpy.dst, cpy.src, cpy.n);
  7655. }
  7656. ggml_vk_submit(transfer_ctx, ctx->fence);
  7657. ggml_vk_wait_for_fence(ctx);
  7658. for (auto& cpy : transfer_ctx->out_memcpys) {
  7659. memcpy(cpy.dst, cpy.src, cpy.n);
  7660. }
  7661. ctx->transfer_ctx.reset();
  7662. }
  7663. static bool ggml_vk_is_empty(ggml_tensor * node) {
  7664. 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;
  7665. }
  7666. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7667. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  7668. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7669. uint64_t total_mat_mul_bytes = 0;
  7670. for (int i = 0; i < cgraph->n_nodes; i++) {
  7671. ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false, false);
  7672. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  7673. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  7674. }
  7675. }
  7676. if (ctx->device->need_compiles) {
  7677. ggml_vk_load_shaders(ctx->device);
  7678. }
  7679. ggml_vk_preallocate_buffers(ctx);
  7680. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  7681. int last_node = cgraph->n_nodes - 1;
  7682. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  7683. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  7684. last_node -= 1;
  7685. }
  7686. // Reserve tensor context space for all nodes
  7687. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  7688. bool first_node_in_batch = true; // true if next node will be first node in a batch
  7689. int submit_node_idx = 0; // index to first node in a batch
  7690. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  7691. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  7692. // (and scaled down based on model size, so smaller models submit earlier).
  7693. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  7694. int nodes_per_submit = 100;
  7695. int submitted_nodes = 0;
  7696. int submit_count = 0;
  7697. uint64_t mul_mat_bytes = 0;
  7698. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  7699. for (int i = 0; i < cgraph->n_nodes; i++) {
  7700. if (first_node_in_batch) {
  7701. submit_node_idx = i;
  7702. }
  7703. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  7704. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  7705. }
  7706. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  7707. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  7708. bool submit = (submitted_nodes >= nodes_per_submit) ||
  7709. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  7710. (i == last_node) ||
  7711. (almost_ready && !ctx->almost_ready_fence_pending);
  7712. bool enqueued = ggml_vk_build_graph(ctx, cgraph->nodes[i], i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i == last_node, almost_ready, submit);
  7713. if (enqueued) {
  7714. ++submitted_nodes;
  7715. #ifndef GGML_VULKAN_CHECK_RESULTS
  7716. if (first_node_in_batch) {
  7717. first_node_in_batch = false;
  7718. }
  7719. #endif
  7720. }
  7721. if (submit && enqueued) {
  7722. first_node_in_batch = true;
  7723. submitted_nodes = 0;
  7724. mul_mat_bytes = 0;
  7725. if (submit_count < 3) {
  7726. mul_mat_bytes_per_submit *= 2;
  7727. }
  7728. submit_count++;
  7729. }
  7730. }
  7731. #ifdef GGML_VULKAN_PERF
  7732. ctx->device->perf_logger->print_timings();
  7733. #endif
  7734. ggml_vk_graph_cleanup(ctx);
  7735. return GGML_STATUS_SUCCESS;
  7736. UNUSED(backend);
  7737. }
  7738. // TODO: enable async and synchronize
  7739. static ggml_backend_i ggml_backend_vk_interface = {
  7740. /* .get_name = */ ggml_backend_vk_name,
  7741. /* .free = */ ggml_backend_vk_free,
  7742. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  7743. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  7744. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  7745. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  7746. /* .graph_plan_create = */ NULL,
  7747. /* .graph_plan_free = */ NULL,
  7748. /* .graph_plan_update = */ NULL,
  7749. /* .graph_plan_compute = */ NULL,
  7750. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  7751. /* .event_record = */ NULL,
  7752. /* .event_wait = */ NULL,
  7753. };
  7754. static ggml_guid_t ggml_backend_vk_guid() {
  7755. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  7756. return &guid;
  7757. }
  7758. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  7759. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  7760. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  7761. ggml_vk_init(ctx, dev_num);
  7762. ggml_backend_t vk_backend = new ggml_backend {
  7763. /* .guid = */ ggml_backend_vk_guid(),
  7764. /* .interface = */ ggml_backend_vk_interface,
  7765. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  7766. /* .context = */ ctx,
  7767. };
  7768. return vk_backend;
  7769. }
  7770. bool ggml_backend_is_vk(ggml_backend_t backend) {
  7771. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  7772. }
  7773. int ggml_backend_vk_get_device_count() {
  7774. return ggml_vk_get_device_count();
  7775. }
  7776. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  7777. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7778. int dev_idx = vk_instance.device_indices[device];
  7779. ggml_vk_get_device_description(dev_idx, description, description_size);
  7780. }
  7781. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  7782. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7783. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  7784. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  7785. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  7786. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  7787. *total = heap.size;
  7788. *free = heap.size;
  7789. break;
  7790. }
  7791. }
  7792. }
  7793. //////////////////////////
  7794. struct ggml_backend_vk_device_context {
  7795. size_t device;
  7796. std::string name;
  7797. std::string description;
  7798. };
  7799. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  7800. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7801. return ctx->name.c_str();
  7802. }
  7803. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  7804. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7805. return ctx->description.c_str();
  7806. }
  7807. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  7808. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  7809. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  7810. }
  7811. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  7812. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7813. return ggml_backend_vk_buffer_type(ctx->device);
  7814. }
  7815. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  7816. UNUSED(dev);
  7817. return ggml_backend_vk_host_buffer_type();
  7818. }
  7819. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  7820. UNUSED(dev);
  7821. return GGML_BACKEND_DEVICE_TYPE_GPU;
  7822. }
  7823. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  7824. props->name = ggml_backend_vk_device_get_name(dev);
  7825. props->description = ggml_backend_vk_device_get_description(dev);
  7826. props->type = ggml_backend_vk_device_get_type(dev);
  7827. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  7828. props->caps = {
  7829. /* .async = */ false,
  7830. /* .host_buffer = */ true,
  7831. /* .buffer_from_host_ptr = */ false,
  7832. /* .events = */ false,
  7833. };
  7834. }
  7835. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  7836. UNUSED(params);
  7837. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7838. return ggml_backend_vk_init(ctx->device);
  7839. }
  7840. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7841. switch (op->op) {
  7842. case GGML_OP_UNARY:
  7843. switch (ggml_get_unary_op(op)) {
  7844. case GGML_UNARY_OP_GELU:
  7845. case GGML_UNARY_OP_GELU_QUICK:
  7846. case GGML_UNARY_OP_SILU:
  7847. case GGML_UNARY_OP_RELU:
  7848. case GGML_UNARY_OP_TANH:
  7849. case GGML_UNARY_OP_SIGMOID:
  7850. return ggml_is_contiguous(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
  7851. default:
  7852. return false;
  7853. }
  7854. break;
  7855. case GGML_OP_MUL_MAT:
  7856. case GGML_OP_MUL_MAT_ID:
  7857. {
  7858. ggml_type src0_type = op->src[0]->type;
  7859. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7860. const vk_device& device = ggml_vk_get_device(ctx->device);
  7861. if (op->op == GGML_OP_MUL_MAT_ID && !device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  7862. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  7863. return false;
  7864. }
  7865. switch (src0_type) {
  7866. case GGML_TYPE_F32:
  7867. case GGML_TYPE_F16:
  7868. case GGML_TYPE_Q4_0:
  7869. case GGML_TYPE_Q4_1:
  7870. case GGML_TYPE_Q5_0:
  7871. case GGML_TYPE_Q5_1:
  7872. case GGML_TYPE_Q8_0:
  7873. case GGML_TYPE_Q2_K:
  7874. case GGML_TYPE_Q3_K:
  7875. case GGML_TYPE_Q4_K:
  7876. case GGML_TYPE_Q5_K:
  7877. case GGML_TYPE_Q6_K:
  7878. case GGML_TYPE_IQ1_S:
  7879. case GGML_TYPE_IQ1_M:
  7880. case GGML_TYPE_IQ2_XXS:
  7881. case GGML_TYPE_IQ2_XS:
  7882. case GGML_TYPE_IQ2_S:
  7883. case GGML_TYPE_IQ3_XXS:
  7884. case GGML_TYPE_IQ3_S:
  7885. case GGML_TYPE_IQ4_XS:
  7886. case GGML_TYPE_IQ4_NL:
  7887. break;
  7888. default:
  7889. return false;
  7890. }
  7891. struct ggml_tensor * a;
  7892. struct ggml_tensor * b;
  7893. if (op->op == GGML_OP_MUL_MAT) {
  7894. a = op->src[0];
  7895. b = op->src[1];
  7896. } else {
  7897. a = op->src[2];
  7898. b = op->src[1];
  7899. }
  7900. if (a->ne[3] != b->ne[3]) {
  7901. return false;
  7902. }
  7903. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) ||
  7904. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  7905. return false;
  7906. }
  7907. return true;
  7908. } break;
  7909. case GGML_OP_FLASH_ATTN_EXT:
  7910. {
  7911. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7912. if (!ggml_vk_get_device(ctx->device)->coopmat2) {
  7913. return false;
  7914. }
  7915. switch (op->src[0]->ne[0]) {
  7916. case 64:
  7917. case 80:
  7918. case 96:
  7919. case 112:
  7920. case 128:
  7921. case 256:
  7922. case 575: // DeepSeek MLA
  7923. break;
  7924. default:
  7925. return false;
  7926. }
  7927. if (op->src[1]->ne[0] != op->src[2]->ne[0]) {
  7928. // different head sizes of K and V are not supported yet
  7929. return false;
  7930. }
  7931. if (op->src[0]->type != GGML_TYPE_F32) {
  7932. return false;
  7933. }
  7934. if (op->type != GGML_TYPE_F32) {
  7935. return false;
  7936. }
  7937. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  7938. return false;
  7939. }
  7940. // It's straightforward to support different K/V dequant, but would
  7941. // significantly increase the number of pipelines
  7942. if (op->src[1]->type != op->src[2]->type) {
  7943. return false;
  7944. }
  7945. switch (op->src[1]->type) {
  7946. case GGML_TYPE_F16:
  7947. case GGML_TYPE_Q4_0:
  7948. case GGML_TYPE_Q4_1:
  7949. case GGML_TYPE_Q5_0:
  7950. case GGML_TYPE_Q5_1:
  7951. case GGML_TYPE_Q8_0:
  7952. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  7953. //case GGML_TYPE_Q2_K:
  7954. //case GGML_TYPE_Q3_K:
  7955. //case GGML_TYPE_Q4_K:
  7956. //case GGML_TYPE_Q5_K:
  7957. //case GGML_TYPE_Q6_K:
  7958. //case GGML_TYPE_IQ1_S:
  7959. //case GGML_TYPE_IQ1_M:
  7960. //case GGML_TYPE_IQ2_XXS:
  7961. //case GGML_TYPE_IQ2_XS:
  7962. //case GGML_TYPE_IQ2_S:
  7963. //case GGML_TYPE_IQ3_XXS:
  7964. //case GGML_TYPE_IQ3_S:
  7965. //case GGML_TYPE_IQ4_XS:
  7966. case GGML_TYPE_IQ4_NL:
  7967. break;
  7968. default:
  7969. return false;
  7970. }
  7971. return true;
  7972. }
  7973. case GGML_OP_GET_ROWS:
  7974. {
  7975. switch (op->src[0]->type) {
  7976. case GGML_TYPE_F32:
  7977. case GGML_TYPE_F16:
  7978. case GGML_TYPE_Q4_0:
  7979. case GGML_TYPE_Q4_1:
  7980. case GGML_TYPE_Q5_0:
  7981. case GGML_TYPE_Q5_1:
  7982. case GGML_TYPE_Q8_0:
  7983. case GGML_TYPE_IQ1_S:
  7984. case GGML_TYPE_IQ1_M:
  7985. case GGML_TYPE_IQ2_XXS:
  7986. case GGML_TYPE_IQ2_XS:
  7987. case GGML_TYPE_IQ2_S:
  7988. case GGML_TYPE_IQ3_XXS:
  7989. case GGML_TYPE_IQ3_S:
  7990. case GGML_TYPE_IQ4_XS:
  7991. case GGML_TYPE_IQ4_NL:
  7992. return true;
  7993. default:
  7994. return false;
  7995. }
  7996. } break;
  7997. case GGML_OP_CONT:
  7998. case GGML_OP_CPY:
  7999. case GGML_OP_DUP:
  8000. {
  8001. ggml_type src0_type = op->src[0]->type;
  8002. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  8003. if (src0_type == GGML_TYPE_F32) {
  8004. switch (src1_type) {
  8005. case GGML_TYPE_F32:
  8006. case GGML_TYPE_F16:
  8007. case GGML_TYPE_Q4_0:
  8008. case GGML_TYPE_Q4_1:
  8009. case GGML_TYPE_Q5_0:
  8010. case GGML_TYPE_Q5_1:
  8011. case GGML_TYPE_Q8_0:
  8012. case GGML_TYPE_IQ4_NL:
  8013. return true;
  8014. default:
  8015. break;
  8016. }
  8017. }
  8018. if (src1_type == GGML_TYPE_F32) {
  8019. switch (src0_type) {
  8020. case GGML_TYPE_Q4_0:
  8021. case GGML_TYPE_Q4_1:
  8022. case GGML_TYPE_Q5_0:
  8023. case GGML_TYPE_Q5_1:
  8024. case GGML_TYPE_Q8_0:
  8025. case GGML_TYPE_IQ4_NL:
  8026. return true;
  8027. default:
  8028. break;
  8029. }
  8030. }
  8031. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8032. return true;
  8033. }
  8034. return false;
  8035. } break;
  8036. case GGML_OP_REPEAT:
  8037. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  8038. case GGML_OP_REPEAT_BACK:
  8039. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  8040. case GGML_OP_ROPE:
  8041. case GGML_OP_ROPE_BACK:
  8042. case GGML_OP_NONE:
  8043. case GGML_OP_RESHAPE:
  8044. case GGML_OP_VIEW:
  8045. case GGML_OP_PERMUTE:
  8046. case GGML_OP_TRANSPOSE:
  8047. case GGML_OP_RMS_NORM:
  8048. return true;
  8049. case GGML_OP_NORM:
  8050. case GGML_OP_GROUP_NORM:
  8051. case GGML_OP_L2_NORM:
  8052. return ggml_is_contiguous(op->src[0]);
  8053. case GGML_OP_ADD:
  8054. case GGML_OP_SUB:
  8055. case GGML_OP_MUL:
  8056. case GGML_OP_DIV:
  8057. case GGML_OP_SILU_BACK:
  8058. case GGML_OP_RMS_NORM_BACK:
  8059. case GGML_OP_SQR:
  8060. case GGML_OP_SIN:
  8061. case GGML_OP_COS:
  8062. case GGML_OP_CLAMP:
  8063. return op->src[0]->type == GGML_TYPE_F32;
  8064. case GGML_OP_UPSCALE:
  8065. return op->op_params[0] == GGML_SCALE_MODE_NEAREST;
  8066. case GGML_OP_ACC:
  8067. case GGML_OP_CONCAT:
  8068. case GGML_OP_SCALE:
  8069. case GGML_OP_PAD:
  8070. case GGML_OP_DIAG_MASK_INF:
  8071. case GGML_OP_SOFT_MAX:
  8072. case GGML_OP_SOFT_MAX_BACK:
  8073. case GGML_OP_ARGSORT:
  8074. case GGML_OP_SUM:
  8075. case GGML_OP_SUM_ROWS:
  8076. case GGML_OP_ARGMAX:
  8077. case GGML_OP_COUNT_EQUAL:
  8078. case GGML_OP_IM2COL:
  8079. case GGML_OP_TIMESTEP_EMBEDDING:
  8080. case GGML_OP_POOL_2D:
  8081. case GGML_OP_RWKV_WKV6:
  8082. case GGML_OP_RWKV_WKV7:
  8083. case GGML_OP_LEAKY_RELU:
  8084. case GGML_OP_OPT_STEP_ADAMW:
  8085. return true;
  8086. default:
  8087. return false;
  8088. }
  8089. UNUSED(dev);
  8090. }
  8091. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  8092. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  8093. return false;
  8094. }
  8095. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8096. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8097. return buft_ctx->device->idx == ctx->device;
  8098. }
  8099. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  8100. const int min_batch_size = 32;
  8101. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  8102. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  8103. UNUSED(dev);
  8104. }
  8105. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  8106. /* .get_name = */ ggml_backend_vk_device_get_name,
  8107. /* .get_description = */ ggml_backend_vk_device_get_description,
  8108. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  8109. /* .get_type = */ ggml_backend_vk_device_get_type,
  8110. /* .get_props = */ ggml_backend_vk_device_get_props,
  8111. /* .init_backend = */ ggml_backend_vk_device_init,
  8112. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  8113. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  8114. /* .buffer_from_host_ptr = */ NULL,
  8115. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  8116. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  8117. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  8118. /* .event_new = */ NULL,
  8119. /* .event_free = */ NULL,
  8120. /* .event_synchronize = */ NULL,
  8121. };
  8122. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  8123. UNUSED(reg);
  8124. return GGML_VK_NAME;
  8125. }
  8126. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  8127. UNUSED(reg);
  8128. return ggml_backend_vk_get_device_count();
  8129. }
  8130. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  8131. static std::vector<ggml_backend_dev_t> devices;
  8132. static bool initialized = false;
  8133. {
  8134. static std::mutex mutex;
  8135. std::lock_guard<std::mutex> lock(mutex);
  8136. if (!initialized) {
  8137. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  8138. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  8139. char desc[256];
  8140. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  8141. ctx->device = i;
  8142. ctx->name = GGML_VK_NAME + std::to_string(i);
  8143. ctx->description = desc;
  8144. devices.push_back(new ggml_backend_device {
  8145. /* .iface = */ ggml_backend_vk_device_i,
  8146. /* .reg = */ reg,
  8147. /* .context = */ ctx,
  8148. });
  8149. }
  8150. initialized = true;
  8151. }
  8152. }
  8153. GGML_ASSERT(device < devices.size());
  8154. return devices[device];
  8155. }
  8156. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  8157. /* .get_name = */ ggml_backend_vk_reg_get_name,
  8158. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  8159. /* .get_device = */ ggml_backend_vk_reg_get_device,
  8160. /* .get_proc_address = */ NULL,
  8161. };
  8162. ggml_backend_reg_t ggml_backend_vk_reg() {
  8163. static ggml_backend_reg reg = {
  8164. /* .api_version = */ GGML_BACKEND_API_VERSION,
  8165. /* .iface = */ ggml_backend_vk_reg_i,
  8166. /* .context = */ nullptr,
  8167. };
  8168. try {
  8169. ggml_vk_instance_init();
  8170. return &reg;
  8171. } catch (const vk::SystemError& e) {
  8172. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  8173. return nullptr;
  8174. }
  8175. }
  8176. // Extension availability
  8177. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  8178. #ifdef GGML_VULKAN_VALIDATE
  8179. bool portability_enumeration_ext = false;
  8180. // Check for portability enumeration extension for MoltenVK support
  8181. for (const auto& properties : instance_extensions) {
  8182. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  8183. return true;
  8184. }
  8185. }
  8186. if (!portability_enumeration_ext) {
  8187. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  8188. }
  8189. #endif
  8190. return false;
  8191. UNUSED(instance_extensions);
  8192. }
  8193. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  8194. #ifdef __APPLE__
  8195. bool portability_enumeration_ext = false;
  8196. // Check for portability enumeration extension for MoltenVK support
  8197. for (const auto& properties : instance_extensions) {
  8198. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  8199. return true;
  8200. }
  8201. }
  8202. if (!portability_enumeration_ext) {
  8203. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  8204. }
  8205. #endif
  8206. return false;
  8207. UNUSED(instance_extensions);
  8208. }
  8209. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  8210. switch (props.vendorID) {
  8211. case VK_VENDOR_ID_INTEL:
  8212. // Intel drivers don't support coopmat properly yet
  8213. return false;
  8214. case VK_VENDOR_ID_AMD:
  8215. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  8216. // Workaround for AMD proprietary driver reporting support on all GPUs
  8217. return arch == vk_device_architecture::AMD_RDNA3;
  8218. }
  8219. return true;
  8220. default:
  8221. return true;
  8222. }
  8223. }
  8224. // checks
  8225. #ifdef GGML_VULKAN_CHECK_RESULTS
  8226. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  8227. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  8228. return;
  8229. }
  8230. for (int j = 0; j < level; j++) {
  8231. std::cerr << " ";
  8232. }
  8233. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  8234. done.push_back(tensor);
  8235. for (int i = 0; i < GGML_MAX_SRC; i++) {
  8236. if (tensor->src[i] != nullptr) {
  8237. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  8238. }
  8239. }
  8240. }
  8241. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  8242. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  8243. return;
  8244. }
  8245. i0 = std::max(i0, 5);
  8246. i1 = std::max(i1, 5);
  8247. i2 = std::max(i2, 0);
  8248. i3 = std::max(i3, 0);
  8249. fprintf(stderr, " ");
  8250. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8251. fprintf(stderr, "%7d ", idx1);
  8252. }
  8253. fprintf(stderr, "\n");
  8254. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  8255. fprintf(stderr, "%7d: ", idx0);
  8256. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8257. 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]) {
  8258. float val;
  8259. if (tensor->type == GGML_TYPE_F32) {
  8260. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  8261. } else if (tensor->type == GGML_TYPE_F16) {
  8262. 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]));
  8263. } else if (tensor->type == GGML_TYPE_I32) {
  8264. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  8265. } else {
  8266. GGML_ABORT("fatal error");
  8267. }
  8268. fprintf(stderr, "% 7.2f ", val);
  8269. } else {
  8270. fprintf(stderr, " ");
  8271. }
  8272. }
  8273. fprintf(stderr, "\n");
  8274. }
  8275. }
  8276. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  8277. void * tensor_data = tensor->data;
  8278. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  8279. if (is_gpu) {
  8280. const size_t tensor_size = ggml_nbytes(tensor);
  8281. tensor_data = malloc(tensor_size);
  8282. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8283. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  8284. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  8285. }
  8286. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  8287. 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;
  8288. if (tensor->src[0] != nullptr) {
  8289. 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;
  8290. }
  8291. if (tensor->src[1] != nullptr) {
  8292. 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;
  8293. }
  8294. std::cerr << std::endl << "Result:" << std::endl;
  8295. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  8296. std::cerr << std::endl;
  8297. std::vector<const ggml_tensor *> done;
  8298. ggml_vk_print_graph_origin(tensor, done);
  8299. if (is_gpu) {
  8300. free(tensor_data);
  8301. }
  8302. }
  8303. void * comp_result;
  8304. size_t comp_size;
  8305. size_t comp_nb[GGML_MAX_DIMS];
  8306. size_t check_counter = 0;
  8307. static void ggml_vk_check_results_0(ggml_tensor * tensor) {
  8308. if (tensor->op == GGML_OP_TRANSPOSE) {
  8309. return;
  8310. }
  8311. check_counter++;
  8312. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  8313. return;
  8314. }
  8315. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  8316. ggml_tensor * src0 = tensor->src[0];
  8317. ggml_tensor * src1 = tensor->src[1];
  8318. struct ggml_init_params iparams = {
  8319. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  8320. /*.mem_buffer =*/ NULL,
  8321. /*.no_alloc =*/ false,
  8322. };
  8323. struct ggml_context * ggml_ctx = ggml_init(iparams);
  8324. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  8325. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  8326. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  8327. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  8328. struct ggml_tensor * tensor_clone = nullptr;
  8329. for (int i = 0; i < 6; i++) {
  8330. ggml_tensor * srci = tensor->src[i];
  8331. if (srci == nullptr) {
  8332. continue;
  8333. }
  8334. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  8335. size_t srci_size = ggml_nbytes(srci);
  8336. src_clone[i] = srci_clone;
  8337. src_size[i] = ggml_nbytes(srci);
  8338. src_buffer[i] = malloc(srci_size);
  8339. srci_clone->data = src_buffer[i];
  8340. if (ggml_backend_buffer_is_host(srci->buffer)) {
  8341. memcpy(srci_clone->data, srci->data, srci_size);
  8342. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8343. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  8344. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  8345. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  8346. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  8347. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  8348. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  8349. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  8350. const int idx = i3*srci->ne[2] + i2;
  8351. 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]);
  8352. }
  8353. }
  8354. srci_clone->nb[0] = srci->nb[0];
  8355. srci_clone->nb[1] = srci->nb[1];
  8356. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  8357. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  8358. }
  8359. } else {
  8360. if (offset + srci_size >= buffer_gpu->size) {
  8361. srci_size = buffer_gpu->size - offset;
  8362. }
  8363. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  8364. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8365. }
  8366. } else {
  8367. GGML_ABORT("fatal error");
  8368. }
  8369. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8370. ggml_vk_print_tensor(srci, srci_name[i]);
  8371. }
  8372. }
  8373. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  8374. const float * params = (const float *)tensor->op_params;
  8375. 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]);
  8376. } else if (tensor->op == GGML_OP_MUL_MAT) {
  8377. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  8378. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  8379. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  8380. } else if (tensor->op == GGML_OP_SUB) {
  8381. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  8382. } else if (tensor->op == GGML_OP_MUL) {
  8383. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  8384. } else if (tensor->op == GGML_OP_DIV) {
  8385. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  8386. } else if (tensor->op == GGML_OP_CONCAT) {
  8387. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  8388. } else if (tensor->op == GGML_OP_UPSCALE) {
  8389. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], tensor->op_params[0], tensor->op_params[1], (ggml_scale_mode) tensor->op_params[0]);
  8390. } else if (tensor->op == GGML_OP_SCALE) {
  8391. const float * params = (const float *)tensor->op_params;
  8392. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], params[0]);
  8393. } else if (tensor->op == GGML_OP_SQR) {
  8394. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  8395. } else if (tensor->op == GGML_OP_SIN) {
  8396. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  8397. } else if (tensor->op == GGML_OP_COS) {
  8398. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  8399. } else if (tensor->op == GGML_OP_CLAMP) {
  8400. const float * params = (const float *)tensor->op_params;
  8401. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  8402. } else if (tensor->op == GGML_OP_PAD) {
  8403. 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]);
  8404. } else if (tensor->op == GGML_OP_REPEAT) {
  8405. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  8406. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  8407. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  8408. } else if (tensor->op == GGML_OP_ADD) {
  8409. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  8410. } else if (tensor->op == GGML_OP_ACC) {
  8411. 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]);
  8412. } else if (tensor->op == GGML_OP_NORM) {
  8413. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  8414. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  8415. const float * float_params = (const float *)tensor->op_params;
  8416. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  8417. } else if (tensor->op == GGML_OP_RMS_NORM) {
  8418. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  8419. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  8420. const float eps = ((float *) tensor->op_params)[0];
  8421. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  8422. } else if (tensor->op == GGML_OP_SILU_BACK) {
  8423. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  8424. } else if (tensor->op == GGML_OP_L2_NORM) {
  8425. const float eps = ((float *) tensor->op_params)[0];
  8426. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  8427. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  8428. if (src1 != nullptr) {
  8429. const float * params = (const float *)tensor->op_params;
  8430. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  8431. } else {
  8432. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  8433. }
  8434. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  8435. 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]);
  8436. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  8437. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  8438. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  8439. const int n_dims = ((int32_t *) tensor->op_params)[1];
  8440. const int mode = ((int32_t *) tensor->op_params)[2];
  8441. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  8442. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  8443. const float freq_base = ((float *) tensor->op_params)[5];
  8444. const float freq_scale = ((float *) tensor->op_params)[6];
  8445. const float ext_factor = ((float *) tensor->op_params)[7];
  8446. const float attn_factor = ((float *) tensor->op_params)[8];
  8447. const float beta_fast = ((float *) tensor->op_params)[9];
  8448. const float beta_slow = ((float *) tensor->op_params)[10];
  8449. if (mode & GGML_ROPE_TYPE_MROPE) {
  8450. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  8451. if (tensor->op == GGML_OP_ROPE) {
  8452. 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);
  8453. } else {
  8454. 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);
  8455. }
  8456. } else {
  8457. if (tensor->op == GGML_OP_ROPE) {
  8458. 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);
  8459. } else {
  8460. 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);
  8461. }
  8462. }
  8463. } else if (tensor->op == GGML_OP_UNARY) {
  8464. switch (ggml_get_unary_op(tensor)) {
  8465. case GGML_UNARY_OP_SILU:
  8466. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  8467. break;
  8468. case GGML_UNARY_OP_GELU:
  8469. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  8470. break;
  8471. case GGML_UNARY_OP_GELU_QUICK:
  8472. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  8473. break;
  8474. case GGML_UNARY_OP_RELU:
  8475. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  8476. break;
  8477. case GGML_UNARY_OP_TANH:
  8478. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  8479. break;
  8480. case GGML_UNARY_OP_SIGMOID:
  8481. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  8482. break;
  8483. default:
  8484. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  8485. GGML_ABORT("fatal error");
  8486. }
  8487. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  8488. if (src1 == nullptr) {
  8489. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  8490. tensor_clone->type = tensor->type;
  8491. } else {
  8492. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  8493. }
  8494. } else if (tensor->op == GGML_OP_CONT) {
  8495. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  8496. } else if (tensor->op == GGML_OP_RESHAPE) {
  8497. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  8498. } else if (tensor->op == GGML_OP_VIEW) {
  8499. 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]);
  8500. } else if (tensor->op == GGML_OP_PERMUTE) {
  8501. int32_t * params = (int32_t *)tensor->op_params;
  8502. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  8503. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  8504. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  8505. } else if (tensor->op == GGML_OP_GET_ROWS) {
  8506. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  8507. } else if (tensor->op == GGML_OP_ARGSORT) {
  8508. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  8509. } else if (tensor->op == GGML_OP_SUM) {
  8510. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  8511. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  8512. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  8513. } else if (tensor->op == GGML_OP_ARGMAX) {
  8514. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  8515. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  8516. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  8517. } else if (tensor->op == GGML_OP_IM2COL) {
  8518. const int32_t s0 = tensor->op_params[0];
  8519. const int32_t s1 = tensor->op_params[1];
  8520. const int32_t p0 = tensor->op_params[2];
  8521. const int32_t p1 = tensor->op_params[3];
  8522. const int32_t d0 = tensor->op_params[4];
  8523. const int32_t d1 = tensor->op_params[5];
  8524. const bool is_2D = tensor->op_params[6] == 1;
  8525. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  8526. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  8527. const int32_t dim = tensor->op_params[0];
  8528. const int32_t max_period = tensor->op_params[1];
  8529. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  8530. } else if (tensor->op == GGML_OP_POOL_2D) {
  8531. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  8532. const int32_t k0 = tensor->op_params[1];
  8533. const int32_t k1 = tensor->op_params[2];
  8534. const int32_t s0 = tensor->op_params[3];
  8535. const int32_t s1 = tensor->op_params[4];
  8536. const int32_t p0 = tensor->op_params[5];
  8537. const int32_t p1 = tensor->op_params[6];
  8538. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  8539. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  8540. const float * op_params = (const float *)tensor->op_params;
  8541. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  8542. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  8543. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  8544. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  8545. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  8546. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  8547. src_clone[4], src_clone[5], src_clone[6]);
  8548. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  8549. src_clone[0]->flags = src0->flags;
  8550. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  8551. src_clone[2], src_clone[3], src_clone[4]);
  8552. }
  8553. else {
  8554. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  8555. GGML_ABORT("fatal error");
  8556. }
  8557. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8558. ggml_build_forward_expand(cgraph, tensor_clone);
  8559. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  8560. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8561. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  8562. }
  8563. comp_size = ggml_nbytes(tensor_clone);
  8564. comp_result = malloc(comp_size);
  8565. memcpy(comp_result, tensor_clone->data, comp_size);
  8566. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8567. for (int i = 0; i < 6; i++) {
  8568. if (src_buffer[i] != nullptr) {
  8569. free(src_buffer[i]);
  8570. }
  8571. }
  8572. ggml_free(ggml_ctx);
  8573. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  8574. }
  8575. static void ggml_vk_check_results_1(ggml_tensor * tensor) {
  8576. if (tensor->op == GGML_OP_TRANSPOSE) {
  8577. return;
  8578. }
  8579. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  8580. return;
  8581. }
  8582. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  8583. ggml_tensor * src0 = tensor->src[0];
  8584. ggml_tensor * src1 = tensor->src[1];
  8585. ggml_tensor * src2 = tensor->src[2];
  8586. ggml_tensor * src3 = tensor->src[3];
  8587. void * tensor_data = tensor->data;
  8588. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8589. size_t tensor_size = ggml_nbytes(tensor);
  8590. tensor_data = malloc(tensor_size);
  8591. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8592. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  8593. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  8594. if (offset + tensor_size >= buffer_gpu->size) {
  8595. tensor_size = buffer_gpu->size - offset;
  8596. }
  8597. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  8598. }
  8599. float first_error_result = -1.0f;
  8600. float first_error_correct = -1.0f;
  8601. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  8602. double avg_err = 0.0;
  8603. size_t counter = 0;
  8604. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  8605. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  8606. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  8607. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  8608. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  8609. float correct = 0.0f;
  8610. float result = 0.0f;
  8611. if (buffer_size_fit) {
  8612. if (tensor->type == GGML_TYPE_F32) {
  8613. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8614. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8615. } else if (tensor->type == GGML_TYPE_F16) {
  8616. 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]));
  8617. 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]));
  8618. } else if (tensor->type == GGML_TYPE_I32) {
  8619. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8620. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8621. } else if (tensor->type == GGML_TYPE_I64) {
  8622. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8623. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8624. } else {
  8625. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  8626. }
  8627. } else {
  8628. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  8629. GGML_ABORT("fatal error");
  8630. }
  8631. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  8632. 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;
  8633. 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;
  8634. if (src0 != nullptr) {
  8635. 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;
  8636. }
  8637. if (src1 != nullptr) {
  8638. 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;
  8639. }
  8640. if (src2 != nullptr) {
  8641. 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;
  8642. }
  8643. if (src3 != nullptr) {
  8644. 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;
  8645. }
  8646. 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;
  8647. std::cerr << std::endl << "Result:" << std::endl;
  8648. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  8649. std::cerr << std::endl << "Correct:" << std::endl;
  8650. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  8651. std::cerr << std::endl;
  8652. std::vector<const ggml_tensor *> done;
  8653. ggml_vk_print_graph_origin(tensor, done);
  8654. GGML_ABORT("fatal error");
  8655. }
  8656. if (first_error[0] == -1 && std::fabs(correct - result) > 0.1f) {
  8657. first_error[0] = i0;
  8658. first_error[1] = i1;
  8659. first_error[2] = i2;
  8660. first_error[3] = i3;
  8661. first_error_result = result;
  8662. first_error_correct = correct;
  8663. }
  8664. // Special case, value is infinite, avoid NaN result in avg_err
  8665. // NaN also appears in results, if both are nan error is 0
  8666. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  8667. avg_err += std::fabs(correct - result);
  8668. }
  8669. counter++;
  8670. }
  8671. }
  8672. }
  8673. }
  8674. avg_err /= counter;
  8675. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8676. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8677. 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;
  8678. if (src0 != nullptr) {
  8679. 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;
  8680. }
  8681. if (src1 != nullptr) {
  8682. 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;
  8683. }
  8684. if (src2 != nullptr) {
  8685. 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;
  8686. }
  8687. if (src3 != nullptr) {
  8688. 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;
  8689. }
  8690. 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;
  8691. std::cerr << std::endl << "Result:" << std::endl;
  8692. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  8693. std::cerr << std::endl << "Correct:" << std::endl;
  8694. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  8695. std::cerr << std::endl;
  8696. std::vector<const ggml_tensor *> done;
  8697. ggml_vk_print_graph_origin(tensor, done);
  8698. }
  8699. if (avg_err > 0.05 || std::isnan(avg_err)) {
  8700. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8701. 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;
  8702. if (src0 != nullptr) {
  8703. 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;
  8704. }
  8705. if (src1 != nullptr) {
  8706. 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;
  8707. }
  8708. if (src2 != nullptr) {
  8709. 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;
  8710. }
  8711. if (src3 != nullptr) {
  8712. 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;
  8713. }
  8714. 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;
  8715. std::cerr << std::endl << "Result:" << std::endl;
  8716. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  8717. std::cerr << std::endl << "Correct:" << std::endl;
  8718. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  8719. std::cerr << std::endl;
  8720. std::vector<const ggml_tensor *> done;
  8721. ggml_vk_print_graph_origin(tensor, done);
  8722. GGML_ABORT("fatal error");
  8723. } else {
  8724. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  8725. }
  8726. free(comp_result);
  8727. comp_result = nullptr;
  8728. comp_size = 0;
  8729. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8730. free(tensor_data);
  8731. }
  8732. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  8733. }
  8734. #endif
  8735. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)