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