ggml-vulkan.cpp 410 KB

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