ggml-vulkan.cpp 417 KB

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