1
0

ggml-vulkan.cpp 310 KB

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