ggml-vulkan.cpp 391 KB

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