ggml-vulkan.cpp 597 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #if defined(_MSC_VER)
  25. # define NOMINMAX 1
  26. # include <windows.h>
  27. # define YIELD() YieldProcessor()
  28. #elif defined(__clang__) || defined(__GNUC__)
  29. # if defined(__x86_64__) ||defined(__i386__)
  30. # include <immintrin.h>
  31. # define YIELD() _mm_pause()
  32. # elif defined(__arm__) || defined(__aarch64__)
  33. # if defined(__clang__)
  34. # include <arm_acle.h>
  35. # define YIELD() __yield()
  36. # else
  37. # define YIELD() asm volatile("yield")
  38. # endif
  39. # endif
  40. #endif
  41. #if !defined(YIELD)
  42. #define YIELD()
  43. #endif
  44. #include "ggml-impl.h"
  45. #include "ggml-backend-impl.h"
  46. #include "ggml-vulkan-shaders.hpp"
  47. // remove this once it's more widely available in the SDK
  48. #if !defined(VK_KHR_shader_bfloat16)
  49. #define VK_KHR_shader_bfloat16 1
  50. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  51. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  52. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  53. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  54. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  55. VkStructureType sType;
  56. void* pNext;
  57. VkBool32 shaderBFloat16Type;
  58. VkBool32 shaderBFloat16DotProduct;
  59. VkBool32 shaderBFloat16CooperativeMatrix;
  60. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  61. #endif
  62. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  63. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  64. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  65. #define VK_VENDOR_ID_AMD 0x1002
  66. #define VK_VENDOR_ID_APPLE 0x106b
  67. #define VK_VENDOR_ID_INTEL 0x8086
  68. #define VK_VENDOR_ID_NVIDIA 0x10de
  69. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  70. #define GGML_VK_MAX_NODES 8192
  71. #define MAX_VK_BUFFERS 256
  72. #define VK_CHECK(err, msg) \
  73. do { \
  74. vk::Result err_ = (err); \
  75. if (err_ != vk::Result::eSuccess) { \
  76. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  77. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  78. exit(1); \
  79. } \
  80. } while (0)
  81. #ifdef GGML_VULKAN_DEBUG
  82. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  83. #else
  84. #define VK_LOG_DEBUG(msg) ((void) 0)
  85. #endif // GGML_VULKAN_DEBUG
  86. struct ggml_backend_vk_context;
  87. #define MAX_PARAMETER_COUNT 8
  88. struct vk_pipeline_struct {
  89. std::string name;
  90. vk::ShaderModule shader_module;
  91. vk::PipelineLayout layout;
  92. vk::Pipeline pipeline;
  93. uint32_t push_constant_size;
  94. uint32_t parameter_count;
  95. std::array<uint32_t, 3> wg_denoms;
  96. uint32_t align;
  97. // set to true to request the pipeline is compiled after the dryrun
  98. bool needed {};
  99. // set to true when the shader has been compiled
  100. bool compiled {};
  101. };
  102. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  103. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  104. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  105. struct vk_matmul_pipeline_struct {
  106. vk_pipeline l, m, s;
  107. vk_pipeline a_l, a_m, a_s;
  108. };
  109. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  110. struct vk_matmul_pipeline2 {
  111. vk_matmul_pipeline2() {
  112. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  113. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  114. }
  115. vk_matmul_pipeline f32acc;
  116. vk_matmul_pipeline f16acc;
  117. };
  118. struct vk_device_struct;
  119. typedef std::shared_ptr<vk_device_struct> vk_device;
  120. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  121. struct vk_buffer_struct;
  122. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  123. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  124. struct ggml_backend_vk_buffer_type_context {
  125. std::string name;
  126. vk_device device;
  127. };
  128. struct vk_queue;
  129. // Stores command pool/buffers. There's an instance of this
  130. // for each (context,queue) pair and for each (device,queue) pair.
  131. struct vk_command_pool {
  132. void init(vk_device& device, vk_queue *q_);
  133. void destroy(vk::Device& device);
  134. vk::CommandPool pool;
  135. uint32_t cmd_buffer_idx;
  136. std::vector<vk::CommandBuffer> cmd_buffers;
  137. vk_queue *q;
  138. };
  139. // Prevent simultaneous submissions to the same queue.
  140. // This could be per vk_queue if we stopped having two vk_queue structures
  141. // sharing the same vk::Queue.
  142. static std::mutex queue_mutex;
  143. struct vk_queue {
  144. uint32_t queue_family_index;
  145. vk::Queue queue;
  146. vk_command_pool cmd_pool;
  147. vk::PipelineStageFlags stage_flags;
  148. bool transfer_only;
  149. // copy everything except the cmd_pool
  150. void copyFrom(vk_queue &other) {
  151. queue_family_index = other.queue_family_index;
  152. queue = other.queue;
  153. stage_flags = other.stage_flags;
  154. transfer_only = other.transfer_only;
  155. }
  156. };
  157. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  158. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  159. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  160. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  161. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  162. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  163. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  164. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  165. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  166. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  167. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  168. /* .is_host = */ NULL,
  169. };
  170. #ifdef GGML_VULKAN_MEMORY_DEBUG
  171. class vk_memory_logger;
  172. #endif
  173. class vk_perf_logger;
  174. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  175. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  176. static constexpr uint32_t p021_max_gqa_ratio = 8;
  177. enum vk_device_architecture {
  178. OTHER,
  179. AMD_GCN,
  180. AMD_RDNA1,
  181. AMD_RDNA2,
  182. AMD_RDNA3,
  183. INTEL_XE2,
  184. NVIDIA_PRE_TURING,
  185. };
  186. // HSK x HSV
  187. enum FaHeadSizes {
  188. FA_HEAD_SIZE_64,
  189. FA_HEAD_SIZE_80,
  190. FA_HEAD_SIZE_96,
  191. FA_HEAD_SIZE_112,
  192. FA_HEAD_SIZE_128,
  193. FA_HEAD_SIZE_192,
  194. FA_HEAD_SIZE_192_128,
  195. FA_HEAD_SIZE_256,
  196. FA_HEAD_SIZE_576_512,
  197. FA_HEAD_SIZE_UNSUPPORTED,
  198. FA_HEAD_SIZE_COUNT = FA_HEAD_SIZE_UNSUPPORTED,
  199. };
  200. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  201. vk::PhysicalDeviceProperties props = device.getProperties();
  202. if (props.vendorID == VK_VENDOR_ID_AMD) {
  203. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  204. bool amd_shader_core_properties = false;
  205. bool integer_dot_product = false;
  206. bool subgroup_size_control = false;
  207. for (const auto& properties : ext_props) {
  208. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  209. amd_shader_core_properties = true;
  210. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  211. integer_dot_product = true;
  212. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  213. subgroup_size_control = true;
  214. }
  215. }
  216. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  217. return vk_device_architecture::OTHER;
  218. }
  219. vk::PhysicalDeviceProperties2 props2;
  220. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  221. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  222. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  223. props2.pNext = &shader_core_props_amd;
  224. shader_core_props_amd.pNext = &integer_dot_props;
  225. integer_dot_props.pNext = &subgroup_size_control_props;
  226. device.getProperties2(&props2);
  227. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  228. return vk_device_architecture::AMD_GCN;
  229. }
  230. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  231. // RDNA
  232. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  233. return vk_device_architecture::AMD_RDNA1;
  234. }
  235. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  236. return vk_device_architecture::AMD_RDNA3;
  237. }
  238. return vk_device_architecture::AMD_RDNA2;
  239. }
  240. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  241. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  242. bool subgroup_size_control = false;
  243. for (const auto& properties : ext_props) {
  244. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  245. subgroup_size_control = true;
  246. }
  247. }
  248. if (!subgroup_size_control) {
  249. return vk_device_architecture::OTHER;
  250. }
  251. vk::PhysicalDeviceProperties2 props2;
  252. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  253. props2.pNext = &subgroup_size_control_props;
  254. device.getProperties2(&props2);
  255. if (subgroup_size_control_props.minSubgroupSize == 16) {
  256. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  257. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  258. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  259. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  260. return vk_device_architecture::INTEL_XE2;
  261. }
  262. } else if (props.vendorID == VK_VENDOR_ID_NVIDIA) {
  263. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  264. bool cooperative_matrix = false;
  265. // Detect "pre-turing" based on lack of coopmat support.
  266. for (const auto& properties : ext_props) {
  267. if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0) {
  268. cooperative_matrix = true;
  269. break;
  270. }
  271. }
  272. if (!cooperative_matrix) {
  273. return vk_device_architecture::NVIDIA_PRE_TURING;
  274. }
  275. }
  276. return vk_device_architecture::OTHER;
  277. }
  278. enum vk_conv_shapes {
  279. CONV_SHAPE_128x128,
  280. CONV_SHAPE_64x32,
  281. CONV_SHAPE_32x256,
  282. CONV_SHAPE_COUNT,
  283. };
  284. struct vk_device_struct {
  285. std::recursive_mutex mutex;
  286. vk::PhysicalDevice physical_device;
  287. vk::PhysicalDeviceProperties properties;
  288. std::string name;
  289. uint64_t max_memory_allocation_size;
  290. uint64_t suballocation_block_size;
  291. bool fp16;
  292. bool bf16;
  293. bool pipeline_robustness;
  294. vk::Device device;
  295. uint32_t vendor_id;
  296. vk::DriverId driver_id;
  297. vk_device_architecture architecture;
  298. vk_queue compute_queue;
  299. vk_queue transfer_queue;
  300. bool single_queue;
  301. uint32_t subgroup_size;
  302. uint32_t shader_core_count;
  303. bool uma;
  304. bool prefer_host_memory;
  305. bool float_controls_rte_fp16;
  306. bool subgroup_add;
  307. bool subgroup_shuffle;
  308. bool integer_dot_product;
  309. bool subgroup_size_control;
  310. uint32_t subgroup_min_size;
  311. uint32_t subgroup_max_size;
  312. bool subgroup_require_full_support;
  313. bool coopmat_support;
  314. bool coopmat_acc_f32_support {};
  315. bool coopmat_acc_f16_support {};
  316. bool coopmat_bf16_support {};
  317. bool coopmat_support_16x16x16_f16acc {};
  318. bool coopmat_support_16x16x16_f32acc {};
  319. bool coopmat1_fa_support {};
  320. uint32_t coopmat_m;
  321. uint32_t coopmat_n;
  322. uint32_t coopmat_k;
  323. bool coopmat_int_support;
  324. uint32_t coopmat_int_m;
  325. uint32_t coopmat_int_n;
  326. uint32_t coopmat_int_k;
  327. bool coopmat2;
  328. size_t idx;
  329. bool mul_mat_l[GGML_TYPE_COUNT];
  330. bool mul_mat_m[GGML_TYPE_COUNT];
  331. bool mul_mat_s[GGML_TYPE_COUNT];
  332. bool mul_mat_id_l[GGML_TYPE_COUNT];
  333. bool mul_mat_id_m[GGML_TYPE_COUNT];
  334. bool mul_mat_id_s[GGML_TYPE_COUNT];
  335. // set to true to indicate that some shaders need to be compiled after the dryrun
  336. bool need_compiles {};
  337. vk::DescriptorSetLayout dsl;
  338. vk_matmul_pipeline pipeline_matmul_f32 {};
  339. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  340. vk_matmul_pipeline pipeline_matmul_bf16 {};
  341. vk_matmul_pipeline2 pipeline_matmul_f16;
  342. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  343. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  344. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  345. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  346. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  347. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  348. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  349. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  350. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  351. vk_pipeline pipeline_matmul_split_k_reduce;
  352. vk_pipeline pipeline_quantize_q8_1;
  353. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  354. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  355. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  356. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  357. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  358. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  359. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  360. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  361. vk_pipeline pipeline_acc_f32;
  362. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  363. vk_pipeline pipeline_add[2][2][2];
  364. vk_pipeline pipeline_add_norepeat[2][2][2];
  365. vk_pipeline pipeline_sub[2][2][2];
  366. vk_pipeline pipeline_sub_norepeat[2][2][2];
  367. vk_pipeline pipeline_mul[2][2][2];
  368. vk_pipeline pipeline_mul_norepeat[2][2][2];
  369. vk_pipeline pipeline_div[2][2][2];
  370. vk_pipeline pipeline_div_norepeat[2][2][2];
  371. vk_pipeline pipeline_add_id_f32;
  372. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  373. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bilinear_ac_f32;
  374. vk_pipeline pipeline_scale_f32;
  375. vk_pipeline pipeline_sqr_f32;
  376. vk_pipeline pipeline_sin_f32;
  377. vk_pipeline pipeline_cos_f32;
  378. vk_pipeline pipeline_clamp_f32;
  379. vk_pipeline pipeline_pad_f32;
  380. vk_pipeline pipeline_roll_f32;
  381. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  382. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  383. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16;
  384. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  385. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  386. vk_pipeline pipeline_set_rows[GGML_TYPE_COUNT];
  387. vk_pipeline pipeline_norm_f32;
  388. vk_pipeline pipeline_group_norm_f32;
  389. vk_pipeline pipeline_rms_norm_f32;
  390. vk_pipeline pipeline_rms_norm_mul_f32;
  391. vk_pipeline pipeline_rms_norm_back_f32;
  392. vk_pipeline pipeline_l2_norm_f32;
  393. // [src/dst 0=fp32,1=fp16]
  394. vk_pipeline pipeline_gelu[2];
  395. vk_pipeline pipeline_gelu_erf[2];
  396. vk_pipeline pipeline_gelu_quick[2];
  397. vk_pipeline pipeline_silu[2];
  398. vk_pipeline pipeline_relu[2];
  399. vk_pipeline pipeline_tanh[2];
  400. vk_pipeline pipeline_sigmoid[2];
  401. vk_pipeline pipeline_geglu[2];
  402. vk_pipeline pipeline_reglu[2];
  403. vk_pipeline pipeline_swiglu[2];
  404. vk_pipeline pipeline_swiglu_oai[2];
  405. vk_pipeline pipeline_geglu_erf[2];
  406. vk_pipeline pipeline_geglu_quick[2];
  407. vk_pipeline pipeline_leaky_relu_f32;
  408. vk_pipeline pipeline_silu_back_f32;
  409. vk_pipeline pipeline_diag_mask_inf_f32;
  410. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  411. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  412. vk_pipeline pipeline_soft_max_back_f32;
  413. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  414. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  415. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  416. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  417. vk_pipeline pipeline_argsort_f32;
  418. vk_pipeline pipeline_sum_rows_f32;
  419. vk_pipeline pipeline_argmax_f32;
  420. vk_pipeline pipeline_count_equal_i32;
  421. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  422. vk_pipeline pipeline_timestep_embedding_f32;
  423. vk_pipeline pipeline_conv_transpose_1d_f32;
  424. vk_pipeline pipeline_pool2d_f32;
  425. vk_pipeline pipeline_rwkv_wkv6_f32;
  426. vk_pipeline pipeline_rwkv_wkv7_f32;
  427. vk_pipeline pipeline_opt_step_adamw_f32;
  428. vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
  429. vk_pipeline pipeline_conv2d_f16_f32[CONV_SHAPE_COUNT];
  430. vk_pipeline pipeline_conv2d_dw_whcn_f32;
  431. vk_pipeline pipeline_conv2d_dw_cwhn_f32;
  432. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  433. vk_pipeline pipeline_flash_attn_f32_f16_cm2[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  434. vk_pipeline pipeline_flash_attn_f32_f16_cm1[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  435. vk_pipeline pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  436. vk_pipeline pipeline_flash_attn_split_k_reduce;
  437. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  438. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  439. vk::Fence fence;
  440. vk_buffer sync_staging;
  441. ggml_backend_buffer_type buffer_type;
  442. bool disable_fusion;
  443. bool disable_host_visible_vidmem;
  444. #ifdef GGML_VULKAN_MEMORY_DEBUG
  445. std::unique_ptr<vk_memory_logger> memory_logger;
  446. #endif
  447. // for GGML_VK_PERF_LOGGER
  448. std::unique_ptr<vk_perf_logger> perf_logger;
  449. vk::QueryPool query_pool;
  450. int32_t num_queries;
  451. ~vk_device_struct() {
  452. VK_LOG_DEBUG("destroy device " << name);
  453. device.destroyFence(fence);
  454. ggml_vk_destroy_buffer(sync_staging);
  455. compute_queue.cmd_pool.destroy(device);
  456. transfer_queue.cmd_pool.destroy(device);
  457. for (auto& pipeline : pipelines) {
  458. if (pipeline.second.expired()) {
  459. continue;
  460. }
  461. vk_pipeline pl = pipeline.second.lock();
  462. ggml_vk_destroy_pipeline(device, pl);
  463. }
  464. pipelines.clear();
  465. device.destroyDescriptorSetLayout(dsl);
  466. device.destroy();
  467. }
  468. };
  469. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  470. cmd_buffer_idx = 0;
  471. q = q_;
  472. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  473. pool = device->device.createCommandPool(command_pool_create_info);
  474. }
  475. void vk_command_pool::destroy(vk::Device& device) {
  476. device.destroyCommandPool(pool);
  477. pool = nullptr;
  478. cmd_buffers.clear();
  479. }
  480. struct vk_buffer_struct {
  481. vk::Buffer buffer = VK_NULL_HANDLE;
  482. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  483. vk::MemoryPropertyFlags memory_property_flags;
  484. void * ptr;
  485. size_t size = 0;
  486. vk_device device;
  487. ~vk_buffer_struct() {
  488. if (size == 0) {
  489. return;
  490. }
  491. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  492. device->device.freeMemory(device_memory);
  493. device->device.destroyBuffer(buffer);
  494. }
  495. };
  496. struct vk_subbuffer {
  497. vk_buffer buffer;
  498. uint64_t offset;
  499. uint64_t size;
  500. operator vk::DescriptorBufferInfo() const {
  501. return { buffer->buffer, offset, size };
  502. }
  503. };
  504. struct vk_semaphore {
  505. vk::Semaphore s;
  506. uint64_t value;
  507. };
  508. struct vk_submission {
  509. vk::CommandBuffer buffer;
  510. std::vector<vk_semaphore> wait_semaphores;
  511. std::vector<vk_semaphore> signal_semaphores;
  512. };
  513. typedef std::vector<vk_submission> vk_sequence;
  514. struct vk_mat_mat_push_constants {
  515. uint32_t M; uint32_t N; uint32_t K;
  516. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  517. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  518. uint32_t k_split;
  519. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  520. uint32_t padded_N;
  521. };
  522. struct vk_mat_vec_push_constants {
  523. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  524. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  525. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  526. };
  527. struct vk_mat_mat_id_push_constants {
  528. uint32_t M; uint32_t N; uint32_t K;
  529. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  530. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  531. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  532. uint32_t padded_N;
  533. };
  534. struct vk_mat_vec_id_push_constants {
  535. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  536. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  537. uint32_t nei0; uint32_t ne11;
  538. };
  539. struct vk_flash_attn_push_constants {
  540. uint32_t N;
  541. uint32_t KV;
  542. uint32_t ne1;
  543. uint32_t ne2;
  544. uint32_t ne3;
  545. uint32_t neq2;
  546. uint32_t neq3;
  547. uint32_t nek2;
  548. uint32_t nek3;
  549. uint32_t nev2;
  550. uint32_t nev3;
  551. uint32_t nem1;
  552. uint32_t nem2;
  553. uint32_t nem3;
  554. uint32_t nb01;
  555. uint32_t nb02;
  556. uint32_t nb03;
  557. uint32_t nb11;
  558. uint32_t nb12;
  559. uint32_t nb13;
  560. uint32_t nb21;
  561. uint32_t nb22;
  562. uint32_t nb23;
  563. float scale;
  564. float max_bias;
  565. float logit_softcap;
  566. uint32_t mask_n_head_log2;
  567. float m0;
  568. float m1;
  569. uint32_t gqa_ratio;
  570. uint32_t split_kv;
  571. uint32_t k_num;
  572. };
  573. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  574. struct vk_op_push_constants {
  575. uint32_t KX;
  576. uint32_t KY;
  577. float param1;
  578. float param2;
  579. };
  580. struct vk_op_glu_push_constants {
  581. uint32_t N;
  582. uint32_t ne00;
  583. uint32_t ne20;
  584. uint32_t mode; // 0: default, 1: swapped, 2: split
  585. float alpha; // for swiglu_oai
  586. float limit;
  587. };
  588. struct vk_op_unary_push_constants {
  589. uint32_t ne;
  590. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  591. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  592. uint32_t misalign_offsets;
  593. float param1; float param2;
  594. uint32_t ne0_012mp; uint32_t ne0_012L;
  595. uint32_t ne0_01mp; uint32_t ne0_01L;
  596. uint32_t ne0_0mp; uint32_t ne0_0L;
  597. uint32_t ne1_012mp; uint32_t ne1_012L;
  598. uint32_t ne1_01mp; uint32_t ne1_01L;
  599. uint32_t ne1_0mp; uint32_t ne1_0L;
  600. };
  601. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  602. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  603. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  604. ne = ne != 0 ? ne : ggml_nelements(dst);
  605. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  606. vk_op_unary_push_constants p{};
  607. p.ne = (uint32_t)ne;
  608. size_t src0_tsize = ggml_type_size(src0->type);
  609. p.ne00 = (uint32_t)src0->ne[0];
  610. p.ne01 = (uint32_t)src0->ne[1];
  611. p.ne02 = (uint32_t)src0->ne[2];
  612. p.ne03 = (uint32_t)src0->ne[3];
  613. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  614. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  615. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  616. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  617. size_t dst_tsize = ggml_type_size(dst->type);
  618. p.ne10 = (uint32_t)dst->ne[0];
  619. p.ne11 = (uint32_t)dst->ne[1];
  620. p.ne12 = (uint32_t)dst->ne[2];
  621. p.ne13 = (uint32_t)dst->ne[3];
  622. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  623. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  624. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  625. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  626. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  627. }
  628. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  629. // Precompute mp (m' in the paper) and L such that division
  630. // can be computed using a multiply (high 32b of 64b result)
  631. // and a shift:
  632. //
  633. // n/d = (mulhi(n, mp) + n) >> L;
  634. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  635. {
  636. // compute L = ceil(log2(d));
  637. L = 0;
  638. while (L < 32 && (uint32_t{1} << L) < d) {
  639. L++;
  640. }
  641. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  642. }
  643. template <typename T> void init_pushconst_fastdiv(T &p) {
  644. GGML_UNUSED(p);
  645. static_assert(!std::is_const<T>::value, "unexpected type");
  646. }
  647. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  648. // Compute magic values to divide by these six numbers.
  649. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  650. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  651. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  652. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  653. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  654. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  655. }
  656. struct vk_op_binary_push_constants {
  657. uint32_t ne;
  658. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  659. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  660. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  661. uint32_t misalign_offsets;
  662. float param1; float param2; int32_t param3;
  663. };
  664. struct vk_op_add_id_push_constants {
  665. uint32_t ne0;
  666. uint32_t ne1;
  667. uint32_t s01;
  668. uint32_t s02;
  669. uint32_t s11;
  670. uint32_t s21;
  671. };
  672. struct vk_op_diag_mask_push_constants {
  673. uint32_t ncols;
  674. uint32_t rows_per_channel;
  675. int32_t n_past;
  676. };
  677. struct vk_op_rope_push_constants {
  678. uint32_t ncols;
  679. uint32_t n_dims;
  680. float freq_scale;
  681. uint32_t p_delta_rows;
  682. float freq_base;
  683. float ext_factor;
  684. float attn_factor;
  685. float corr_dims[2];
  686. float theta_scale;
  687. uint32_t has_ff;
  688. uint32_t ne02;
  689. uint32_t s1;
  690. uint32_t s2;
  691. int32_t sections[4];
  692. uint32_t is_back;
  693. };
  694. struct vk_op_soft_max_push_constants {
  695. uint32_t KX;
  696. uint32_t KY;
  697. uint32_t ne00;
  698. uint32_t ne01;
  699. uint32_t ne02;
  700. uint32_t ne12;
  701. uint32_t ne13;
  702. uint32_t nb11;
  703. uint32_t nb12;
  704. uint32_t nb13;
  705. float scale;
  706. float max_bias;
  707. float m0;
  708. float m1;
  709. uint32_t n_head_log2;
  710. uint32_t nrows_x;
  711. uint32_t has_sinks;
  712. };
  713. struct vk_op_argsort_push_constants {
  714. uint32_t ncols;
  715. uint32_t ncols_pad;
  716. int32_t order;
  717. };
  718. struct vk_op_im2col_push_constants {
  719. uint32_t batch_offset; uint32_t offset_delta;
  720. uint32_t IC;
  721. uint32_t IW; uint32_t IH;
  722. uint32_t OW; uint32_t OH;
  723. uint32_t KW; uint32_t KH;
  724. uint32_t pelements;
  725. uint32_t CHW;
  726. int32_t s0; int32_t s1;
  727. int32_t p0; int32_t p1;
  728. int32_t d0; int32_t d1;
  729. };
  730. struct vk_op_timestep_embedding_push_constants {
  731. uint32_t nb1;
  732. uint32_t dim;
  733. uint32_t max_period;
  734. };
  735. struct vk_op_conv_transpose_1d_push_constants {
  736. uint32_t Cout;
  737. uint32_t Cin;
  738. uint32_t K;
  739. uint32_t L;
  740. uint32_t KL;
  741. uint32_t nb01;
  742. uint32_t nb02;
  743. uint32_t nb11;
  744. uint32_t nb1;
  745. int32_t s0;
  746. };
  747. struct vk_op_pool2d_push_constants {
  748. uint32_t IW; uint32_t IH;
  749. uint32_t OW; uint32_t OH;
  750. uint32_t OC;
  751. uint32_t pelements;
  752. uint32_t op;
  753. int32_t k0; int32_t k1;
  754. int32_t s0; int32_t s1;
  755. int32_t p0; int32_t p1;
  756. };
  757. struct vk_op_rwkv_wkv6_push_constants {
  758. uint32_t B;
  759. uint32_t T;
  760. uint32_t C;
  761. uint32_t H;
  762. };
  763. struct vk_op_rwkv_wkv7_push_constants {
  764. uint32_t B;
  765. uint32_t T;
  766. uint32_t C;
  767. uint32_t H;
  768. };
  769. struct vk_op_conv2d_push_constants {
  770. uint32_t Cout;
  771. uint32_t Cin;
  772. uint32_t N;
  773. uint32_t KW;
  774. uint32_t KH;
  775. uint32_t W;
  776. uint32_t H;
  777. uint32_t OW;
  778. uint32_t OH;
  779. uint32_t s0;
  780. uint32_t s1;
  781. uint32_t p0;
  782. uint32_t p1;
  783. uint32_t d0;
  784. uint32_t d1;
  785. uint32_t nb01;
  786. uint32_t nb02;
  787. uint32_t nb03;
  788. uint32_t nb11;
  789. uint32_t nb12;
  790. uint32_t nb13;
  791. uint32_t nb1;
  792. uint32_t nb2;
  793. uint32_t nb3;
  794. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH
  795. uint32_t KWmp; uint32_t KWL;
  796. uint32_t KWKHmp; uint32_t KWKHL;
  797. uint32_t OWmp; uint32_t OWL;
  798. uint32_t OWOHmp; uint32_t OWOHL;
  799. };
  800. template <> void init_pushconst_fastdiv(vk_op_conv2d_push_constants &p) {
  801. // Compute magic values to divide by KW, KW*KH, OW, OW*OH
  802. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  803. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  804. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  805. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  806. }
  807. struct vk_op_conv2d_dw_push_constants {
  808. uint32_t ne;
  809. uint32_t batches;
  810. uint32_t channels;
  811. uint32_t dst_w;
  812. uint32_t dst_h;
  813. uint32_t src_w;
  814. uint32_t src_h;
  815. uint32_t knl_w;
  816. uint32_t knl_h;
  817. int32_t stride_x;
  818. int32_t stride_y;
  819. int32_t pad_x;
  820. int32_t pad_y;
  821. int32_t dilation_x;
  822. int32_t dilation_y;
  823. };
  824. struct vk_op_upscale_push_constants {
  825. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  826. uint32_t ne00; uint32_t ne01;
  827. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  828. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  829. float sf0; float sf1; float sf2; float sf3;
  830. };
  831. // Allow pre-recording command buffers
  832. struct vk_staging_memcpy {
  833. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  834. void * dst;
  835. const void * src;
  836. size_t n;
  837. };
  838. struct vk_context_struct {
  839. vk_submission * s;
  840. std::vector<vk_sequence> seqs;
  841. int exit_tensor_idx;
  842. std::vector<vk_staging_memcpy> in_memcpys;
  843. std::vector<vk_staging_memcpy> out_memcpys;
  844. vk_command_pool * p {};
  845. };
  846. typedef std::shared_ptr<vk_context_struct> vk_context;
  847. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  848. struct ggml_vk_garbage_collector {
  849. std::vector<vk_semaphore> tl_semaphores;
  850. std::vector<vk_semaphore> semaphores;
  851. std::vector<vk::Event> events;
  852. std::vector<vk_buffer> temp_buffers;
  853. std::vector<vk_context> contexts;
  854. };
  855. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  856. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  857. static std::string format_size(size_t size) {
  858. const size_t kib = 1024;
  859. const size_t mib = kib * 1024;
  860. const size_t gib = mib * 1024;
  861. std::ostringstream oss;
  862. oss << std::fixed << std::setprecision(2);
  863. if (size >= gib) {
  864. oss << static_cast<double>(size) / gib << " GiB";
  865. } else if (size >= mib) {
  866. oss << static_cast<double>(size) / mib << " MiB";
  867. } else if (size >= kib) {
  868. oss << static_cast<double>(size) / kib << " KiB";
  869. } else {
  870. oss << size << " B";
  871. }
  872. return oss.str();
  873. }
  874. static std::mutex log_mutex;
  875. class vk_memory_logger {
  876. public:
  877. vk_memory_logger(): total_device(0), total_host(0) {}
  878. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  879. void log_deallocation(vk_buffer_ref buf_ref);
  880. private:
  881. std::map<vk::Buffer, size_t> allocations; // Track allocations
  882. size_t total_device;
  883. size_t total_host;
  884. };
  885. #else
  886. #define VK_LOG_MEMORY(msg) ((void) 0)
  887. #endif // GGML_VULKAN_MEMORY_DEBUG
  888. class vk_perf_logger {
  889. public:
  890. void print_timings() {
  891. if (timings.empty()) {
  892. return;
  893. }
  894. uint64_t total_all_op_times = 0;
  895. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  896. for (const auto & t : timings) {
  897. uint64_t total_op_times = 0;
  898. for (const auto & time : t.second) {
  899. total_op_times += time;
  900. }
  901. std::cerr << t.first << ": " << t.second.size() << " x " << (total_op_times / t.second.size() / 1000.0)
  902. << " us";
  903. // If we have as many flops entries as timing entries for the op, then compute and log the flops/S.
  904. auto it = flops.find(t.first);
  905. if (it != flops.end() && (it->second).size() == t.second.size()) {
  906. uint64_t total_op_flops = 0;
  907. for (const auto & elem : it->second) {
  908. total_op_flops += elem;
  909. }
  910. std::cerr << " ("
  911. << (double(total_op_flops) / (1000.0 * 1000.0 * 1000.0)) /
  912. (double(total_op_times) / (1000.0 * 1000.0 * 1000.0))
  913. << " GFLOPS/s)";
  914. }
  915. total_all_op_times += total_op_times;
  916. std::cerr << std::endl;
  917. }
  918. if (timings.size() > 0) {
  919. std::cerr << "Total time: " << total_all_op_times / 1000.0 << " us." << std::endl;
  920. }
  921. timings.clear();
  922. flops.clear();
  923. }
  924. void log_timing(const ggml_tensor * node, uint64_t time) {
  925. if (node->op == GGML_OP_UNARY) {
  926. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  927. return;
  928. }
  929. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  930. const uint64_t m = node->src[0]->ne[1];
  931. const uint64_t n = node->src[1]->ne[1];
  932. const uint64_t k = node->src[1]->ne[0];
  933. std::string name = ggml_op_name(node->op);
  934. if (n == 1) {
  935. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  936. } else {
  937. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  938. }
  939. timings[name].push_back(time);
  940. flops[name].push_back(m * n * (k + (k - 1)));
  941. return;
  942. }
  943. if (node->op == GGML_OP_CONV_2D) {
  944. std::string name = ggml_op_name(node->op);
  945. ggml_tensor * knl = node->src[0];
  946. uint64_t OW = node->ne[0];
  947. uint64_t OH = node->ne[1];
  948. uint64_t N = node->ne[3];
  949. uint64_t Cout = node->ne[2];
  950. uint64_t KW = knl->ne[0];
  951. uint64_t KH = knl->ne[1];
  952. uint64_t Cin = knl->ne[2];
  953. // KxCRS @ CRSxNPQ = KxNPQ -> M=K, K=CRS, N=NPQ
  954. uint64_t size_M = Cout;
  955. uint64_t size_K = Cin * KW * KH;
  956. uint64_t size_N = N * OW * OH;
  957. uint64_t n_flops = size_M * size_N * (size_K + (size_K - 1));
  958. name += " M=Cout=" + std::to_string(size_M) + ", K=Cin*KW*KH=" + std::to_string(size_K) +
  959. ", N=N*OW*OH=" + std::to_string(size_N);
  960. flops[name].push_back(n_flops);
  961. timings[name].push_back(time);
  962. return;
  963. }
  964. timings[ggml_op_name(node->op)].push_back(time);
  965. }
  966. private:
  967. std::map<std::string, std::vector<uint64_t>> timings;
  968. std::map<std::string, std::vector<uint64_t>> flops;
  969. };
  970. struct ggml_backend_vk_context {
  971. std::string name;
  972. vk_device device;
  973. size_t semaphore_idx, event_idx;
  974. ggml_vk_garbage_collector gc;
  975. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  976. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  977. vk::Fence fence, almost_ready_fence;
  978. bool almost_ready_fence_pending {};
  979. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  980. vk_context_ref compute_ctx;
  981. vk_context_ref transfer_ctx;
  982. std::vector<vk_context_ref> tensor_ctxs;
  983. std::vector<vk::DescriptorPool> descriptor_pools;
  984. std::vector<vk::DescriptorSet> descriptor_sets;
  985. uint32_t descriptor_set_idx {};
  986. uint32_t pipeline_descriptor_set_requirements {};
  987. vk_command_pool compute_cmd_pool;
  988. vk_command_pool transfer_cmd_pool;
  989. // number of additional consecutive nodes that are being fused with the
  990. // node currently being processed
  991. int num_additional_fused_ops {};
  992. };
  993. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  994. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  995. if (tensor->view_src) {
  996. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  997. }
  998. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  999. }
  1000. struct ggml_backend_vk_buffer_context {
  1001. vk_device_ref device;
  1002. vk_buffer dev_buffer;
  1003. std::string name;
  1004. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  1005. device(device),
  1006. dev_buffer(dev_buffer),
  1007. name(name) {
  1008. }
  1009. ~ggml_backend_vk_buffer_context() {
  1010. ggml_vk_destroy_buffer(dev_buffer);
  1011. }
  1012. };
  1013. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1014. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  1015. std::lock_guard<std::mutex> guard(log_mutex);
  1016. vk_buffer buf = buf_ref.lock();
  1017. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1018. const std::string type = device ? "device" : "host";
  1019. allocations[buf->buffer] = size;
  1020. total_device += device ? size : 0;
  1021. total_host += device ? 0 : size;
  1022. 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));
  1023. }
  1024. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  1025. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  1026. return;
  1027. }
  1028. std::lock_guard<std::mutex> guard(log_mutex);
  1029. vk_buffer buf = buf_ref.lock();
  1030. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1031. std::string type = device ? "device" : "host";
  1032. auto it = allocations.find(buf->buffer);
  1033. total_device -= device ? it->second : 0;
  1034. total_host -= device ? 0 : it->second;
  1035. if (it != allocations.end()) {
  1036. 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));
  1037. allocations.erase(it);
  1038. } else {
  1039. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  1040. }
  1041. }
  1042. #endif // GGML_VULKAN_MEMORY_DEBUG
  1043. struct vk_instance_t {
  1044. vk::Instance instance;
  1045. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  1046. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  1047. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  1048. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  1049. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  1050. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  1051. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  1052. std::vector<size_t> device_indices;
  1053. vk_device devices[GGML_VK_MAX_DEVICES];
  1054. };
  1055. static bool vk_instance_initialized = false;
  1056. static vk_instance_t vk_instance;
  1057. static bool vk_perf_logger_enabled = false;
  1058. #ifdef GGML_VULKAN_CHECK_RESULTS
  1059. static size_t vk_skip_checks;
  1060. static size_t vk_output_tensor;
  1061. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  1062. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1063. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1064. #endif
  1065. 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);
  1066. static void ggml_backend_vk_free(ggml_backend_t backend);
  1067. // Wait for ctx->fence to be signaled.
  1068. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  1069. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  1070. // during this wait.
  1071. if (ctx->almost_ready_fence_pending) {
  1072. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  1073. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  1074. ctx->almost_ready_fence_pending = false;
  1075. }
  1076. // Spin (w/pause) waiting for the graph to finish executing.
  1077. vk::Result result;
  1078. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  1079. if (result != vk::Result::eNotReady) {
  1080. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  1081. exit(1);
  1082. }
  1083. for (uint32_t i = 0; i < 100; ++i) {
  1084. YIELD();
  1085. YIELD();
  1086. YIELD();
  1087. YIELD();
  1088. YIELD();
  1089. YIELD();
  1090. YIELD();
  1091. YIELD();
  1092. YIELD();
  1093. YIELD();
  1094. }
  1095. }
  1096. ctx->device->device.resetFences({ ctx->fence });
  1097. }
  1098. // variables to track number of compiles in progress
  1099. static uint32_t compile_count = 0;
  1100. static std::mutex compile_count_mutex;
  1101. static std::condition_variable compile_count_cond;
  1102. static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipeline, size_t spv_size, const void* spv_data, const std::string entrypoint,
  1103. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  1104. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  1105. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  1106. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  1107. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  1108. GGML_ASSERT(parameter_count > 0);
  1109. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  1110. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  1111. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  1112. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  1113. vk::PushConstantRange pcr(
  1114. vk::ShaderStageFlagBits::eCompute,
  1115. 0,
  1116. pipeline->push_constant_size
  1117. );
  1118. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  1119. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  1120. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1121. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1122. specialization_entries[i].constantID = i;
  1123. specialization_entries[i].offset = i * sizeof(uint32_t);
  1124. specialization_entries[i].size = sizeof(uint32_t);
  1125. }
  1126. vk::SpecializationInfo specialization_info(
  1127. specialization_entries.size(),
  1128. specialization_entries.data(),
  1129. specialization_constants.size() * sizeof(uint32_t),
  1130. specialization_constants.data()
  1131. );
  1132. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1133. if (device->subgroup_require_full_support && require_full_subgroups) {
  1134. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1135. }
  1136. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1137. pipeline_shader_stage_create_flags,
  1138. vk::ShaderStageFlagBits::eCompute,
  1139. pipeline->shader_module,
  1140. entrypoint.c_str(),
  1141. &specialization_info);
  1142. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1143. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1144. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1145. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1146. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1147. }
  1148. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1149. vk::PipelineCreateFlags{},
  1150. pipeline_shader_create_info,
  1151. pipeline->layout);
  1152. vk::PipelineRobustnessCreateInfoEXT rci;
  1153. if (device->pipeline_robustness && disable_robustness) {
  1154. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1155. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1156. compute_pipeline_create_info.setPNext(&rci);
  1157. }
  1158. try {
  1159. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1160. } catch (const vk::SystemError& e) {
  1161. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1162. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1163. throw e;
  1164. }
  1165. pipeline->compiled = true;
  1166. if (vk_instance.debug_utils_support) {
  1167. vk::DebugUtilsObjectNameInfoEXT duoni;
  1168. duoni.objectType = vk::ObjectType::ePipeline;
  1169. duoni.pObjectName = pipeline->name.c_str();
  1170. duoni.objectHandle = /*reinterpret_cast*/(uint64_t)(static_cast<VkPipeline>(pipeline->pipeline));
  1171. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1172. }
  1173. {
  1174. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1175. device->pipelines.insert({ pipeline->name, pipeline });
  1176. }
  1177. {
  1178. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1179. assert(compile_count > 0);
  1180. compile_count--;
  1181. }
  1182. compile_count_cond.notify_all();
  1183. }
  1184. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1185. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1186. device.destroyPipelineLayout(pipeline->layout);
  1187. device.destroyShaderModule(pipeline->shader_module);
  1188. device.destroyPipeline(pipeline->pipeline);
  1189. }
  1190. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1191. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1192. ctx->pipeline_descriptor_set_requirements += n;
  1193. if (!pipeline->compiled) {
  1194. pipeline->needed = true;
  1195. ctx->device->need_compiles = true;
  1196. }
  1197. }
  1198. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1199. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1200. // Enough descriptors are available
  1201. return;
  1202. }
  1203. vk_device& device = ctx->device;
  1204. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1205. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1206. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1207. while (to_alloc > 0) {
  1208. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1209. to_alloc -= alloc_count;
  1210. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1211. if (pool_idx >= ctx->descriptor_pools.size()) {
  1212. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1213. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1214. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1215. }
  1216. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1217. for (uint32_t i = 0; i < alloc_count; i++) {
  1218. layouts[i] = device->dsl;
  1219. }
  1220. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1221. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1222. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1223. pool_idx++;
  1224. }
  1225. }
  1226. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1227. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1228. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1229. // Reuse command buffer
  1230. return p.cmd_buffers[p.cmd_buffer_idx++];
  1231. }
  1232. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1233. p.pool,
  1234. vk::CommandBufferLevel::ePrimary,
  1235. 1);
  1236. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1237. auto buf = cmd_buffers.front();
  1238. p.cmd_buffers.push_back(buf);
  1239. p.cmd_buffer_idx++;
  1240. return buf;
  1241. }
  1242. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1243. if (ctx->seqs.empty()) {
  1244. if (fence) {
  1245. std::lock_guard<std::mutex> guard(queue_mutex);
  1246. ctx->p->q->queue.submit({}, fence);
  1247. }
  1248. return;
  1249. }
  1250. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1251. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1252. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1253. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1254. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1255. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1256. std::vector<vk::SubmitInfo> submit_infos;
  1257. int idx = -1;
  1258. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1259. size_t reserve = 0;
  1260. for (const auto& sequence : ctx->seqs) {
  1261. reserve += sequence.size();
  1262. }
  1263. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1264. tl_wait_semaphores.reserve(reserve);
  1265. tl_wait_vals.reserve(reserve);
  1266. tl_signal_semaphores.reserve(reserve);
  1267. tl_signal_vals.reserve(reserve);
  1268. tl_submit_infos.reserve(reserve);
  1269. submit_infos.reserve(reserve);
  1270. stage_flags.reserve(reserve);
  1271. for (const auto& sequence : ctx->seqs) {
  1272. for (const auto& submission : sequence) {
  1273. stage_flags.push_back({});
  1274. idx++;
  1275. tl_wait_vals.push_back({});
  1276. tl_wait_semaphores.push_back({});
  1277. tl_signal_vals.push_back({});
  1278. tl_signal_semaphores.push_back({});
  1279. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1280. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1281. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1282. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1283. }
  1284. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1285. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1286. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1287. }
  1288. tl_submit_infos.push_back({
  1289. (uint32_t) submission.wait_semaphores.size(),
  1290. tl_wait_vals[idx].data(),
  1291. (uint32_t) submission.signal_semaphores.size(),
  1292. tl_signal_vals[idx].data(),
  1293. });
  1294. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1295. tl_submit_infos[idx].pNext = nullptr;
  1296. vk::SubmitInfo si{
  1297. (uint32_t) submission.wait_semaphores.size(),
  1298. tl_wait_semaphores[idx].data(),
  1299. stage_flags[idx].data(),
  1300. 1,
  1301. &submission.buffer,
  1302. (uint32_t) submission.signal_semaphores.size(),
  1303. tl_signal_semaphores[idx].data(),
  1304. };
  1305. si.setPNext(&tl_submit_infos[idx]);
  1306. submit_infos.push_back(si);
  1307. }
  1308. }
  1309. std::lock_guard<std::mutex> guard(queue_mutex);
  1310. ctx->p->q->queue.submit(submit_infos, fence);
  1311. ctx->seqs.clear();
  1312. }
  1313. 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) {
  1314. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1315. const uint32_t qfsize = queue_family_props.size();
  1316. // Try with avoid preferences first
  1317. for (uint32_t i = 0; i < qfsize; i++) {
  1318. 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)) {
  1319. return i;
  1320. }
  1321. }
  1322. // Fall back to only required
  1323. for (size_t i = 0; i < qfsize; i++) {
  1324. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1325. return i;
  1326. }
  1327. }
  1328. // Fall back to reusing compute queue
  1329. for (size_t i = 0; i < qfsize; i++) {
  1330. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1331. return i;
  1332. }
  1333. }
  1334. // Fall back to ignoring min_num_queries
  1335. for (size_t i = 0; i < qfsize; i++) {
  1336. if (queue_family_props[i].queueFlags & required) {
  1337. return i;
  1338. }
  1339. }
  1340. // 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.
  1341. // 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.
  1342. if (compute_index >= 0) {
  1343. return compute_index;
  1344. }
  1345. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1346. for(auto &q_family : queue_family_props) {
  1347. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1348. }
  1349. abort();
  1350. }
  1351. 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) {
  1352. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1353. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1354. q.queue_family_index = queue_family_index;
  1355. q.transfer_only = transfer_only;
  1356. q.cmd_pool.init(device, &q);
  1357. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1358. q.stage_flags = stage_flags;
  1359. }
  1360. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1361. vk_context result = std::make_shared<vk_context_struct>();
  1362. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1363. ctx->gc.contexts.emplace_back(result);
  1364. result->p = &p;
  1365. return result;
  1366. }
  1367. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1368. vk_context result = std::make_shared<vk_context_struct>();
  1369. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1370. result->p = &p;
  1371. return result;
  1372. }
  1373. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1374. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1375. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1376. vk::SemaphoreCreateInfo ci{};
  1377. ci.setPNext(&tci);
  1378. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1379. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1380. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1381. }
  1382. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1383. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1384. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1385. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1386. vk::SemaphoreCreateInfo ci{};
  1387. ci.setPNext(&tci);
  1388. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1389. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1390. }
  1391. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1392. }
  1393. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1394. if (ctx->event_idx >= ctx->gc.events.size()) {
  1395. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1396. }
  1397. return ctx->gc.events[ctx->event_idx++];
  1398. }
  1399. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1400. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1401. // Requires command buffers to be done
  1402. device->device.resetCommandPool(p.pool);
  1403. p.cmd_buffer_idx = 0;
  1404. }
  1405. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1406. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1407. // Arbitrary frequency to cleanup/reuse command buffers
  1408. static constexpr uint32_t cleanup_frequency = 10;
  1409. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1410. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1411. }
  1412. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1413. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1414. }
  1415. }
  1416. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1417. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1418. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1419. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1420. (flags & memory_type.propertyFlags) == flags &&
  1421. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1422. return static_cast<int32_t>(i);
  1423. }
  1424. }
  1425. return UINT32_MAX;
  1426. }
  1427. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1428. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1429. if (size > device->max_memory_allocation_size) {
  1430. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1431. }
  1432. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1433. if (size == 0) {
  1434. buf->size = 0;
  1435. return buf;
  1436. }
  1437. vk::BufferCreateInfo buffer_create_info{
  1438. vk::BufferCreateFlags(),
  1439. size,
  1440. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1441. vk::SharingMode::eExclusive,
  1442. 0,
  1443. nullptr,
  1444. };
  1445. buf->buffer = device->device.createBuffer(buffer_create_info);
  1446. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1447. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1448. uint32_t memory_type_index = UINT32_MAX;
  1449. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1450. buf->memory_property_flags = req_flags;
  1451. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1452. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1453. buf->memory_property_flags = fallback_flags;
  1454. }
  1455. if (memory_type_index == UINT32_MAX) {
  1456. device->device.destroyBuffer(buf->buffer);
  1457. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1458. }
  1459. try {
  1460. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1461. } catch (const vk::SystemError& e) {
  1462. if (buf->memory_property_flags != fallback_flags) {
  1463. // Try again with fallback flags
  1464. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1465. buf->memory_property_flags = fallback_flags;
  1466. try {
  1467. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1468. }
  1469. catch (const vk::SystemError& e) {
  1470. device->device.destroyBuffer(buf->buffer);
  1471. throw e;
  1472. }
  1473. } else {
  1474. // Out of Host/Device memory, clean up buffer
  1475. device->device.destroyBuffer(buf->buffer);
  1476. throw e;
  1477. }
  1478. }
  1479. buf->ptr = nullptr;
  1480. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1481. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1482. }
  1483. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1484. buf->device = device;
  1485. buf->size = size;
  1486. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1487. device->memory_logger->log_allocation(buf, size);
  1488. #endif
  1489. return buf;
  1490. }
  1491. 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)) {
  1492. try {
  1493. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1494. } catch (const vk::SystemError& e) {
  1495. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1496. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1497. throw e;
  1498. }
  1499. }
  1500. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1501. vk_buffer buf;
  1502. try {
  1503. if (device->prefer_host_memory) {
  1504. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1505. } else if (device->uma) {
  1506. // Fall back to host memory type
  1507. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1508. } else if (device->disable_host_visible_vidmem) {
  1509. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1510. } else {
  1511. // use rebar if available, otherwise fallback to device only visible memory
  1512. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1513. }
  1514. } catch (const vk::SystemError& e) {
  1515. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1516. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1517. throw e;
  1518. }
  1519. return buf;
  1520. }
  1521. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1522. if (buf == nullptr) {
  1523. return;
  1524. }
  1525. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1526. if (buf->device != nullptr) {
  1527. buf->device->memory_logger->log_deallocation(buf);
  1528. }
  1529. #endif
  1530. buf.reset();
  1531. }
  1532. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1533. return { buf, 0, VK_WHOLE_SIZE };
  1534. }
  1535. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1536. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1537. const bool transfer_queue = ctx->p->q->transfer_only;
  1538. ctx->s->buffer.pipelineBarrier(
  1539. ctx->p->q->stage_flags,
  1540. ctx->p->q->stage_flags,
  1541. {},
  1542. { {
  1543. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1544. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1545. } },
  1546. {},
  1547. {}
  1548. );
  1549. }
  1550. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1551. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1552. if (events.empty()) {
  1553. return;
  1554. }
  1555. ctx->s->buffer.waitEvents(
  1556. events,
  1557. ctx->p->q->stage_flags,
  1558. ctx->p->q->stage_flags,
  1559. {},
  1560. {},
  1561. {}
  1562. );
  1563. }
  1564. enum FaCodePath {
  1565. FA_SCALAR,
  1566. FA_COOPMAT1,
  1567. FA_COOPMAT2,
  1568. };
  1569. static FaHeadSizes fa_get_head_sizes(uint32_t hsk, uint32_t hsv) {
  1570. if (hsk != 192 && hsk != 576 && hsk != hsv) {
  1571. return FA_HEAD_SIZE_UNSUPPORTED;
  1572. }
  1573. switch (hsk) {
  1574. case 64: return FA_HEAD_SIZE_64;
  1575. case 80: return FA_HEAD_SIZE_80;
  1576. case 96: return FA_HEAD_SIZE_96;
  1577. case 112: return FA_HEAD_SIZE_112;
  1578. case 128: return FA_HEAD_SIZE_128;
  1579. case 192:
  1580. if (hsv == 192) {
  1581. return FA_HEAD_SIZE_192;
  1582. } else if (hsv == 128) {
  1583. return FA_HEAD_SIZE_192_128;
  1584. } else {
  1585. return FA_HEAD_SIZE_UNSUPPORTED;
  1586. }
  1587. case 256: return FA_HEAD_SIZE_256;
  1588. case 576:
  1589. if (hsv == 512) {
  1590. return FA_HEAD_SIZE_576_512;
  1591. } else {
  1592. return FA_HEAD_SIZE_UNSUPPORTED;
  1593. }
  1594. default: return FA_HEAD_SIZE_UNSUPPORTED;
  1595. }
  1596. }
  1597. // number of rows/cols for flash attention shader
  1598. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1599. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1600. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1601. if (hsv >= 512) {
  1602. return 2;
  1603. } else {
  1604. return 8;
  1605. }
  1606. }
  1607. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1608. // 128 threads split into four subgroups, each subgroup does 1/4
  1609. // of the Bc dimension.
  1610. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1611. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1612. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1613. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1614. if (path == FA_COOPMAT2) {
  1615. return flash_attention_num_small_rows;
  1616. } else {
  1617. return scalar_flash_attention_num_small_rows;
  1618. }
  1619. }
  1620. static std::array<uint32_t, 2> fa_rows_cols(FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) {
  1621. GGML_UNUSED(clamp);
  1622. GGML_UNUSED(hsv);
  1623. if (path == FA_SCALAR) {
  1624. if (small_rows) {
  1625. return {scalar_flash_attention_num_small_rows, 64};
  1626. } else {
  1627. return {get_fa_scalar_num_large_rows(hsv), 32};
  1628. }
  1629. }
  1630. if (path == FA_COOPMAT1) {
  1631. if (small_rows) {
  1632. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1633. } else {
  1634. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1635. }
  1636. }
  1637. // small rows, large cols
  1638. if (small_rows) {
  1639. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1640. }
  1641. // small cols to reduce register count
  1642. if (ggml_is_quantized(type) || hsk >= 256) {
  1643. if (hsk >= 512) {
  1644. return {32, 32};
  1645. } else {
  1646. return {64, 32};
  1647. }
  1648. }
  1649. return {64, 64};
  1650. }
  1651. static bool ggml_vk_matmul_shmem_support(const vk_device& device, const std::vector<uint32_t>& warptile, bool mul_mat_id, ggml_type src0_type) {
  1652. uint32_t lut_size = 0;
  1653. switch (src0_type) {
  1654. case GGML_TYPE_IQ1_S:
  1655. case GGML_TYPE_IQ1_M:
  1656. lut_size = 2*2048;
  1657. break;
  1658. case GGML_TYPE_IQ2_XXS:
  1659. lut_size = 8*256;
  1660. break;
  1661. case GGML_TYPE_IQ2_XS:
  1662. lut_size = 8*512;
  1663. break;
  1664. case GGML_TYPE_IQ2_S:
  1665. lut_size = 8*1024;
  1666. break;
  1667. case GGML_TYPE_IQ3_XXS:
  1668. lut_size = 4*256;
  1669. break;
  1670. case GGML_TYPE_IQ3_S:
  1671. lut_size = 4*512;
  1672. break;
  1673. case GGML_TYPE_IQ4_NL:
  1674. case GGML_TYPE_IQ4_XS:
  1675. case GGML_TYPE_MXFP4:
  1676. lut_size = 4*16;
  1677. break;
  1678. default:
  1679. break;
  1680. }
  1681. // Needs to be kept up to date on shader changes
  1682. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1683. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1684. const uint32_t warps = warptile[0] / warptile[10];
  1685. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1686. const uint32_t mmid_row_ids = mul_mat_id ? (4096 * sizeof(uint32_t) + 4/*_ne1*/) : 0;
  1687. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1688. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1689. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1690. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1691. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1692. return supported;
  1693. }
  1694. struct GpuPipelineConfig {
  1695. // GPU architecture identifier.
  1696. // Example: vk_device_architecture::AMD_GCN
  1697. vk_device_architecture arch;
  1698. // Mapping of pipeline names to their specific subgroup sizes.
  1699. // Example: {"soft_max_f32", 64}
  1700. std::unordered_map<std::string, uint32_t> pipelines;
  1701. // Default subgroup size for this GPU.
  1702. // Defaults to 0 if not explicitly provided.
  1703. uint32_t default_subgroup_size = 0;
  1704. };
  1705. // Pipeline configuration for RDNA1 GPUs.
  1706. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1707. {"soft_max", 64}, {"im2col", 64},
  1708. {"argmax", 64}, {"mul_mat_vec", 64},
  1709. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1710. };
  1711. // Pipeline configuration for RDNA2 GPUs.
  1712. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1713. {"soft_max", 64}, {"im2col", 64},
  1714. };
  1715. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1716. // Define configurations for different GPUs.
  1717. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1718. {
  1719. vk_device_architecture::AMD_RDNA1,
  1720. {
  1721. rdna1_pipelines,
  1722. },
  1723. RDNA_DEFAULT_SUBGROUP_SIZE
  1724. },
  1725. {
  1726. vk_device_architecture::AMD_RDNA2,
  1727. {
  1728. rdna2_pipelines,
  1729. },
  1730. RDNA_DEFAULT_SUBGROUP_SIZE
  1731. },
  1732. };
  1733. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1734. for (const auto &config : gpu_pipeline_configs) {
  1735. if (config.arch == arch) {
  1736. auto pipIt = config.pipelines.find(pipeline_name);
  1737. if (pipIt != config.pipelines.end()) {
  1738. return pipIt->second;
  1739. }
  1740. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1741. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1742. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1743. for (const auto &entry : sorted_pipelines) {
  1744. if (pipeline_name.find(entry.first) != std::string::npos) {
  1745. return entry.second;
  1746. }
  1747. }
  1748. return config.default_subgroup_size;
  1749. }
  1750. }
  1751. return 0; // If no matching configuration is found
  1752. }
  1753. static void ggml_vk_load_shaders(vk_device& device) {
  1754. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1755. // some shaders have a minimum subgroup size
  1756. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1757. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1758. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1759. // mulmat
  1760. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1761. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1762. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1763. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1764. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1765. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1766. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1767. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1768. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1769. uint32_t l_align, m_align, s_align;
  1770. if (device->coopmat2) {
  1771. // spec constants and tile sizes for non-quant matmul/matmul_id
  1772. l_warptile = { 256, 128, 256, 64, 1 };
  1773. m_warptile = { 256, 128, 128, 64, 0 };
  1774. s_warptile = { 128, 64, 64, 64, 0 };
  1775. l_wg_denoms = {128, 256, 1 };
  1776. m_wg_denoms = {128, 128, 1 };
  1777. s_wg_denoms = { 64, 64, 1 };
  1778. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1779. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1780. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1781. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1782. l_mmq_wg_denoms = { 128, 256, 1 };
  1783. m_mmq_wg_denoms = { 128, 128, 1 };
  1784. s_mmq_wg_denoms = { 32, 64, 1 };
  1785. // spec constants and tile sizes for quant matmul (Qi_K)
  1786. l_warptile_mmq_k = { 256, 128, 256, 64, 1 };
  1787. m_warptile_mmq_k = { 256, 128, 128, 64, 1 };
  1788. s_warptile_mmq_k = { 256, 32, 64, 128, 0 };
  1789. l_mmq_wg_denoms_k = { 128, 256, 1 };
  1790. m_mmq_wg_denoms_k = { 128, 128, 1 };
  1791. s_mmq_wg_denoms_k = { 32, 64, 1 };
  1792. // spec constants and tile sizes for quant matmul_id
  1793. l_warptile_mmqid = { 256, 128, 128, 16, 0 };
  1794. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1795. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1796. l_mmqid_wg_denoms = { 128, 128, 1 };
  1797. m_mmqid_wg_denoms = { 128, 64, 1 };
  1798. s_mmqid_wg_denoms = { 128, 64, 1 };
  1799. l_align = 128;
  1800. m_align = 64;
  1801. s_align = 32;
  1802. } else {
  1803. // Matrix cores require different warp group sizes
  1804. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1805. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1806. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1807. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1808. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1809. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1810. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1811. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1812. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1813. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1814. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1815. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1816. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1817. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1818. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1819. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1820. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1821. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1822. // chip specific tuning
  1823. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1824. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1825. }
  1826. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1827. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1828. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1829. l_align = 128;
  1830. m_align = 64;
  1831. s_align = 32;
  1832. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1833. ggml_type t = (ggml_type)i;
  1834. // Disable medium and large matrix multiplication if not enough shared memory is available
  1835. // Check mmq warptiles as the largest configuration
  1836. // Throw an error if not enough for any matrix multiplication is available
  1837. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1838. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1839. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1840. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1841. device->mul_mat_m[i] = false;
  1842. device->mul_mat_l[i] = false;
  1843. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1844. device->mul_mat_l[i] = false;
  1845. }
  1846. // Disable mul_mat_id if not enough shared memory is available
  1847. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1848. device->mul_mat_id_s[i] = false;
  1849. device->mul_mat_id_m[i] = false;
  1850. device->mul_mat_id_l[i] = false;
  1851. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1852. device->mul_mat_id_m[i] = false;
  1853. device->mul_mat_id_l[i] = false;
  1854. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1855. device->mul_mat_id_l[i] = false;
  1856. }
  1857. }
  1858. }
  1859. if (!device->pipeline_matmul_f32) {
  1860. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1861. }
  1862. if (!device->pipeline_matmul_f32_f16) {
  1863. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1864. }
  1865. if (!device->pipeline_matmul_id_f32) {
  1866. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1867. }
  1868. if (!device->pipeline_matmul_bf16) {
  1869. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1870. }
  1871. if (!device->pipeline_matmul_id_bf16) {
  1872. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1873. }
  1874. std::vector<std::future<void>> compiles;
  1875. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const std::string &entrypoint,
  1876. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1877. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1878. if (!require_full_subgroups && required_subgroup_size == 0) {
  1879. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1880. }
  1881. if (!pipeline) {
  1882. pipeline = std::make_shared<vk_pipeline_struct>();
  1883. pipeline->name = name;
  1884. pipeline->parameter_count = parameter_count;
  1885. pipeline->push_constant_size = push_constant_size;
  1886. pipeline->wg_denoms = wg_denoms;
  1887. pipeline->align = align;
  1888. }
  1889. if (!pipeline->needed || pipeline->compiled) {
  1890. return;
  1891. }
  1892. {
  1893. // wait until fewer than N compiles are in progress
  1894. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1895. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1896. while (compile_count >= N) {
  1897. compile_count_cond.wait(guard);
  1898. }
  1899. compile_count++;
  1900. }
  1901. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1902. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1903. };
  1904. auto const &fa_wg_denoms = [&](FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  1905. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  1906. };
  1907. auto const &fa_spec_constants = [&](FaCodePath path, uint32_t hsk, uint32_t hsv, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  1908. // For large number of rows, 128 invocations seems to work best.
  1909. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1910. // can't use 256 for D==80.
  1911. // For scalar, use 128 (arbitrary)
  1912. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  1913. const uint32_t D = (hsk|hsv);
  1914. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  1915. ? scalar_flash_attention_workgroup_size
  1916. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  1917. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  1918. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  1919. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  1920. const uint32_t D_lsb = D ^ (D & (D-1));
  1921. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  1922. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1923. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1924. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  1925. };
  1926. #define CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, HSK, HSV, HEAD_SIZES) \
  1927. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1928. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1929. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1930. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1931. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1932. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1933. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1934. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1935. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  1936. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 64, 64, 64) \
  1937. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 80, 80, 80) \
  1938. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 96, 96, 96) \
  1939. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 112, 112, 112) \
  1940. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 128, 128, 128) \
  1941. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 192, 192) \
  1942. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 128, 192_128) \
  1943. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 256, 256, 256) \
  1944. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 576, 512, 576_512)
  1945. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  1946. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  1947. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  1948. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1949. if (device->coopmat1_fa_support) {
  1950. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  1951. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  1952. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  1953. }
  1954. #endif
  1955. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1956. if (device->coopmat2) {
  1957. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  1958. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  1959. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  1960. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  1961. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  1962. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  1963. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  1964. }
  1965. #endif
  1966. #undef CREATE_FA2
  1967. #undef CREATE_FA
  1968. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1969. if (device->coopmat2) {
  1970. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1971. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1972. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1973. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1974. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1975. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1976. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1977. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1978. // Create 2 variants, {f16,f32} accumulator
  1979. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1980. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1981. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1982. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1983. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1984. if (device->coopmat_bf16_support) {
  1985. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1986. }
  1987. #endif
  1988. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0], matmul_q4_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1989. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1], matmul_q4_1_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1990. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0], matmul_q5_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1991. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1], matmul_q5_1_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1992. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0], matmul_q8_0_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1993. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K], matmul_q2_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1994. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K], matmul_q3_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1995. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K], matmul_q4_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1996. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K], matmul_q5_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1997. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K], matmul_q6_k_f16, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1998. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S], matmul_iq1_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1999. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M], matmul_iq1_m_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2000. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2001. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2002. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S], matmul_iq2_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2003. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2004. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S], matmul_iq3_s_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2005. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2006. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2007. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_MXFP4], matmul_mxfp4_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2008. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2009. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2010. if (device->coopmat_bf16_support) {
  2011. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2012. }
  2013. #endif
  2014. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2015. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2016. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2017. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2018. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2019. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2020. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2021. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2022. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2023. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2024. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2025. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2026. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2027. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2028. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2029. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2030. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2031. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2032. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2033. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f16acc, matmul_id_mxfp4_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2034. #undef CREATE_MM
  2035. #undef CREATE_MM2
  2036. } else
  2037. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2038. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2039. if (device->coopmat_support) {
  2040. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2041. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2042. if (device->mul_mat ## ID ## _l[TYPE]) \
  2043. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  2044. if (device->mul_mat ## ID ## _m[TYPE]) \
  2045. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  2046. if (device->mul_mat ## ID ## _s[TYPE]) \
  2047. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm1_len, NAMELC ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  2048. if (device->mul_mat ## ID ## _l[TYPE]) \
  2049. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  2050. if (device->mul_mat ## ID ## _m[TYPE]) \
  2051. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  2052. if (device->mul_mat ## ID ## _s[TYPE]) \
  2053. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm1_len, NAMELC ## _aligned ## F16ACC ## _cm1_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  2054. // Create 2 variants, {f16,f32} accumulator
  2055. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2056. if (device->coopmat_acc_f16_support) { \
  2057. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2058. } \
  2059. if (device->coopmat_acc_f32_support) { \
  2060. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2061. } \
  2062. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2063. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2064. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2065. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2066. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2067. if (device->coopmat_bf16_support) {
  2068. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  2069. }
  2070. #endif
  2071. if (device->coopmat_acc_f16_support) {
  2072. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2073. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2074. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2075. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2076. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2077. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2078. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2079. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2080. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2081. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2082. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2083. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2084. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2085. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2086. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2087. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2088. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2089. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2090. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2091. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2092. } else {
  2093. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2094. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2095. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2096. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2097. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2098. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2099. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2100. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2101. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2102. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2103. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2104. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2105. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2106. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2107. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2108. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2109. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2110. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2111. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2112. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2113. }
  2114. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2115. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2116. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2117. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2118. if (device->coopmat_bf16_support) {
  2119. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2120. }
  2121. #endif
  2122. if (device->coopmat_acc_f16_support) {
  2123. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2124. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2125. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2126. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2127. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2128. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2129. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2130. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2131. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2132. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2133. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2134. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2135. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2136. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2137. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2138. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2139. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2140. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2141. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2142. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f16acc, matmul_id_mxfp4_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2143. } else {
  2144. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2145. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2146. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2147. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2148. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2149. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2150. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2151. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2152. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2153. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2154. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2155. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2156. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2157. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2158. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2159. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2160. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2161. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2162. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2163. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f16acc, matmul_id_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2164. }
  2165. #undef CREATE_MM2
  2166. #undef CREATE_MM
  2167. } else
  2168. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2169. if (device->fp16) {
  2170. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2171. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2172. if (device->mul_mat ## ID ## _l[TYPE]) \
  2173. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2174. if (device->mul_mat ## ID ## _m[TYPE]) \
  2175. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2176. if (device->mul_mat ## ID ## _s[TYPE]) \
  2177. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2178. if (device->mul_mat ## ID ## _l[TYPE]) \
  2179. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  2180. if (device->mul_mat ## ID ## _m[TYPE]) \
  2181. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  2182. if (device->mul_mat ## ID ## _s[TYPE]) \
  2183. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  2184. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2185. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2186. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->l, #NAMELC "_f16acc_l", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2187. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->l, #NAMELC "_l", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2188. } \
  2189. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2190. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->m, #NAMELC "_f16acc_m", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2191. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->m, #NAMELC "_m", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2192. } \
  2193. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2194. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->s, #NAMELC "_f16acc_s", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2195. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f32acc->s, #NAMELC "_s", NAMELC ## _len, NAMELC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2196. } \
  2197. // Create 2 variants, {f16,f32} accumulator
  2198. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2199. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2200. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2201. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2202. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2203. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2204. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2205. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2206. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2207. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2208. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2209. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2210. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2211. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2212. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2213. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2214. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2215. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2216. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2217. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2218. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2219. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2220. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2221. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2222. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2223. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2224. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2225. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2226. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2227. if (device->integer_dot_product) {
  2228. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0], matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2229. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1], matmul_q4_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2230. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0], matmul_q5_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2231. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1], matmul_q5_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2232. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0], matmul_q8_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2233. }
  2234. #endif
  2235. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2236. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2237. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2238. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2239. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2240. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2241. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2242. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2243. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2244. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2245. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2246. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2247. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2248. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2249. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2250. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2251. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2252. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2253. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2254. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2255. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2256. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2257. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2258. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f16acc, matmul_id_mxfp4_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2259. #undef CREATE_MM2
  2260. #undef CREATE_MMQ
  2261. #undef CREATE_MM
  2262. } else {
  2263. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2264. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2265. if (device->mul_mat ## ID ## _l[TYPE]) \
  2266. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2267. if (device->mul_mat ## ID ## _m[TYPE]) \
  2268. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2269. if (device->mul_mat ## ID ## _s[TYPE]) \
  2270. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2271. if (device->mul_mat ## ID ## _l[TYPE]) \
  2272. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  2273. if (device->mul_mat ## ID ## _m[TYPE]) \
  2274. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  2275. if (device->mul_mat ## ID ## _s[TYPE]) \
  2276. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  2277. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2278. if (device->mul_mat ## ID ## _l[TYPE]) \
  2279. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC "_l", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2280. if (device->mul_mat ## ID ## _m[TYPE]) \
  2281. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC "_m", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2282. if (device->mul_mat ## ID ## _s[TYPE]) \
  2283. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC "_s", NAMELC ## _fp32_len, NAMELC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2284. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2285. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2286. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2287. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2288. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2289. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2290. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2291. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2292. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2293. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2294. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2295. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2296. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2297. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2298. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2299. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2300. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2301. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2302. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2303. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2304. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2305. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2306. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2307. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2308. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2309. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2310. if (device->integer_dot_product) {
  2311. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2312. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2313. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2314. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2315. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  2316. }
  2317. #endif
  2318. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2319. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2320. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2321. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2322. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2323. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2324. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2325. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2326. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2327. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2328. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2329. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2330. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2331. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2332. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2333. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2334. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2335. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2336. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2337. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2338. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2339. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2340. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2341. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2342. }
  2343. // reusing CREATE_MM from the fp32 path
  2344. if ((device->coopmat2 || device->coopmat_support)
  2345. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2346. && !device->coopmat_bf16_support
  2347. #endif
  2348. ) {
  2349. // use scalar tile sizes
  2350. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2351. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2352. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2353. l_wg_denoms = {128, 128, 1 };
  2354. m_wg_denoms = { 64, 64, 1 };
  2355. s_wg_denoms = { 32, 32, 1 };
  2356. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2357. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2358. }
  2359. #undef CREATE_MM
  2360. // mul mat vec
  2361. // the number of rows computed per shader depends on GPU model and quant
  2362. uint32_t rm_stdq = 1;
  2363. uint32_t rm_kq = 2;
  2364. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2365. if (device->architecture == AMD_GCN) {
  2366. rm_stdq = 2;
  2367. rm_kq = 4;
  2368. }
  2369. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2370. rm_stdq = 2;
  2371. uint32_t rm_iq = 2 * rm_kq;
  2372. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2373. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2374. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2375. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f32_f32_len, mul_mat_vec_bf16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2376. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2377. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2378. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2379. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2380. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), 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*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2381. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2382. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2383. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2384. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2385. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2386. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2387. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2388. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2389. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2390. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2391. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2392. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2393. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2394. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2395. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f32_f32_"+std::to_string(i+1), mul_mat_vec_mxfp4_f32_f32_len, mul_mat_vec_mxfp4_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2396. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2397. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2398. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f16_f32_len, mul_mat_vec_bf16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2399. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2400. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2401. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2402. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2403. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), 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*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2404. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2405. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2406. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2407. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2408. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2409. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2410. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2411. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2412. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2413. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2414. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2415. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2416. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2417. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2418. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f16_f32_"+std::to_string(i+1), mul_mat_vec_mxfp4_f16_f32_len, mul_mat_vec_mxfp4_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2419. }
  2420. 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), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2421. 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), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2422. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_BF16], "mul_mat_vec_id_bf16_f32", mul_mat_vec_id_bf16_f32_len, mul_mat_vec_id_bf16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2423. 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), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2424. 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), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2425. 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), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2426. 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), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2427. 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*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq}, 1, true);
  2428. 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), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2429. 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), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2430. 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), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2431. 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), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2432. 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), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2433. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_S], "mul_mat_vec_id_iq1_s_f32", mul_mat_vec_id_iq1_s_f32_len, mul_mat_vec_id_iq1_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2434. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_M], "mul_mat_vec_id_iq1_m_f32", mul_mat_vec_id_iq1_m_f32_len, mul_mat_vec_id_iq1_m_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2435. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XXS], "mul_mat_vec_id_iq2_xxs_f32", mul_mat_vec_id_iq2_xxs_f32_len, mul_mat_vec_id_iq2_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2436. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XS], "mul_mat_vec_id_iq2_xs_f32", mul_mat_vec_id_iq2_xs_f32_len, mul_mat_vec_id_iq2_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2437. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_S], "mul_mat_vec_id_iq2_s_f32", mul_mat_vec_id_iq2_s_f32_len, mul_mat_vec_id_iq2_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2438. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_XXS], "mul_mat_vec_id_iq3_xxs_f32", mul_mat_vec_id_iq3_xxs_f32_len, mul_mat_vec_id_iq3_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2439. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_S], "mul_mat_vec_id_iq3_s_f32", mul_mat_vec_id_iq3_s_f32_len, mul_mat_vec_id_iq3_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2440. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_XS], "mul_mat_vec_id_iq4_xs_f32", mul_mat_vec_id_iq4_xs_f32_len, mul_mat_vec_id_iq4_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2441. 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), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2442. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_MXFP4], "mul_mat_vec_id_mxfp4_f32", mul_mat_vec_id_mxfp4_f32_len, mul_mat_vec_id_mxfp4_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2443. // dequant shaders
  2444. 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);
  2445. 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);
  2446. 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);
  2447. 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);
  2448. 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);
  2449. 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);
  2450. 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);
  2451. 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);
  2452. 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);
  2453. 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);
  2454. 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);
  2455. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_S], "dequant_iq1_s", dequant_iq1_s_len, dequant_iq1_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2456. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_M], "dequant_iq1_m", dequant_iq1_m_len, dequant_iq1_m_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2457. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XXS], "dequant_iq2_xxs", dequant_iq2_xxs_len, dequant_iq2_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2458. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XS], "dequant_iq2_xs", dequant_iq2_xs_len, dequant_iq2_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2459. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_S], "dequant_iq2_s", dequant_iq2_s_len, dequant_iq2_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2460. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_XXS], "dequant_iq3_xxs", dequant_iq3_xxs_len, dequant_iq3_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2461. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_S], "dequant_iq3_s", dequant_iq3_s_len, dequant_iq3_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2462. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_XS], "dequant_iq4_xs", dequant_iq4_xs_len, dequant_iq4_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  2463. 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);
  2464. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_MXFP4], "dequant_mxfp4", dequant_mxfp4_len, dequant_mxfp4_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  2465. // get_rows
  2466. 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);
  2467. 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);
  2468. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_BF16], "get_rows_bf16", get_rows_bf16_len, get_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2469. 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);
  2470. 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);
  2471. 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);
  2472. 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);
  2473. 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);
  2474. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_S], "get_rows_iq1_s", get_rows_iq1_s_len, get_rows_iq1_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2475. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_M], "get_rows_iq1_m", get_rows_iq1_m_len, get_rows_iq1_m_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2476. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs", get_rows_iq2_xxs_len, get_rows_iq2_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2477. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs", get_rows_iq2_xs_len, get_rows_iq2_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2478. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_S], "get_rows_iq2_s", get_rows_iq2_s_len, get_rows_iq2_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2479. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs", get_rows_iq3_xxs_len, get_rows_iq3_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2480. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_S], "get_rows_iq3_s", get_rows_iq3_s_len, get_rows_iq3_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2481. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs", get_rows_iq4_xs_len, get_rows_iq4_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2482. 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);
  2483. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_MXFP4], "get_rows_mxfp4", get_rows_mxfp4_len, get_rows_mxfp4_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2484. 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);
  2485. 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);
  2486. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_BF16], "get_rows_bf16_f32", get_rows_bf16_f32_len, get_rows_bf16_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  2487. 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);
  2488. 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);
  2489. 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);
  2490. 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);
  2491. 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);
  2492. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_S], "get_rows_iq1_s_f32", get_rows_iq1_s_f32_len, get_rows_iq1_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2493. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_M], "get_rows_iq1_m_f32", get_rows_iq1_m_f32_len, get_rows_iq1_m_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2494. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs_f32", get_rows_iq2_xxs_f32_len, get_rows_iq2_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2495. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs_f32", get_rows_iq2_xs_f32_len, get_rows_iq2_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2496. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_S], "get_rows_iq2_s_f32", get_rows_iq2_s_f32_len, get_rows_iq2_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2497. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs_f32", get_rows_iq3_xxs_f32_len, get_rows_iq3_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2498. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_S], "get_rows_iq3_s_f32", get_rows_iq3_s_f32_len, get_rows_iq3_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2499. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs_f32", get_rows_iq4_xs_f32_len, get_rows_iq4_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2500. 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);
  2501. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_MXFP4], "get_rows_mxfp4_f32", get_rows_mxfp4_f32_len, get_rows_mxfp4_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2502. 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 * 4, 1, 1}, {}, 1);
  2503. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_split_k_reduce, "fa_split_k_reduce", fa_split_k_reduce_len, fa_split_k_reduce_data, "main", 3, 5 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  2504. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_len, quantize_q8_1_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  2505. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2506. if (device->subgroup_add && device->subgroup_require_full_support) {
  2507. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  2508. } else {
  2509. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  2510. }
  2511. }
  2512. 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, 12 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2513. 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);
  2514. 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);
  2515. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1);
  2516. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1);
  2517. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_back_f32, "rms_norm_back_f32", rms_norm_back_f32_len, rms_norm_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2518. ggml_vk_create_pipeline(device, device->pipeline_l2_norm_f32, "l2_norm_f32", l2_norm_f32_len, l2_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2519. 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);
  2520. 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);
  2521. 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);
  2522. ggml_vk_create_pipeline(device, device->pipeline_cpy_f16_f32, "cpy_f16_f32", cpy_f16_f32_len, cpy_f16_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2523. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_bf16,"cpy_f32_bf16",cpy_f32_bf16_len,cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2524. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f32, "contig_cpy_f32_f32", contig_cpy_f32_f32_len, contig_cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2525. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f16, "contig_cpy_f32_f16", contig_cpy_f32_f16_len, contig_cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2526. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f16, "contig_cpy_f16_f16", contig_cpy_f16_f16_len, contig_cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2527. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f32, "contig_cpy_f16_f32", contig_cpy_f16_f32_len, contig_cpy_f16_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2528. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_bf16,"contig_cpy_f32_bf16",contig_cpy_f32_bf16_len,contig_cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2529. if (device->float_controls_rte_fp16) {
  2530. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2531. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2532. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_rte_len, cpy_f32_q5_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2533. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_rte_len, cpy_f32_q5_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2534. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_rte_len, cpy_f32_q8_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2535. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_rte_len, cpy_f32_iq4_nl_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2536. } else {
  2537. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_len, cpy_f32_q4_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2538. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_len, cpy_f32_q4_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2539. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_len, cpy_f32_q5_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2540. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_len, cpy_f32_q5_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2541. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_len, cpy_f32_q8_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2542. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_len, cpy_f32_iq4_nl_data, "main", 2, sizeof(vk_op_unary_push_constants), {32, 1, 1}, {}, 1);
  2543. }
  2544. if (device->float_controls_rte_fp16) {
  2545. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_rte_len, set_rows_f32_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2546. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_rte_len, set_rows_f16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2547. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_rte_len, set_rows_bf16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2548. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_rte_len, set_rows_q4_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2549. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_rte_len, set_rows_q4_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2550. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_rte_len, set_rows_q5_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2551. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_rte_len, set_rows_q5_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2552. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_rte_len, set_rows_q8_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2553. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_rte_len, set_rows_iq4_nl_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2554. } else {
  2555. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_len, set_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2556. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_len, set_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2557. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_len, set_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2558. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_len, set_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2559. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_len, set_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2560. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_len, set_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2561. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_len, set_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2562. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_len, set_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2563. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_len, set_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2564. }
  2565. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_0], "cpy_q4_0_f32", cpy_q4_0_f32_len, cpy_q4_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2566. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_1], "cpy_q4_1_f32", cpy_q4_1_f32_len, cpy_q4_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2567. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_0], "cpy_q5_0_f32", cpy_q5_0_f32_len, cpy_q5_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2568. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_1], "cpy_q5_1_f32", cpy_q5_1_f32_len, cpy_q5_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2569. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q8_0], "cpy_q8_0_f32", cpy_q8_0_f32_len, cpy_q8_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2570. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_IQ4_NL], "cpy_iq4_nl_f32", cpy_iq4_nl_f32_len, cpy_iq4_nl_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2571. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2572. std::string s;
  2573. s += std::string(src0_f16 ? "_f16" : "_f32");
  2574. s += std::string(src1_f16 ? "_f16" : "_f32");
  2575. s += std::string(dst_f16 ? "_f16" : "_f32");
  2576. return s;
  2577. };
  2578. bool rte = device->float_controls_rte_fp16;
  2579. #define CREATE_BINARY(name, namemod, spec) \
  2580. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2581. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2582. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d][rte], name ## _data[s0][s1][d][rte], \
  2583. "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2584. CREATE_BINARY(add, , {0})
  2585. CREATE_BINARY(add, _norepeat, {1})
  2586. CREATE_BINARY(sub, , {0})
  2587. CREATE_BINARY(sub, _norepeat, {1})
  2588. CREATE_BINARY(mul, , {0})
  2589. CREATE_BINARY(mul, _norepeat, {1})
  2590. CREATE_BINARY(div, , {0})
  2591. CREATE_BINARY(div, _norepeat, {1})
  2592. #undef CREATE_BINARY
  2593. ggml_vk_create_pipeline(device, device->pipeline_add_id_f32, "add_id_f32", add_id_f32_len, add_id_f32_data, "main", 4, sizeof(vk_op_add_id_push_constants), {1, 1, 1}, {}, 1);
  2594. 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);
  2595. 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);
  2596. 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);
  2597. 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);
  2598. ggml_vk_create_pipeline(device, device->pipeline_upscale_nearest_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_NEAREST}, 1);
  2599. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR}, 1);
  2600. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_ac_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS}, 1);
  2601. 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);
  2602. 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);
  2603. 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);
  2604. 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);
  2605. 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);
  2606. 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);
  2607. ggml_vk_create_pipeline(device, device->pipeline_roll_f32, "roll_f32", roll_f32_len, roll_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2608. 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);
  2609. ggml_vk_create_pipeline(device, device->pipeline_repeat_back_f32, "repeat_back_f32", repeat_back_f32_len, repeat_back_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2610. #define CREATE_UNARY(name) \
  2611. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32", name ## _f32_len, name ## _f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  2612. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16", name ## _f16_len, name ## _f16_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2613. CREATE_UNARY(gelu)
  2614. CREATE_UNARY(gelu_erf)
  2615. CREATE_UNARY(gelu_quick)
  2616. CREATE_UNARY(silu)
  2617. CREATE_UNARY(relu)
  2618. CREATE_UNARY(tanh)
  2619. CREATE_UNARY(sigmoid)
  2620. #undef CREATE_UNARY
  2621. #define CREATE_GLU(name) \
  2622. if (device->float_controls_rte_fp16) { \
  2623. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32_rte", name ## _f32_rte_len, name ## _f32_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2624. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16_rte", name ## _f16_rte_len, name ## _f16_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2625. } else { \
  2626. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32", name ## _f32_len, name ## _f32_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2627. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16", name ## _f16_len, name ## _f16_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  2628. }
  2629. CREATE_GLU(geglu)
  2630. CREATE_GLU(reglu)
  2631. CREATE_GLU(swiglu)
  2632. CREATE_GLU(swiglu_oai)
  2633. CREATE_GLU(geglu_erf)
  2634. CREATE_GLU(geglu_quick)
  2635. #undef CREATE_GLU
  2636. 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);
  2637. ggml_vk_create_pipeline(device, device->pipeline_silu_back_f32, "silu_back_f32", silu_back_f32_len, silu_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2638. 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), {1, 512, 1}, {}, 1, true);
  2639. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2640. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2641. 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", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2642. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2643. ggml_vk_create_pipeline(device, device->pipeline_soft_max_back_f32, "soft_max_back_f32", soft_max_back_f32_len, soft_max_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2644. 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);
  2645. 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);
  2646. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f32, "rope_multi_f32", rope_multi_f32_len, rope_multi_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2647. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f32, "rope_vision_f32", rope_vision_f32_len, rope_vision_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2648. if (device->float_controls_rte_fp16) {
  2649. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_rte_len, rope_norm_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2650. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_rte_len, rope_neox_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2651. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_rte_len, rope_multi_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2652. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_rte_len, rope_vision_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2653. } else {
  2654. 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);
  2655. 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);
  2656. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_len, rope_multi_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2657. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_len, rope_vision_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2658. }
  2659. 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);
  2660. ggml_vk_create_pipeline(device, device->pipeline_argmax_f32, "argmax_f32", argmax_f32_len, argmax_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2661. 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);
  2662. ggml_vk_create_pipeline(device, device->pipeline_count_equal_i32, "count_equal_i32", count_equal_i32_len, count_equal_i32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, { device->subgroup_size }, 1);
  2663. 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), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2664. if (device->float_controls_rte_fp16) {
  2665. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2666. } else {
  2667. 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), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2668. }
  2669. 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);
  2670. ggml_vk_create_pipeline(device, device->pipeline_conv_transpose_1d_f32, "conv_transpose_1d_f32", conv_transpose_1d_f32_len, conv_transpose_1d_f32_data, "main", 3, sizeof(vk_op_conv_transpose_1d_push_constants), {1, 1, 1}, {}, 1);
  2671. ggml_vk_create_pipeline(device, device->pipeline_pool2d_f32, "pool2d_f32", pool2d_f32_len, pool2d_f32_data, "main", 2, sizeof(vk_op_pool2d_push_constants), {512, 1, 1}, {}, 1);
  2672. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv6_f32, "rwkv_wkv6_f32", rwkv_wkv6_f32_len, rwkv_wkv6_f32_data, "main", 7, sizeof(vk_op_rwkv_wkv6_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2673. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv7_f32, "rwkv_wkv7_f32", rwkv_wkv7_f32_len, rwkv_wkv7_f32_data, "main", 8, sizeof(vk_op_rwkv_wkv7_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2674. ggml_vk_create_pipeline(device, device->pipeline_opt_step_adamw_f32, "opt_step_adamw_f32", opt_step_adamw_f32_len, opt_step_adamw_f32_data, "main", 5, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2675. // conv2d
  2676. for (uint32_t s = 0; s < CONV_SHAPE_COUNT; ++s) {
  2677. uint32_t conv2d_WG_SIZE = 256;
  2678. uint32_t conv2d_BS_K = 128;
  2679. uint32_t conv2d_BS_CRS = 16;
  2680. uint32_t use_collectives = 0; // Enables subgroup ops for preventing the re-calculation of indices.
  2681. uint32_t conv2d_BS_NPQ = 128;
  2682. uint32_t conv2d_TS_K = 8;
  2683. uint32_t conv2d_SHMEM_PAD = 4;
  2684. bool conv2d_UNROLL = true;
  2685. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2686. if (device->coopmat2) {
  2687. conv2d_SHMEM_PAD = 8; // 8 float16_t
  2688. }
  2689. #endif
  2690. if (device->vendor_id == VK_VENDOR_ID_INTEL) {
  2691. conv2d_SHMEM_PAD = 0;
  2692. conv2d_UNROLL = false;
  2693. } else if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2694. conv2d_SHMEM_PAD = device->architecture == vk_device_architecture::AMD_GCN ? 1 : 4;
  2695. }
  2696. switch (s) {
  2697. default:
  2698. case CONV_SHAPE_128x128:
  2699. conv2d_BS_K = 128;
  2700. conv2d_BS_NPQ = 128;
  2701. conv2d_BS_CRS = 16;
  2702. if (device->vendor_id == VK_VENDOR_ID_AMD && device->architecture != vk_device_architecture::AMD_GCN) {
  2703. conv2d_UNROLL = false;
  2704. }
  2705. break;
  2706. case CONV_SHAPE_64x32:
  2707. conv2d_BS_K = 64;
  2708. conv2d_BS_NPQ = 32;
  2709. conv2d_BS_CRS = 32;
  2710. conv2d_TS_K = 4;
  2711. break;
  2712. case CONV_SHAPE_32x256:
  2713. conv2d_BS_K = 32;
  2714. conv2d_BS_NPQ = 256;
  2715. conv2d_BS_CRS = 16;
  2716. break;
  2717. }
  2718. // Use collectives on pre-Turing NVIDIA GPUs and GCN AMD cards, which had slower integer math.
  2719. bool allow_collectives_nv = device->vendor_id != VK_VENDOR_ID_NVIDIA ||
  2720. device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  2721. bool allow_collectives_amd = device->vendor_id != VK_VENDOR_ID_AMD ||
  2722. device->architecture == vk_device_architecture::AMD_GCN;
  2723. if (device->subgroup_shuffle &&
  2724. device->vendor_id != VK_VENDOR_ID_INTEL && // Do not enable collectives on Intel, see PR 14316.
  2725. allow_collectives_nv &&
  2726. allow_collectives_amd) {
  2727. use_collectives = 1;
  2728. conv2d_BS_CRS = std::min(
  2729. device->subgroup_size,
  2730. conv2d_BS_CRS); // CRS block size should be capped at subgroup size for correctness when shuffle is used.
  2731. }
  2732. uint32_t conv2d_shmem_req =
  2733. (conv2d_BS_K * (conv2d_BS_CRS + conv2d_SHMEM_PAD) + conv2d_BS_CRS * (conv2d_BS_NPQ + conv2d_SHMEM_PAD)) * sizeof(float);
  2734. if (device->properties.limits.maxComputeSharedMemorySize < conv2d_shmem_req) {
  2735. conv2d_BS_CRS = 8;
  2736. if (use_collectives) {
  2737. conv2d_BS_CRS = std::min(device->subgroup_size, conv2d_BS_CRS);
  2738. }
  2739. }
  2740. std::array<uint32_t, 3> wg_denoms = { conv2d_BS_K, conv2d_BS_NPQ, 1 };
  2741. std::vector<uint32_t> spec_constants = { conv2d_WG_SIZE, conv2d_BS_K, conv2d_BS_CRS, conv2d_BS_NPQ, conv2d_TS_K, use_collectives, conv2d_SHMEM_PAD };
  2742. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2743. if (device->coopmat2) {
  2744. ggml_vk_create_pipeline(
  2745. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_cm2_len, conv2d_f32_cm2_data, "main", 3,
  2746. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2747. ggml_vk_create_pipeline(
  2748. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_cm2_len, conv2d_f16_f32_cm2_data, "main", 3,
  2749. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2750. } else
  2751. #endif
  2752. if (conv2d_UNROLL) {
  2753. ggml_vk_create_pipeline(
  2754. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_unroll_len, conv2d_f32_unroll_data, "main", 3,
  2755. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2756. ggml_vk_create_pipeline(
  2757. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_unroll_len, conv2d_f16_f32_unroll_data, "main", 3,
  2758. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2759. } else {
  2760. ggml_vk_create_pipeline(
  2761. device, device->pipeline_conv2d_f32[s], "conv2d_f32", conv2d_f32_len, conv2d_f32_data, "main", 3,
  2762. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2763. ggml_vk_create_pipeline(
  2764. device, device->pipeline_conv2d_f16_f32[s], "conv2d_f16_f32", conv2d_f16_f32_len, conv2d_f16_f32_data, "main", 3,
  2765. sizeof(vk_op_conv2d_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  2766. }
  2767. }
  2768. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f32, "conv2d_dw_whcn_f32", conv2d_dw_whcn_f32_len, conv2d_dw_whcn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2769. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f32, "conv2d_dw_cwhn_f32", conv2d_dw_cwhn_f32_len, conv2d_dw_cwhn_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  2770. for (auto &c : compiles) {
  2771. c.wait();
  2772. }
  2773. device->need_compiles = false;
  2774. }
  2775. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2776. static vk_device ggml_vk_get_device(size_t idx) {
  2777. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2778. if (vk_instance.devices[idx] == nullptr) {
  2779. VK_LOG_DEBUG("Initializing new vk_device");
  2780. vk_device device = std::make_shared<vk_device_struct>();
  2781. vk_instance.devices[idx] = device;
  2782. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2783. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2784. #endif
  2785. if (vk_perf_logger_enabled) {
  2786. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2787. }
  2788. size_t dev_num = vk_instance.device_indices[idx];
  2789. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2790. if (dev_num >= physical_devices.size()) {
  2791. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2792. throw std::runtime_error("Device not found");
  2793. }
  2794. device->physical_device = physical_devices[dev_num];
  2795. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2796. device->architecture = get_device_architecture(device->physical_device);
  2797. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2798. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2799. const char* GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM = getenv("GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM");
  2800. device->disable_host_visible_vidmem = GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM != nullptr;
  2801. bool fp16_storage = false;
  2802. bool fp16_compute = false;
  2803. bool maintenance4_support = false;
  2804. bool sm_builtins = false;
  2805. bool amd_shader_core_properties2 = false;
  2806. bool pipeline_robustness = false;
  2807. bool coopmat2_support = false;
  2808. device->coopmat_support = false;
  2809. device->integer_dot_product = false;
  2810. bool bfloat16_support = false;
  2811. for (const auto& properties : ext_props) {
  2812. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2813. maintenance4_support = true;
  2814. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2815. fp16_storage = true;
  2816. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2817. fp16_compute = true;
  2818. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2819. sm_builtins = true;
  2820. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2821. amd_shader_core_properties2 = true;
  2822. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2823. pipeline_robustness = true;
  2824. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2825. device->subgroup_size_control = true;
  2826. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2827. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2828. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2829. device->coopmat_support = true;
  2830. device->coopmat_m = 0;
  2831. device->coopmat_n = 0;
  2832. device->coopmat_k = 0;
  2833. #endif
  2834. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2835. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2836. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2837. coopmat2_support = true;
  2838. #endif
  2839. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2840. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2841. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2842. device->integer_dot_product = true;
  2843. #endif
  2844. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2845. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  2846. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2847. bfloat16_support = true;
  2848. #endif
  2849. }
  2850. }
  2851. vk::PhysicalDeviceProperties2 props2;
  2852. vk::PhysicalDeviceMaintenance3Properties props3;
  2853. vk::PhysicalDeviceMaintenance4Properties props4;
  2854. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2855. vk::PhysicalDeviceDriverProperties driver_props;
  2856. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2857. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2858. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2859. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2860. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2861. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2862. props2.pNext = &props3;
  2863. props3.pNext = &subgroup_props;
  2864. subgroup_props.pNext = &driver_props;
  2865. driver_props.pNext = &vk11_props;
  2866. vk11_props.pNext = &vk12_props;
  2867. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2868. if (maintenance4_support) {
  2869. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2870. last_struct = (VkBaseOutStructure *)&props4;
  2871. }
  2872. if (sm_builtins) {
  2873. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2874. last_struct = (VkBaseOutStructure *)&sm_props;
  2875. }
  2876. if (amd_shader_core_properties2) {
  2877. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2878. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2879. }
  2880. if (device->subgroup_size_control) {
  2881. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2882. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2883. }
  2884. #if defined(VK_NV_cooperative_matrix2)
  2885. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2886. if (coopmat2_support) {
  2887. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2888. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2889. }
  2890. #endif
  2891. if (device->integer_dot_product) {
  2892. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2893. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2894. }
  2895. device->physical_device.getProperties2(&props2);
  2896. device->properties = props2.properties;
  2897. device->vendor_id = device->properties.vendorID;
  2898. device->driver_id = driver_props.driverID;
  2899. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2900. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2901. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2902. } else if (maintenance4_support) {
  2903. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2904. } else {
  2905. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2906. }
  2907. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2908. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2909. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2910. } else {
  2911. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2912. device->suballocation_block_size = 1024*1024*1024;
  2913. }
  2914. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2915. device->subgroup_size = subgroup_props.subgroupSize;
  2916. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2917. if (sm_builtins) {
  2918. device->shader_core_count = sm_props.shaderSMCount;
  2919. } else if (amd_shader_core_properties2) {
  2920. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2921. } else {
  2922. device->shader_core_count = 0;
  2923. }
  2924. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2925. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2926. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2927. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2928. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  2929. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2930. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2931. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2932. device->coopmat_support = false;
  2933. }
  2934. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2935. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2936. // Try to find a non-graphics compute queue and transfer-focused queues
  2937. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2938. 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);
  2939. const float priorities[] = { 1.0f, 1.0f };
  2940. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2941. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2942. if (compute_queue_family_index != transfer_queue_family_index) {
  2943. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2944. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2945. } else if(!device->single_queue) {
  2946. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2947. } else {
  2948. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2949. }
  2950. vk::DeviceCreateInfo device_create_info;
  2951. std::vector<const char *> device_extensions;
  2952. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2953. VkPhysicalDeviceFeatures2 device_features2;
  2954. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2955. device_features2.pNext = nullptr;
  2956. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2957. VkPhysicalDeviceVulkan11Features vk11_features;
  2958. vk11_features.pNext = nullptr;
  2959. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2960. device_features2.pNext = &vk11_features;
  2961. VkPhysicalDeviceVulkan12Features vk12_features;
  2962. vk12_features.pNext = nullptr;
  2963. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2964. vk11_features.pNext = &vk12_features;
  2965. last_struct = (VkBaseOutStructure *)&vk12_features;
  2966. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2967. pl_robustness_features.pNext = nullptr;
  2968. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2969. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2970. if (pipeline_robustness) {
  2971. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2972. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2973. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2974. }
  2975. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2976. subgroup_size_control_features.pNext = nullptr;
  2977. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2978. subgroup_size_control_features.computeFullSubgroups = false;
  2979. subgroup_size_control_features.subgroupSizeControl = false;
  2980. if (device->subgroup_size_control) {
  2981. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2982. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2983. }
  2984. #if defined(VK_KHR_cooperative_matrix)
  2985. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2986. coopmat_features.pNext = nullptr;
  2987. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2988. coopmat_features.cooperativeMatrix = VK_FALSE;
  2989. if (device->coopmat_support) {
  2990. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2991. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2992. }
  2993. #endif
  2994. #if defined(VK_NV_cooperative_matrix2)
  2995. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2996. coopmat2_features.pNext = nullptr;
  2997. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2998. if (coopmat2_support) {
  2999. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  3000. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  3001. device_extensions.push_back("VK_NV_cooperative_matrix2");
  3002. }
  3003. #endif
  3004. #if defined(VK_KHR_shader_bfloat16)
  3005. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3006. bfloat16_features.pNext = nullptr;
  3007. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3008. if (bfloat16_support) {
  3009. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3010. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3011. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3012. }
  3013. #endif
  3014. VkPhysicalDeviceMaintenance4Features maint4_features {};
  3015. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  3016. if (maintenance4_support) {
  3017. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  3018. last_struct = (VkBaseOutStructure *)&maint4_features;
  3019. device_extensions.push_back("VK_KHR_maintenance4");
  3020. }
  3021. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3022. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3023. if (device->integer_dot_product) {
  3024. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3025. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3026. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  3027. }
  3028. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  3029. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  3030. #if defined(VK_KHR_shader_bfloat16)
  3031. device->bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3032. #else
  3033. device->bf16 = false;
  3034. #endif
  3035. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  3036. if (device->subgroup_size_control) {
  3037. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  3038. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  3039. device_extensions.push_back("VK_EXT_subgroup_size_control");
  3040. }
  3041. device->subgroup_size_control = device->subgroup_size_control &&
  3042. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  3043. subgroup_size_control_features.subgroupSizeControl;
  3044. if (device->subgroup_size_control) {
  3045. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  3046. }
  3047. #if defined(VK_KHR_cooperative_matrix)
  3048. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  3049. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  3050. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  3051. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  3052. device->subgroup_max_size >= 32;
  3053. #endif
  3054. if (coopmat2_support) {
  3055. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3056. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  3057. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  3058. coopmat2_features.cooperativeMatrixReductions &&
  3059. coopmat2_features.cooperativeMatrixConversions &&
  3060. coopmat2_features.cooperativeMatrixPerElementOperations &&
  3061. coopmat2_features.cooperativeMatrixTensorAddressing &&
  3062. coopmat2_features.cooperativeMatrixBlockLoads &&
  3063. vk12_features.bufferDeviceAddress) {
  3064. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  3065. uint32_t count = 0;
  3066. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  3067. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  3068. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  3069. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  3070. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  3071. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  3072. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  3073. flexible_dimensions.resize(count, empty_prop);
  3074. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  3075. bool found_fp16_128 = false,
  3076. found_fp16_256 = false,
  3077. found_fp32_128 = false,
  3078. found_fp32_256 = false;
  3079. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  3080. // with 32x16x16 and 256 with 32x32x16.
  3081. for (auto &prop : flexible_dimensions) {
  3082. if (prop.saturatingAccumulation == VK_FALSE &&
  3083. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  3084. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3085. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3086. if (prop.workgroupInvocations == 128 &&
  3087. prop.MGranularity <= 32 &&
  3088. prop.NGranularity <= 16 &&
  3089. prop.KGranularity <= 16) {
  3090. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3091. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3092. found_fp16_128 = true;
  3093. }
  3094. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3095. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3096. found_fp32_128 = true;
  3097. }
  3098. }
  3099. if (prop.workgroupInvocations == 256 &&
  3100. prop.MGranularity <= 32 &&
  3101. prop.NGranularity <= 32 &&
  3102. prop.KGranularity <= 16) {
  3103. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3104. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3105. found_fp16_256 = true;
  3106. }
  3107. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3108. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3109. found_fp32_256 = true;
  3110. }
  3111. }
  3112. }
  3113. }
  3114. if (found_fp16_128 && found_fp16_256 &&
  3115. found_fp32_128 && found_fp32_256 &&
  3116. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  3117. device->coopmat2 = true;
  3118. }
  3119. }
  3120. #endif
  3121. }
  3122. if (!vk11_features.storageBuffer16BitAccess) {
  3123. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  3124. throw std::runtime_error("Unsupported device");
  3125. }
  3126. device_extensions.push_back("VK_KHR_16bit_storage");
  3127. #ifdef GGML_VULKAN_VALIDATE
  3128. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  3129. #endif
  3130. if (device->fp16) {
  3131. device_extensions.push_back("VK_KHR_shader_float16_int8");
  3132. }
  3133. #if defined(VK_KHR_cooperative_matrix)
  3134. if (device->coopmat_support) {
  3135. // Query supported shapes
  3136. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  3137. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  3138. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  3139. uint32_t cm_props_num;
  3140. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  3141. cm_props.resize(cm_props_num);
  3142. for (auto& prop : cm_props) {
  3143. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  3144. }
  3145. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  3146. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  3147. for (auto& prop : cm_props) {
  3148. VK_LOG_DEBUG("ggml_vulkan: M: " << prop.MSize << " N: " << prop.NSize << " K: " << prop.KSize << " A: " << vk::to_string((vk::ComponentTypeKHR)prop.AType) << " B: " << vk::to_string((vk::ComponentTypeKHR)prop.BType) << " C: " << vk::to_string((vk::ComponentTypeKHR)prop.CType) << " Result: " << vk::to_string((vk::ComponentTypeKHR)prop.ResultType) << " saturatingAccumulation: " << prop.saturatingAccumulation << " scope: " << vk::to_string((vk::ScopeKHR)prop.scope));
  3149. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  3150. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  3151. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3152. ) {
  3153. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  3154. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  3155. // coopmat sizes not set yet
  3156. if (device->coopmat_m == 0) {
  3157. device->coopmat_acc_f32_support = true;
  3158. device->coopmat_m = prop.MSize;
  3159. device->coopmat_n = prop.NSize;
  3160. device->coopmat_k = prop.KSize;
  3161. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3162. // Only enable if shape is identical
  3163. device->coopmat_acc_f32_support = true;
  3164. }
  3165. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3166. device->coopmat_support_16x16x16_f32acc = true;
  3167. }
  3168. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  3169. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  3170. // coopmat sizes not set yet
  3171. if (device->coopmat_m == 0) {
  3172. device->coopmat_acc_f16_support = true;
  3173. device->coopmat_m = prop.MSize;
  3174. device->coopmat_n = prop.NSize;
  3175. device->coopmat_k = prop.KSize;
  3176. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3177. // Only enable if shape is identical
  3178. device->coopmat_acc_f16_support = true;
  3179. }
  3180. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3181. device->coopmat_support_16x16x16_f16acc = true;
  3182. }
  3183. }
  3184. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  3185. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  3186. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  3187. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  3188. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  3189. device->coopmat_int_m == 0
  3190. ) {
  3191. device->coopmat_int_support = true;
  3192. device->coopmat_int_m = prop.MSize;
  3193. device->coopmat_int_n = prop.NSize;
  3194. device->coopmat_int_k = prop.KSize;
  3195. }
  3196. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3197. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3198. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3199. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3200. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3201. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3202. ) {
  3203. // coopmat sizes not set yet
  3204. if (device->coopmat_m == 0) {
  3205. device->coopmat_bf16_support = true;
  3206. device->coopmat_m = prop.MSize;
  3207. device->coopmat_n = prop.NSize;
  3208. device->coopmat_k = prop.KSize;
  3209. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3210. // Only enable if shape is identical
  3211. device->coopmat_bf16_support = true;
  3212. }
  3213. }
  3214. #endif
  3215. }
  3216. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  3217. // No suitable matmul mode found
  3218. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  3219. device->coopmat_support = false;
  3220. }
  3221. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3222. device->coopmat_bf16_support = false;
  3223. }
  3224. }
  3225. if (device->coopmat_support) {
  3226. device_extensions.push_back("VK_KHR_cooperative_matrix");
  3227. }
  3228. #if defined(VK_KHR_shader_bfloat16)
  3229. if (device->coopmat_bf16_support) {
  3230. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3231. }
  3232. #endif
  3233. #endif
  3234. device->name = GGML_VK_NAME + std::to_string(idx);
  3235. device_create_info = {
  3236. vk::DeviceCreateFlags(),
  3237. device_queue_create_infos,
  3238. {},
  3239. device_extensions
  3240. };
  3241. device_create_info.setPNext(&device_features2);
  3242. device->device = device->physical_device.createDevice(device_create_info);
  3243. // Queues
  3244. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  3245. // Shaders
  3246. // Disable matmul tile sizes early if performance low or not supported
  3247. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3248. switch (device->vendor_id) {
  3249. #ifndef GGML_VULKAN_RUN_TESTS
  3250. case VK_VENDOR_ID_AMD:
  3251. case VK_VENDOR_ID_INTEL:
  3252. device->mul_mat_l[i] = false;
  3253. device->mul_mat_m[i] = true;
  3254. device->mul_mat_s[i] = true;
  3255. device->mul_mat_id_l[i] = false;
  3256. device->mul_mat_id_m[i] = true;
  3257. device->mul_mat_id_s[i] = true;
  3258. break;
  3259. case VK_VENDOR_ID_APPLE:
  3260. device->mul_mat_l[i] = false;
  3261. device->mul_mat_m[i] = true;
  3262. device->mul_mat_s[i] = false;
  3263. device->mul_mat_id_l[i] = false;
  3264. device->mul_mat_id_m[i] = true;
  3265. device->mul_mat_id_s[i] = false;
  3266. break;
  3267. #endif
  3268. default:
  3269. device->mul_mat_l[i] = true;
  3270. device->mul_mat_m[i] = true;
  3271. device->mul_mat_s[i] = true;
  3272. device->mul_mat_id_l[i] = true;
  3273. device->mul_mat_id_m[i] = true;
  3274. device->mul_mat_id_s[i] = true;
  3275. break;
  3276. }
  3277. }
  3278. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3279. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3280. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3281. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3282. dsl_binding_flags.push_back({});
  3283. }
  3284. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3285. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3286. {},
  3287. dsl_binding);
  3288. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3289. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3290. ggml_vk_load_shaders(device);
  3291. if (!device->single_queue) {
  3292. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3293. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3294. } else {
  3295. // TODO: Use pointer or reference to avoid copy
  3296. device->transfer_queue.copyFrom(device->compute_queue);
  3297. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3298. }
  3299. device->buffer_type = {
  3300. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3301. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3302. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3303. };
  3304. device->fence = device->device.createFence({});
  3305. device->idx = idx;
  3306. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3307. return device;
  3308. }
  3309. return vk_instance.devices[idx];
  3310. }
  3311. static void ggml_vk_print_gpu_info(size_t idx) {
  3312. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3313. size_t dev_num = vk_instance.device_indices[idx];
  3314. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3315. GGML_ASSERT(vk_instance_initialized);
  3316. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3317. if (dev_num >= devices.size()) {
  3318. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3319. throw std::runtime_error("Device not found");
  3320. }
  3321. vk::PhysicalDevice physical_device = devices[dev_num];
  3322. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3323. bool fp16_storage = false;
  3324. bool fp16_compute = false;
  3325. bool coopmat_support = false;
  3326. bool coopmat2_support = false;
  3327. bool integer_dot_product = false;
  3328. bool bfloat16_support = false;
  3329. for (auto properties : ext_props) {
  3330. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3331. fp16_storage = true;
  3332. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3333. fp16_compute = true;
  3334. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3335. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3336. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3337. coopmat_support = true;
  3338. #endif
  3339. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3340. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3341. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3342. coopmat2_support = true;
  3343. #endif
  3344. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3345. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3346. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3347. integer_dot_product = true;
  3348. #endif
  3349. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3350. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3351. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3352. bfloat16_support = true;
  3353. #endif
  3354. }
  3355. }
  3356. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3357. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3358. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3359. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3360. vk::PhysicalDeviceProperties2 props2;
  3361. vk::PhysicalDeviceMaintenance3Properties props3;
  3362. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3363. vk::PhysicalDeviceDriverProperties driver_props;
  3364. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3365. props2.pNext = &props3;
  3366. props3.pNext = &subgroup_props;
  3367. subgroup_props.pNext = &driver_props;
  3368. // Pointer to the last chain element
  3369. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3370. if (integer_dot_product) {
  3371. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3372. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3373. }
  3374. physical_device.getProperties2(&props2);
  3375. VkPhysicalDeviceFeatures2 device_features2;
  3376. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3377. device_features2.pNext = nullptr;
  3378. VkPhysicalDeviceVulkan11Features vk11_features;
  3379. vk11_features.pNext = nullptr;
  3380. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3381. device_features2.pNext = &vk11_features;
  3382. VkPhysicalDeviceVulkan12Features vk12_features;
  3383. vk12_features.pNext = nullptr;
  3384. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3385. vk11_features.pNext = &vk12_features;
  3386. // Pointer to the last chain element
  3387. last_struct = (VkBaseOutStructure *)&vk12_features;
  3388. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3389. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3390. coopmat_features.pNext = nullptr;
  3391. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3392. coopmat_features.cooperativeMatrix = VK_FALSE;
  3393. if (coopmat_support) {
  3394. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3395. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3396. }
  3397. #endif
  3398. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3399. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3400. if (integer_dot_product) {
  3401. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3402. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3403. }
  3404. #if defined(VK_KHR_shader_bfloat16)
  3405. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3406. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3407. if (bfloat16_support) {
  3408. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3409. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3410. }
  3411. #endif
  3412. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3413. fp16 = fp16 && vk12_features.shaderFloat16;
  3414. #if defined(VK_KHR_shader_bfloat16)
  3415. bool bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3416. #else
  3417. bool bf16 = false;
  3418. #endif
  3419. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3420. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3421. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3422. integer_dot_product = integer_dot_product
  3423. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3424. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3425. coopmat_support = coopmat_support
  3426. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3427. && coopmat_features.cooperativeMatrix
  3428. #endif
  3429. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3430. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3431. std::string device_name = props2.properties.deviceName.data();
  3432. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | bf16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  3433. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, bf16, subgroup_size,
  3434. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3435. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3436. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3437. }
  3438. }
  3439. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3440. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3441. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3442. static void ggml_vk_instance_init() {
  3443. if (vk_instance_initialized) {
  3444. return;
  3445. }
  3446. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3447. uint32_t api_version = vk::enumerateInstanceVersion();
  3448. if (api_version < VK_API_VERSION_1_2) {
  3449. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3450. GGML_ABORT("fatal error");
  3451. }
  3452. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3453. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3454. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3455. #ifdef __APPLE__
  3456. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3457. #endif
  3458. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3459. std::vector<const char*> layers;
  3460. if (validation_ext) {
  3461. layers.push_back("VK_LAYER_KHRONOS_validation");
  3462. }
  3463. std::vector<const char*> extensions;
  3464. if (validation_ext) {
  3465. extensions.push_back("VK_EXT_validation_features");
  3466. }
  3467. #ifdef __APPLE__
  3468. if (portability_enumeration_ext) {
  3469. extensions.push_back("VK_KHR_portability_enumeration");
  3470. }
  3471. #endif
  3472. if (debug_utils_ext) {
  3473. extensions.push_back("VK_EXT_debug_utils");
  3474. }
  3475. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3476. #ifdef __APPLE__
  3477. if (portability_enumeration_ext) {
  3478. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3479. }
  3480. #endif
  3481. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3482. vk::ValidationFeaturesEXT validation_features;
  3483. if (validation_ext) {
  3484. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3485. validation_features = {
  3486. features_enable,
  3487. {},
  3488. };
  3489. validation_features.setPNext(nullptr);
  3490. instance_create_info.setPNext(&validation_features);
  3491. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3492. }
  3493. vk_instance.instance = vk::createInstance(instance_create_info);
  3494. vk_instance_initialized = true;
  3495. if (debug_utils_ext) {
  3496. vk_instance.debug_utils_support = true;
  3497. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3498. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3499. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3500. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3501. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3502. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3503. }
  3504. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3505. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3506. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3507. if (devices_env != nullptr) {
  3508. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  3509. std::string devices(devices_env);
  3510. std::replace(devices.begin(), devices.end(), ',', ' ');
  3511. std::stringstream ss(devices);
  3512. size_t tmp;
  3513. while (ss >> tmp) {
  3514. if(tmp >= num_available_devices) {
  3515. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3516. throw std::runtime_error("Invalid Vulkan device index");
  3517. }
  3518. vk_instance.device_indices.push_back(tmp);
  3519. }
  3520. } else {
  3521. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3522. // If no vulkan devices are found, return early
  3523. if (devices.empty()) {
  3524. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3525. return;
  3526. }
  3527. // Default to using all dedicated GPUs
  3528. for (size_t i = 0; i < devices.size(); i++) {
  3529. vk::PhysicalDeviceProperties2 new_props;
  3530. vk::PhysicalDeviceDriverProperties new_driver;
  3531. vk::PhysicalDeviceIDProperties new_id;
  3532. new_props.pNext = &new_driver;
  3533. new_driver.pNext = &new_id;
  3534. devices[i].getProperties2(&new_props);
  3535. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3536. // Check if there are two physical devices corresponding to the same GPU
  3537. auto old_device = std::find_if(
  3538. vk_instance.device_indices.begin(),
  3539. vk_instance.device_indices.end(),
  3540. [&devices, &new_id](const size_t k){
  3541. vk::PhysicalDeviceProperties2 old_props;
  3542. vk::PhysicalDeviceIDProperties old_id;
  3543. old_props.pNext = &old_id;
  3544. devices[k].getProperties2(&old_props);
  3545. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3546. }
  3547. );
  3548. if (old_device == vk_instance.device_indices.end()) {
  3549. vk_instance.device_indices.push_back(i);
  3550. } else {
  3551. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3552. // This can cause error when splitting layers aross the devices, need to keep only 1
  3553. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3554. vk::PhysicalDeviceProperties2 old_props;
  3555. vk::PhysicalDeviceDriverProperties old_driver;
  3556. old_props.pNext = &old_driver;
  3557. devices[*old_device].getProperties2(&old_props);
  3558. std::map<vk::DriverId, int> driver_priorities {};
  3559. int old_priority = std::numeric_limits<int>::max();
  3560. int new_priority = std::numeric_limits<int>::max();
  3561. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3562. // Smaller number -> higher priority
  3563. switch (old_props.properties.vendorID) {
  3564. case VK_VENDOR_ID_AMD:
  3565. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3566. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3567. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3568. break;
  3569. case VK_VENDOR_ID_INTEL:
  3570. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3571. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3572. break;
  3573. case VK_VENDOR_ID_NVIDIA:
  3574. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3575. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3576. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3577. #endif
  3578. break;
  3579. }
  3580. if (driver_priorities.count(old_driver.driverID)) {
  3581. old_priority = driver_priorities[old_driver.driverID];
  3582. }
  3583. if (driver_priorities.count(new_driver.driverID)) {
  3584. new_priority = driver_priorities[new_driver.driverID];
  3585. }
  3586. if (new_priority < old_priority) {
  3587. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3588. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3589. vk_instance.device_indices.push_back(i);
  3590. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3591. }
  3592. else {
  3593. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3594. }
  3595. }
  3596. }
  3597. }
  3598. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3599. // If only CPU devices are available, return without devices.
  3600. if (vk_instance.device_indices.empty()) {
  3601. for (size_t i = 0; i < devices.size(); i++) {
  3602. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3603. vk_instance.device_indices.push_back(i);
  3604. break;
  3605. }
  3606. }
  3607. }
  3608. if (vk_instance.device_indices.empty()) {
  3609. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3610. return;
  3611. }
  3612. }
  3613. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3614. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3615. ggml_vk_print_gpu_info(i);
  3616. }
  3617. }
  3618. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3619. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3620. ggml_vk_instance_init();
  3621. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3622. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3623. ctx->device = ggml_vk_get_device(idx);
  3624. ctx->semaphore_idx = 0;
  3625. ctx->event_idx = 0;
  3626. ctx->prealloc_size_x = 0;
  3627. ctx->prealloc_size_y = 0;
  3628. ctx->prealloc_size_split_k = 0;
  3629. ctx->fence = ctx->device->device.createFence({});
  3630. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3631. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3632. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3633. #ifdef GGML_VULKAN_CHECK_RESULTS
  3634. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3635. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3636. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3637. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3638. #endif
  3639. }
  3640. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3641. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3642. switch (type) {
  3643. case GGML_TYPE_F32:
  3644. case GGML_TYPE_Q4_0:
  3645. case GGML_TYPE_Q4_1:
  3646. case GGML_TYPE_Q5_0:
  3647. case GGML_TYPE_Q5_1:
  3648. case GGML_TYPE_Q8_0:
  3649. case GGML_TYPE_Q2_K:
  3650. case GGML_TYPE_Q3_K:
  3651. case GGML_TYPE_Q4_K:
  3652. case GGML_TYPE_Q5_K:
  3653. case GGML_TYPE_Q6_K:
  3654. case GGML_TYPE_IQ1_S:
  3655. case GGML_TYPE_IQ1_M:
  3656. case GGML_TYPE_IQ2_XXS:
  3657. case GGML_TYPE_IQ2_XS:
  3658. case GGML_TYPE_IQ2_S:
  3659. case GGML_TYPE_IQ3_XXS:
  3660. case GGML_TYPE_IQ3_S:
  3661. case GGML_TYPE_IQ4_XS:
  3662. case GGML_TYPE_IQ4_NL:
  3663. case GGML_TYPE_MXFP4:
  3664. break;
  3665. default:
  3666. return nullptr;
  3667. }
  3668. return ctx->device->pipeline_dequant[type];
  3669. }
  3670. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  3671. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3672. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3673. return ctx->device->pipeline_matmul_f32;
  3674. }
  3675. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3676. return ctx->device->pipeline_matmul_f32_f16;
  3677. }
  3678. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3679. return ctx->device->pipeline_matmul_bf16;
  3680. }
  3681. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3682. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3683. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3684. }
  3685. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3686. return ctx->device->pipeline_matmul_f16.f16acc;
  3687. }
  3688. } else {
  3689. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3690. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3691. }
  3692. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3693. return ctx->device->pipeline_matmul_f16.f32acc;
  3694. }
  3695. }
  3696. // MMQ
  3697. if (src1_type == GGML_TYPE_Q8_1) {
  3698. vk_matmul_pipeline pipelines = (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f32acc;
  3699. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3700. return nullptr;
  3701. }
  3702. return pipelines;
  3703. }
  3704. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3705. return nullptr;
  3706. }
  3707. switch (src0_type) {
  3708. case GGML_TYPE_Q4_0:
  3709. case GGML_TYPE_Q4_1:
  3710. case GGML_TYPE_Q5_0:
  3711. case GGML_TYPE_Q5_1:
  3712. case GGML_TYPE_Q8_0:
  3713. case GGML_TYPE_Q2_K:
  3714. case GGML_TYPE_Q3_K:
  3715. case GGML_TYPE_Q4_K:
  3716. case GGML_TYPE_Q5_K:
  3717. case GGML_TYPE_Q6_K:
  3718. case GGML_TYPE_IQ1_S:
  3719. case GGML_TYPE_IQ1_M:
  3720. case GGML_TYPE_IQ2_XXS:
  3721. case GGML_TYPE_IQ2_XS:
  3722. case GGML_TYPE_IQ2_S:
  3723. case GGML_TYPE_IQ3_XXS:
  3724. case GGML_TYPE_IQ3_S:
  3725. case GGML_TYPE_IQ4_XS:
  3726. case GGML_TYPE_IQ4_NL:
  3727. case GGML_TYPE_MXFP4:
  3728. break;
  3729. default:
  3730. return nullptr;
  3731. }
  3732. if (ctx->device->coopmat2) {
  3733. assert(src1_type == GGML_TYPE_F16);
  3734. return prec == GGML_PREC_DEFAULT ? ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f32acc;
  3735. }
  3736. if (ctx->device->coopmat_support) {
  3737. return (ctx->device->fp16 && ctx->device->coopmat_acc_f16_support && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3738. }
  3739. return (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  3740. }
  3741. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type, uint32_t num_cols) {
  3742. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3743. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3744. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3745. switch (a_type) {
  3746. case GGML_TYPE_F32:
  3747. case GGML_TYPE_F16:
  3748. case GGML_TYPE_BF16:
  3749. case GGML_TYPE_Q4_0:
  3750. case GGML_TYPE_Q4_1:
  3751. case GGML_TYPE_Q5_0:
  3752. case GGML_TYPE_Q5_1:
  3753. case GGML_TYPE_Q8_0:
  3754. case GGML_TYPE_Q2_K:
  3755. case GGML_TYPE_Q3_K:
  3756. case GGML_TYPE_Q4_K:
  3757. case GGML_TYPE_Q5_K:
  3758. case GGML_TYPE_Q6_K:
  3759. case GGML_TYPE_IQ1_S:
  3760. case GGML_TYPE_IQ1_M:
  3761. case GGML_TYPE_IQ2_XXS:
  3762. case GGML_TYPE_IQ2_XS:
  3763. case GGML_TYPE_IQ2_S:
  3764. case GGML_TYPE_IQ3_XXS:
  3765. case GGML_TYPE_IQ3_S:
  3766. case GGML_TYPE_IQ4_XS:
  3767. case GGML_TYPE_IQ4_NL:
  3768. case GGML_TYPE_MXFP4:
  3769. break;
  3770. default:
  3771. return nullptr;
  3772. }
  3773. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  3774. }
  3775. 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, ggml_prec prec) {
  3776. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3777. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3778. return ctx->device->pipeline_matmul_id_f32;
  3779. }
  3780. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3781. return ctx->device->pipeline_matmul_id_bf16;
  3782. }
  3783. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3784. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3785. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3786. }
  3787. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3788. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3789. }
  3790. } else {
  3791. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3792. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3793. }
  3794. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3795. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3796. }
  3797. }
  3798. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3799. switch (src0_type) {
  3800. case GGML_TYPE_Q4_0:
  3801. case GGML_TYPE_Q4_1:
  3802. case GGML_TYPE_Q5_0:
  3803. case GGML_TYPE_Q5_1:
  3804. case GGML_TYPE_Q8_0:
  3805. case GGML_TYPE_Q2_K:
  3806. case GGML_TYPE_Q3_K:
  3807. case GGML_TYPE_Q4_K:
  3808. case GGML_TYPE_Q5_K:
  3809. case GGML_TYPE_Q6_K:
  3810. case GGML_TYPE_IQ1_S:
  3811. case GGML_TYPE_IQ1_M:
  3812. case GGML_TYPE_IQ2_XXS:
  3813. case GGML_TYPE_IQ2_XS:
  3814. case GGML_TYPE_IQ2_S:
  3815. case GGML_TYPE_IQ3_XXS:
  3816. case GGML_TYPE_IQ3_S:
  3817. case GGML_TYPE_IQ4_XS:
  3818. case GGML_TYPE_IQ4_NL:
  3819. case GGML_TYPE_MXFP4:
  3820. break;
  3821. default:
  3822. return nullptr;
  3823. }
  3824. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3825. }
  3826. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3827. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3828. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3829. switch (a_type) {
  3830. case GGML_TYPE_F32:
  3831. case GGML_TYPE_F16:
  3832. case GGML_TYPE_BF16:
  3833. case GGML_TYPE_Q4_0:
  3834. case GGML_TYPE_Q4_1:
  3835. case GGML_TYPE_Q5_0:
  3836. case GGML_TYPE_Q5_1:
  3837. case GGML_TYPE_Q8_0:
  3838. case GGML_TYPE_Q2_K:
  3839. case GGML_TYPE_Q3_K:
  3840. case GGML_TYPE_Q4_K:
  3841. case GGML_TYPE_Q5_K:
  3842. case GGML_TYPE_Q6_K:
  3843. case GGML_TYPE_IQ1_S:
  3844. case GGML_TYPE_IQ1_M:
  3845. case GGML_TYPE_IQ2_XXS:
  3846. case GGML_TYPE_IQ2_XS:
  3847. case GGML_TYPE_IQ2_S:
  3848. case GGML_TYPE_IQ3_XXS:
  3849. case GGML_TYPE_IQ3_S:
  3850. case GGML_TYPE_IQ4_XS:
  3851. case GGML_TYPE_IQ4_NL:
  3852. case GGML_TYPE_MXFP4:
  3853. break;
  3854. default:
  3855. return nullptr;
  3856. }
  3857. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3858. }
  3859. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3860. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3861. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3862. int best_i = -1;
  3863. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3864. int worst_i = -1;
  3865. size_t worst_size = 0; //largest unused buffer seen so far
  3866. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3867. vk_buffer &b = ctx->buffer_pool[i];
  3868. if (b != nullptr && b->size >= size && b->size < best_size) {
  3869. best_i = i;
  3870. best_size = b->size;
  3871. }
  3872. if (b != nullptr && b->size > worst_size) {
  3873. worst_i = i;
  3874. worst_size = b->size;
  3875. }
  3876. }
  3877. if(best_i != -1) {
  3878. //found the smallest buffer that fits our needs
  3879. vk_buffer b = ctx->buffer_pool[best_i];
  3880. ctx->buffer_pool[best_i].reset();
  3881. return b;
  3882. }
  3883. if(worst_i != -1) {
  3884. //no buffer that fits our needs, resize largest one to save memory
  3885. vk_buffer& b = ctx->buffer_pool[worst_i];
  3886. ggml_vk_destroy_buffer(b);
  3887. }
  3888. return ggml_vk_create_buffer_device(ctx->device, size);
  3889. }
  3890. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3891. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3892. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3893. vk_buffer& b = ctx->buffer_pool[i];
  3894. if (b == nullptr) {
  3895. b = buffer;
  3896. return;
  3897. }
  3898. }
  3899. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3900. ggml_vk_destroy_buffer(buffer);
  3901. }
  3902. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3903. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3904. // Try to find existing temp buffer with enough capacity
  3905. for (auto& buffer : ctx->gc.temp_buffers) {
  3906. if (buffer->size >= size) {
  3907. return buffer;
  3908. }
  3909. }
  3910. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3911. // Otherwise create new buffer
  3912. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3913. ctx->gc.temp_buffers.push_back(buf);
  3914. return buf;
  3915. }
  3916. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3917. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3918. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3919. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3920. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3921. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3922. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3923. size/1024.0/1024.0);
  3924. device->device.freeMemory(buf->device_memory);
  3925. device->device.destroyBuffer(buf->buffer);
  3926. return nullptr;
  3927. }
  3928. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3929. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3930. return buf->ptr;
  3931. }
  3932. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3933. if (ptr == nullptr) {
  3934. return;
  3935. }
  3936. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3937. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3938. vk_buffer buf;
  3939. size_t index;
  3940. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3941. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3942. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3943. if (ptr >= addr && ptr < endr) {
  3944. buf = std::get<2>(device->pinned_memory[i]);
  3945. index = i;
  3946. break;
  3947. }
  3948. }
  3949. if (buf == nullptr) {
  3950. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3951. return;
  3952. }
  3953. ggml_vk_destroy_buffer(buf);
  3954. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3955. }
  3956. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3957. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3958. buf = nullptr;
  3959. buf_offset = 0;
  3960. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3961. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3962. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3963. if (ptr >= addr && ptr < endr) {
  3964. buf = std::get<2>(device->pinned_memory[i]);
  3965. buf_offset = ((const uint8_t *)ptr) - addr;
  3966. break;
  3967. }
  3968. }
  3969. }
  3970. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  3971. vk_submission s;
  3972. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  3973. if (one_time) {
  3974. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3975. } else {
  3976. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3977. }
  3978. return s;
  3979. }
  3980. template <typename T> size_t push_constant_size(const T &t) {
  3981. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3982. GGML_UNUSED(t);
  3983. return sizeof(T);
  3984. }
  3985. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  3986. GGML_UNUSED(t);
  3987. return sizeof(T) * t.size();
  3988. }
  3989. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  3990. GGML_UNUSED(t);
  3991. return sizeof(T) * N;
  3992. }
  3993. template <typename T> const T *push_constant_data(const T &t) {
  3994. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3995. return &t;
  3996. }
  3997. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  3998. return t.data();
  3999. }
  4000. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  4001. return t.data();
  4002. }
  4003. template <typename T>
  4004. 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, const T &push_constants, std::array<uint32_t, 3> elements) {
  4005. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  4006. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  4007. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  4008. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  4009. for (auto& buffer : descriptor_buffer_infos) {
  4010. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  4011. }
  4012. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  4013. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  4014. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  4015. GGML_ASSERT(pipeline->parameter_count == descriptor_buffer_infos.size());
  4016. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  4017. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  4018. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  4019. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  4020. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  4021. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  4022. pipeline->layout,
  4023. 0,
  4024. { descriptor_set },
  4025. {});
  4026. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  4027. }
  4028. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  4029. s.buffer.end();
  4030. s.wait_semaphores = std::move(wait_semaphores);
  4031. s.signal_semaphores = std::move(signal_semaphores);
  4032. }
  4033. static void ggml_vk_ctx_end(vk_context& ctx) {
  4034. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  4035. if (ctx->s == nullptr) {
  4036. return;
  4037. }
  4038. ctx->s->buffer.end();
  4039. ctx->s = nullptr;
  4040. }
  4041. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  4042. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  4043. if (subctx->s != nullptr) {
  4044. ggml_vk_ctx_end(subctx);
  4045. }
  4046. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  4047. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  4048. }
  4049. static size_t ggml_vk_align_size(size_t width, size_t align) {
  4050. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  4051. return CEIL_DIV(width, align) * align;
  4052. }
  4053. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  4054. if (memcpys == nullptr) {
  4055. memcpy(dst, src, size);
  4056. } else {
  4057. memcpys->emplace_back(dst, src, size);
  4058. }
  4059. }
  4060. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  4061. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  4062. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  4063. ggml_vk_destroy_buffer(device->sync_staging);
  4064. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  4065. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4066. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4067. }
  4068. }
  4069. 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) {
  4070. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  4071. GGML_ASSERT(!ggml_is_contiguous(tensor));
  4072. // Buffer is already mapped
  4073. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4074. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4075. GGML_ABORT("fatal error");
  4076. }
  4077. // Check if src is pinned memory
  4078. vk_buffer buf = nullptr;
  4079. size_t buf_offset = 0;
  4080. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  4081. const uint64_t ne0 = tensor->ne[0];
  4082. const uint64_t ne1 = tensor->ne[1];
  4083. const uint64_t ne2 = tensor->ne[2];
  4084. const uint64_t ne3 = tensor->ne[3];
  4085. const uint64_t nb0 = tensor->nb[0];
  4086. const uint64_t nb1 = tensor->nb[1];
  4087. const uint64_t nb2 = tensor->nb[2];
  4088. const uint64_t nb3 = tensor->nb[3];
  4089. const ggml_type type = tensor->type;
  4090. const uint64_t ts = ggml_type_size(type);
  4091. const uint64_t bs = ggml_blck_size(type);
  4092. const uint64_t dstnb0 = ts;
  4093. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  4094. const uint64_t dstnb2 = dstnb1*ne1;
  4095. const uint64_t dstnb3 = dstnb2*ne2;
  4096. const uint64_t ne = ggml_nelements(tensor);
  4097. if (buf != nullptr) {
  4098. // Memory is pinned, use as staging buffer
  4099. std::vector<vk::BufferCopy> slices;
  4100. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4101. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4102. // Find longest contiguous slice
  4103. if (ne1*nb1 == dstnb2) {
  4104. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  4105. } else {
  4106. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4107. if (ne0*nb0/bs == dstnb1) {
  4108. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  4109. } else {
  4110. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4111. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4112. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4113. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  4114. }
  4115. }
  4116. }
  4117. }
  4118. }
  4119. }
  4120. ggml_vk_sync_buffers(subctx);
  4121. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4122. return;
  4123. }
  4124. if (!sync_staging) {
  4125. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4126. }
  4127. // Staging buffer required
  4128. vk_buffer& staging = ctx->device->sync_staging;
  4129. const uint64_t copy_size = ts*ne/bs;
  4130. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  4131. VkBufferCopy buf_copy{ 0, offset, copy_size };
  4132. ggml_vk_sync_buffers(subctx);
  4133. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4134. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4135. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4136. // Find longest contiguous slice
  4137. if (ne1*nb1 == dstnb2) {
  4138. 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);
  4139. } else {
  4140. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4141. if (ne0*nb0/bs == dstnb1) {
  4142. 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);
  4143. } else {
  4144. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4145. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4146. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4147. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  4148. }
  4149. }
  4150. }
  4151. }
  4152. }
  4153. }
  4154. }
  4155. 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) {
  4156. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  4157. // Buffer is already mapped
  4158. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4159. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4160. GGML_ABORT("fatal error");
  4161. }
  4162. // Check if src is pinned memory
  4163. vk_buffer buf = nullptr;
  4164. size_t buf_offset = 0;
  4165. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  4166. if (buf != nullptr) {
  4167. // Memory is pinned, use as staging buffer
  4168. std::vector<vk::BufferCopy> slices(1);
  4169. if (width == spitch) {
  4170. // Only do single write if stride is equal
  4171. slices[0].srcOffset = buf_offset;
  4172. slices[0].dstOffset = offset;
  4173. slices[0].size = width * height;
  4174. } else {
  4175. slices.resize(height);
  4176. for (size_t i = 0; i < height; i++) {
  4177. slices[i].srcOffset = buf_offset + i * spitch;
  4178. slices[i].dstOffset = offset + i * width;
  4179. slices[i].size = width;
  4180. }
  4181. }
  4182. ggml_vk_sync_buffers(subctx);
  4183. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4184. return;
  4185. }
  4186. VK_LOG_DEBUG("STAGING");
  4187. if (!sync_staging) {
  4188. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4189. }
  4190. // Staging buffer required
  4191. const size_t copy_size = width*height;
  4192. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  4193. vk_buffer& staging_buffer = dst->device->sync_staging;
  4194. VkBufferCopy buf_copy = {
  4195. 0,
  4196. offset,
  4197. copy_size};
  4198. ggml_vk_sync_buffers(subctx);
  4199. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4200. if (width == spitch) {
  4201. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  4202. } else {
  4203. for (size_t i = 0; i < height; i++) {
  4204. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  4205. }
  4206. }
  4207. }
  4208. 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) {
  4209. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  4210. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  4211. }
  4212. 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) {
  4213. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  4214. // Buffer is already mapped
  4215. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4216. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4217. for (size_t i = 0; i < height; i++) {
  4218. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  4219. }
  4220. } else {
  4221. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4222. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4223. ggml_vk_ctx_begin(dst->device, subctx);
  4224. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  4225. ggml_vk_ctx_end(subctx);
  4226. for (auto& cpy : subctx->in_memcpys) {
  4227. memcpy(cpy.dst, cpy.src, cpy.n);
  4228. }
  4229. ggml_vk_submit(subctx, dst->device->fence);
  4230. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  4231. dst->device->device.resetFences({ dst->device->fence });
  4232. ggml_vk_queue_command_pools_cleanup(dst->device);
  4233. }
  4234. }
  4235. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  4236. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  4237. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  4238. }
  4239. 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) {
  4240. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  4241. GGML_ASSERT(width > 0);
  4242. GGML_ASSERT(height > 0);
  4243. GGML_ASSERT(src != nullptr);
  4244. // TODO: staging_offset is not used
  4245. // Check if dst is pinned memory
  4246. vk_buffer buf = nullptr;
  4247. size_t buf_offset = 0;
  4248. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  4249. std::vector<vk::BufferCopy> slices(1);
  4250. if (width == spitch && width == dpitch) {
  4251. // Only do single write if stride is equal
  4252. slices[0].srcOffset = offset;
  4253. slices[0].dstOffset = buf_offset;
  4254. slices[0].size = width * height;
  4255. } else {
  4256. slices.resize(height);
  4257. for (size_t i = 0; i < height; i++) {
  4258. slices[i].srcOffset = offset + i * spitch;
  4259. slices[i].dstOffset = buf_offset + i * dpitch;
  4260. slices[i].size = width;
  4261. }
  4262. }
  4263. if (buf != nullptr) {
  4264. // Memory is pinned, use as staging buffer
  4265. ggml_vk_sync_buffers(subctx);
  4266. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  4267. return;
  4268. }
  4269. VK_LOG_DEBUG("STAGING");
  4270. if (!sync_staging) {
  4271. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  4272. }
  4273. // Fall back to staging buffer
  4274. const size_t copy_size = dpitch * height;
  4275. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4276. vk_buffer& staging_buffer = src->device->sync_staging;
  4277. ggml_vk_sync_buffers(subctx);
  4278. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4279. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4280. }
  4281. 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) {
  4282. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4283. }
  4284. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4285. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4286. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4287. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4288. // the HW device to host copy path.
  4289. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4290. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4291. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4292. } else {
  4293. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4294. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4295. ggml_vk_ctx_begin(src->device, subctx);
  4296. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4297. ggml_vk_ctx_end(subctx);
  4298. ggml_vk_submit(subctx, src->device->fence);
  4299. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4300. src->device->device.resetFences({ src->device->fence });
  4301. ggml_vk_queue_command_pools_cleanup(src->device);
  4302. for (auto& cpy : subctx->out_memcpys) {
  4303. memcpy(cpy.dst, cpy.src, cpy.n);
  4304. }
  4305. }
  4306. }
  4307. 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) {
  4308. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4309. // Make sure both buffers are on same device
  4310. GGML_ASSERT(src->device == dst->device);
  4311. VkBufferCopy bc{ src_offset, dst_offset, size };
  4312. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4313. }
  4314. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4315. if (src->device == dst->device) {
  4316. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4317. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4318. // Copy within the device
  4319. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4320. ggml_vk_ctx_begin(src->device, subctx);
  4321. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4322. ggml_vk_ctx_end(subctx);
  4323. ggml_vk_submit(subctx, src->device->fence);
  4324. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4325. src->device->device.resetFences({ src->device->fence });
  4326. ggml_vk_queue_command_pools_cleanup(src->device);
  4327. } else {
  4328. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4329. // Copy device to device
  4330. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4331. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4332. // Copy to src staging buffer
  4333. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4334. // memcpy to dst staging buffer
  4335. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4336. // Copy to dst buffer
  4337. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4338. }
  4339. }
  4340. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4341. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4342. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4343. }
  4344. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4345. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4346. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4347. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4348. ggml_vk_ctx_begin(dst->device, subctx);
  4349. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4350. ggml_vk_ctx_end(subctx);
  4351. ggml_vk_submit(subctx, dst->device->fence);
  4352. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4353. dst->device->device.resetFences({ dst->device->fence });
  4354. ggml_vk_queue_command_pools_cleanup(dst->device);
  4355. }
  4356. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, uint32_t m, uint32_t n, uint32_t k, const vk_pipeline& pipeline) {
  4357. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4358. uint32_t split_k = 1;
  4359. if (ctx->device->shader_core_count != 0 && m >= pipeline->wg_denoms[0] && n >= pipeline->wg_denoms[1]) {
  4360. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4361. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4362. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4363. if (k >= 2048) {
  4364. if (m_tiles * n_tiles <= ctx->device->shader_core_count / 2) {
  4365. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4366. } else if (m_tiles * n_tiles <= ctx->device->shader_core_count * 2 / 3) {
  4367. split_k = 3;
  4368. }
  4369. // Cap the split at 8x. Unless k is huge this is a lot of overhead.
  4370. split_k = std::min(split_k, 8u);
  4371. // ggml_vk_matmul will align the splits to be a multiple of 256.
  4372. // If this rounded up size would cause the last split to be empty,
  4373. // then reduce the split count.
  4374. while (true) {
  4375. if (split_k == 1) {
  4376. break;
  4377. }
  4378. uint32_t k_split = CEIL_DIV(k, split_k);
  4379. k_split = ROUNDUP_POW2(k_split, 256);
  4380. if (k_split * (split_k - 1) < k) {
  4381. break;
  4382. }
  4383. split_k--;
  4384. }
  4385. }
  4386. }
  4387. return split_k;
  4388. }
  4389. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type, ggml_type src1_type) {
  4390. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4391. if (ctx->device->coopmat2) {
  4392. const uint32_t shader_core_count = ctx->device->shader_core_count;
  4393. const uint32_t tiles_l = CEIL_DIV(m, mmp->a_l->wg_denoms[0]) * CEIL_DIV(n, mmp->a_l->wg_denoms[1]);
  4394. const uint32_t tiles_m = CEIL_DIV(m, mmp->a_m->wg_denoms[0]) * CEIL_DIV(n, mmp->a_m->wg_denoms[1]);
  4395. // Use large shader when the N dimension is greater than the medium shader's tile size
  4396. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4397. // Prefer large over medium if either:
  4398. // - medium or large tiles would overfill the GPU
  4399. // - large tiles with a split_k==3 fits in the GPU and medium tiles with split_k==2 does not
  4400. // (medium with split_k==2 is probably better if it fits - more workgroups running and less split_k overhead)
  4401. bool prefer_large = tiles_m > shader_core_count || tiles_l > shader_core_count ||
  4402. // split_k==3 with large tiles likely better than medium tiles with no split_k.
  4403. (tiles_l <= shader_core_count / 3 && tiles_m > shader_core_count / 2);
  4404. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large && prefer_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  4405. return aligned ? mmp->a_l : mmp->l;
  4406. }
  4407. // Use medium shader when the N dimension is greater than the small shader's tile size
  4408. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4409. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4410. return aligned ? mmp->a_m : mmp->m;
  4411. }
  4412. return aligned ? mmp->a_s : mmp->s;
  4413. }
  4414. if ((ctx->device->mul_mat_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_l[src0_type])) {
  4415. return aligned ? mmp->a_s : mmp->s;
  4416. }
  4417. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4418. return aligned ? mmp->a_m : mmp->m;
  4419. }
  4420. return aligned ? mmp->a_l : mmp->l;
  4421. GGML_UNUSED(src1_type);
  4422. }
  4423. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type, ggml_type src1_type) {
  4424. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4425. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4426. }
  4427. static void ggml_vk_matmul(
  4428. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4429. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4430. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4431. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4432. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4433. uint32_t padded_n) {
  4434. 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 << ", padded_n: " << padded_n << ")");
  4435. ggml_vk_sync_buffers(subctx);
  4436. if (split_k == 1) {
  4437. 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, padded_n };
  4438. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4439. return;
  4440. }
  4441. GGML_ASSERT(batch_stride_d == m * n);
  4442. // Round the split size up to a multiple of 256 (k-quant alignment)
  4443. uint32_t k_split = CEIL_DIV(k, split_k);
  4444. k_split = ROUNDUP_POW2(k_split, 256);
  4445. 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, k_split, ne02, ne12, broadcast2, broadcast3, padded_n };
  4446. // Make sure enough workgroups get assigned for split k to work
  4447. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, split_k_buffer }, pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  4448. ggml_vk_sync_buffers(subctx);
  4449. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4450. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4451. }
  4452. static vk_pipeline ggml_vk_guess_matmul_id_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type) {
  4453. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4454. if (ctx->device->coopmat2) {
  4455. // Use large shader when the N dimension is greater than the medium shader's tile size
  4456. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4457. if ((ctx->device->mul_mat_id_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_s[src0_type])) {
  4458. return aligned ? mmp->a_l : mmp->l;
  4459. }
  4460. // Use medium shader when the N dimension is greater than the small shader's tile size
  4461. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4462. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4463. return aligned ? mmp->a_m : mmp->m;
  4464. }
  4465. return aligned ? mmp->a_s : mmp->s;
  4466. }
  4467. if ((ctx->device->mul_mat_id_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_l[src0_type])) {
  4468. return aligned ? mmp->a_s : mmp->s;
  4469. }
  4470. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4471. return aligned ? mmp->a_m : mmp->m;
  4472. }
  4473. return aligned ? mmp->a_l : mmp->l;
  4474. }
  4475. static uint32_t ggml_vk_guess_matmul_id_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type) {
  4476. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4477. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4478. }
  4479. static void ggml_vk_matmul_id(
  4480. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4481. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4482. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4483. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4484. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4485. uint32_t padded_n) {
  4486. 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 << "), " <<
  4487. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4488. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4489. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4490. ggml_vk_sync_buffers(subctx);
  4491. 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,
  4492. nei0, nei1, nbi1, ne11, padded_n };
  4493. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4494. }
  4495. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4496. return
  4497. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4498. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4499. (tensor->ne[3] == 1 || tensor->nb[3] == tensor->nb[2]*tensor->ne[2]);
  4500. }
  4501. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4502. // Choose "contiguous copy" shader if src/dst are contiguous
  4503. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4504. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4505. if (contig) {
  4506. return ctx->device->pipeline_contig_cpy_f32_f32;
  4507. } else {
  4508. return ctx->device->pipeline_cpy_f32_f32;
  4509. }
  4510. }
  4511. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4512. if (contig) {
  4513. return ctx->device->pipeline_contig_cpy_f32_f16;
  4514. } else {
  4515. return ctx->device->pipeline_cpy_f32_f16;
  4516. }
  4517. }
  4518. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4519. if (contig) {
  4520. return ctx->device->pipeline_contig_cpy_f16_f16;
  4521. } else {
  4522. return ctx->device->pipeline_cpy_f16_f16;
  4523. }
  4524. }
  4525. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4526. if (contig) {
  4527. return ctx->device->pipeline_contig_cpy_f16_f32;
  4528. } else {
  4529. return ctx->device->pipeline_cpy_f16_f32;
  4530. }
  4531. }
  4532. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4533. if (contig) {
  4534. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4535. } else {
  4536. return ctx->device->pipeline_cpy_f32_bf16;
  4537. }
  4538. }
  4539. if (src->type == GGML_TYPE_F32) {
  4540. switch (to) {
  4541. case GGML_TYPE_Q4_0:
  4542. case GGML_TYPE_Q4_1:
  4543. case GGML_TYPE_Q5_0:
  4544. case GGML_TYPE_Q5_1:
  4545. case GGML_TYPE_Q8_0:
  4546. case GGML_TYPE_IQ4_NL:
  4547. return ctx->device->pipeline_cpy_f32_quant[to];
  4548. default:
  4549. break;
  4550. }
  4551. }
  4552. if (to == GGML_TYPE_F32) {
  4553. switch (src->type) {
  4554. case GGML_TYPE_Q4_0:
  4555. case GGML_TYPE_Q4_1:
  4556. case GGML_TYPE_Q5_0:
  4557. case GGML_TYPE_Q5_1:
  4558. case GGML_TYPE_Q8_0:
  4559. case GGML_TYPE_IQ4_NL:
  4560. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4561. default:
  4562. break;
  4563. }
  4564. }
  4565. if (src->type == to) {
  4566. // Copy two or four bytes at a time, depending on block size.
  4567. // For quantized types, we scale by block size/type size. But
  4568. // this path is also used for bf16->bf16 for example, where the
  4569. // type size must be exactly 2 or 4.
  4570. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4571. if ((ggml_type_size(src->type) % 4) == 0) {
  4572. if (contig) {
  4573. return ctx->device->pipeline_contig_cpy_f32_f32;
  4574. } else {
  4575. return ctx->device->pipeline_cpy_f32_f32;
  4576. }
  4577. } else {
  4578. if (contig) {
  4579. return ctx->device->pipeline_contig_cpy_f16_f16;
  4580. } else {
  4581. return ctx->device->pipeline_cpy_f16_f16;
  4582. }
  4583. }
  4584. }
  4585. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4586. GGML_ABORT("fatal error");
  4587. }
  4588. 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) {
  4589. 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] << "), ";
  4590. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4591. const int tensor_type_size = ggml_type_size(tensor->type);
  4592. const uint32_t ne = ggml_nelements(tensor);
  4593. std::array<uint32_t, 3> elements;
  4594. if (ne > 262144) {
  4595. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4596. } else if (ne > 512) {
  4597. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4598. } else {
  4599. elements = { ne, 1, 1 };
  4600. }
  4601. vk_op_unary_push_constants pc = {
  4602. (uint32_t)ne,
  4603. (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,
  4604. (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]),
  4605. 0,
  4606. 0.0f, 0.0f,
  4607. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4608. };
  4609. init_pushconst_fastdiv(pc);
  4610. ggml_vk_sync_buffers(subctx);
  4611. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4612. }
  4613. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  4614. switch(type) {
  4615. case GGML_TYPE_Q8_1:
  4616. return ctx->device->pipeline_quantize_q8_1;
  4617. default:
  4618. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4619. GGML_ABORT("fatal error");
  4620. }
  4621. }
  4622. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  4623. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4624. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4625. ggml_vk_sync_buffers(subctx);
  4626. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4627. }
  4628. 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) {
  4629. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << ggml_type_name(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];
  4630. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << ggml_type_name(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];
  4631. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << ggml_type_name(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];
  4632. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4633. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4634. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4635. const uint64_t ne00 = src0->ne[0];
  4636. const uint64_t ne01 = src0->ne[1];
  4637. const uint64_t ne02 = src0->ne[2];
  4638. const uint64_t ne03 = src0->ne[3];
  4639. const uint64_t ne10 = src1->ne[0];
  4640. const uint64_t ne11 = src1->ne[1];
  4641. const uint64_t ne12 = src1->ne[2];
  4642. const uint64_t ne13 = src1->ne[3];
  4643. const uint64_t ne20 = dst->ne[0];
  4644. const uint64_t ne21 = dst->ne[1];
  4645. const uint64_t r2 = ne12 / ne02;
  4646. const uint64_t r3 = ne13 / ne03;
  4647. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4648. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4649. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4650. vk_buffer d_Qx = nullptr;
  4651. size_t qx_buf_offset = 0;
  4652. vk_buffer d_Qy = nullptr;
  4653. size_t qy_buf_offset = 0;
  4654. bool src0_uma = false;
  4655. bool src1_uma = false;
  4656. if (ctx->device->uma) {
  4657. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4658. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4659. src0_uma = d_Qx != nullptr;
  4660. src1_uma = d_Qy != nullptr;
  4661. }
  4662. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4663. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4664. !ggml_vk_dim01_contiguous(src0);
  4665. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4666. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4667. !ggml_vk_dim01_contiguous(src1);
  4668. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4669. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4670. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4671. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4672. // Check for mmq first
  4673. vk_matmul_pipeline mmp = quantize_y ? ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, GGML_TYPE_Q8_1, (ggml_prec)dst->op_params[0]) : nullptr;
  4674. if (mmp == nullptr) {
  4675. // Fall back to f16 dequant mul mat
  4676. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4677. quantize_y = false;
  4678. }
  4679. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4680. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4681. if (qx_needs_dequant) {
  4682. // Fall back to dequant + f16 mulmat
  4683. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, f16_type, y_f32_kernel ? GGML_TYPE_F32 : f16_type, (ggml_prec)dst->op_params[0]);
  4684. }
  4685. // Not implemented
  4686. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4687. const uint32_t kpad = quantize_y ? 0 : ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11, qx_needs_dequant ? f16_type : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type)));
  4688. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4689. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? f16_type : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type));
  4690. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4691. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4692. const int x_ne = ne01 * ne00;
  4693. const int y_ne = padded_n * ne10;
  4694. const int d_ne = ne11 * ne01;
  4695. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4696. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4697. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4698. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4699. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  4700. const uint64_t d_sz = sizeof(float) * d_ne;
  4701. vk_pipeline to_fp16_vk_0 = nullptr;
  4702. vk_pipeline to_fp16_vk_1 = nullptr;
  4703. vk_pipeline to_q8_1 = nullptr;
  4704. if (x_non_contig) {
  4705. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4706. } else {
  4707. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4708. }
  4709. if (y_non_contig) {
  4710. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4711. } else {
  4712. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4713. }
  4714. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4715. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4716. if (quantize_y) {
  4717. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4718. }
  4719. if (dryrun) {
  4720. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4721. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4722. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4723. if (
  4724. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4725. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4726. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4727. GGML_ABORT("Requested preallocation size is too large");
  4728. }
  4729. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4730. ctx->prealloc_size_x = x_sz_upd;
  4731. }
  4732. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  4733. ctx->prealloc_size_y = y_sz_upd;
  4734. }
  4735. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  4736. ctx->prealloc_size_split_k = split_k_size;
  4737. }
  4738. // Request descriptor sets
  4739. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4740. if (qx_needs_dequant) {
  4741. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4742. }
  4743. if (qy_needs_dequant) {
  4744. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4745. }
  4746. if (quantize_y) {
  4747. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  4748. }
  4749. if (split_k > 1) {
  4750. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  4751. }
  4752. return;
  4753. }
  4754. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4755. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4756. GGML_ASSERT(d_D != nullptr);
  4757. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  4758. vk_buffer d_X;
  4759. uint64_t x_buf_offset = 0;
  4760. vk_buffer d_Y;
  4761. uint64_t y_buf_offset = 0;
  4762. if (!src0_uma) {
  4763. d_Qx = src0_buf_ctx->dev_buffer;
  4764. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4765. GGML_ASSERT(d_Qx != nullptr);
  4766. }
  4767. if (!src1_uma) {
  4768. d_Qy = src1_buf_ctx->dev_buffer;
  4769. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4770. GGML_ASSERT(d_Qy != nullptr);
  4771. }
  4772. if (qx_needs_dequant) {
  4773. d_X = ctx->prealloc_x;
  4774. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4775. } else {
  4776. d_X = d_Qx;
  4777. x_buf_offset = qx_buf_offset;
  4778. GGML_ASSERT(qx_sz == x_sz);
  4779. }
  4780. if (qy_needs_dequant) {
  4781. d_Y = ctx->prealloc_y;
  4782. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4783. } else if (quantize_y) {
  4784. d_Y = ctx->prealloc_y;
  4785. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  4786. } else {
  4787. d_Y = d_Qy;
  4788. y_buf_offset = qy_buf_offset;
  4789. GGML_ASSERT(qy_sz == y_sz);
  4790. }
  4791. if (x_non_contig) {
  4792. 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 });
  4793. } else if (qx_needs_dequant) {
  4794. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4795. ggml_vk_sync_buffers(subctx);
  4796. 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, { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  4797. }
  4798. if (y_non_contig) {
  4799. 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 });
  4800. }
  4801. if (quantize_y) {
  4802. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13);
  4803. }
  4804. uint32_t stride_batch_x = ne00*ne01;
  4805. uint32_t stride_batch_y = ne10*ne11;
  4806. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4807. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4808. }
  4809. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  4810. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4811. }
  4812. // compute
  4813. ggml_vk_matmul(
  4814. ctx, subctx, pipeline,
  4815. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4816. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  4817. ne01, ne11, ne10,
  4818. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  4819. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  4820. ); // NOLINT
  4821. }
  4822. 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) {
  4823. 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];
  4824. 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];
  4825. 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];
  4826. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  4827. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4828. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4829. const uint64_t ne00 = src0->ne[0];
  4830. const uint64_t ne01 = src0->ne[1];
  4831. const uint64_t ne02 = src0->ne[2];
  4832. const uint64_t ne03 = src0->ne[3];
  4833. const uint64_t ne10 = src1->ne[0];
  4834. const uint64_t ne11 = src1->ne[1];
  4835. const uint64_t ne12 = src1->ne[2];
  4836. const uint64_t ne13 = src1->ne[3];
  4837. const uint64_t ne20 = dst->ne[0];
  4838. const uint64_t ne21 = dst->ne[1];
  4839. const uint64_t ne22 = dst->ne[2];
  4840. const uint64_t ne23 = dst->ne[3];
  4841. const uint64_t r2 = ne12 / ne02;
  4842. const uint64_t r3 = ne13 / ne03;
  4843. // batch_n indicates that we need to compute a few vector results, and this assumes
  4844. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4845. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4846. bool batch_n = ne11 > 1;
  4847. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4848. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4849. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4850. vk_buffer d_Qx = nullptr;
  4851. size_t qx_buf_offset = 0;
  4852. vk_buffer d_Qy = nullptr;
  4853. size_t qy_buf_offset = 0;
  4854. bool src0_uma = false;
  4855. bool src1_uma = false;
  4856. if (ctx->device->uma) {
  4857. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4858. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4859. src0_uma = d_Qx != nullptr;
  4860. src1_uma = d_Qy != nullptr;
  4861. }
  4862. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4863. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4864. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4865. const bool qx_needs_dequant = x_non_contig;
  4866. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4867. // Not implemented
  4868. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4869. const uint64_t x_ne = ne01 * ne00;
  4870. const uint64_t y_ne = ne11 * ne10;
  4871. const uint64_t d_ne = ne11 * ne01;
  4872. 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);
  4873. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4874. 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;
  4875. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4876. const uint64_t d_sz = sizeof(float) * d_ne;
  4877. vk_pipeline to_fp16_vk_0 = nullptr;
  4878. vk_pipeline to_fp16_vk_1 = nullptr;
  4879. if (x_non_contig) {
  4880. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4881. }
  4882. if (y_non_contig) {
  4883. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4884. } else {
  4885. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4886. }
  4887. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  4888. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4889. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4890. GGML_ASSERT(dmmv != nullptr);
  4891. if (dryrun) {
  4892. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4893. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4894. if (
  4895. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4896. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4897. GGML_ABORT("Requested preallocation size is too large");
  4898. }
  4899. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4900. ctx->prealloc_size_x = x_sz_upd;
  4901. }
  4902. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4903. ctx->prealloc_size_y = y_sz_upd;
  4904. }
  4905. // Request descriptor sets
  4906. if (qx_needs_dequant) {
  4907. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4908. }
  4909. if (qy_needs_dequant) {
  4910. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4911. }
  4912. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  4913. return;
  4914. }
  4915. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4916. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4917. GGML_ASSERT(d_D != nullptr);
  4918. vk_buffer d_X;
  4919. uint64_t x_buf_offset = 0;
  4920. vk_buffer d_Y;
  4921. uint64_t y_buf_offset = 0;
  4922. if(!src0_uma) {
  4923. d_Qx = src0_buf_ctx->dev_buffer;
  4924. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4925. GGML_ASSERT(d_Qx != nullptr);
  4926. }
  4927. if(!src1_uma) {
  4928. d_Qy = src1_buf_ctx->dev_buffer;
  4929. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4930. GGML_ASSERT(d_Qy != nullptr);
  4931. }
  4932. if (qx_needs_dequant) {
  4933. d_X = ctx->prealloc_x;
  4934. } else {
  4935. d_X = d_Qx;
  4936. x_buf_offset = qx_buf_offset;
  4937. GGML_ASSERT(qx_sz == x_sz);
  4938. }
  4939. if (qy_needs_dequant) {
  4940. d_Y = ctx->prealloc_y;
  4941. } else {
  4942. d_Y = d_Qy;
  4943. y_buf_offset = qy_buf_offset;
  4944. GGML_ASSERT(qy_sz == y_sz);
  4945. }
  4946. if (x_non_contig) {
  4947. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4948. 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 });
  4949. }
  4950. if (y_non_contig) {
  4951. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4952. 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 });
  4953. }
  4954. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4955. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4956. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4957. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4958. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4959. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4960. }
  4961. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4962. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4963. }
  4964. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4965. uint32_t groups_x = ne01;
  4966. uint32_t groups_z = 1;
  4967. if (ne01 > max_groups_x) {
  4968. groups_z = 64;
  4969. groups_x = CEIL_DIV(groups_x, groups_z);
  4970. }
  4971. // compute
  4972. const vk_mat_vec_push_constants pc = {
  4973. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4974. stride_batch_x, stride_batch_y, stride_batch_d,
  4975. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4976. };
  4977. ggml_vk_sync_buffers(subctx);
  4978. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4979. { 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} },
  4980. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4981. }
  4982. 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) {
  4983. 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];
  4984. 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];
  4985. 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];
  4986. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4987. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4988. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4989. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4990. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4991. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4992. const uint64_t ne00 = src0->ne[0];
  4993. const uint64_t ne01 = src0->ne[1];
  4994. const uint64_t ne02 = src0->ne[2];
  4995. // const uint64_t ne03 = src0->ne[3];
  4996. const uint64_t ne10 = src1->ne[0];
  4997. const uint64_t ne11 = src1->ne[1];
  4998. const uint64_t ne12 = src1->ne[2];
  4999. // const uint64_t ne13 = src1->ne[3];
  5000. GGML_ASSERT(ne11 == 1);
  5001. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5002. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5003. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5004. vk_buffer d_Qy = nullptr;
  5005. size_t qy_buf_offset = 0;
  5006. bool src1_uma = false;
  5007. if (ctx->device->uma) {
  5008. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5009. src1_uma = d_Qy != nullptr;
  5010. }
  5011. const uint64_t x_ne = ne00 * ne01 * ne02;
  5012. const uint64_t y_ne = ne10 * ne11 * ne12;
  5013. const uint64_t d_ne = ne01 * ne11 * ne12;
  5014. 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);
  5015. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5016. const uint64_t d_sz = sizeof(float) * d_ne;
  5017. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  5018. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  5019. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  5020. gqa_ratio = 1;
  5021. }
  5022. if (dryrun) {
  5023. // Request descriptor sets
  5024. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  5025. return;
  5026. }
  5027. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5028. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5029. GGML_ASSERT(d_D != nullptr);
  5030. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5031. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5032. GGML_ASSERT(d_Qx != nullptr);
  5033. if (!src1_uma) {
  5034. d_Qy = src1_buf_ctx->dev_buffer;
  5035. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5036. GGML_ASSERT(d_Qx != nullptr);
  5037. }
  5038. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5039. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5040. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5041. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5042. // compute
  5043. 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)) };
  5044. uint32_t workgroups_z = (uint32_t)ne12;
  5045. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  5046. if (gqa_ratio > 1) {
  5047. workgroups_z /= gqa_ratio;
  5048. }
  5049. ggml_vk_sync_buffers(subctx);
  5050. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], { 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 } }, pc, { 1, (uint32_t)ne01, workgroups_z });
  5051. }
  5052. 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) {
  5053. 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];
  5054. 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];
  5055. 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];
  5056. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5057. GGML_ASSERT(!ggml_is_transposed(src0));
  5058. GGML_ASSERT(!ggml_is_transposed(src1));
  5059. GGML_ASSERT(!ggml_is_permuted(src0));
  5060. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5061. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5062. const uint64_t ne00 = src0->ne[0];
  5063. const uint64_t ne01 = src0->ne[1];
  5064. const uint64_t ne02 = src0->ne[2];
  5065. const uint64_t ne03 = src0->ne[3];
  5066. const uint64_t nb01 = src0->nb[1];
  5067. const uint64_t nb02 = src0->nb[2];
  5068. const uint64_t nb12 = src1->nb[2];
  5069. // const uint64_t ne10 = src1->ne[0];
  5070. const uint64_t ne11 = src1->ne[1];
  5071. const uint64_t ne12 = src1->ne[2];
  5072. // const uint64_t ne13 = src1->ne[3];
  5073. const uint32_t nb03 = (uint32_t)(src0->nb[3] / sizeof(ggml_fp16_t));
  5074. const uint32_t nb13 = (uint32_t)(src1->nb[3] / sizeof(float));
  5075. const uint32_t nb23 = (uint32_t)(dst->nb[3] / sizeof(float));
  5076. GGML_ASSERT(ne11 == 1);
  5077. GGML_ASSERT(src0->ne[3] == src1->ne[3]); // checked in supports_op
  5078. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5079. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5080. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5081. vk_buffer d_Qy = nullptr;
  5082. size_t qy_buf_offset = 0;
  5083. bool src1_uma = false;
  5084. if (ctx->device->uma) {
  5085. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5086. src1_uma = d_Qy != nullptr;
  5087. }
  5088. const uint64_t d_ne = ne01 * ne11 * ne12 * ne03;
  5089. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  5090. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  5091. const uint32_t channel_stride_y = nb12 / sizeof(float);
  5092. const uint64_t qx_sz = ggml_nbytes(src0);
  5093. const uint64_t qy_sz = ggml_nbytes(src1);
  5094. const uint64_t d_sz = sizeof(float) * d_ne;
  5095. if (dryrun) {
  5096. // Request descriptor sets
  5097. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  5098. return;
  5099. }
  5100. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5101. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5102. GGML_ASSERT(d_D != nullptr);
  5103. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5104. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5105. GGML_ASSERT(d_Qx != nullptr);
  5106. if (!src1_uma) {
  5107. d_Qy = src1_buf_ctx->dev_buffer;
  5108. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5109. GGML_ASSERT(d_Qx != nullptr);
  5110. }
  5111. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5112. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5113. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5114. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5115. // compute
  5116. const std::array<uint32_t, 12> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, channel_stride_y, (uint32_t)(ne12 / ne02), (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)), nb03, nb13, nb23 };
  5117. ggml_vk_sync_buffers(subctx);
  5118. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  5119. { 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 } }, pc, { (uint32_t)ne03, (uint32_t)ne01, (uint32_t)ne12 });
  5120. }
  5121. 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) {
  5122. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  5123. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  5124. // detect 0213 permutation, and batch size of 1
  5125. src0->nb[0] <= src0->nb[2] &&
  5126. src0->nb[2] <= src0->nb[1] &&
  5127. src0->nb[1] <= src0->nb[3] &&
  5128. src1->nb[0] <= src1->nb[2] &&
  5129. src1->nb[2] <= src1->nb[1] &&
  5130. src1->nb[1] <= src1->nb[3] &&
  5131. src0->ne[3] == 1 &&
  5132. src1->ne[3] == 1) {
  5133. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5134. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  5135. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  5136. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5137. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  5138. // when ne12 and ne13 are one.
  5139. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  5140. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  5141. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5142. } else {
  5143. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5144. }
  5145. }
  5146. 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) {
  5147. 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];
  5148. 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];
  5149. 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];
  5150. 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] << "),)");
  5151. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5152. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5153. const uint64_t ne00 = src0->ne[0];
  5154. const uint64_t ne01 = src0->ne[1];
  5155. const uint64_t ne02 = src0->ne[2];
  5156. const uint64_t ne03 = src0->ne[3];
  5157. const uint64_t ne10 = src1->ne[0];
  5158. const uint64_t ne11 = src1->ne[1];
  5159. const uint64_t ne12 = src1->ne[2];
  5160. const uint64_t ne13 = src1->ne[3];
  5161. const uint64_t nei0 = ids->ne[0];
  5162. const uint64_t nei1 = ids->ne[1];
  5163. GGML_ASSERT(nei0 * nei1 <= 4096);
  5164. const uint32_t nbi1 = ids->nb[1];
  5165. const uint32_t nbi2 = ids->nb[2];
  5166. const uint64_t ne20 = dst->ne[0];
  5167. const uint64_t ne21 = dst->ne[1];
  5168. const uint64_t ne22 = dst->ne[2];
  5169. const uint64_t ne23 = dst->ne[3];
  5170. const uint64_t n_as = ne02;
  5171. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5172. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5173. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5174. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5175. vk_buffer d_Qx = nullptr;
  5176. size_t qx_buf_offset = 0;
  5177. vk_buffer d_Qy = nullptr;
  5178. size_t qy_buf_offset = 0;
  5179. vk_buffer d_ids = nullptr;
  5180. size_t ids_buf_offset = 0;
  5181. bool src0_uma = false;
  5182. bool src1_uma = false;
  5183. bool ids_uma = false;
  5184. if (ctx->device->uma) {
  5185. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5186. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5187. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5188. src0_uma = d_Qx != nullptr;
  5189. src1_uma = d_Qy != nullptr;
  5190. ids_uma = d_ids != nullptr;
  5191. }
  5192. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  5193. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  5194. !ggml_vk_dim01_contiguous(src0);
  5195. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  5196. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  5197. !ggml_vk_dim01_contiguous(src1);
  5198. // If src0 is BF16, try to use a BF16 x BF16 multiply
  5199. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  5200. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  5201. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  5202. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  5203. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  5204. if (qx_needs_dequant) {
  5205. // Fall back to dequant + f16 mulmat
  5206. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, f16_type, y_f32_kernel ? GGML_TYPE_F32 : f16_type, (ggml_prec)dst->op_params[0]);
  5207. }
  5208. // Not implemented
  5209. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5210. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? f16_type : src0->type));
  5211. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  5212. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  5213. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  5214. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  5215. const uint64_t x_ne = ne01 * ne00;
  5216. const uint64_t y_ne = padded_n * ne10;
  5217. const uint64_t d_ne = ne21 * ne20;
  5218. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  5219. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5220. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  5221. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5222. const uint64_t ids_sz = nbi2;
  5223. const uint64_t d_sz = sizeof(float) * d_ne;
  5224. vk_pipeline to_fp16_vk_0 = nullptr;
  5225. vk_pipeline to_fp16_vk_1 = nullptr;
  5226. if (x_non_contig) {
  5227. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  5228. } else {
  5229. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  5230. }
  5231. if (y_non_contig) {
  5232. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  5233. } else {
  5234. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5235. }
  5236. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5237. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5238. if (dryrun) {
  5239. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5240. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5241. if (
  5242. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5243. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5244. GGML_ABORT("Requested preallocation size is too large");
  5245. }
  5246. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5247. ctx->prealloc_size_x = x_sz_upd;
  5248. }
  5249. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5250. ctx->prealloc_size_y = y_sz_upd;
  5251. }
  5252. // Request descriptor sets
  5253. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5254. if (qx_needs_dequant) {
  5255. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5256. }
  5257. if (qy_needs_dequant) {
  5258. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5259. }
  5260. return;
  5261. }
  5262. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5263. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5264. GGML_ASSERT(d_D != nullptr);
  5265. vk_buffer d_X;
  5266. uint64_t x_buf_offset = 0;
  5267. vk_buffer d_Y;
  5268. uint64_t y_buf_offset = 0;
  5269. if (!src0_uma) {
  5270. d_Qx = src0_buf_ctx->dev_buffer;
  5271. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5272. GGML_ASSERT(d_Qx != nullptr);
  5273. }
  5274. if (!src1_uma) {
  5275. d_Qy = src1_buf_ctx->dev_buffer;
  5276. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5277. GGML_ASSERT(d_Qy != nullptr);
  5278. }
  5279. if (!ids_uma) {
  5280. d_ids = ids_buf_ctx->dev_buffer;
  5281. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5282. GGML_ASSERT(d_ids != nullptr);
  5283. }
  5284. if (qx_needs_dequant) {
  5285. d_X = ctx->prealloc_x;
  5286. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  5287. } else {
  5288. d_X = d_Qx;
  5289. x_buf_offset = qx_buf_offset;
  5290. GGML_ASSERT(qx_sz == x_sz);
  5291. }
  5292. if (qy_needs_dequant) {
  5293. d_Y = ctx->prealloc_y;
  5294. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5295. } else {
  5296. d_Y = d_Qy;
  5297. y_buf_offset = qy_buf_offset;
  5298. GGML_ASSERT(qy_sz == y_sz);
  5299. }
  5300. if (x_non_contig) {
  5301. 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 });
  5302. } else if (qx_needs_dequant) {
  5303. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5304. ggml_vk_sync_buffers(subctx);
  5305. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  5306. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc, { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  5307. }
  5308. if (y_non_contig) {
  5309. 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 });
  5310. }
  5311. uint32_t stride_batch_x = ne00*ne01;
  5312. uint32_t stride_batch_y = ne10*ne11;
  5313. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5314. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5315. }
  5316. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5317. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5318. }
  5319. // compute
  5320. ggml_vk_matmul_id(
  5321. ctx, subctx, pipeline,
  5322. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  5323. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  5324. ne01, ne21, ne10, ne10, ne10, ne01,
  5325. stride_batch_x, stride_batch_y, ne20*ne21,
  5326. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  5327. ); // NOLINT
  5328. }
  5329. 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) {
  5330. 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];
  5331. 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];
  5332. 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];
  5333. 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];
  5334. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5335. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5336. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5337. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5338. const uint64_t ne00 = src0->ne[0];
  5339. const uint64_t ne01 = src0->ne[1];
  5340. const uint64_t ne02 = src0->ne[2];
  5341. const uint64_t ne03 = src0->ne[3];
  5342. const uint64_t ne10 = src1->ne[0];
  5343. const uint64_t ne11 = src1->ne[1];
  5344. const uint64_t ne12 = src1->ne[2];
  5345. const uint64_t ne13 = src1->ne[3];
  5346. const uint64_t nei0 = ids->ne[0];
  5347. const uint64_t nei1 = ids->ne[1];
  5348. const uint64_t nbi2 = ids->nb[2];
  5349. GGML_ASSERT(nei1 == 1);
  5350. const uint64_t ne20 = dst->ne[0];
  5351. const uint64_t ne21 = dst->ne[1];
  5352. const uint64_t ne22 = dst->ne[2];
  5353. const uint64_t ne23 = dst->ne[3];
  5354. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5355. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5356. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5357. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5358. vk_buffer d_Qx = nullptr;
  5359. size_t qx_buf_offset = 0;
  5360. vk_buffer d_Qy = nullptr;
  5361. size_t qy_buf_offset = 0;
  5362. vk_buffer d_ids = nullptr;
  5363. size_t ids_buf_offset = 0;
  5364. bool src0_uma = false;
  5365. bool src1_uma = false;
  5366. bool ids_uma = false;
  5367. if (ctx->device->uma) {
  5368. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5369. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5370. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5371. src0_uma = d_Qx != nullptr;
  5372. src1_uma = d_Qy != nullptr;
  5373. ids_uma = d_ids != nullptr;
  5374. }
  5375. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5376. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5377. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5378. const bool qx_needs_dequant = x_non_contig;
  5379. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5380. // Not implemented
  5381. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5382. const uint64_t x_ne = ne01 * ne00;
  5383. const uint64_t y_ne = ne11 * ne10;
  5384. const uint64_t d_ne = ne21 * ne20;
  5385. 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);
  5386. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5387. 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;
  5388. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5389. const uint64_t ids_sz = nbi2;
  5390. const uint64_t d_sz = sizeof(float) * d_ne;
  5391. vk_pipeline to_fp16_vk_0 = nullptr;
  5392. vk_pipeline to_fp16_vk_1 = nullptr;
  5393. if (x_non_contig) {
  5394. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5395. }
  5396. if (y_non_contig) {
  5397. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5398. } else {
  5399. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5400. }
  5401. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5402. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5403. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5404. GGML_ASSERT(dmmv != nullptr);
  5405. if (dryrun) {
  5406. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5407. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5408. if (
  5409. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5410. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5411. GGML_ABORT("Requested preallocation size is too large");
  5412. }
  5413. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5414. ctx->prealloc_size_x = x_sz_upd;
  5415. }
  5416. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5417. ctx->prealloc_size_y = y_sz_upd;
  5418. }
  5419. // Request descriptor sets
  5420. if (qx_needs_dequant) {
  5421. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5422. }
  5423. if (qy_needs_dequant) {
  5424. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5425. }
  5426. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5427. return;
  5428. }
  5429. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5430. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5431. GGML_ASSERT(d_D != nullptr);
  5432. vk_buffer d_X;
  5433. uint64_t x_buf_offset = 0;
  5434. vk_buffer d_Y;
  5435. uint64_t y_buf_offset = 0;
  5436. if(!src0_uma) {
  5437. d_Qx = src0_buf_ctx->dev_buffer;
  5438. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5439. GGML_ASSERT(d_Qx != nullptr);
  5440. }
  5441. if(!src1_uma) {
  5442. d_Qy = src1_buf_ctx->dev_buffer;
  5443. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5444. GGML_ASSERT(d_Qy != nullptr);
  5445. }
  5446. if(!ids_uma) {
  5447. d_ids = ids_buf_ctx->dev_buffer;
  5448. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5449. GGML_ASSERT(d_ids != nullptr);
  5450. }
  5451. if (qx_needs_dequant) {
  5452. d_X = ctx->prealloc_x;
  5453. } else {
  5454. d_X = d_Qx;
  5455. x_buf_offset = qx_buf_offset;
  5456. GGML_ASSERT(qx_sz == x_sz);
  5457. }
  5458. if (qy_needs_dequant) {
  5459. d_Y = ctx->prealloc_y;
  5460. } else {
  5461. d_Y = d_Qy;
  5462. y_buf_offset = qy_buf_offset;
  5463. GGML_ASSERT(qy_sz == y_sz);
  5464. }
  5465. if (x_non_contig) {
  5466. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5467. 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 });
  5468. }
  5469. if (y_non_contig) {
  5470. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5471. 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 });
  5472. }
  5473. uint32_t stride_batch_y = ne10*ne11;
  5474. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5475. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5476. }
  5477. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5478. uint32_t groups_x = ne01;
  5479. uint32_t groups_z = 1;
  5480. if (ne01 > max_groups_x) {
  5481. groups_z = 64;
  5482. groups_x = CEIL_DIV(groups_x, groups_z);
  5483. }
  5484. // compute
  5485. const vk_mat_vec_id_push_constants pc = {
  5486. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5487. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5488. (uint32_t)nei0, (uint32_t)ne11,
  5489. };
  5490. ggml_vk_sync_buffers(subctx);
  5491. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5492. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5493. 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 } },
  5494. pc, { groups_x, (uint32_t)nei0, groups_z });
  5495. }
  5496. 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) {
  5497. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5498. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5499. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5500. } else {
  5501. // Split based on number of ids, to fit in shared memory
  5502. const uint32_t nei0 = (uint32_t)src2->ne[0];
  5503. const uint32_t nei1 = (uint32_t)src2->ne[1];
  5504. GGML_ASSERT(nei0 <= 4096);
  5505. const uint32_t split_size = std::min(nei1, 4096u / nei0);
  5506. ggml_tensor src1_copy = *src1;
  5507. ggml_tensor src2_copy = *src2;
  5508. ggml_tensor dst_copy = *dst;
  5509. for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
  5510. const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
  5511. src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
  5512. src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
  5513. dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
  5514. src1_copy.ne[2] = n_tokens;
  5515. src2_copy.ne[1] = n_tokens;
  5516. dst_copy.ne[2] = n_tokens;
  5517. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
  5518. }
  5519. }
  5520. }
  5521. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5522. // Needs to be kept up to date on shader changes
  5523. GGML_UNUSED(hsv);
  5524. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5525. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  5526. const uint32_t Bc = scalar_flash_attention_Bc;
  5527. const uint32_t tmpsh = wg_size * sizeof(float);
  5528. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5529. const uint32_t masksh = Bc * Br * sizeof(float);
  5530. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5531. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5532. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5533. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5534. return supported;
  5535. }
  5536. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5537. // Needs to be kept up to date on shader changes
  5538. GGML_UNUSED(hsv);
  5539. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5540. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5541. const uint32_t Bc = scalar_flash_attention_Bc;
  5542. const uint32_t acctype = f32acc ? 4 : 2;
  5543. const uint32_t f16vec4 = 8;
  5544. const uint32_t tmpsh = wg_size * sizeof(float);
  5545. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5546. const uint32_t Qf = Br * (hsk / 4 + 2) * f16vec4;
  5547. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5548. const uint32_t sfsh = Bc * sfshstride * acctype;
  5549. const uint32_t kshstride = hsk / 4 + 2;
  5550. const uint32_t ksh = Bc * kshstride * f16vec4;
  5551. const uint32_t slope = Br * sizeof(float);
  5552. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5553. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5554. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5555. return supported;
  5556. }
  5557. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, const ggml_tensor * sinks, ggml_tensor * dst, bool dryrun = false) {
  5558. VK_LOG_DEBUG("ggml_vk_flash_attn((" << q << ", name=" << q->name << ", type=" << q->type << ", ne0=" << q->ne[0] << ", ne1=" << q->ne[1] << ", ne2=" << q->ne[2] << ", ne3=" << q->ne[3] << ", nb0=" << q->nb[0] << ", nb1=" << q->nb[1] << ", nb2=" << q->nb[2] << ", nb3=" << q->nb[3];
  5559. std::cerr << "), (" << k << ", name=" << k->name << ", type=" << k->type << ", ne0=" << k->ne[0] << ", ne1=" << k->ne[1] << ", ne2=" << k->ne[2] << ", ne3=" << k->ne[3] << ", nb0=" << k->nb[0] << ", nb1=" << k->nb[1] << ", nb2=" << k->nb[2] << ", nb3=" << k->nb[3];
  5560. std::cerr << "), (" << v << ", name=" << v->name << ", type=" << v->type << ", ne0=" << v->ne[0] << ", ne1=" << v->ne[1] << ", ne2=" << v->ne[2] << ", ne3=" << v->ne[3] << ", nb0=" << v->nb[0] << ", nb1=" << v->nb[1] << ", nb2=" << v->nb[2] << ", nb3=" << v->nb[3];
  5561. 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];
  5562. if (sinks) {
  5563. std::cerr << "), (" << sinks << ", name=" << sinks->name << ", type=" << sinks->type << ", ne0=" << sinks->ne[0] << ", ne1=" << sinks->ne[1] << ", ne2=" << sinks->ne[2] << ", ne3=" << sinks->ne[3] << ", nb0=" << sinks->nb[0] << ", nb1=" << sinks->nb[1] << ", nb2=" << sinks->nb[2] << ", nb3=" << sinks->nb[3];
  5564. }
  5565. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5566. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5567. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5568. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5569. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5570. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5571. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5572. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5573. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5574. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5575. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5576. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  5577. const uint32_t HSK = nek0;
  5578. const uint32_t HSV = nev0;
  5579. uint32_t N = neq1;
  5580. const uint32_t KV = nek1;
  5581. GGML_ASSERT(ne0 == HSV);
  5582. GGML_ASSERT(ne2 == N);
  5583. // input tensor rows must be contiguous
  5584. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  5585. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  5586. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  5587. GGML_ASSERT(neq0 == HSK);
  5588. GGML_ASSERT(neq1 == N);
  5589. GGML_ASSERT(nev1 == nek1);
  5590. // dst cannot be transposed or permuted
  5591. GGML_ASSERT(nb0 == sizeof(float));
  5592. GGML_ASSERT(nb0 <= nb1);
  5593. GGML_ASSERT(nb1 <= nb2);
  5594. GGML_ASSERT(nb2 <= nb3);
  5595. assert(dst->type == GGML_TYPE_F32);
  5596. assert(q->type == GGML_TYPE_F32);
  5597. assert(k->type == v->type);
  5598. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  5599. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  5600. if (path == FA_COOPMAT1) {
  5601. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  5602. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  5603. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  5604. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  5605. path = FA_SCALAR;
  5606. }
  5607. }
  5608. uint32_t gqa_ratio = 1;
  5609. uint32_t qk_ratio = neq2 / nek2;
  5610. uint32_t workgroups_x = (uint32_t)neq1;
  5611. uint32_t workgroups_y = (uint32_t)neq2;
  5612. uint32_t workgroups_z = (uint32_t)neq3;
  5613. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  5614. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  5615. uint32_t max_gqa;
  5616. switch (path) {
  5617. case FA_SCALAR:
  5618. case FA_COOPMAT1:
  5619. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  5620. max_gqa = get_fa_scalar_num_large_rows(HSV);
  5621. break;
  5622. case FA_COOPMAT2:
  5623. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  5624. break;
  5625. default:
  5626. GGML_ASSERT(0);
  5627. }
  5628. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  5629. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  5630. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  5631. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  5632. // and change addressing calculations to index Q's dimension 2.
  5633. gqa_ratio = qk_ratio;
  5634. N = gqa_ratio;
  5635. workgroups_y /= N;
  5636. }
  5637. vk_pipeline *pipelines;
  5638. bool small_rows = N <= get_fa_num_small_rows(path);
  5639. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  5640. // So use scalar instead.
  5641. if (small_rows && path == FA_COOPMAT1) {
  5642. path = FA_SCALAR;
  5643. }
  5644. // scalar is faster than coopmat2 when N==1
  5645. if (N == 1 && path == FA_COOPMAT2) {
  5646. path = FA_SCALAR;
  5647. }
  5648. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  5649. if (path == FA_SCALAR &&
  5650. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  5651. small_rows = true;
  5652. }
  5653. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  5654. FaHeadSizes head_sizes = fa_get_head_sizes(k->ne[0], v->ne[0]);
  5655. switch (path) {
  5656. case FA_SCALAR:
  5657. pipelines = &ctx->device->pipeline_flash_attn_f32_f16[k->type][head_sizes][f32acc][small_rows][0];
  5658. break;
  5659. case FA_COOPMAT1:
  5660. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm1[k->type][head_sizes][f32acc][small_rows][0];
  5661. break;
  5662. case FA_COOPMAT2:
  5663. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm2[k->type][head_sizes][f32acc][small_rows][0];
  5664. break;
  5665. default:
  5666. GGML_ASSERT(0);
  5667. }
  5668. assert(pipelines);
  5669. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  5670. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  5671. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  5672. bool aligned = (KV % pipelines[1]->align) == 0 &&
  5673. // the "aligned" shader variant will forcibly align strides, for performance
  5674. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  5675. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  5676. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  5677. vk_pipeline pipeline = pipelines[aligned];
  5678. assert(pipeline);
  5679. uint32_t split_kv = KV;
  5680. uint32_t split_k = 1;
  5681. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  5682. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  5683. // Try to use split_k when KV is large enough to be worth the overhead
  5684. if (workgroups_x == 1 && shader_core_count > 0) {
  5685. // Try to run two workgroups per SM.
  5686. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  5687. if (split_k > 1) {
  5688. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  5689. // of "align", so recompute split_k based on that.
  5690. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), pipelines[1]->align);
  5691. split_k = CEIL_DIV(KV, split_kv);
  5692. workgroups_x = split_k;
  5693. }
  5694. }
  5695. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  5696. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  5697. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  5698. if (split_k_size > ctx->device->max_memory_allocation_size) {
  5699. GGML_ABORT("Requested preallocation size is too large");
  5700. }
  5701. if (ctx->prealloc_size_split_k < split_k_size) {
  5702. ctx->prealloc_size_split_k = split_k_size;
  5703. }
  5704. if (dryrun) {
  5705. // Request descriptor sets
  5706. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5707. if (split_k > 1) {
  5708. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  5709. }
  5710. return;
  5711. }
  5712. float scale = 1.0f;
  5713. float max_bias = 0.0f;
  5714. float logit_softcap = 0.0f;
  5715. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  5716. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  5717. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  5718. if (logit_softcap != 0) {
  5719. scale /= logit_softcap;
  5720. }
  5721. const uint32_t n_head_kv = neq2;
  5722. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5723. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5724. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5725. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr, d_S = nullptr;
  5726. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0, s_buf_offset = 0;
  5727. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false, S_uma = false;
  5728. if (ctx->device->uma) {
  5729. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  5730. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  5731. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  5732. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  5733. Q_uma = d_Q != nullptr;
  5734. K_uma = d_K != nullptr;
  5735. V_uma = d_V != nullptr;
  5736. D_uma = d_D != nullptr;
  5737. if (mask) {
  5738. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  5739. M_uma = d_M != nullptr;
  5740. }
  5741. if (sinks) {
  5742. ggml_vk_host_get(ctx->device, sinks->data, d_S, s_buf_offset);
  5743. S_uma = d_S != nullptr;
  5744. }
  5745. }
  5746. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5747. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  5748. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  5749. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  5750. if (!Q_uma) {
  5751. d_Q = q_buf_ctx->dev_buffer;
  5752. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  5753. }
  5754. if (!K_uma) {
  5755. d_K = k_buf_ctx->dev_buffer;
  5756. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  5757. }
  5758. if (!V_uma) {
  5759. d_V = v_buf_ctx->dev_buffer;
  5760. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  5761. }
  5762. if (!D_uma) {
  5763. d_D = d_buf_ctx->dev_buffer;
  5764. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5765. }
  5766. if (!M_uma) {
  5767. d_M = d_Q;
  5768. m_buf_offset = q_buf_offset;
  5769. if (mask) {
  5770. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  5771. d_M = m_buf_ctx->dev_buffer;
  5772. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  5773. }
  5774. }
  5775. if (!S_uma) {
  5776. d_S = d_Q;
  5777. s_buf_offset = q_buf_offset;
  5778. if (sinks) {
  5779. ggml_backend_vk_buffer_context * s_buf_ctx = (ggml_backend_vk_buffer_context*)sinks->buffer->context;
  5780. d_S = s_buf_ctx->dev_buffer;
  5781. s_buf_offset = vk_tensor_offset(sinks) + sinks->view_offs;
  5782. }
  5783. }
  5784. uint32_t mask_n_head_log2 = ((sinks != nullptr) << 24) | ((mask != nullptr) << 16) | n_head_log2;
  5785. const vk_flash_attn_push_constants pc = { N, KV,
  5786. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  5787. (uint32_t)neq2, (uint32_t)neq3,
  5788. (uint32_t)nek2, (uint32_t)nek3,
  5789. (uint32_t)nev2, (uint32_t)nev3,
  5790. nem1, nem2, nem3,
  5791. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  5792. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  5793. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  5794. scale, max_bias, logit_softcap,
  5795. mask_n_head_log2, m0, m1,
  5796. gqa_ratio, split_kv, split_k };
  5797. ggml_vk_sync_buffers(subctx);
  5798. if (split_k > 1) {
  5799. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5800. {
  5801. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5802. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5803. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5804. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5805. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  5806. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5807. },
  5808. // We only use split_k when group query attention is enabled, which means
  5809. // there's no more than one tile of rows (i.e. workgroups_x would have been
  5810. // one). We reuse workgroups_x to mean the number of splits, so we need to
  5811. // cancel out the divide by wg_denoms[0].
  5812. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  5813. ggml_vk_sync_buffers(subctx);
  5814. const std::array<uint32_t, 5> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k, (sinks != nullptr) };
  5815. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  5816. {
  5817. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5818. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  5819. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5820. },
  5821. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  5822. } else {
  5823. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5824. {
  5825. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5826. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5827. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5828. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5829. vk_subbuffer{d_S, s_buf_offset, VK_WHOLE_SIZE},
  5830. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5831. },
  5832. pc, { workgroups_x, workgroups_y, workgroups_z });
  5833. }
  5834. }
  5835. static std::array<uint32_t, 3> ggml_vk_get_conv_elements(const ggml_tensor *dst) {
  5836. const ggml_tensor *src0 = dst->src[0];
  5837. const ggml_tensor *src1 = dst->src[1];
  5838. // src0 - kernel: [KW, KH, Cin, Cout]
  5839. // src1 - input: [W, H, Cin, N]
  5840. // dst - result: [OW, OH, Cout, N]
  5841. // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d)
  5842. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  5843. return (ins + 2 * p - d * (ks - 1) - 1) / s + 1;
  5844. };
  5845. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  5846. int64_t W = src1->ne[0];
  5847. int64_t H = src1->ne[1];
  5848. int64_t KW = src0->ne[0];
  5849. int64_t KH = src0->ne[1];
  5850. int64_t Cout = src0->ne[3];
  5851. int64_t N = src1->ne[3];
  5852. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[1], dst->op_params[3], dst->op_params[5]);
  5853. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], dst->op_params[2], dst->op_params[4]);
  5854. int64_t NPQ = N * OW * OH;
  5855. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  5856. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  5857. return elements;
  5858. }
  5859. 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) {
  5860. switch (op) {
  5861. case GGML_OP_GET_ROWS:
  5862. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5863. if (dst->type == GGML_TYPE_F16) {
  5864. return ctx->device->pipeline_get_rows[src0->type];
  5865. }
  5866. if (dst->type == GGML_TYPE_F32) {
  5867. return ctx->device->pipeline_get_rows_f32[src0->type];
  5868. }
  5869. return nullptr;
  5870. case GGML_OP_ACC:
  5871. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5872. return ctx->device->pipeline_acc_f32;
  5873. }
  5874. return nullptr;
  5875. case GGML_OP_ADD:
  5876. case GGML_OP_SUB:
  5877. case GGML_OP_MUL:
  5878. case GGML_OP_DIV:
  5879. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5880. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  5881. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  5882. return nullptr;
  5883. }
  5884. switch (op) {
  5885. case GGML_OP_ADD:
  5886. {
  5887. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  5888. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5889. }
  5890. case GGML_OP_SUB:
  5891. {
  5892. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  5893. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5894. }
  5895. case GGML_OP_MUL:
  5896. {
  5897. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  5898. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5899. }
  5900. case GGML_OP_DIV:
  5901. {
  5902. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  5903. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5904. }
  5905. default:
  5906. break;
  5907. }
  5908. return nullptr;
  5909. case GGML_OP_ADD_ID:
  5910. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && src2->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32) {
  5911. return ctx->device->pipeline_add_id_f32;
  5912. }
  5913. return nullptr;
  5914. case GGML_OP_CONCAT:
  5915. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5916. return ctx->device->pipeline_concat_f32;
  5917. }
  5918. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5919. return ctx->device->pipeline_concat_f16;
  5920. }
  5921. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  5922. return ctx->device->pipeline_concat_i32;
  5923. }
  5924. return nullptr;
  5925. case GGML_OP_UPSCALE:
  5926. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5927. int mode = ggml_get_op_params_i32(dst, 0);
  5928. switch (mode) {
  5929. case GGML_SCALE_MODE_NEAREST:
  5930. return ctx->device->pipeline_upscale_nearest_f32;
  5931. case GGML_SCALE_MODE_BILINEAR:
  5932. return ctx->device->pipeline_upscale_bilinear_f32;
  5933. case GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS:
  5934. return ctx->device->pipeline_upscale_bilinear_ac_f32;
  5935. }
  5936. }
  5937. return nullptr;
  5938. case GGML_OP_SCALE:
  5939. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5940. return ctx->device->pipeline_scale_f32;
  5941. }
  5942. return nullptr;
  5943. case GGML_OP_SQR:
  5944. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5945. return ctx->device->pipeline_sqr_f32;
  5946. }
  5947. return nullptr;
  5948. case GGML_OP_SIN:
  5949. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5950. return ctx->device->pipeline_sin_f32;
  5951. }
  5952. return nullptr;
  5953. case GGML_OP_COS:
  5954. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5955. return ctx->device->pipeline_cos_f32;
  5956. }
  5957. return nullptr;
  5958. case GGML_OP_CLAMP:
  5959. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5960. return ctx->device->pipeline_clamp_f32;
  5961. }
  5962. return nullptr;
  5963. case GGML_OP_PAD:
  5964. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5965. return ctx->device->pipeline_pad_f32;
  5966. }
  5967. return nullptr;
  5968. case GGML_OP_ROLL:
  5969. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5970. return ctx->device->pipeline_roll_f32;
  5971. }
  5972. return nullptr;
  5973. case GGML_OP_REPEAT:
  5974. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  5975. return ctx->device->pipeline_repeat_f32;
  5976. }
  5977. return nullptr;
  5978. case GGML_OP_REPEAT_BACK:
  5979. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5980. return ctx->device->pipeline_repeat_back_f32;
  5981. }
  5982. return nullptr;
  5983. case GGML_OP_CPY:
  5984. case GGML_OP_CONT:
  5985. case GGML_OP_DUP:
  5986. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  5987. case GGML_OP_SET_ROWS:
  5988. return ctx->device->pipeline_set_rows[dst->type];
  5989. case GGML_OP_SILU_BACK:
  5990. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5991. return ctx->device->pipeline_silu_back_f32;
  5992. }
  5993. return nullptr;
  5994. case GGML_OP_NORM:
  5995. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5996. return ctx->device->pipeline_norm_f32;
  5997. }
  5998. return nullptr;
  5999. case GGML_OP_GROUP_NORM:
  6000. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6001. return ctx->device->pipeline_group_norm_f32;
  6002. }
  6003. return nullptr;
  6004. case GGML_OP_RMS_NORM:
  6005. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6006. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  6007. }
  6008. return nullptr;
  6009. case GGML_OP_RMS_NORM_BACK:
  6010. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6011. return ctx->device->pipeline_rms_norm_back_f32;
  6012. }
  6013. return nullptr;
  6014. case GGML_OP_L2_NORM:
  6015. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6016. return ctx->device->pipeline_l2_norm_f32;
  6017. }
  6018. return nullptr;
  6019. case GGML_OP_UNARY:
  6020. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6021. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6022. (src0->type != dst->type)) {
  6023. return nullptr;
  6024. }
  6025. switch (ggml_get_unary_op(dst)) {
  6026. case GGML_UNARY_OP_SILU:
  6027. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  6028. case GGML_UNARY_OP_GELU:
  6029. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  6030. case GGML_UNARY_OP_GELU_ERF:
  6031. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  6032. case GGML_UNARY_OP_GELU_QUICK:
  6033. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  6034. case GGML_UNARY_OP_RELU:
  6035. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  6036. case GGML_UNARY_OP_TANH:
  6037. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  6038. case GGML_UNARY_OP_SIGMOID:
  6039. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  6040. default:
  6041. break;
  6042. }
  6043. return nullptr;
  6044. case GGML_OP_GLU:
  6045. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6046. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6047. (src0->type != dst->type)) {
  6048. return nullptr;
  6049. }
  6050. switch (ggml_get_glu_op(dst)) {
  6051. case GGML_GLU_OP_GEGLU:
  6052. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  6053. case GGML_GLU_OP_REGLU:
  6054. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  6055. case GGML_GLU_OP_SWIGLU:
  6056. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  6057. case GGML_GLU_OP_SWIGLU_OAI:
  6058. return ctx->device->pipeline_swiglu_oai[dst->type == GGML_TYPE_F16];
  6059. case GGML_GLU_OP_GEGLU_ERF:
  6060. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  6061. case GGML_GLU_OP_GEGLU_QUICK:
  6062. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  6063. default:
  6064. break;
  6065. }
  6066. return nullptr;
  6067. case GGML_OP_DIAG_MASK_INF:
  6068. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6069. return ctx->device->pipeline_diag_mask_inf_f32;
  6070. }
  6071. return nullptr;
  6072. case GGML_OP_SOFT_MAX:
  6073. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  6074. GGML_ASSERT(!src2 || src2->type == GGML_TYPE_F32);
  6075. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  6076. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  6077. }
  6078. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6079. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  6080. }
  6081. return nullptr;
  6082. case GGML_OP_SOFT_MAX_BACK:
  6083. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6084. return ctx->device->pipeline_soft_max_back_f32;
  6085. }
  6086. return nullptr;
  6087. case GGML_OP_ROPE:
  6088. case GGML_OP_ROPE_BACK:
  6089. {
  6090. const int mode = ((const int32_t *) dst->op_params)[2];
  6091. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  6092. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  6093. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  6094. if (is_neox) {
  6095. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6096. return ctx->device->pipeline_rope_neox_f32;
  6097. }
  6098. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6099. return ctx->device->pipeline_rope_neox_f16;
  6100. }
  6101. } else if (is_mrope && !is_vision) {
  6102. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6103. return ctx->device->pipeline_rope_multi_f32;
  6104. }
  6105. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6106. return ctx->device->pipeline_rope_multi_f16;
  6107. }
  6108. } else if (is_vision) {
  6109. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6110. return ctx->device->pipeline_rope_vision_f32;
  6111. }
  6112. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6113. return ctx->device->pipeline_rope_vision_f16;
  6114. }
  6115. } else {
  6116. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6117. return ctx->device->pipeline_rope_norm_f32;
  6118. }
  6119. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6120. return ctx->device->pipeline_rope_norm_f16;
  6121. }
  6122. }
  6123. return nullptr;
  6124. }
  6125. case GGML_OP_ARGSORT:
  6126. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6127. return ctx->device->pipeline_argsort_f32;
  6128. }
  6129. return nullptr;
  6130. case GGML_OP_SUM:
  6131. case GGML_OP_SUM_ROWS:
  6132. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6133. return ctx->device->pipeline_sum_rows_f32;
  6134. }
  6135. return nullptr;
  6136. case GGML_OP_ARGMAX:
  6137. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6138. return ctx->device->pipeline_argmax_f32;
  6139. }
  6140. return nullptr;
  6141. case GGML_OP_COUNT_EQUAL:
  6142. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  6143. return ctx->device->pipeline_count_equal_i32;
  6144. }
  6145. return nullptr;
  6146. case GGML_OP_IM2COL:
  6147. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6148. return ctx->device->pipeline_im2col_f32;
  6149. }
  6150. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  6151. return ctx->device->pipeline_im2col_f32_f16;
  6152. }
  6153. return nullptr;
  6154. case GGML_OP_TIMESTEP_EMBEDDING:
  6155. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6156. return ctx->device->pipeline_timestep_embedding_f32;
  6157. }
  6158. return nullptr;
  6159. case GGML_OP_CONV_TRANSPOSE_1D:
  6160. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6161. return ctx->device->pipeline_conv_transpose_1d_f32;
  6162. }
  6163. return nullptr;
  6164. case GGML_OP_POOL_2D:
  6165. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6166. return ctx->device->pipeline_pool2d_f32;
  6167. }
  6168. return nullptr;
  6169. case GGML_OP_RWKV_WKV6:
  6170. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6171. return ctx->device->pipeline_rwkv_wkv6_f32;
  6172. }
  6173. return nullptr;
  6174. case GGML_OP_RWKV_WKV7:
  6175. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6176. return ctx->device->pipeline_rwkv_wkv7_f32;
  6177. }
  6178. return nullptr;
  6179. case GGML_OP_OPT_STEP_ADAMW:
  6180. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6181. return ctx->device->pipeline_opt_step_adamw_f32;
  6182. }
  6183. return nullptr;
  6184. case GGML_OP_LEAKY_RELU:
  6185. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6186. return ctx->device->pipeline_leaky_relu_f32;
  6187. }
  6188. return nullptr;
  6189. case GGML_OP_CONV_2D:
  6190. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
  6191. ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  6192. auto elements = ggml_vk_get_conv_elements(dst);
  6193. vk_conv_shapes shape;
  6194. uint32_t tiles[CONV_SHAPE_COUNT];
  6195. for (uint32_t i = 0; i < CONV_SHAPE_COUNT; ++i) {
  6196. tiles[i] = CEIL_DIV(elements[0], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[0]) * CEIL_DIV(elements[1], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[1]);
  6197. }
  6198. // We can't query number of shader cores on Intel, use 32 as a placeholder
  6199. // so small convolutions will still choose a smaller tile.
  6200. const uint32_t shader_core_count = ctx->device->shader_core_count > 0 ? ctx->device->shader_core_count : 32;
  6201. if (elements[0] > 64 && tiles[CONV_SHAPE_128x128] >= shader_core_count * 2) {
  6202. shape = CONV_SHAPE_128x128;
  6203. } else if (elements[0] <= 32 && tiles[CONV_SHAPE_32x256] >= shader_core_count * 2) {
  6204. shape = CONV_SHAPE_32x256;
  6205. } else {
  6206. shape = CONV_SHAPE_64x32;
  6207. }
  6208. if (src0->type == GGML_TYPE_F32) {
  6209. return ctx->device->pipeline_conv2d_f32[shape];
  6210. } else if (src0->type == GGML_TYPE_F16) {
  6211. return ctx->device->pipeline_conv2d_f16_f32[shape];
  6212. }
  6213. }
  6214. return nullptr;
  6215. case GGML_OP_CONV_2D_DW:
  6216. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6217. if (ggml_is_contiguous(src1)) {
  6218. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  6219. } else if (ggml_is_contiguous_channels(src1)) {
  6220. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  6221. }
  6222. }
  6223. return nullptr;
  6224. default:
  6225. return nullptr;
  6226. }
  6227. GGML_UNUSED(src2);
  6228. }
  6229. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  6230. switch (op) {
  6231. case GGML_OP_CPY:
  6232. case GGML_OP_GET_ROWS:
  6233. case GGML_OP_ADD:
  6234. case GGML_OP_SUB:
  6235. case GGML_OP_MUL:
  6236. case GGML_OP_DIV:
  6237. case GGML_OP_ADD_ID:
  6238. case GGML_OP_CONCAT:
  6239. case GGML_OP_UPSCALE:
  6240. case GGML_OP_SQR:
  6241. case GGML_OP_SIN:
  6242. case GGML_OP_COS:
  6243. case GGML_OP_CLAMP:
  6244. case GGML_OP_PAD:
  6245. case GGML_OP_REPEAT:
  6246. case GGML_OP_REPEAT_BACK:
  6247. case GGML_OP_ROPE:
  6248. case GGML_OP_RMS_NORM:
  6249. case GGML_OP_CONV_2D_DW:
  6250. case GGML_OP_IM2COL:
  6251. case GGML_OP_SET_ROWS:
  6252. return true;
  6253. default:
  6254. return false;
  6255. }
  6256. }
  6257. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  6258. {
  6259. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  6260. }
  6261. template <typename T> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, T &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6262. GGML_UNUSED(p);
  6263. GGML_UNUSED(src0);
  6264. GGML_UNUSED(src1);
  6265. GGML_UNUSED(src2);
  6266. GGML_UNUSED(dst);
  6267. static_assert(!std::is_const<T>::value, "unexpected type");
  6268. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  6269. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  6270. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  6271. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  6272. }
  6273. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_unary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6274. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6275. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6276. p.misalign_offsets = (a_offset << 16) | d_offset;
  6277. GGML_UNUSED(src1);
  6278. GGML_UNUSED(src2);
  6279. }
  6280. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_binary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6281. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6282. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  6283. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6284. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  6285. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  6286. GGML_UNUSED(src2);
  6287. }
  6288. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_upscale_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  6289. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  6290. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  6291. p.a_offset = a_offset;
  6292. p.d_offset = d_offset;
  6293. GGML_UNUSED(src1);
  6294. GGML_UNUSED(src2);
  6295. }
  6296. template<typename PC>
  6297. 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, PC&& pc, bool dryrun = false) {
  6298. 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];
  6299. if (src1 != nullptr) {
  6300. 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];
  6301. }
  6302. if (src2 != nullptr) {
  6303. 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];
  6304. }
  6305. 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];
  6306. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  6307. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  6308. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  6309. GGML_ASSERT(dst->buffer != nullptr);
  6310. const uint64_t ne00 = src0->ne[0];
  6311. const uint64_t ne01 = src0->ne[1];
  6312. const uint64_t ne02 = src0->ne[2];
  6313. const uint64_t ne03 = src0->ne[3];
  6314. const uint64_t ne0 = ne00 * ne01;
  6315. const bool use_src1 = src1 != nullptr;
  6316. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  6317. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  6318. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  6319. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  6320. const uint64_t ne1 = ne10 * ne11;
  6321. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  6322. const bool use_src2 = src2 != nullptr;
  6323. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  6324. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  6325. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  6326. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  6327. const uint64_t ne2 = ne20 * ne21;
  6328. const uint64_t ned0 = dst->ne[0];
  6329. const uint64_t ned1 = dst->ne[1];
  6330. const uint64_t ned2 = dst->ne[2];
  6331. const uint64_t ned3 = dst->ne[3];
  6332. const uint64_t ned = ned0 * ned1;
  6333. init_pushconst_fastdiv(pc);
  6334. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  6335. if (pipeline == nullptr) {
  6336. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  6337. if (src1 != nullptr) {
  6338. std::cerr << " and " << ggml_type_name(src1->type);
  6339. }
  6340. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  6341. GGML_ABORT("fatal error");
  6342. }
  6343. if (dryrun) {
  6344. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6345. return;
  6346. }
  6347. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  6348. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6349. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  6350. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  6351. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  6352. vk_buffer d_X = nullptr;
  6353. size_t x_buf_offset = 0;
  6354. vk_buffer d_Y = nullptr;
  6355. size_t y_buf_offset = 0;
  6356. vk_buffer d_Z = nullptr;
  6357. size_t z_buf_offset = 0;
  6358. bool src0_uma = false;
  6359. bool src1_uma = false;
  6360. bool src2_uma = false;
  6361. if (ctx->device->uma) {
  6362. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  6363. src0_uma = d_X != nullptr;
  6364. if (use_src1) {
  6365. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  6366. src1_uma = d_Y != nullptr;
  6367. }
  6368. if (use_src2) {
  6369. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  6370. src2_uma = d_Z != nullptr;
  6371. }
  6372. }
  6373. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  6374. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  6375. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  6376. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  6377. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6378. // Workaround for tiny tensor inputs on ROPE
  6379. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  6380. y_sz = VK_WHOLE_SIZE;
  6381. }
  6382. GGML_ASSERT(d_D != nullptr);
  6383. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6384. if(!src0_uma) {
  6385. d_X = src0_buf_ctx->dev_buffer;
  6386. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6387. GGML_ASSERT(d_X != nullptr);
  6388. }
  6389. if (use_src1 && !src1_uma) {
  6390. d_Y = src1_buf_ctx->dev_buffer;
  6391. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6392. GGML_ASSERT(d_Y != nullptr);
  6393. }
  6394. if (use_src2 && !src2_uma) {
  6395. d_Z = src2_buf_ctx->dev_buffer;
  6396. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  6397. GGML_ASSERT(d_Z != nullptr);
  6398. }
  6399. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  6400. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  6401. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6402. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6403. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6404. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6405. if (op_supports_incontiguous) {
  6406. x_sz = ggml_nbytes(src0);
  6407. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  6408. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  6409. d_sz = ggml_nbytes(dst);
  6410. if (x_buf_offset + x_sz >= d_X->size) {
  6411. x_sz = VK_WHOLE_SIZE;
  6412. }
  6413. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  6414. y_sz = VK_WHOLE_SIZE;
  6415. }
  6416. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  6417. z_sz = VK_WHOLE_SIZE;
  6418. }
  6419. if (d_buf_offset + d_sz >= d_D->size) {
  6420. d_sz = VK_WHOLE_SIZE;
  6421. }
  6422. }
  6423. std::array<uint32_t, 3> elements;
  6424. // Single call if dimension 2 is contiguous
  6425. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  6426. switch (op) {
  6427. case GGML_OP_NORM:
  6428. case GGML_OP_RMS_NORM_BACK:
  6429. case GGML_OP_L2_NORM:
  6430. case GGML_OP_SOFT_MAX:
  6431. case GGML_OP_SOFT_MAX_BACK:
  6432. case GGML_OP_SUM_ROWS:
  6433. case GGML_OP_ARGMAX:
  6434. {
  6435. const uint32_t nr = ggml_nrows(src0);
  6436. if (nr > 262144) {
  6437. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  6438. } else if (nr > 512) {
  6439. elements = { 512, CEIL_DIV(nr, 512), 1 };
  6440. } else {
  6441. elements = { nr, 1, 1 };
  6442. }
  6443. } break;
  6444. case GGML_OP_RMS_NORM:
  6445. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  6446. break;
  6447. case GGML_OP_SUM:
  6448. // We use GGML_OP_SUM_ROWS with 1 row.
  6449. elements = { 1, 1, 1 };
  6450. break;
  6451. case GGML_OP_GROUP_NORM:
  6452. {
  6453. const uint32_t num_groups = dst->op_params[0];
  6454. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  6455. } break;
  6456. case GGML_OP_DIAG_MASK_INF:
  6457. case GGML_OP_ROPE:
  6458. case GGML_OP_ROPE_BACK:
  6459. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6460. break;
  6461. case GGML_OP_GET_ROWS:
  6462. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6463. break;
  6464. case GGML_OP_ARGSORT:
  6465. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6466. break;
  6467. case GGML_OP_IM2COL:
  6468. {
  6469. const bool is_2D = dst->op_params[6] == 1;
  6470. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6471. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6472. const uint32_t KW = src0->ne[0];
  6473. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6474. const uint32_t OW = dst->ne[1];
  6475. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6476. elements = { OW * KW * KH, OH, batch * IC };
  6477. } break;
  6478. case GGML_OP_TIMESTEP_EMBEDDING:
  6479. {
  6480. const uint32_t dim = dst->op_params[0];
  6481. uint32_t half_ceil = (dim + 1) / 2;
  6482. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6483. } break;
  6484. case GGML_OP_CONV_TRANSPOSE_1D:
  6485. {
  6486. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6487. } break;
  6488. case GGML_OP_POOL_2D:
  6489. {
  6490. const uint32_t N = dst->ne[3];
  6491. const uint32_t OC = dst->ne[2];
  6492. const uint32_t OH = dst->ne[1];
  6493. const uint32_t OW = dst->ne[0];
  6494. elements = { N * OC * OH * OW, 1, 1};
  6495. } break;
  6496. case GGML_OP_CONV_2D:
  6497. {
  6498. elements = ggml_vk_get_conv_elements(dst);
  6499. } break;
  6500. case GGML_OP_ADD:
  6501. case GGML_OP_SUB:
  6502. case GGML_OP_DIV:
  6503. case GGML_OP_MUL:
  6504. case GGML_OP_SCALE:
  6505. case GGML_OP_SQR:
  6506. case GGML_OP_SIN:
  6507. case GGML_OP_COS:
  6508. case GGML_OP_CLAMP:
  6509. case GGML_OP_PAD:
  6510. case GGML_OP_ROLL:
  6511. case GGML_OP_REPEAT:
  6512. case GGML_OP_REPEAT_BACK:
  6513. case GGML_OP_CPY:
  6514. case GGML_OP_CONCAT:
  6515. case GGML_OP_UPSCALE:
  6516. case GGML_OP_UNARY:
  6517. case GGML_OP_GLU:
  6518. case GGML_OP_CONV_2D_DW:
  6519. {
  6520. uint32_t ne = ggml_nelements(dst);
  6521. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6522. // Convert from number of logical elements to 2- or 4-byte units.
  6523. ne /= ggml_blck_size(src0->type);
  6524. if ((ggml_type_size(src0->type) % 4) == 0) {
  6525. ne *= ggml_type_size(src0->type) / 4;
  6526. } else {
  6527. ne *= ggml_type_size(src0->type) / 2;
  6528. }
  6529. }
  6530. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  6531. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  6532. // So divide by block size here before splitting into 512x512 groups.
  6533. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6534. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  6535. }
  6536. if (ne > 262144) {
  6537. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6538. } else if (ne > 512) {
  6539. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6540. } else {
  6541. elements = { ne, 1, 1 };
  6542. }
  6543. } break;
  6544. case GGML_OP_ADD_ID:
  6545. {
  6546. elements = { (uint32_t)ne01, (uint32_t)ne02, 1 };
  6547. } break;
  6548. case GGML_OP_SET_ROWS:
  6549. {
  6550. uint32_t ne = ggml_nelements(src0);
  6551. if (ggml_is_quantized(dst->type)) {
  6552. // quants run 32 threads each doing QUANT_K elements
  6553. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  6554. } else {
  6555. // scalar types do one element per thread, running 512 threads
  6556. ne = CEIL_DIV(ne, 512);
  6557. }
  6558. if (ne > 262144) {
  6559. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6560. } else if (ne > 512) {
  6561. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6562. } else {
  6563. elements = { ne, 1, 1 };
  6564. }
  6565. }
  6566. break;
  6567. default:
  6568. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  6569. break;
  6570. }
  6571. if (!op_supports_incontiguous) {
  6572. if (x_sz != VK_WHOLE_SIZE) {
  6573. x_sz *= ne02 * ne03;
  6574. }
  6575. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  6576. y_sz *= ne12 * ne13;
  6577. }
  6578. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  6579. z_sz *= ne22 * ne23;
  6580. }
  6581. if (d_sz != VK_WHOLE_SIZE) {
  6582. d_sz *= ned2 * ned3;
  6583. }
  6584. }
  6585. if (op == GGML_OP_GLU) {
  6586. // Empty src1 is possible in glu, but the shader needs a buffer
  6587. vk_subbuffer subbuf_y;
  6588. if (use_src1) {
  6589. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6590. } else {
  6591. subbuf_y = { d_X, 0, x_sz };
  6592. }
  6593. ggml_vk_sync_buffers(subctx);
  6594. 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 } }, pc, elements);
  6595. } else if (op == GGML_OP_SOFT_MAX) {
  6596. // Empty src1 and src2 is possible in soft_max, but the shader needs a buffer
  6597. vk_subbuffer subbuf_y;
  6598. if (use_src1) {
  6599. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6600. } else {
  6601. subbuf_y = { d_X, 0, x_sz };
  6602. }
  6603. vk_subbuffer subbuf_z;
  6604. if (use_src2) {
  6605. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6606. } else {
  6607. subbuf_z = { d_X, 0, x_sz };
  6608. }
  6609. ggml_vk_sync_buffers(subctx);
  6610. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6611. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  6612. // Empty src2 is possible in rope, but the shader needs a buffer
  6613. vk_subbuffer subbuf_z;
  6614. if (use_src2) {
  6615. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6616. } else {
  6617. subbuf_z = { d_X, 0, x_sz };
  6618. }
  6619. ggml_vk_sync_buffers(subctx);
  6620. 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 } }, pc, elements);
  6621. } else if (op == GGML_OP_IM2COL) {
  6622. // im2col uses only src1 and dst buffers
  6623. ggml_vk_sync_buffers(subctx);
  6624. 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 } }, pc, elements);
  6625. } else if (op == GGML_OP_COUNT_EQUAL) {
  6626. ggml_vk_sync_buffers(subctx);
  6627. // count_equal assumes that destination buffer is initialized with zeroes
  6628. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  6629. ggml_vk_sync_buffers(subctx);
  6630. 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 } }, pc, elements);
  6631. } else if (use_src2) {
  6632. ggml_vk_sync_buffers(subctx);
  6633. 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 } }, pc, elements);
  6634. } else if (use_src1) {
  6635. ggml_vk_sync_buffers(subctx);
  6636. 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 } }, pc, elements);
  6637. } else {
  6638. ggml_vk_sync_buffers(subctx);
  6639. 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 } }, pc, elements);
  6640. }
  6641. }
  6642. 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) {
  6643. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6644. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6645. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6646. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  6647. (uint32_t)ggml_nelements(src0),
  6648. (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,
  6649. (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,
  6650. (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,
  6651. 0,
  6652. 0.0f, 0.0f, 0,
  6653. }, dryrun);
  6654. }
  6655. 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) {
  6656. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6657. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6658. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6659. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6660. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6661. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6662. int offset = dst->op_params[3] / 4; // offset in bytes
  6663. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  6664. (uint32_t)ggml_nelements(src0),
  6665. (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,
  6666. (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,
  6667. (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,
  6668. 0,
  6669. 0.0f, 0.0f, offset,
  6670. }, dryrun);
  6671. }
  6672. 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) {
  6673. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6674. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6675. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6676. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  6677. (uint32_t)ggml_nelements(src0),
  6678. (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,
  6679. (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,
  6680. (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,
  6681. 0,
  6682. 0.0f, 0.0f, 0,
  6683. }, dryrun);
  6684. }
  6685. static void ggml_vk_sub(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6686. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6687. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6688. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6689. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  6690. (uint32_t)ggml_nelements(src0),
  6691. (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,
  6692. (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,
  6693. (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,
  6694. 0,
  6695. 0.0f, 0.0f, 0,
  6696. }, dryrun);
  6697. }
  6698. 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) {
  6699. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6700. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6701. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6702. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  6703. (uint32_t)ggml_nelements(src0),
  6704. (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,
  6705. (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,
  6706. (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,
  6707. 0,
  6708. 0.0f, 0.0f, 0,
  6709. }, dryrun);
  6710. }
  6711. 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) {
  6712. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6713. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6714. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6715. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  6716. (uint32_t)ggml_nelements(src0),
  6717. (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,
  6718. (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,
  6719. (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,
  6720. 0,
  6721. 0.0f, 0.0f, 0,
  6722. }, dryrun);
  6723. }
  6724. static void ggml_vk_add_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) {
  6725. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6726. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6727. const uint32_t src2_type_size = ggml_type_size(src2->type);
  6728. ggml_vk_op_f32<vk_op_add_id_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ADD_ID, {
  6729. (uint32_t)dst->ne[0],
  6730. (uint32_t)dst->ne[1],
  6731. (uint32_t)src0->nb[1] / src0_type_size,
  6732. (uint32_t)src0->nb[2] / src0_type_size,
  6733. (uint32_t)src1->nb[1] / src1_type_size,
  6734. (uint32_t)src2->nb[1] / src2_type_size,
  6735. }, dryrun);
  6736. }
  6737. static void ggml_vk_op_f32_wkv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_rwkv_wkv6_push_constants&& pc, int version, bool dryrun = false) {
  6738. GGML_ASSERT(version == 6 || version == 7);
  6739. int num_srcs = version == 6 ? 6 : 7;
  6740. for (int i = 0; i < num_srcs; i++) {
  6741. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  6742. }
  6743. GGML_ASSERT(dst->buffer != nullptr);
  6744. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  6745. GGML_ASSERT(pipeline != nullptr);
  6746. if (dryrun) {
  6747. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6748. return;
  6749. }
  6750. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6751. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6752. for (int i = 0; i < num_srcs; i++) {
  6753. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  6754. }
  6755. ggml_vk_sync_buffers(subctx);
  6756. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6757. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6758. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  6759. if (ctx->device->uma) {
  6760. for (int i = 0; i < num_srcs; i++) {
  6761. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  6762. srcs_uma[i] = d_srcs[i] != nullptr;
  6763. }
  6764. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  6765. dst_uma = d_D != nullptr;
  6766. }
  6767. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6768. for (int i = 0; i < num_srcs; i++) {
  6769. src_sizes[i] = ggml_nbytes(dst->src[i]);
  6770. if (!srcs_uma[i]) {
  6771. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  6772. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  6773. }
  6774. }
  6775. const uint64_t dst_size = ggml_nbytes(dst);
  6776. if (!dst_uma) {
  6777. d_D = dst_buf_ctx->dev_buffer;
  6778. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  6779. }
  6780. std::array<uint32_t, 3> elements = {
  6781. (uint32_t)(pc.B * pc.H),
  6782. 1,
  6783. 1
  6784. };
  6785. if (version == 6) {
  6786. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6787. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6788. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6789. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6790. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6791. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6792. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6793. vk_subbuffer{ d_D, dst_offset, dst_size }
  6794. }, pc, elements);
  6795. } else if (version == 7) {
  6796. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6797. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6798. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6799. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6800. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6801. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6802. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6803. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  6804. vk_subbuffer{ d_D, dst_offset, dst_size }
  6805. }, pc, elements);
  6806. } else {
  6807. // shouldn't happen
  6808. GGML_ASSERT(false);
  6809. }
  6810. }
  6811. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6812. const size_t seq_length = dst->src[0]->ne[2];
  6813. const size_t n_embed = dst->ne[0];
  6814. const size_t n_heads = dst->src[0]->ne[1];
  6815. const size_t n_seqs = dst->src[5]->ne[1];
  6816. ggml_vk_op_f32_wkv(
  6817. ctx, subctx, dst,
  6818. {
  6819. (uint32_t)n_seqs,
  6820. (uint32_t)seq_length,
  6821. (uint32_t)n_embed,
  6822. (uint32_t)n_heads,
  6823. },
  6824. 6,
  6825. dryrun
  6826. );
  6827. }
  6828. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6829. const size_t seq_length = dst->src[0]->ne[2];
  6830. const size_t n_embed = dst->ne[0];
  6831. const size_t n_heads = dst->src[0]->ne[1];
  6832. const size_t n_seqs = dst->src[6]->ne[1];
  6833. ggml_vk_op_f32_wkv(
  6834. ctx, subctx, dst,
  6835. {
  6836. (uint32_t)n_seqs,
  6837. (uint32_t)seq_length,
  6838. (uint32_t)n_embed,
  6839. (uint32_t)n_heads,
  6840. },
  6841. 7,
  6842. dryrun
  6843. );
  6844. }
  6845. static void ggml_vk_op_f32_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_push_constants&& pc, bool dryrun = false) {
  6846. const ggml_tensor * x = dst->src[0];
  6847. const ggml_tensor * g = dst->src[1];
  6848. const ggml_tensor * gm = dst->src[2];
  6849. const ggml_tensor * gv = dst->src[3];
  6850. const ggml_tensor * p = dst->src[4];
  6851. GGML_ASSERT(x->type == GGML_TYPE_F32);
  6852. GGML_ASSERT(g->type == GGML_TYPE_F32);
  6853. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  6854. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  6855. GGML_ASSERT(p->type == GGML_TYPE_F32);
  6856. GGML_ASSERT(dst->buffer != nullptr);
  6857. GGML_ASSERT(ggml_is_contiguous(x));
  6858. GGML_ASSERT(ggml_is_contiguous(g));
  6859. GGML_ASSERT(ggml_is_contiguous(gm));
  6860. GGML_ASSERT(ggml_is_contiguous(gv));
  6861. GGML_ASSERT(ggml_is_contiguous(p));
  6862. GGML_ASSERT(ggml_are_same_shape(x, g));
  6863. GGML_ASSERT(ggml_are_same_shape(x, gm));
  6864. GGML_ASSERT(ggml_are_same_shape(x, gv));
  6865. GGML_ASSERT(ggml_nelements(p) == 7);
  6866. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  6867. GGML_ASSERT(pipeline != nullptr);
  6868. if (dryrun) {
  6869. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6870. return;
  6871. }
  6872. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  6873. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  6874. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  6875. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  6876. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  6877. ggml_vk_sync_buffers(subctx);
  6878. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  6879. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  6880. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  6881. if (ctx->device->uma) {
  6882. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  6883. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  6884. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  6885. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  6886. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  6887. X_uma = d_X != nullptr;
  6888. G_uma = d_G != nullptr;
  6889. GM_uma = d_GM != nullptr;
  6890. GV_uma = d_GV != nullptr;
  6891. P_uma = d_P != nullptr;
  6892. }
  6893. if (!X_uma) {
  6894. d_X = x_buf_ctx->dev_buffer;
  6895. x_offset = vk_tensor_offset(x) + x->view_offs;
  6896. }
  6897. if (!G_uma) {
  6898. d_G = g_buf_ctx->dev_buffer;
  6899. g_offset = vk_tensor_offset(g) + g->view_offs;
  6900. }
  6901. if (!GM_uma) {
  6902. d_GM = gm_buf_ctx->dev_buffer;
  6903. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  6904. }
  6905. if (!GV_uma) {
  6906. d_GV = gv_buf_ctx->dev_buffer;
  6907. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  6908. }
  6909. if (!P_uma) {
  6910. d_P = p_buf_ctx->dev_buffer;
  6911. p_offset = vk_tensor_offset(p) + p->view_offs;
  6912. }
  6913. const uint64_t x_size = ggml_nbytes(x);
  6914. const uint64_t g_size = ggml_nbytes(g);
  6915. const uint64_t gm_size = ggml_nbytes(gm);
  6916. const uint64_t gv_size = ggml_nbytes(gv);
  6917. const uint64_t p_size = ggml_nbytes(p);
  6918. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  6919. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6920. vk_subbuffer{ d_X, x_offset, x_size },
  6921. vk_subbuffer{ d_G, g_offset, g_size },
  6922. vk_subbuffer{ d_GM, gm_offset, gm_size },
  6923. vk_subbuffer{ d_GV, gv_offset, gv_size },
  6924. vk_subbuffer{ d_P, p_offset, p_size },
  6925. }, pc, elements);
  6926. }
  6927. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6928. const size_t n = ggml_nelements(dst->src[0]);
  6929. ggml_vk_op_f32_opt_step_adamw(
  6930. ctx, subctx, dst,
  6931. { (uint32_t)n, 0, 0.0f, 0.0f },
  6932. dryrun
  6933. );
  6934. }
  6935. 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) {
  6936. int * op_params = (int *)dst->op_params;
  6937. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6938. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6939. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6940. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  6941. (uint32_t)ggml_nelements(dst),
  6942. (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,
  6943. (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,
  6944. (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,
  6945. 0,
  6946. 0.0f, 0.0f, op_params[0],
  6947. }, dryrun);
  6948. }
  6949. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6950. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6951. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  6952. float sf0 = (float)dst->ne[0] / src0->ne[0];
  6953. float sf1 = (float)dst->ne[1] / src0->ne[1];
  6954. float sf2 = (float)dst->ne[2] / src0->ne[2];
  6955. float sf3 = (float)dst->ne[3] / src0->ne[3];
  6956. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  6957. sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
  6958. sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
  6959. }
  6960. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  6961. (uint32_t)ggml_nelements(dst), 0, 0,
  6962. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1],
  6963. (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,
  6964. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  6965. sf0, sf1, sf2, sf3,
  6966. }, dryrun);
  6967. }
  6968. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6969. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6970. p.param1 = ggml_get_op_params_f32(dst, 0);
  6971. p.param2 = ggml_get_op_params_f32(dst, 1);
  6972. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  6973. }
  6974. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6975. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6976. }
  6977. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6978. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6979. }
  6980. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6981. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6982. }
  6983. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6984. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6985. p.param1 = ggml_get_op_params_f32(dst, 0);
  6986. p.param2 = ggml_get_op_params_f32(dst, 1);
  6987. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  6988. }
  6989. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6990. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6991. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  6992. }
  6993. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6994. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  6995. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  6996. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  6997. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  6998. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  6999. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  7000. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  7001. memcpy(&p.param1, &s01_packed, sizeof(float));
  7002. memcpy(&p.param2, &s23_packed, sizeof(float));
  7003. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  7004. }
  7005. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7006. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7007. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  7008. }
  7009. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7010. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  7011. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  7012. }
  7013. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7014. uint32_t ne = (uint32_t)ggml_nelements(src0);
  7015. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7016. // Convert from number of logical elements to 2- or 4-byte units.
  7017. ne /= ggml_blck_size(src0->type);
  7018. if ((ggml_type_size(src0->type) % 4) == 0) {
  7019. ne *= ggml_type_size(src0->type) / 4;
  7020. } else {
  7021. ne *= ggml_type_size(src0->type) / 2;
  7022. }
  7023. }
  7024. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  7025. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  7026. }
  7027. static void ggml_vk_set_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7028. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7029. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7030. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7031. // Skip empty skip_rows operations. For most ops the empty check at the start
  7032. // of ggml_vk_build_graph is sufficient, but set_rows can have a nonempty dst
  7033. // with empty srcs.
  7034. if (ggml_is_empty(src0) || ggml_is_empty(src1)) {
  7035. return;
  7036. }
  7037. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  7038. (uint32_t)ggml_nelements(src0),
  7039. (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,
  7040. (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,
  7041. (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,
  7042. 0,
  7043. 0.0f, 0.0f, 0,
  7044. }, dryrun);
  7045. }
  7046. static void ggml_vk_silu_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7047. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SILU_BACK, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  7048. }
  7049. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7050. float * op_params = (float *)dst->op_params;
  7051. 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);
  7052. }
  7053. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7054. const int * int_op_params = (const int *)dst->op_params;
  7055. const float * float_op_params = (const float *)dst->op_params;
  7056. const uint32_t num_groups = int_op_params[0];
  7057. const float eps = float_op_params[1];
  7058. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  7059. 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);
  7060. }
  7061. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, float * op_params, bool dryrun = false) {
  7062. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7063. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7064. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7065. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  7066. (uint32_t)ggml_nelements(src0),
  7067. (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,
  7068. (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,
  7069. (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,
  7070. 0,
  7071. op_params[0], 0.0f, 0,
  7072. }, dryrun);
  7073. }
  7074. static void ggml_vk_rms_norm_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7075. float * op_params = (float *)dst->op_params;
  7076. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  7077. }
  7078. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7079. float * op_params = (float *)dst->op_params;
  7080. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_L2_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  7081. }
  7082. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7083. 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);
  7084. }
  7085. static void ggml_vk_glu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7086. const float * op_params_f = (const float *)dst->op_params;
  7087. const bool swapped = (bool)dst->op_params[1];
  7088. const bool split = src1 != nullptr;
  7089. const float alpha = op_params_f[2];
  7090. const float limit = op_params_f[3];
  7091. GGML_ASSERT(ggml_is_contiguous(src0));
  7092. if (!split) {
  7093. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  7094. } else {
  7095. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  7096. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  7097. GGML_ASSERT(src0->type == src1->type);
  7098. }
  7099. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  7100. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU,
  7101. {
  7102. (uint32_t)ggml_nelements(dst),
  7103. (uint32_t)src0->ne[0],
  7104. (uint32_t)dst->ne[0],
  7105. mode,
  7106. alpha,
  7107. limit
  7108. }, dryrun);
  7109. }
  7110. 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) {
  7111. int32_t * op_params = (int32_t *)dst->op_params;
  7112. 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);
  7113. }
  7114. static void ggml_vk_soft_max(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) {
  7115. float * op_params = (float *)dst->op_params;
  7116. float scale = op_params[0];
  7117. float max_bias = op_params[1];
  7118. const uint32_t ncols = (uint32_t)src0->ne[0];
  7119. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  7120. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  7121. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  7122. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  7123. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  7124. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  7125. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  7126. const uint32_t n_head_kv = src0->ne[2];
  7127. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  7128. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7129. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7130. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_SOFT_MAX, {
  7131. ncols,
  7132. src1 != nullptr ? nrows_y : (uint32_t)0,
  7133. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  7134. ne12, ne13,
  7135. nb11, nb12, nb13,
  7136. scale, max_bias,
  7137. m0, m1,
  7138. n_head_log2,
  7139. nrows_x,
  7140. src2 != nullptr
  7141. }, dryrun);
  7142. }
  7143. static void ggml_vk_soft_max_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7144. float * op_params = (float *)dst->op_params;
  7145. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], op_params[1] }, dryrun);
  7146. }
  7147. 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 backprop, bool dryrun = false) {
  7148. const int n_dims = ((int32_t *) dst->op_params)[1];
  7149. const int mode = ((int32_t *) dst->op_params)[2];
  7150. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  7151. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7152. const float freq_base = ((float *) dst->op_params)[5];
  7153. const float freq_scale = ((float *) dst->op_params)[6];
  7154. const float ext_factor = ((float *) dst->op_params)[7];
  7155. const float attn_factor = ((float *) dst->op_params)[8];
  7156. const float beta_fast = ((float *) dst->op_params)[9];
  7157. const float beta_slow = ((float *) dst->op_params)[10];
  7158. int sections[4] {};
  7159. if (mode & GGML_ROPE_TYPE_MROPE) {
  7160. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7161. }
  7162. float corr_dims[2];
  7163. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7164. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7165. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  7166. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  7167. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  7168. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  7169. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  7170. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  7171. sections[0], sections[1], sections[2], sections[3], backprop
  7172. }, dryrun);
  7173. }
  7174. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7175. int32_t * op_params = (int32_t *)dst->op_params;
  7176. uint32_t ncols = src0->ne[0];
  7177. uint32_t ncols_pad = 1;
  7178. while (ncols_pad < ncols) {
  7179. ncols_pad *= 2;
  7180. }
  7181. GGML_ASSERT(ncols_pad <= 1024);
  7182. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  7183. ncols,
  7184. ncols_pad,
  7185. op_params[0],
  7186. }, dryrun);
  7187. }
  7188. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7189. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  7190. }
  7191. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7192. 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);
  7193. }
  7194. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7195. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  7196. }
  7197. static void ggml_vk_count_equal(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7198. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_COUNT_EQUAL, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  7199. }
  7200. 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) {
  7201. const int32_t s0 = dst->op_params[0];
  7202. const int32_t s1 = dst->op_params[1];
  7203. const int32_t p0 = dst->op_params[2];
  7204. const int32_t p1 = dst->op_params[3];
  7205. const int32_t d0 = dst->op_params[4];
  7206. const int32_t d1 = dst->op_params[5];
  7207. const bool is_2D = dst->op_params[6] == 1;
  7208. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  7209. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  7210. const uint32_t IW = src1->ne[0];
  7211. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  7212. const uint32_t KW = src0->ne[0];
  7213. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  7214. const uint32_t OW = dst->ne[1];
  7215. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7216. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  7217. const uint32_t pelements = OW * KW * KH;
  7218. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  7219. batch_offset, offset_delta,
  7220. IC, IW, IH, OW, OH, KW, KH,
  7221. pelements,
  7222. IC * KH * KW,
  7223. s0, s1, p0, p1, d0, d1,
  7224. }, dryrun);
  7225. }
  7226. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7227. const uint32_t dim = dst->op_params[0];
  7228. const uint32_t max_period = dst->op_params[1];
  7229. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  7230. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  7231. nb1, dim, max_period,
  7232. }, dryrun);
  7233. }
  7234. static void ggml_vk_conv_transpose_1d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7235. // src0: (K, Cout, Cin, 1) -- kernel
  7236. // src1: (L, Cin, 1, 1) -- input
  7237. // dst: (*, Cout, 1, 1)
  7238. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7239. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7240. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7241. GGML_TENSOR_BINARY_OP_LOCALS
  7242. GGML_ASSERT(nb00 == sizeof(float));
  7243. GGML_ASSERT(nb10 == sizeof(float));
  7244. const int32_t s0 = dst->op_params[0];
  7245. vk_op_conv_transpose_1d_push_constants p{};
  7246. p.Cout = static_cast<uint32_t>(ne01);
  7247. p.Cin = static_cast<uint32_t>(ne02);
  7248. p.K = static_cast<uint32_t>(ne00);
  7249. p.L = static_cast<uint32_t>(ne10);
  7250. p.KL = static_cast<uint32_t>(ne0);
  7251. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  7252. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  7253. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  7254. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  7255. p.s0 = static_cast<uint32_t>(s0);
  7256. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  7257. }
  7258. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7259. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  7260. const int32_t k1 = dst->op_params[1];
  7261. const int32_t k0 = dst->op_params[2];
  7262. const int32_t s1 = dst->op_params[3];
  7263. const int32_t s0 = dst->op_params[4];
  7264. const int32_t p1 = dst->op_params[5];
  7265. const int32_t p0 = dst->op_params[6];
  7266. const uint32_t IH = src0->ne[1];
  7267. const uint32_t IW = src0->ne[0];
  7268. const uint32_t N = dst->ne[3];
  7269. const uint32_t OC = dst->ne[2];
  7270. const uint32_t OH = dst->ne[1];
  7271. const uint32_t OW = dst->ne[0];
  7272. const uint32_t parallel_elements = N * OC * OH * OW;
  7273. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  7274. IW, IH, OW, OH, OC,
  7275. parallel_elements,
  7276. op,
  7277. k0, k1, s0, s1, p0, p1,
  7278. }, dryrun);
  7279. }
  7280. static void ggml_vk_conv_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  7281. const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7282. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7283. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7284. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  7285. GGML_TENSOR_BINARY_OP_LOCALS
  7286. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  7287. GGML_ASSERT(nb10 == sizeof(float));
  7288. GGML_ASSERT(nb0 == sizeof(float));
  7289. vk_op_conv2d_push_constants p{};
  7290. p.Cout = static_cast<uint32_t>(ne03);
  7291. p.Cin = static_cast<uint32_t>(ne02);
  7292. p.N = static_cast<uint32_t>(ne13);
  7293. p.KW = static_cast<uint32_t>(ne00);
  7294. p.KH = static_cast<uint32_t>(ne01);
  7295. p.W = static_cast<uint32_t>(ne10);
  7296. p.H = static_cast<uint32_t>(ne11);
  7297. p.OW = static_cast<uint32_t>(ne0);
  7298. p.OH = static_cast<uint32_t>(ne1);
  7299. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  7300. p.s1 = static_cast<uint32_t>(dst->op_params[1]);
  7301. p.p0 = static_cast<uint32_t>(dst->op_params[2]);
  7302. p.p1 = static_cast<uint32_t>(dst->op_params[3]);
  7303. p.d0 = static_cast<uint32_t>(dst->op_params[4]);
  7304. p.d1 = static_cast<uint32_t>(dst->op_params[5]);
  7305. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  7306. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  7307. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  7308. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  7309. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  7310. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  7311. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  7312. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  7313. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  7314. GGML_ASSERT(ne03 == ne2);
  7315. GGML_ASSERT(ne02 == ne12);
  7316. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D, std::move(p), dryrun);
  7317. }
  7318. static void ggml_vk_conv_2d_dw(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  7319. vk_op_conv2d_dw_push_constants p{};
  7320. p.ne = ggml_nelements(dst);
  7321. p.channels = dst->ne[2];
  7322. p.batches = dst->ne[3];
  7323. p.dst_w = dst->ne[0];
  7324. p.dst_h = dst->ne[1];
  7325. p.src_w = src1->ne[0];
  7326. p.src_h = src1->ne[1];
  7327. p.knl_w = src0->ne[0];
  7328. p.knl_h = src0->ne[1];
  7329. p.stride_x = dst->op_params[0];
  7330. p.stride_y = dst->op_params[1];
  7331. p.pad_x = dst->op_params[2];
  7332. p.pad_y = dst->op_params[3];
  7333. p.dilation_x = dst->op_params[4];
  7334. p.dilation_y = dst->op_params[5];
  7335. GGML_ASSERT(src0->ne[3] == p.channels);
  7336. GGML_ASSERT(src1->ne[3] == p.batches);
  7337. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  7338. }
  7339. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  7340. const float * op_params = (const float *)dst->op_params;
  7341. 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);
  7342. }
  7343. #ifdef GGML_VULKAN_RUN_TESTS
  7344. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  7345. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  7346. return;
  7347. }
  7348. i0 = std::max(i0, 5);
  7349. i1 = std::max(i1, 5);
  7350. i2 = std::max(i2, 0);
  7351. fprintf(stderr, " ");
  7352. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7353. fprintf(stderr, "%7d ", idx1);
  7354. }
  7355. fprintf(stderr, "\n");
  7356. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7357. fprintf(stderr, "%7d: ", idx0);
  7358. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7359. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  7360. float val;
  7361. if (type == GGML_TYPE_F32) {
  7362. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  7363. } else if (type == GGML_TYPE_F16) {
  7364. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  7365. } else {
  7366. GGML_ABORT("fatal error");
  7367. }
  7368. fprintf(stderr, "% 7.2f ", val);
  7369. } else {
  7370. fprintf(stderr, " ");
  7371. }
  7372. }
  7373. fprintf(stderr, "\n");
  7374. }
  7375. }
  7376. template <typename X_TYPE, typename Y_TYPE>
  7377. 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) {
  7378. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  7379. const size_t x_ne = m * k * batch;
  7380. const size_t y_ne = k * n * batch;
  7381. const size_t d_ne = m * n * batch;
  7382. vk_pipeline p;
  7383. std::string shname;
  7384. if (shader_size == 0) {
  7385. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7386. p = ctx->device->pipeline_matmul_f32->a_s;
  7387. shname = "F32_ALIGNED_S";
  7388. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7389. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  7390. shname = "F32_F16_ALIGNED_S";
  7391. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7392. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  7393. shname = "F16_F32_ALIGNED_S";
  7394. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7395. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  7396. shname = "F16_ALIGNED_S";
  7397. } else {
  7398. GGML_ABORT("fatal error");
  7399. }
  7400. } else if (shader_size == 1) {
  7401. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7402. p = ctx->device->pipeline_matmul_f32->a_m;
  7403. shname = "F32_ALIGNED_M";
  7404. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7405. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  7406. shname = "F32_F16_ALIGNED_M";
  7407. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7408. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  7409. shname = "F16_F32_ALIGNED_M";
  7410. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7411. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  7412. shname = "F16_ALIGNED_M";
  7413. } else {
  7414. GGML_ABORT("fatal error");
  7415. }
  7416. } else if (shader_size == 2) {
  7417. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7418. p = ctx->device->pipeline_matmul_f32->a_l;
  7419. shname = "F32_ALIGNED_L";
  7420. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7421. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  7422. shname = "F32_F16_ALIGNED_L";
  7423. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7424. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  7425. shname = "F16_F32_ALIGNED_L";
  7426. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7427. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  7428. shname = "F16_ALIGNED_L";
  7429. } else {
  7430. GGML_ABORT("fatal error");
  7431. }
  7432. } else {
  7433. GGML_ASSERT(0);
  7434. }
  7435. const size_t kpad = ggml_vk_align_size(k, p->align);
  7436. if (k != kpad) {
  7437. if (shader_size == 0) {
  7438. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7439. p = ctx->device->pipeline_matmul_f32->s;
  7440. shname = "F32_S";
  7441. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7442. p = ctx->device->pipeline_matmul_f32_f16->s;
  7443. shname = "F32_F16_S";
  7444. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7445. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  7446. shname = "F16_F32_S";
  7447. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7448. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  7449. shname = "F16_S";
  7450. }
  7451. } else if (shader_size == 1) {
  7452. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7453. p = ctx->device->pipeline_matmul_f32->m;
  7454. shname = "F32_M";
  7455. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7456. p = ctx->device->pipeline_matmul_f32_f16->m;
  7457. shname = "F32_F16_M";
  7458. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7459. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  7460. shname = "F16_F32_M";
  7461. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7462. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  7463. shname = "F16_M";
  7464. }
  7465. } else if (shader_size == 2) {
  7466. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7467. p = ctx->device->pipeline_matmul_f32->l;
  7468. shname = "F32_L";
  7469. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7470. p = ctx->device->pipeline_matmul_f32_f16->l;
  7471. shname = "F32_F16_L";
  7472. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7473. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  7474. shname = "F16_F32_L";
  7475. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7476. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  7477. shname = "F16_L";
  7478. }
  7479. }
  7480. }
  7481. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7482. if (split_k > 1) {
  7483. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7484. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7485. // Resize buffer
  7486. if (ctx->prealloc_split_k != nullptr) {
  7487. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7488. }
  7489. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7490. }
  7491. }
  7492. if (ctx->device->need_compiles) {
  7493. ggml_vk_load_shaders(ctx->device);
  7494. }
  7495. ggml_pipeline_allocate_descriptor_sets(ctx);
  7496. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7497. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7498. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7499. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  7500. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  7501. float* d = (float *) malloc(sizeof(float) * d_ne);
  7502. for (size_t i = 0; i < x_ne; i++) {
  7503. if (std::is_same<float, X_TYPE>()) {
  7504. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7505. // x[i] = 1.0f;
  7506. // x[i] = i + 1;
  7507. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7508. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7509. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7510. // x[i] = ggml_fp32_to_fp16(1.0f);
  7511. // x[i] = ggml_fp32_to_fp16(i + 1);
  7512. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7513. } else {
  7514. GGML_ABORT("fatal error");
  7515. }
  7516. }
  7517. for (size_t i = 0; i < y_ne; i++) {
  7518. if (std::is_same<float, Y_TYPE>()) {
  7519. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7520. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7521. // y[i] = i + 1;
  7522. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7523. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7524. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7525. // y[i] = ggml_fp32_to_fp16(i + 1);
  7526. } else {
  7527. GGML_ABORT("fatal error");
  7528. }
  7529. }
  7530. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  7531. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  7532. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7533. ggml_vk_ctx_begin(ctx->device, subctx);
  7534. for (size_t i = 0; i < num_it; i++) {
  7535. ggml_vk_matmul(
  7536. 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),
  7537. m, n, k,
  7538. k, k, m, k*m, k*n, m*n,
  7539. split_k, batch, batch, batch, 1, 1, n
  7540. );
  7541. }
  7542. ggml_vk_ctx_end(subctx);
  7543. auto begin = std::chrono::high_resolution_clock::now();
  7544. ggml_vk_submit(subctx, ctx->fence);
  7545. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  7546. ctx->device->device.resetFences({ ctx->fence });
  7547. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7548. auto end = std::chrono::high_resolution_clock::now();
  7549. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7550. // copy dst to host
  7551. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  7552. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  7553. ggml_init_params iparams = {
  7554. /*.mem_size =*/ 1024*1024*1024,
  7555. /*.mem_buffer =*/ NULL,
  7556. /*.no_alloc =*/ true,
  7557. };
  7558. ggml_context * ggml_ctx = ggml_init(iparams);
  7559. ggml_type src0_type;
  7560. ggml_type src1_type;
  7561. if (std::is_same<float, X_TYPE>()) {
  7562. src0_type = GGML_TYPE_F32;
  7563. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7564. src0_type = GGML_TYPE_F16;
  7565. } else {
  7566. GGML_ABORT("fatal error");
  7567. }
  7568. if (std::is_same<float, Y_TYPE>()) {
  7569. src1_type = GGML_TYPE_F32;
  7570. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7571. src1_type = GGML_TYPE_F16;
  7572. } else {
  7573. GGML_ABORT("fatal error");
  7574. }
  7575. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  7576. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  7577. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7578. src0_ggml->data = x;
  7579. src1_ggml->data = y;
  7580. tensor_ggml->data = d_chk;
  7581. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7582. ggml_build_forward_expand(cgraph, tensor_ggml);
  7583. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7584. ggml_free(ggml_ctx);
  7585. double avg_err = 0.0;
  7586. int first_err_n = -1;
  7587. int first_err_m = -1;
  7588. int first_err_b = -1;
  7589. for (size_t i = 0; i < m*n*batch; i++) {
  7590. double err = std::fabs(d[i] - d_chk[i]);
  7591. avg_err += err;
  7592. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7593. first_err_b = i / (m * n);
  7594. first_err_n = (i % (m * n)) / m;
  7595. first_err_m = (i % (m * n)) % m;
  7596. }
  7597. }
  7598. avg_err /= m * n;
  7599. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7600. std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  7601. if (avg_err > 0.1 || std::isnan(avg_err)) {
  7602. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7603. std::cerr << "Actual result: " << std::endl << std::endl;
  7604. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7605. std::cerr << "Expected result: " << std::endl << std::endl;
  7606. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7607. if (split_k > 1) {
  7608. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7609. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7610. std::cerr << "d_buf0: " << std::endl << std::endl;
  7611. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7612. std::cerr << "d_buf1: " << std::endl << std::endl;
  7613. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7614. std::cerr << "d_buf2: " << std::endl << std::endl;
  7615. 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);
  7616. std::cerr << "d_buf3: " << std::endl << std::endl;
  7617. 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);
  7618. free(split_k_buf);
  7619. }
  7620. }
  7621. free(d_chk);
  7622. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  7623. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  7624. ggml_vk_destroy_buffer(d_X);
  7625. ggml_vk_destroy_buffer(d_Y);
  7626. ggml_vk_destroy_buffer(d_D);
  7627. free(x);
  7628. free(y);
  7629. free(d);
  7630. }
  7631. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  7632. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  7633. return;
  7634. }
  7635. i0 = std::max(i0, 5);
  7636. i1 = std::max(i1, 5);
  7637. i2 = std::max(i2, 0);
  7638. i3 = std::max(i3, 0);
  7639. fprintf(stderr, " ");
  7640. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7641. fprintf(stderr, "%7d ", idx1);
  7642. }
  7643. fprintf(stderr, "\n");
  7644. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7645. fprintf(stderr, "%7d: ", idx0);
  7646. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7647. 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]) {
  7648. float val;
  7649. if (tensor->type == GGML_TYPE_F32) {
  7650. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7651. } else if (tensor->type == GGML_TYPE_F16) {
  7652. 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]));
  7653. } else {
  7654. GGML_ABORT("fatal error");
  7655. }
  7656. fprintf(stderr, "% 7.2f ", val);
  7657. } else {
  7658. fprintf(stderr, " ");
  7659. }
  7660. }
  7661. fprintf(stderr, "\n");
  7662. }
  7663. }
  7664. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  7665. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  7666. }
  7667. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  7668. if (quant == GGML_TYPE_F32) {
  7669. memcpy(to, from, sizeof(float) * ne);
  7670. return;
  7671. }
  7672. const auto * tt = ggml_get_type_traits(quant);
  7673. ggml_to_float_t dequant_fn = tt->to_float;
  7674. dequant_fn(from, to, ne);
  7675. }
  7676. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7677. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  7678. const size_t x_sz = sizeof(float) * ne;
  7679. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  7680. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7681. float * x = (float *) malloc(x_sz);
  7682. void * qx = malloc(qx_sz);
  7683. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7684. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7685. float * x_ref = (float *) malloc(x_sz);
  7686. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  7687. for (size_t i = 0; i < ne; i++) {
  7688. x[i] = rand() / (float)RAND_MAX;
  7689. }
  7690. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  7691. ggml_vk_quantize_data(x, qx, ne, quant);
  7692. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  7693. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7694. if (ctx->device->need_compiles) {
  7695. ggml_vk_load_shaders(ctx->device);
  7696. }
  7697. ggml_pipeline_allocate_descriptor_sets(ctx);
  7698. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7699. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7700. ggml_vk_ctx_begin(ctx->device, subctx);
  7701. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  7702. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc, { (uint32_t)ne, 1, 1});
  7703. ggml_vk_ctx_end(subctx);
  7704. auto begin = std::chrono::high_resolution_clock::now();
  7705. ggml_vk_submit(subctx, ctx->fence);
  7706. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7707. ctx->device->device.resetFences({ ctx->fence });
  7708. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7709. auto end = std::chrono::high_resolution_clock::now();
  7710. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7711. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  7712. int first_err = -1;
  7713. double avg_err = 0.0;
  7714. for (size_t i = 0; i < ne; i++) {
  7715. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  7716. avg_err += error;
  7717. if (first_err < 0 && error > 0.05) {
  7718. first_err = i;
  7719. }
  7720. }
  7721. avg_err /= ne;
  7722. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  7723. if (avg_err > 0.1) {
  7724. std::cerr << "first_error = " << first_err << std::endl;
  7725. std::cerr << "Actual result: " << std::endl << std::endl;
  7726. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7727. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  7728. }
  7729. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  7730. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7731. std::cerr << x_ref[i] << ", ";
  7732. }
  7733. std::cerr << std::endl;
  7734. }
  7735. ggml_vk_destroy_buffer(x_buf);
  7736. ggml_vk_destroy_buffer(qx_buf);
  7737. free(x);
  7738. free(qx);
  7739. free(x_ref);
  7740. free(x_chk);
  7741. }
  7742. // This does not work without ggml q8_1 quantization support
  7743. //
  7744. // typedef uint16_t ggml_half;
  7745. // typedef uint32_t ggml_half2;
  7746. //
  7747. // #define QK8_1 32
  7748. // typedef struct {
  7749. // union {
  7750. // struct {
  7751. // ggml_half d; // delta
  7752. // ggml_half s; // d * sum(qs[i])
  7753. // } GGML_COMMON_AGGR_S;
  7754. // ggml_half2 ds;
  7755. // } GGML_COMMON_AGGR_U;
  7756. // int8_t qs[QK8_1]; // quants
  7757. // } block_q8_1;
  7758. //
  7759. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7760. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  7761. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  7762. //
  7763. // const size_t x_sz = sizeof(float) * ne;
  7764. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7765. // float * x = (float *) malloc(x_sz);
  7766. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  7767. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  7768. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7769. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7770. //
  7771. // for (size_t i = 0; i < ne; i++) {
  7772. // x[i] = rand() / (float)RAND_MAX;
  7773. // }
  7774. //
  7775. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  7776. //
  7777. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7778. //
  7779. // if (ctx->device->need_compiles) {
  7780. // ggml_vk_load_shaders(ctx->device);
  7781. // }
  7782. //
  7783. // ggml_pipeline_allocate_descriptor_sets(ctx);
  7784. //
  7785. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  7786. //
  7787. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7788. // ggml_vk_ctx_begin(ctx->device, subctx);
  7789. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  7790. // ggml_vk_ctx_end(subctx);
  7791. //
  7792. // auto begin = std::chrono::high_resolution_clock::now();
  7793. //
  7794. // ggml_vk_submit(subctx, ctx->fence);
  7795. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  7796. // ctx->device->device.resetFences({ ctx->fence });
  7797. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  7798. //
  7799. // auto end = std::chrono::high_resolution_clock::now();
  7800. //
  7801. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7802. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  7803. //
  7804. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  7805. //
  7806. // int first_err = -1;
  7807. //
  7808. // for (size_t i = 0; i < ne / 32; i++) {
  7809. // double error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d));
  7810. //
  7811. // if (first_err < 0 && error > 0.1) {
  7812. // first_err = i;
  7813. // }
  7814. //
  7815. // error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s));
  7816. //
  7817. // if (first_err < 0 && error > 0.1) {
  7818. // first_err = i;
  7819. // }
  7820. //
  7821. // for (size_t j = 0; j < 32; j++) {
  7822. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  7823. //
  7824. // if (first_err < 0 && error > 1) {
  7825. // first_err = i;
  7826. // }
  7827. // }
  7828. // }
  7829. //
  7830. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  7831. //
  7832. // if (first_err != -1) {
  7833. // std::cerr << "first_error = " << first_err << std::endl;
  7834. // std::cerr << "Actual result: " << std::endl << std::endl;
  7835. // std::cout << "d=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  7836. // for (size_t j = 0; j < 32; j++) {
  7837. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  7838. // }
  7839. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  7840. // std::cout << "d=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  7841. // for (size_t j = 0; j < 32; j++) {
  7842. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  7843. // }
  7844. // std::cerr << std::endl;
  7845. // }
  7846. //
  7847. // ggml_vk_destroy_buffer(x_buf);
  7848. // ggml_vk_destroy_buffer(qx_buf);
  7849. //
  7850. // free(x);
  7851. // free(qx);
  7852. // free(qx_res);
  7853. // }
  7854. 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, bool mmq = false) {
  7855. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  7856. const size_t x_ne = m * k * batch;
  7857. const size_t y_ne = k * n * batch;
  7858. const size_t d_ne = m * n * batch;
  7859. vk_matmul_pipeline2 * pipelines;
  7860. if (mmq) {
  7861. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  7862. } else {
  7863. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  7864. }
  7865. const bool fp16acc = ctx->device->fp16;
  7866. vk_pipeline p;
  7867. std::string shname;
  7868. if (shader_size == 0) {
  7869. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  7870. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  7871. } else if (shader_size == 1) {
  7872. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  7873. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  7874. } else if (shader_size == 2) {
  7875. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  7876. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  7877. } else {
  7878. GGML_ASSERT(0);
  7879. }
  7880. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  7881. if (mmq || k != kpad) {
  7882. if (shader_size == 0) {
  7883. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  7884. shname = std::string(ggml_type_name(quant)) + "_S";
  7885. } else if (shader_size == 1) {
  7886. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  7887. shname = std::string(ggml_type_name(quant)) + "_M";
  7888. } else if (shader_size == 2) {
  7889. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  7890. shname = std::string(ggml_type_name(quant)) + "_L";
  7891. } else {
  7892. GGML_ASSERT(0);
  7893. }
  7894. }
  7895. if (p == nullptr) {
  7896. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  7897. return;
  7898. }
  7899. const size_t x_sz = sizeof(float) * x_ne;
  7900. const size_t y_sz = sizeof(float) * y_ne;
  7901. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7902. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  7903. const size_t d_sz = sizeof(float) * d_ne;
  7904. float * x = (float *) malloc(x_sz);
  7905. float * y = (float *) malloc(y_sz);
  7906. void * qx = malloc(qx_sz);
  7907. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7908. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7909. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7910. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7911. float * d = (float *) malloc(d_sz);
  7912. float * d_chk = (float *) malloc(d_sz);
  7913. for (size_t i = 0; i < x_ne; i++) {
  7914. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7915. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7916. // x[i] = i % k;
  7917. }
  7918. ggml_vk_quantize_data(x, qx, x_ne, quant);
  7919. for (size_t i = 0; i < y_ne; i++) {
  7920. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7921. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7922. // y[i] = i % k;
  7923. }
  7924. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7925. if (split_k > 1) {
  7926. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7927. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7928. // Resize buffer
  7929. if (ctx->prealloc_split_k != nullptr) {
  7930. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7931. }
  7932. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7933. }
  7934. }
  7935. if (mmq) {
  7936. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  7937. }
  7938. if (ctx->device->need_compiles) {
  7939. ggml_vk_load_shaders(ctx->device);
  7940. }
  7941. ggml_pipeline_allocate_descriptor_sets(ctx);
  7942. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7943. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  7944. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7945. ggml_vk_ctx_begin(ctx->device, subctx);
  7946. if (mmq) {
  7947. for (size_t i = 0; i < num_it; i++) {
  7948. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  7949. ggml_vk_matmul(
  7950. ctx, subctx, p, { qx_buf, 0, qx_sz }, { qy_buf, 0, qy_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  7951. m, n, k,
  7952. k, k, m, k*m, k*n, m*n,
  7953. split_k, batch, batch, batch, 1, 1, n
  7954. );
  7955. }
  7956. } else {
  7957. for (size_t i = 0; i < num_it; i++) {
  7958. ggml_vk_matmul(
  7959. ctx, subctx, p, { qx_buf, 0, qx_sz }, { y_buf, 0, y_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  7960. m, n, k,
  7961. k, k, m, k*m, k*n, m*n,
  7962. split_k, batch, batch, batch, 1, 1, n
  7963. );
  7964. }
  7965. }
  7966. ggml_vk_ctx_end(subctx);
  7967. auto begin = std::chrono::high_resolution_clock::now();
  7968. ggml_vk_submit(subctx, ctx->fence);
  7969. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7970. ctx->device->device.resetFences({ ctx->fence });
  7971. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7972. auto end = std::chrono::high_resolution_clock::now();
  7973. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7974. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  7975. ggml_init_params iparams = {
  7976. /*.mem_size =*/ 1024*1024*1024,
  7977. /*.mem_buffer =*/ NULL,
  7978. /*.no_alloc =*/ true,
  7979. };
  7980. ggml_context * ggml_ctx = ggml_init(iparams);
  7981. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  7982. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  7983. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7984. src0_ggml->data = qx;
  7985. src1_ggml->data = y;
  7986. tensor_ggml->data = d_chk;
  7987. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7988. ggml_build_forward_expand(cgraph, tensor_ggml);
  7989. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7990. ggml_free(ggml_ctx);
  7991. double avg_err = 0.0;
  7992. int first_err_n = -1;
  7993. int first_err_m = -1;
  7994. int first_err_b = -1;
  7995. for (size_t i = 0; i < m*n*batch; i++) {
  7996. double err = std::fabs(d[i] - d_chk[i]);
  7997. avg_err += err;
  7998. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7999. first_err_b = i / (m * n);
  8000. first_err_n = (i % (m * n)) / m;
  8001. first_err_m = (i % (m * n)) % m;
  8002. }
  8003. }
  8004. avg_err /= m * n;
  8005. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  8006. std::cerr << "TEST dequant matmul " << shname;
  8007. if (mmq) {
  8008. std::cerr << " mmq";
  8009. }
  8010. std::cerr << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  8011. if (avg_err > 0.01 || std::isnan(avg_err)) {
  8012. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  8013. std::cerr << "Actual result: " << std::endl << std::endl;
  8014. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8015. std::cerr << std::endl;
  8016. std::cerr << "Expected result: " << std::endl << std::endl;
  8017. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8018. std::cerr << "src0: " << std::endl << std::endl;
  8019. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  8020. std::cerr << std::endl;
  8021. std::cerr << "src1: " << std::endl << std::endl;
  8022. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  8023. if (split_k > 1) {
  8024. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  8025. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  8026. std::cerr << "d_buf0: " << std::endl << std::endl;
  8027. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8028. std::cerr << "d_buf1: " << std::endl << std::endl;
  8029. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8030. std::cerr << "d_buf2: " << std::endl << std::endl;
  8031. 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);
  8032. std::cerr << "d_buf3: " << std::endl << std::endl;
  8033. 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);
  8034. free(split_k_buf);
  8035. }
  8036. }
  8037. ggml_vk_destroy_buffer(qx_buf);
  8038. ggml_vk_destroy_buffer(y_buf);
  8039. ggml_vk_destroy_buffer(qy_buf);
  8040. ggml_vk_destroy_buffer(d_buf);
  8041. free(x);
  8042. free(qx);
  8043. free(y);
  8044. free(d);
  8045. free(d_chk);
  8046. }
  8047. #endif
  8048. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  8049. #if defined(GGML_VULKAN_RUN_TESTS)
  8050. const std::vector<size_t> vals {
  8051. 512, 512, 128,
  8052. 128, 512, 512,
  8053. 4096, 512, 4096,
  8054. 11008, 512, 4096,
  8055. 4096, 512, 11008,
  8056. 32000, 512, 4096,
  8057. 8, 8, 8,
  8058. 100, 46, 576,
  8059. 623, 111, 128,
  8060. 100, 46, 558,
  8061. 512, 1, 256,
  8062. 128, 110, 622,
  8063. 511, 511, 127,
  8064. 511, 511, 7,
  8065. 511, 511, 17,
  8066. 49, 49, 128,
  8067. 128, 49, 49,
  8068. 4096, 49, 4096,
  8069. };
  8070. const size_t num_it = 100;
  8071. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  8072. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  8073. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  8074. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  8075. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  8076. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  8077. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  8078. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  8079. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  8080. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  8081. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  8082. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  8083. abort();
  8084. for (size_t i = 0; i < vals.size(); i += 3) {
  8085. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  8086. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  8087. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  8088. std::cerr << '\n';
  8089. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  8090. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  8091. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  8092. std::cerr << '\n';
  8093. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  8094. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  8095. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  8096. std::cerr << '\n' << std::endl;
  8097. if (vals[i + 2] % 32 == 0) {
  8098. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  8099. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  8100. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  8101. std::cerr << '\n';
  8102. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  8103. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  8104. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  8105. std::cerr << '\n';
  8106. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  8107. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  8108. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  8109. std::cerr << '\n' << std::endl;
  8110. }
  8111. if (vals[i + 2] % 256 == 0) {
  8112. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  8113. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  8114. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  8115. std::cerr << '\n';
  8116. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  8117. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  8118. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  8119. std::cerr << '\n';
  8120. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  8121. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  8122. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  8123. std::cerr << '\n' << std::endl;
  8124. }
  8125. }
  8126. GGML_ABORT("fatal error");
  8127. #endif
  8128. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  8129. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  8130. // Resize buffer
  8131. if (ctx->prealloc_x != nullptr) {
  8132. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8133. }
  8134. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  8135. }
  8136. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  8137. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  8138. // Resize buffer
  8139. if (ctx->prealloc_y != nullptr) {
  8140. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8141. }
  8142. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  8143. }
  8144. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  8145. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  8146. // Resize buffer
  8147. if (ctx->prealloc_split_k != nullptr) {
  8148. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8149. }
  8150. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  8151. }
  8152. }
  8153. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_cgraph * cgraph, ggml_tensor* tensor, int tensor_idx, bool use_fence, bool almost_ready);
  8154. // Returns true if node has enqueued work into the queue, false otherwise
  8155. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  8156. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
  8157. ggml_tensor * node = cgraph->nodes[node_idx];
  8158. if (ggml_is_empty(node) || !node->buffer) {
  8159. return false;
  8160. }
  8161. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  8162. ctx->semaphore_idx = 0;
  8163. const ggml_tensor * src0 = node->src[0];
  8164. const ggml_tensor * src1 = node->src[1];
  8165. const ggml_tensor * src2 = node->src[2];
  8166. const ggml_tensor * src3 = node->src[3];
  8167. switch (node->op) {
  8168. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  8169. case GGML_OP_RESHAPE:
  8170. case GGML_OP_VIEW:
  8171. case GGML_OP_PERMUTE:
  8172. case GGML_OP_TRANSPOSE:
  8173. case GGML_OP_NONE:
  8174. return false;
  8175. case GGML_OP_UNARY:
  8176. switch (ggml_get_unary_op(node)) {
  8177. case GGML_UNARY_OP_SILU:
  8178. case GGML_UNARY_OP_GELU:
  8179. case GGML_UNARY_OP_GELU_ERF:
  8180. case GGML_UNARY_OP_GELU_QUICK:
  8181. case GGML_UNARY_OP_RELU:
  8182. case GGML_UNARY_OP_TANH:
  8183. case GGML_UNARY_OP_SIGMOID:
  8184. break;
  8185. default:
  8186. return false;
  8187. }
  8188. break;
  8189. case GGML_OP_GLU:
  8190. switch (ggml_get_glu_op(node)) {
  8191. case GGML_GLU_OP_GEGLU:
  8192. case GGML_GLU_OP_REGLU:
  8193. case GGML_GLU_OP_SWIGLU:
  8194. case GGML_GLU_OP_SWIGLU_OAI:
  8195. case GGML_GLU_OP_GEGLU_ERF:
  8196. case GGML_GLU_OP_GEGLU_QUICK:
  8197. break;
  8198. default:
  8199. return false;
  8200. }
  8201. break;
  8202. case GGML_OP_REPEAT:
  8203. case GGML_OP_REPEAT_BACK:
  8204. case GGML_OP_GET_ROWS:
  8205. case GGML_OP_ADD:
  8206. case GGML_OP_ADD_ID:
  8207. case GGML_OP_ACC:
  8208. case GGML_OP_SUB:
  8209. case GGML_OP_MUL:
  8210. case GGML_OP_DIV:
  8211. case GGML_OP_CONCAT:
  8212. case GGML_OP_UPSCALE:
  8213. case GGML_OP_SCALE:
  8214. case GGML_OP_SQR:
  8215. case GGML_OP_SIN:
  8216. case GGML_OP_COS:
  8217. case GGML_OP_CLAMP:
  8218. case GGML_OP_PAD:
  8219. case GGML_OP_ROLL:
  8220. case GGML_OP_CPY:
  8221. case GGML_OP_SET_ROWS:
  8222. case GGML_OP_CONT:
  8223. case GGML_OP_DUP:
  8224. case GGML_OP_SILU_BACK:
  8225. case GGML_OP_NORM:
  8226. case GGML_OP_GROUP_NORM:
  8227. case GGML_OP_RMS_NORM:
  8228. case GGML_OP_RMS_NORM_BACK:
  8229. case GGML_OP_L2_NORM:
  8230. case GGML_OP_DIAG_MASK_INF:
  8231. case GGML_OP_SOFT_MAX:
  8232. case GGML_OP_SOFT_MAX_BACK:
  8233. case GGML_OP_ROPE:
  8234. case GGML_OP_ROPE_BACK:
  8235. case GGML_OP_MUL_MAT:
  8236. case GGML_OP_MUL_MAT_ID:
  8237. case GGML_OP_ARGSORT:
  8238. case GGML_OP_SUM:
  8239. case GGML_OP_SUM_ROWS:
  8240. case GGML_OP_ARGMAX:
  8241. case GGML_OP_COUNT_EQUAL:
  8242. case GGML_OP_IM2COL:
  8243. case GGML_OP_TIMESTEP_EMBEDDING:
  8244. case GGML_OP_CONV_TRANSPOSE_1D:
  8245. case GGML_OP_POOL_2D:
  8246. case GGML_OP_CONV_2D:
  8247. case GGML_OP_CONV_2D_DW:
  8248. case GGML_OP_RWKV_WKV6:
  8249. case GGML_OP_RWKV_WKV7:
  8250. case GGML_OP_LEAKY_RELU:
  8251. case GGML_OP_FLASH_ATTN_EXT:
  8252. case GGML_OP_OPT_STEP_ADAMW:
  8253. break;
  8254. default:
  8255. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  8256. GGML_ABORT("fatal error");
  8257. return false;
  8258. }
  8259. vk_context compute_ctx;
  8260. if (!dryrun) {
  8261. if (ctx->compute_ctx.expired()) {
  8262. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8263. ctx->compute_ctx = compute_ctx;
  8264. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8265. } else {
  8266. compute_ctx = ctx->compute_ctx.lock();
  8267. }
  8268. } else {
  8269. switch (node->op) {
  8270. case GGML_OP_REPEAT:
  8271. case GGML_OP_REPEAT_BACK:
  8272. case GGML_OP_ACC:
  8273. case GGML_OP_GET_ROWS:
  8274. case GGML_OP_ADD:
  8275. case GGML_OP_SUB:
  8276. case GGML_OP_MUL:
  8277. case GGML_OP_DIV:
  8278. case GGML_OP_CONCAT:
  8279. case GGML_OP_UPSCALE:
  8280. case GGML_OP_SCALE:
  8281. case GGML_OP_SQR:
  8282. case GGML_OP_SIN:
  8283. case GGML_OP_COS:
  8284. case GGML_OP_CLAMP:
  8285. case GGML_OP_PAD:
  8286. case GGML_OP_CPY:
  8287. case GGML_OP_SET_ROWS:
  8288. case GGML_OP_CONT:
  8289. case GGML_OP_DUP:
  8290. case GGML_OP_SILU_BACK:
  8291. case GGML_OP_NORM:
  8292. case GGML_OP_GROUP_NORM:
  8293. case GGML_OP_RMS_NORM:
  8294. case GGML_OP_RMS_NORM_BACK:
  8295. case GGML_OP_L2_NORM:
  8296. case GGML_OP_UNARY:
  8297. case GGML_OP_GLU:
  8298. case GGML_OP_DIAG_MASK_INF:
  8299. case GGML_OP_SOFT_MAX:
  8300. case GGML_OP_SOFT_MAX_BACK:
  8301. case GGML_OP_ROPE:
  8302. case GGML_OP_ROPE_BACK:
  8303. case GGML_OP_ARGSORT:
  8304. case GGML_OP_SUM:
  8305. case GGML_OP_SUM_ROWS:
  8306. case GGML_OP_ARGMAX:
  8307. case GGML_OP_COUNT_EQUAL:
  8308. case GGML_OP_IM2COL:
  8309. case GGML_OP_TIMESTEP_EMBEDDING:
  8310. case GGML_OP_CONV_TRANSPOSE_1D:
  8311. case GGML_OP_POOL_2D:
  8312. case GGML_OP_CONV_2D:
  8313. case GGML_OP_CONV_2D_DW:
  8314. case GGML_OP_LEAKY_RELU:
  8315. {
  8316. // These operations all go through ggml_vk_op_f32, so short-circuit and
  8317. // do the only thing needed for the dryrun.
  8318. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  8319. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8320. return false;
  8321. }
  8322. default:
  8323. break;
  8324. }
  8325. }
  8326. switch (node->op) {
  8327. case GGML_OP_REPEAT:
  8328. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  8329. break;
  8330. case GGML_OP_REPEAT_BACK:
  8331. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  8332. break;
  8333. case GGML_OP_ACC:
  8334. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  8335. break;
  8336. case GGML_OP_GET_ROWS:
  8337. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  8338. break;
  8339. case GGML_OP_ADD:
  8340. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  8341. break;
  8342. case GGML_OP_SUB:
  8343. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  8344. break;
  8345. case GGML_OP_MUL:
  8346. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  8347. break;
  8348. case GGML_OP_DIV:
  8349. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  8350. break;
  8351. case GGML_OP_ADD_ID:
  8352. ggml_vk_add_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8353. break;
  8354. case GGML_OP_CONCAT:
  8355. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  8356. break;
  8357. case GGML_OP_UPSCALE:
  8358. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  8359. break;
  8360. case GGML_OP_SCALE:
  8361. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  8362. break;
  8363. case GGML_OP_SQR:
  8364. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  8365. break;
  8366. case GGML_OP_SIN:
  8367. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  8368. break;
  8369. case GGML_OP_COS:
  8370. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  8371. break;
  8372. case GGML_OP_CLAMP:
  8373. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  8374. break;
  8375. case GGML_OP_PAD:
  8376. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  8377. break;
  8378. case GGML_OP_ROLL:
  8379. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  8380. break;
  8381. case GGML_OP_CPY:
  8382. case GGML_OP_CONT:
  8383. case GGML_OP_DUP:
  8384. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  8385. break;
  8386. case GGML_OP_SET_ROWS:
  8387. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  8388. break;
  8389. case GGML_OP_SILU_BACK:
  8390. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  8391. break;
  8392. case GGML_OP_NORM:
  8393. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  8394. break;
  8395. case GGML_OP_GROUP_NORM:
  8396. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  8397. break;
  8398. case GGML_OP_RMS_NORM:
  8399. if (ctx->num_additional_fused_ops > 0) {
  8400. // fused rms_norm + mul
  8401. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  8402. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  8403. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  8404. } else {
  8405. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  8406. }
  8407. break;
  8408. case GGML_OP_RMS_NORM_BACK:
  8409. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  8410. break;
  8411. case GGML_OP_L2_NORM:
  8412. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  8413. break;
  8414. case GGML_OP_UNARY:
  8415. switch (ggml_get_unary_op(node)) {
  8416. case GGML_UNARY_OP_SILU:
  8417. case GGML_UNARY_OP_GELU:
  8418. case GGML_UNARY_OP_GELU_ERF:
  8419. case GGML_UNARY_OP_GELU_QUICK:
  8420. case GGML_UNARY_OP_RELU:
  8421. case GGML_UNARY_OP_TANH:
  8422. case GGML_UNARY_OP_SIGMOID:
  8423. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  8424. break;
  8425. default:
  8426. return false;
  8427. }
  8428. break;
  8429. case GGML_OP_GLU:
  8430. switch (ggml_get_glu_op(node)) {
  8431. case GGML_GLU_OP_GEGLU:
  8432. case GGML_GLU_OP_REGLU:
  8433. case GGML_GLU_OP_SWIGLU:
  8434. case GGML_GLU_OP_SWIGLU_OAI:
  8435. case GGML_GLU_OP_GEGLU_ERF:
  8436. case GGML_GLU_OP_GEGLU_QUICK:
  8437. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  8438. break;
  8439. default:
  8440. return false;
  8441. }
  8442. break;
  8443. case GGML_OP_DIAG_MASK_INF:
  8444. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  8445. break;
  8446. case GGML_OP_SOFT_MAX:
  8447. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8448. break;
  8449. case GGML_OP_SOFT_MAX_BACK:
  8450. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  8451. break;
  8452. case GGML_OP_ROPE:
  8453. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  8454. break;
  8455. case GGML_OP_ROPE_BACK:
  8456. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  8457. break;
  8458. case GGML_OP_ARGSORT:
  8459. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  8460. break;
  8461. case GGML_OP_SUM:
  8462. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  8463. break;
  8464. case GGML_OP_SUM_ROWS:
  8465. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  8466. break;
  8467. case GGML_OP_ARGMAX:
  8468. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  8469. break;
  8470. case GGML_OP_COUNT_EQUAL:
  8471. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  8472. break;
  8473. case GGML_OP_IM2COL:
  8474. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  8475. break;
  8476. case GGML_OP_TIMESTEP_EMBEDDING:
  8477. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  8478. break;
  8479. case GGML_OP_CONV_TRANSPOSE_1D:
  8480. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  8481. break;
  8482. case GGML_OP_POOL_2D:
  8483. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  8484. break;
  8485. case GGML_OP_CONV_2D:
  8486. ggml_vk_conv_2d(ctx, compute_ctx, src0, src1, node, dryrun);
  8487. break;
  8488. case GGML_OP_CONV_2D_DW:
  8489. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  8490. break;
  8491. case GGML_OP_LEAKY_RELU:
  8492. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  8493. break;
  8494. case GGML_OP_MUL_MAT:
  8495. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  8496. break;
  8497. case GGML_OP_MUL_MAT_ID:
  8498. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8499. break;
  8500. case GGML_OP_FLASH_ATTN_EXT:
  8501. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node->src[4], node, dryrun);
  8502. break;
  8503. case GGML_OP_RWKV_WKV6:
  8504. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  8505. break;
  8506. case GGML_OP_RWKV_WKV7:
  8507. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  8508. break;
  8509. case GGML_OP_OPT_STEP_ADAMW:
  8510. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  8511. break;
  8512. default:
  8513. return false;
  8514. }
  8515. if (dryrun) {
  8516. return false;
  8517. }
  8518. ctx->tensor_ctxs[node_idx] = compute_ctx;
  8519. #if defined(GGML_VULKAN_CHECK_RESULTS)
  8520. // Force context reset on each node so that each tensor ends up in its own context
  8521. // and can be run and compared to its CPU equivalent separately
  8522. last_node = true;
  8523. #endif
  8524. if (submit || last_node) {
  8525. ggml_vk_ctx_end(compute_ctx);
  8526. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  8527. if (last_node) {
  8528. compute_ctx->exit_tensor_idx = node_idx_begin;
  8529. }
  8530. else {
  8531. compute_ctx->exit_tensor_idx = -1;
  8532. }
  8533. ctx->compute_ctx.reset();
  8534. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  8535. if (!ok) {
  8536. if (node->op == GGML_OP_UNARY) {
  8537. 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;
  8538. } else if (node->op == GGML_OP_GLU) {
  8539. std::cerr << __func__ << ": error: op not supported GLU " << node->name << " (" << ggml_glu_op_name(static_cast<ggml_glu_op>(node->op_params[0])) << ")" << std::endl;
  8540. } else {
  8541. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  8542. }
  8543. }
  8544. }
  8545. return true;
  8546. }
  8547. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, ggml_tensor * tensor, int tensor_idx, bool use_fence = true, bool almost_ready = false) {
  8548. GGML_UNUSED(cgraph);
  8549. ggml_backend_buffer * buf = nullptr;
  8550. switch (tensor->op) {
  8551. case GGML_OP_ADD:
  8552. case GGML_OP_ACC:
  8553. case GGML_OP_GET_ROWS:
  8554. case GGML_OP_SUB:
  8555. case GGML_OP_MUL:
  8556. case GGML_OP_DIV:
  8557. case GGML_OP_ADD_ID:
  8558. case GGML_OP_CONCAT:
  8559. case GGML_OP_UPSCALE:
  8560. case GGML_OP_SCALE:
  8561. case GGML_OP_SQR:
  8562. case GGML_OP_SIN:
  8563. case GGML_OP_COS:
  8564. case GGML_OP_CLAMP:
  8565. case GGML_OP_PAD:
  8566. case GGML_OP_ROLL:
  8567. case GGML_OP_CPY:
  8568. case GGML_OP_SET_ROWS:
  8569. case GGML_OP_CONT:
  8570. case GGML_OP_DUP:
  8571. case GGML_OP_SILU_BACK:
  8572. case GGML_OP_NORM:
  8573. case GGML_OP_GROUP_NORM:
  8574. case GGML_OP_RMS_NORM:
  8575. case GGML_OP_RMS_NORM_BACK:
  8576. case GGML_OP_L2_NORM:
  8577. case GGML_OP_DIAG_MASK_INF:
  8578. case GGML_OP_SOFT_MAX:
  8579. case GGML_OP_SOFT_MAX_BACK:
  8580. case GGML_OP_ROPE:
  8581. case GGML_OP_ROPE_BACK:
  8582. case GGML_OP_RESHAPE:
  8583. case GGML_OP_VIEW:
  8584. case GGML_OP_PERMUTE:
  8585. case GGML_OP_TRANSPOSE:
  8586. case GGML_OP_NONE:
  8587. case GGML_OP_ARGSORT:
  8588. case GGML_OP_SUM:
  8589. case GGML_OP_SUM_ROWS:
  8590. case GGML_OP_ARGMAX:
  8591. case GGML_OP_COUNT_EQUAL:
  8592. case GGML_OP_IM2COL:
  8593. case GGML_OP_TIMESTEP_EMBEDDING:
  8594. case GGML_OP_CONV_TRANSPOSE_1D:
  8595. case GGML_OP_POOL_2D:
  8596. case GGML_OP_CONV_2D:
  8597. case GGML_OP_CONV_2D_DW:
  8598. case GGML_OP_RWKV_WKV6:
  8599. case GGML_OP_RWKV_WKV7:
  8600. case GGML_OP_LEAKY_RELU:
  8601. case GGML_OP_REPEAT:
  8602. case GGML_OP_REPEAT_BACK:
  8603. case GGML_OP_OPT_STEP_ADAMW:
  8604. buf = tensor->buffer;
  8605. break;
  8606. case GGML_OP_UNARY:
  8607. switch (ggml_get_unary_op(tensor)) {
  8608. case GGML_UNARY_OP_SILU:
  8609. case GGML_UNARY_OP_GELU:
  8610. case GGML_UNARY_OP_GELU_ERF:
  8611. case GGML_UNARY_OP_GELU_QUICK:
  8612. case GGML_UNARY_OP_RELU:
  8613. case GGML_UNARY_OP_TANH:
  8614. case GGML_UNARY_OP_SIGMOID:
  8615. buf = tensor->buffer;
  8616. break;
  8617. default:
  8618. return false;
  8619. }
  8620. break;
  8621. case GGML_OP_GLU:
  8622. switch (ggml_get_glu_op(tensor)) {
  8623. case GGML_GLU_OP_GEGLU:
  8624. case GGML_GLU_OP_REGLU:
  8625. case GGML_GLU_OP_SWIGLU:
  8626. case GGML_GLU_OP_SWIGLU_OAI:
  8627. case GGML_GLU_OP_GEGLU_ERF:
  8628. case GGML_GLU_OP_GEGLU_QUICK:
  8629. buf = tensor->buffer;
  8630. break;
  8631. default:
  8632. return false;
  8633. }
  8634. break;
  8635. case GGML_OP_MUL_MAT:
  8636. case GGML_OP_MUL_MAT_ID:
  8637. case GGML_OP_FLASH_ATTN_EXT:
  8638. buf = tensor->buffer;
  8639. break;
  8640. default:
  8641. return false;
  8642. }
  8643. if (buf == nullptr) {
  8644. return false;
  8645. }
  8646. 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 << ")");
  8647. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  8648. // always wait for the GPU work to be done for the last submit
  8649. if (tensor_idx == subctx->exit_tensor_idx) {
  8650. use_fence = true;
  8651. }
  8652. // Only run if ctx hasn't been submitted yet
  8653. if (!subctx->seqs.empty()) {
  8654. #ifdef GGML_VULKAN_CHECK_RESULTS
  8655. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  8656. use_fence = true;
  8657. #endif
  8658. // Do staging buffer copies
  8659. for (auto& cpy : subctx->in_memcpys) {
  8660. memcpy(cpy.dst, cpy.src, cpy.n);
  8661. }
  8662. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  8663. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  8664. ctx->almost_ready_fence_pending = true;
  8665. } else {
  8666. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  8667. }
  8668. if (use_fence) {
  8669. ggml_vk_wait_for_fence(ctx);
  8670. }
  8671. #ifdef GGML_VULKAN_CHECK_RESULTS
  8672. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  8673. #endif
  8674. }
  8675. if (tensor_idx == subctx->exit_tensor_idx) {
  8676. // Do staging buffer copies
  8677. for (auto& cpy : subctx->out_memcpys) {
  8678. memcpy(cpy.dst, cpy.src, cpy.n);
  8679. }
  8680. subctx->in_memcpys.clear();
  8681. subctx->out_memcpys.clear();
  8682. }
  8683. return true;
  8684. }
  8685. // Clean up after graph processing is done
  8686. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  8687. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  8688. for (auto& buffer : ctx->gc.temp_buffers) {
  8689. ggml_vk_pool_free(ctx, buffer);
  8690. }
  8691. ctx->gc.temp_buffers.clear();
  8692. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8693. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8694. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  8695. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  8696. }
  8697. ctx->gc.semaphores.clear();
  8698. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  8699. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  8700. }
  8701. ctx->gc.tl_semaphores.clear();
  8702. ctx->semaphore_idx = 0;
  8703. ctx->event_idx = 0;
  8704. for (auto& event : ctx->gc.events) {
  8705. ctx->device->device.resetEvent(event);
  8706. }
  8707. ctx->tensor_ctxs.clear();
  8708. ctx->gc.contexts.clear();
  8709. ctx->pipeline_descriptor_set_requirements = 0;
  8710. ctx->descriptor_set_idx = 0;
  8711. }
  8712. // Clean up on backend free
  8713. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  8714. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  8715. ggml_vk_graph_cleanup(ctx);
  8716. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8717. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8718. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8719. for (auto& buffer : ctx->buffer_pool) {
  8720. ggml_vk_destroy_buffer(buffer);
  8721. }
  8722. ctx->prealloc_size_x = 0;
  8723. ctx->prealloc_size_y = 0;
  8724. ctx->prealloc_size_split_k = 0;
  8725. for (auto& event : ctx->gc.events) {
  8726. ctx->device->device.destroyEvent(event);
  8727. }
  8728. ctx->gc.events.clear();
  8729. ctx->device->device.destroyFence(ctx->fence);
  8730. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  8731. for (auto& pool : ctx->descriptor_pools) {
  8732. ctx->device->device.destroyDescriptorPool(pool);
  8733. }
  8734. ctx->descriptor_pools.clear();
  8735. ctx->descriptor_sets.clear();
  8736. ctx->compute_cmd_pool.destroy(ctx->device->device);
  8737. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  8738. }
  8739. static int ggml_vk_get_device_count() {
  8740. ggml_vk_instance_init();
  8741. return vk_instance.device_indices.size();
  8742. }
  8743. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  8744. ggml_vk_instance_init();
  8745. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  8746. vk::PhysicalDeviceProperties props;
  8747. devices[device].getProperties(&props);
  8748. snprintf(description, description_size, "%s", props.deviceName.data());
  8749. }
  8750. // backend interface
  8751. #define UNUSED GGML_UNUSED
  8752. // device backend
  8753. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  8754. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  8755. }
  8756. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8757. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  8758. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8759. ggml_vk_destroy_buffer(ctx->dev_buffer);
  8760. delete ctx;
  8761. }
  8762. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  8763. return vk_ptr_base;
  8764. UNUSED(buffer);
  8765. }
  8766. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8767. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  8768. if (tensor->view_src != nullptr) {
  8769. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  8770. }
  8771. return GGML_STATUS_SUCCESS;
  8772. }
  8773. static void ggml_backend_vk_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
  8774. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  8775. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8776. vk_buffer buf = buf_ctx->dev_buffer;
  8777. uint32_t val32 = (uint32_t)value * 0x01010101;
  8778. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  8779. }
  8780. 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) {
  8781. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8782. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8783. vk_buffer buf = buf_ctx->dev_buffer;
  8784. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8785. }
  8786. 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) {
  8787. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8788. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8789. vk_buffer buf = buf_ctx->dev_buffer;
  8790. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8791. }
  8792. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8793. if (ggml_backend_buffer_is_vk(src->buffer)) {
  8794. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8795. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8796. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8797. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8798. ggml_vk_buffer_copy(dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  8799. return true;
  8800. }
  8801. return false;
  8802. UNUSED(buffer);
  8803. }
  8804. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8805. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8806. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  8807. }
  8808. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  8809. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  8810. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  8811. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  8812. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  8813. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  8814. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  8815. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  8816. /* .clear = */ ggml_backend_vk_buffer_clear,
  8817. /* .reset = */ NULL,
  8818. };
  8819. // vk buffer type
  8820. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8821. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8822. return ctx->name.c_str();
  8823. }
  8824. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8825. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  8826. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8827. vk_buffer dev_buffer = nullptr;
  8828. try {
  8829. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  8830. } catch (const vk::SystemError& e) {
  8831. return nullptr;
  8832. }
  8833. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  8834. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  8835. }
  8836. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8837. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8838. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  8839. }
  8840. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8841. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8842. return ctx->device->suballocation_block_size;
  8843. }
  8844. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8845. return ggml_nbytes(tensor);
  8846. UNUSED(buft);
  8847. }
  8848. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  8849. ggml_vk_instance_init();
  8850. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  8851. vk_device dev = ggml_vk_get_device(dev_num);
  8852. return &dev->buffer_type;
  8853. }
  8854. // host buffer type
  8855. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8856. return GGML_VK_NAME "_Host";
  8857. UNUSED(buft);
  8858. }
  8859. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  8860. return GGML_VK_NAME "_Host";
  8861. UNUSED(buffer);
  8862. }
  8863. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8864. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  8865. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  8866. }
  8867. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8868. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  8869. size += 32; // Behave like the CPU buffer type
  8870. void * ptr = nullptr;
  8871. try {
  8872. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  8873. } catch (vk::SystemError& e) {
  8874. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  8875. // fallback to cpu buffer
  8876. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8877. }
  8878. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8879. buffer->buft = buft;
  8880. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  8881. return buffer;
  8882. UNUSED(buft);
  8883. }
  8884. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8885. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  8886. UNUSED(buft);
  8887. }
  8888. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8889. return vk_instance.devices[0]->suballocation_block_size;
  8890. UNUSED(buft);
  8891. }
  8892. // Should be changed to return device-specific host buffer type
  8893. // but that probably requires changes in llama.cpp
  8894. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  8895. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  8896. /* .iface = */ {
  8897. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  8898. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  8899. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  8900. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  8901. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8902. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8903. },
  8904. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  8905. /* .context = */ nullptr,
  8906. };
  8907. // Make sure device 0 is initialized
  8908. ggml_vk_instance_init();
  8909. ggml_vk_get_device(0);
  8910. return &ggml_backend_vk_buffer_type_host;
  8911. }
  8912. // backend
  8913. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  8914. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8915. return ctx->name.c_str();
  8916. }
  8917. static void ggml_backend_vk_free(ggml_backend_t backend) {
  8918. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8919. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  8920. ggml_vk_cleanup(ctx);
  8921. delete ctx;
  8922. delete backend;
  8923. }
  8924. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  8925. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8926. return &ctx->device->buffer_type;
  8927. }
  8928. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8929. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  8930. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8931. 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");
  8932. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8933. vk_context transfer_ctx;
  8934. if (ctx->transfer_ctx.expired()) {
  8935. // Initialize new transfer context
  8936. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8937. ctx->transfer_ctx = transfer_ctx;
  8938. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8939. } else {
  8940. transfer_ctx = ctx->transfer_ctx.lock();
  8941. }
  8942. vk_buffer buf = buf_ctx->dev_buffer;
  8943. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8944. }
  8945. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8946. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  8947. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8948. 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");
  8949. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8950. vk_context transfer_ctx;
  8951. if (ctx->transfer_ctx.expired()) {
  8952. // Initialize new transfer context
  8953. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8954. ctx->transfer_ctx = transfer_ctx;
  8955. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8956. } else {
  8957. transfer_ctx = ctx->transfer_ctx.lock();
  8958. }
  8959. vk_buffer buf = buf_ctx->dev_buffer;
  8960. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8961. }
  8962. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  8963. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  8964. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8965. 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)) {
  8966. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8967. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8968. vk_context transfer_ctx;
  8969. if (ctx->transfer_ctx.expired()) {
  8970. // Initialize new transfer context
  8971. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8972. ctx->transfer_ctx = transfer_ctx;
  8973. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8974. } else {
  8975. transfer_ctx = ctx->transfer_ctx.lock();
  8976. }
  8977. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8978. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8979. ggml_vk_buffer_copy_async(transfer_ctx, dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  8980. return true;
  8981. }
  8982. return false;
  8983. }
  8984. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  8985. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  8986. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8987. if(ctx->transfer_ctx.expired()) {
  8988. return;
  8989. }
  8990. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  8991. ggml_vk_ctx_end(transfer_ctx);
  8992. for (auto& cpy : transfer_ctx->in_memcpys) {
  8993. memcpy(cpy.dst, cpy.src, cpy.n);
  8994. }
  8995. ggml_vk_submit(transfer_ctx, ctx->fence);
  8996. ggml_vk_wait_for_fence(ctx);
  8997. for (auto& cpy : transfer_ctx->out_memcpys) {
  8998. memcpy(cpy.dst, cpy.src, cpy.n);
  8999. }
  9000. ctx->transfer_ctx.reset();
  9001. }
  9002. static bool ggml_vk_is_empty(ggml_tensor * node) {
  9003. 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;
  9004. }
  9005. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  9006. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  9007. return false;
  9008. }
  9009. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  9010. // additional constraints specific to this fusion
  9011. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  9012. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  9013. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  9014. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  9015. // rms_norm only supports f32
  9016. if (mul->src[0]->type != GGML_TYPE_F32 ||
  9017. mul->src[1]->type != GGML_TYPE_F32 ||
  9018. mul->type != GGML_TYPE_F32) {
  9019. return false;
  9020. }
  9021. // if rms_norm is the B operand, then we don't handle broadcast
  9022. if (rms_norm == mul->src[1] &&
  9023. !ggml_are_same_shape(mul->src[0], rms_norm)) {
  9024. return false;
  9025. }
  9026. // rms_norm shader assumes contiguous rows
  9027. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  9028. return false;
  9029. }
  9030. }
  9031. return true;
  9032. }
  9033. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9034. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  9035. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  9036. if (vk_instance.debug_utils_support) {
  9037. vk::DebugUtilsLabelEXT dul = {};
  9038. dul.pLabelName = "ggml_backend_vk_graph_compute";
  9039. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  9040. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  9041. }
  9042. uint64_t total_mat_mul_bytes = 0;
  9043. for (int i = 0; i < cgraph->n_nodes; i++) {
  9044. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  9045. ctx->num_additional_fused_ops = 1;
  9046. }
  9047. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  9048. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  9049. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  9050. } else if (cgraph->nodes[i]->op == GGML_OP_CONV_2D) {
  9051. // Return CRSxNPQxsizeof(*) to account as many bytes as mul_mat has in im2col->mul_mat mode.
  9052. auto CRS_size =
  9053. cgraph->nodes[i]->src[0]->ne[0] * cgraph->nodes[i]->src[0]->ne[1] * cgraph->nodes[i]->src[0]->ne[2];
  9054. auto NPQ_size = cgraph->nodes[i]->ne[0] * cgraph->nodes[i]->ne[1] * cgraph->nodes[i]->ne[3];
  9055. total_mat_mul_bytes += NPQ_size * CRS_size * ggml_type_size(cgraph->nodes[i]->type);
  9056. }
  9057. i += ctx->num_additional_fused_ops;
  9058. ctx->num_additional_fused_ops = 0;
  9059. }
  9060. if (ctx->device->need_compiles) {
  9061. ggml_vk_load_shaders(ctx->device);
  9062. }
  9063. ggml_vk_preallocate_buffers(ctx);
  9064. ggml_pipeline_allocate_descriptor_sets(ctx);
  9065. int last_node = cgraph->n_nodes - 1;
  9066. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  9067. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  9068. last_node -= 1;
  9069. }
  9070. // Reserve tensor context space for all nodes
  9071. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  9072. bool first_node_in_batch = true; // true if next node will be first node in a batch
  9073. int submit_node_idx = 0; // index to first node in a batch
  9074. vk_context compute_ctx;
  9075. if (vk_perf_logger_enabled) {
  9076. // allocate/resize the query pool
  9077. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  9078. if (ctx->device->query_pool) {
  9079. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  9080. }
  9081. vk::QueryPoolCreateInfo query_create_info;
  9082. query_create_info.queryType = vk::QueryType::eTimestamp;
  9083. query_create_info.queryCount = cgraph->n_nodes + 100;
  9084. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  9085. ctx->device->num_queries = query_create_info.queryCount;
  9086. }
  9087. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  9088. GGML_ASSERT(ctx->compute_ctx.expired());
  9089. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9090. ctx->compute_ctx = compute_ctx;
  9091. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9092. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  9093. }
  9094. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  9095. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  9096. // (and scaled down based on model size, so smaller models submit earlier).
  9097. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  9098. int nodes_per_submit = 100;
  9099. int submitted_nodes = 0;
  9100. int submit_count = 0;
  9101. uint64_t mul_mat_bytes = 0;
  9102. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  9103. for (int i = 0; i < cgraph->n_nodes; i++) {
  9104. if (first_node_in_batch) {
  9105. submit_node_idx = i;
  9106. }
  9107. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  9108. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  9109. }
  9110. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  9111. ctx->num_additional_fused_ops = 1;
  9112. }
  9113. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  9114. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  9115. bool submit = (submitted_nodes >= nodes_per_submit) ||
  9116. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  9117. (i + ctx->num_additional_fused_ops == last_node) ||
  9118. (almost_ready && !ctx->almost_ready_fence_pending);
  9119. bool enqueued = ggml_vk_build_graph(ctx, cgraph, i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i + ctx->num_additional_fused_ops == last_node, almost_ready, submit);
  9120. if (vk_perf_logger_enabled) {
  9121. if (ctx->compute_ctx.expired()) {
  9122. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9123. ctx->compute_ctx = compute_ctx;
  9124. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9125. } else {
  9126. compute_ctx = ctx->compute_ctx.lock();
  9127. }
  9128. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  9129. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  9130. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  9131. }
  9132. }
  9133. if (enqueued) {
  9134. ++submitted_nodes;
  9135. #ifndef GGML_VULKAN_CHECK_RESULTS
  9136. if (first_node_in_batch) {
  9137. first_node_in_batch = false;
  9138. }
  9139. #endif
  9140. }
  9141. if (submit && enqueued) {
  9142. first_node_in_batch = true;
  9143. submitted_nodes = 0;
  9144. mul_mat_bytes = 0;
  9145. if (submit_count < 3) {
  9146. mul_mat_bytes_per_submit *= 2;
  9147. }
  9148. submit_count++;
  9149. }
  9150. i += ctx->num_additional_fused_ops;
  9151. ctx->num_additional_fused_ops = 0;
  9152. }
  9153. if (vk_perf_logger_enabled) {
  9154. // End the command buffer and submit/wait
  9155. GGML_ASSERT(!ctx->compute_ctx.expired());
  9156. compute_ctx = ctx->compute_ctx.lock();
  9157. ggml_vk_ctx_end(compute_ctx);
  9158. ggml_vk_submit(compute_ctx, ctx->device->fence);
  9159. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  9160. ctx->device->device.resetFences({ ctx->device->fence });
  9161. // Get the results and pass them to the logger
  9162. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  9163. VK_CHECK(ctx->device->device.getQueryPoolResults(ctx->device->query_pool, 0, cgraph->n_nodes + 1, (cgraph->n_nodes + 1)*sizeof(uint64_t), timestamps.data(), sizeof(uint64_t), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait), "get timestamp results");
  9164. for (int i = 0; i < cgraph->n_nodes; i++) {
  9165. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  9166. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  9167. }
  9168. }
  9169. ctx->device->perf_logger->print_timings();
  9170. }
  9171. ggml_vk_graph_cleanup(ctx);
  9172. return GGML_STATUS_SUCCESS;
  9173. UNUSED(backend);
  9174. }
  9175. // TODO: enable async and synchronize
  9176. static ggml_backend_i ggml_backend_vk_interface = {
  9177. /* .get_name = */ ggml_backend_vk_name,
  9178. /* .free = */ ggml_backend_vk_free,
  9179. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  9180. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  9181. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  9182. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  9183. /* .graph_plan_create = */ NULL,
  9184. /* .graph_plan_free = */ NULL,
  9185. /* .graph_plan_update = */ NULL,
  9186. /* .graph_plan_compute = */ NULL,
  9187. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  9188. /* .event_record = */ NULL,
  9189. /* .event_wait = */ NULL,
  9190. };
  9191. static ggml_guid_t ggml_backend_vk_guid() {
  9192. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  9193. return &guid;
  9194. }
  9195. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  9196. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  9197. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  9198. ggml_vk_init(ctx, dev_num);
  9199. ggml_backend_t vk_backend = new ggml_backend {
  9200. /* .guid = */ ggml_backend_vk_guid(),
  9201. /* .iface = */ ggml_backend_vk_interface,
  9202. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  9203. /* .context = */ ctx,
  9204. };
  9205. return vk_backend;
  9206. }
  9207. bool ggml_backend_is_vk(ggml_backend_t backend) {
  9208. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  9209. }
  9210. int ggml_backend_vk_get_device_count() {
  9211. return ggml_vk_get_device_count();
  9212. }
  9213. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  9214. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  9215. int dev_idx = vk_instance.device_indices[device];
  9216. ggml_vk_get_device_description(dev_idx, description, description_size);
  9217. }
  9218. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  9219. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  9220. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  9221. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  9222. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  9223. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  9224. *total = heap.size;
  9225. *free = heap.size;
  9226. break;
  9227. }
  9228. }
  9229. }
  9230. //////////////////////////
  9231. struct ggml_backend_vk_device_context {
  9232. size_t device;
  9233. std::string name;
  9234. std::string description;
  9235. };
  9236. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  9237. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9238. return ctx->name.c_str();
  9239. }
  9240. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  9241. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9242. return ctx->description.c_str();
  9243. }
  9244. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  9245. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  9246. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  9247. }
  9248. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  9249. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9250. return ggml_backend_vk_buffer_type(ctx->device);
  9251. }
  9252. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  9253. UNUSED(dev);
  9254. return ggml_backend_vk_host_buffer_type();
  9255. }
  9256. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  9257. UNUSED(dev);
  9258. return GGML_BACKEND_DEVICE_TYPE_GPU;
  9259. }
  9260. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  9261. props->name = ggml_backend_vk_device_get_name(dev);
  9262. props->description = ggml_backend_vk_device_get_description(dev);
  9263. props->type = ggml_backend_vk_device_get_type(dev);
  9264. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  9265. props->caps = {
  9266. /* .async = */ false,
  9267. /* .host_buffer = */ true,
  9268. /* .buffer_from_host_ptr = */ false,
  9269. /* .events = */ false,
  9270. };
  9271. }
  9272. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  9273. UNUSED(params);
  9274. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9275. return ggml_backend_vk_init(ctx->device);
  9276. }
  9277. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9278. switch (op->op) {
  9279. case GGML_OP_UNARY:
  9280. switch (ggml_get_unary_op(op)) {
  9281. case GGML_UNARY_OP_GELU:
  9282. case GGML_UNARY_OP_GELU_ERF:
  9283. case GGML_UNARY_OP_GELU_QUICK:
  9284. case GGML_UNARY_OP_SILU:
  9285. case GGML_UNARY_OP_RELU:
  9286. case GGML_UNARY_OP_TANH:
  9287. case GGML_UNARY_OP_SIGMOID:
  9288. return ggml_is_contiguous(op->src[0]) &&
  9289. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9290. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  9291. (op->src[0]->type == op->type);
  9292. default:
  9293. return false;
  9294. }
  9295. break;
  9296. case GGML_OP_GLU:
  9297. switch (ggml_get_glu_op(op)) {
  9298. case GGML_GLU_OP_GEGLU:
  9299. case GGML_GLU_OP_REGLU:
  9300. case GGML_GLU_OP_SWIGLU:
  9301. case GGML_GLU_OP_SWIGLU_OAI:
  9302. case GGML_GLU_OP_GEGLU_ERF:
  9303. case GGML_GLU_OP_GEGLU_QUICK:
  9304. return ggml_is_contiguous(op->src[0]) &&
  9305. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9306. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  9307. (op->src[0]->type == op->type);
  9308. default:
  9309. return false;
  9310. }
  9311. break;
  9312. case GGML_OP_MUL_MAT:
  9313. case GGML_OP_MUL_MAT_ID:
  9314. {
  9315. ggml_type src0_type = op->src[0]->type;
  9316. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9317. const vk_device& device = ggml_vk_get_device(ctx->device);
  9318. if (op->op == GGML_OP_MUL_MAT_ID) {
  9319. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  9320. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  9321. return false;
  9322. }
  9323. }
  9324. switch (src0_type) {
  9325. case GGML_TYPE_F32:
  9326. case GGML_TYPE_F16:
  9327. case GGML_TYPE_BF16:
  9328. case GGML_TYPE_Q4_0:
  9329. case GGML_TYPE_Q4_1:
  9330. case GGML_TYPE_Q5_0:
  9331. case GGML_TYPE_Q5_1:
  9332. case GGML_TYPE_Q8_0:
  9333. case GGML_TYPE_Q2_K:
  9334. case GGML_TYPE_Q3_K:
  9335. case GGML_TYPE_Q4_K:
  9336. case GGML_TYPE_Q5_K:
  9337. case GGML_TYPE_Q6_K:
  9338. case GGML_TYPE_IQ1_S:
  9339. case GGML_TYPE_IQ1_M:
  9340. case GGML_TYPE_IQ2_XXS:
  9341. case GGML_TYPE_IQ2_XS:
  9342. case GGML_TYPE_IQ2_S:
  9343. case GGML_TYPE_IQ3_XXS:
  9344. case GGML_TYPE_IQ3_S:
  9345. case GGML_TYPE_IQ4_XS:
  9346. case GGML_TYPE_IQ4_NL:
  9347. case GGML_TYPE_MXFP4:
  9348. break;
  9349. default:
  9350. return false;
  9351. }
  9352. struct ggml_tensor * a;
  9353. struct ggml_tensor * b;
  9354. if (op->op == GGML_OP_MUL_MAT) {
  9355. a = op->src[0];
  9356. b = op->src[1];
  9357. } else {
  9358. a = op->src[2];
  9359. b = op->src[1];
  9360. }
  9361. if (a->ne[3] != b->ne[3]) {
  9362. return false;
  9363. }
  9364. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16) ||
  9365. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  9366. return false;
  9367. }
  9368. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  9369. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  9370. // So don't support this combination for now.
  9371. return false;
  9372. }
  9373. return true;
  9374. } break;
  9375. case GGML_OP_FLASH_ATTN_EXT:
  9376. {
  9377. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9378. auto device = ggml_vk_get_device(ctx->device);
  9379. bool coopmat2 = device->coopmat2;
  9380. FaHeadSizes head_sizes = fa_get_head_sizes(op->src[1]->ne[0], op->src[2]->ne[0]);
  9381. if (head_sizes == FA_HEAD_SIZE_UNSUPPORTED) {
  9382. return false;
  9383. }
  9384. if (op->src[4] && op->src[4]->type != GGML_TYPE_F32) {
  9385. return false;
  9386. }
  9387. if (op->src[0]->type != GGML_TYPE_F32) {
  9388. return false;
  9389. }
  9390. if (op->type != GGML_TYPE_F32) {
  9391. return false;
  9392. }
  9393. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  9394. return false;
  9395. }
  9396. // It's straightforward to support different K/V dequant, but would
  9397. // significantly increase the number of pipelines
  9398. if (op->src[1]->type != op->src[2]->type) {
  9399. return false;
  9400. }
  9401. switch (op->src[1]->type) {
  9402. case GGML_TYPE_F16:
  9403. case GGML_TYPE_Q4_0:
  9404. case GGML_TYPE_Q8_0:
  9405. // supported in scalar and coopmat2 paths
  9406. break;
  9407. case GGML_TYPE_Q4_1:
  9408. case GGML_TYPE_Q5_0:
  9409. case GGML_TYPE_Q5_1:
  9410. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  9411. //case GGML_TYPE_Q2_K:
  9412. //case GGML_TYPE_Q3_K:
  9413. //case GGML_TYPE_Q4_K:
  9414. //case GGML_TYPE_Q5_K:
  9415. //case GGML_TYPE_Q6_K:
  9416. //case GGML_TYPE_IQ1_S:
  9417. //case GGML_TYPE_IQ1_M:
  9418. //case GGML_TYPE_IQ2_XXS:
  9419. //case GGML_TYPE_IQ2_XS:
  9420. //case GGML_TYPE_IQ2_S:
  9421. //case GGML_TYPE_IQ3_XXS:
  9422. //case GGML_TYPE_IQ3_S:
  9423. //case GGML_TYPE_IQ4_XS:
  9424. case GGML_TYPE_IQ4_NL:
  9425. // currently supported only in coopmat2 path
  9426. if (!coopmat2) {
  9427. return false;
  9428. }
  9429. break;
  9430. default:
  9431. return false;
  9432. }
  9433. if (!coopmat2 && !device->subgroup_shuffle) {
  9434. // scalar FA uses subgroupShuffle
  9435. return false;
  9436. }
  9437. return true;
  9438. }
  9439. case GGML_OP_GET_ROWS:
  9440. {
  9441. switch (op->src[0]->type) {
  9442. case GGML_TYPE_F32:
  9443. case GGML_TYPE_F16:
  9444. case GGML_TYPE_BF16:
  9445. case GGML_TYPE_Q4_0:
  9446. case GGML_TYPE_Q4_1:
  9447. case GGML_TYPE_Q5_0:
  9448. case GGML_TYPE_Q5_1:
  9449. case GGML_TYPE_Q8_0:
  9450. case GGML_TYPE_IQ1_S:
  9451. case GGML_TYPE_IQ1_M:
  9452. case GGML_TYPE_IQ2_XXS:
  9453. case GGML_TYPE_IQ2_XS:
  9454. case GGML_TYPE_IQ2_S:
  9455. case GGML_TYPE_IQ3_XXS:
  9456. case GGML_TYPE_IQ3_S:
  9457. case GGML_TYPE_IQ4_XS:
  9458. case GGML_TYPE_IQ4_NL:
  9459. case GGML_TYPE_MXFP4:
  9460. return true;
  9461. default:
  9462. return false;
  9463. }
  9464. } break;
  9465. case GGML_OP_SET_ROWS:
  9466. {
  9467. switch (op->type) {
  9468. case GGML_TYPE_F32:
  9469. case GGML_TYPE_F16:
  9470. case GGML_TYPE_BF16:
  9471. case GGML_TYPE_Q4_0:
  9472. case GGML_TYPE_Q4_1:
  9473. case GGML_TYPE_Q5_0:
  9474. case GGML_TYPE_Q5_1:
  9475. case GGML_TYPE_Q8_0:
  9476. case GGML_TYPE_IQ4_NL:
  9477. return true;
  9478. default:
  9479. return false;
  9480. }
  9481. } break;
  9482. case GGML_OP_CONT:
  9483. case GGML_OP_CPY:
  9484. case GGML_OP_DUP:
  9485. {
  9486. ggml_type src0_type = op->src[0]->type;
  9487. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  9488. if (src0_type == GGML_TYPE_F32) {
  9489. switch (src1_type) {
  9490. case GGML_TYPE_F32:
  9491. case GGML_TYPE_F16:
  9492. case GGML_TYPE_BF16:
  9493. case GGML_TYPE_Q4_0:
  9494. case GGML_TYPE_Q4_1:
  9495. case GGML_TYPE_Q5_0:
  9496. case GGML_TYPE_Q5_1:
  9497. case GGML_TYPE_Q8_0:
  9498. case GGML_TYPE_IQ4_NL:
  9499. return true;
  9500. default:
  9501. break;
  9502. }
  9503. }
  9504. if (src1_type == GGML_TYPE_F32) {
  9505. switch (src0_type) {
  9506. case GGML_TYPE_F16:
  9507. case GGML_TYPE_Q4_0:
  9508. case GGML_TYPE_Q4_1:
  9509. case GGML_TYPE_Q5_0:
  9510. case GGML_TYPE_Q5_1:
  9511. case GGML_TYPE_Q8_0:
  9512. case GGML_TYPE_IQ4_NL:
  9513. return true;
  9514. default:
  9515. break;
  9516. }
  9517. }
  9518. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9519. return true;
  9520. }
  9521. // We can handle copying from a type to the same type if it's
  9522. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  9523. // so the type/block size must be a multiple of 4.
  9524. if (src0_type == src1_type &&
  9525. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  9526. (ggml_type_size(src0_type) % 2) == 0) {
  9527. return true;
  9528. }
  9529. return false;
  9530. } break;
  9531. case GGML_OP_REPEAT:
  9532. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  9533. case GGML_OP_REPEAT_BACK:
  9534. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  9535. case GGML_OP_ROPE:
  9536. case GGML_OP_ROPE_BACK:
  9537. case GGML_OP_NONE:
  9538. case GGML_OP_RESHAPE:
  9539. case GGML_OP_VIEW:
  9540. case GGML_OP_PERMUTE:
  9541. case GGML_OP_TRANSPOSE:
  9542. case GGML_OP_RMS_NORM:
  9543. return true;
  9544. case GGML_OP_NORM:
  9545. case GGML_OP_GROUP_NORM:
  9546. case GGML_OP_L2_NORM:
  9547. return ggml_is_contiguous(op->src[0]);
  9548. case GGML_OP_ADD:
  9549. case GGML_OP_SUB:
  9550. case GGML_OP_MUL:
  9551. case GGML_OP_DIV:
  9552. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9553. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  9554. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  9555. case GGML_OP_ADD_ID:
  9556. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32 && op->src[2]->type == GGML_TYPE_I32 &&
  9557. op->type == GGML_TYPE_F32;
  9558. case GGML_OP_SILU_BACK:
  9559. case GGML_OP_RMS_NORM_BACK:
  9560. case GGML_OP_SQR:
  9561. case GGML_OP_SIN:
  9562. case GGML_OP_COS:
  9563. case GGML_OP_CLAMP:
  9564. return op->src[0]->type == GGML_TYPE_F32;
  9565. case GGML_OP_UPSCALE:
  9566. case GGML_OP_ACC:
  9567. case GGML_OP_CONCAT:
  9568. case GGML_OP_SCALE:
  9569. case GGML_OP_PAD:
  9570. case GGML_OP_ROLL:
  9571. case GGML_OP_DIAG_MASK_INF:
  9572. case GGML_OP_SOFT_MAX:
  9573. case GGML_OP_SOFT_MAX_BACK:
  9574. case GGML_OP_ARGSORT:
  9575. case GGML_OP_SUM:
  9576. case GGML_OP_SUM_ROWS:
  9577. case GGML_OP_ARGMAX:
  9578. case GGML_OP_COUNT_EQUAL:
  9579. case GGML_OP_IM2COL:
  9580. case GGML_OP_TIMESTEP_EMBEDDING:
  9581. case GGML_OP_CONV_2D_DW:
  9582. case GGML_OP_POOL_2D:
  9583. case GGML_OP_RWKV_WKV6:
  9584. case GGML_OP_RWKV_WKV7:
  9585. case GGML_OP_LEAKY_RELU:
  9586. case GGML_OP_OPT_STEP_ADAMW:
  9587. return true;
  9588. case GGML_OP_CONV_TRANSPOSE_1D:
  9589. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  9590. case GGML_OP_CONV_2D:
  9591. {
  9592. // Op is disabled for Apple because it segfaults at pipeline create time on MoltenVK
  9593. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9594. const vk_device& device = ggml_vk_get_device(ctx->device);
  9595. bool is_Apple = ggml_vk_get_device(ctx->device)->vendor_id == VK_VENDOR_ID_APPLE;
  9596. // Channel-contiguous format is not supported yet.
  9597. return ((op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9598. op->src[1]->type == GGML_TYPE_F32 &&
  9599. op->type == GGML_TYPE_F32 &&
  9600. ggml_is_contiguous(op->src[0]) &&
  9601. ggml_is_contiguous(op->src[1]) &&
  9602. ggml_is_contiguous(op)) && !is_Apple;
  9603. }
  9604. default:
  9605. return false;
  9606. }
  9607. UNUSED(dev);
  9608. }
  9609. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  9610. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  9611. return false;
  9612. }
  9613. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9614. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  9615. return buft_ctx->device->idx == ctx->device;
  9616. }
  9617. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9618. const int min_batch_size = 32;
  9619. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  9620. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  9621. UNUSED(dev);
  9622. }
  9623. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  9624. /* .get_name = */ ggml_backend_vk_device_get_name,
  9625. /* .get_description = */ ggml_backend_vk_device_get_description,
  9626. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  9627. /* .get_type = */ ggml_backend_vk_device_get_type,
  9628. /* .get_props = */ ggml_backend_vk_device_get_props,
  9629. /* .init_backend = */ ggml_backend_vk_device_init,
  9630. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  9631. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  9632. /* .buffer_from_host_ptr = */ NULL,
  9633. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  9634. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  9635. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  9636. /* .event_new = */ NULL,
  9637. /* .event_free = */ NULL,
  9638. /* .event_synchronize = */ NULL,
  9639. };
  9640. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  9641. UNUSED(reg);
  9642. return GGML_VK_NAME;
  9643. }
  9644. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  9645. UNUSED(reg);
  9646. return ggml_backend_vk_get_device_count();
  9647. }
  9648. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  9649. static std::vector<ggml_backend_dev_t> devices;
  9650. static bool initialized = false;
  9651. {
  9652. static std::mutex mutex;
  9653. std::lock_guard<std::mutex> lock(mutex);
  9654. if (!initialized) {
  9655. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  9656. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  9657. char desc[256];
  9658. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  9659. ctx->device = i;
  9660. ctx->name = GGML_VK_NAME + std::to_string(i);
  9661. ctx->description = desc;
  9662. devices.push_back(new ggml_backend_device {
  9663. /* .iface = */ ggml_backend_vk_device_i,
  9664. /* .reg = */ reg,
  9665. /* .context = */ ctx,
  9666. });
  9667. }
  9668. initialized = true;
  9669. }
  9670. }
  9671. GGML_ASSERT(device < devices.size());
  9672. return devices[device];
  9673. }
  9674. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  9675. /* .get_name = */ ggml_backend_vk_reg_get_name,
  9676. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  9677. /* .get_device = */ ggml_backend_vk_reg_get_device,
  9678. /* .get_proc_address = */ NULL,
  9679. };
  9680. ggml_backend_reg_t ggml_backend_vk_reg() {
  9681. static ggml_backend_reg reg = {
  9682. /* .api_version = */ GGML_BACKEND_API_VERSION,
  9683. /* .iface = */ ggml_backend_vk_reg_i,
  9684. /* .context = */ nullptr,
  9685. };
  9686. try {
  9687. ggml_vk_instance_init();
  9688. return &reg;
  9689. } catch (const vk::SystemError& e) {
  9690. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  9691. return nullptr;
  9692. }
  9693. }
  9694. // Extension availability
  9695. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9696. #ifdef GGML_VULKAN_VALIDATE
  9697. bool portability_enumeration_ext = false;
  9698. // Check for portability enumeration extension for MoltenVK support
  9699. for (const auto& properties : instance_extensions) {
  9700. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9701. return true;
  9702. }
  9703. }
  9704. if (!portability_enumeration_ext) {
  9705. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9706. }
  9707. #endif
  9708. return false;
  9709. UNUSED(instance_extensions);
  9710. }
  9711. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9712. #ifdef __APPLE__
  9713. bool portability_enumeration_ext = false;
  9714. // Check for portability enumeration extension for MoltenVK support
  9715. for (const auto& properties : instance_extensions) {
  9716. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9717. return true;
  9718. }
  9719. }
  9720. if (!portability_enumeration_ext) {
  9721. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9722. }
  9723. #endif
  9724. return false;
  9725. UNUSED(instance_extensions);
  9726. }
  9727. // Extension availability
  9728. static bool ggml_vk_instance_debug_utils_ext_available(
  9729. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  9730. // Check for portability enumeration extension for MoltenVK support
  9731. for (const auto & properties : instance_extensions) {
  9732. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  9733. return true;
  9734. }
  9735. }
  9736. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  9737. return false;
  9738. UNUSED(instance_extensions);
  9739. }
  9740. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  9741. switch (props.vendorID) {
  9742. case VK_VENDOR_ID_INTEL:
  9743. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  9744. // while some older hardware (ex. Arc A770) has performance regressions
  9745. return arch == vk_device_architecture::INTEL_XE2;
  9746. case VK_VENDOR_ID_AMD:
  9747. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  9748. // Workaround for AMD proprietary driver reporting support on all GPUs
  9749. return arch == vk_device_architecture::AMD_RDNA3;
  9750. }
  9751. return true;
  9752. default:
  9753. return true;
  9754. }
  9755. }
  9756. // checks
  9757. #ifdef GGML_VULKAN_CHECK_RESULTS
  9758. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  9759. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  9760. return;
  9761. }
  9762. for (int j = 0; j < level; j++) {
  9763. std::cerr << " ";
  9764. }
  9765. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  9766. done.push_back(tensor);
  9767. for (int i = 0; i < GGML_MAX_SRC; i++) {
  9768. if (tensor->src[i] != nullptr) {
  9769. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  9770. }
  9771. }
  9772. }
  9773. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  9774. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  9775. return;
  9776. }
  9777. i0 = std::max(i0, 5);
  9778. i1 = std::max(i1, 5);
  9779. i2 = std::max(i2, 0);
  9780. i3 = std::max(i3, 0);
  9781. fprintf(stderr, " ");
  9782. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9783. fprintf(stderr, "%7d ", idx1);
  9784. }
  9785. fprintf(stderr, "\n");
  9786. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9787. fprintf(stderr, "%7d: ", idx0);
  9788. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9789. 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]) {
  9790. float val;
  9791. if (tensor->type == GGML_TYPE_F32) {
  9792. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9793. } else if (tensor->type == GGML_TYPE_F16) {
  9794. 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]));
  9795. } else if (tensor->type == GGML_TYPE_I32) {
  9796. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9797. } else {
  9798. GGML_ABORT("fatal error");
  9799. }
  9800. fprintf(stderr, "% 7.2f ", val);
  9801. } else {
  9802. fprintf(stderr, " ");
  9803. }
  9804. }
  9805. fprintf(stderr, "\n");
  9806. }
  9807. }
  9808. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  9809. void * tensor_data = tensor->data;
  9810. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  9811. if (is_gpu) {
  9812. const size_t tensor_size = ggml_nbytes(tensor);
  9813. tensor_data = malloc(tensor_size);
  9814. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9815. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  9816. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  9817. }
  9818. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  9819. 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;
  9820. if (tensor->src[0] != nullptr) {
  9821. 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;
  9822. }
  9823. if (tensor->src[1] != nullptr) {
  9824. 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;
  9825. }
  9826. std::cerr << std::endl << "Result:" << std::endl;
  9827. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9828. std::cerr << std::endl;
  9829. std::vector<const ggml_tensor *> done;
  9830. ggml_vk_print_graph_origin(tensor, done);
  9831. if (is_gpu) {
  9832. free(tensor_data);
  9833. }
  9834. }
  9835. void * comp_result;
  9836. size_t comp_size;
  9837. size_t comp_nb[GGML_MAX_DIMS];
  9838. size_t check_counter = 0;
  9839. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9840. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9841. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  9842. return;
  9843. }
  9844. bool fused_rms_norm_mul = false;
  9845. int rms_norm_idx = -1;
  9846. if (ctx->num_additional_fused_ops == 1 &&
  9847. tensor->op == GGML_OP_RMS_NORM &&
  9848. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9849. fused_rms_norm_mul = true;
  9850. tensor = cgraph->nodes[tensor_idx + 1];
  9851. }
  9852. check_counter++;
  9853. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9854. return;
  9855. }
  9856. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  9857. ggml_tensor * src0 = tensor->src[0];
  9858. ggml_tensor * src1 = tensor->src[1];
  9859. struct ggml_init_params iparams = {
  9860. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  9861. /*.mem_buffer =*/ NULL,
  9862. /*.no_alloc =*/ false,
  9863. };
  9864. struct ggml_context * ggml_ctx = ggml_init(iparams);
  9865. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9866. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  9867. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9868. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  9869. struct ggml_tensor * tensor_clone = nullptr;
  9870. for (int i = 0; i < 6; i++) {
  9871. ggml_tensor * srci = tensor->src[i];
  9872. if (fused_rms_norm_mul) {
  9873. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  9874. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  9875. switch (i) {
  9876. case 0: srci = rms_norm->src[0]; break;
  9877. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  9878. default: continue;
  9879. }
  9880. }
  9881. if (srci == nullptr) {
  9882. continue;
  9883. }
  9884. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  9885. size_t srci_size = ggml_nbytes(srci);
  9886. src_clone[i] = srci_clone;
  9887. src_size[i] = ggml_nbytes(srci);
  9888. src_buffer[i] = malloc(srci_size);
  9889. srci_clone->data = src_buffer[i];
  9890. if (ggml_backend_buffer_is_host(srci->buffer)) {
  9891. memcpy(srci_clone->data, srci->data, srci_size);
  9892. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9893. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  9894. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  9895. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9896. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  9897. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  9898. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  9899. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  9900. const int idx = i3*srci->ne[2] + i2;
  9901. ggml_vk_buffer_read(buffer_gpu, offset + idx * srci->nb[2], ((char *)srci_clone->data + idx * srci_clone->nb[2]), srci->ne[1] * srci->nb[1]);
  9902. }
  9903. }
  9904. srci_clone->nb[0] = srci->nb[0];
  9905. srci_clone->nb[1] = srci->nb[1];
  9906. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  9907. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  9908. }
  9909. } else {
  9910. if (offset + srci_size >= buffer_gpu->size) {
  9911. srci_size = buffer_gpu->size - offset;
  9912. }
  9913. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  9914. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9915. }
  9916. } else {
  9917. GGML_ABORT("fatal error");
  9918. }
  9919. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9920. ggml_vk_print_tensor(srci, srci_name[i]);
  9921. }
  9922. }
  9923. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  9924. const float * params = (const float *)tensor->op_params;
  9925. tensor_clone = ggml_flash_attn_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], params[0], params[1], params[2]);
  9926. if (src_clone[4]) {
  9927. ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]);
  9928. }
  9929. } else if (tensor->op == GGML_OP_MUL_MAT) {
  9930. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  9931. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  9932. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  9933. } else if (tensor->op == GGML_OP_SUB) {
  9934. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  9935. } else if (tensor->op == GGML_OP_MUL) {
  9936. if (fused_rms_norm_mul) {
  9937. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  9938. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  9939. } else {
  9940. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  9941. }
  9942. } else if (tensor->op == GGML_OP_DIV) {
  9943. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  9944. } else if (tensor->op == GGML_OP_CONCAT) {
  9945. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  9946. } else if (tensor->op == GGML_OP_UPSCALE) {
  9947. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  9948. } else if (tensor->op == GGML_OP_SCALE) {
  9949. const float * params = (const float *)tensor->op_params;
  9950. tensor_clone = ggml_scale_bias(ggml_ctx, src_clone[0], params[0], params[1]);
  9951. } else if (tensor->op == GGML_OP_SQR) {
  9952. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  9953. } else if (tensor->op == GGML_OP_SIN) {
  9954. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  9955. } else if (tensor->op == GGML_OP_COS) {
  9956. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  9957. } else if (tensor->op == GGML_OP_CLAMP) {
  9958. const float * params = (const float *)tensor->op_params;
  9959. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  9960. } else if (tensor->op == GGML_OP_PAD) {
  9961. tensor_clone = ggml_pad(ggml_ctx, src_clone[0], tensor->ne[0] - src_clone[0]->ne[0], tensor->ne[1] - src_clone[0]->ne[1], tensor->ne[2] - src_clone[0]->ne[2], tensor->ne[3] - src_clone[0]->ne[3]);
  9962. } else if (tensor->op == GGML_OP_REPEAT) {
  9963. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  9964. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  9965. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  9966. } else if (tensor->op == GGML_OP_ADD) {
  9967. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  9968. } else if (tensor->op == GGML_OP_ACC) {
  9969. tensor_clone = ggml_acc(ggml_ctx, src_clone[0], src_clone[1], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3]);
  9970. } else if (tensor->op == GGML_OP_NORM) {
  9971. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9972. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  9973. const float * float_params = (const float *)tensor->op_params;
  9974. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  9975. } else if (tensor->op == GGML_OP_RMS_NORM) {
  9976. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9977. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  9978. const float eps = ((float *) tensor->op_params)[0];
  9979. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  9980. } else if (tensor->op == GGML_OP_SILU_BACK) {
  9981. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  9982. } else if (tensor->op == GGML_OP_L2_NORM) {
  9983. const float eps = ((float *) tensor->op_params)[0];
  9984. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  9985. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  9986. if (src1 != nullptr) {
  9987. const float * params = (const float *)tensor->op_params;
  9988. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  9989. } else {
  9990. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  9991. }
  9992. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  9993. tensor_clone = ggml_soft_max_ext_back(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  9994. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  9995. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  9996. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  9997. const int n_dims = ((int32_t *) tensor->op_params)[1];
  9998. const int mode = ((int32_t *) tensor->op_params)[2];
  9999. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  10000. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  10001. const float freq_base = ((float *) tensor->op_params)[5];
  10002. const float freq_scale = ((float *) tensor->op_params)[6];
  10003. const float ext_factor = ((float *) tensor->op_params)[7];
  10004. const float attn_factor = ((float *) tensor->op_params)[8];
  10005. const float beta_fast = ((float *) tensor->op_params)[9];
  10006. const float beta_slow = ((float *) tensor->op_params)[10];
  10007. if (mode & GGML_ROPE_TYPE_MROPE) {
  10008. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  10009. if (tensor->op == GGML_OP_ROPE) {
  10010. tensor_clone = ggml_rope_multi(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10011. } else {
  10012. tensor_clone = ggml_rope_multi_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10013. }
  10014. } else {
  10015. if (tensor->op == GGML_OP_ROPE) {
  10016. tensor_clone = ggml_rope_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10017. } else {
  10018. tensor_clone = ggml_rope_ext_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  10019. }
  10020. }
  10021. } else if (tensor->op == GGML_OP_UNARY) {
  10022. switch (ggml_get_unary_op(tensor)) {
  10023. case GGML_UNARY_OP_SILU:
  10024. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  10025. break;
  10026. case GGML_UNARY_OP_GELU:
  10027. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  10028. break;
  10029. case GGML_UNARY_OP_GELU_ERF:
  10030. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  10031. break;
  10032. case GGML_UNARY_OP_GELU_QUICK:
  10033. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  10034. break;
  10035. case GGML_UNARY_OP_RELU:
  10036. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  10037. break;
  10038. case GGML_UNARY_OP_TANH:
  10039. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  10040. break;
  10041. case GGML_UNARY_OP_SIGMOID:
  10042. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  10043. break;
  10044. default:
  10045. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  10046. GGML_ABORT("fatal error");
  10047. }
  10048. } else if (tensor->op == GGML_OP_GLU) {
  10049. if (src_clone[1] == nullptr) {
  10050. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  10051. } else {
  10052. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  10053. }
  10054. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  10055. if (src1 == nullptr) {
  10056. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  10057. tensor_clone->type = tensor->type;
  10058. } else {
  10059. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  10060. }
  10061. } else if (tensor->op == GGML_OP_CONT) {
  10062. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  10063. } else if (tensor->op == GGML_OP_RESHAPE) {
  10064. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  10065. } else if (tensor->op == GGML_OP_VIEW) {
  10066. tensor_clone = ggml_view_4d(ggml_ctx, src_clone[0], 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]);
  10067. } else if (tensor->op == GGML_OP_PERMUTE) {
  10068. int32_t * params = (int32_t *)tensor->op_params;
  10069. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  10070. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  10071. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  10072. } else if (tensor->op == GGML_OP_GET_ROWS) {
  10073. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  10074. } else if (tensor->op == GGML_OP_ARGSORT) {
  10075. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  10076. } else if (tensor->op == GGML_OP_SUM) {
  10077. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  10078. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  10079. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  10080. } else if (tensor->op == GGML_OP_ARGMAX) {
  10081. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  10082. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  10083. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  10084. } else if (tensor->op == GGML_OP_IM2COL) {
  10085. const int32_t s0 = tensor->op_params[0];
  10086. const int32_t s1 = tensor->op_params[1];
  10087. const int32_t p0 = tensor->op_params[2];
  10088. const int32_t p1 = tensor->op_params[3];
  10089. const int32_t d0 = tensor->op_params[4];
  10090. const int32_t d1 = tensor->op_params[5];
  10091. const bool is_2D = tensor->op_params[6] == 1;
  10092. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  10093. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  10094. const int32_t dim = tensor->op_params[0];
  10095. const int32_t max_period = tensor->op_params[1];
  10096. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  10097. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  10098. const int32_t s0 = tensor->op_params[0];
  10099. const int32_t p0 = tensor->op_params[1];
  10100. const int32_t d0 = tensor->op_params[2];
  10101. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  10102. } else if (tensor->op == GGML_OP_POOL_2D) {
  10103. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  10104. const int32_t k0 = tensor->op_params[1];
  10105. const int32_t k1 = tensor->op_params[2];
  10106. const int32_t s0 = tensor->op_params[3];
  10107. const int32_t s1 = tensor->op_params[4];
  10108. const int32_t p0 = tensor->op_params[5];
  10109. const int32_t p1 = tensor->op_params[6];
  10110. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  10111. } else if (tensor->op == GGML_OP_CONV_2D) {
  10112. const int32_t s0 = tensor->op_params[0];
  10113. const int32_t s1 = tensor->op_params[1];
  10114. const int32_t p0 = tensor->op_params[2];
  10115. const int32_t p1 = tensor->op_params[3];
  10116. const int32_t d0 = tensor->op_params[4];
  10117. const int32_t d1 = tensor->op_params[5];
  10118. tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1);
  10119. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  10120. const float * op_params = (const float *)tensor->op_params;
  10121. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  10122. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  10123. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  10124. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  10125. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  10126. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  10127. src_clone[4], src_clone[5], src_clone[6]);
  10128. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  10129. src_clone[0]->flags = src0->flags;
  10130. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  10131. src_clone[2], src_clone[3], src_clone[4]);
  10132. }
  10133. else {
  10134. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  10135. GGML_ABORT("fatal error");
  10136. }
  10137. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  10138. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  10139. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  10140. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10141. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  10142. }
  10143. comp_size = ggml_nbytes(tensor_clone);
  10144. comp_result = malloc(comp_size);
  10145. memcpy(comp_result, tensor_clone->data, comp_size);
  10146. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  10147. for (int i = 0; i < 6; i++) {
  10148. if (src_buffer[i] != nullptr) {
  10149. free(src_buffer[i]);
  10150. }
  10151. }
  10152. ggml_free(ggml_ctx);
  10153. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  10154. }
  10155. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  10156. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  10157. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  10158. return;
  10159. }
  10160. bool fused_rms_norm_mul = false;
  10161. if (ctx->num_additional_fused_ops == 1 &&
  10162. tensor->op == GGML_OP_RMS_NORM &&
  10163. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  10164. fused_rms_norm_mul = true;
  10165. tensor = cgraph->nodes[tensor_idx + 1];
  10166. }
  10167. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  10168. return;
  10169. }
  10170. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  10171. ggml_tensor * src0 = tensor->src[0];
  10172. ggml_tensor * src1 = tensor->src[1];
  10173. ggml_tensor * src2 = tensor->src[2];
  10174. ggml_tensor * src3 = tensor->src[3];
  10175. void * tensor_data = tensor->data;
  10176. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  10177. size_t tensor_size = ggml_nbytes(tensor);
  10178. tensor_data = malloc(tensor_size);
  10179. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10180. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  10181. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  10182. if (offset + tensor_size >= buffer_gpu->size) {
  10183. tensor_size = buffer_gpu->size - offset;
  10184. }
  10185. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  10186. }
  10187. float first_error_result = -1.0f;
  10188. float first_error_correct = -1.0f;
  10189. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  10190. double avg_err = 0.0;
  10191. size_t counter = 0;
  10192. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  10193. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  10194. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  10195. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  10196. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  10197. float correct = 0.0f;
  10198. float result = 0.0f;
  10199. if (buffer_size_fit) {
  10200. if (tensor->type == GGML_TYPE_F32) {
  10201. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  10202. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  10203. } else if (tensor->type == GGML_TYPE_F16) {
  10204. 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]));
  10205. 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]));
  10206. } else if (tensor->type == GGML_TYPE_BF16) {
  10207. correct = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]));
  10208. result = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]));
  10209. } else if (tensor->type == GGML_TYPE_I32) {
  10210. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  10211. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  10212. } else if (tensor->type == GGML_TYPE_I64) {
  10213. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  10214. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  10215. } else {
  10216. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  10217. }
  10218. } else {
  10219. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  10220. GGML_ABORT("fatal error");
  10221. }
  10222. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  10223. 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;
  10224. 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;
  10225. if (src0 != nullptr) {
  10226. 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;
  10227. }
  10228. if (src1 != nullptr) {
  10229. 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;
  10230. }
  10231. if (src2 != nullptr) {
  10232. 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;
  10233. }
  10234. if (src3 != nullptr) {
  10235. std::cerr << "src3=" << src3 << " src3->name=" << src3->name << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  10236. }
  10237. 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;
  10238. std::cerr << std::endl << "Result:" << std::endl;
  10239. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  10240. std::cerr << std::endl << "Correct:" << std::endl;
  10241. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  10242. std::cerr << std::endl;
  10243. std::vector<const ggml_tensor *> done;
  10244. ggml_vk_print_graph_origin(tensor, done);
  10245. GGML_ABORT("fatal error");
  10246. }
  10247. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  10248. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  10249. first_error[0] = i0;
  10250. first_error[1] = i1;
  10251. first_error[2] = i2;
  10252. first_error[3] = i3;
  10253. first_error_result = result;
  10254. first_error_correct = correct;
  10255. }
  10256. // Special case, value is infinite, avoid NaN result in avg_err
  10257. // NaN also appears in results, if both are nan error is 0
  10258. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  10259. avg_err += std::fabs(correct - result) / denom;
  10260. }
  10261. counter++;
  10262. }
  10263. }
  10264. }
  10265. }
  10266. avg_err /= counter;
  10267. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  10268. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  10269. 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;
  10270. if (src0 != nullptr) {
  10271. 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;
  10272. }
  10273. if (src1 != nullptr) {
  10274. 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;
  10275. }
  10276. if (src2 != nullptr) {
  10277. 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;
  10278. }
  10279. if (src3 != nullptr) {
  10280. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  10281. }
  10282. 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;
  10283. std::cerr << std::endl << "Result:" << std::endl;
  10284. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  10285. std::cerr << std::endl << "Correct:" << std::endl;
  10286. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  10287. std::cerr << std::endl;
  10288. std::vector<const ggml_tensor *> done;
  10289. ggml_vk_print_graph_origin(tensor, done);
  10290. }
  10291. if (avg_err > 0.5 || std::isnan(avg_err)) {
  10292. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  10293. 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;
  10294. if (src0 != nullptr) {
  10295. 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;
  10296. }
  10297. if (src1 != nullptr) {
  10298. 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;
  10299. }
  10300. if (src2 != nullptr) {
  10301. 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;
  10302. }
  10303. if (src3 != nullptr) {
  10304. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  10305. }
  10306. 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;
  10307. std::cerr << std::endl << "Result:" << std::endl;
  10308. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  10309. std::cerr << std::endl << "Correct:" << std::endl;
  10310. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  10311. std::cerr << std::endl;
  10312. std::vector<const ggml_tensor *> done;
  10313. ggml_vk_print_graph_origin(tensor, done);
  10314. GGML_ABORT("fatal error");
  10315. } else {
  10316. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  10317. }
  10318. free(comp_result);
  10319. comp_result = nullptr;
  10320. comp_size = 0;
  10321. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  10322. free(tensor_data);
  10323. }
  10324. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  10325. }
  10326. #endif
  10327. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)