ggml-vulkan.cpp 388 KB

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