ggml-vulkan.cpp 701 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236
  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. // See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
  8. #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
  9. // We use VULKAN_HPP_DEFAULT_DISPATCHER, but not VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
  10. // to avoid conflicts with applications or other libraries who might use it.
  11. #if VK_HEADER_VERSION >= 301
  12. namespace vk::detail { class DispatchLoaderDynamic; }
  13. using vk::detail::DispatchLoaderDynamic;
  14. #else
  15. namespace vk { class DispatchLoaderDynamic; }
  16. using vk::DispatchLoaderDynamic;
  17. #endif
  18. DispatchLoaderDynamic & ggml_vk_default_dispatcher();
  19. #define VULKAN_HPP_DEFAULT_DISPATCHER ggml_vk_default_dispatcher()
  20. #include <vulkan/vulkan.hpp>
  21. #include <algorithm>
  22. #include <cmath>
  23. #include <iomanip>
  24. #include <iostream>
  25. #include <tuple>
  26. #include <vector>
  27. #include <sstream>
  28. #include <utility>
  29. #include <memory>
  30. #include <limits>
  31. #include <map>
  32. #include <unordered_map>
  33. #include <memory>
  34. #include <mutex>
  35. #include <future>
  36. #include <thread>
  37. #if defined(_MSC_VER)
  38. # define NOMINMAX 1
  39. # include <windows.h>
  40. # define YIELD() YieldProcessor()
  41. #elif defined(__clang__) || defined(__GNUC__)
  42. # if defined(__x86_64__) ||defined(__i386__)
  43. # include <immintrin.h>
  44. # define YIELD() _mm_pause()
  45. # elif defined(__arm__) || defined(__aarch64__)
  46. # if defined(__clang__)
  47. # include <arm_acle.h>
  48. # define YIELD() __yield()
  49. # else
  50. # define YIELD() asm volatile("yield")
  51. # endif
  52. # endif
  53. #endif
  54. #if !defined(YIELD)
  55. #define YIELD()
  56. #endif
  57. #include "ggml-impl.h"
  58. #include "ggml-backend-impl.h"
  59. #include "ggml-vulkan-shaders.hpp"
  60. // remove this once it's more widely available in the SDK
  61. #if !defined(VK_KHR_shader_bfloat16)
  62. #define VK_KHR_shader_bfloat16 1
  63. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  64. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  65. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  66. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  67. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  68. VkStructureType sType;
  69. void* pNext;
  70. VkBool32 shaderBFloat16Type;
  71. VkBool32 shaderBFloat16DotProduct;
  72. VkBool32 shaderBFloat16CooperativeMatrix;
  73. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  74. #endif
  75. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  76. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  77. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  78. #define VK_VENDOR_ID_AMD 0x1002
  79. #define VK_VENDOR_ID_APPLE 0x106b
  80. #define VK_VENDOR_ID_INTEL 0x8086
  81. #define VK_VENDOR_ID_NVIDIA 0x10de
  82. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  83. #define GGML_VK_MAX_NODES 8192
  84. #define MAX_VK_BUFFERS 256
  85. #define VK_CHECK(err, msg) \
  86. do { \
  87. vk::Result err_ = (err); \
  88. if (err_ != vk::Result::eSuccess) { \
  89. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  90. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  91. exit(1); \
  92. } \
  93. } while (0)
  94. #ifdef GGML_VULKAN_DEBUG
  95. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  96. #else
  97. #define VK_LOG_DEBUG(msg) ((void) 0)
  98. #endif // GGML_VULKAN_DEBUG
  99. struct ggml_backend_vk_context;
  100. #define MAX_PARAMETER_COUNT 12
  101. // Max number of adds that can be fused without exceeding MAX_PARAMETER_COUNT.
  102. #define MAX_FUSED_ADDS (MAX_PARAMETER_COUNT - 3)
  103. struct vk_pipeline_struct {
  104. std::string name;
  105. vk::ShaderModule shader_module;
  106. vk::PipelineLayout layout;
  107. vk::Pipeline pipeline;
  108. uint32_t push_constant_size;
  109. uint32_t parameter_count;
  110. std::array<uint32_t, 3> wg_denoms;
  111. uint32_t align;
  112. // true if fields have been set by ggml_vk_create_pipeline
  113. bool initialized {};
  114. // set to true to request the pipeline is compiled after the dryrun
  115. bool needed {};
  116. // set to true when the shader has been compiled
  117. bool compiled {};
  118. // number of registers used, extracted from pipeline executable properties
  119. uint32_t register_count {};
  120. };
  121. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  122. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  123. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  124. struct vk_matmul_pipeline_struct {
  125. vk_pipeline l, m, s;
  126. vk_pipeline a_l, a_m, a_s;
  127. };
  128. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  129. struct vk_matmul_pipeline2 {
  130. vk_matmul_pipeline2() {
  131. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  132. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  133. }
  134. vk_matmul_pipeline f32acc;
  135. vk_matmul_pipeline f16acc;
  136. };
  137. struct vk_device_struct;
  138. typedef std::shared_ptr<vk_device_struct> vk_device;
  139. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  140. struct vk_buffer_struct;
  141. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  142. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  143. struct ggml_backend_vk_buffer_type_context {
  144. std::string name;
  145. vk_device device;
  146. };
  147. struct vk_queue;
  148. // Stores command pool/buffers. There's an instance of this
  149. // for each (context,queue) pair and for each (device,queue) pair.
  150. struct vk_command_pool {
  151. void init(vk_device& device, vk_queue *q_);
  152. void destroy(vk::Device& device);
  153. vk::CommandPool pool;
  154. uint32_t cmd_buffer_idx;
  155. std::vector<vk::CommandBuffer> cmd_buffers;
  156. vk_queue *q;
  157. };
  158. // Prevent simultaneous submissions to the same queue.
  159. // This could be per vk_queue if we stopped having two vk_queue structures
  160. // sharing the same vk::Queue.
  161. static std::mutex queue_mutex;
  162. struct vk_queue {
  163. uint32_t queue_family_index;
  164. vk::Queue queue;
  165. vk_command_pool cmd_pool;
  166. vk::PipelineStageFlags stage_flags;
  167. bool transfer_only;
  168. // copy everything except the cmd_pool
  169. void copyFrom(vk_queue &other) {
  170. queue_family_index = other.queue_family_index;
  171. queue = other.queue;
  172. stage_flags = other.stage_flags;
  173. transfer_only = other.transfer_only;
  174. }
  175. };
  176. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  177. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  178. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  179. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  180. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  181. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  182. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  183. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  184. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  185. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  186. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  187. /* .is_host = */ NULL,
  188. };
  189. #ifdef GGML_VULKAN_MEMORY_DEBUG
  190. class vk_memory_logger;
  191. #endif
  192. class vk_perf_logger;
  193. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  194. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  195. static constexpr uint32_t p021_max_gqa_ratio = 8;
  196. enum vk_device_architecture {
  197. OTHER,
  198. AMD_GCN,
  199. AMD_RDNA1,
  200. AMD_RDNA2,
  201. AMD_RDNA3,
  202. INTEL_XE2,
  203. NVIDIA_PRE_TURING,
  204. };
  205. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  206. vk::PhysicalDeviceProperties props = device.getProperties();
  207. if (props.vendorID == VK_VENDOR_ID_AMD) {
  208. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  209. bool amd_shader_core_properties = false;
  210. bool integer_dot_product = false;
  211. bool subgroup_size_control = false;
  212. for (const auto& properties : ext_props) {
  213. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  214. amd_shader_core_properties = true;
  215. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  216. integer_dot_product = true;
  217. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  218. subgroup_size_control = true;
  219. }
  220. }
  221. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  222. return vk_device_architecture::OTHER;
  223. }
  224. vk::PhysicalDeviceProperties2 props2;
  225. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  226. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  227. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  228. props2.pNext = &shader_core_props_amd;
  229. shader_core_props_amd.pNext = &integer_dot_props;
  230. integer_dot_props.pNext = &subgroup_size_control_props;
  231. device.getProperties2(&props2);
  232. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  233. return vk_device_architecture::AMD_GCN;
  234. }
  235. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  236. // RDNA
  237. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  238. return vk_device_architecture::AMD_RDNA1;
  239. }
  240. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  241. return vk_device_architecture::AMD_RDNA3;
  242. }
  243. return vk_device_architecture::AMD_RDNA2;
  244. }
  245. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  246. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  247. bool subgroup_size_control = false;
  248. for (const auto& properties : ext_props) {
  249. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  250. subgroup_size_control = true;
  251. }
  252. }
  253. if (!subgroup_size_control) {
  254. return vk_device_architecture::OTHER;
  255. }
  256. vk::PhysicalDeviceProperties2 props2;
  257. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  258. props2.pNext = &subgroup_size_control_props;
  259. device.getProperties2(&props2);
  260. if (subgroup_size_control_props.minSubgroupSize == 16) {
  261. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  262. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  263. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  264. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  265. return vk_device_architecture::INTEL_XE2;
  266. }
  267. } else if (props.vendorID == VK_VENDOR_ID_NVIDIA) {
  268. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  269. bool cooperative_matrix = false;
  270. // Detect "pre-turing" based on lack of coopmat support.
  271. for (const auto& properties : ext_props) {
  272. if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0) {
  273. cooperative_matrix = true;
  274. break;
  275. }
  276. }
  277. if (!cooperative_matrix) {
  278. return vk_device_architecture::NVIDIA_PRE_TURING;
  279. }
  280. }
  281. return vk_device_architecture::OTHER;
  282. }
  283. enum vk_conv_shapes {
  284. CONV_SHAPE_128x128,
  285. CONV_SHAPE_64x32,
  286. CONV_SHAPE_32x256,
  287. CONV_SHAPE_COUNT,
  288. };
  289. enum dmmv_wg_sizes {
  290. DMMV_WG_SIZE_SUBGROUP,
  291. DMMV_WG_SIZE_LARGE,
  292. DMMV_WG_SIZE_COUNT,
  293. };
  294. enum FaCodePath {
  295. FA_SCALAR,
  296. FA_COOPMAT1,
  297. FA_COOPMAT2,
  298. };
  299. struct vk_fa_pipeline_state {
  300. vk_fa_pipeline_state(uint32_t HSK, uint32_t HSV, bool small_rows, FaCodePath path, bool aligned, bool f32acc)
  301. : HSK(HSK), HSV(HSV), small_rows(small_rows), path(path), aligned(aligned), f32acc(f32acc) {}
  302. uint32_t HSK, HSV;
  303. bool small_rows;
  304. FaCodePath path;
  305. bool aligned;
  306. bool f32acc;
  307. bool operator<(const vk_fa_pipeline_state &b) const {
  308. return std::tie(HSK, HSV, small_rows, path, aligned, f32acc) <
  309. std::tie(b.HSK, b.HSV, b.small_rows, b.path, b.aligned, b.f32acc);
  310. }
  311. };
  312. enum shader_reduction_mode {
  313. SHADER_REDUCTION_MODE_SHMEM,
  314. SHADER_REDUCTION_MODE_HYBRID,
  315. SHADER_REDUCTION_MODE_SUBGROUP,
  316. SHADER_REDUCTION_MODE_COUNT,
  317. };
  318. static constexpr uint32_t num_argsort_pipelines = 11;
  319. static constexpr uint32_t max_argsort_cols = 1 << (num_argsort_pipelines-1);
  320. static constexpr uint32_t num_topk_moe_pipelines = 10;
  321. static constexpr std::array topk_moe_norm{ GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT,
  322. GGML_OP_VIEW, GGML_OP_GET_ROWS, GGML_OP_RESHAPE,
  323. GGML_OP_SUM_ROWS, GGML_OP_DIV, GGML_OP_RESHAPE };
  324. static constexpr std::array topk_moe { GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT,
  325. GGML_OP_VIEW, GGML_OP_GET_ROWS };
  326. struct vk_device_struct {
  327. std::recursive_mutex mutex;
  328. vk::PhysicalDevice physical_device;
  329. vk::PhysicalDeviceProperties properties;
  330. std::string name;
  331. uint64_t max_memory_allocation_size;
  332. uint64_t max_buffer_size;
  333. uint64_t suballocation_block_size;
  334. bool fp16;
  335. bool bf16;
  336. bool pipeline_robustness;
  337. vk::Device device;
  338. uint32_t vendor_id;
  339. vk::DriverId driver_id;
  340. vk_device_architecture architecture;
  341. vk_queue compute_queue;
  342. vk_queue transfer_queue;
  343. bool single_queue;
  344. uint32_t subgroup_size;
  345. uint32_t shader_core_count;
  346. bool uma;
  347. bool prefer_host_memory;
  348. bool float_controls_rte_fp16;
  349. bool subgroup_arithmetic;
  350. bool subgroup_shuffle;
  351. bool subgroup_ballot;
  352. bool subgroup_clustered;
  353. bool multi_add;
  354. bool shader_int64;
  355. bool buffer_device_address;
  356. bool add_rms_fusion;
  357. uint32_t partials_binding_alignment;
  358. bool integer_dot_product;
  359. // 0: default, 1: force mmvq, -1: disable mmvq
  360. int32_t mmvq_mode;
  361. bool subgroup_size_control;
  362. uint32_t subgroup_min_size;
  363. uint32_t subgroup_max_size;
  364. bool subgroup_require_full_support;
  365. bool coopmat_support;
  366. bool coopmat_acc_f32_support {};
  367. bool coopmat_acc_f16_support {};
  368. bool coopmat_bf16_support {};
  369. bool coopmat_support_16x16x16_f16acc {};
  370. bool coopmat_support_16x16x16_f32acc {};
  371. bool coopmat1_fa_support {};
  372. uint32_t coopmat_m;
  373. uint32_t coopmat_n;
  374. uint32_t coopmat_k;
  375. bool coopmat_int_support;
  376. uint32_t coopmat_int_m;
  377. uint32_t coopmat_int_n;
  378. uint32_t coopmat_int_k;
  379. bool coopmat2;
  380. bool pipeline_executable_properties_support {};
  381. size_t idx;
  382. bool mul_mat_l[GGML_TYPE_COUNT];
  383. bool mul_mat_m[GGML_TYPE_COUNT];
  384. bool mul_mat_s[GGML_TYPE_COUNT];
  385. bool mul_mat_id_l[GGML_TYPE_COUNT];
  386. bool mul_mat_id_m[GGML_TYPE_COUNT];
  387. bool mul_mat_id_s[GGML_TYPE_COUNT];
  388. // set to true to indicate that some shaders need to be compiled after the dryrun
  389. bool need_compiles {};
  390. vk::DescriptorSetLayout dsl;
  391. vk_matmul_pipeline pipeline_matmul_f32 {};
  392. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  393. vk_matmul_pipeline pipeline_matmul_bf16 {};
  394. vk_matmul_pipeline2 pipeline_matmul_f16;
  395. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  396. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  397. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  398. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  399. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  400. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  401. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  402. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  403. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  404. vk_pipeline pipeline_matmul_split_k_reduce;
  405. vk_pipeline pipeline_quantize_q8_1;
  406. vk_pipeline pipeline_quantize_q8_1_x4;
  407. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  408. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  409. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  410. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  411. vk_pipeline pipeline_dequant_mul_mat_vec_q8_1_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  412. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  413. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  414. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  415. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  416. vk_pipeline pipeline_acc_f32;
  417. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  418. vk_pipeline pipeline_add[2][2][2];
  419. vk_pipeline pipeline_add_norepeat[2][2][2];
  420. vk_pipeline pipeline_sub[2][2][2];
  421. vk_pipeline pipeline_sub_norepeat[2][2][2];
  422. vk_pipeline pipeline_mul[2][2][2];
  423. vk_pipeline pipeline_mul_norepeat[2][2][2];
  424. vk_pipeline pipeline_div[2][2][2];
  425. vk_pipeline pipeline_div_norepeat[2][2][2];
  426. vk_pipeline pipeline_add_rms[2][2][2];
  427. vk_pipeline pipeline_add_rms_norepeat[2][2][2];
  428. // indexed by num_additional_fused_ops == num_adds - 1
  429. vk_pipeline pipeline_multi_add[MAX_FUSED_ADDS];
  430. vk_pipeline pipeline_multi_add_rms[MAX_FUSED_ADDS];
  431. vk_pipeline pipeline_add_id_f32;
  432. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  433. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bilinear_ac_f32;
  434. vk_pipeline pipeline_scale_f32;
  435. vk_pipeline pipeline_sqr_f32;
  436. vk_pipeline pipeline_sqrt_f32;
  437. vk_pipeline pipeline_sin_f32;
  438. vk_pipeline pipeline_cos_f32;
  439. vk_pipeline pipeline_clamp_f32;
  440. vk_pipeline pipeline_pad_f32;
  441. vk_pipeline pipeline_roll_f32;
  442. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  443. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16, pipeline_cpy_f32_i32, pipeline_cpy_i32_f32;
  444. 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, pipeline_contig_cpy_f32_i32, pipeline_contig_cpy_i32_f32;
  445. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  446. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  447. vk_pipeline pipeline_set_rows_i32[GGML_TYPE_COUNT];
  448. vk_pipeline pipeline_set_rows_i64[GGML_TYPE_COUNT];
  449. vk_pipeline pipeline_norm_f32;
  450. vk_pipeline pipeline_group_norm_f32;
  451. vk_pipeline pipeline_rms_norm_f32;
  452. vk_pipeline pipeline_rms_norm_mul_f32;
  453. vk_pipeline pipeline_rms_norm_partials_f32;
  454. vk_pipeline pipeline_rms_norm_mul_partials_f32;
  455. vk_pipeline pipeline_rms_norm_back_f32;
  456. vk_pipeline pipeline_l2_norm_f32;
  457. // [src/dst 0=fp32,1=fp16]
  458. vk_pipeline pipeline_exp[2];
  459. vk_pipeline pipeline_gelu[2];
  460. vk_pipeline pipeline_gelu_erf[2];
  461. vk_pipeline pipeline_gelu_quick[2];
  462. vk_pipeline pipeline_silu[2];
  463. vk_pipeline pipeline_relu[2];
  464. vk_pipeline pipeline_tanh[2];
  465. vk_pipeline pipeline_sigmoid[2];
  466. vk_pipeline pipeline_hardsigmoid[2];
  467. vk_pipeline pipeline_hardswish[2];
  468. vk_pipeline pipeline_geglu[2];
  469. vk_pipeline pipeline_reglu[2];
  470. vk_pipeline pipeline_swiglu[2];
  471. vk_pipeline pipeline_swiglu_oai[2];
  472. vk_pipeline pipeline_geglu_erf[2];
  473. vk_pipeline pipeline_geglu_quick[2];
  474. vk_pipeline pipeline_leaky_relu_f32;
  475. vk_pipeline pipeline_silu_back_f32;
  476. vk_pipeline pipeline_diag_mask_inf_f32;
  477. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  478. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  479. vk_pipeline pipeline_soft_max_back_f32;
  480. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  481. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  482. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  483. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  484. vk_pipeline pipeline_argsort_f32[num_argsort_pipelines];
  485. vk_pipeline pipeline_sum_rows_f32;
  486. vk_pipeline pipeline_argmax_f32;
  487. vk_pipeline pipeline_count_equal_i32;
  488. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  489. vk_pipeline pipeline_im2col_3d_f32, pipeline_im2col_3d_f32_f16;
  490. vk_pipeline pipeline_timestep_embedding_f32;
  491. vk_pipeline pipeline_conv_transpose_1d_f32;
  492. vk_pipeline pipeline_pool2d_f32;
  493. vk_pipeline pipeline_rwkv_wkv6_f32;
  494. vk_pipeline pipeline_rwkv_wkv7_f32;
  495. vk_pipeline pipeline_ssm_scan_f32_d128;
  496. vk_pipeline pipeline_ssm_scan_f32_d256;
  497. vk_pipeline pipeline_ssm_conv_f32;
  498. vk_pipeline pipeline_opt_step_adamw_f32;
  499. vk_pipeline pipeline_opt_step_sgd_f32;
  500. vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
  501. vk_pipeline pipeline_conv2d_f16_f32[CONV_SHAPE_COUNT];
  502. vk_pipeline pipeline_conv_transpose_2d_f32[CONV_SHAPE_COUNT];
  503. vk_pipeline pipeline_conv_transpose_2d_f16_f32[CONV_SHAPE_COUNT];
  504. vk_pipeline pipeline_conv2d_dw_whcn_f32, pipeline_conv2d_dw_whcn_f16_f32;
  505. vk_pipeline pipeline_conv2d_dw_cwhn_f32, pipeline_conv2d_dw_cwhn_f16_f32;
  506. std::map<vk_fa_pipeline_state, vk_pipeline> pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT];
  507. vk_pipeline pipeline_flash_attn_split_k_reduce;
  508. // [2] is {!norm, norm}
  509. vk_pipeline pipeline_topk_moe[num_topk_moe_pipelines][2];
  510. std::vector<vk_pipeline_ref> all_pipelines;
  511. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  512. vk::Fence fence;
  513. vk_buffer sync_staging;
  514. ggml_backend_buffer_type buffer_type;
  515. bool disable_fusion;
  516. bool disable_host_visible_vidmem;
  517. bool allow_sysmem_fallback;
  518. bool disable_graph_optimize;
  519. #ifdef GGML_VULKAN_MEMORY_DEBUG
  520. std::unique_ptr<vk_memory_logger> memory_logger;
  521. #endif
  522. // for GGML_VK_PERF_LOGGER
  523. std::unique_ptr<vk_perf_logger> perf_logger;
  524. vk::QueryPool query_pool;
  525. int32_t num_queries;
  526. ~vk_device_struct() {
  527. VK_LOG_DEBUG("destroy device " << name);
  528. device.destroyFence(fence);
  529. ggml_vk_destroy_buffer(sync_staging);
  530. compute_queue.cmd_pool.destroy(device);
  531. transfer_queue.cmd_pool.destroy(device);
  532. for (auto& pipeline : all_pipelines) {
  533. if (pipeline.expired()) {
  534. continue;
  535. }
  536. vk_pipeline pl = pipeline.lock();
  537. ggml_vk_destroy_pipeline(device, pl);
  538. }
  539. all_pipelines.clear();
  540. device.destroyDescriptorSetLayout(dsl);
  541. device.destroy();
  542. }
  543. };
  544. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  545. cmd_buffer_idx = 0;
  546. q = q_;
  547. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  548. pool = device->device.createCommandPool(command_pool_create_info);
  549. }
  550. void vk_command_pool::destroy(vk::Device& device) {
  551. device.destroyCommandPool(pool);
  552. pool = nullptr;
  553. cmd_buffers.clear();
  554. }
  555. struct vk_buffer_struct {
  556. vk::Buffer buffer = VK_NULL_HANDLE;
  557. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  558. vk::MemoryPropertyFlags memory_property_flags;
  559. void * ptr;
  560. size_t size = 0;
  561. vk::DeviceAddress bda_addr {};
  562. vk_device device;
  563. ~vk_buffer_struct() {
  564. if (size == 0) {
  565. return;
  566. }
  567. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  568. device->device.freeMemory(device_memory);
  569. device->device.destroyBuffer(buffer);
  570. }
  571. };
  572. struct vk_subbuffer {
  573. vk_buffer buffer;
  574. uint64_t offset;
  575. uint64_t size;
  576. operator vk::DescriptorBufferInfo() const {
  577. return { buffer->buffer, offset, size };
  578. }
  579. };
  580. struct vk_semaphore {
  581. vk::Semaphore s;
  582. uint64_t value;
  583. };
  584. struct vk_submission {
  585. vk::CommandBuffer buffer;
  586. std::vector<vk_semaphore> wait_semaphores;
  587. std::vector<vk_semaphore> signal_semaphores;
  588. };
  589. typedef std::vector<vk_submission> vk_sequence;
  590. struct vk_mat_mat_push_constants {
  591. uint32_t M; uint32_t N; uint32_t K;
  592. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  593. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  594. uint32_t k_split;
  595. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  596. uint32_t padded_N;
  597. };
  598. struct vk_mat_vec_push_constants {
  599. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  600. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  601. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  602. };
  603. struct vk_mat_mat_id_push_constants {
  604. uint32_t M; uint32_t N; uint32_t K;
  605. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  606. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  607. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  608. uint32_t padded_N;
  609. };
  610. struct vk_mat_vec_id_push_constants {
  611. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  612. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  613. uint32_t nei0; uint32_t ne11;
  614. };
  615. struct vk_flash_attn_push_constants {
  616. uint32_t N;
  617. uint32_t KV;
  618. uint32_t ne1;
  619. uint32_t ne2;
  620. uint32_t ne3;
  621. uint32_t neq2;
  622. uint32_t neq3;
  623. uint32_t nek2;
  624. uint32_t nek3;
  625. uint32_t nev2;
  626. uint32_t nev3;
  627. uint32_t nem1;
  628. uint32_t nem2;
  629. uint32_t nem3;
  630. uint32_t nb01;
  631. uint32_t nb02;
  632. uint32_t nb03;
  633. uint32_t nb11;
  634. uint32_t nb12;
  635. uint32_t nb13;
  636. uint32_t nb21;
  637. uint32_t nb22;
  638. uint32_t nb23;
  639. float scale;
  640. float max_bias;
  641. float logit_softcap;
  642. uint32_t mask_n_head_log2;
  643. float m0;
  644. float m1;
  645. uint32_t gqa_ratio;
  646. uint32_t split_kv;
  647. uint32_t k_num;
  648. };
  649. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  650. struct vk_op_push_constants {
  651. uint32_t KX;
  652. uint32_t KY;
  653. float param1;
  654. float param2;
  655. };
  656. struct vk_op_glu_push_constants {
  657. uint32_t N;
  658. uint32_t ne00;
  659. uint32_t ne20;
  660. uint32_t mode; // 0: default, 1: swapped, 2: split
  661. float alpha; // for swiglu_oai
  662. float limit;
  663. };
  664. struct vk_op_unary_push_constants {
  665. uint32_t ne;
  666. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  667. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  668. uint32_t misalign_offsets;
  669. float param1; float param2;
  670. uint32_t ne0_012mp; uint32_t ne0_012L;
  671. uint32_t ne0_01mp; uint32_t ne0_01L;
  672. uint32_t ne0_0mp; uint32_t ne0_0L;
  673. uint32_t ne1_012mp; uint32_t ne1_012L;
  674. uint32_t ne1_01mp; uint32_t ne1_01L;
  675. uint32_t ne1_0mp; uint32_t ne1_0L;
  676. };
  677. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  678. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  679. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  680. ne = ne != 0 ? ne : ggml_nelements(dst);
  681. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  682. vk_op_unary_push_constants p{};
  683. p.ne = (uint32_t)ne;
  684. size_t src0_tsize = ggml_type_size(src0->type);
  685. p.ne00 = (uint32_t)src0->ne[0];
  686. p.ne01 = (uint32_t)src0->ne[1];
  687. p.ne02 = (uint32_t)src0->ne[2];
  688. p.ne03 = (uint32_t)src0->ne[3];
  689. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  690. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  691. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  692. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  693. size_t dst_tsize = ggml_type_size(dst->type);
  694. p.ne10 = (uint32_t)dst->ne[0];
  695. p.ne11 = (uint32_t)dst->ne[1];
  696. p.ne12 = (uint32_t)dst->ne[2];
  697. p.ne13 = (uint32_t)dst->ne[3];
  698. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  699. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  700. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  701. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  702. return p; // offsets are initialized later in ggml_vk_op
  703. }
  704. struct vk_op_pad_push_constants {
  705. uint32_t ne;
  706. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  707. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  708. uint32_t misalign_offsets;
  709. uint32_t lp0; uint32_t rp0;
  710. uint32_t lp1; uint32_t rp1;
  711. uint32_t lp2; uint32_t rp2;
  712. uint32_t lp3; uint32_t rp3;
  713. };
  714. static vk_op_pad_push_constants vk_op_pad_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst) {
  715. int64_t ne = ggml_nelements(dst);
  716. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  717. vk_op_pad_push_constants p{};
  718. p.ne = (uint32_t)ne;
  719. size_t src0_tsize = ggml_type_size(src0->type);
  720. p.ne00 = (uint32_t)src0->ne[0];
  721. p.ne01 = (uint32_t)src0->ne[1];
  722. p.ne02 = (uint32_t)src0->ne[2];
  723. p.ne03 = (uint32_t)src0->ne[3];
  724. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  725. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  726. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  727. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  728. size_t dst_tsize = ggml_type_size(dst->type);
  729. p.ne10 = (uint32_t)dst->ne[0];
  730. p.ne11 = (uint32_t)dst->ne[1];
  731. p.ne12 = (uint32_t)dst->ne[2];
  732. p.ne13 = (uint32_t)dst->ne[3];
  733. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  734. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  735. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  736. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  737. p.lp0 = dst->op_params[0];
  738. p.rp0 = dst->op_params[1];
  739. p.lp1 = dst->op_params[2];
  740. p.rp1 = dst->op_params[3];
  741. p.lp2 = dst->op_params[4];
  742. p.rp2 = dst->op_params[5];
  743. p.lp3 = dst->op_params[6];
  744. p.rp3 = dst->op_params[7];
  745. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  746. }
  747. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  748. // Precompute mp (m' in the paper) and L such that division
  749. // can be computed using a multiply (high 32b of 64b result)
  750. // and a shift:
  751. //
  752. // n/d = (mulhi(n, mp) + n) >> L;
  753. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  754. {
  755. // compute L = ceil(log2(d));
  756. L = 0;
  757. while (L < 32 && (uint32_t{1} << L) < d) {
  758. L++;
  759. }
  760. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  761. }
  762. template <typename T> void init_pushconst_fastdiv(T &p) {
  763. GGML_UNUSED(p);
  764. static_assert(!std::is_const<T>::value, "unexpected type");
  765. }
  766. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  767. // Compute magic values to divide by these six numbers.
  768. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  769. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  770. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  771. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  772. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  773. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  774. }
  775. struct vk_op_binary_push_constants {
  776. uint32_t ne;
  777. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  778. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  779. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  780. uint32_t misalign_offsets;
  781. float param1; float param2; int32_t param3;
  782. };
  783. struct vk_op_multi_add_push_constants {
  784. // shape for dst
  785. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23;
  786. // strides for srcs+dst
  787. uint32_t nb[MAX_PARAMETER_COUNT][4];
  788. uint32_t rms_partials;
  789. };
  790. // update multi_add.comp if this changes
  791. static_assert(MAX_PARAMETER_COUNT == 12);
  792. static_assert(sizeof(vk_op_multi_add_push_constants) <= 256);
  793. struct vk_op_topk_moe_push_constants {
  794. uint32_t n_rows;
  795. uint32_t n_expert_used;
  796. };
  797. struct vk_op_add_id_push_constants {
  798. uint32_t ne0;
  799. uint32_t ne1;
  800. uint32_t s01;
  801. uint32_t s02;
  802. uint32_t s11;
  803. uint32_t s21;
  804. };
  805. struct vk_op_diag_mask_push_constants {
  806. uint32_t ncols;
  807. uint32_t rows_per_channel;
  808. int32_t n_past;
  809. };
  810. struct vk_op_rope_push_constants {
  811. uint32_t ncols;
  812. uint32_t n_dims;
  813. float freq_scale;
  814. uint32_t p_delta_rows;
  815. float freq_base;
  816. float ext_factor;
  817. float attn_factor;
  818. float corr_dims[2];
  819. float theta_scale;
  820. uint32_t has_ff;
  821. uint32_t ne02;
  822. uint32_t s1;
  823. uint32_t s2;
  824. int32_t sections[4];
  825. uint32_t is_back;
  826. };
  827. struct vk_op_soft_max_push_constants {
  828. uint32_t KX;
  829. uint32_t KY;
  830. uint32_t ne00;
  831. uint32_t ne01;
  832. uint32_t ne02;
  833. uint32_t ne12;
  834. uint32_t ne13;
  835. uint32_t nb11;
  836. uint32_t nb12;
  837. uint32_t nb13;
  838. float scale;
  839. float max_bias;
  840. float m0;
  841. float m1;
  842. uint32_t n_head_log2;
  843. uint32_t nrows_x;
  844. uint32_t has_sinks;
  845. };
  846. struct vk_op_argsort_push_constants {
  847. uint32_t ncols;
  848. int32_t order;
  849. };
  850. struct vk_op_im2col_push_constants {
  851. uint64_t dst_addr;
  852. uint32_t batch_offset; uint32_t offset_delta;
  853. uint32_t IC;
  854. uint32_t IW; uint32_t IH;
  855. uint32_t OW; uint32_t OH;
  856. uint32_t KW; uint32_t KH;
  857. uint32_t pelements;
  858. uint32_t CHW;
  859. int32_t s0; int32_t s1;
  860. int32_t p0; int32_t p1;
  861. int32_t d0; int32_t d1;
  862. };
  863. struct vk_op_im2col_3d_push_constants {
  864. uint64_t dst_addr;
  865. uint32_t nb10;
  866. uint32_t nb11;
  867. uint32_t nb12;
  868. uint32_t nb13;
  869. uint32_t s0;
  870. uint32_t s1;
  871. uint32_t s2;
  872. uint32_t p0;
  873. uint32_t p1;
  874. uint32_t p2;
  875. uint32_t d0;
  876. uint32_t d1;
  877. uint32_t d2;
  878. uint32_t IW;
  879. uint32_t IH;
  880. uint32_t ID;
  881. uint32_t IC;
  882. uint32_t KW;
  883. uint32_t OH;
  884. uint32_t KD_KH_KW;
  885. uint32_t KH_KW;
  886. uint32_t IC_KD_KH_KW;
  887. uint32_t N_OD_OH;
  888. uint32_t OD_OH;
  889. uint32_t OD_OH_OW_IC_KD_KH_KW;
  890. uint32_t OH_OW_IC_KD_KH_KW;
  891. uint32_t OW_IC_KD_KH_KW;
  892. uint32_t misalign_offsets;
  893. };
  894. struct vk_op_timestep_embedding_push_constants {
  895. uint32_t nb1;
  896. uint32_t dim;
  897. uint32_t max_period;
  898. };
  899. struct vk_op_conv_transpose_1d_push_constants {
  900. uint32_t Cout;
  901. uint32_t Cin;
  902. uint32_t K;
  903. uint32_t L;
  904. uint32_t KL;
  905. uint32_t nb01;
  906. uint32_t nb02;
  907. uint32_t nb11;
  908. uint32_t nb1;
  909. int32_t s0;
  910. };
  911. struct vk_op_pool2d_push_constants {
  912. uint32_t IW; uint32_t IH;
  913. uint32_t OW; uint32_t OH;
  914. uint32_t OC;
  915. uint32_t pelements;
  916. uint32_t op;
  917. int32_t k0; int32_t k1;
  918. int32_t s0; int32_t s1;
  919. int32_t p0; int32_t p1;
  920. };
  921. struct vk_op_rwkv_wkv6_push_constants {
  922. uint32_t B;
  923. uint32_t T;
  924. uint32_t C;
  925. uint32_t H;
  926. };
  927. struct vk_op_rwkv_wkv7_push_constants {
  928. uint32_t B;
  929. uint32_t T;
  930. uint32_t C;
  931. uint32_t H;
  932. };
  933. struct vk_op_ssm_scan_push_constants {
  934. uint32_t nb02, nb03, nb12, nb13;
  935. uint32_t nb21, nb22, nb31;
  936. uint32_t nb42, nb43, nb52, nb53;
  937. uint32_t s_off;
  938. uint32_t n_head, d_head, n_group, n_tok;
  939. };
  940. struct vk_op_ssm_conv_push_constants {
  941. uint32_t nb01, nb02;
  942. uint32_t nb11;
  943. uint32_t dst_nb0, dst_nb1, dst_nb2;
  944. uint32_t nc, ncs, nr, n_t, n_s;
  945. };
  946. struct vk_op_conv2d_push_constants {
  947. uint32_t Cout;
  948. uint32_t Cin;
  949. uint32_t N;
  950. uint32_t KW;
  951. uint32_t KH;
  952. uint32_t W;
  953. uint32_t H;
  954. uint32_t OW;
  955. uint32_t OH;
  956. uint32_t s0;
  957. uint32_t s1;
  958. uint32_t p0;
  959. uint32_t p1;
  960. uint32_t d0;
  961. uint32_t d1;
  962. uint32_t nb01;
  963. uint32_t nb02;
  964. uint32_t nb03;
  965. uint32_t nb11;
  966. uint32_t nb12;
  967. uint32_t nb13;
  968. uint32_t nb1;
  969. uint32_t nb2;
  970. uint32_t nb3;
  971. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH
  972. uint32_t KWmp; uint32_t KWL;
  973. uint32_t KWKHmp; uint32_t KWKHL;
  974. uint32_t OWmp; uint32_t OWL;
  975. uint32_t OWOHmp; uint32_t OWOHL;
  976. };
  977. template <> void init_pushconst_fastdiv(vk_op_conv2d_push_constants &p) {
  978. // Compute magic values to divide by KW, KW*KH, OW, OW*OH
  979. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  980. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  981. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  982. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  983. }
  984. struct vk_op_conv_transpose_2d_push_constants {
  985. uint32_t Cout;
  986. uint32_t Cin;
  987. uint32_t N;
  988. uint32_t KW;
  989. uint32_t KH;
  990. uint32_t W;
  991. uint32_t H;
  992. uint32_t OW;
  993. uint32_t OH;
  994. uint32_t s0;
  995. uint32_t s1;
  996. uint32_t p0;
  997. uint32_t p1;
  998. uint32_t d0;
  999. uint32_t d1;
  1000. uint32_t nb01;
  1001. uint32_t nb02;
  1002. uint32_t nb03;
  1003. uint32_t nb11;
  1004. uint32_t nb12;
  1005. uint32_t nb13;
  1006. uint32_t nb1;
  1007. uint32_t nb2;
  1008. uint32_t nb3;
  1009. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH, s0, s1
  1010. uint32_t KWmp; uint32_t KWL;
  1011. uint32_t KWKHmp; uint32_t KWKHL;
  1012. uint32_t OWmp; uint32_t OWL;
  1013. uint32_t OWOHmp; uint32_t OWOHL;
  1014. uint32_t s0mp; uint32_t s0L;
  1015. uint32_t s1mp; uint32_t s1L;
  1016. };
  1017. template <> void init_pushconst_fastdiv(vk_op_conv_transpose_2d_push_constants &p) {
  1018. // Compute magic values to divide by KW, KW*KH, OW, OW*OH, s0, s1
  1019. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  1020. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  1021. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  1022. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  1023. init_fastdiv_values(p.s0, p.s0mp, p.s0L);
  1024. init_fastdiv_values(p.s1, p.s1mp, p.s1L);
  1025. }
  1026. struct vk_op_conv2d_dw_push_constants {
  1027. uint32_t ne;
  1028. uint32_t batches;
  1029. uint32_t channels;
  1030. uint32_t dst_w;
  1031. uint32_t dst_h;
  1032. uint32_t src_w;
  1033. uint32_t src_h;
  1034. uint32_t knl_w;
  1035. uint32_t knl_h;
  1036. int32_t stride_x;
  1037. int32_t stride_y;
  1038. int32_t pad_x;
  1039. int32_t pad_y;
  1040. int32_t dilation_x;
  1041. int32_t dilation_y;
  1042. };
  1043. struct vk_op_upscale_push_constants {
  1044. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  1045. uint32_t ne00; uint32_t ne01;
  1046. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  1047. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  1048. float sf0; float sf1; float sf2; float sf3;
  1049. };
  1050. struct vk_op_sum_rows_push_constants
  1051. {
  1052. uint32_t n_cols;
  1053. uint32_t ne01, ne02;
  1054. uint32_t nb01, nb02, nb03;
  1055. uint32_t nb11, nb12, nb13;
  1056. float weight;
  1057. uint32_t misalign_offsets;
  1058. uint32_t ne0_12mp, ne0_12L;
  1059. uint32_t ne0_1mp, ne0_1L;
  1060. };
  1061. static vk_op_sum_rows_push_constants vk_op_sum_rows_push_constants_init(const ggml_tensor * src, const ggml_tensor * dst, int64_t n_cols) {
  1062. uint32_t type_size = (uint32_t)ggml_type_size(src->type);
  1063. vk_op_sum_rows_push_constants p = {};
  1064. p.n_cols = (uint32_t)n_cols;
  1065. p.ne01 = (uint32_t)src->ne[1];
  1066. p.ne02 = (uint32_t)src->ne[2];
  1067. p.nb01 = (uint32_t)src->nb[1] / type_size;
  1068. p.nb02 = (uint32_t)src->nb[2] / type_size;
  1069. p.nb03 = (uint32_t)src->nb[3] / type_size;
  1070. p.nb11 = (uint32_t)dst->nb[1] / type_size;
  1071. p.nb12 = (uint32_t)dst->nb[2] / type_size;
  1072. p.nb13 = (uint32_t)dst->nb[3] / type_size;
  1073. p.weight = 1.0f;
  1074. return p;
  1075. }
  1076. template <> void init_pushconst_fastdiv(vk_op_sum_rows_push_constants &p) {
  1077. init_fastdiv_values(p.ne01*p.ne02, p.ne0_12mp, p.ne0_12L);
  1078. init_fastdiv_values(p.ne01, p.ne0_1mp, p.ne0_1L);
  1079. }
  1080. // Allow pre-recording command buffers
  1081. struct vk_staging_memcpy {
  1082. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  1083. void * dst;
  1084. const void * src;
  1085. size_t n;
  1086. };
  1087. struct vk_staging_memset {
  1088. vk_staging_memset(void * _dst, uint32_t _val, size_t _n) : dst(_dst), val(_val), n(_n) {}
  1089. void * dst;
  1090. uint32_t val;
  1091. size_t n;
  1092. };
  1093. struct vk_context_struct {
  1094. vk_submission * s;
  1095. std::vector<vk_sequence> seqs;
  1096. int exit_tensor_idx;
  1097. std::vector<vk_staging_memcpy> in_memcpys;
  1098. std::vector<vk_staging_memcpy> out_memcpys;
  1099. std::vector<vk_staging_memset> memsets;
  1100. vk_command_pool * p {};
  1101. };
  1102. typedef std::shared_ptr<vk_context_struct> vk_context;
  1103. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  1104. struct ggml_vk_garbage_collector {
  1105. std::vector<vk_semaphore> tl_semaphores;
  1106. std::vector<vk_semaphore> semaphores;
  1107. std::vector<vk::Event> events;
  1108. std::vector<vk_buffer> temp_buffers;
  1109. std::vector<vk_context> contexts;
  1110. };
  1111. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  1112. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  1113. static std::string format_size(size_t size) {
  1114. const size_t kib = 1024;
  1115. const size_t mib = kib * 1024;
  1116. const size_t gib = mib * 1024;
  1117. std::ostringstream oss;
  1118. oss << std::fixed << std::setprecision(2);
  1119. if (size >= gib) {
  1120. oss << static_cast<double>(size) / gib << " GiB";
  1121. } else if (size >= mib) {
  1122. oss << static_cast<double>(size) / mib << " MiB";
  1123. } else if (size >= kib) {
  1124. oss << static_cast<double>(size) / kib << " KiB";
  1125. } else {
  1126. oss << size << " B";
  1127. }
  1128. return oss.str();
  1129. }
  1130. class vk_memory_logger {
  1131. public:
  1132. vk_memory_logger(): total_device(0), total_host(0) {}
  1133. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  1134. void log_deallocation(vk_buffer_ref buf_ref);
  1135. private:
  1136. std::map<vk::Buffer, size_t> allocations; // Track allocations
  1137. size_t total_device;
  1138. size_t total_host;
  1139. };
  1140. #else
  1141. #define VK_LOG_MEMORY(msg) ((void) 0)
  1142. #endif // GGML_VULKAN_MEMORY_DEBUG
  1143. class vk_perf_logger {
  1144. public:
  1145. void print_timings() {
  1146. if (timings.empty()) {
  1147. return;
  1148. }
  1149. uint64_t total_all_op_times = 0;
  1150. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  1151. for (const auto & t : timings) {
  1152. uint64_t total_op_times = 0;
  1153. for (const auto & time : t.second) {
  1154. total_op_times += time;
  1155. }
  1156. std::cerr << t.first << ": " << t.second.size() << " x " << (total_op_times / t.second.size() / 1000.0)
  1157. << " us";
  1158. // If we have as many flops entries as timing entries for the op, then compute and log the flops/S.
  1159. auto it = flops.find(t.first);
  1160. if (it != flops.end() && (it->second).size() == t.second.size()) {
  1161. uint64_t total_op_flops = 0;
  1162. for (const auto & elem : it->second) {
  1163. total_op_flops += elem;
  1164. }
  1165. std::cerr << " ("
  1166. << (double(total_op_flops) / (1000.0 * 1000.0 * 1000.0)) /
  1167. (double(total_op_times) / (1000.0 * 1000.0 * 1000.0))
  1168. << " GFLOPS/s)";
  1169. }
  1170. total_all_op_times += total_op_times;
  1171. std::cerr << std::endl;
  1172. }
  1173. if (timings.size() > 0) {
  1174. std::cerr << "Total time: " << total_all_op_times / 1000.0 << " us." << std::endl;
  1175. }
  1176. timings.clear();
  1177. flops.clear();
  1178. }
  1179. void log_timing(const ggml_tensor * node, uint64_t time) {
  1180. if (node->op == GGML_OP_UNARY) {
  1181. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  1182. return;
  1183. }
  1184. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  1185. const uint64_t m = node->src[0]->ne[1];
  1186. const uint64_t n = node->ne[1];
  1187. const uint64_t k = node->src[1]->ne[0];
  1188. const uint64_t batch = node->src[1]->ne[2] * node->src[1]->ne[3];
  1189. std::string name = ggml_op_name(node->op);
  1190. if ((node->op == GGML_OP_MUL_MAT && n <= mul_mat_vec_max_cols) ||
  1191. (node->op == GGML_OP_MUL_MAT_ID && node->src[2]->ne[1] == 1)) {
  1192. name += "_VEC";
  1193. }
  1194. name += " ";
  1195. name += ggml_type_name(node->src[0]->type);
  1196. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  1197. if (batch > 1) {
  1198. name += " batch=" + std::to_string(batch);
  1199. }
  1200. timings[name].push_back(time);
  1201. flops[name].push_back(m * n * (k + (k - 1)) * batch);
  1202. return;
  1203. }
  1204. if (node->op == GGML_OP_CONV_2D || node->op == GGML_OP_CONV_TRANSPOSE_2D) {
  1205. std::string name = ggml_op_name(node->op);
  1206. ggml_tensor * knl = node->src[0];
  1207. uint64_t OW = node->ne[0];
  1208. uint64_t OH = node->ne[1];
  1209. uint64_t N = node->ne[3];
  1210. uint64_t Cout = node->ne[2];
  1211. uint64_t KW = knl->ne[0];
  1212. uint64_t KH = knl->ne[1];
  1213. uint64_t Cin = node->src[1]->ne[2];
  1214. // KxCRS @ CRSxNPQ = KxNPQ -> M=K, K=CRS, N=NPQ
  1215. uint64_t size_M = Cout;
  1216. uint64_t size_K = Cin * KW * KH;
  1217. uint64_t size_N = N * OW * OH;
  1218. uint64_t n_flops = size_M * size_N * (size_K + (size_K - 1));
  1219. name += " M=Cout=" + std::to_string(size_M) + ", K=Cin*KW*KH=" + std::to_string(size_K) +
  1220. ", N=N*OW*OH=" + std::to_string(size_N);
  1221. flops[name].push_back(n_flops);
  1222. timings[name].push_back(time);
  1223. return;
  1224. }
  1225. if (node->op == GGML_OP_RMS_NORM) {
  1226. std::string name = ggml_op_name(node->op);
  1227. name += "(" + std::to_string(node->ne[0]) + "," + std::to_string(node->ne[1]) + "," + std::to_string(node->ne[2]) + "," + std::to_string(node->ne[3]) + ")";
  1228. timings[name].push_back(time);
  1229. return;
  1230. }
  1231. timings[ggml_op_name(node->op)].push_back(time);
  1232. }
  1233. private:
  1234. std::map<std::string, std::vector<uint64_t>> timings;
  1235. std::map<std::string, std::vector<uint64_t>> flops;
  1236. };
  1237. struct ggml_backend_vk_context {
  1238. std::string name;
  1239. vk_device device;
  1240. size_t semaphore_idx, event_idx;
  1241. ggml_vk_garbage_collector gc;
  1242. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k, prealloc_size_add_rms_partials, prealloc_size_add_rms_partials_offset;
  1243. vk_buffer prealloc_x, prealloc_y, prealloc_split_k, prealloc_add_rms_partials;
  1244. vk::Fence fence, almost_ready_fence;
  1245. bool almost_ready_fence_pending {};
  1246. // Set before op_add and unset after op_rms_norm to indicate that the add should
  1247. // write partial sums to accumulate the square of the vector components
  1248. bool do_add_rms_partials;
  1249. // Cache most recent tensor that was converted into prealloc_y, and what pipeline it used to convert.
  1250. vk_pipeline_struct * prealloc_y_last_pipeline_used {};
  1251. const ggml_tensor * prealloc_y_last_tensor_used {};
  1252. // Track which nodes have been used since the last sync, and whether they were written to
  1253. std::vector<const ggml_tensor *> unsynced_nodes_written;
  1254. std::vector<const ggml_tensor *> unsynced_nodes_read;
  1255. // Track which prealloc buffers have pending reads that need to be synchronized.
  1256. // These are checked before writing to the buffer (and call ggml_vk_sync_buffers if set),
  1257. // and set to true after the buffer contents are consumed.
  1258. bool prealloc_x_need_sync, prealloc_y_need_sync, prealloc_split_k_need_sync;
  1259. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  1260. vk_context_ref compute_ctx;
  1261. vk_context_ref transfer_ctx;
  1262. std::vector<vk_context_ref> tensor_ctxs;
  1263. std::vector<vk::DescriptorPool> descriptor_pools;
  1264. std::vector<vk::DescriptorSet> descriptor_sets;
  1265. uint32_t descriptor_set_idx {};
  1266. uint32_t pipeline_descriptor_set_requirements {};
  1267. vk_command_pool compute_cmd_pool;
  1268. vk_command_pool transfer_cmd_pool;
  1269. // number of additional consecutive nodes that are being fused with the
  1270. // node currently being processed
  1271. int num_additional_fused_ops {};
  1272. };
  1273. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  1274. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  1275. if (tensor->view_src) {
  1276. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  1277. }
  1278. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  1279. }
  1280. struct ggml_backend_vk_buffer_context {
  1281. vk_device_ref device;
  1282. vk_buffer dev_buffer;
  1283. std::string name;
  1284. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  1285. device(device),
  1286. dev_buffer(dev_buffer),
  1287. name(name) {
  1288. }
  1289. ~ggml_backend_vk_buffer_context() {
  1290. ggml_vk_destroy_buffer(dev_buffer);
  1291. }
  1292. };
  1293. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1294. static std::mutex log_mutex;
  1295. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  1296. std::lock_guard<std::mutex> guard(log_mutex);
  1297. vk_buffer buf = buf_ref.lock();
  1298. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1299. const std::string type = device ? "device" : "host";
  1300. allocations[buf->buffer] = size;
  1301. total_device += device ? size : 0;
  1302. total_host += device ? 0 : size;
  1303. 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));
  1304. }
  1305. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  1306. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  1307. return;
  1308. }
  1309. std::lock_guard<std::mutex> guard(log_mutex);
  1310. vk_buffer buf = buf_ref.lock();
  1311. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1312. std::string type = device ? "device" : "host";
  1313. auto it = allocations.find(buf->buffer);
  1314. total_device -= device ? it->second : 0;
  1315. total_host -= device ? 0 : it->second;
  1316. if (it != allocations.end()) {
  1317. 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));
  1318. allocations.erase(it);
  1319. } else {
  1320. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  1321. }
  1322. }
  1323. #endif // GGML_VULKAN_MEMORY_DEBUG
  1324. struct vk_instance_t {
  1325. vk::Instance instance;
  1326. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  1327. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  1328. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  1329. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  1330. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  1331. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  1332. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  1333. std::vector<size_t> device_indices;
  1334. std::vector<bool> device_supports_membudget;
  1335. vk_device devices[GGML_VK_MAX_DEVICES];
  1336. };
  1337. static bool vk_instance_initialized = false;
  1338. static vk_instance_t vk_instance;
  1339. static bool vk_perf_logger_enabled = false;
  1340. #ifdef GGML_VULKAN_CHECK_RESULTS
  1341. static size_t vk_skip_checks;
  1342. static size_t vk_output_tensor;
  1343. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  1344. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1345. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1346. #endif
  1347. 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);
  1348. static void ggml_backend_vk_free(ggml_backend_t backend);
  1349. static VkDeviceSize ggml_vk_get_max_buffer_range(const ggml_backend_vk_context * ctx, const vk_buffer &buf, const VkDeviceSize offset) {
  1350. const VkDeviceSize range = std::min(VkDeviceSize{buf->size - offset},
  1351. VkDeviceSize{ctx->device->properties.limits.maxStorageBufferRange});
  1352. return range;
  1353. }
  1354. // Wait for ctx->fence to be signaled.
  1355. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  1356. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  1357. // during this wait.
  1358. if (ctx->almost_ready_fence_pending) {
  1359. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  1360. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  1361. ctx->almost_ready_fence_pending = false;
  1362. }
  1363. // Spin (w/pause) waiting for the graph to finish executing.
  1364. vk::Result result;
  1365. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  1366. if (result != vk::Result::eNotReady) {
  1367. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  1368. exit(1);
  1369. }
  1370. for (uint32_t i = 0; i < 100; ++i) {
  1371. YIELD();
  1372. YIELD();
  1373. YIELD();
  1374. YIELD();
  1375. YIELD();
  1376. YIELD();
  1377. YIELD();
  1378. YIELD();
  1379. YIELD();
  1380. YIELD();
  1381. }
  1382. }
  1383. ctx->device->device.resetFences({ ctx->fence });
  1384. }
  1385. // variables to track number of compiles in progress
  1386. static uint32_t compile_count = 0;
  1387. static std::mutex compile_count_mutex;
  1388. static std::condition_variable compile_count_cond;
  1389. 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,
  1390. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  1391. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  1392. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  1393. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  1394. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  1395. GGML_ASSERT(parameter_count > 0);
  1396. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  1397. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  1398. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  1399. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  1400. vk::PushConstantRange pcr(
  1401. vk::ShaderStageFlagBits::eCompute,
  1402. 0,
  1403. pipeline->push_constant_size
  1404. );
  1405. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  1406. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  1407. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1408. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1409. specialization_entries[i].constantID = i;
  1410. specialization_entries[i].offset = i * sizeof(uint32_t);
  1411. specialization_entries[i].size = sizeof(uint32_t);
  1412. }
  1413. vk::SpecializationInfo specialization_info(
  1414. specialization_entries.size(),
  1415. specialization_entries.data(),
  1416. specialization_constants.size() * sizeof(uint32_t),
  1417. specialization_constants.data()
  1418. );
  1419. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1420. if (device->subgroup_require_full_support && require_full_subgroups) {
  1421. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1422. }
  1423. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1424. pipeline_shader_stage_create_flags,
  1425. vk::ShaderStageFlagBits::eCompute,
  1426. pipeline->shader_module,
  1427. entrypoint.c_str(),
  1428. &specialization_info);
  1429. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1430. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1431. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1432. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1433. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1434. }
  1435. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1436. device->pipeline_executable_properties_support ?
  1437. vk::PipelineCreateFlagBits::eCaptureStatisticsKHR :
  1438. vk::PipelineCreateFlags{},
  1439. pipeline_shader_create_info,
  1440. pipeline->layout);
  1441. vk::PipelineRobustnessCreateInfoEXT rci;
  1442. if (device->pipeline_robustness && disable_robustness) {
  1443. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1444. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1445. compute_pipeline_create_info.setPNext(&rci);
  1446. }
  1447. try {
  1448. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1449. } catch (const vk::SystemError& e) {
  1450. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1451. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1452. throw e;
  1453. }
  1454. pipeline->compiled = true;
  1455. if (vk_instance.debug_utils_support) {
  1456. vk::DebugUtilsObjectNameInfoEXT duoni;
  1457. duoni.objectType = vk::ObjectType::ePipeline;
  1458. duoni.pObjectName = pipeline->name.c_str();
  1459. duoni.objectHandle = /*reinterpret_cast*/(uint64_t)(static_cast<VkPipeline>(pipeline->pipeline));
  1460. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1461. }
  1462. if (device->pipeline_executable_properties_support) {
  1463. vk::PipelineExecutableInfoKHR executableInfo;
  1464. executableInfo.pipeline = pipeline->pipeline;
  1465. auto statistics = device->device.getPipelineExecutableStatisticsKHR(executableInfo);
  1466. for (auto & s : statistics) {
  1467. // "Register Count" is reported by NVIDIA drivers.
  1468. if (strcmp(s.name, "Register Count") == 0) {
  1469. VK_LOG_DEBUG(pipeline->name << " " << s.name << ": " << s.value.u64 << " registers");
  1470. pipeline->register_count = (uint32_t)s.value.u64;
  1471. }
  1472. }
  1473. }
  1474. {
  1475. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1476. device->all_pipelines.push_back(pipeline);
  1477. }
  1478. {
  1479. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1480. assert(compile_count > 0);
  1481. compile_count--;
  1482. }
  1483. compile_count_cond.notify_all();
  1484. }
  1485. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1486. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1487. device.destroyPipelineLayout(pipeline->layout);
  1488. device.destroyShaderModule(pipeline->shader_module);
  1489. device.destroyPipeline(pipeline->pipeline);
  1490. }
  1491. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1492. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1493. ctx->pipeline_descriptor_set_requirements += n;
  1494. if (!pipeline->compiled) {
  1495. pipeline->needed = true;
  1496. ctx->device->need_compiles = true;
  1497. }
  1498. }
  1499. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1500. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1501. // Enough descriptors are available
  1502. return;
  1503. }
  1504. vk_device& device = ctx->device;
  1505. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1506. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1507. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1508. while (to_alloc > 0) {
  1509. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1510. to_alloc -= alloc_count;
  1511. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1512. if (pool_idx >= ctx->descriptor_pools.size()) {
  1513. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1514. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1515. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1516. }
  1517. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1518. for (uint32_t i = 0; i < alloc_count; i++) {
  1519. layouts[i] = device->dsl;
  1520. }
  1521. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1522. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1523. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1524. pool_idx++;
  1525. }
  1526. }
  1527. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1528. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1529. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1530. // Reuse command buffer
  1531. return p.cmd_buffers[p.cmd_buffer_idx++];
  1532. }
  1533. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1534. p.pool,
  1535. vk::CommandBufferLevel::ePrimary,
  1536. 1);
  1537. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1538. auto buf = cmd_buffers.front();
  1539. p.cmd_buffers.push_back(buf);
  1540. p.cmd_buffer_idx++;
  1541. return buf;
  1542. }
  1543. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1544. if (ctx->seqs.empty()) {
  1545. if (fence) {
  1546. std::lock_guard<std::mutex> guard(queue_mutex);
  1547. ctx->p->q->queue.submit({}, fence);
  1548. }
  1549. return;
  1550. }
  1551. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1552. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1553. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1554. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1555. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1556. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1557. std::vector<vk::SubmitInfo> submit_infos;
  1558. int idx = -1;
  1559. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1560. size_t reserve = 0;
  1561. for (const auto& sequence : ctx->seqs) {
  1562. reserve += sequence.size();
  1563. }
  1564. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1565. tl_wait_semaphores.reserve(reserve);
  1566. tl_wait_vals.reserve(reserve);
  1567. tl_signal_semaphores.reserve(reserve);
  1568. tl_signal_vals.reserve(reserve);
  1569. tl_submit_infos.reserve(reserve);
  1570. submit_infos.reserve(reserve);
  1571. stage_flags.reserve(reserve);
  1572. for (const auto& sequence : ctx->seqs) {
  1573. for (const auto& submission : sequence) {
  1574. stage_flags.push_back({});
  1575. idx++;
  1576. tl_wait_vals.push_back({});
  1577. tl_wait_semaphores.push_back({});
  1578. tl_signal_vals.push_back({});
  1579. tl_signal_semaphores.push_back({});
  1580. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1581. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1582. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1583. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1584. }
  1585. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1586. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1587. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1588. }
  1589. tl_submit_infos.push_back({
  1590. (uint32_t) submission.wait_semaphores.size(),
  1591. tl_wait_vals[idx].data(),
  1592. (uint32_t) submission.signal_semaphores.size(),
  1593. tl_signal_vals[idx].data(),
  1594. });
  1595. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1596. tl_submit_infos[idx].pNext = nullptr;
  1597. vk::SubmitInfo si{
  1598. (uint32_t) submission.wait_semaphores.size(),
  1599. tl_wait_semaphores[idx].data(),
  1600. stage_flags[idx].data(),
  1601. 1,
  1602. &submission.buffer,
  1603. (uint32_t) submission.signal_semaphores.size(),
  1604. tl_signal_semaphores[idx].data(),
  1605. };
  1606. si.setPNext(&tl_submit_infos[idx]);
  1607. submit_infos.push_back(si);
  1608. }
  1609. }
  1610. std::lock_guard<std::mutex> guard(queue_mutex);
  1611. ctx->p->q->queue.submit(submit_infos, fence);
  1612. ctx->seqs.clear();
  1613. }
  1614. 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) {
  1615. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1616. const uint32_t qfsize = queue_family_props.size();
  1617. // Try with avoid preferences first
  1618. for (uint32_t i = 0; i < qfsize; i++) {
  1619. 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)) {
  1620. return i;
  1621. }
  1622. }
  1623. // Fall back to only required
  1624. for (size_t i = 0; i < qfsize; i++) {
  1625. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1626. return i;
  1627. }
  1628. }
  1629. // Fall back to reusing compute queue
  1630. for (size_t i = 0; i < qfsize; i++) {
  1631. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1632. return i;
  1633. }
  1634. }
  1635. // Fall back to ignoring min_num_queries
  1636. for (size_t i = 0; i < qfsize; i++) {
  1637. if (queue_family_props[i].queueFlags & required) {
  1638. return i;
  1639. }
  1640. }
  1641. // 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.
  1642. // 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.
  1643. if (compute_index >= 0) {
  1644. return compute_index;
  1645. }
  1646. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1647. for(auto &q_family : queue_family_props) {
  1648. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1649. }
  1650. abort();
  1651. }
  1652. 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) {
  1653. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1654. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1655. q.queue_family_index = queue_family_index;
  1656. q.transfer_only = transfer_only;
  1657. q.cmd_pool.init(device, &q);
  1658. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1659. q.stage_flags = stage_flags;
  1660. }
  1661. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1662. vk_context result = std::make_shared<vk_context_struct>();
  1663. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1664. ctx->gc.contexts.emplace_back(result);
  1665. result->p = &p;
  1666. return result;
  1667. }
  1668. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1669. vk_context result = std::make_shared<vk_context_struct>();
  1670. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1671. result->p = &p;
  1672. return result;
  1673. }
  1674. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1675. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1676. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1677. vk::SemaphoreCreateInfo ci{};
  1678. ci.setPNext(&tci);
  1679. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1680. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1681. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1682. }
  1683. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1684. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1685. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1686. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1687. vk::SemaphoreCreateInfo ci{};
  1688. ci.setPNext(&tci);
  1689. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1690. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1691. }
  1692. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1693. }
  1694. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1695. if (ctx->event_idx >= ctx->gc.events.size()) {
  1696. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1697. }
  1698. return ctx->gc.events[ctx->event_idx++];
  1699. }
  1700. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1701. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1702. // Requires command buffers to be done
  1703. device->device.resetCommandPool(p.pool);
  1704. p.cmd_buffer_idx = 0;
  1705. }
  1706. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1707. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1708. // Arbitrary frequency to cleanup/reuse command buffers
  1709. static constexpr uint32_t cleanup_frequency = 10;
  1710. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1711. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1712. }
  1713. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1714. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1715. }
  1716. }
  1717. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1718. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1719. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1720. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1721. (flags & memory_type.propertyFlags) == flags &&
  1722. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1723. return static_cast<int32_t>(i);
  1724. }
  1725. }
  1726. return UINT32_MAX;
  1727. }
  1728. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, const std::initializer_list<vk::MemoryPropertyFlags> & req_flags_list) {
  1729. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags_list.begin()[0]) << ", " << to_string(req_flags_list.begin()[req_flags_list.size()-1]) << ")");
  1730. if (size > device->max_buffer_size) {
  1731. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device buffer size limit");
  1732. }
  1733. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1734. if (size == 0) {
  1735. buf->size = 0;
  1736. return buf;
  1737. }
  1738. vk::BufferUsageFlags usage_flags = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst;
  1739. vk::MemoryAllocateFlags mem_flags {};
  1740. if (device->buffer_device_address) {
  1741. usage_flags |= vk::BufferUsageFlagBits::eShaderDeviceAddress;
  1742. mem_flags |= vk::MemoryAllocateFlagBits::eDeviceAddress;
  1743. }
  1744. vk::BufferCreateInfo buffer_create_info{
  1745. vk::BufferCreateFlags(),
  1746. size,
  1747. usage_flags,
  1748. vk::SharingMode::eExclusive,
  1749. 0,
  1750. nullptr,
  1751. };
  1752. buf->buffer = device->device.createBuffer(buffer_create_info);
  1753. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1754. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1755. const vk::MemoryAllocateFlagsInfo mem_flags_info { mem_flags };
  1756. for (auto it = req_flags_list.begin(); it != req_flags_list.end(); it++) {
  1757. const auto & req_flags = *it;
  1758. uint32_t memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1759. if (memory_type_index == UINT32_MAX) {
  1760. continue;
  1761. }
  1762. buf->memory_property_flags = req_flags;
  1763. try {
  1764. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index, &mem_flags_info });
  1765. break;
  1766. } catch (const vk::SystemError& e) {
  1767. // loop and retry
  1768. // during last attempt throw the exception
  1769. if (it + 1 == req_flags_list.end()) {
  1770. device->device.destroyBuffer(buf->buffer);
  1771. throw e;
  1772. }
  1773. }
  1774. }
  1775. if (!buf->device_memory) {
  1776. device->device.destroyBuffer(buf->buffer);
  1777. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1778. }
  1779. buf->ptr = nullptr;
  1780. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1781. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1782. }
  1783. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1784. buf->device = device;
  1785. buf->size = size;
  1786. if (device->buffer_device_address) {
  1787. const vk::BufferDeviceAddressInfo addressInfo(buf->buffer);
  1788. buf->bda_addr = device->device.getBufferAddress(addressInfo);
  1789. }
  1790. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1791. device->memory_logger->log_allocation(buf, size);
  1792. #endif
  1793. return buf;
  1794. }
  1795. 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)) {
  1796. try {
  1797. return ggml_vk_create_buffer(device, size, {req_flags, fallback_flags});
  1798. } catch (const vk::SystemError& e) {
  1799. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1800. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1801. throw e;
  1802. }
  1803. }
  1804. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1805. vk_buffer buf;
  1806. try {
  1807. if (device->prefer_host_memory) {
  1808. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1809. vk::MemoryPropertyFlagBits::eDeviceLocal});
  1810. } else if (device->uma) {
  1811. // Fall back to host memory type
  1812. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal,
  1813. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1814. } else if (device->disable_host_visible_vidmem) {
  1815. if (device->allow_sysmem_fallback) {
  1816. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal,
  1817. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1818. } else {
  1819. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  1820. }
  1821. } else {
  1822. // use rebar if available, otherwise fallback to device only visible memory
  1823. if (device->allow_sysmem_fallback) {
  1824. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1825. vk::MemoryPropertyFlagBits::eDeviceLocal,
  1826. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1827. } else {
  1828. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1829. vk::MemoryPropertyFlagBits::eDeviceLocal});
  1830. }
  1831. }
  1832. } catch (const vk::SystemError& e) {
  1833. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1834. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1835. throw e;
  1836. }
  1837. return buf;
  1838. }
  1839. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1840. if (buf == nullptr) {
  1841. return;
  1842. }
  1843. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1844. if (buf->device != nullptr) {
  1845. buf->device->memory_logger->log_deallocation(buf);
  1846. }
  1847. #endif
  1848. buf.reset();
  1849. }
  1850. static vk_subbuffer ggml_vk_subbuffer(const ggml_backend_vk_context* ctx, const vk_buffer& buf, size_t offset = 0) {
  1851. return { buf, offset, ggml_vk_get_max_buffer_range(ctx, buf, offset) };
  1852. }
  1853. static void ggml_vk_sync_buffers(ggml_backend_vk_context* ctx, vk_context& subctx) {
  1854. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1855. const bool transfer_queue = subctx->p->q->transfer_only;
  1856. if (ctx) {
  1857. ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false;
  1858. }
  1859. subctx->s->buffer.pipelineBarrier(
  1860. subctx->p->q->stage_flags,
  1861. subctx->p->q->stage_flags,
  1862. {},
  1863. { {
  1864. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1865. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1866. } },
  1867. {},
  1868. {}
  1869. );
  1870. }
  1871. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1872. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1873. if (events.empty()) {
  1874. return;
  1875. }
  1876. ctx->s->buffer.waitEvents(
  1877. events,
  1878. ctx->p->q->stage_flags,
  1879. ctx->p->q->stage_flags,
  1880. {},
  1881. {},
  1882. {}
  1883. );
  1884. }
  1885. // number of rows/cols for flash attention shader
  1886. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1887. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1888. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1889. if (hsv >= 192) {
  1890. return 2;
  1891. } else {
  1892. return 8;
  1893. }
  1894. }
  1895. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1896. // 128 threads split into four subgroups, each subgroup does 1/4
  1897. // of the Bc dimension.
  1898. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1899. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1900. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1901. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1902. if (path == FA_COOPMAT2) {
  1903. return flash_attention_num_small_rows;
  1904. } else {
  1905. return scalar_flash_attention_num_small_rows;
  1906. }
  1907. }
  1908. 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) {
  1909. GGML_UNUSED(clamp);
  1910. GGML_UNUSED(hsv);
  1911. if (path == FA_SCALAR) {
  1912. if (small_rows) {
  1913. return {scalar_flash_attention_num_small_rows, 64};
  1914. } else {
  1915. if ((hsv | hsk) & 8) {
  1916. // HSV/HSK not being a multiple of 16 makes D_split smaller, which makes cols_per_iter
  1917. // larger, and Bc needs to be >= cols_per_thread. 64 is large enough, 32 is not.
  1918. return {get_fa_scalar_num_large_rows(hsv), 64};
  1919. } else {
  1920. return {get_fa_scalar_num_large_rows(hsv), 32};
  1921. }
  1922. }
  1923. }
  1924. if (path == FA_COOPMAT1) {
  1925. if (small_rows) {
  1926. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1927. } else {
  1928. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1929. }
  1930. }
  1931. // small rows, large cols
  1932. if (small_rows) {
  1933. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1934. }
  1935. // small cols to reduce register count
  1936. if (ggml_is_quantized(type) || hsk >= 256 || hsv >= 256) {
  1937. if (hsk >= 512 || hsv >= 512) {
  1938. return {32, 32};
  1939. } else {
  1940. return {64, 32};
  1941. }
  1942. }
  1943. return {64, 64};
  1944. }
  1945. static uint32_t fa_align(FaCodePath path, uint32_t hsk, uint32_t hsv, ggml_type type, bool small_rows) {
  1946. return fa_rows_cols(path, hsk, hsv, 0, type, small_rows)[1];
  1947. }
  1948. 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) {
  1949. uint32_t lut_size = 0;
  1950. switch (src0_type) {
  1951. case GGML_TYPE_IQ1_S:
  1952. case GGML_TYPE_IQ1_M:
  1953. lut_size = 2*2048;
  1954. break;
  1955. case GGML_TYPE_IQ2_XXS:
  1956. lut_size = 8*256;
  1957. break;
  1958. case GGML_TYPE_IQ2_XS:
  1959. lut_size = 8*512;
  1960. break;
  1961. case GGML_TYPE_IQ2_S:
  1962. lut_size = 8*1024;
  1963. break;
  1964. case GGML_TYPE_IQ3_XXS:
  1965. lut_size = 4*256;
  1966. break;
  1967. case GGML_TYPE_IQ3_S:
  1968. lut_size = 4*512;
  1969. break;
  1970. case GGML_TYPE_IQ4_NL:
  1971. case GGML_TYPE_IQ4_XS:
  1972. case GGML_TYPE_MXFP4:
  1973. lut_size = 4*16;
  1974. break;
  1975. default:
  1976. break;
  1977. }
  1978. // Needs to be kept up to date on shader changes
  1979. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1980. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1981. const uint32_t warps = warptile[0] / warptile[10];
  1982. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1983. const uint32_t mmid_row_ids = mul_mat_id ? (warptile[2] * 2 * sizeof(uint16_t)) : 0;
  1984. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1985. const uint32_t ballots_sh = mul_mat_id ? (warps * 4 * sizeof(uint32_t)) : 0;
  1986. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size + ballots_sh;
  1987. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1988. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1989. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1990. return supported;
  1991. }
  1992. struct GpuPipelineConfig {
  1993. // GPU architecture identifier.
  1994. // Example: vk_device_architecture::AMD_GCN
  1995. vk_device_architecture arch;
  1996. // Mapping of pipeline names to their specific subgroup sizes.
  1997. // Example: {"soft_max_f32", 64}
  1998. std::unordered_map<std::string, uint32_t> pipelines;
  1999. // Default subgroup size for this GPU.
  2000. // Defaults to 0 if not explicitly provided.
  2001. uint32_t default_subgroup_size = 0;
  2002. };
  2003. // Pipeline configuration for RDNA1 GPUs.
  2004. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  2005. {"soft_max", 64}, {"im2col", 64},
  2006. {"argmax", 64}, {"mul_mat_vec", 64},
  2007. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  2008. };
  2009. // Pipeline configuration for RDNA2 GPUs.
  2010. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  2011. {"soft_max", 64}, {"im2col", 64},
  2012. };
  2013. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  2014. // Define configurations for different GPUs.
  2015. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  2016. {
  2017. vk_device_architecture::AMD_RDNA1,
  2018. {
  2019. rdna1_pipelines,
  2020. },
  2021. RDNA_DEFAULT_SUBGROUP_SIZE
  2022. },
  2023. {
  2024. vk_device_architecture::AMD_RDNA2,
  2025. {
  2026. rdna2_pipelines,
  2027. },
  2028. RDNA_DEFAULT_SUBGROUP_SIZE
  2029. },
  2030. };
  2031. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  2032. for (const auto &config : gpu_pipeline_configs) {
  2033. if (config.arch == arch) {
  2034. auto pipIt = config.pipelines.find(pipeline_name);
  2035. if (pipIt != config.pipelines.end()) {
  2036. return pipIt->second;
  2037. }
  2038. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  2039. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  2040. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  2041. for (const auto &entry : sorted_pipelines) {
  2042. if (pipeline_name.find(entry.first) != std::string::npos) {
  2043. return entry.second;
  2044. }
  2045. }
  2046. return config.default_subgroup_size;
  2047. }
  2048. }
  2049. return 0; // If no matching configuration is found
  2050. }
  2051. static void ggml_vk_load_shaders(vk_device& device) {
  2052. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  2053. // some shaders have a minimum subgroup size
  2054. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  2055. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  2056. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  2057. const uint32_t mul_mat_subgroup_size = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control) ? device->subgroup_min_size : device->subgroup_size;
  2058. const uint32_t mul_mat_subgroup_size_8 = std::max(mul_mat_subgroup_size, 8u);
  2059. const uint32_t mul_mat_subgroup_size_16 = std::max(mul_mat_subgroup_size, 16u);
  2060. const uint32_t mul_mat_subgroup_size_32 = std::max(mul_mat_subgroup_size, 32u);
  2061. const bool subgroup_min_size_16 = (!device->subgroup_size_control && device->subgroup_size >= 16) ||
  2062. (device->subgroup_size_control && device->subgroup_max_size >= 16);
  2063. // mulmat
  2064. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  2065. l_warptile_id, m_warptile_id, s_warptile_id,
  2066. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  2067. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  2068. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  2069. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  2070. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  2071. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  2072. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  2073. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  2074. uint32_t l_align, m_align, s_align;
  2075. if (device->coopmat2) {
  2076. // spec constants and tile sizes for non-quant matmul/matmul_id
  2077. l_warptile = { 256, 128, 256, 64, 1 };
  2078. m_warptile = { 256, 128, 128, 64, 0 };
  2079. s_warptile = { 128, 64, 64, 64, 0 };
  2080. l_wg_denoms = {128, 256, 1 };
  2081. m_wg_denoms = {128, 128, 1 };
  2082. s_wg_denoms = { 64, 64, 1 };
  2083. // spec constants and tile sizes for quant matmul (non-Qi_K)
  2084. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  2085. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  2086. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  2087. l_mmq_wg_denoms = { 128, 256, 1 };
  2088. m_mmq_wg_denoms = { 128, 128, 1 };
  2089. s_mmq_wg_denoms = { 32, 64, 1 };
  2090. // spec constants and tile sizes for quant matmul (Qi_K)
  2091. l_warptile_mmq_k = { 256, 128, 256, 64, 1 };
  2092. m_warptile_mmq_k = { 256, 128, 128, 64, 1 };
  2093. s_warptile_mmq_k = { 256, 32, 64, 128, 0 };
  2094. l_mmq_wg_denoms_k = { 128, 256, 1 };
  2095. m_mmq_wg_denoms_k = { 128, 128, 1 };
  2096. s_mmq_wg_denoms_k = { 32, 64, 1 };
  2097. // spec constants and tile sizes for quant matmul_id
  2098. l_warptile_mmqid = { 256, 128, 128, 16, 1, device->subgroup_size };
  2099. m_warptile_mmqid = { 256, 128, 64, 16, 0, device->subgroup_size };
  2100. s_warptile_mmqid = { 256, 128, 64, 16, 0, device->subgroup_size };
  2101. l_mmqid_wg_denoms = { 128, 128, 1 };
  2102. m_mmqid_wg_denoms = { 128, 64, 1 };
  2103. s_mmqid_wg_denoms = { 128, 64, 1 };
  2104. l_align = 128;
  2105. m_align = 64;
  2106. s_align = 32;
  2107. } else {
  2108. // Matrix cores require different warp group sizes
  2109. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  2110. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  2111. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  2112. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  2113. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  2114. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  2115. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  2116. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  2117. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  2118. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  2119. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  2120. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  2121. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  2122. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  2123. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  2124. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2125. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  2126. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  2127. l_warptile_id = { 128, 128, 128, 16, mul_mat_subgroup_size_16 * 2, 64, 2, tm_l, tn_l, tk_l, mul_mat_subgroup_size_16 };
  2128. m_warptile_id = { 128, 64, 64, 16, mul_mat_subgroup_size_16, 32, 2, tm_m, tn_m, tk_m, mul_mat_subgroup_size_16 };
  2129. s_warptile_id = { mul_mat_subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, mul_mat_subgroup_size_16 };
  2130. l_warptile_mmqid = { 128, 128, 128, 32, mul_mat_subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, mul_mat_subgroup_size_8 };
  2131. m_warptile_mmqid = { 128, 64, 64, 32, mul_mat_subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, mul_mat_subgroup_size_8 };
  2132. s_warptile_mmqid = { mul_mat_subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, mul_mat_subgroup_size_8 };
  2133. // chip specific tuning
  2134. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  2135. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  2136. m_warptile_mmqid = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  2137. }
  2138. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  2139. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  2140. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  2141. l_align = 128;
  2142. m_align = 64;
  2143. s_align = 32;
  2144. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2145. ggml_type t = (ggml_type)i;
  2146. // Disable medium and large matrix multiplication if not enough shared memory is available
  2147. // Check mmq warptiles as the largest configuration
  2148. // Throw an error if not enough for any matrix multiplication is available
  2149. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  2150. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  2151. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  2152. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  2153. device->mul_mat_m[i] = false;
  2154. device->mul_mat_l[i] = false;
  2155. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  2156. device->mul_mat_l[i] = false;
  2157. }
  2158. // Disable mul_mat_id if not enough shared memory is available
  2159. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmqid, true, t)) {
  2160. device->mul_mat_id_s[i] = false;
  2161. device->mul_mat_id_m[i] = false;
  2162. device->mul_mat_id_l[i] = false;
  2163. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmqid, true, t)) {
  2164. device->mul_mat_id_m[i] = false;
  2165. device->mul_mat_id_l[i] = false;
  2166. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmqid, true, t)) {
  2167. device->mul_mat_id_l[i] = false;
  2168. }
  2169. }
  2170. }
  2171. if (!device->pipeline_matmul_f32) {
  2172. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  2173. }
  2174. if (!device->pipeline_matmul_f32_f16) {
  2175. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  2176. }
  2177. if (!device->pipeline_matmul_id_f32) {
  2178. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  2179. }
  2180. if (!device->pipeline_matmul_bf16) {
  2181. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  2182. }
  2183. if (!device->pipeline_matmul_id_bf16) {
  2184. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  2185. }
  2186. std::vector<std::future<void>> compiles;
  2187. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const char *name, size_t spv_size, const void* spv_data, const char *entrypoint,
  2188. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  2189. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  2190. if (!require_full_subgroups && required_subgroup_size == 0) {
  2191. required_subgroup_size = get_subgroup_size(name, device->architecture);
  2192. }
  2193. if (!pipeline) {
  2194. pipeline = std::make_shared<vk_pipeline_struct>();
  2195. }
  2196. if (!pipeline->initialized) {
  2197. pipeline->name = name;
  2198. pipeline->parameter_count = parameter_count;
  2199. pipeline->push_constant_size = push_constant_size;
  2200. pipeline->wg_denoms = wg_denoms;
  2201. pipeline->align = align;
  2202. pipeline->initialized = true;
  2203. }
  2204. if (!pipeline->needed || pipeline->compiled) {
  2205. return;
  2206. }
  2207. {
  2208. // wait until fewer than N compiles are in progress
  2209. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  2210. std::unique_lock<std::mutex> guard(compile_count_mutex);
  2211. while (compile_count >= N) {
  2212. compile_count_cond.wait(guard);
  2213. }
  2214. compile_count++;
  2215. }
  2216. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  2217. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  2218. };
  2219. auto const &ggml_vk_create_pipeline2 = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const char *entrypoint,
  2220. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  2221. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  2222. return ggml_vk_create_pipeline(device, pipeline, name.c_str(), spv_size, spv_data, entrypoint,
  2223. parameter_count, push_constant_size, wg_denoms, specialization_constants,
  2224. align, disable_robustness, require_full_subgroups, required_subgroup_size);
  2225. };
  2226. 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> {
  2227. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  2228. };
  2229. 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> {
  2230. // For large number of rows, 128 invocations seems to work best.
  2231. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  2232. // can't use 256 for D==80.
  2233. // For scalar, use 128 (arbitrary)
  2234. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  2235. const uint32_t D = (hsk|hsv);
  2236. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  2237. ? scalar_flash_attention_workgroup_size
  2238. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  2239. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  2240. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  2241. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  2242. const uint32_t D_lsb = D ^ (D & (D-1));
  2243. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  2244. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  2245. };
  2246. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  2247. for (auto &fa : device->pipeline_flash_attn_f32_f16[TYPE]) { \
  2248. uint32_t HSK = fa.first.HSK; \
  2249. uint32_t HSV = fa.first.HSV; \
  2250. bool small_rows = fa.first.small_rows; \
  2251. FaCodePath path = fa.first.path; \
  2252. bool aligned = fa.first.aligned; \
  2253. bool f32acc = fa.first.f32acc; \
  2254. if (path == FAPATH) { \
  2255. if (aligned) { \
  2256. if (f32acc) { \
  2257. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_aligned_f32acc" #NAMELC, 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,small_rows), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_align(FAPATH,HSK,HSV,TYPE,small_rows), true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2258. } else { \
  2259. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_aligned_f16acc" #NAMELC, 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,small_rows), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_align(FAPATH,HSK,HSV,TYPE,small_rows), true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2260. } \
  2261. } else { \
  2262. if (f32acc) { \
  2263. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_f32acc" #NAMELC, 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,small_rows), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,small_rows), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2264. } else { \
  2265. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_f16acc" #NAMELC, 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,small_rows), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,small_rows), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2266. } \
  2267. } \
  2268. } \
  2269. }
  2270. CREATE_FA(GGML_TYPE_F32, f32, FA_SCALAR, )
  2271. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  2272. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  2273. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  2274. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2275. if (device->coopmat1_fa_support) {
  2276. CREATE_FA(GGML_TYPE_F32, f32, FA_COOPMAT1, _cm1)
  2277. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  2278. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  2279. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  2280. }
  2281. #endif
  2282. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2283. if (device->coopmat2) {
  2284. CREATE_FA(GGML_TYPE_F32, f32, FA_COOPMAT2, _cm2)
  2285. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  2286. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  2287. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  2288. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  2289. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  2290. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  2291. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  2292. }
  2293. #endif
  2294. #undef CREATE_FA
  2295. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2296. if (device->coopmat2) {
  2297. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2298. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2299. 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); \
  2300. 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); \
  2301. 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); \
  2302. 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); \
  2303. 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); \
  2304. 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); \
  2305. // Create 2 variants, {f16,f32} accumulator
  2306. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2307. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2308. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2309. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2310. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2311. if (device->coopmat_bf16_support) {
  2312. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2313. }
  2314. #endif
  2315. 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)
  2316. 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)
  2317. 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)
  2318. 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)
  2319. 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)
  2320. 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)
  2321. 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)
  2322. 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)
  2323. 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)
  2324. 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)
  2325. 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)
  2326. 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)
  2327. 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)
  2328. 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)
  2329. 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)
  2330. 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)
  2331. 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)
  2332. 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)
  2333. 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)
  2334. 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)
  2335. GGML_ASSERT(device->subgroup_ballot);
  2336. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2337. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2338. if (device->coopmat_bf16_support) {
  2339. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2340. }
  2341. #endif
  2342. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2343. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2344. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2345. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2346. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2347. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2348. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2349. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2350. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2351. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2352. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2353. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2354. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2355. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2356. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2357. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2358. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2359. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2360. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2361. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2362. #undef CREATE_MM
  2363. #undef CREATE_MM2
  2364. } else
  2365. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2366. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2367. if (device->coopmat_support) {
  2368. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2369. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2370. if (device->mul_mat ## ID ## _l[TYPE]) \
  2371. 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); \
  2372. if (device->mul_mat ## ID ## _m[TYPE]) \
  2373. 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); \
  2374. if (device->mul_mat ## ID ## _s[TYPE]) \
  2375. 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); \
  2376. if (device->mul_mat ## ID ## _l[TYPE]) \
  2377. 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); \
  2378. if (device->mul_mat ## ID ## _m[TYPE]) \
  2379. 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); \
  2380. if (device->mul_mat ## ID ## _s[TYPE]) \
  2381. 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); \
  2382. // Create 2 variants, {f16,f32} accumulator
  2383. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2384. if (device->coopmat_acc_f16_support) { \
  2385. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2386. } \
  2387. if (device->coopmat_acc_f32_support) { \
  2388. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2389. } \
  2390. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2391. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2392. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2393. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2394. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2395. if (device->coopmat_bf16_support) {
  2396. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  2397. }
  2398. #endif
  2399. if (device->coopmat_acc_f16_support) {
  2400. 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, );
  2401. 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, );
  2402. 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, );
  2403. 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, );
  2404. 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, );
  2405. 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, );
  2406. 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, );
  2407. 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, );
  2408. 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, );
  2409. 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, );
  2410. 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, );
  2411. 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, );
  2412. 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, );
  2413. 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, );
  2414. 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, );
  2415. 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, );
  2416. 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, );
  2417. 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, );
  2418. 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, );
  2419. 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, );
  2420. } else {
  2421. 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, );
  2422. 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, );
  2423. 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, );
  2424. 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, );
  2425. 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, );
  2426. 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, );
  2427. 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, );
  2428. 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, );
  2429. 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, );
  2430. 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, );
  2431. 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, );
  2432. 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, );
  2433. 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, );
  2434. 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, );
  2435. 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, );
  2436. 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, );
  2437. 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, );
  2438. 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, );
  2439. 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, );
  2440. 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, );
  2441. }
  2442. GGML_ASSERT(device->subgroup_ballot);
  2443. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2444. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2445. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_subgroup_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2446. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2447. if (device->coopmat_bf16_support) {
  2448. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2449. }
  2450. #endif
  2451. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2452. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2453. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2454. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2455. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2456. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2457. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2458. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2459. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2460. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2461. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2462. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2463. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2464. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2465. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2466. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2467. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2468. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2469. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2470. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2471. #undef CREATE_MM2
  2472. #undef CREATE_MM
  2473. } else
  2474. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2475. if (device->fp16) {
  2476. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2477. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2478. if (device->mul_mat ## ID ## _l[TYPE]) \
  2479. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2480. if (device->mul_mat ## ID ## _m[TYPE]) \
  2481. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2482. if (device->mul_mat ## ID ## _s[TYPE]) \
  2483. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2484. if (device->mul_mat ## ID ## _l[TYPE]) \
  2485. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2486. if (device->mul_mat ## ID ## _m[TYPE]) \
  2487. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2488. if (device->mul_mat ## ID ## _s[TYPE]) \
  2489. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2490. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2491. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2492. 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); \
  2493. 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); \
  2494. } \
  2495. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2496. 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); \
  2497. 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); \
  2498. } \
  2499. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2500. 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); \
  2501. 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); \
  2502. } \
  2503. // Create 2 variants, {f16,f32} accumulator
  2504. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2505. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2506. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2507. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2508. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2509. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2510. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2511. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2512. 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, , 0);
  2513. 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, , 0);
  2514. 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, , 0);
  2515. 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, , 0);
  2516. 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, , 0);
  2517. 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, , 0);
  2518. 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, , 0);
  2519. 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, , 0);
  2520. 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, , 0);
  2521. 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, , 0);
  2522. 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, , 0);
  2523. 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, , 0);
  2524. 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, , 0);
  2525. 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, , 0);
  2526. 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, , 0);
  2527. 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, , 0);
  2528. 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, , 0);
  2529. 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, , 0);
  2530. 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, , 0);
  2531. 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, , 0);
  2532. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2533. if (device->integer_dot_product) {
  2534. 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, );
  2535. 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, );
  2536. 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, );
  2537. 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, );
  2538. 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, );
  2539. }
  2540. #endif
  2541. if (device->subgroup_ballot && device->subgroup_require_full_support && subgroup_min_size_16) {
  2542. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2543. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2544. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_subgroup_f16_f32, wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2545. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile_id, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2546. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2547. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2548. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2549. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2550. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2551. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2552. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2553. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2554. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2555. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2556. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2557. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2558. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2559. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2560. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2561. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2562. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2563. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2564. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2565. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2566. } else {
  2567. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2568. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2569. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2570. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2571. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_q4_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2572. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_q4_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2573. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_q5_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2574. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_q5_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2575. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_q8_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2576. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_q2_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2577. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_q3_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2578. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_q4_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2579. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_q5_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2580. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_q6_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2581. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_iq1_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2582. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_iq1_m_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2583. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_iq2_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2584. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_iq2_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2585. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_iq2_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2586. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_iq3_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2587. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_iq3_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2588. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_iq4_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2589. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_iq4_nl_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2590. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_mxfp4_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2591. }
  2592. #undef CREATE_MM2
  2593. #undef CREATE_MMQ
  2594. #undef CREATE_MM
  2595. } else {
  2596. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2597. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2598. if (device->mul_mat ## ID ## _l[TYPE]) \
  2599. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2600. if (device->mul_mat ## ID ## _m[TYPE]) \
  2601. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2602. if (device->mul_mat ## ID ## _s[TYPE]) \
  2603. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2604. if (device->mul_mat ## ID ## _l[TYPE]) \
  2605. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2606. if (device->mul_mat ## ID ## _m[TYPE]) \
  2607. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2608. if (device->mul_mat ## ID ## _s[TYPE]) \
  2609. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2610. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2611. if (device->mul_mat ## ID ## _l[TYPE]) \
  2612. 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); \
  2613. if (device->mul_mat ## ID ## _m[TYPE]) \
  2614. 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); \
  2615. if (device->mul_mat ## ID ## _s[TYPE]) \
  2616. 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); \
  2617. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2618. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2619. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2620. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2621. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2622. 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, , 0);
  2623. 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, , 0);
  2624. 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, , 0);
  2625. 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, , 0);
  2626. 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, , 0);
  2627. 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, , 0);
  2628. 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, , 0);
  2629. 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, , 0);
  2630. 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, , 0);
  2631. 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, , 0);
  2632. 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, , 0);
  2633. 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, , 0);
  2634. 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, , 0);
  2635. 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, , 0);
  2636. 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, , 0);
  2637. 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, , 0);
  2638. 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, , 0);
  2639. 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, , 0);
  2640. 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, , 0);
  2641. 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, , 0);
  2642. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2643. if (device->integer_dot_product) {
  2644. 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, );
  2645. 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, );
  2646. 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, );
  2647. 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, );
  2648. 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, );
  2649. }
  2650. #endif
  2651. if (device->subgroup_ballot && device->subgroup_require_full_support && subgroup_min_size_16) {
  2652. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2653. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_subgroup_f16, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2654. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_subgroup_f16_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2655. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile_id, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2656. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_subgroup_q4_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2657. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_subgroup_q4_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2658. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_subgroup_q5_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2659. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_subgroup_q5_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2660. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_subgroup_q8_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2661. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_subgroup_q2_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2662. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_subgroup_q3_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2663. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_subgroup_q4_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2664. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_subgroup_q5_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2665. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_subgroup_q6_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2666. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_subgroup_iq1_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2667. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_subgroup_iq1_m_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2668. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_subgroup_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2669. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_subgroup_iq2_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2670. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_subgroup_iq2_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2671. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_subgroup_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2672. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_subgroup_iq3_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2673. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_subgroup_iq4_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2674. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_subgroup_iq4_nl_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2675. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_subgroup_mxfp4_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2676. } else {
  2677. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2678. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2679. 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, 0);
  2680. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2681. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2682. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2683. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2684. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2685. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2686. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2687. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2688. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2689. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2690. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2691. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2692. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2693. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2694. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2695. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2696. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2697. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2698. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2699. 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_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2700. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_mxfp4_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2701. }
  2702. }
  2703. // reusing CREATE_MM from the fp32 path
  2704. if ((device->coopmat2 || device->coopmat_support)
  2705. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2706. && !device->coopmat_bf16_support
  2707. #endif
  2708. ) {
  2709. // use scalar tile sizes
  2710. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2711. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2712. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2713. l_wg_denoms = {128, 128, 1 };
  2714. m_wg_denoms = { 64, 64, 1 };
  2715. s_wg_denoms = { 32, 32, 1 };
  2716. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2717. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2718. }
  2719. #undef CREATE_MM
  2720. // mul mat vec
  2721. // the number of rows computed per shader depends on GPU model and quant
  2722. uint32_t rm_stdq = 1;
  2723. uint32_t rm_kq = 2;
  2724. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2725. if (device->architecture == AMD_GCN) {
  2726. rm_stdq = 2;
  2727. rm_kq = 4;
  2728. }
  2729. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2730. rm_stdq = 2;
  2731. uint32_t rm_iq = 2 * rm_kq;
  2732. const bool use_subgroups = device->subgroup_arithmetic && device->architecture != vk_device_architecture::AMD_GCN;
  2733. // Ensure a subgroup size >= 16 is available
  2734. const bool use_subgroups16 = use_subgroups && subgroup_min_size_16;
  2735. const uint32_t subgroup_size = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control && device->subgroup_min_size <= 16 && device->subgroup_max_size >= 16) ? 16 : device->subgroup_size;
  2736. const uint32_t subgroup_size16 = std::max(subgroup_size, 16u);
  2737. const uint32_t force_subgroup_size = use_subgroups ? subgroup_size : 0;
  2738. const uint32_t force_subgroup_size16 = use_subgroups16 ? subgroup_size16 : 0;
  2739. for (uint32_t w = 0; w < DMMV_WG_SIZE_COUNT; ++w) {
  2740. const uint32_t wg_size_subgroup = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size : (subgroup_size * 4);
  2741. const uint32_t wg_size_subgroup16 = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size16 : (subgroup_size16 * 4);
  2742. const shader_reduction_mode reduc = (use_subgroups && w == DMMV_WG_SIZE_SUBGROUP) ? SHADER_REDUCTION_MODE_SUBGROUP :
  2743. (use_subgroups && w == DMMV_WG_SIZE_LARGE) ? SHADER_REDUCTION_MODE_HYBRID :
  2744. SHADER_REDUCTION_MODE_SHMEM;
  2745. const shader_reduction_mode reduc16 = (use_subgroups16 && w == DMMV_WG_SIZE_SUBGROUP) ? SHADER_REDUCTION_MODE_SUBGROUP :
  2746. (use_subgroups16 && w == DMMV_WG_SIZE_LARGE) ? SHADER_REDUCTION_MODE_HYBRID :
  2747. SHADER_REDUCTION_MODE_SHMEM;
  2748. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2749. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32", arr_dmmv_f32_f32_f32_len[reduc], arr_dmmv_f32_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2750. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32", arr_dmmv_f16_f32_f32_len[reduc], arr_dmmv_f16_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2751. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32", arr_dmmv_bf16_f32_f32_len[reduc], arr_dmmv_bf16_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2752. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32", arr_dmmv_q4_0_f32_f32_len[reduc], arr_dmmv_q4_0_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2753. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32", arr_dmmv_q4_1_f32_f32_len[reduc], arr_dmmv_q4_1_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2754. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32", arr_dmmv_q5_0_f32_f32_len[reduc], arr_dmmv_q5_0_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2755. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32", arr_dmmv_q5_1_f32_f32_len[reduc], arr_dmmv_q5_1_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2756. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32", arr_dmmv_q8_0_f32_f32_len[reduc], arr_dmmv_q8_0_f32_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2757. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32", arr_dmmv_q2_k_f32_f32_len[reduc16], arr_dmmv_q2_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2758. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32", arr_dmmv_q3_k_f32_f32_len[reduc16], arr_dmmv_q3_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2759. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32", arr_dmmv_q4_k_f32_f32_len[reduc16], arr_dmmv_q4_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2760. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32", arr_dmmv_q5_k_f32_f32_len[reduc16], arr_dmmv_q5_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2761. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32", arr_dmmv_q6_k_f32_f32_len[reduc16], arr_dmmv_q6_k_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2762. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32", arr_dmmv_iq1_s_f32_f32_len[reduc16], arr_dmmv_iq1_s_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2763. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32", arr_dmmv_iq1_m_f32_f32_len[reduc16], arr_dmmv_iq1_m_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2764. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32", arr_dmmv_iq2_xxs_f32_f32_len[reduc16], arr_dmmv_iq2_xxs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2765. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32", arr_dmmv_iq2_xs_f32_f32_len[reduc16], arr_dmmv_iq2_xs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2766. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32", arr_dmmv_iq2_s_f32_f32_len[reduc16], arr_dmmv_iq2_s_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2767. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32", arr_dmmv_iq3_xxs_f32_f32_len[reduc16], arr_dmmv_iq3_xxs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2768. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32", arr_dmmv_iq3_s_f32_f32_len[reduc16], arr_dmmv_iq3_s_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2769. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32", arr_dmmv_iq4_xs_f32_f32_len[reduc16], arr_dmmv_iq4_xs_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2770. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32", arr_dmmv_iq4_nl_f32_f32_len[reduc16], arr_dmmv_iq4_nl_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2771. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f32_f32", arr_dmmv_mxfp4_f32_f32_len[reduc16], arr_dmmv_mxfp4_f32_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2772. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32", arr_dmmv_f32_f16_f32_len[reduc], arr_dmmv_f32_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2773. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32", arr_dmmv_f16_f16_f32_len[reduc], arr_dmmv_f16_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2774. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32", arr_dmmv_bf16_f16_f32_len[reduc], arr_dmmv_bf16_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2775. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32", arr_dmmv_q4_0_f16_f32_len[reduc], arr_dmmv_q4_0_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2776. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32", arr_dmmv_q4_1_f16_f32_len[reduc], arr_dmmv_q4_1_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2777. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32", arr_dmmv_q5_0_f16_f32_len[reduc], arr_dmmv_q5_0_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2778. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32", arr_dmmv_q5_1_f16_f32_len[reduc], arr_dmmv_q5_1_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2779. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32", arr_dmmv_q8_0_f16_f32_len[reduc], arr_dmmv_q8_0_f16_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2780. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32", arr_dmmv_q2_k_f16_f32_len[reduc16], arr_dmmv_q2_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2781. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32", arr_dmmv_q3_k_f16_f32_len[reduc16], arr_dmmv_q3_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2782. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32", arr_dmmv_q4_k_f16_f32_len[reduc16], arr_dmmv_q4_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2783. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32", arr_dmmv_q5_k_f16_f32_len[reduc16], arr_dmmv_q5_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2784. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32", arr_dmmv_q6_k_f16_f32_len[reduc16], arr_dmmv_q6_k_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2785. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32", arr_dmmv_iq1_s_f16_f32_len[reduc16], arr_dmmv_iq1_s_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2786. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32", arr_dmmv_iq1_m_f16_f32_len[reduc16], arr_dmmv_iq1_m_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2787. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32", arr_dmmv_iq2_xxs_f16_f32_len[reduc16], arr_dmmv_iq2_xxs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2788. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32", arr_dmmv_iq2_xs_f16_f32_len[reduc16], arr_dmmv_iq2_xs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2789. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32", arr_dmmv_iq2_s_f16_f32_len[reduc16], arr_dmmv_iq2_s_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2790. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32", arr_dmmv_iq3_xxs_f16_f32_len[reduc16], arr_dmmv_iq3_xxs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2791. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32", arr_dmmv_iq3_s_f16_f32_len[reduc16], arr_dmmv_iq3_s_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2792. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32", arr_dmmv_iq4_xs_f16_f32_len[reduc16], arr_dmmv_iq4_xs_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2793. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32", arr_dmmv_iq4_nl_f16_f32_len[reduc16], arr_dmmv_iq4_nl_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2794. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f16_f32", arr_dmmv_mxfp4_f16_f32_len[reduc16], arr_dmmv_mxfp4_f16_f32_data[reduc16], "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2795. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2796. if (device->integer_dot_product) {
  2797. const uint32_t subgroup_size_int = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control) ? device->subgroup_min_size : device->subgroup_size;
  2798. const uint32_t wg_size_subgroup_int = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size_int : (subgroup_size_int * 4);
  2799. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_q8_1_f32", arr_dmmv_q4_0_q8_1_f32_len[reduc], arr_dmmv_q4_0_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2800. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_q8_1_f32", arr_dmmv_q4_1_q8_1_f32_len[reduc], arr_dmmv_q4_1_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2801. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_q8_1_f32", arr_dmmv_q5_0_q8_1_f32_len[reduc], arr_dmmv_q5_0_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2802. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_q8_1_f32", arr_dmmv_q5_1_q8_1_f32_len[reduc], arr_dmmv_q5_1_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2803. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_q8_1_f32", arr_dmmv_q8_0_q8_1_f32_len[reduc], arr_dmmv_q8_0_q8_1_f32_data[reduc], "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup_int, 1*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2804. }
  2805. #endif // GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT
  2806. }
  2807. }
  2808. 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);
  2809. 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);
  2810. 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);
  2811. 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);
  2812. 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);
  2813. 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);
  2814. 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);
  2815. 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);
  2816. 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);
  2817. 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);
  2818. 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);
  2819. 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);
  2820. 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);
  2821. 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);
  2822. 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);
  2823. 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);
  2824. 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);
  2825. 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);
  2826. 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);
  2827. 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);
  2828. 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);
  2829. 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);
  2830. 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);
  2831. // dequant shaders
  2832. 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);
  2833. 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);
  2834. 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);
  2835. 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);
  2836. 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);
  2837. 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);
  2838. 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);
  2839. 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);
  2840. 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);
  2841. 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);
  2842. 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);
  2843. 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);
  2844. 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);
  2845. 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);
  2846. 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);
  2847. 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);
  2848. 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);
  2849. 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);
  2850. 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);
  2851. 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);
  2852. 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);
  2853. // get_rows
  2854. 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);
  2855. 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);
  2856. 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);
  2857. 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);
  2858. 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);
  2859. 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);
  2860. 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);
  2861. 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);
  2862. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q2_K], "get_rows_q2_k", get_rows_q2_k_len, get_rows_q2_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2863. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q3_K], "get_rows_q3_k", get_rows_q3_k_len, get_rows_q3_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2864. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_K], "get_rows_q4_k", get_rows_q4_k_len, get_rows_q4_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2865. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_K], "get_rows_q5_k", get_rows_q5_k_len, get_rows_q5_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2866. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q6_K], "get_rows_q6_k", get_rows_q6_k_len, get_rows_q6_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2867. 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);
  2868. 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);
  2869. 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);
  2870. 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);
  2871. 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);
  2872. 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);
  2873. 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);
  2874. 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);
  2875. 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);
  2876. 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);
  2877. 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);
  2878. 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);
  2879. 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);
  2880. 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);
  2881. 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);
  2882. 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);
  2883. 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);
  2884. 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);
  2885. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q2_K], "get_rows_q2_k_f32", get_rows_q2_k_f32_len, get_rows_q2_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2886. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q3_K], "get_rows_q3_k_f32", get_rows_q3_k_f32_len, get_rows_q3_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2887. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_K], "get_rows_q4_k_f32", get_rows_q4_k_f32_len, get_rows_q4_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2888. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_K], "get_rows_q5_k_f32", get_rows_q5_k_f32_len, get_rows_q5_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2889. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q6_K], "get_rows_q6_k_f32", get_rows_q6_k_f32_len, get_rows_q6_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2890. 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);
  2891. 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);
  2892. 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);
  2893. 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);
  2894. 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);
  2895. 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);
  2896. 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);
  2897. 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);
  2898. 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);
  2899. 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);
  2900. 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);
  2901. 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);
  2902. if (device->subgroup_clustered && device->subgroup_require_full_support) {
  2903. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_subgroup_len, quantize_q8_1_subgroup_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1, true, true);
  2904. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1_x4, "quantize_q8_1_x4", quantize_q8_1_x4_subgroup_len, quantize_q8_1_x4_subgroup_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1, true, true);
  2905. } else {
  2906. 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);
  2907. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1_x4, "quantize_q8_1_x4", quantize_q8_1_x4_len, quantize_q8_1_x4_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  2908. }
  2909. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2910. if (device->subgroup_arithmetic && device->subgroup_require_full_support) {
  2911. ggml_vk_create_pipeline2(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);
  2912. } else {
  2913. ggml_vk_create_pipeline2(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);
  2914. }
  2915. }
  2916. 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);
  2917. 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);
  2918. 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);
  2919. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1, true);
  2920. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1, true);
  2921. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_partials_f32, "rms_norm_partials_f32", rms_norm_partials_f32_len, rms_norm_partials_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1, true);
  2922. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_partials_f32, "rms_norm_mul_partials_f32", rms_norm_partials_f32_len, rms_norm_partials_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1, true);
  2923. 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);
  2924. 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);
  2925. 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);
  2926. 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);
  2927. 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);
  2928. 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);
  2929. 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);
  2930. ggml_vk_create_pipeline(device, device->pipeline_cpy_i32_f32, "cpy_i32_f32", cpy_i32_f32_len, cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2931. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_i32, "cpy_f32_i32", cpy_f32_i32_len, cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2932. 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);
  2933. 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);
  2934. 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);
  2935. 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);
  2936. 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);
  2937. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_i32_f32, "contig_cpy_i32_f32", contig_cpy_i32_f32_len, contig_cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2938. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_i32, "contig_cpy_f32_i32", contig_cpy_f32_i32_len, contig_cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2939. if (device->float_controls_rte_fp16) {
  2940. 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);
  2941. 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);
  2942. 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);
  2943. 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);
  2944. 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);
  2945. 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);
  2946. } else {
  2947. 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);
  2948. 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);
  2949. 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);
  2950. 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);
  2951. 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);
  2952. 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);
  2953. }
  2954. #define SET_ROWS(itype, rte) \
  2955. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_F32], "set_rows_f32" #itype, set_rows_f32 ## itype ## rte ## _len, set_rows_f32 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2956. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_F16], "set_rows_f16" #itype, set_rows_f16 ## itype ## rte ## _len, set_rows_f16 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2957. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_BF16], "set_rows_bf16" #itype, set_rows_bf16 ## itype ## rte ## _len, set_rows_bf16 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2958. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q4_0], "set_rows_q4_0" #itype, set_rows_q4_0 ## itype ## rte ## _len, set_rows_q4_0 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2959. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q4_1], "set_rows_q4_1" #itype, set_rows_q4_1 ## itype ## rte ## _len, set_rows_q4_1 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2960. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q5_0], "set_rows_q5_0" #itype, set_rows_q5_0 ## itype ## rte ## _len, set_rows_q5_0 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2961. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q5_1], "set_rows_q5_1" #itype, set_rows_q5_1 ## itype ## rte ## _len, set_rows_q5_1 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2962. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q8_0], "set_rows_q8_0" #itype, set_rows_q8_0 ## itype ## rte ## _len, set_rows_q8_0 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  2963. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_IQ4_NL], "set_rows_iq4_nl" #itype, set_rows_iq4_nl ## itype ## rte ## _len, set_rows_iq4_nl ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2964. if (device->float_controls_rte_fp16) {
  2965. SET_ROWS(_i32, _rte)
  2966. SET_ROWS(_i64, _rte)
  2967. } else {
  2968. SET_ROWS(_i32, )
  2969. SET_ROWS(_i64, )
  2970. }
  2971. #undef SET_ROWS
  2972. 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);
  2973. 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);
  2974. 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);
  2975. 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);
  2976. 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);
  2977. 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);
  2978. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2979. std::string s;
  2980. s += std::string(src0_f16 ? "_f16" : "_f32");
  2981. s += std::string(src1_f16 ? "_f16" : "_f32");
  2982. s += std::string(dst_f16 ? "_f16" : "_f32");
  2983. return s;
  2984. };
  2985. bool rte = device->float_controls_rte_fp16;
  2986. #define CREATE_BINARY(name, namemod, spec, bindings) \
  2987. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2988. ggml_vk_create_pipeline2(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2989. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d][rte], name ## _data[s0][s1][d][rte], \
  2990. "main", (bindings), sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2991. CREATE_BINARY(add, , {0}, 4)
  2992. CREATE_BINARY(add, _norepeat, {1}, 4)
  2993. CREATE_BINARY(sub, , {0}, 3)
  2994. CREATE_BINARY(sub, _norepeat, {1}, 3)
  2995. CREATE_BINARY(mul, , {0}, 3)
  2996. CREATE_BINARY(mul, _norepeat, {1}, 3)
  2997. CREATE_BINARY(div, , {0}, 3)
  2998. CREATE_BINARY(div, _norepeat, {1}, 3)
  2999. CREATE_BINARY(add_rms, , {0}, 4)
  3000. CREATE_BINARY(add_rms, _norepeat, {1}, 4)
  3001. #undef CREATE_BINARY
  3002. if (device->multi_add) {
  3003. for (uint32_t i = 0; i < MAX_FUSED_ADDS; ++i) {
  3004. ggml_vk_create_pipeline2(device, device->pipeline_multi_add[i], "multi_add_f32_" + std::to_string(i+1), multi_add_f32_len, multi_add_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  3005. ggml_vk_create_pipeline2(device, device->pipeline_multi_add_rms[i], "multi_add_rms_f32_" + std::to_string(i+1), multi_add_rms_f32_len, multi_add_rms_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  3006. }
  3007. }
  3008. 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);
  3009. 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);
  3010. 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);
  3011. 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);
  3012. 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);
  3013. 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);
  3014. 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);
  3015. 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);
  3016. 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);
  3017. 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);
  3018. ggml_vk_create_pipeline(device, device->pipeline_sqrt_f32, "sqrt_f32", sqrt_f32_len, sqrt_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  3019. 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);
  3020. 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);
  3021. 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);
  3022. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_pad_push_constants), {512, 1, 1}, {}, 1);
  3023. 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);
  3024. 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);
  3025. 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);
  3026. #define CREATE_UNARY(name) \
  3027. 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); \
  3028. 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);
  3029. CREATE_UNARY(gelu)
  3030. CREATE_UNARY(gelu_erf)
  3031. CREATE_UNARY(gelu_quick)
  3032. CREATE_UNARY(silu)
  3033. CREATE_UNARY(relu)
  3034. CREATE_UNARY(tanh)
  3035. CREATE_UNARY(sigmoid)
  3036. CREATE_UNARY(hardsigmoid)
  3037. CREATE_UNARY(hardswish)
  3038. #undef CREATE_UNARY
  3039. #define CREATE_UNARY_RTE(name) \
  3040. if (device->float_controls_rte_fp16) { \
  3041. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32_rte", name ## _f32_rte_len, name ## _f32_rte_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  3042. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16_rte", name ## _f16_rte_len, name ## _f16_rte_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  3043. } else { \
  3044. 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); \
  3045. 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); \
  3046. }
  3047. CREATE_UNARY_RTE(exp)
  3048. #undef CREATE_UNARY_RTE
  3049. #define CREATE_GLU(name) \
  3050. if (device->float_controls_rte_fp16) { \
  3051. 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); \
  3052. 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); \
  3053. } else { \
  3054. 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); \
  3055. 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); \
  3056. }
  3057. CREATE_GLU(geglu)
  3058. CREATE_GLU(reglu)
  3059. CREATE_GLU(swiglu)
  3060. CREATE_GLU(swiglu_oai)
  3061. CREATE_GLU(geglu_erf)
  3062. CREATE_GLU(geglu_quick)
  3063. #undef CREATE_GLU
  3064. 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);
  3065. 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);
  3066. 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);
  3067. 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);
  3068. 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);
  3069. 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);
  3070. 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);
  3071. 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, true);
  3072. 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);
  3073. 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);
  3074. 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);
  3075. 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);
  3076. if (device->float_controls_rte_fp16) {
  3077. 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);
  3078. 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);
  3079. 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);
  3080. 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);
  3081. } else {
  3082. 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);
  3083. 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);
  3084. 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);
  3085. 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);
  3086. }
  3087. for (uint32_t i = 0; i < num_argsort_pipelines; ++i) {
  3088. ggml_vk_create_pipeline2(device, device->pipeline_argsort_f32[i], "argsort_f32_"+std::to_string(i), argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1u<<i, 1, 1}, {1u<<i, i}, 1, true);
  3089. }
  3090. 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);
  3091. 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_sum_rows_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  3092. 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);
  3093. #define IM2COL(bda) \
  3094. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32 ## bda ## _len, im2col_f32 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true); \
  3095. ggml_vk_create_pipeline(device, device->pipeline_im2col_3d_f32, "im2col_3d_f32", im2col_3d_f32 ## bda ## _len, im2col_3d_f32 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_3d_push_constants), {512, 1, 1}, { 512 }, 1, true); \
  3096. if (device->float_controls_rte_fp16) { \
  3097. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte ## bda ## _len, im2col_f32_f16_rte ## bda ## _data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true); \
  3098. ggml_vk_create_pipeline(device, device->pipeline_im2col_3d_f32_f16, "im2col_3d_f32_f16", im2col_3d_f32_f16_rte ## bda ## _len, im2col_3d_f32_f16_rte ## bda ## _data, "main", 2, sizeof(vk_op_im2col_3d_push_constants), {512, 1, 1}, { 512 }, 1, true); \
  3099. } else { \
  3100. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16 ## bda ## _len, im2col_f32_f16 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true); \
  3101. ggml_vk_create_pipeline(device, device->pipeline_im2col_3d_f32_f16, "im2col_3d_f32_f16", im2col_3d_f32_f16 ## bda ## _len, im2col_3d_f32_f16 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_3d_push_constants), {512, 1, 1}, { 512 }, 1, true); \
  3102. }
  3103. if (device->shader_int64 && device->buffer_device_address) {
  3104. IM2COL(_bda)
  3105. } else {
  3106. IM2COL()
  3107. }
  3108. 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);
  3109. 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);
  3110. 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);
  3111. 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);
  3112. 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);
  3113. if (device->subgroup_arithmetic && device->subgroup_require_full_support) {
  3114. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 16}, 1, true, true);
  3115. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 16}, 1, true, true);
  3116. } else {
  3117. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 16}, 1, true, true);
  3118. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 16}, 1, true, true);
  3119. }
  3120. ggml_vk_create_pipeline(device, device->pipeline_ssm_conv_f32, "ssm_conv_f32", ssm_conv_f32_len, ssm_conv_f32_data, "main", 3, sizeof(vk_op_ssm_conv_push_constants), {32, 1, 1}, {32}, 1);
  3121. 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);
  3122. ggml_vk_create_pipeline(device, device->pipeline_opt_step_sgd_f32, "opt_step_sgd_f32", opt_step_sgd_f32_len, opt_step_sgd_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  3123. // conv2d, conv_transpose_2d
  3124. for (uint32_t s = 0; s < CONV_SHAPE_COUNT; ++s) {
  3125. uint32_t conv2d_WG_SIZE = 256;
  3126. uint32_t conv2d_BS_K = 128;
  3127. uint32_t conv2d_BS_CRS = 16;
  3128. uint32_t use_collectives = 0; // Enables subgroup ops for preventing the re-calculation of indices.
  3129. uint32_t conv2d_BS_NPQ = 128;
  3130. uint32_t conv2d_TS_K = 8;
  3131. uint32_t conv2d_SHMEM_PAD = 4;
  3132. bool conv2d_UNROLL = true;
  3133. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3134. if (device->coopmat2) {
  3135. conv2d_SHMEM_PAD = 8; // 8 float16_t
  3136. }
  3137. #endif
  3138. if (device->vendor_id == VK_VENDOR_ID_INTEL) {
  3139. conv2d_SHMEM_PAD = 0;
  3140. conv2d_UNROLL = false;
  3141. } else if (device->vendor_id == VK_VENDOR_ID_AMD) {
  3142. conv2d_SHMEM_PAD = device->architecture == vk_device_architecture::AMD_GCN ? 1 : 4;
  3143. }
  3144. switch (s) {
  3145. default:
  3146. case CONV_SHAPE_128x128:
  3147. conv2d_BS_K = 128;
  3148. conv2d_BS_NPQ = 128;
  3149. conv2d_BS_CRS = 16;
  3150. if (device->vendor_id == VK_VENDOR_ID_AMD && device->architecture != vk_device_architecture::AMD_GCN) {
  3151. conv2d_UNROLL = false;
  3152. }
  3153. break;
  3154. case CONV_SHAPE_64x32:
  3155. conv2d_BS_K = 64;
  3156. conv2d_BS_NPQ = 32;
  3157. conv2d_BS_CRS = 32;
  3158. conv2d_TS_K = 4;
  3159. break;
  3160. case CONV_SHAPE_32x256:
  3161. conv2d_BS_K = 32;
  3162. conv2d_BS_NPQ = 256;
  3163. conv2d_BS_CRS = 16;
  3164. break;
  3165. }
  3166. // Use collectives on pre-Turing NVIDIA GPUs and GCN AMD cards, which had slower integer math.
  3167. bool allow_collectives_nv = device->vendor_id != VK_VENDOR_ID_NVIDIA ||
  3168. device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  3169. bool allow_collectives_amd = device->vendor_id != VK_VENDOR_ID_AMD ||
  3170. device->architecture == vk_device_architecture::AMD_GCN;
  3171. if (device->subgroup_shuffle &&
  3172. device->vendor_id != VK_VENDOR_ID_INTEL && // Do not enable collectives on Intel, see PR 14316.
  3173. allow_collectives_nv &&
  3174. allow_collectives_amd) {
  3175. use_collectives = 1;
  3176. conv2d_BS_CRS = std::min(
  3177. device->subgroup_size,
  3178. conv2d_BS_CRS); // CRS block size should be capped at subgroup size for correctness when shuffle is used.
  3179. }
  3180. uint32_t conv2d_shmem_req =
  3181. (conv2d_BS_K * (conv2d_BS_CRS + conv2d_SHMEM_PAD) + conv2d_BS_CRS * (conv2d_BS_NPQ + conv2d_SHMEM_PAD)) * sizeof(float);
  3182. if (device->properties.limits.maxComputeSharedMemorySize < conv2d_shmem_req) {
  3183. conv2d_BS_CRS = 8;
  3184. if (use_collectives) {
  3185. conv2d_BS_CRS = std::min(device->subgroup_size, conv2d_BS_CRS);
  3186. }
  3187. }
  3188. std::array<uint32_t, 3> wg_denoms = { conv2d_BS_K, conv2d_BS_NPQ, 1 };
  3189. 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 };
  3190. #define CREATE_CONV(name, type_suffix, spv_suffix) \
  3191. ggml_vk_create_pipeline( \
  3192. device, device->pipeline_##name##type_suffix[s], #name #type_suffix, \
  3193. name##type_suffix##spv_suffix##_len, name##type_suffix##spv_suffix##_data, "main", 3, \
  3194. sizeof(vk_op_##name##_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  3195. #define CREATE_CONVS(spv_suffix) \
  3196. CREATE_CONV(conv2d, _f32, spv_suffix) \
  3197. CREATE_CONV(conv2d, _f16_f32, spv_suffix) \
  3198. if (device->properties.limits.maxPushConstantsSize >= sizeof(vk_op_conv_transpose_2d_push_constants)) { \
  3199. CREATE_CONV(conv_transpose_2d, _f32, spv_suffix) \
  3200. CREATE_CONV(conv_transpose_2d, _f16_f32, spv_suffix) \
  3201. }
  3202. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3203. if (device->coopmat2) {
  3204. CREATE_CONVS(_cm2)
  3205. } else
  3206. #endif
  3207. if (conv2d_UNROLL) {
  3208. CREATE_CONVS(_unroll)
  3209. } else {
  3210. CREATE_CONVS( )
  3211. }
  3212. #undef CREATE_CONV
  3213. #undef CREATE_CONVS
  3214. }
  3215. 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);
  3216. 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);
  3217. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f16_f32, "conv2d_dw_whcn_f16_f32", conv2d_dw_whcn_f16_f32_len, conv2d_dw_whcn_f16_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  3218. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f16_f32, "conv2d_dw_cwhn_f16_f32", conv2d_dw_cwhn_f16_f32_len, conv2d_dw_cwhn_f16_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  3219. for (uint32_t i = 0; i < num_topk_moe_pipelines; ++i) {
  3220. ggml_vk_create_pipeline2(device, device->pipeline_topk_moe[i][0], "topk_moe_f32_"+std::to_string(i), topk_moe_f32_len, topk_moe_f32_data, "main", 3, sizeof(vk_op_topk_moe_push_constants), {1, 1, 1}, {device->subgroup_size, 1u<<i, 0}, 1, true, true);
  3221. ggml_vk_create_pipeline2(device, device->pipeline_topk_moe[i][1], "topk_moe_f32_"+std::to_string(i), topk_moe_f32_len, topk_moe_f32_data, "main", 3, sizeof(vk_op_topk_moe_push_constants), {1, 1, 1}, {device->subgroup_size, 1u<<i, 1}, 1, true, true);
  3222. }
  3223. for (auto &c : compiles) {
  3224. c.wait();
  3225. }
  3226. device->need_compiles = false;
  3227. }
  3228. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  3229. static vk_device ggml_vk_get_device(size_t idx) {
  3230. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  3231. if (vk_instance.devices[idx] == nullptr) {
  3232. VK_LOG_DEBUG("Initializing new vk_device");
  3233. vk_device device = std::make_shared<vk_device_struct>();
  3234. vk_instance.devices[idx] = device;
  3235. #ifdef GGML_VULKAN_MEMORY_DEBUG
  3236. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  3237. #endif
  3238. if (vk_perf_logger_enabled) {
  3239. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  3240. }
  3241. size_t dev_num = vk_instance.device_indices[idx];
  3242. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  3243. if (dev_num >= physical_devices.size()) {
  3244. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3245. throw std::runtime_error("Device not found");
  3246. }
  3247. device->physical_device = physical_devices[dev_num];
  3248. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  3249. device->architecture = get_device_architecture(device->physical_device);
  3250. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  3251. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  3252. const char* GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM = getenv("GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM");
  3253. device->disable_host_visible_vidmem = GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM != nullptr;
  3254. const char* GGML_VK_ALLOW_SYSMEM_FALLBACK = getenv("GGML_VK_ALLOW_SYSMEM_FALLBACK");
  3255. device->allow_sysmem_fallback = GGML_VK_ALLOW_SYSMEM_FALLBACK != nullptr;
  3256. const char* GGML_VK_DISABLE_GRAPH_OPTIMIZE = getenv("GGML_VK_DISABLE_GRAPH_OPTIMIZE");
  3257. device->disable_graph_optimize = GGML_VK_DISABLE_GRAPH_OPTIMIZE != nullptr;
  3258. bool fp16_storage = false;
  3259. bool fp16_compute = false;
  3260. bool maintenance4_support = false;
  3261. bool sm_builtins = false;
  3262. bool amd_shader_core_properties2 = false;
  3263. bool pipeline_robustness = false;
  3264. bool coopmat2_support = false;
  3265. bool pipeline_executable_properties_support = false;
  3266. device->coopmat_support = false;
  3267. device->integer_dot_product = false;
  3268. bool bfloat16_support = false;
  3269. for (const auto& properties : ext_props) {
  3270. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  3271. maintenance4_support = true;
  3272. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3273. fp16_storage = true;
  3274. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3275. fp16_compute = true;
  3276. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  3277. sm_builtins = true;
  3278. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  3279. amd_shader_core_properties2 = true;
  3280. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  3281. pipeline_robustness = true;
  3282. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  3283. device->subgroup_size_control = true;
  3284. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3285. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3286. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3287. device->coopmat_support = true;
  3288. device->coopmat_m = 0;
  3289. device->coopmat_n = 0;
  3290. device->coopmat_k = 0;
  3291. #endif
  3292. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3293. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3294. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3295. coopmat2_support = true;
  3296. #endif
  3297. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3298. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3299. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3300. device->integer_dot_product = true;
  3301. #endif
  3302. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3303. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3304. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3305. bfloat16_support = true;
  3306. #endif
  3307. } else if (strcmp("VK_KHR_pipeline_executable_properties", properties.extensionName) == 0) {
  3308. pipeline_executable_properties_support = true;
  3309. }
  3310. }
  3311. vk::PhysicalDeviceProperties2 props2;
  3312. vk::PhysicalDeviceMaintenance3Properties props3;
  3313. vk::PhysicalDeviceMaintenance4Properties props4;
  3314. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3315. vk::PhysicalDeviceDriverProperties driver_props;
  3316. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  3317. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  3318. vk::PhysicalDeviceVulkan11Properties vk11_props;
  3319. vk::PhysicalDeviceVulkan12Properties vk12_props;
  3320. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  3321. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3322. props2.pNext = &props3;
  3323. props3.pNext = &subgroup_props;
  3324. subgroup_props.pNext = &driver_props;
  3325. driver_props.pNext = &vk11_props;
  3326. vk11_props.pNext = &vk12_props;
  3327. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  3328. if (maintenance4_support) {
  3329. last_struct->pNext = (VkBaseOutStructure *)&props4;
  3330. last_struct = (VkBaseOutStructure *)&props4;
  3331. }
  3332. if (sm_builtins) {
  3333. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  3334. last_struct = (VkBaseOutStructure *)&sm_props;
  3335. }
  3336. if (amd_shader_core_properties2) {
  3337. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  3338. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  3339. }
  3340. if (device->subgroup_size_control) {
  3341. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  3342. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  3343. }
  3344. #if defined(VK_NV_cooperative_matrix2)
  3345. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  3346. if (coopmat2_support) {
  3347. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  3348. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  3349. }
  3350. #endif
  3351. if (device->integer_dot_product) {
  3352. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3353. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3354. }
  3355. device->physical_device.getProperties2(&props2);
  3356. device->properties = props2.properties;
  3357. device->vendor_id = device->properties.vendorID;
  3358. device->driver_id = driver_props.driverID;
  3359. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  3360. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  3361. device->max_memory_allocation_size = std::stoull(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  3362. } else if (maintenance4_support) {
  3363. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  3364. } else {
  3365. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  3366. }
  3367. const char* GGML_VK_FORCE_MAX_BUFFER_SIZE = getenv("GGML_VK_FORCE_MAX_BUFFER_SIZE");
  3368. if (GGML_VK_FORCE_MAX_BUFFER_SIZE != nullptr) {
  3369. device->max_buffer_size = std::stoull(GGML_VK_FORCE_MAX_BUFFER_SIZE);
  3370. } else if (maintenance4_support) {
  3371. device->max_buffer_size = props4.maxBufferSize;
  3372. } else {
  3373. device->max_buffer_size = device->max_memory_allocation_size;
  3374. }
  3375. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  3376. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  3377. device->suballocation_block_size = std::stoull(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  3378. } else {
  3379. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  3380. device->suballocation_block_size = 1024*1024*1024;
  3381. }
  3382. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  3383. device->subgroup_size = subgroup_props.subgroupSize;
  3384. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3385. if (sm_builtins) {
  3386. device->shader_core_count = sm_props.shaderSMCount;
  3387. } else if (amd_shader_core_properties2) {
  3388. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  3389. } else {
  3390. device->shader_core_count = 0;
  3391. }
  3392. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  3393. device->subgroup_arithmetic = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3394. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  3395. #ifdef __APPLE__
  3396. // Workaround for subgroup arithmetic failing on MoltenVK with AMD GPUs (issue 15846)
  3397. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  3398. device->subgroup_arithmetic = false;
  3399. }
  3400. #endif
  3401. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3402. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  3403. device->subgroup_clustered = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3404. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eClustered);
  3405. device->subgroup_ballot = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3406. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eBallot);
  3407. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  3408. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3409. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  3410. device->coopmat_support = false;
  3411. }
  3412. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  3413. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  3414. // Try to find a non-graphics compute queue and transfer-focused queues
  3415. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  3416. 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);
  3417. const float priorities[] = { 1.0f, 1.0f };
  3418. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  3419. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  3420. if (compute_queue_family_index != transfer_queue_family_index) {
  3421. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  3422. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  3423. } else if(!device->single_queue) {
  3424. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  3425. } else {
  3426. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  3427. }
  3428. vk::DeviceCreateInfo device_create_info;
  3429. std::vector<const char *> device_extensions;
  3430. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  3431. VkPhysicalDeviceFeatures2 device_features2;
  3432. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3433. device_features2.pNext = nullptr;
  3434. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  3435. VkPhysicalDeviceVulkan11Features vk11_features;
  3436. vk11_features.pNext = nullptr;
  3437. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3438. device_features2.pNext = &vk11_features;
  3439. VkPhysicalDeviceVulkan12Features vk12_features;
  3440. vk12_features.pNext = nullptr;
  3441. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3442. vk11_features.pNext = &vk12_features;
  3443. last_struct = (VkBaseOutStructure *)&vk12_features;
  3444. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  3445. pl_robustness_features.pNext = nullptr;
  3446. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  3447. pl_robustness_features.pipelineRobustness = VK_FALSE;
  3448. if (pipeline_robustness) {
  3449. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  3450. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  3451. device_extensions.push_back("VK_EXT_pipeline_robustness");
  3452. }
  3453. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  3454. subgroup_size_control_features.pNext = nullptr;
  3455. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  3456. subgroup_size_control_features.computeFullSubgroups = false;
  3457. subgroup_size_control_features.subgroupSizeControl = false;
  3458. if (device->subgroup_size_control) {
  3459. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  3460. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  3461. }
  3462. #if defined(VK_KHR_cooperative_matrix)
  3463. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3464. coopmat_features.pNext = nullptr;
  3465. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3466. coopmat_features.cooperativeMatrix = VK_FALSE;
  3467. if (device->coopmat_support) {
  3468. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3469. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3470. }
  3471. #endif
  3472. #if defined(VK_NV_cooperative_matrix2)
  3473. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  3474. coopmat2_features.pNext = nullptr;
  3475. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  3476. if (coopmat2_support) {
  3477. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  3478. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  3479. device_extensions.push_back("VK_NV_cooperative_matrix2");
  3480. }
  3481. #endif
  3482. #if defined(VK_KHR_shader_bfloat16)
  3483. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3484. bfloat16_features.pNext = nullptr;
  3485. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3486. if (bfloat16_support) {
  3487. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3488. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3489. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3490. }
  3491. #endif
  3492. VkPhysicalDeviceMaintenance4Features maint4_features {};
  3493. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  3494. if (maintenance4_support) {
  3495. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  3496. last_struct = (VkBaseOutStructure *)&maint4_features;
  3497. device_extensions.push_back("VK_KHR_maintenance4");
  3498. }
  3499. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3500. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3501. if (device->integer_dot_product) {
  3502. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3503. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3504. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  3505. }
  3506. VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR pep_features {};
  3507. pep_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR;
  3508. if (pipeline_executable_properties_support) {
  3509. last_struct->pNext = (VkBaseOutStructure *)&pep_features;
  3510. last_struct = (VkBaseOutStructure *)&pep_features;
  3511. device_extensions.push_back("VK_KHR_pipeline_executable_properties");
  3512. }
  3513. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  3514. device->pipeline_executable_properties_support = pipeline_executable_properties_support;
  3515. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  3516. #if defined(VK_KHR_shader_bfloat16)
  3517. device->bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3518. #else
  3519. device->bf16 = false;
  3520. #endif
  3521. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  3522. device->multi_add = vk12_props.shaderRoundingModeRTEFloat16 &&
  3523. device->properties.limits.maxPushConstantsSize >= sizeof(vk_op_multi_add_push_constants) &&
  3524. vk12_features.runtimeDescriptorArray &&
  3525. device->vendor_id != VK_VENDOR_ID_INTEL &&
  3526. getenv("GGML_VK_DISABLE_MULTI_ADD") == nullptr;
  3527. device->shader_int64 = device_features2.features.shaderInt64;
  3528. device->buffer_device_address = vk12_features.bufferDeviceAddress;
  3529. if (device->subgroup_size_control) {
  3530. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  3531. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  3532. device_extensions.push_back("VK_EXT_subgroup_size_control");
  3533. }
  3534. device->subgroup_size_control = device->subgroup_size_control &&
  3535. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  3536. subgroup_size_control_features.subgroupSizeControl;
  3537. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  3538. #if defined(VK_KHR_cooperative_matrix)
  3539. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  3540. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  3541. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  3542. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  3543. device->subgroup_max_size >= 32;
  3544. #endif
  3545. if (coopmat2_support) {
  3546. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3547. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  3548. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  3549. coopmat2_features.cooperativeMatrixReductions &&
  3550. coopmat2_features.cooperativeMatrixConversions &&
  3551. coopmat2_features.cooperativeMatrixPerElementOperations &&
  3552. coopmat2_features.cooperativeMatrixTensorAddressing &&
  3553. coopmat2_features.cooperativeMatrixBlockLoads &&
  3554. vk12_features.bufferDeviceAddress) {
  3555. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  3556. uint32_t count = 0;
  3557. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  3558. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  3559. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  3560. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  3561. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  3562. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  3563. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  3564. flexible_dimensions.resize(count, empty_prop);
  3565. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  3566. bool found_fp16_128 = false,
  3567. found_fp16_256 = false,
  3568. found_fp32_128 = false,
  3569. found_fp32_256 = false;
  3570. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  3571. // with 32x16x16 and 256 with 32x32x16.
  3572. for (auto &prop : flexible_dimensions) {
  3573. if (prop.saturatingAccumulation == VK_FALSE &&
  3574. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  3575. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3576. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3577. if (prop.workgroupInvocations == 128 &&
  3578. prop.MGranularity <= 32 &&
  3579. prop.NGranularity <= 16 &&
  3580. prop.KGranularity <= 16) {
  3581. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3582. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3583. found_fp16_128 = true;
  3584. }
  3585. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3586. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3587. found_fp32_128 = true;
  3588. }
  3589. }
  3590. if (prop.workgroupInvocations == 256 &&
  3591. prop.MGranularity <= 32 &&
  3592. prop.NGranularity <= 32 &&
  3593. prop.KGranularity <= 16) {
  3594. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3595. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3596. found_fp16_256 = true;
  3597. }
  3598. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3599. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3600. found_fp32_256 = true;
  3601. }
  3602. }
  3603. }
  3604. }
  3605. if (found_fp16_128 && found_fp16_256 &&
  3606. found_fp32_128 && found_fp32_256 &&
  3607. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  3608. device->coopmat2 = true;
  3609. }
  3610. }
  3611. #endif
  3612. }
  3613. if (!vk11_features.storageBuffer16BitAccess) {
  3614. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  3615. throw std::runtime_error("Unsupported device");
  3616. }
  3617. device_extensions.push_back("VK_KHR_16bit_storage");
  3618. #ifdef GGML_VULKAN_VALIDATE
  3619. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  3620. #endif
  3621. if (device->fp16) {
  3622. device_extensions.push_back("VK_KHR_shader_float16_int8");
  3623. }
  3624. #if defined(VK_KHR_cooperative_matrix)
  3625. if (device->coopmat_support) {
  3626. // Query supported shapes
  3627. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  3628. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  3629. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  3630. uint32_t cm_props_num;
  3631. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  3632. cm_props.resize(cm_props_num);
  3633. for (auto& prop : cm_props) {
  3634. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  3635. }
  3636. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  3637. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  3638. for (auto& prop : cm_props) {
  3639. 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));
  3640. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  3641. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  3642. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3643. ) {
  3644. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  3645. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  3646. // coopmat sizes not set yet
  3647. if (device->coopmat_m == 0) {
  3648. device->coopmat_acc_f32_support = true;
  3649. device->coopmat_m = prop.MSize;
  3650. device->coopmat_n = prop.NSize;
  3651. device->coopmat_k = prop.KSize;
  3652. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3653. // Only enable if shape is identical
  3654. device->coopmat_acc_f32_support = true;
  3655. }
  3656. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3657. device->coopmat_support_16x16x16_f32acc = true;
  3658. }
  3659. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  3660. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  3661. // coopmat sizes not set yet
  3662. if (device->coopmat_m == 0) {
  3663. device->coopmat_acc_f16_support = true;
  3664. device->coopmat_m = prop.MSize;
  3665. device->coopmat_n = prop.NSize;
  3666. device->coopmat_k = prop.KSize;
  3667. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3668. // Only enable if shape is identical
  3669. device->coopmat_acc_f16_support = true;
  3670. }
  3671. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3672. device->coopmat_support_16x16x16_f16acc = true;
  3673. }
  3674. }
  3675. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  3676. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  3677. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  3678. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  3679. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  3680. device->coopmat_int_m == 0
  3681. ) {
  3682. device->coopmat_int_support = true;
  3683. device->coopmat_int_m = prop.MSize;
  3684. device->coopmat_int_n = prop.NSize;
  3685. device->coopmat_int_k = prop.KSize;
  3686. }
  3687. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3688. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3689. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3690. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3691. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3692. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3693. ) {
  3694. // coopmat sizes not set yet
  3695. if (device->coopmat_m == 0) {
  3696. device->coopmat_bf16_support = true;
  3697. device->coopmat_m = prop.MSize;
  3698. device->coopmat_n = prop.NSize;
  3699. device->coopmat_k = prop.KSize;
  3700. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3701. // Only enable if shape is identical
  3702. device->coopmat_bf16_support = true;
  3703. }
  3704. }
  3705. #endif
  3706. }
  3707. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  3708. // No suitable matmul mode found
  3709. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  3710. device->coopmat_support = false;
  3711. }
  3712. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3713. device->coopmat_bf16_support = false;
  3714. }
  3715. }
  3716. if (device->coopmat_support) {
  3717. device_extensions.push_back("VK_KHR_cooperative_matrix");
  3718. }
  3719. #if defined(VK_KHR_shader_bfloat16)
  3720. if (device->coopmat_bf16_support) {
  3721. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3722. }
  3723. #endif
  3724. #endif
  3725. device->name = GGML_VK_NAME + std::to_string(idx);
  3726. device_create_info = {
  3727. vk::DeviceCreateFlags(),
  3728. device_queue_create_infos,
  3729. {},
  3730. device_extensions
  3731. };
  3732. device_create_info.setPNext(&device_features2);
  3733. device->device = device->physical_device.createDevice(device_create_info);
  3734. // Queues
  3735. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  3736. // Shaders
  3737. // Disable matmul tile sizes early if performance low or not supported
  3738. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3739. switch (device->vendor_id) {
  3740. #ifndef GGML_VULKAN_RUN_TESTS
  3741. case VK_VENDOR_ID_AMD:
  3742. case VK_VENDOR_ID_INTEL:
  3743. device->mul_mat_l[i] = false;
  3744. device->mul_mat_m[i] = true;
  3745. device->mul_mat_s[i] = true;
  3746. device->mul_mat_id_l[i] = false;
  3747. device->mul_mat_id_m[i] = true;
  3748. device->mul_mat_id_s[i] = true;
  3749. break;
  3750. case VK_VENDOR_ID_APPLE:
  3751. device->mul_mat_l[i] = false;
  3752. device->mul_mat_m[i] = true;
  3753. device->mul_mat_s[i] = false;
  3754. device->mul_mat_id_l[i] = false;
  3755. device->mul_mat_id_m[i] = true;
  3756. device->mul_mat_id_s[i] = false;
  3757. break;
  3758. #endif
  3759. default:
  3760. device->mul_mat_l[i] = true;
  3761. device->mul_mat_m[i] = true;
  3762. device->mul_mat_s[i] = true;
  3763. device->mul_mat_id_l[i] = true;
  3764. device->mul_mat_id_m[i] = true;
  3765. device->mul_mat_id_s[i] = true;
  3766. break;
  3767. }
  3768. }
  3769. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3770. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3771. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3772. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3773. dsl_binding_flags.push_back({});
  3774. }
  3775. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3776. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3777. {},
  3778. dsl_binding);
  3779. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3780. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3781. ggml_vk_load_shaders(device);
  3782. if (!device->single_queue) {
  3783. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3784. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3785. } else {
  3786. // TODO: Use pointer or reference to avoid copy
  3787. device->transfer_queue.copyFrom(device->compute_queue);
  3788. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3789. }
  3790. device->buffer_type = {
  3791. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3792. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3793. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3794. };
  3795. device->fence = device->device.createFence({});
  3796. device->idx = idx;
  3797. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3798. device->add_rms_fusion = !device->disable_fusion &&
  3799. device->subgroup_arithmetic &&
  3800. device->vendor_id != VK_VENDOR_ID_INTEL;
  3801. device->partials_binding_alignment =
  3802. std::max(4u, (uint32_t)device->properties.limits.minStorageBufferOffsetAlignment);
  3803. device->mmvq_mode = 0;
  3804. if (getenv("GGML_VK_DISABLE_MMVQ")) {
  3805. device->mmvq_mode = -1;
  3806. } else if (getenv("GGML_VK_FORCE_MMVQ")) {
  3807. device->mmvq_mode = 1;
  3808. }
  3809. return device;
  3810. }
  3811. return vk_instance.devices[idx];
  3812. }
  3813. static void ggml_vk_print_gpu_info(size_t idx) {
  3814. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3815. size_t dev_num = vk_instance.device_indices[idx];
  3816. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3817. GGML_ASSERT(vk_instance_initialized);
  3818. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3819. if (dev_num >= devices.size()) {
  3820. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3821. throw std::runtime_error("Device not found");
  3822. }
  3823. vk::PhysicalDevice physical_device = devices[dev_num];
  3824. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3825. bool fp16_storage = false;
  3826. bool fp16_compute = false;
  3827. bool coopmat_support = false;
  3828. bool coopmat2_support = false;
  3829. bool integer_dot_product = false;
  3830. bool bfloat16_support = false;
  3831. for (auto properties : ext_props) {
  3832. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3833. fp16_storage = true;
  3834. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3835. fp16_compute = true;
  3836. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3837. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3838. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3839. coopmat_support = true;
  3840. #endif
  3841. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3842. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3843. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3844. coopmat2_support = true;
  3845. #endif
  3846. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3847. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3848. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3849. integer_dot_product = true;
  3850. #endif
  3851. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3852. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3853. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3854. bfloat16_support = true;
  3855. #endif
  3856. }
  3857. }
  3858. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3859. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3860. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3861. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3862. vk::PhysicalDeviceProperties2 props2;
  3863. vk::PhysicalDeviceMaintenance3Properties props3;
  3864. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3865. vk::PhysicalDeviceDriverProperties driver_props;
  3866. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3867. props2.pNext = &props3;
  3868. props3.pNext = &subgroup_props;
  3869. subgroup_props.pNext = &driver_props;
  3870. // Pointer to the last chain element
  3871. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3872. if (integer_dot_product) {
  3873. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3874. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3875. }
  3876. physical_device.getProperties2(&props2);
  3877. VkPhysicalDeviceFeatures2 device_features2;
  3878. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3879. device_features2.pNext = nullptr;
  3880. VkPhysicalDeviceVulkan11Features vk11_features;
  3881. vk11_features.pNext = nullptr;
  3882. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3883. device_features2.pNext = &vk11_features;
  3884. VkPhysicalDeviceVulkan12Features vk12_features;
  3885. vk12_features.pNext = nullptr;
  3886. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3887. vk11_features.pNext = &vk12_features;
  3888. // Pointer to the last chain element
  3889. last_struct = (VkBaseOutStructure *)&vk12_features;
  3890. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3891. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3892. coopmat_features.pNext = nullptr;
  3893. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3894. coopmat_features.cooperativeMatrix = VK_FALSE;
  3895. if (coopmat_support) {
  3896. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3897. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3898. }
  3899. #endif
  3900. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3901. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3902. if (integer_dot_product) {
  3903. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3904. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3905. }
  3906. #if defined(VK_KHR_shader_bfloat16)
  3907. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3908. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3909. if (bfloat16_support) {
  3910. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3911. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3912. }
  3913. #endif
  3914. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3915. fp16 = fp16 && vk12_features.shaderFloat16;
  3916. #if defined(VK_KHR_shader_bfloat16)
  3917. bool bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3918. #else
  3919. bool bf16 = false;
  3920. #endif
  3921. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3922. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3923. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3924. integer_dot_product = integer_dot_product
  3925. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3926. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3927. coopmat_support = coopmat_support
  3928. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3929. && coopmat_features.cooperativeMatrix
  3930. #endif
  3931. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3932. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3933. std::string device_name = props2.properties.deviceName.data();
  3934. 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",
  3935. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, bf16, subgroup_size,
  3936. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3937. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3938. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3939. }
  3940. }
  3941. static bool ggml_vk_instance_validation_ext_available();
  3942. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3943. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3944. static bool ggml_vk_device_is_supported(const vk::PhysicalDevice & vkdev);
  3945. static DispatchLoaderDynamic ggml_vk_default_dispatcher_instance;
  3946. DispatchLoaderDynamic & ggml_vk_default_dispatcher() {
  3947. return ggml_vk_default_dispatcher_instance;
  3948. }
  3949. static void ggml_vk_instance_init() {
  3950. if (vk_instance_initialized) {
  3951. return;
  3952. }
  3953. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3954. // See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
  3955. ggml_vk_default_dispatcher_instance.init(vkGetInstanceProcAddr);
  3956. uint32_t api_version = vk::enumerateInstanceVersion();
  3957. if (api_version < VK_API_VERSION_1_2) {
  3958. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3959. throw vk::SystemError(vk::Result::eErrorFeatureNotPresent, "Vulkan 1.2 required");
  3960. }
  3961. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3962. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3963. const bool validation_ext = ggml_vk_instance_validation_ext_available();
  3964. #ifdef __APPLE__
  3965. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3966. #endif
  3967. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3968. std::vector<const char*> layers;
  3969. if (validation_ext) {
  3970. layers.push_back("VK_LAYER_KHRONOS_validation");
  3971. }
  3972. std::vector<const char*> extensions;
  3973. if (validation_ext) {
  3974. extensions.push_back("VK_EXT_validation_features");
  3975. }
  3976. #ifdef __APPLE__
  3977. if (portability_enumeration_ext) {
  3978. extensions.push_back("VK_KHR_portability_enumeration");
  3979. }
  3980. #endif
  3981. if (debug_utils_ext) {
  3982. extensions.push_back("VK_EXT_debug_utils");
  3983. }
  3984. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3985. #ifdef __APPLE__
  3986. if (portability_enumeration_ext) {
  3987. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3988. }
  3989. #endif
  3990. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3991. vk::ValidationFeaturesEXT validation_features;
  3992. if (validation_ext) {
  3993. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3994. validation_features = {
  3995. features_enable,
  3996. {},
  3997. };
  3998. validation_features.setPNext(nullptr);
  3999. instance_create_info.setPNext(&validation_features);
  4000. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  4001. }
  4002. vk_instance.instance = vk::createInstance(instance_create_info);
  4003. vk_instance_initialized = true;
  4004. if (debug_utils_ext) {
  4005. vk_instance.debug_utils_support = true;
  4006. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  4007. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  4008. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  4009. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  4010. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  4011. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  4012. }
  4013. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  4014. // See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
  4015. VULKAN_HPP_DEFAULT_DISPATCHER.init(vk_instance.instance);
  4016. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  4017. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  4018. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  4019. if (devices_env != nullptr) {
  4020. size_t num_available_devices = devices.size();
  4021. std::string devices(devices_env);
  4022. std::replace(devices.begin(), devices.end(), ',', ' ');
  4023. std::stringstream ss(devices);
  4024. size_t tmp;
  4025. while (ss >> tmp) {
  4026. if(tmp >= num_available_devices) {
  4027. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  4028. throw std::runtime_error("Invalid Vulkan device index");
  4029. }
  4030. vk_instance.device_indices.push_back(tmp);
  4031. }
  4032. } else {
  4033. // If no vulkan devices are found, return early
  4034. if (devices.empty()) {
  4035. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  4036. return;
  4037. }
  4038. // Default to using all dedicated GPUs
  4039. for (size_t i = 0; i < devices.size(); i++) {
  4040. vk::PhysicalDeviceProperties2 new_props;
  4041. vk::PhysicalDeviceDriverProperties new_driver;
  4042. vk::PhysicalDeviceIDProperties new_id;
  4043. new_props.pNext = &new_driver;
  4044. new_driver.pNext = &new_id;
  4045. devices[i].getProperties2(&new_props);
  4046. if ((new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu || new_props.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu) && ggml_vk_device_is_supported(devices[i])) {
  4047. // Check if there are two physical devices corresponding to the same GPU
  4048. auto old_device = std::find_if(
  4049. vk_instance.device_indices.begin(),
  4050. vk_instance.device_indices.end(),
  4051. [&devices, &new_id](const size_t k){
  4052. vk::PhysicalDeviceProperties2 old_props;
  4053. vk::PhysicalDeviceIDProperties old_id;
  4054. old_props.pNext = &old_id;
  4055. devices[k].getProperties2(&old_props);
  4056. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  4057. }
  4058. );
  4059. if (old_device == vk_instance.device_indices.end()) {
  4060. vk_instance.device_indices.push_back(i);
  4061. } else {
  4062. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  4063. // This can cause error when splitting layers aross the devices, need to keep only 1
  4064. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  4065. vk::PhysicalDeviceProperties2 old_props;
  4066. vk::PhysicalDeviceDriverProperties old_driver;
  4067. old_props.pNext = &old_driver;
  4068. devices[*old_device].getProperties2(&old_props);
  4069. std::map<vk::DriverId, int> driver_priorities {};
  4070. int old_priority = std::numeric_limits<int>::max();
  4071. int new_priority = std::numeric_limits<int>::max();
  4072. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  4073. // Smaller number -> higher priority
  4074. switch (old_props.properties.vendorID) {
  4075. case VK_VENDOR_ID_AMD:
  4076. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  4077. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  4078. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  4079. break;
  4080. case VK_VENDOR_ID_INTEL:
  4081. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  4082. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  4083. break;
  4084. case VK_VENDOR_ID_NVIDIA:
  4085. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  4086. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  4087. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  4088. #endif
  4089. break;
  4090. }
  4091. if (driver_priorities.count(old_driver.driverID)) {
  4092. old_priority = driver_priorities[old_driver.driverID];
  4093. }
  4094. if (driver_priorities.count(new_driver.driverID)) {
  4095. new_priority = driver_priorities[new_driver.driverID];
  4096. }
  4097. if (new_priority < old_priority) {
  4098. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  4099. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  4100. vk_instance.device_indices.push_back(i);
  4101. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  4102. }
  4103. else {
  4104. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  4105. }
  4106. }
  4107. }
  4108. }
  4109. // If no GPUs found, fall back to the first non-CPU device.
  4110. // If only CPU devices are available, return without devices.
  4111. if (vk_instance.device_indices.empty()) {
  4112. for (size_t i = 0; i < devices.size(); i++) {
  4113. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  4114. vk_instance.device_indices.push_back(i);
  4115. break;
  4116. }
  4117. }
  4118. }
  4119. if (vk_instance.device_indices.empty()) {
  4120. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  4121. return;
  4122. }
  4123. }
  4124. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  4125. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  4126. vk::PhysicalDevice vkdev = devices[vk_instance.device_indices[i]];
  4127. std::vector<vk::ExtensionProperties> extensionprops = vkdev.enumerateDeviceExtensionProperties();
  4128. bool membudget_supported = false;
  4129. for (const auto & ext : extensionprops) {
  4130. if (strcmp(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, ext.extensionName) == 0) {
  4131. membudget_supported = true;
  4132. break;
  4133. }
  4134. }
  4135. vk_instance.device_supports_membudget.push_back(membudget_supported);
  4136. ggml_vk_print_gpu_info(i);
  4137. }
  4138. }
  4139. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  4140. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  4141. ggml_vk_instance_init();
  4142. GGML_ASSERT(idx < vk_instance.device_indices.size());
  4143. ctx->name = GGML_VK_NAME + std::to_string(idx);
  4144. ctx->device = ggml_vk_get_device(idx);
  4145. ctx->semaphore_idx = 0;
  4146. ctx->event_idx = 0;
  4147. ctx->prealloc_size_x = 0;
  4148. ctx->prealloc_size_y = 0;
  4149. ctx->prealloc_size_split_k = 0;
  4150. ctx->fence = ctx->device->device.createFence({});
  4151. ctx->almost_ready_fence = ctx->device->device.createFence({});
  4152. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  4153. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  4154. #ifdef GGML_VULKAN_CHECK_RESULTS
  4155. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  4156. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  4157. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  4158. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  4159. #endif
  4160. }
  4161. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  4162. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  4163. switch (type) {
  4164. case GGML_TYPE_F32:
  4165. case GGML_TYPE_Q4_0:
  4166. case GGML_TYPE_Q4_1:
  4167. case GGML_TYPE_Q5_0:
  4168. case GGML_TYPE_Q5_1:
  4169. case GGML_TYPE_Q8_0:
  4170. case GGML_TYPE_Q2_K:
  4171. case GGML_TYPE_Q3_K:
  4172. case GGML_TYPE_Q4_K:
  4173. case GGML_TYPE_Q5_K:
  4174. case GGML_TYPE_Q6_K:
  4175. case GGML_TYPE_IQ1_S:
  4176. case GGML_TYPE_IQ1_M:
  4177. case GGML_TYPE_IQ2_XXS:
  4178. case GGML_TYPE_IQ2_XS:
  4179. case GGML_TYPE_IQ2_S:
  4180. case GGML_TYPE_IQ3_XXS:
  4181. case GGML_TYPE_IQ3_S:
  4182. case GGML_TYPE_IQ4_XS:
  4183. case GGML_TYPE_IQ4_NL:
  4184. case GGML_TYPE_MXFP4:
  4185. break;
  4186. default:
  4187. return nullptr;
  4188. }
  4189. return ctx->device->pipeline_dequant[type];
  4190. }
  4191. 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) {
  4192. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  4193. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4194. return ctx->device->pipeline_matmul_f32;
  4195. }
  4196. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  4197. return ctx->device->pipeline_matmul_f32_f16;
  4198. }
  4199. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  4200. return ctx->device->pipeline_matmul_bf16;
  4201. }
  4202. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  4203. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4204. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  4205. }
  4206. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4207. return ctx->device->pipeline_matmul_f16.f16acc;
  4208. }
  4209. } else {
  4210. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4211. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  4212. }
  4213. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4214. return ctx->device->pipeline_matmul_f16.f32acc;
  4215. }
  4216. }
  4217. // MMQ
  4218. if (src1_type == GGML_TYPE_Q8_1) {
  4219. 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;
  4220. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  4221. return nullptr;
  4222. }
  4223. return pipelines;
  4224. }
  4225. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  4226. return nullptr;
  4227. }
  4228. switch (src0_type) {
  4229. case GGML_TYPE_Q4_0:
  4230. case GGML_TYPE_Q4_1:
  4231. case GGML_TYPE_Q5_0:
  4232. case GGML_TYPE_Q5_1:
  4233. case GGML_TYPE_Q8_0:
  4234. case GGML_TYPE_Q2_K:
  4235. case GGML_TYPE_Q3_K:
  4236. case GGML_TYPE_Q4_K:
  4237. case GGML_TYPE_Q5_K:
  4238. case GGML_TYPE_Q6_K:
  4239. case GGML_TYPE_IQ1_S:
  4240. case GGML_TYPE_IQ1_M:
  4241. case GGML_TYPE_IQ2_XXS:
  4242. case GGML_TYPE_IQ2_XS:
  4243. case GGML_TYPE_IQ2_S:
  4244. case GGML_TYPE_IQ3_XXS:
  4245. case GGML_TYPE_IQ3_S:
  4246. case GGML_TYPE_IQ4_XS:
  4247. case GGML_TYPE_IQ4_NL:
  4248. case GGML_TYPE_MXFP4:
  4249. break;
  4250. default:
  4251. return nullptr;
  4252. }
  4253. if (ctx->device->coopmat2) {
  4254. assert(src1_type == GGML_TYPE_F16);
  4255. 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;
  4256. }
  4257. if (ctx->device->coopmat_support) {
  4258. 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;
  4259. }
  4260. 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;
  4261. }
  4262. 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, uint32_t m, uint32_t k) {
  4263. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  4264. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16 || b_type == GGML_TYPE_Q8_1);
  4265. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  4266. if (b_type == GGML_TYPE_Q8_1) {
  4267. switch (a_type) {
  4268. case GGML_TYPE_Q4_0:
  4269. case GGML_TYPE_Q4_1:
  4270. case GGML_TYPE_Q5_0:
  4271. case GGML_TYPE_Q5_1:
  4272. case GGML_TYPE_Q8_0:
  4273. break;
  4274. default:
  4275. return nullptr;
  4276. }
  4277. }
  4278. switch (a_type) {
  4279. case GGML_TYPE_F32:
  4280. case GGML_TYPE_F16:
  4281. case GGML_TYPE_BF16:
  4282. case GGML_TYPE_Q4_0:
  4283. case GGML_TYPE_Q4_1:
  4284. case GGML_TYPE_Q5_0:
  4285. case GGML_TYPE_Q5_1:
  4286. case GGML_TYPE_Q8_0:
  4287. case GGML_TYPE_Q2_K:
  4288. case GGML_TYPE_Q3_K:
  4289. case GGML_TYPE_Q4_K:
  4290. case GGML_TYPE_Q5_K:
  4291. case GGML_TYPE_Q6_K:
  4292. case GGML_TYPE_IQ1_S:
  4293. case GGML_TYPE_IQ1_M:
  4294. case GGML_TYPE_IQ2_XXS:
  4295. case GGML_TYPE_IQ2_XS:
  4296. case GGML_TYPE_IQ2_S:
  4297. case GGML_TYPE_IQ3_XXS:
  4298. case GGML_TYPE_IQ3_S:
  4299. case GGML_TYPE_IQ4_XS:
  4300. case GGML_TYPE_IQ4_NL:
  4301. case GGML_TYPE_MXFP4:
  4302. break;
  4303. default:
  4304. return nullptr;
  4305. }
  4306. // heuristic to choose workgroup size
  4307. uint32_t dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  4308. if ((ctx->device->vendor_id == VK_VENDOR_ID_NVIDIA && ctx->device->architecture != vk_device_architecture::NVIDIA_PRE_TURING) || ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  4309. // Prefer larger workgroups when M is small, to spread the work out more
  4310. // and keep more SMs busy.
  4311. // q6_k seems to prefer small workgroup size even for "medium" values of M.
  4312. if (a_type == GGML_TYPE_Q6_K) {
  4313. if (m < 4096 && k >= 1024) {
  4314. dmmv_wg = DMMV_WG_SIZE_LARGE;
  4315. }
  4316. } else {
  4317. if (m <= 8192 && k >= 1024) {
  4318. dmmv_wg = DMMV_WG_SIZE_LARGE;
  4319. }
  4320. }
  4321. }
  4322. if (b_type == GGML_TYPE_Q8_1) {
  4323. if (ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  4324. dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  4325. }
  4326. return ctx->device->pipeline_dequant_mul_mat_vec_q8_1_f32[dmmv_wg][a_type][num_cols-1];
  4327. }
  4328. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[dmmv_wg][a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[dmmv_wg][a_type][num_cols-1];
  4329. }
  4330. 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) {
  4331. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  4332. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4333. return ctx->device->pipeline_matmul_id_f32;
  4334. }
  4335. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  4336. return ctx->device->pipeline_matmul_id_bf16;
  4337. }
  4338. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  4339. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4340. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  4341. }
  4342. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4343. return ctx->device->pipeline_matmul_id_f16.f16acc;
  4344. }
  4345. } else {
  4346. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4347. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  4348. }
  4349. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4350. return ctx->device->pipeline_matmul_id_f16.f32acc;
  4351. }
  4352. }
  4353. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  4354. switch (src0_type) {
  4355. case GGML_TYPE_Q4_0:
  4356. case GGML_TYPE_Q4_1:
  4357. case GGML_TYPE_Q5_0:
  4358. case GGML_TYPE_Q5_1:
  4359. case GGML_TYPE_Q8_0:
  4360. case GGML_TYPE_Q2_K:
  4361. case GGML_TYPE_Q3_K:
  4362. case GGML_TYPE_Q4_K:
  4363. case GGML_TYPE_Q5_K:
  4364. case GGML_TYPE_Q6_K:
  4365. case GGML_TYPE_IQ1_S:
  4366. case GGML_TYPE_IQ1_M:
  4367. case GGML_TYPE_IQ2_XXS:
  4368. case GGML_TYPE_IQ2_XS:
  4369. case GGML_TYPE_IQ2_S:
  4370. case GGML_TYPE_IQ3_XXS:
  4371. case GGML_TYPE_IQ3_S:
  4372. case GGML_TYPE_IQ4_XS:
  4373. case GGML_TYPE_IQ4_NL:
  4374. case GGML_TYPE_MXFP4:
  4375. break;
  4376. default:
  4377. return nullptr;
  4378. }
  4379. // XXX TODO 'prec' is not actually allowed in mul_mat_id.
  4380. bool prefer_fp16acc = ctx->device->fp16 /*&& prec == GGML_PREC_DEFAULT*/;
  4381. bool support_fp16acc = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc != nullptr;
  4382. bool support_fp32acc = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc != nullptr;
  4383. if (support_fp16acc && (prefer_fp16acc || !support_fp32acc)) {
  4384. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc;
  4385. } else {
  4386. GGML_ASSERT(support_fp32acc);
  4387. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  4388. }
  4389. }
  4390. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  4391. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec_id()");
  4392. GGML_ASSERT(b_type == GGML_TYPE_F32);
  4393. switch (a_type) {
  4394. case GGML_TYPE_F32:
  4395. case GGML_TYPE_F16:
  4396. case GGML_TYPE_BF16:
  4397. case GGML_TYPE_Q4_0:
  4398. case GGML_TYPE_Q4_1:
  4399. case GGML_TYPE_Q5_0:
  4400. case GGML_TYPE_Q5_1:
  4401. case GGML_TYPE_Q8_0:
  4402. case GGML_TYPE_Q2_K:
  4403. case GGML_TYPE_Q3_K:
  4404. case GGML_TYPE_Q4_K:
  4405. case GGML_TYPE_Q5_K:
  4406. case GGML_TYPE_Q6_K:
  4407. case GGML_TYPE_IQ1_S:
  4408. case GGML_TYPE_IQ1_M:
  4409. case GGML_TYPE_IQ2_XXS:
  4410. case GGML_TYPE_IQ2_XS:
  4411. case GGML_TYPE_IQ2_S:
  4412. case GGML_TYPE_IQ3_XXS:
  4413. case GGML_TYPE_IQ3_S:
  4414. case GGML_TYPE_IQ4_XS:
  4415. case GGML_TYPE_IQ4_NL:
  4416. case GGML_TYPE_MXFP4:
  4417. break;
  4418. default:
  4419. return nullptr;
  4420. }
  4421. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  4422. }
  4423. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  4424. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  4425. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  4426. int best_i = -1;
  4427. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  4428. int worst_i = -1;
  4429. size_t worst_size = 0; //largest unused buffer seen so far
  4430. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  4431. vk_buffer &b = ctx->buffer_pool[i];
  4432. if (b != nullptr && b->size >= size && b->size < best_size) {
  4433. best_i = i;
  4434. best_size = b->size;
  4435. }
  4436. if (b != nullptr && b->size > worst_size) {
  4437. worst_i = i;
  4438. worst_size = b->size;
  4439. }
  4440. }
  4441. if(best_i != -1) {
  4442. //found the smallest buffer that fits our needs
  4443. vk_buffer b = ctx->buffer_pool[best_i];
  4444. ctx->buffer_pool[best_i].reset();
  4445. return b;
  4446. }
  4447. if(worst_i != -1) {
  4448. //no buffer that fits our needs, resize largest one to save memory
  4449. vk_buffer& b = ctx->buffer_pool[worst_i];
  4450. ggml_vk_destroy_buffer(b);
  4451. }
  4452. return ggml_vk_create_buffer_device(ctx->device, size);
  4453. }
  4454. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  4455. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  4456. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  4457. vk_buffer& b = ctx->buffer_pool[i];
  4458. if (b == nullptr) {
  4459. b = buffer;
  4460. return;
  4461. }
  4462. }
  4463. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  4464. ggml_vk_destroy_buffer(buffer);
  4465. }
  4466. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  4467. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  4468. // Try to find existing temp buffer with enough capacity
  4469. for (auto& buffer : ctx->gc.temp_buffers) {
  4470. if (buffer->size >= size) {
  4471. return buffer;
  4472. }
  4473. }
  4474. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  4475. // Otherwise create new buffer
  4476. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  4477. ctx->gc.temp_buffers.push_back(buf);
  4478. return buf;
  4479. }
  4480. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  4481. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  4482. vk_buffer buf = ggml_vk_create_buffer(device, size,
  4483. {vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4484. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  4485. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  4486. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  4487. size/1024.0/1024.0);
  4488. device->device.freeMemory(buf->device_memory);
  4489. device->device.destroyBuffer(buf->buffer);
  4490. return nullptr;
  4491. }
  4492. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4493. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  4494. return buf->ptr;
  4495. }
  4496. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  4497. if (ptr == nullptr) {
  4498. return;
  4499. }
  4500. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  4501. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4502. vk_buffer buf;
  4503. size_t index;
  4504. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4505. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4506. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4507. if (ptr >= addr && ptr < endr) {
  4508. buf = std::get<2>(device->pinned_memory[i]);
  4509. index = i;
  4510. break;
  4511. }
  4512. }
  4513. if (buf == nullptr) {
  4514. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  4515. return;
  4516. }
  4517. ggml_vk_destroy_buffer(buf);
  4518. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  4519. }
  4520. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  4521. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4522. buf = nullptr;
  4523. buf_offset = 0;
  4524. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4525. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4526. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4527. if (ptr >= addr && ptr < endr) {
  4528. buf = std::get<2>(device->pinned_memory[i]);
  4529. buf_offset = ((const uint8_t *)ptr) - addr;
  4530. break;
  4531. }
  4532. }
  4533. }
  4534. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  4535. vk_submission s;
  4536. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  4537. if (one_time) {
  4538. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  4539. } else {
  4540. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  4541. }
  4542. return s;
  4543. }
  4544. template <typename T> size_t push_constant_size(const T &t) {
  4545. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4546. GGML_UNUSED(t);
  4547. return sizeof(T);
  4548. }
  4549. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  4550. GGML_UNUSED(t);
  4551. return sizeof(T) * t.size();
  4552. }
  4553. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  4554. GGML_UNUSED(t);
  4555. return sizeof(T) * N;
  4556. }
  4557. template <typename T> const T *push_constant_data(const T &t) {
  4558. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4559. return &t;
  4560. }
  4561. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  4562. return t.data();
  4563. }
  4564. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  4565. return t.data();
  4566. }
  4567. template <typename T>
  4568. 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) {
  4569. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  4570. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  4571. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  4572. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  4573. for (auto& buffer : descriptor_buffer_infos) {
  4574. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  4575. }
  4576. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  4577. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  4578. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  4579. GGML_ASSERT(pipeline->parameter_count == descriptor_buffer_infos.size());
  4580. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  4581. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  4582. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  4583. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  4584. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  4585. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  4586. pipeline->layout,
  4587. 0,
  4588. { descriptor_set },
  4589. {});
  4590. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  4591. }
  4592. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  4593. s.buffer.end();
  4594. s.wait_semaphores = std::move(wait_semaphores);
  4595. s.signal_semaphores = std::move(signal_semaphores);
  4596. }
  4597. static void ggml_vk_ctx_end(vk_context& ctx) {
  4598. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  4599. if (ctx->s == nullptr) {
  4600. return;
  4601. }
  4602. ctx->s->buffer.end();
  4603. ctx->s = nullptr;
  4604. }
  4605. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  4606. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  4607. if (subctx->s != nullptr) {
  4608. ggml_vk_ctx_end(subctx);
  4609. }
  4610. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  4611. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  4612. }
  4613. static size_t ggml_vk_align_size(size_t width, size_t align) {
  4614. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  4615. return CEIL_DIV(width, align) * align;
  4616. }
  4617. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  4618. if (memcpys == nullptr) {
  4619. memcpy(dst, src, size);
  4620. } else {
  4621. memcpys->emplace_back(dst, src, size);
  4622. }
  4623. }
  4624. static void deferred_memset(void * dst, uint32_t val, size_t size, std::vector<vk_staging_memset>* memsets = nullptr) {
  4625. if (memsets == nullptr) {
  4626. memset(dst, val, size);
  4627. } else {
  4628. memsets->emplace_back(dst, val, size);
  4629. }
  4630. }
  4631. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  4632. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  4633. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  4634. ggml_vk_destroy_buffer(device->sync_staging);
  4635. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  4636. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4637. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4638. }
  4639. }
  4640. 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) {
  4641. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  4642. GGML_ASSERT(!ggml_is_contiguous(tensor));
  4643. // Buffer is already mapped
  4644. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4645. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4646. GGML_ABORT("fatal error");
  4647. }
  4648. // Check if src is pinned memory
  4649. vk_buffer buf = nullptr;
  4650. size_t buf_offset = 0;
  4651. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  4652. const uint64_t ne0 = tensor->ne[0];
  4653. const uint64_t ne1 = tensor->ne[1];
  4654. const uint64_t ne2 = tensor->ne[2];
  4655. const uint64_t ne3 = tensor->ne[3];
  4656. const uint64_t nb0 = tensor->nb[0];
  4657. const uint64_t nb1 = tensor->nb[1];
  4658. const uint64_t nb2 = tensor->nb[2];
  4659. const uint64_t nb3 = tensor->nb[3];
  4660. const ggml_type type = tensor->type;
  4661. const uint64_t ts = ggml_type_size(type);
  4662. const uint64_t bs = ggml_blck_size(type);
  4663. const uint64_t dstnb0 = ts;
  4664. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  4665. const uint64_t dstnb2 = dstnb1*ne1;
  4666. const uint64_t dstnb3 = dstnb2*ne2;
  4667. const uint64_t ne = ggml_nelements(tensor);
  4668. if (buf != nullptr) {
  4669. // Memory is pinned, use as staging buffer
  4670. std::vector<vk::BufferCopy> slices;
  4671. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4672. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4673. // Find longest contiguous slice
  4674. if (ne1*nb1 == dstnb2) {
  4675. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  4676. } else {
  4677. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4678. if (ne0*nb0/bs == dstnb1) {
  4679. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  4680. } else {
  4681. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4682. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4683. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4684. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  4685. }
  4686. }
  4687. }
  4688. }
  4689. }
  4690. }
  4691. ggml_vk_sync_buffers(ctx, subctx);
  4692. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4693. return;
  4694. }
  4695. if (!sync_staging) {
  4696. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4697. }
  4698. // Staging buffer required
  4699. vk_buffer& staging = ctx->device->sync_staging;
  4700. const uint64_t copy_size = ts*ne/bs;
  4701. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  4702. VkBufferCopy buf_copy{ 0, offset, copy_size };
  4703. ggml_vk_sync_buffers(ctx, subctx);
  4704. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4705. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4706. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4707. // Find longest contiguous slice
  4708. if (ne1*nb1 == dstnb2) {
  4709. 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);
  4710. } else {
  4711. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4712. if (ne0*nb0/bs == dstnb1) {
  4713. 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);
  4714. } else {
  4715. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4716. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4717. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4718. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  4719. }
  4720. }
  4721. }
  4722. }
  4723. }
  4724. }
  4725. }
  4726. 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) {
  4727. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  4728. // Buffer is already mapped
  4729. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4730. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4731. GGML_ABORT("fatal error");
  4732. }
  4733. // Check if src is pinned memory
  4734. vk_buffer buf = nullptr;
  4735. size_t buf_offset = 0;
  4736. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  4737. if (buf != nullptr) {
  4738. // Memory is pinned, use as staging buffer
  4739. std::vector<vk::BufferCopy> slices(1);
  4740. if (width == spitch) {
  4741. // Only do single write if stride is equal
  4742. slices[0].srcOffset = buf_offset;
  4743. slices[0].dstOffset = offset;
  4744. slices[0].size = width * height;
  4745. } else {
  4746. slices.resize(height);
  4747. for (size_t i = 0; i < height; i++) {
  4748. slices[i].srcOffset = buf_offset + i * spitch;
  4749. slices[i].dstOffset = offset + i * width;
  4750. slices[i].size = width;
  4751. }
  4752. }
  4753. ggml_vk_sync_buffers(nullptr, subctx);
  4754. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4755. return;
  4756. }
  4757. VK_LOG_DEBUG("STAGING");
  4758. if (!sync_staging) {
  4759. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4760. }
  4761. // Staging buffer required
  4762. const size_t copy_size = width*height;
  4763. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  4764. vk_buffer& staging_buffer = dst->device->sync_staging;
  4765. VkBufferCopy buf_copy = {
  4766. 0,
  4767. offset,
  4768. copy_size};
  4769. ggml_vk_sync_buffers(nullptr, subctx);
  4770. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4771. if (width == spitch) {
  4772. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  4773. } else {
  4774. for (size_t i = 0; i < height; i++) {
  4775. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  4776. }
  4777. }
  4778. }
  4779. 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) {
  4780. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  4781. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  4782. }
  4783. 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) {
  4784. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  4785. // Buffer is already mapped
  4786. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4787. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4788. for (size_t i = 0; i < height; i++) {
  4789. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  4790. }
  4791. } else {
  4792. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4793. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4794. ggml_vk_ctx_begin(dst->device, subctx);
  4795. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  4796. ggml_vk_ctx_end(subctx);
  4797. for (auto& cpy : subctx->in_memcpys) {
  4798. memcpy(cpy.dst, cpy.src, cpy.n);
  4799. }
  4800. for (auto& mset : subctx->memsets) {
  4801. memset(mset.dst, mset.val, mset.n);
  4802. }
  4803. ggml_vk_submit(subctx, dst->device->fence);
  4804. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  4805. dst->device->device.resetFences({ dst->device->fence });
  4806. ggml_vk_queue_command_pools_cleanup(dst->device);
  4807. }
  4808. }
  4809. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  4810. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  4811. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  4812. }
  4813. 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) {
  4814. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  4815. GGML_ASSERT(width > 0);
  4816. GGML_ASSERT(height > 0);
  4817. GGML_ASSERT(src != nullptr);
  4818. // TODO: staging_offset is not used
  4819. // Check if dst is pinned memory
  4820. vk_buffer buf = nullptr;
  4821. size_t buf_offset = 0;
  4822. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  4823. std::vector<vk::BufferCopy> slices(1);
  4824. if (width == spitch && width == dpitch) {
  4825. // Only do single write if stride is equal
  4826. slices[0].srcOffset = offset;
  4827. slices[0].dstOffset = buf_offset;
  4828. slices[0].size = width * height;
  4829. } else {
  4830. slices.resize(height);
  4831. for (size_t i = 0; i < height; i++) {
  4832. slices[i].srcOffset = offset + i * spitch;
  4833. slices[i].dstOffset = buf_offset + i * dpitch;
  4834. slices[i].size = width;
  4835. }
  4836. }
  4837. if (buf != nullptr) {
  4838. // Memory is pinned, use as staging buffer
  4839. ggml_vk_sync_buffers(nullptr, subctx);
  4840. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  4841. return;
  4842. }
  4843. VK_LOG_DEBUG("STAGING");
  4844. if (!sync_staging) {
  4845. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  4846. }
  4847. // Fall back to staging buffer
  4848. const size_t copy_size = dpitch * height;
  4849. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4850. vk_buffer& staging_buffer = src->device->sync_staging;
  4851. ggml_vk_sync_buffers(nullptr, subctx);
  4852. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4853. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4854. }
  4855. 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) {
  4856. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4857. }
  4858. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4859. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4860. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4861. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4862. // the HW device to host copy path.
  4863. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4864. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4865. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4866. } else {
  4867. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4868. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4869. ggml_vk_ctx_begin(src->device, subctx);
  4870. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4871. ggml_vk_ctx_end(subctx);
  4872. ggml_vk_submit(subctx, src->device->fence);
  4873. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4874. src->device->device.resetFences({ src->device->fence });
  4875. ggml_vk_queue_command_pools_cleanup(src->device);
  4876. for (auto& cpy : subctx->out_memcpys) {
  4877. memcpy(cpy.dst, cpy.src, cpy.n);
  4878. }
  4879. }
  4880. }
  4881. 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) {
  4882. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4883. // Make sure both buffers are on same device
  4884. GGML_ASSERT(src->device == dst->device);
  4885. VkBufferCopy bc{ src_offset, dst_offset, size };
  4886. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4887. }
  4888. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4889. if (src->device == dst->device) {
  4890. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4891. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4892. // Copy within the device
  4893. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4894. ggml_vk_ctx_begin(src->device, subctx);
  4895. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4896. ggml_vk_ctx_end(subctx);
  4897. ggml_vk_submit(subctx, src->device->fence);
  4898. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4899. src->device->device.resetFences({ src->device->fence });
  4900. ggml_vk_queue_command_pools_cleanup(src->device);
  4901. } else {
  4902. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4903. // Copy device to device
  4904. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4905. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4906. // Copy to src staging buffer
  4907. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4908. // memcpy to dst staging buffer
  4909. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4910. // Copy to dst buffer
  4911. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4912. }
  4913. }
  4914. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4915. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4916. if (dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible &&
  4917. dst->device->uma) {
  4918. deferred_memset((uint8_t*)dst->ptr + offset, c, size, &ctx->memsets);
  4919. return;
  4920. }
  4921. // Fall back to GPU fillBuffer for non-UMA or non-host-visible buffers
  4922. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4923. }
  4924. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4925. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4926. if (dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible &&
  4927. dst->device->uma) {
  4928. memset((uint8_t*)dst->ptr + offset, c, size);
  4929. return;
  4930. }
  4931. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4932. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4933. ggml_vk_ctx_begin(dst->device, subctx);
  4934. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4935. ggml_vk_ctx_end(subctx);
  4936. ggml_vk_submit(subctx, dst->device->fence);
  4937. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4938. dst->device->device.resetFences({ dst->device->fence });
  4939. ggml_vk_queue_command_pools_cleanup(dst->device);
  4940. }
  4941. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, uint32_t m, uint32_t n, uint32_t k, bool disable_split_k, const vk_pipeline& pipeline) {
  4942. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ", " << disable_split_k << ")");
  4943. if (disable_split_k) {
  4944. return 1;
  4945. }
  4946. uint32_t split_k = 1;
  4947. if (ctx->device->shader_core_count != 0 && m >= pipeline->wg_denoms[0] && n >= pipeline->wg_denoms[1]) {
  4948. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4949. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4950. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4951. if (k >= 2048) {
  4952. if (m_tiles * n_tiles <= ctx->device->shader_core_count / 2) {
  4953. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4954. } else if (m_tiles * n_tiles <= ctx->device->shader_core_count * 2 / 3) {
  4955. split_k = 3;
  4956. }
  4957. // Cap the split at 8x. Unless k is huge this is a lot of overhead.
  4958. split_k = std::min(split_k, 8u);
  4959. // ggml_vk_matmul will align the splits to be a multiple of 256.
  4960. // If this rounded up size would cause the last split to be empty,
  4961. // then reduce the split count.
  4962. while (true) {
  4963. if (split_k == 1) {
  4964. break;
  4965. }
  4966. uint32_t k_split = CEIL_DIV(k, split_k);
  4967. k_split = ROUNDUP_POW2(k_split, 256);
  4968. if (k_split * (split_k - 1) < k) {
  4969. break;
  4970. }
  4971. split_k--;
  4972. }
  4973. }
  4974. }
  4975. return split_k;
  4976. }
  4977. 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) {
  4978. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4979. if (ctx->device->coopmat2) {
  4980. const uint32_t shader_core_count = ctx->device->shader_core_count;
  4981. const uint32_t tiles_l = CEIL_DIV(m, mmp->a_l->wg_denoms[0]) * CEIL_DIV(n, mmp->a_l->wg_denoms[1]);
  4982. const uint32_t tiles_m = CEIL_DIV(m, mmp->a_m->wg_denoms[0]) * CEIL_DIV(n, mmp->a_m->wg_denoms[1]);
  4983. // Use large shader when the N dimension is greater than the medium shader's tile size
  4984. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4985. // Prefer large over medium if either:
  4986. // - medium or large tiles would overfill the GPU
  4987. // - large tiles with a split_k==3 fits in the GPU and medium tiles with split_k==2 does not
  4988. // (medium with split_k==2 is probably better if it fits - more workgroups running and less split_k overhead)
  4989. bool prefer_large = tiles_m > shader_core_count || tiles_l > shader_core_count ||
  4990. // split_k==3 with large tiles likely better than medium tiles with no split_k.
  4991. (tiles_l <= shader_core_count / 3 && tiles_m > shader_core_count / 2);
  4992. 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])) {
  4993. return aligned ? mmp->a_l : mmp->l;
  4994. }
  4995. // Use medium shader when the N dimension is greater than the small shader's tile size
  4996. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4997. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4998. return aligned ? mmp->a_m : mmp->m;
  4999. }
  5000. return aligned ? mmp->a_s : mmp->s;
  5001. }
  5002. 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])) {
  5003. return aligned ? mmp->a_s : mmp->s;
  5004. }
  5005. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  5006. return aligned ? mmp->a_m : mmp->m;
  5007. }
  5008. return aligned ? mmp->a_l : mmp->l;
  5009. GGML_UNUSED(src1_type);
  5010. }
  5011. 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) {
  5012. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  5013. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  5014. }
  5015. static void ggml_vk_matmul(
  5016. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  5017. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  5018. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  5019. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  5020. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  5021. uint32_t padded_n) {
  5022. 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 << ")");
  5023. if (split_k == 1) {
  5024. 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 };
  5025. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  5026. return;
  5027. }
  5028. if (ctx->prealloc_split_k_need_sync) {
  5029. ggml_vk_sync_buffers(ctx, subctx);
  5030. }
  5031. GGML_ASSERT(batch_stride_d == m * n);
  5032. // Round the split size up to a multiple of 256 (k-quant alignment)
  5033. uint32_t k_split = CEIL_DIV(k, split_k);
  5034. k_split = ROUNDUP_POW2(k_split, 256);
  5035. 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 };
  5036. // Make sure enough workgroups get assigned for split k to work
  5037. 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 });
  5038. ggml_vk_sync_buffers(ctx, subctx);
  5039. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  5040. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  5041. ctx->prealloc_split_k_need_sync = true;
  5042. }
  5043. 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) {
  5044. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  5045. if (ctx->device->coopmat2) {
  5046. // Use large shader when the N dimension is greater than the medium shader's tile size
  5047. uint32_t crossover_large = mmp->m->wg_denoms[1];
  5048. 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])) {
  5049. return aligned ? mmp->a_l : mmp->l;
  5050. }
  5051. // Use medium shader when the N dimension is greater than the small shader's tile size
  5052. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  5053. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  5054. return aligned ? mmp->a_m : mmp->m;
  5055. }
  5056. return aligned ? mmp->a_s : mmp->s;
  5057. }
  5058. 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])) {
  5059. return aligned ? mmp->a_s : mmp->s;
  5060. }
  5061. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  5062. return aligned ? mmp->a_m : mmp->m;
  5063. }
  5064. return aligned ? mmp->a_l : mmp->l;
  5065. }
  5066. 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) {
  5067. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  5068. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  5069. }
  5070. static void ggml_vk_matmul_id(
  5071. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  5072. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  5073. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  5074. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  5075. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  5076. uint32_t padded_n) {
  5077. 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 << "), " <<
  5078. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  5079. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  5080. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  5081. 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,
  5082. nei0, nei1, nbi1, ne11, padded_n };
  5083. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  5084. }
  5085. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  5086. return
  5087. tensor->nb[0] == ggml_type_size(tensor->type) &&
  5088. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  5089. (tensor->ne[3] == 1 || tensor->nb[3] == tensor->nb[2]*tensor->ne[2]);
  5090. }
  5091. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  5092. // Choose "contiguous copy" shader if src/dst are contiguous
  5093. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  5094. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  5095. if (contig) {
  5096. return ctx->device->pipeline_contig_cpy_f32_f32;
  5097. } else {
  5098. return ctx->device->pipeline_cpy_f32_f32;
  5099. }
  5100. }
  5101. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  5102. if (contig) {
  5103. return ctx->device->pipeline_contig_cpy_f32_f16;
  5104. } else {
  5105. return ctx->device->pipeline_cpy_f32_f16;
  5106. }
  5107. }
  5108. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  5109. if (contig) {
  5110. return ctx->device->pipeline_contig_cpy_f16_f16;
  5111. } else {
  5112. return ctx->device->pipeline_cpy_f16_f16;
  5113. }
  5114. }
  5115. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  5116. if (contig) {
  5117. return ctx->device->pipeline_contig_cpy_f16_f32;
  5118. } else {
  5119. return ctx->device->pipeline_cpy_f16_f32;
  5120. }
  5121. }
  5122. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  5123. if (contig) {
  5124. return ctx->device->pipeline_contig_cpy_f32_bf16;
  5125. } else {
  5126. return ctx->device->pipeline_cpy_f32_bf16;
  5127. }
  5128. }
  5129. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_I32) {
  5130. if (contig) {
  5131. return ctx->device->pipeline_contig_cpy_f32_i32;
  5132. } else {
  5133. return ctx->device->pipeline_cpy_f32_i32;
  5134. }
  5135. }
  5136. if (src->type == GGML_TYPE_I32 && to == GGML_TYPE_F32) {
  5137. if (contig) {
  5138. return ctx->device->pipeline_contig_cpy_i32_f32;
  5139. } else {
  5140. return ctx->device->pipeline_cpy_i32_f32;
  5141. }
  5142. }
  5143. if (src->type == GGML_TYPE_F32) {
  5144. switch (to) {
  5145. case GGML_TYPE_Q4_0:
  5146. case GGML_TYPE_Q4_1:
  5147. case GGML_TYPE_Q5_0:
  5148. case GGML_TYPE_Q5_1:
  5149. case GGML_TYPE_Q8_0:
  5150. case GGML_TYPE_IQ4_NL:
  5151. return ctx->device->pipeline_cpy_f32_quant[to];
  5152. default:
  5153. break;
  5154. }
  5155. }
  5156. if (to == GGML_TYPE_F32) {
  5157. switch (src->type) {
  5158. case GGML_TYPE_Q4_0:
  5159. case GGML_TYPE_Q4_1:
  5160. case GGML_TYPE_Q5_0:
  5161. case GGML_TYPE_Q5_1:
  5162. case GGML_TYPE_Q8_0:
  5163. case GGML_TYPE_IQ4_NL:
  5164. return ctx->device->pipeline_cpy_quant_f32[src->type];
  5165. default:
  5166. break;
  5167. }
  5168. }
  5169. if (src->type == to) {
  5170. // Copy two or four bytes at a time, depending on block size.
  5171. // For quantized types, we scale by block size/type size. But
  5172. // this path is also used for bf16->bf16 for example, where the
  5173. // type size must be exactly 2 or 4.
  5174. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  5175. if ((ggml_type_size(src->type) % 4) == 0) {
  5176. if (contig) {
  5177. return ctx->device->pipeline_contig_cpy_f32_f32;
  5178. } else {
  5179. return ctx->device->pipeline_cpy_f32_f32;
  5180. }
  5181. } else {
  5182. if (contig) {
  5183. return ctx->device->pipeline_contig_cpy_f16_f16;
  5184. } else {
  5185. return ctx->device->pipeline_cpy_f16_f16;
  5186. }
  5187. }
  5188. }
  5189. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  5190. GGML_ABORT("fatal error");
  5191. }
  5192. 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) {
  5193. 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] << "), ";
  5194. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  5195. const int tensor_type_size = ggml_type_size(tensor->type);
  5196. const uint32_t ne = ggml_nelements(tensor);
  5197. std::array<uint32_t, 3> elements;
  5198. if (ne > 262144) {
  5199. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  5200. } else if (ne > 512) {
  5201. elements = { 512, CEIL_DIV(ne, 512), 1 };
  5202. } else {
  5203. elements = { ne, 1, 1 };
  5204. }
  5205. vk_op_unary_push_constants pc = {
  5206. (uint32_t)ne,
  5207. (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,
  5208. (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]),
  5209. 0,
  5210. 0.0f, 0.0f,
  5211. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5212. };
  5213. init_pushconst_fastdiv(pc);
  5214. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  5215. ggml_vk_sync_buffers(ctx, subctx);
  5216. }
  5217. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type, bool use_x4_blocks) {
  5218. switch(type) {
  5219. case GGML_TYPE_Q8_1:
  5220. return use_x4_blocks ? ctx->device->pipeline_quantize_q8_1_x4 : ctx->device->pipeline_quantize_q8_1;
  5221. default:
  5222. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  5223. GGML_ABORT("fatal error");
  5224. }
  5225. }
  5226. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne, bool use_x4_blocks = false) {
  5227. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  5228. vk_pipeline pipeline = use_x4_blocks ? ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true) : ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, false);
  5229. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  5230. ggml_vk_sync_buffers(ctx, subctx);
  5231. }
  5232. 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 disable_split_k, bool dryrun = false) {
  5233. 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];
  5234. 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];
  5235. 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];
  5236. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5237. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5238. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5239. const uint64_t ne00 = src0->ne[0];
  5240. const uint64_t ne01 = src0->ne[1];
  5241. const uint64_t ne02 = src0->ne[2];
  5242. const uint64_t ne03 = src0->ne[3];
  5243. const uint64_t ne10 = src1->ne[0];
  5244. const uint64_t ne11 = src1->ne[1];
  5245. const uint64_t ne12 = src1->ne[2];
  5246. const uint64_t ne13 = src1->ne[3];
  5247. const uint64_t ne21 = dst->ne[1];
  5248. const uint32_t stride_d = dst->nb[1] / ggml_type_size(dst->type);
  5249. const uint32_t stride_batch_d = stride_d*ne21;
  5250. const uint64_t r2 = ne12 / ne02;
  5251. const uint64_t r3 = ne13 / ne03;
  5252. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5253. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5254. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5255. vk_buffer d_Qx = nullptr;
  5256. size_t qx_buf_offset = 0;
  5257. vk_buffer d_Qy = nullptr;
  5258. size_t qy_buf_offset = 0;
  5259. bool src0_uma = false;
  5260. bool src1_uma = false;
  5261. if (ctx->device->uma) {
  5262. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5263. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5264. src0_uma = d_Qx != nullptr;
  5265. src1_uma = d_Qy != nullptr;
  5266. }
  5267. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  5268. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  5269. !ggml_vk_dim01_contiguous(src0);
  5270. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  5271. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  5272. !ggml_vk_dim01_contiguous(src1);
  5273. // If src0 is BF16, try to use a BF16 x BF16 multiply
  5274. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  5275. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  5276. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  5277. // Check for mmq first
  5278. 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;
  5279. if (mmp == nullptr) {
  5280. // Fall back to f16 dequant mul mat
  5281. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  5282. quantize_y = false;
  5283. }
  5284. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  5285. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  5286. if (qx_needs_dequant) {
  5287. // Fall back to dequant + f16 mulmat
  5288. 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]);
  5289. }
  5290. // Not implemented
  5291. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5292. 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)));
  5293. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  5294. 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));
  5295. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  5296. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  5297. const int x_ne = ne01 * ne00;
  5298. const int y_ne = padded_n * ne10;
  5299. const int d_ne = ne11 * ne01;
  5300. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, disable_split_k, pipeline);
  5301. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  5302. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5303. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  5304. 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);
  5305. const uint64_t d_sz = sizeof(float) * d_ne;
  5306. vk_pipeline to_fp16_vk_0 = nullptr;
  5307. vk_pipeline to_fp16_vk_1 = nullptr;
  5308. vk_pipeline to_q8_1 = nullptr;
  5309. if (x_non_contig) {
  5310. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  5311. } else {
  5312. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  5313. }
  5314. if (y_non_contig) {
  5315. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  5316. } else {
  5317. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5318. }
  5319. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5320. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5321. if (quantize_y) {
  5322. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  5323. }
  5324. if (dryrun) {
  5325. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5326. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5327. if (quantize_y) {
  5328. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  5329. }
  5330. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  5331. if (
  5332. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5333. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5334. (split_k > 1 && split_k_size > ctx->device->properties.limits.maxStorageBufferRange)) {
  5335. GGML_ABORT("Requested preallocation size is too large");
  5336. }
  5337. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5338. ctx->prealloc_size_x = x_sz_upd;
  5339. }
  5340. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  5341. ctx->prealloc_size_y = y_sz_upd;
  5342. }
  5343. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  5344. ctx->prealloc_size_split_k = split_k_size;
  5345. }
  5346. // Request descriptor sets
  5347. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5348. if (qx_needs_dequant) {
  5349. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5350. }
  5351. if (qy_needs_dequant) {
  5352. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5353. }
  5354. if (quantize_y) {
  5355. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  5356. }
  5357. if (split_k > 1) {
  5358. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  5359. }
  5360. return;
  5361. }
  5362. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5363. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5364. GGML_ASSERT(d_D != nullptr);
  5365. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  5366. vk_buffer d_X;
  5367. uint64_t x_buf_offset = 0;
  5368. vk_buffer d_Y;
  5369. uint64_t y_buf_offset = 0;
  5370. if (!src0_uma) {
  5371. d_Qx = src0_buf_ctx->dev_buffer;
  5372. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5373. GGML_ASSERT(d_Qx != nullptr);
  5374. }
  5375. if (!src1_uma) {
  5376. d_Qy = src1_buf_ctx->dev_buffer;
  5377. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5378. GGML_ASSERT(d_Qy != nullptr);
  5379. }
  5380. if (qx_needs_dequant) {
  5381. d_X = ctx->prealloc_x;
  5382. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  5383. } else {
  5384. d_X = d_Qx;
  5385. x_buf_offset = qx_buf_offset;
  5386. GGML_ASSERT(qx_sz == x_sz);
  5387. }
  5388. if (qy_needs_dequant) {
  5389. d_Y = ctx->prealloc_y;
  5390. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5391. } else if (quantize_y) {
  5392. d_Y = ctx->prealloc_y;
  5393. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  5394. } else {
  5395. d_Y = d_Qy;
  5396. y_buf_offset = qy_buf_offset;
  5397. GGML_ASSERT(qy_sz == y_sz);
  5398. }
  5399. if (x_non_contig || qx_needs_dequant) {
  5400. if (ctx->prealloc_x_need_sync) {
  5401. ggml_vk_sync_buffers(ctx, subctx);
  5402. }
  5403. }
  5404. if (x_non_contig) {
  5405. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  5406. } else if (qx_needs_dequant) {
  5407. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5408. 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});
  5409. ggml_vk_sync_buffers(ctx, subctx);
  5410. }
  5411. if (y_non_contig) {
  5412. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5413. ctx->prealloc_y_last_tensor_used != src1) {
  5414. if (ctx->prealloc_y_need_sync) {
  5415. ggml_vk_sync_buffers(ctx, subctx);
  5416. }
  5417. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  5418. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5419. ctx->prealloc_y_last_tensor_used = src1;
  5420. }
  5421. }
  5422. if (quantize_y) {
  5423. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  5424. ctx->prealloc_y_last_tensor_used != src1) {
  5425. if (ctx->prealloc_y_need_sync) {
  5426. ggml_vk_sync_buffers(ctx, subctx);
  5427. }
  5428. ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0), y_ne * ne12 * ne13, true);
  5429. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  5430. ctx->prealloc_y_last_tensor_used = src1;
  5431. }
  5432. }
  5433. uint32_t stride_batch_x = ne00*ne01;
  5434. uint32_t stride_batch_y = ne10*ne11;
  5435. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5436. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5437. }
  5438. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  5439. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5440. }
  5441. uint32_t y_sz_total = y_sz * ne12 * ne13;
  5442. if (quantize_y) {
  5443. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  5444. }
  5445. // compute
  5446. ggml_vk_matmul(
  5447. ctx, subctx, pipeline,
  5448. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz_total },
  5449. ggml_vk_subbuffer(ctx, d_D, d_buf_offset), { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  5450. ne01, ne11, ne10,
  5451. ne10, ne10, stride_d, stride_batch_x, stride_batch_y, stride_batch_d,
  5452. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  5453. ); // NOLINT
  5454. if (x_non_contig || qx_needs_dequant) {
  5455. ctx->prealloc_x_need_sync = true;
  5456. }
  5457. if (y_non_contig || quantize_y) {
  5458. ctx->prealloc_y_need_sync = true;
  5459. }
  5460. }
  5461. // Device tuning
  5462. static bool ggml_vk_should_use_mmvq(const vk_device& device, uint32_t m, uint32_t n, uint32_t k, ggml_type src0_type) {
  5463. if (device->mmvq_mode == 1) {
  5464. return true;
  5465. } else if (device->mmvq_mode == -1) {
  5466. return false;
  5467. }
  5468. // MMVQ is generally good for batches
  5469. if (n > 1) {
  5470. return true;
  5471. }
  5472. switch (device->vendor_id) {
  5473. case VK_VENDOR_ID_NVIDIA:
  5474. switch (src0_type) {
  5475. case GGML_TYPE_Q8_0:
  5476. return device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  5477. default:
  5478. return true;
  5479. }
  5480. case VK_VENDOR_ID_AMD:
  5481. switch (src0_type) {
  5482. case GGML_TYPE_Q8_0:
  5483. return device->architecture == vk_device_architecture::AMD_GCN;
  5484. default:
  5485. return true;
  5486. }
  5487. case VK_VENDOR_ID_INTEL:
  5488. switch (src0_type) {
  5489. // From tests on A770 Linux, may need more tuning
  5490. case GGML_TYPE_Q4_0:
  5491. case GGML_TYPE_Q5_1:
  5492. return false;
  5493. default:
  5494. return true;
  5495. }
  5496. default:
  5497. return true;
  5498. }
  5499. GGML_UNUSED(m);
  5500. GGML_UNUSED(k);
  5501. }
  5502. 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) {
  5503. 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];
  5504. 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];
  5505. 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];
  5506. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  5507. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5508. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5509. const uint64_t ne00 = src0->ne[0];
  5510. const uint64_t ne01 = src0->ne[1];
  5511. const uint64_t ne02 = src0->ne[2];
  5512. const uint64_t ne03 = src0->ne[3];
  5513. const uint64_t ne10 = src1->ne[0];
  5514. const uint64_t ne11 = src1->ne[1];
  5515. const uint64_t ne12 = src1->ne[2];
  5516. const uint64_t ne13 = src1->ne[3];
  5517. const uint64_t ne20 = dst->ne[0];
  5518. const uint64_t ne21 = dst->ne[1];
  5519. const uint64_t ne22 = dst->ne[2];
  5520. const uint64_t ne23 = dst->ne[3];
  5521. const uint64_t r2 = ne12 / ne02;
  5522. const uint64_t r3 = ne13 / ne03;
  5523. // batch_n indicates that we need to compute a few vector results, and this assumes
  5524. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  5525. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  5526. bool batch_n = ne11 > 1;
  5527. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5528. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5529. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5530. vk_buffer d_Qx = nullptr;
  5531. size_t qx_buf_offset = 0;
  5532. vk_buffer d_Qy = nullptr;
  5533. size_t qy_buf_offset = 0;
  5534. bool src0_uma = false;
  5535. bool src1_uma = false;
  5536. if (ctx->device->uma) {
  5537. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5538. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5539. src0_uma = d_Qx != nullptr;
  5540. src1_uma = d_Qy != nullptr;
  5541. }
  5542. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5543. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5544. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5545. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0 && ggml_vk_should_use_mmvq(ctx->device, ne01, ne11, ne10, src0->type);
  5546. vk_pipeline to_fp16_vk_0 = nullptr;
  5547. vk_pipeline to_fp16_vk_1 = nullptr;
  5548. if (x_non_contig) {
  5549. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5550. }
  5551. if (y_non_contig) {
  5552. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5553. } else {
  5554. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5555. }
  5556. // Check for mmq first
  5557. vk_pipeline dmmv = quantize_y ? ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, GGML_TYPE_Q8_1, ne11, ne20, ne00) : nullptr;
  5558. vk_pipeline to_q8_1 = nullptr;
  5559. if (dmmv == nullptr) {
  5560. // Fall back to f16 dequant mul mat
  5561. dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11, ne20, ne00);
  5562. quantize_y = false;
  5563. }
  5564. if (quantize_y) {
  5565. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  5566. }
  5567. const bool qx_needs_dequant = x_non_contig;
  5568. const bool qy_needs_dequant = !quantize_y && ((src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig);
  5569. // Not implemented
  5570. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5571. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5572. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5573. GGML_ASSERT(dmmv != nullptr);
  5574. const uint64_t x_ne = ne01 * ne00;
  5575. const uint64_t y_ne = ne11 * ne10;
  5576. const uint64_t d_ne = ne11 * ne01;
  5577. 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);
  5578. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5579. 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;
  5580. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  5581. const uint64_t d_sz = sizeof(float) * d_ne;
  5582. if (dryrun) {
  5583. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5584. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5585. if (quantize_y) {
  5586. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  5587. }
  5588. if (
  5589. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5590. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange)) {
  5591. GGML_ABORT("Requested preallocation size is too large");
  5592. }
  5593. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5594. ctx->prealloc_size_x = x_sz_upd;
  5595. }
  5596. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  5597. ctx->prealloc_size_y = y_sz_upd;
  5598. }
  5599. // Request descriptor sets
  5600. if (qx_needs_dequant) {
  5601. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5602. }
  5603. if (qy_needs_dequant) {
  5604. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5605. }
  5606. if (quantize_y) {
  5607. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  5608. }
  5609. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5610. return;
  5611. }
  5612. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5613. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5614. GGML_ASSERT(d_D != nullptr);
  5615. vk_buffer d_X;
  5616. uint64_t x_buf_offset = 0;
  5617. vk_buffer d_Y;
  5618. uint64_t y_buf_offset = 0;
  5619. if(!src0_uma) {
  5620. d_Qx = src0_buf_ctx->dev_buffer;
  5621. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5622. GGML_ASSERT(d_Qx != nullptr);
  5623. }
  5624. if(!src1_uma) {
  5625. d_Qy = src1_buf_ctx->dev_buffer;
  5626. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5627. GGML_ASSERT(d_Qy != nullptr);
  5628. }
  5629. if (qx_needs_dequant) {
  5630. d_X = ctx->prealloc_x;
  5631. } else {
  5632. d_X = d_Qx;
  5633. x_buf_offset = qx_buf_offset;
  5634. GGML_ASSERT(qx_sz == x_sz);
  5635. }
  5636. if (qy_needs_dequant) {
  5637. d_Y = ctx->prealloc_y;
  5638. } else if (quantize_y) {
  5639. d_Y = ctx->prealloc_y;
  5640. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  5641. } else {
  5642. d_Y = d_Qy;
  5643. y_buf_offset = qy_buf_offset;
  5644. GGML_ASSERT(qy_sz == y_sz);
  5645. }
  5646. if (x_non_contig) {
  5647. if (ctx->prealloc_x_need_sync) {
  5648. ggml_vk_sync_buffers(ctx, subctx);
  5649. }
  5650. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5651. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  5652. }
  5653. if (y_non_contig) {
  5654. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5655. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5656. ctx->prealloc_y_last_tensor_used != src1) {
  5657. if (ctx->prealloc_y_need_sync) {
  5658. ggml_vk_sync_buffers(ctx, subctx);
  5659. }
  5660. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  5661. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5662. ctx->prealloc_y_last_tensor_used = src1;
  5663. }
  5664. }
  5665. if (quantize_y) {
  5666. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  5667. ctx->prealloc_y_last_tensor_used != src1) {
  5668. if (ctx->prealloc_y_need_sync) {
  5669. ggml_vk_sync_buffers(ctx, subctx);
  5670. }
  5671. ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0), y_ne * ne12 * ne13, true);
  5672. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  5673. ctx->prealloc_y_last_tensor_used = src1;
  5674. }
  5675. }
  5676. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  5677. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  5678. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  5679. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  5680. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5681. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5682. }
  5683. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5684. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5685. }
  5686. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5687. uint32_t groups_x = ne01;
  5688. uint32_t groups_z = 1;
  5689. if (ne01 > max_groups_x) {
  5690. groups_z = 64;
  5691. groups_x = CEIL_DIV(groups_x, groups_z);
  5692. }
  5693. // TODO: Clean up this whole sz * ne_2 * ne_3 thing, it hasn't been necessary for a long time
  5694. uint32_t y_sz_total = y_sz * ne12 * ne13;
  5695. if (quantize_y) {
  5696. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  5697. }
  5698. // compute
  5699. const vk_mat_vec_push_constants pc = {
  5700. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5701. stride_batch_x, stride_batch_y, stride_batch_d,
  5702. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  5703. };
  5704. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5705. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz_total }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  5706. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  5707. if (x_non_contig) {
  5708. ctx->prealloc_x_need_sync = true;
  5709. }
  5710. if (y_non_contig || quantize_y) {
  5711. ctx->prealloc_y_need_sync = true;
  5712. }
  5713. }
  5714. 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) {
  5715. 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];
  5716. 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];
  5717. 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];
  5718. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5719. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  5720. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  5721. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  5722. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5723. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5724. const uint64_t ne00 = src0->ne[0];
  5725. const uint64_t ne01 = src0->ne[1];
  5726. const uint64_t ne02 = src0->ne[2];
  5727. // const uint64_t ne03 = src0->ne[3];
  5728. const uint64_t ne10 = src1->ne[0];
  5729. const uint64_t ne11 = src1->ne[1];
  5730. const uint64_t ne12 = src1->ne[2];
  5731. // const uint64_t ne13 = src1->ne[3];
  5732. GGML_ASSERT(ne11 == 1);
  5733. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5734. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5735. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5736. vk_buffer d_Qy = nullptr;
  5737. size_t qy_buf_offset = 0;
  5738. bool src1_uma = false;
  5739. if (ctx->device->uma) {
  5740. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5741. src1_uma = d_Qy != nullptr;
  5742. }
  5743. const uint64_t x_ne = ne00 * ne01 * ne02;
  5744. const uint64_t y_ne = ne10 * ne11 * ne12;
  5745. const uint64_t d_ne = ne01 * ne11 * ne12;
  5746. 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);
  5747. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5748. const uint64_t d_sz = sizeof(float) * d_ne;
  5749. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  5750. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  5751. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  5752. gqa_ratio = 1;
  5753. }
  5754. if (dryrun) {
  5755. // Request descriptor sets
  5756. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  5757. return;
  5758. }
  5759. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5760. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5761. GGML_ASSERT(d_D != nullptr);
  5762. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5763. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5764. GGML_ASSERT(d_Qx != nullptr);
  5765. if (!src1_uma) {
  5766. d_Qy = src1_buf_ctx->dev_buffer;
  5767. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5768. GGML_ASSERT(d_Qx != nullptr);
  5769. }
  5770. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5771. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5772. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5773. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5774. // compute
  5775. 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)) };
  5776. uint32_t workgroups_z = (uint32_t)ne12;
  5777. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  5778. if (gqa_ratio > 1) {
  5779. workgroups_z /= gqa_ratio;
  5780. }
  5781. 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 });
  5782. }
  5783. 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) {
  5784. 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];
  5785. 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];
  5786. 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];
  5787. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5788. GGML_ASSERT(!ggml_is_transposed(src0));
  5789. GGML_ASSERT(!ggml_is_transposed(src1));
  5790. GGML_ASSERT(!ggml_is_permuted(src0));
  5791. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5792. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5793. const uint64_t ne00 = src0->ne[0];
  5794. const uint64_t ne01 = src0->ne[1];
  5795. const uint64_t ne02 = src0->ne[2];
  5796. const uint64_t ne03 = src0->ne[3];
  5797. const uint64_t nb01 = src0->nb[1];
  5798. const uint64_t nb02 = src0->nb[2];
  5799. const uint64_t nb12 = src1->nb[2];
  5800. // const uint64_t ne10 = src1->ne[0];
  5801. const uint64_t ne11 = src1->ne[1];
  5802. const uint64_t ne12 = src1->ne[2];
  5803. // const uint64_t ne13 = src1->ne[3];
  5804. const uint32_t nb03 = (uint32_t)(src0->nb[3] / sizeof(ggml_fp16_t));
  5805. const uint32_t nb13 = (uint32_t)(src1->nb[3] / sizeof(float));
  5806. const uint32_t nb23 = (uint32_t)(dst->nb[3] / sizeof(float));
  5807. GGML_ASSERT(ne11 == 1);
  5808. GGML_ASSERT(src0->ne[3] == src1->ne[3]); // checked in supports_op
  5809. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5810. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5811. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5812. vk_buffer d_Qy = nullptr;
  5813. size_t qy_buf_offset = 0;
  5814. bool src1_uma = false;
  5815. if (ctx->device->uma) {
  5816. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5817. src1_uma = d_Qy != nullptr;
  5818. }
  5819. const uint64_t d_ne = ne01 * ne11 * ne12 * ne03;
  5820. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  5821. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  5822. const uint32_t channel_stride_y = nb12 / sizeof(float);
  5823. const uint64_t qx_sz = ggml_nbytes(src0);
  5824. const uint64_t qy_sz = ggml_nbytes(src1);
  5825. const uint64_t d_sz = sizeof(float) * d_ne;
  5826. if (dryrun) {
  5827. // Request descriptor sets
  5828. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  5829. return;
  5830. }
  5831. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5832. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5833. GGML_ASSERT(d_D != nullptr);
  5834. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5835. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5836. GGML_ASSERT(d_Qx != nullptr);
  5837. if (!src1_uma) {
  5838. d_Qy = src1_buf_ctx->dev_buffer;
  5839. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5840. GGML_ASSERT(d_Qx != nullptr);
  5841. }
  5842. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5843. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5844. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5845. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5846. // compute
  5847. 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 };
  5848. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  5849. { 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 });
  5850. }
  5851. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * src0, ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5852. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  5853. // Handle huge A matrix by splitting the M dimensions. This works well for convolution use cases
  5854. // where the M dimension is very large.
  5855. // Split_k doesn't work with M splitting.
  5856. const size_t nbytes = ggml_nbytes(src0);
  5857. const bool needs_split = nbytes > ctx->device->properties.limits.maxStorageBufferRange;
  5858. if (needs_split) {
  5859. // Choose the number of rows that can fit (and divide by two, to allow for any additional offsets)
  5860. const uint32_t M_split = ctx->device->properties.limits.maxStorageBufferRange / (2 * src0->nb[1]);
  5861. uint32_t m_offset = 0;
  5862. while (m_offset < dst->ne[0]) {
  5863. const uint32_t cur_M_size = std::min(M_split, (uint32_t)(dst->ne[0] - m_offset));
  5864. ggml_tensor dst2 = *dst;
  5865. ggml_tensor src02 = *src0;
  5866. dst2.view_src = dst->view_src ? dst->view_src : dst;
  5867. src02.view_src = src0->view_src ? src0->view_src : src0;
  5868. dst2.view_offs += m_offset * dst->nb[0];
  5869. src02.view_offs += m_offset * src0->nb[1];
  5870. dst2.ne[0] = cur_M_size;
  5871. src02.ne[1] = cur_M_size;
  5872. ggml_vk_mul_mat_q_f16(ctx, subctx, &src02, src1, &dst2, true, dryrun);
  5873. m_offset += cur_M_size;
  5874. }
  5875. } else if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  5876. // detect 0213 permutation, and batch size of 1
  5877. src0->nb[0] <= src0->nb[2] &&
  5878. src0->nb[2] <= src0->nb[1] &&
  5879. src0->nb[1] <= src0->nb[3] &&
  5880. src1->nb[0] <= src1->nb[2] &&
  5881. src1->nb[2] <= src1->nb[1] &&
  5882. src1->nb[1] <= src1->nb[3] &&
  5883. src0->ne[3] == 1 &&
  5884. src1->ne[3] == 1) {
  5885. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5886. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  5887. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  5888. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  5889. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  5890. // when ne12 and ne13 are one.
  5891. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  5892. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  5893. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  5894. } else {
  5895. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, false, dryrun);
  5896. }
  5897. }
  5898. 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) {
  5899. 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];
  5900. 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];
  5901. 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];
  5902. 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] << "),)");
  5903. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5904. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5905. const uint64_t ne00 = src0->ne[0];
  5906. const uint64_t ne01 = src0->ne[1];
  5907. const uint64_t ne02 = src0->ne[2];
  5908. const uint64_t ne03 = src0->ne[3];
  5909. const uint64_t ne10 = src1->ne[0];
  5910. const uint64_t ne11 = src1->ne[1];
  5911. const uint64_t ne12 = src1->ne[2];
  5912. const uint64_t ne13 = src1->ne[3];
  5913. const uint64_t nei0 = ids->ne[0];
  5914. const uint64_t nei1 = ids->ne[1];
  5915. const uint32_t nbi1 = ids->nb[1];
  5916. const uint32_t nbi2 = ids->nb[2];
  5917. const uint64_t ne20 = dst->ne[0];
  5918. const uint64_t ne21 = dst->ne[1];
  5919. const uint64_t ne22 = dst->ne[2];
  5920. const uint64_t ne23 = dst->ne[3];
  5921. const uint64_t n_as = ne02;
  5922. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5923. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5924. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5925. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5926. vk_buffer d_Qx = nullptr;
  5927. size_t qx_buf_offset = 0;
  5928. vk_buffer d_Qy = nullptr;
  5929. size_t qy_buf_offset = 0;
  5930. vk_buffer d_ids = nullptr;
  5931. size_t ids_buf_offset = 0;
  5932. bool src0_uma = false;
  5933. bool src1_uma = false;
  5934. bool ids_uma = false;
  5935. if (ctx->device->uma) {
  5936. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5937. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5938. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5939. src0_uma = d_Qx != nullptr;
  5940. src1_uma = d_Qy != nullptr;
  5941. ids_uma = d_ids != nullptr;
  5942. }
  5943. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  5944. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  5945. !ggml_vk_dim01_contiguous(src0);
  5946. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  5947. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  5948. !ggml_vk_dim01_contiguous(src1);
  5949. // If src0 is BF16, try to use a BF16 x BF16 multiply
  5950. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  5951. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  5952. 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]);
  5953. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  5954. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  5955. if (qx_needs_dequant) {
  5956. // Fall back to dequant + f16 mulmat
  5957. 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]);
  5958. }
  5959. // Not implemented
  5960. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5961. 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));
  5962. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  5963. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  5964. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  5965. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  5966. const uint64_t x_ne = ne01 * ne00;
  5967. const uint64_t y_ne = padded_n * ne10;
  5968. const uint64_t d_ne = ne21 * ne20;
  5969. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  5970. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5971. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  5972. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5973. const uint64_t ids_sz = nbi2;
  5974. const uint64_t d_sz = sizeof(float) * d_ne;
  5975. vk_pipeline to_fp16_vk_0 = nullptr;
  5976. vk_pipeline to_fp16_vk_1 = nullptr;
  5977. if (x_non_contig) {
  5978. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  5979. } else {
  5980. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  5981. }
  5982. if (y_non_contig) {
  5983. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  5984. } else {
  5985. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5986. }
  5987. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5988. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5989. if (dryrun) {
  5990. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5991. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5992. if (
  5993. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5994. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange)) {
  5995. GGML_ABORT("Requested preallocation size is too large");
  5996. }
  5997. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5998. ctx->prealloc_size_x = x_sz_upd;
  5999. }
  6000. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  6001. ctx->prealloc_size_y = y_sz_upd;
  6002. }
  6003. // Request descriptor sets
  6004. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6005. if (qx_needs_dequant) {
  6006. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  6007. }
  6008. if (qy_needs_dequant) {
  6009. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  6010. }
  6011. return;
  6012. }
  6013. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6014. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6015. GGML_ASSERT(d_D != nullptr);
  6016. vk_buffer d_X;
  6017. uint64_t x_buf_offset = 0;
  6018. vk_buffer d_Y;
  6019. uint64_t y_buf_offset = 0;
  6020. if (!src0_uma) {
  6021. d_Qx = src0_buf_ctx->dev_buffer;
  6022. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6023. GGML_ASSERT(d_Qx != nullptr);
  6024. }
  6025. if (!src1_uma) {
  6026. d_Qy = src1_buf_ctx->dev_buffer;
  6027. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6028. GGML_ASSERT(d_Qy != nullptr);
  6029. }
  6030. if (!ids_uma) {
  6031. d_ids = ids_buf_ctx->dev_buffer;
  6032. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  6033. GGML_ASSERT(d_ids != nullptr);
  6034. }
  6035. if (qx_needs_dequant) {
  6036. d_X = ctx->prealloc_x;
  6037. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  6038. } else {
  6039. d_X = d_Qx;
  6040. x_buf_offset = qx_buf_offset;
  6041. GGML_ASSERT(qx_sz == x_sz);
  6042. }
  6043. if (qy_needs_dequant) {
  6044. d_Y = ctx->prealloc_y;
  6045. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  6046. } else {
  6047. d_Y = d_Qy;
  6048. y_buf_offset = qy_buf_offset;
  6049. GGML_ASSERT(qy_sz == y_sz);
  6050. }
  6051. if (x_non_contig || qx_needs_dequant) {
  6052. if (ctx->prealloc_x_need_sync) {
  6053. ggml_vk_sync_buffers(ctx, subctx);
  6054. }
  6055. }
  6056. if (x_non_contig) {
  6057. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  6058. } else if (qx_needs_dequant) {
  6059. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  6060. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  6061. { 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});
  6062. ggml_vk_sync_buffers(ctx, subctx);
  6063. }
  6064. if (y_non_contig) {
  6065. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  6066. ctx->prealloc_y_last_tensor_used != src1) {
  6067. if (ctx->prealloc_y_need_sync) {
  6068. ggml_vk_sync_buffers(ctx, subctx);
  6069. }
  6070. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  6071. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  6072. ctx->prealloc_y_last_tensor_used = src1;
  6073. }
  6074. }
  6075. uint32_t stride_batch_x = ne00*ne01;
  6076. uint32_t stride_batch_y = ne10*ne11;
  6077. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  6078. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  6079. }
  6080. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  6081. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  6082. }
  6083. // compute
  6084. ggml_vk_matmul_id(
  6085. ctx, subctx, pipeline,
  6086. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  6087. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  6088. ne01, ne21, ne10, ne10, ne10, ne01,
  6089. stride_batch_x, stride_batch_y, ne20*ne21,
  6090. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  6091. ); // NOLINT
  6092. if (x_non_contig || qx_needs_dequant) {
  6093. ctx->prealloc_x_need_sync = true;
  6094. }
  6095. if (y_non_contig) {
  6096. ctx->prealloc_y_need_sync = true;
  6097. }
  6098. }
  6099. 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) {
  6100. 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];
  6101. 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];
  6102. 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];
  6103. 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];
  6104. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  6105. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  6106. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  6107. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  6108. const uint64_t ne00 = src0->ne[0];
  6109. const uint64_t ne01 = src0->ne[1];
  6110. const uint64_t ne02 = src0->ne[2];
  6111. const uint64_t ne03 = src0->ne[3];
  6112. const uint64_t ne10 = src1->ne[0];
  6113. const uint64_t ne11 = src1->ne[1];
  6114. const uint64_t ne12 = src1->ne[2];
  6115. const uint64_t ne13 = src1->ne[3];
  6116. const uint64_t nei0 = ids->ne[0];
  6117. const uint64_t nei1 = ids->ne[1];
  6118. const uint64_t nbi2 = ids->nb[2];
  6119. GGML_ASSERT(nei1 == 1);
  6120. const uint64_t ne20 = dst->ne[0];
  6121. const uint64_t ne21 = dst->ne[1];
  6122. const uint64_t ne22 = dst->ne[2];
  6123. const uint64_t ne23 = dst->ne[3];
  6124. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6125. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  6126. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  6127. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  6128. vk_buffer d_Qx = nullptr;
  6129. size_t qx_buf_offset = 0;
  6130. vk_buffer d_Qy = nullptr;
  6131. size_t qy_buf_offset = 0;
  6132. vk_buffer d_ids = nullptr;
  6133. size_t ids_buf_offset = 0;
  6134. bool src0_uma = false;
  6135. bool src1_uma = false;
  6136. bool ids_uma = false;
  6137. if (ctx->device->uma) {
  6138. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  6139. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  6140. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  6141. src0_uma = d_Qx != nullptr;
  6142. src1_uma = d_Qy != nullptr;
  6143. ids_uma = d_ids != nullptr;
  6144. }
  6145. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  6146. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  6147. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  6148. const bool qx_needs_dequant = x_non_contig;
  6149. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  6150. // Not implemented
  6151. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  6152. const uint64_t x_ne = ne01 * ne00;
  6153. const uint64_t y_ne = ne11 * ne10;
  6154. const uint64_t d_ne = ne21 * ne20;
  6155. 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);
  6156. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  6157. 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;
  6158. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  6159. const uint64_t ids_sz = nbi2;
  6160. const uint64_t d_sz = sizeof(float) * d_ne;
  6161. vk_pipeline to_fp16_vk_0 = nullptr;
  6162. vk_pipeline to_fp16_vk_1 = nullptr;
  6163. if (x_non_contig) {
  6164. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  6165. }
  6166. if (y_non_contig) {
  6167. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  6168. } else {
  6169. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  6170. }
  6171. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  6172. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  6173. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  6174. GGML_ASSERT(dmmv != nullptr);
  6175. if (dryrun) {
  6176. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  6177. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  6178. if (
  6179. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  6180. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange)) {
  6181. GGML_ABORT("Requested preallocation size is too large");
  6182. }
  6183. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  6184. ctx->prealloc_size_x = x_sz_upd;
  6185. }
  6186. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  6187. ctx->prealloc_size_y = y_sz_upd;
  6188. }
  6189. // Request descriptor sets
  6190. if (qx_needs_dequant) {
  6191. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  6192. }
  6193. if (qy_needs_dequant) {
  6194. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  6195. }
  6196. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  6197. return;
  6198. }
  6199. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6200. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6201. GGML_ASSERT(d_D != nullptr);
  6202. vk_buffer d_X;
  6203. uint64_t x_buf_offset = 0;
  6204. vk_buffer d_Y;
  6205. uint64_t y_buf_offset = 0;
  6206. if(!src0_uma) {
  6207. d_Qx = src0_buf_ctx->dev_buffer;
  6208. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6209. GGML_ASSERT(d_Qx != nullptr);
  6210. }
  6211. if(!src1_uma) {
  6212. d_Qy = src1_buf_ctx->dev_buffer;
  6213. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6214. GGML_ASSERT(d_Qy != nullptr);
  6215. }
  6216. if(!ids_uma) {
  6217. d_ids = ids_buf_ctx->dev_buffer;
  6218. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  6219. GGML_ASSERT(d_ids != nullptr);
  6220. }
  6221. if (qx_needs_dequant) {
  6222. d_X = ctx->prealloc_x;
  6223. } else {
  6224. d_X = d_Qx;
  6225. x_buf_offset = qx_buf_offset;
  6226. GGML_ASSERT(qx_sz == x_sz);
  6227. }
  6228. if (qy_needs_dequant) {
  6229. d_Y = ctx->prealloc_y;
  6230. } else {
  6231. d_Y = d_Qy;
  6232. y_buf_offset = qy_buf_offset;
  6233. GGML_ASSERT(qy_sz == y_sz);
  6234. }
  6235. if (x_non_contig) {
  6236. if (ctx->prealloc_x_need_sync) {
  6237. ggml_vk_sync_buffers(ctx, subctx);
  6238. }
  6239. }
  6240. if (x_non_contig) {
  6241. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  6242. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  6243. }
  6244. if (y_non_contig) {
  6245. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  6246. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  6247. ctx->prealloc_y_last_tensor_used != src1) {
  6248. if (ctx->prealloc_y_need_sync) {
  6249. ggml_vk_sync_buffers(ctx, subctx);
  6250. }
  6251. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  6252. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  6253. ctx->prealloc_y_last_tensor_used = src1;
  6254. }
  6255. }
  6256. uint32_t stride_batch_y = ne10*ne11;
  6257. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  6258. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  6259. }
  6260. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  6261. uint32_t groups_x = ne01;
  6262. uint32_t groups_z = 1;
  6263. if (ne01 > max_groups_x) {
  6264. groups_z = 64;
  6265. groups_x = CEIL_DIV(groups_x, groups_z);
  6266. }
  6267. // compute
  6268. const vk_mat_vec_id_push_constants pc = {
  6269. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  6270. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  6271. (uint32_t)nei0, (uint32_t)ne11,
  6272. };
  6273. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  6274. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  6275. 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 } },
  6276. pc, { groups_x, (uint32_t)nei0, groups_z });
  6277. if (x_non_contig) {
  6278. ctx->prealloc_x_need_sync = true;
  6279. }
  6280. if (y_non_contig) {
  6281. ctx->prealloc_y_need_sync = true;
  6282. }
  6283. }
  6284. 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) {
  6285. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  6286. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  6287. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  6288. } else {
  6289. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  6290. }
  6291. }
  6292. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  6293. // Needs to be kept up to date on shader changes
  6294. GGML_UNUSED(hsv);
  6295. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  6296. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  6297. const uint32_t Bc = scalar_flash_attention_Bc;
  6298. const uint32_t tmpsh = wg_size * sizeof(float);
  6299. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  6300. const uint32_t masksh = Bc * Br * sizeof(float);
  6301. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  6302. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  6303. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  6304. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  6305. return supported;
  6306. }
  6307. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  6308. // Needs to be kept up to date on shader changes
  6309. GGML_UNUSED(hsv);
  6310. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  6311. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  6312. const uint32_t Bc = scalar_flash_attention_Bc;
  6313. const uint32_t hsk_pad = ROUNDUP_POW2(hsk, 16);
  6314. const uint32_t acctype = f32acc ? 4 : 2;
  6315. const uint32_t f16vec4 = 8;
  6316. const uint32_t tmpsh = wg_size * sizeof(float);
  6317. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  6318. const uint32_t qstride = hsk_pad / 4 + 2;
  6319. const uint32_t Qf = Br * qstride * f16vec4;
  6320. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  6321. const uint32_t sfsh = Bc * sfshstride * acctype;
  6322. const uint32_t kshstride = hsk_pad / 4 + 2;
  6323. const uint32_t ksh = Bc * kshstride * f16vec4;
  6324. const uint32_t slope = Br * sizeof(float);
  6325. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  6326. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  6327. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  6328. return supported;
  6329. }
  6330. 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) {
  6331. 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];
  6332. 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];
  6333. 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];
  6334. 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];
  6335. if (sinks) {
  6336. 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];
  6337. }
  6338. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  6339. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  6340. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  6341. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  6342. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  6343. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  6344. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  6345. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  6346. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  6347. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  6348. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  6349. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  6350. const uint32_t HSK = nek0;
  6351. const uint32_t HSV = nev0;
  6352. uint32_t N = neq1;
  6353. const uint32_t KV = nek1;
  6354. GGML_ASSERT(ne0 == HSV);
  6355. GGML_ASSERT(ne2 == N);
  6356. // input tensor rows must be contiguous
  6357. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  6358. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  6359. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  6360. GGML_ASSERT(neq0 == HSK);
  6361. GGML_ASSERT(neq1 == N);
  6362. GGML_ASSERT(nev1 == nek1);
  6363. // dst cannot be transposed or permuted
  6364. GGML_ASSERT(nb0 == sizeof(float));
  6365. GGML_ASSERT(nb0 <= nb1);
  6366. GGML_ASSERT(nb1 <= nb2);
  6367. GGML_ASSERT(nb2 <= nb3);
  6368. assert(dst->type == GGML_TYPE_F32);
  6369. assert(q->type == GGML_TYPE_F32);
  6370. assert(k->type == v->type);
  6371. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  6372. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  6373. if (path == FA_COOPMAT1) {
  6374. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  6375. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  6376. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  6377. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  6378. path = FA_SCALAR;
  6379. }
  6380. }
  6381. uint32_t gqa_ratio = 1;
  6382. uint32_t qk_ratio = neq2 / nek2;
  6383. uint32_t workgroups_x = (uint32_t)neq1;
  6384. uint32_t workgroups_y = (uint32_t)neq2;
  6385. uint32_t workgroups_z = (uint32_t)neq3;
  6386. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  6387. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  6388. uint32_t max_gqa;
  6389. switch (path) {
  6390. case FA_SCALAR:
  6391. case FA_COOPMAT1:
  6392. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  6393. max_gqa = get_fa_scalar_num_large_rows(HSV);
  6394. break;
  6395. case FA_COOPMAT2:
  6396. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  6397. break;
  6398. default:
  6399. GGML_ASSERT(0);
  6400. }
  6401. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  6402. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  6403. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  6404. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  6405. // and change addressing calculations to index Q's dimension 2.
  6406. gqa_ratio = qk_ratio;
  6407. N = gqa_ratio;
  6408. workgroups_y /= N;
  6409. }
  6410. bool small_rows = N <= get_fa_num_small_rows(path);
  6411. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  6412. // So use scalar instead.
  6413. if (small_rows && path == FA_COOPMAT1) {
  6414. path = FA_SCALAR;
  6415. }
  6416. // scalar is faster than coopmat2 when N==1
  6417. if (N == 1 && path == FA_COOPMAT2) {
  6418. path = FA_SCALAR;
  6419. }
  6420. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  6421. if (path == FA_SCALAR &&
  6422. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  6423. small_rows = true;
  6424. }
  6425. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  6426. uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  6427. uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  6428. // For F32, the shader treats it as a block of size 4 (for vec4 loads)
  6429. if (k->type == GGML_TYPE_F32) {
  6430. k_stride /= 4;
  6431. }
  6432. if (v->type == GGML_TYPE_F32) {
  6433. v_stride /= 4;
  6434. }
  6435. uint32_t alignment = fa_align(path, HSK, HSV, k->type, small_rows);
  6436. bool aligned = (KV % alignment) == 0 &&
  6437. // the "aligned" shader variant will forcibly align strides, for performance
  6438. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  6439. // Need to use the coopmat2 variant that clamps loads when HSK/HSV aren't sufficiently aligned.
  6440. if (((HSK | HSV) % 16) != 0 && path == FA_COOPMAT2) {
  6441. aligned = false;
  6442. }
  6443. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  6444. vk_fa_pipeline_state fa_pipeline_state(HSK, HSV, small_rows, path, aligned, f32acc);
  6445. vk_pipeline pipeline = nullptr;
  6446. auto &pipelines = ctx->device->pipeline_flash_attn_f32_f16[k->type];
  6447. auto it = pipelines.find(fa_pipeline_state);
  6448. if (it != pipelines.end()) {
  6449. pipeline = it->second;
  6450. } else {
  6451. pipelines[fa_pipeline_state] = pipeline = std::make_shared<vk_pipeline_struct>();
  6452. }
  6453. assert(pipeline);
  6454. uint32_t split_kv = KV;
  6455. uint32_t split_k = 1;
  6456. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  6457. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  6458. // Try to use split_k when KV is large enough to be worth the overhead
  6459. if (workgroups_x == 1 && shader_core_count > 0) {
  6460. // Try to run two workgroups per SM.
  6461. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  6462. if (split_k > 1) {
  6463. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  6464. // of "align", so recompute split_k based on that.
  6465. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), alignment);
  6466. split_k = CEIL_DIV(KV, split_kv);
  6467. workgroups_x = split_k;
  6468. }
  6469. }
  6470. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  6471. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  6472. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  6473. if (split_k_size > ctx->device->properties.limits.maxStorageBufferRange) {
  6474. GGML_ABORT("Requested preallocation size is too large");
  6475. }
  6476. if (ctx->prealloc_size_split_k < split_k_size) {
  6477. ctx->prealloc_size_split_k = split_k_size;
  6478. }
  6479. if (dryrun) {
  6480. // Request descriptor sets
  6481. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6482. if (split_k > 1) {
  6483. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  6484. }
  6485. return;
  6486. }
  6487. float scale = 1.0f;
  6488. float max_bias = 0.0f;
  6489. float logit_softcap = 0.0f;
  6490. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  6491. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  6492. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  6493. if (logit_softcap != 0) {
  6494. scale /= logit_softcap;
  6495. }
  6496. const uint32_t n_head_kv = neq2;
  6497. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6498. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6499. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6500. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr, d_S = nullptr;
  6501. 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;
  6502. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false, S_uma = false;
  6503. if (ctx->device->uma) {
  6504. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  6505. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  6506. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  6507. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  6508. Q_uma = d_Q != nullptr;
  6509. K_uma = d_K != nullptr;
  6510. V_uma = d_V != nullptr;
  6511. D_uma = d_D != nullptr;
  6512. if (mask) {
  6513. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  6514. M_uma = d_M != nullptr;
  6515. }
  6516. if (sinks) {
  6517. ggml_vk_host_get(ctx->device, sinks->data, d_S, s_buf_offset);
  6518. S_uma = d_S != nullptr;
  6519. }
  6520. }
  6521. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6522. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  6523. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  6524. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  6525. if (!Q_uma) {
  6526. d_Q = q_buf_ctx->dev_buffer;
  6527. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  6528. }
  6529. if (!K_uma) {
  6530. d_K = k_buf_ctx->dev_buffer;
  6531. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  6532. }
  6533. if (!V_uma) {
  6534. d_V = v_buf_ctx->dev_buffer;
  6535. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  6536. }
  6537. if (!D_uma) {
  6538. d_D = d_buf_ctx->dev_buffer;
  6539. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6540. }
  6541. if (!M_uma) {
  6542. d_M = d_Q;
  6543. m_buf_offset = q_buf_offset;
  6544. if (mask) {
  6545. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  6546. d_M = m_buf_ctx->dev_buffer;
  6547. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  6548. }
  6549. }
  6550. if (!S_uma) {
  6551. d_S = d_Q;
  6552. s_buf_offset = q_buf_offset;
  6553. if (sinks) {
  6554. ggml_backend_vk_buffer_context * s_buf_ctx = (ggml_backend_vk_buffer_context*)sinks->buffer->context;
  6555. d_S = s_buf_ctx->dev_buffer;
  6556. s_buf_offset = vk_tensor_offset(sinks) + sinks->view_offs;
  6557. }
  6558. }
  6559. uint32_t mask_n_head_log2 = ((sinks != nullptr) << 24) | ((mask != nullptr) << 16) | n_head_log2;
  6560. const vk_flash_attn_push_constants pc = { N, KV,
  6561. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  6562. (uint32_t)neq2, (uint32_t)neq3,
  6563. (uint32_t)nek2, (uint32_t)nek3,
  6564. (uint32_t)nev2, (uint32_t)nev3,
  6565. nem1, nem2, nem3,
  6566. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  6567. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  6568. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  6569. scale, max_bias, logit_softcap,
  6570. mask_n_head_log2, m0, m1,
  6571. gqa_ratio, split_kv, split_k };
  6572. if (split_k > 1) {
  6573. if (ctx->prealloc_split_k_need_sync) {
  6574. ggml_vk_sync_buffers(ctx, subctx);
  6575. }
  6576. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6577. {
  6578. ggml_vk_subbuffer(ctx, d_Q, q_buf_offset),
  6579. ggml_vk_subbuffer(ctx, d_K, k_buf_offset),
  6580. ggml_vk_subbuffer(ctx, d_V, v_buf_offset),
  6581. ggml_vk_subbuffer(ctx, d_M, m_buf_offset),
  6582. ggml_vk_subbuffer(ctx, d_S, s_buf_offset),
  6583. ggml_vk_subbuffer(ctx, ctx->prealloc_split_k, 0),
  6584. },
  6585. // We only use split_k when group query attention is enabled, which means
  6586. // there's no more than one tile of rows (i.e. workgroups_x would have been
  6587. // one). We reuse workgroups_x to mean the number of splits, so we need to
  6588. // cancel out the divide by wg_denoms[0].
  6589. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  6590. ggml_vk_sync_buffers(ctx, subctx);
  6591. const std::array<uint32_t, 5> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k, (sinks != nullptr) };
  6592. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  6593. {
  6594. ggml_vk_subbuffer(ctx, ctx->prealloc_split_k, 0),
  6595. ggml_vk_subbuffer(ctx, d_S, s_buf_offset),
  6596. ggml_vk_subbuffer(ctx, d_D, d_buf_offset),
  6597. },
  6598. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  6599. ctx->prealloc_split_k_need_sync = true;
  6600. } else {
  6601. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6602. {
  6603. ggml_vk_subbuffer(ctx, d_Q, q_buf_offset),
  6604. ggml_vk_subbuffer(ctx, d_K, k_buf_offset),
  6605. ggml_vk_subbuffer(ctx, d_V, v_buf_offset),
  6606. ggml_vk_subbuffer(ctx, d_M, m_buf_offset),
  6607. ggml_vk_subbuffer(ctx, d_S, s_buf_offset),
  6608. ggml_vk_subbuffer(ctx, d_D, d_buf_offset),
  6609. },
  6610. pc, { workgroups_x, workgroups_y, workgroups_z });
  6611. }
  6612. }
  6613. static std::array<uint32_t, 3> ggml_vk_get_conv_elements(const ggml_tensor *dst) {
  6614. const ggml_tensor *src0 = dst->src[0];
  6615. const ggml_tensor *src1 = dst->src[1];
  6616. // src0 - kernel: [KW, KH, Cin, Cout]
  6617. // src1 - input: [W, H, Cin, N]
  6618. // dst - result: [OW, OH, Cout, N]
  6619. // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d)
  6620. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  6621. return (ins + 2 * p - d * (ks - 1) - 1) / s + 1;
  6622. };
  6623. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  6624. int64_t W = src1->ne[0];
  6625. int64_t H = src1->ne[1];
  6626. int64_t KW = src0->ne[0];
  6627. int64_t KH = src0->ne[1];
  6628. int64_t Cout = src0->ne[3];
  6629. int64_t N = src1->ne[3];
  6630. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[1], dst->op_params[3], dst->op_params[5]);
  6631. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], dst->op_params[2], dst->op_params[4]);
  6632. int64_t NPQ = N * OW * OH;
  6633. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  6634. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  6635. return elements;
  6636. }
  6637. static std::array<uint32_t, 3> ggml_vk_get_conv_transpose_2d_elements(const ggml_tensor *dst) {
  6638. const ggml_tensor *src0 = dst->src[0];
  6639. const ggml_tensor *src1 = dst->src[1];
  6640. // src0 - kernel: [KW, KH, Cout, Cin]
  6641. // src1 - input: [W, H, Cin, N]
  6642. // dst - result: [OW, OH, Cout, N]
  6643. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  6644. return (ins - 1) * s - 2 * p + (ks - 1) * d + 1;
  6645. };
  6646. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  6647. int64_t W = src1->ne[0];
  6648. int64_t H = src1->ne[1];
  6649. int64_t KW = src0->ne[0];
  6650. int64_t KH = src0->ne[1];
  6651. int64_t Cout = src0->ne[2];
  6652. int64_t N = src1->ne[3];
  6653. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[0], 0, 1);
  6654. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], 0, 1);
  6655. int64_t NPQ = N * OW * OH;
  6656. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  6657. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  6658. return elements;
  6659. }
  6660. 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, const ggml_tensor * dst, ggml_op op) {
  6661. switch (op) {
  6662. case GGML_OP_GET_ROWS:
  6663. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6664. if (dst->type == GGML_TYPE_F16) {
  6665. return ctx->device->pipeline_get_rows[src0->type];
  6666. }
  6667. if (dst->type == GGML_TYPE_F32) {
  6668. return ctx->device->pipeline_get_rows_f32[src0->type];
  6669. }
  6670. return nullptr;
  6671. case GGML_OP_ACC:
  6672. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6673. return ctx->device->pipeline_acc_f32;
  6674. }
  6675. return nullptr;
  6676. case GGML_OP_ADD:
  6677. case GGML_OP_SUB:
  6678. case GGML_OP_MUL:
  6679. case GGML_OP_DIV:
  6680. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6681. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  6682. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  6683. return nullptr;
  6684. }
  6685. switch (op) {
  6686. case GGML_OP_ADD:
  6687. {
  6688. if (ctx->num_additional_fused_ops > 0) {
  6689. if (ctx->do_add_rms_partials) {
  6690. return ctx->device->pipeline_multi_add_rms[ctx->num_additional_fused_ops];
  6691. } else {
  6692. return ctx->device->pipeline_multi_add[ctx->num_additional_fused_ops];
  6693. }
  6694. }
  6695. if (ctx->do_add_rms_partials) {
  6696. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_rms_norepeat : ctx->device->pipeline_add_rms;
  6697. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6698. } else {
  6699. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  6700. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6701. }
  6702. }
  6703. case GGML_OP_SUB:
  6704. {
  6705. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  6706. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6707. }
  6708. case GGML_OP_MUL:
  6709. {
  6710. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  6711. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6712. }
  6713. case GGML_OP_DIV:
  6714. {
  6715. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  6716. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  6717. }
  6718. default:
  6719. break;
  6720. }
  6721. return nullptr;
  6722. case GGML_OP_ADD_ID:
  6723. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && src2->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32) {
  6724. return ctx->device->pipeline_add_id_f32;
  6725. }
  6726. return nullptr;
  6727. case GGML_OP_CONCAT:
  6728. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6729. return ctx->device->pipeline_concat_f32;
  6730. }
  6731. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6732. return ctx->device->pipeline_concat_f16;
  6733. }
  6734. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  6735. return ctx->device->pipeline_concat_i32;
  6736. }
  6737. return nullptr;
  6738. case GGML_OP_UPSCALE:
  6739. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6740. int mode = ggml_get_op_params_i32(dst, 0);
  6741. switch (mode) {
  6742. case GGML_SCALE_MODE_NEAREST:
  6743. return ctx->device->pipeline_upscale_nearest_f32;
  6744. case GGML_SCALE_MODE_BILINEAR:
  6745. return ctx->device->pipeline_upscale_bilinear_f32;
  6746. case GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS:
  6747. return ctx->device->pipeline_upscale_bilinear_ac_f32;
  6748. }
  6749. }
  6750. return nullptr;
  6751. case GGML_OP_SCALE:
  6752. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6753. return ctx->device->pipeline_scale_f32;
  6754. }
  6755. return nullptr;
  6756. case GGML_OP_SQR:
  6757. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6758. return ctx->device->pipeline_sqr_f32;
  6759. }
  6760. return nullptr;
  6761. case GGML_OP_SQRT:
  6762. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6763. return ctx->device->pipeline_sqrt_f32;
  6764. }
  6765. return nullptr;
  6766. case GGML_OP_SIN:
  6767. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6768. return ctx->device->pipeline_sin_f32;
  6769. }
  6770. return nullptr;
  6771. case GGML_OP_COS:
  6772. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6773. return ctx->device->pipeline_cos_f32;
  6774. }
  6775. return nullptr;
  6776. case GGML_OP_CLAMP:
  6777. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6778. return ctx->device->pipeline_clamp_f32;
  6779. }
  6780. return nullptr;
  6781. case GGML_OP_PAD:
  6782. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6783. return ctx->device->pipeline_pad_f32;
  6784. }
  6785. return nullptr;
  6786. case GGML_OP_ROLL:
  6787. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6788. return ctx->device->pipeline_roll_f32;
  6789. }
  6790. return nullptr;
  6791. case GGML_OP_REPEAT:
  6792. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  6793. return ctx->device->pipeline_repeat_f32;
  6794. }
  6795. return nullptr;
  6796. case GGML_OP_REPEAT_BACK:
  6797. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6798. return ctx->device->pipeline_repeat_back_f32;
  6799. }
  6800. return nullptr;
  6801. case GGML_OP_CPY:
  6802. case GGML_OP_CONT:
  6803. case GGML_OP_DUP:
  6804. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  6805. case GGML_OP_SET_ROWS:
  6806. if (src1->type == GGML_TYPE_I64) {
  6807. return ctx->device->pipeline_set_rows_i64[dst->type];
  6808. } else {
  6809. return ctx->device->pipeline_set_rows_i32[dst->type];
  6810. }
  6811. case GGML_OP_SILU_BACK:
  6812. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6813. return ctx->device->pipeline_silu_back_f32;
  6814. }
  6815. return nullptr;
  6816. case GGML_OP_NORM:
  6817. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6818. return ctx->device->pipeline_norm_f32;
  6819. }
  6820. return nullptr;
  6821. case GGML_OP_GROUP_NORM:
  6822. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6823. return ctx->device->pipeline_group_norm_f32;
  6824. }
  6825. return nullptr;
  6826. case GGML_OP_RMS_NORM:
  6827. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6828. if (ctx->do_add_rms_partials) {
  6829. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_partials_f32 : ctx->device->pipeline_rms_norm_partials_f32;
  6830. } else {
  6831. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  6832. }
  6833. }
  6834. return nullptr;
  6835. case GGML_OP_RMS_NORM_BACK:
  6836. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6837. return ctx->device->pipeline_rms_norm_back_f32;
  6838. }
  6839. return nullptr;
  6840. case GGML_OP_L2_NORM:
  6841. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6842. return ctx->device->pipeline_l2_norm_f32;
  6843. }
  6844. return nullptr;
  6845. case GGML_OP_UNARY:
  6846. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6847. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6848. (src0->type != dst->type)) {
  6849. return nullptr;
  6850. }
  6851. switch (ggml_get_unary_op(dst)) {
  6852. case GGML_UNARY_OP_EXP:
  6853. return ctx->device->pipeline_exp[dst->type == GGML_TYPE_F16];
  6854. case GGML_UNARY_OP_SILU:
  6855. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  6856. case GGML_UNARY_OP_GELU:
  6857. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  6858. case GGML_UNARY_OP_GELU_ERF:
  6859. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  6860. case GGML_UNARY_OP_GELU_QUICK:
  6861. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  6862. case GGML_UNARY_OP_RELU:
  6863. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  6864. case GGML_UNARY_OP_TANH:
  6865. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  6866. case GGML_UNARY_OP_SIGMOID:
  6867. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  6868. case GGML_UNARY_OP_HARDSIGMOID:
  6869. return ctx->device->pipeline_hardsigmoid[dst->type == GGML_TYPE_F16];
  6870. case GGML_UNARY_OP_HARDSWISH:
  6871. return ctx->device->pipeline_hardswish[dst->type == GGML_TYPE_F16];
  6872. default:
  6873. break;
  6874. }
  6875. return nullptr;
  6876. case GGML_OP_GLU:
  6877. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6878. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  6879. (src0->type != dst->type)) {
  6880. return nullptr;
  6881. }
  6882. switch (ggml_get_glu_op(dst)) {
  6883. case GGML_GLU_OP_GEGLU:
  6884. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  6885. case GGML_GLU_OP_REGLU:
  6886. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  6887. case GGML_GLU_OP_SWIGLU:
  6888. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  6889. case GGML_GLU_OP_SWIGLU_OAI:
  6890. return ctx->device->pipeline_swiglu_oai[dst->type == GGML_TYPE_F16];
  6891. case GGML_GLU_OP_GEGLU_ERF:
  6892. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  6893. case GGML_GLU_OP_GEGLU_QUICK:
  6894. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  6895. default:
  6896. break;
  6897. }
  6898. return nullptr;
  6899. case GGML_OP_DIAG_MASK_INF:
  6900. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6901. return ctx->device->pipeline_diag_mask_inf_f32;
  6902. }
  6903. return nullptr;
  6904. case GGML_OP_SOFT_MAX:
  6905. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  6906. GGML_ASSERT(!src2 || src2->type == GGML_TYPE_F32);
  6907. if (ctx->num_additional_fused_ops) {
  6908. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  6909. GGML_ASSERT(idx < num_topk_moe_pipelines);
  6910. bool with_norm = ctx->num_additional_fused_ops == topk_moe_norm.size() - 1;
  6911. return ctx->device->pipeline_topk_moe[idx][with_norm];
  6912. }
  6913. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  6914. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  6915. }
  6916. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6917. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  6918. }
  6919. return nullptr;
  6920. case GGML_OP_SOFT_MAX_BACK:
  6921. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6922. return ctx->device->pipeline_soft_max_back_f32;
  6923. }
  6924. return nullptr;
  6925. case GGML_OP_ROPE:
  6926. case GGML_OP_ROPE_BACK:
  6927. {
  6928. const int mode = ((const int32_t *) dst->op_params)[2];
  6929. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  6930. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  6931. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  6932. if (is_neox) {
  6933. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6934. return ctx->device->pipeline_rope_neox_f32;
  6935. }
  6936. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6937. return ctx->device->pipeline_rope_neox_f16;
  6938. }
  6939. } else if (is_mrope && !is_vision) {
  6940. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6941. return ctx->device->pipeline_rope_multi_f32;
  6942. }
  6943. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6944. return ctx->device->pipeline_rope_multi_f16;
  6945. }
  6946. } else if (is_vision) {
  6947. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6948. return ctx->device->pipeline_rope_vision_f32;
  6949. }
  6950. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6951. return ctx->device->pipeline_rope_vision_f16;
  6952. }
  6953. } else {
  6954. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6955. return ctx->device->pipeline_rope_norm_f32;
  6956. }
  6957. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6958. return ctx->device->pipeline_rope_norm_f16;
  6959. }
  6960. }
  6961. return nullptr;
  6962. }
  6963. case GGML_OP_ARGSORT:
  6964. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6965. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  6966. return ctx->device->pipeline_argsort_f32[idx];
  6967. }
  6968. return nullptr;
  6969. case GGML_OP_SUM:
  6970. case GGML_OP_SUM_ROWS:
  6971. case GGML_OP_MEAN:
  6972. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6973. return ctx->device->pipeline_sum_rows_f32;
  6974. }
  6975. return nullptr;
  6976. case GGML_OP_ARGMAX:
  6977. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  6978. return ctx->device->pipeline_argmax_f32;
  6979. }
  6980. return nullptr;
  6981. case GGML_OP_COUNT_EQUAL:
  6982. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  6983. return ctx->device->pipeline_count_equal_i32;
  6984. }
  6985. return nullptr;
  6986. case GGML_OP_IM2COL:
  6987. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6988. return ctx->device->pipeline_im2col_f32;
  6989. }
  6990. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  6991. return ctx->device->pipeline_im2col_f32_f16;
  6992. }
  6993. return nullptr;
  6994. case GGML_OP_IM2COL_3D:
  6995. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6996. return ctx->device->pipeline_im2col_3d_f32;
  6997. }
  6998. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  6999. return ctx->device->pipeline_im2col_3d_f32_f16;
  7000. }
  7001. return nullptr;
  7002. case GGML_OP_TIMESTEP_EMBEDDING:
  7003. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7004. return ctx->device->pipeline_timestep_embedding_f32;
  7005. }
  7006. return nullptr;
  7007. case GGML_OP_CONV_TRANSPOSE_1D:
  7008. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7009. return ctx->device->pipeline_conv_transpose_1d_f32;
  7010. }
  7011. return nullptr;
  7012. case GGML_OP_POOL_2D:
  7013. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7014. return ctx->device->pipeline_pool2d_f32;
  7015. }
  7016. return nullptr;
  7017. case GGML_OP_RWKV_WKV6:
  7018. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7019. return ctx->device->pipeline_rwkv_wkv6_f32;
  7020. }
  7021. return nullptr;
  7022. case GGML_OP_RWKV_WKV7:
  7023. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7024. return ctx->device->pipeline_rwkv_wkv7_f32;
  7025. }
  7026. return nullptr;
  7027. case GGML_OP_SSM_SCAN:
  7028. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7029. const uint32_t d_state = src0->ne[0];
  7030. if (d_state == 128) {
  7031. return ctx->device->pipeline_ssm_scan_f32_d128;
  7032. } else if (d_state == 256) {
  7033. return ctx->device->pipeline_ssm_scan_f32_d256;
  7034. }
  7035. }
  7036. return nullptr;
  7037. case GGML_OP_SSM_CONV:
  7038. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7039. return ctx->device->pipeline_ssm_conv_f32;
  7040. }
  7041. return nullptr;
  7042. case GGML_OP_OPT_STEP_ADAMW:
  7043. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7044. return ctx->device->pipeline_opt_step_adamw_f32;
  7045. }
  7046. return nullptr;
  7047. case GGML_OP_OPT_STEP_SGD:
  7048. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7049. return ctx->device->pipeline_opt_step_sgd_f32;
  7050. }
  7051. return nullptr;
  7052. case GGML_OP_LEAKY_RELU:
  7053. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7054. return ctx->device->pipeline_leaky_relu_f32;
  7055. }
  7056. return nullptr;
  7057. case GGML_OP_CONV_2D:
  7058. case GGML_OP_CONV_TRANSPOSE_2D:
  7059. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
  7060. ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  7061. std::array<uint32_t, 3> elements;
  7062. if (op == GGML_OP_CONV_2D) elements = ggml_vk_get_conv_elements(dst);
  7063. else if (op == GGML_OP_CONV_TRANSPOSE_2D) elements = ggml_vk_get_conv_transpose_2d_elements(dst);
  7064. vk_conv_shapes shape;
  7065. uint32_t tiles[CONV_SHAPE_COUNT];
  7066. for (uint32_t i = 0; i < CONV_SHAPE_COUNT; ++i) {
  7067. 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]);
  7068. }
  7069. // We can't query number of shader cores on Intel, use 32 as a placeholder
  7070. // so small convolutions will still choose a smaller tile.
  7071. const uint32_t shader_core_count = ctx->device->shader_core_count > 0 ? ctx->device->shader_core_count : 32;
  7072. if (elements[0] > 64 && tiles[CONV_SHAPE_128x128] >= shader_core_count * 2) {
  7073. shape = CONV_SHAPE_128x128;
  7074. } else if (elements[0] <= 32 && tiles[CONV_SHAPE_32x256] >= shader_core_count * 2) {
  7075. shape = CONV_SHAPE_32x256;
  7076. } else {
  7077. shape = CONV_SHAPE_64x32;
  7078. }
  7079. if (op == GGML_OP_CONV_2D) {
  7080. if (src0->type == GGML_TYPE_F32) {
  7081. return ctx->device->pipeline_conv2d_f32[shape];
  7082. } else if (src0->type == GGML_TYPE_F16) {
  7083. return ctx->device->pipeline_conv2d_f16_f32[shape];
  7084. }
  7085. } else if (op == GGML_OP_CONV_TRANSPOSE_2D) {
  7086. if (src0->type == GGML_TYPE_F32) {
  7087. return ctx->device->pipeline_conv_transpose_2d_f32[shape];
  7088. } else if (src0->type == GGML_TYPE_F16) {
  7089. return ctx->device->pipeline_conv_transpose_2d_f16_f32[shape];
  7090. }
  7091. }
  7092. }
  7093. return nullptr;
  7094. case GGML_OP_CONV_2D_DW:
  7095. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7096. if (ggml_is_contiguous(src1)) {
  7097. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  7098. } else if (ggml_is_contiguous_channels(src1)) {
  7099. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  7100. }
  7101. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  7102. if (ggml_is_contiguous(src1)) {
  7103. return ctx->device->pipeline_conv2d_dw_whcn_f16_f32;
  7104. } else if (ggml_is_contiguous_channels(src1)) {
  7105. return ctx->device->pipeline_conv2d_dw_cwhn_f16_f32;
  7106. }
  7107. }
  7108. return nullptr;
  7109. default:
  7110. return nullptr;
  7111. }
  7112. GGML_UNUSED(src2);
  7113. }
  7114. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  7115. switch (op) {
  7116. case GGML_OP_CPY:
  7117. case GGML_OP_GET_ROWS:
  7118. case GGML_OP_ADD:
  7119. case GGML_OP_SUB:
  7120. case GGML_OP_MUL:
  7121. case GGML_OP_DIV:
  7122. case GGML_OP_ADD_ID:
  7123. case GGML_OP_CONCAT:
  7124. case GGML_OP_UPSCALE:
  7125. case GGML_OP_SQR:
  7126. case GGML_OP_SQRT:
  7127. case GGML_OP_SIN:
  7128. case GGML_OP_COS:
  7129. case GGML_OP_CLAMP:
  7130. case GGML_OP_PAD:
  7131. case GGML_OP_REPEAT:
  7132. case GGML_OP_REPEAT_BACK:
  7133. case GGML_OP_ROPE:
  7134. case GGML_OP_RMS_NORM:
  7135. case GGML_OP_CONV_2D_DW:
  7136. case GGML_OP_IM2COL:
  7137. case GGML_OP_IM2COL_3D:
  7138. case GGML_OP_SET_ROWS:
  7139. case GGML_OP_SUM:
  7140. case GGML_OP_SUM_ROWS:
  7141. case GGML_OP_MEAN:
  7142. return true;
  7143. default:
  7144. return false;
  7145. }
  7146. }
  7147. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  7148. {
  7149. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  7150. }
  7151. 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) {
  7152. GGML_UNUSED(p);
  7153. GGML_UNUSED(src0);
  7154. GGML_UNUSED(src1);
  7155. GGML_UNUSED(src2);
  7156. GGML_UNUSED(dst);
  7157. static_assert(!std::is_const<T>::value, "unexpected type");
  7158. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  7159. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  7160. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  7161. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  7162. }
  7163. 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) {
  7164. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7165. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7166. p.misalign_offsets = (a_offset << 16) | d_offset;
  7167. GGML_UNUSED(src1);
  7168. GGML_UNUSED(src2);
  7169. }
  7170. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_sum_rows_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  7171. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7172. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7173. p.misalign_offsets = (a_offset << 16) | d_offset;
  7174. GGML_UNUSED(src1);
  7175. GGML_UNUSED(src2);
  7176. }
  7177. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_pad_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  7178. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7179. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7180. p.misalign_offsets = (a_offset << 16) | d_offset;
  7181. GGML_UNUSED(src1);
  7182. GGML_UNUSED(src2);
  7183. }
  7184. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_im2col_3d_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  7185. const uint32_t a_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  7186. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7187. p.misalign_offsets = (a_offset << 16) | d_offset;
  7188. GGML_UNUSED(src0);
  7189. GGML_UNUSED(src2);
  7190. }
  7191. 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) {
  7192. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7193. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  7194. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7195. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  7196. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  7197. GGML_UNUSED(src2);
  7198. }
  7199. 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) {
  7200. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7201. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7202. p.a_offset = a_offset;
  7203. p.d_offset = d_offset;
  7204. GGML_UNUSED(src1);
  7205. GGML_UNUSED(src2);
  7206. }
  7207. template<typename PC>
  7208. 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) {
  7209. 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];
  7210. if (src1 != nullptr) {
  7211. 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];
  7212. }
  7213. if (src2 != nullptr) {
  7214. 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];
  7215. }
  7216. 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];
  7217. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  7218. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  7219. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  7220. GGML_ASSERT(dst->buffer != nullptr);
  7221. const uint64_t ne00 = src0->ne[0];
  7222. const uint64_t ne01 = src0->ne[1];
  7223. const uint64_t ne02 = src0->ne[2];
  7224. const uint64_t ne03 = src0->ne[3];
  7225. const uint64_t ne0 = ne00 * ne01;
  7226. const bool use_src1 = src1 != nullptr;
  7227. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  7228. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  7229. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  7230. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  7231. const uint64_t ne1 = ne10 * ne11;
  7232. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  7233. const bool use_src2 = src2 != nullptr;
  7234. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  7235. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  7236. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  7237. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  7238. const uint64_t ne2 = ne20 * ne21;
  7239. const uint64_t ned0 = dst->ne[0];
  7240. const uint64_t ned1 = dst->ne[1];
  7241. const uint64_t ned2 = dst->ne[2];
  7242. const uint64_t ned3 = dst->ne[3];
  7243. const uint64_t ned = ned0 * ned1;
  7244. init_pushconst_fastdiv(pc);
  7245. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  7246. if (pipeline == nullptr) {
  7247. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  7248. if (src1 != nullptr) {
  7249. std::cerr << " and " << ggml_type_name(src1->type);
  7250. }
  7251. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  7252. GGML_ABORT("fatal error");
  7253. }
  7254. if (dryrun) {
  7255. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7256. return;
  7257. }
  7258. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  7259. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7260. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  7261. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  7262. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  7263. vk_buffer d_X = nullptr;
  7264. size_t x_buf_offset = 0;
  7265. vk_buffer d_Y = nullptr;
  7266. size_t y_buf_offset = 0;
  7267. vk_buffer d_Z = nullptr;
  7268. size_t z_buf_offset = 0;
  7269. bool src0_uma = false;
  7270. bool src1_uma = false;
  7271. bool src2_uma = false;
  7272. if (ctx->device->uma) {
  7273. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  7274. src0_uma = d_X != nullptr;
  7275. if (use_src1) {
  7276. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  7277. src1_uma = d_Y != nullptr;
  7278. }
  7279. if (use_src2) {
  7280. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  7281. src2_uma = d_Z != nullptr;
  7282. }
  7283. }
  7284. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  7285. GGML_ASSERT(d_D != nullptr);
  7286. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  7287. if(!src0_uma) {
  7288. d_X = src0_buf_ctx->dev_buffer;
  7289. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  7290. GGML_ASSERT(d_X != nullptr);
  7291. }
  7292. if (use_src1 && !src1_uma) {
  7293. d_Y = src1_buf_ctx->dev_buffer;
  7294. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  7295. GGML_ASSERT(d_Y != nullptr);
  7296. }
  7297. if (use_src2 && !src2_uma) {
  7298. d_Z = src2_buf_ctx->dev_buffer;
  7299. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  7300. GGML_ASSERT(d_Z != nullptr);
  7301. }
  7302. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  7303. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  7304. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7305. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7306. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7307. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7308. std::array<uint32_t, 3> elements;
  7309. // Single call if dimension 2 is contiguous
  7310. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  7311. switch (op) {
  7312. case GGML_OP_NORM:
  7313. case GGML_OP_RMS_NORM_BACK:
  7314. case GGML_OP_L2_NORM:
  7315. case GGML_OP_SOFT_MAX:
  7316. case GGML_OP_SOFT_MAX_BACK:
  7317. case GGML_OP_SUM_ROWS:
  7318. case GGML_OP_MEAN:
  7319. case GGML_OP_ARGMAX:
  7320. {
  7321. const uint32_t nr = ggml_nrows(src0);
  7322. if (nr > 262144) {
  7323. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  7324. } else if (nr > 512) {
  7325. elements = { 512, CEIL_DIV(nr, 512), 1 };
  7326. } else {
  7327. elements = { nr, 1, 1 };
  7328. }
  7329. } break;
  7330. case GGML_OP_RMS_NORM:
  7331. if (ctx->do_add_rms_partials) {
  7332. // Run one element per thread, 128 threads per workgroup
  7333. elements = { (uint32_t)CEIL_DIV(ne00, 128), 1, 1 };
  7334. } else {
  7335. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  7336. }
  7337. break;
  7338. case GGML_OP_SUM:
  7339. // We use GGML_OP_SUM_ROWS with 1 row.
  7340. elements = { 1, 1, 1 };
  7341. break;
  7342. case GGML_OP_GROUP_NORM:
  7343. {
  7344. const uint32_t num_groups = dst->op_params[0];
  7345. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  7346. } break;
  7347. case GGML_OP_DIAG_MASK_INF:
  7348. case GGML_OP_ROPE:
  7349. case GGML_OP_ROPE_BACK:
  7350. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  7351. break;
  7352. case GGML_OP_GET_ROWS:
  7353. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  7354. elements[1] = std::min(elements[1], ctx->device->properties.limits.maxComputeWorkGroupCount[1]);
  7355. elements[2] = std::min(elements[2], ctx->device->properties.limits.maxComputeWorkGroupCount[2]);
  7356. break;
  7357. case GGML_OP_ARGSORT:
  7358. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  7359. break;
  7360. case GGML_OP_IM2COL:
  7361. {
  7362. const bool is_2D = dst->op_params[6] == 1;
  7363. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  7364. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  7365. const uint32_t KW = src0->ne[0];
  7366. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  7367. const uint32_t OW = dst->ne[1];
  7368. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  7369. elements = { OW * KW * KH, OH, batch * IC };
  7370. } break;
  7371. case GGML_OP_IM2COL_3D:
  7372. {
  7373. const uint32_t IC = ((const uint32_t *)(dst->op_params))[9];
  7374. const uint32_t N = ne13 / IC;
  7375. const uint32_t KD = ne02;
  7376. const uint32_t KH = ne01;
  7377. const uint32_t KW = ne00;
  7378. const uint32_t OD = ned3 / N;
  7379. const uint32_t OH = ned2;
  7380. const uint32_t OW = ned1;
  7381. const uint32_t IC_KD_KH_KW = IC*KD*KH*KW;
  7382. const uint32_t N_OD_OH = N*OD*OH;
  7383. elements = { IC_KD_KH_KW, OW, N_OD_OH };
  7384. elements[2] = std::min(elements[2], ctx->device->properties.limits.maxComputeWorkGroupCount[2]);
  7385. } break;
  7386. case GGML_OP_TIMESTEP_EMBEDDING:
  7387. {
  7388. const uint32_t dim = dst->op_params[0];
  7389. uint32_t half_ceil = (dim + 1) / 2;
  7390. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  7391. } break;
  7392. case GGML_OP_CONV_TRANSPOSE_1D:
  7393. {
  7394. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  7395. } break;
  7396. case GGML_OP_POOL_2D:
  7397. {
  7398. const uint32_t N = dst->ne[3];
  7399. const uint32_t OC = dst->ne[2];
  7400. const uint32_t OH = dst->ne[1];
  7401. const uint32_t OW = dst->ne[0];
  7402. elements = { N * OC * OH * OW, 1, 1};
  7403. } break;
  7404. case GGML_OP_CONV_2D:
  7405. {
  7406. elements = ggml_vk_get_conv_elements(dst);
  7407. } break;
  7408. case GGML_OP_CONV_TRANSPOSE_2D:
  7409. {
  7410. elements = ggml_vk_get_conv_transpose_2d_elements(dst);
  7411. } break;
  7412. case GGML_OP_ADD:
  7413. case GGML_OP_SUB:
  7414. case GGML_OP_DIV:
  7415. case GGML_OP_MUL:
  7416. case GGML_OP_SCALE:
  7417. case GGML_OP_SQR:
  7418. case GGML_OP_SQRT:
  7419. case GGML_OP_SIN:
  7420. case GGML_OP_COS:
  7421. case GGML_OP_CLAMP:
  7422. case GGML_OP_PAD:
  7423. case GGML_OP_ROLL:
  7424. case GGML_OP_REPEAT:
  7425. case GGML_OP_REPEAT_BACK:
  7426. case GGML_OP_CPY:
  7427. case GGML_OP_CONCAT:
  7428. case GGML_OP_UPSCALE:
  7429. case GGML_OP_UNARY:
  7430. case GGML_OP_GLU:
  7431. case GGML_OP_CONV_2D_DW:
  7432. {
  7433. uint32_t ne = ggml_nelements(dst);
  7434. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7435. // Convert from number of logical elements to 2- or 4-byte units.
  7436. ne /= ggml_blck_size(src0->type);
  7437. if ((ggml_type_size(src0->type) % 4) == 0) {
  7438. ne *= ggml_type_size(src0->type) / 4;
  7439. } else {
  7440. ne *= ggml_type_size(src0->type) / 2;
  7441. }
  7442. }
  7443. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  7444. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  7445. // So divide by block size here before splitting into 512x512 groups.
  7446. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7447. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  7448. }
  7449. if (ne > 262144) {
  7450. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7451. } else if (ne > 512) {
  7452. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7453. } else {
  7454. elements = { ne, 1, 1 };
  7455. }
  7456. } break;
  7457. case GGML_OP_ADD_ID:
  7458. {
  7459. elements = { (uint32_t)ne01, (uint32_t)ne02, 1 };
  7460. } break;
  7461. case GGML_OP_SET_ROWS:
  7462. {
  7463. uint32_t ne = ggml_nelements(src0);
  7464. if (ggml_is_quantized(dst->type)) {
  7465. // quants run 32 threads each doing QUANT_K elements
  7466. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  7467. } else {
  7468. // scalar types do one element per thread, running 512 threads
  7469. ne = CEIL_DIV(ne, 512);
  7470. }
  7471. if (ne > 262144) {
  7472. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7473. } else if (ne > 512) {
  7474. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7475. } else {
  7476. elements = { ne, 1, 1 };
  7477. }
  7478. }
  7479. break;
  7480. case GGML_OP_SSM_CONV:
  7481. {
  7482. const uint32_t nr = src0->ne[1];
  7483. const uint32_t n_t = dst->ne[1];
  7484. const uint32_t n_s = dst->ne[2];
  7485. elements = { nr, n_t, n_s };
  7486. }
  7487. break;
  7488. default:
  7489. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  7490. break;
  7491. }
  7492. uint64_t x_sz, y_sz, z_sz, d_sz;
  7493. if (op_supports_incontiguous) {
  7494. x_sz = ggml_nbytes(src0) + get_misalign_bytes(ctx, src0);
  7495. y_sz = use_src1 ? ggml_nbytes(src1) + get_misalign_bytes(ctx, src1) : 0;
  7496. z_sz = use_src2 ? ggml_nbytes(src2) + get_misalign_bytes(ctx, src2) : 0;
  7497. d_sz = ggml_nbytes(dst) + get_misalign_bytes(ctx, dst);
  7498. if (x_buf_offset + x_sz >= d_X->size) {
  7499. x_sz = ggml_vk_get_max_buffer_range(ctx, d_X, x_buf_offset);
  7500. }
  7501. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  7502. y_sz = ggml_vk_get_max_buffer_range(ctx, d_Y, y_buf_offset);
  7503. }
  7504. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  7505. z_sz = ggml_vk_get_max_buffer_range(ctx, d_Z, z_buf_offset);
  7506. }
  7507. if (d_buf_offset + d_sz >= d_D->size) {
  7508. d_sz = ggml_vk_get_max_buffer_range(ctx, d_D, d_buf_offset);
  7509. }
  7510. } else {
  7511. x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0 * ne02 * ne03;
  7512. y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 * ne12 * ne13 : 0;
  7513. z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 * ne22 * ne23 : 0;
  7514. d_sz = ggml_type_size(dst->type) * ned * ned2 * ned3;
  7515. }
  7516. if (op == GGML_OP_ADD || op == GGML_OP_RMS_NORM) {
  7517. vk_buffer d_A = ctx->do_add_rms_partials ? ctx->prealloc_add_rms_partials : d_X;
  7518. size_t a_buf_offset = ctx->do_add_rms_partials ? ctx->prealloc_size_add_rms_partials_offset : 0;
  7519. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  7520. { vk_subbuffer{ d_X, x_buf_offset, x_sz },
  7521. vk_subbuffer{ d_Y, y_buf_offset, y_sz },
  7522. vk_subbuffer{ d_D, d_buf_offset, d_sz },
  7523. ggml_vk_subbuffer(ctx, d_A, a_buf_offset),
  7524. }, pc, elements);
  7525. } else if (op == GGML_OP_GLU) {
  7526. // Empty src1 is possible in glu, but the shader needs a buffer
  7527. vk_subbuffer subbuf_y;
  7528. if (use_src1) {
  7529. subbuf_y = { d_Y, y_buf_offset, y_sz };
  7530. } else {
  7531. subbuf_y = { d_X, 0, x_sz };
  7532. }
  7533. 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);
  7534. } else if (op == GGML_OP_SOFT_MAX) {
  7535. // Empty src1 and src2 is possible in soft_max, but the shader needs a buffer
  7536. vk_subbuffer subbuf_y;
  7537. if (use_src1) {
  7538. subbuf_y = { d_Y, y_buf_offset, y_sz };
  7539. } else {
  7540. subbuf_y = { d_X, 0, x_sz };
  7541. }
  7542. vk_subbuffer subbuf_z;
  7543. if (use_src2) {
  7544. subbuf_z = { d_Z, z_buf_offset, z_sz };
  7545. } else {
  7546. subbuf_z = { d_X, 0, x_sz };
  7547. }
  7548. 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);
  7549. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  7550. // Empty src2 is possible in rope, but the shader needs a buffer
  7551. vk_subbuffer subbuf_z;
  7552. if (use_src2) {
  7553. subbuf_z = { d_Z, z_buf_offset, z_sz };
  7554. } else {
  7555. subbuf_z = { d_X, 0, x_sz };
  7556. }
  7557. 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);
  7558. } else if (op == GGML_OP_IM2COL || op == GGML_OP_IM2COL_3D) {
  7559. if (ctx->device->shader_int64 && ctx->device->buffer_device_address) {
  7560. // buffer device address path doesn't use dst buffer
  7561. d_sz = 1;
  7562. }
  7563. // im2col uses only src1 and dst buffers
  7564. 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);
  7565. } else if (op == GGML_OP_COUNT_EQUAL) {
  7566. // count_equal assumes that destination buffer is initialized with zeroes
  7567. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  7568. ggml_vk_sync_buffers(ctx, subctx);
  7569. 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);
  7570. } else if (op == GGML_OP_OPT_STEP_SGD) {
  7571. // OPT_STEP_SGD works on src0, it does not need dst
  7572. 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 } }, pc, elements);
  7573. } else if (use_src2) {
  7574. 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);
  7575. } else if (use_src1) {
  7576. 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);
  7577. } else {
  7578. 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);
  7579. }
  7580. }
  7581. 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) {
  7582. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7583. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7584. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7585. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  7586. (uint32_t)ggml_nelements(src0),
  7587. (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,
  7588. (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,
  7589. (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,
  7590. 0,
  7591. 0.0f, 0.0f, 0,
  7592. }, dryrun);
  7593. }
  7594. 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) {
  7595. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7596. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7597. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7598. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  7599. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  7600. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  7601. int offset = dst->op_params[3] / 4; // offset in bytes
  7602. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  7603. (uint32_t)ggml_nelements(src0),
  7604. (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,
  7605. (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,
  7606. (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,
  7607. 0,
  7608. 0.0f, 0.0f, offset,
  7609. }, dryrun);
  7610. }
  7611. static void ggml_vk_multi_add(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cgraph * cgraph, int node_idx, bool dryrun = false) {
  7612. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  7613. const ggml_tensor *dst = cgraph->nodes[node_idx + ctx->num_additional_fused_ops];
  7614. // Make a list of all the tensors used by the op.
  7615. // Last element of the list is the dest tensor.
  7616. const ggml_tensor *tensors[MAX_PARAMETER_COUNT];
  7617. uint32_t num_srcs = ctx->num_additional_fused_ops + 2;
  7618. uint32_t num_tensors = num_srcs + 1;
  7619. GGML_ASSERT(num_tensors + ctx->do_add_rms_partials <= MAX_PARAMETER_COUNT);
  7620. tensors[0] = first_node->src[0];
  7621. tensors[1] = first_node->src[1];
  7622. for (int32_t i = 0; i < ctx->num_additional_fused_ops; ++i) {
  7623. // check whether the previous result is src[0] or src[1]
  7624. if (cgraph->nodes[node_idx + i] == cgraph->nodes[node_idx + i + 1]->src[0]) {
  7625. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[1];
  7626. } else {
  7627. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[0];
  7628. }
  7629. }
  7630. tensors[num_srcs] = dst;
  7631. vk_op_multi_add_push_constants pc;
  7632. pc.ne20 = (uint32_t)dst->ne[0];
  7633. pc.ne21 = (uint32_t)dst->ne[1];
  7634. pc.ne22 = (uint32_t)dst->ne[2];
  7635. pc.ne23 = (uint32_t)dst->ne[3];
  7636. for (uint32_t i = 0; i < num_tensors; ++i) {
  7637. const ggml_tensor *t = tensors[i];
  7638. pc.nb[i][0] = (uint32_t)t->nb[0] / sizeof(float);
  7639. pc.nb[i][1] = (uint32_t)t->nb[1] / sizeof(float);
  7640. pc.nb[i][2] = (uint32_t)t->nb[2] / sizeof(float);
  7641. pc.nb[i][3] = (uint32_t)t->nb[3] / sizeof(float);
  7642. }
  7643. pc.rms_partials = ctx->do_add_rms_partials;
  7644. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, tensors[0], tensors[1], nullptr, dst, dst->op);
  7645. if (pipeline == nullptr) {
  7646. std::cerr << "ggml_vulkan: Error: Missing multi_add";
  7647. GGML_ABORT("fatal error");
  7648. }
  7649. if (dryrun) {
  7650. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7651. return;
  7652. }
  7653. ggml_backend_vk_buffer_context * buf_ctx[MAX_PARAMETER_COUNT];
  7654. vk_buffer buf[MAX_PARAMETER_COUNT];
  7655. size_t offset[MAX_PARAMETER_COUNT];
  7656. bool uma[MAX_PARAMETER_COUNT];
  7657. for (uint32_t i = 0; i < num_tensors; ++i) {
  7658. buf_ctx[i] = (ggml_backend_vk_buffer_context *)tensors[i]->buffer->context;
  7659. buf[i] = nullptr;
  7660. offset[i] = 0;
  7661. uma[i] = false;
  7662. if (ctx->device->uma) {
  7663. ggml_vk_host_get(ctx->device, tensors[i]->data, buf[i], offset[i]);
  7664. uma[i] = buf[i] != nullptr;
  7665. }
  7666. if (!uma[i]) {
  7667. buf[i] = buf_ctx[i]->dev_buffer;
  7668. offset[i] = vk_tensor_offset(tensors[i]) + tensors[i]->view_offs;
  7669. }
  7670. GGML_ASSERT(buf[i] != nullptr);
  7671. }
  7672. // If any remaining descriptors are unused, just point them at src[0]
  7673. for (uint32_t i = num_tensors; i < MAX_PARAMETER_COUNT; ++i) {
  7674. buf[i] = buf[0];
  7675. offset[i] = 0;
  7676. }
  7677. if (ctx->do_add_rms_partials) {
  7678. buf[num_tensors] = ctx->prealloc_add_rms_partials;
  7679. offset[num_tensors] = ctx->prealloc_size_add_rms_partials_offset;
  7680. }
  7681. std::array<uint32_t, 3> elements;
  7682. uint32_t ne = ggml_nelements(dst);
  7683. if (ne > 262144) {
  7684. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7685. } else if (ne > 512) {
  7686. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7687. } else {
  7688. elements = { ne, 1, 1 };
  7689. }
  7690. static_assert(MAX_PARAMETER_COUNT == 12);
  7691. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  7692. {
  7693. ggml_vk_subbuffer(ctx, buf[0], offset[0]),
  7694. ggml_vk_subbuffer(ctx, buf[1], offset[1]),
  7695. ggml_vk_subbuffer(ctx, buf[2], offset[2]),
  7696. ggml_vk_subbuffer(ctx, buf[3], offset[3]),
  7697. ggml_vk_subbuffer(ctx, buf[4], offset[4]),
  7698. ggml_vk_subbuffer(ctx, buf[5], offset[5]),
  7699. ggml_vk_subbuffer(ctx, buf[6], offset[6]),
  7700. ggml_vk_subbuffer(ctx, buf[7], offset[7]),
  7701. ggml_vk_subbuffer(ctx, buf[8], offset[8]),
  7702. ggml_vk_subbuffer(ctx, buf[9], offset[9]),
  7703. ggml_vk_subbuffer(ctx, buf[10], offset[10]),
  7704. ggml_vk_subbuffer(ctx, buf[11], offset[11]),
  7705. }, pc, elements);
  7706. }
  7707. 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) {
  7708. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7709. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7710. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7711. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  7712. (uint32_t)ggml_nelements(src0),
  7713. (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,
  7714. (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,
  7715. (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,
  7716. 0,
  7717. 0.0f, 0.0f, ctx->do_add_rms_partials,
  7718. }, dryrun);
  7719. }
  7720. 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) {
  7721. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7722. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7723. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7724. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  7725. (uint32_t)ggml_nelements(src0),
  7726. (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,
  7727. (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,
  7728. (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,
  7729. 0,
  7730. 0.0f, 0.0f, 0,
  7731. }, dryrun);
  7732. }
  7733. 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) {
  7734. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7735. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7736. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7737. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  7738. (uint32_t)ggml_nelements(src0),
  7739. (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,
  7740. (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,
  7741. (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,
  7742. 0,
  7743. 0.0f, 0.0f, 0,
  7744. }, dryrun);
  7745. }
  7746. 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) {
  7747. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7748. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7749. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7750. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  7751. (uint32_t)ggml_nelements(src0),
  7752. (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,
  7753. (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,
  7754. (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,
  7755. 0,
  7756. 0.0f, 0.0f, 0,
  7757. }, dryrun);
  7758. }
  7759. 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) {
  7760. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7761. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7762. const uint32_t src2_type_size = ggml_type_size(src2->type);
  7763. ggml_vk_op_f32<vk_op_add_id_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ADD_ID, {
  7764. (uint32_t)dst->ne[0],
  7765. (uint32_t)dst->ne[1],
  7766. (uint32_t)src0->nb[1] / src0_type_size,
  7767. (uint32_t)src0->nb[2] / src0_type_size,
  7768. (uint32_t)src1->nb[1] / src1_type_size,
  7769. (uint32_t)src2->nb[1] / src2_type_size,
  7770. }, dryrun);
  7771. }
  7772. 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) {
  7773. GGML_ASSERT(version == 6 || version == 7);
  7774. int num_srcs = version == 6 ? 6 : 7;
  7775. for (int i = 0; i < num_srcs; i++) {
  7776. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  7777. }
  7778. GGML_ASSERT(dst->buffer != nullptr);
  7779. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  7780. GGML_ASSERT(pipeline != nullptr);
  7781. if (dryrun) {
  7782. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7783. return;
  7784. }
  7785. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7786. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  7787. for (int i = 0; i < num_srcs; i++) {
  7788. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  7789. }
  7790. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  7791. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  7792. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  7793. if (ctx->device->uma) {
  7794. for (int i = 0; i < num_srcs; i++) {
  7795. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  7796. srcs_uma[i] = d_srcs[i] != nullptr;
  7797. }
  7798. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  7799. dst_uma = d_D != nullptr;
  7800. }
  7801. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  7802. for (int i = 0; i < num_srcs; i++) {
  7803. src_sizes[i] = ggml_nbytes(dst->src[i]);
  7804. if (!srcs_uma[i]) {
  7805. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  7806. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  7807. }
  7808. }
  7809. const uint64_t dst_size = ggml_nbytes(dst);
  7810. if (!dst_uma) {
  7811. d_D = dst_buf_ctx->dev_buffer;
  7812. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  7813. }
  7814. std::array<uint32_t, 3> elements = {
  7815. (uint32_t)(pc.B * pc.H),
  7816. 1,
  7817. 1
  7818. };
  7819. if (version == 6) {
  7820. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7821. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  7822. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  7823. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  7824. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  7825. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  7826. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  7827. vk_subbuffer{ d_D, dst_offset, dst_size }
  7828. }, pc, elements);
  7829. } else if (version == 7) {
  7830. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7831. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  7832. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  7833. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  7834. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  7835. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  7836. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  7837. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  7838. vk_subbuffer{ d_D, dst_offset, dst_size }
  7839. }, pc, elements);
  7840. } else {
  7841. // shouldn't happen
  7842. GGML_ASSERT(false);
  7843. }
  7844. }
  7845. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7846. const size_t seq_length = dst->src[0]->ne[2];
  7847. const size_t n_embed = dst->ne[0];
  7848. const size_t n_heads = dst->src[0]->ne[1];
  7849. const size_t n_seqs = dst->src[5]->ne[1];
  7850. ggml_vk_op_f32_wkv(
  7851. ctx, subctx, dst,
  7852. {
  7853. (uint32_t)n_seqs,
  7854. (uint32_t)seq_length,
  7855. (uint32_t)n_embed,
  7856. (uint32_t)n_heads,
  7857. },
  7858. 6,
  7859. dryrun
  7860. );
  7861. }
  7862. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7863. const size_t seq_length = dst->src[0]->ne[2];
  7864. const size_t n_embed = dst->ne[0];
  7865. const size_t n_heads = dst->src[0]->ne[1];
  7866. const size_t n_seqs = dst->src[6]->ne[1];
  7867. ggml_vk_op_f32_wkv(
  7868. ctx, subctx, dst,
  7869. {
  7870. (uint32_t)n_seqs,
  7871. (uint32_t)seq_length,
  7872. (uint32_t)n_embed,
  7873. (uint32_t)n_heads,
  7874. },
  7875. 7,
  7876. dryrun
  7877. );
  7878. }
  7879. static void ggml_vk_ssm_scan(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7880. const ggml_tensor * src0 = dst->src[0];
  7881. const ggml_tensor * src1 = dst->src[1];
  7882. const ggml_tensor * src2 = dst->src[2];
  7883. const ggml_tensor * src3 = dst->src[3];
  7884. const ggml_tensor * src4 = dst->src[4];
  7885. const ggml_tensor * src5 = dst->src[5];
  7886. GGML_ASSERT(dst->buffer != nullptr);
  7887. const uint32_t head_dim = src0->ne[1];
  7888. const uint32_t n_head = src1->ne[1];
  7889. const uint32_t n_group = src4->ne[1];
  7890. const uint32_t n_tok = src1->ne[2];
  7891. const uint32_t n_seq = src1->ne[3];
  7892. bool is_mamba2 = (src3->nb[1] == sizeof(float));
  7893. GGML_ASSERT(is_mamba2);
  7894. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, dst->op);
  7895. GGML_ASSERT(pipeline != nullptr);
  7896. if (dryrun) {
  7897. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7898. return;
  7899. }
  7900. const int64_t s_off = ggml_nelements(src1) * sizeof(float);
  7901. const vk_op_ssm_scan_push_constants pc = {
  7902. (uint32_t)src0->nb[2], (uint32_t)src0->nb[3],
  7903. (uint32_t)src1->nb[2], (uint32_t)src1->nb[3],
  7904. (uint32_t)src2->nb[1], (uint32_t)src2->nb[2],
  7905. (uint32_t)src3->nb[1],
  7906. (uint32_t)src4->nb[2], (uint32_t)src4->nb[3],
  7907. (uint32_t)src5->nb[2], (uint32_t)src5->nb[3],
  7908. (uint32_t)s_off,
  7909. n_head, head_dim, n_group, n_tok
  7910. };
  7911. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7912. ggml_backend_vk_buffer_context * src_buf_ctxs[GGML_MAX_SRC];
  7913. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  7914. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  7915. }
  7916. vk_buffer d_D = nullptr, d_srcs[GGML_MAX_SRC] = { nullptr };
  7917. size_t dst_offset = 0, src_offsets[GGML_MAX_SRC] = { 0 };
  7918. bool dst_uma = false, srcs_uma[GGML_MAX_SRC] = { false };
  7919. if (ctx->device->uma) {
  7920. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  7921. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  7922. srcs_uma[i] = d_srcs[i] != nullptr;
  7923. }
  7924. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  7925. dst_uma = d_D != nullptr;
  7926. }
  7927. if (!dst_uma) {
  7928. d_D = dst_buf_ctx->dev_buffer;
  7929. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  7930. }
  7931. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  7932. if (!srcs_uma[i]) {
  7933. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  7934. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  7935. }
  7936. }
  7937. size_t dst_size = ggml_nbytes(dst);
  7938. size_t src_sizes[GGML_MAX_SRC];
  7939. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  7940. src_sizes[i] = ggml_nbytes(dst->src[i]);
  7941. }
  7942. std::array<uint32_t, 3> elements;
  7943. const int splitH = 16;
  7944. const uint32_t num_workgroups_x = CEIL_DIV(n_head * head_dim, splitH);
  7945. const uint32_t num_workgroups_y = n_seq;
  7946. elements = { num_workgroups_x, num_workgroups_y, 1 };
  7947. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  7948. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  7949. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  7950. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  7951. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  7952. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  7953. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  7954. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  7955. vk_subbuffer{ d_D, dst_offset, dst_size }
  7956. }, pc, elements);
  7957. }
  7958. static void ggml_vk_ssm_conv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  7959. const ggml_tensor * src0 = dst->src[0];
  7960. const ggml_tensor * src1 = dst->src[1];
  7961. ggml_vk_op_f32<vk_op_ssm_conv_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SSM_CONV, {
  7962. (uint32_t)src0->nb[1], (uint32_t)src0->nb[2],
  7963. (uint32_t)src1->nb[1],
  7964. (uint32_t)dst->nb[0], (uint32_t)dst->nb[1], (uint32_t)dst->nb[2],
  7965. (uint32_t)src1->ne[0],
  7966. (uint32_t)src0->ne[0],
  7967. (uint32_t)src0->ne[1],
  7968. (uint32_t)dst->ne[1],
  7969. (uint32_t)dst->ne[2],
  7970. }, dryrun);
  7971. }
  7972. 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) {
  7973. const ggml_tensor * x = dst->src[0];
  7974. const ggml_tensor * g = dst->src[1];
  7975. const ggml_tensor * gm = dst->src[2];
  7976. const ggml_tensor * gv = dst->src[3];
  7977. const ggml_tensor * p = dst->src[4];
  7978. GGML_ASSERT(x->type == GGML_TYPE_F32);
  7979. GGML_ASSERT(g->type == GGML_TYPE_F32);
  7980. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  7981. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  7982. GGML_ASSERT(p->type == GGML_TYPE_F32);
  7983. GGML_ASSERT(dst->buffer != nullptr);
  7984. GGML_ASSERT(ggml_is_contiguous(x));
  7985. GGML_ASSERT(ggml_is_contiguous(g));
  7986. GGML_ASSERT(ggml_is_contiguous(gm));
  7987. GGML_ASSERT(ggml_is_contiguous(gv));
  7988. GGML_ASSERT(ggml_is_contiguous(p));
  7989. GGML_ASSERT(ggml_are_same_shape(x, g));
  7990. GGML_ASSERT(ggml_are_same_shape(x, gm));
  7991. GGML_ASSERT(ggml_are_same_shape(x, gv));
  7992. GGML_ASSERT(ggml_nelements(p) == 7);
  7993. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  7994. GGML_ASSERT(pipeline != nullptr);
  7995. if (dryrun) {
  7996. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7997. return;
  7998. }
  7999. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  8000. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  8001. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  8002. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  8003. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  8004. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  8005. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  8006. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  8007. if (ctx->device->uma) {
  8008. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  8009. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  8010. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  8011. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  8012. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  8013. X_uma = d_X != nullptr;
  8014. G_uma = d_G != nullptr;
  8015. GM_uma = d_GM != nullptr;
  8016. GV_uma = d_GV != nullptr;
  8017. P_uma = d_P != nullptr;
  8018. }
  8019. if (!X_uma) {
  8020. d_X = x_buf_ctx->dev_buffer;
  8021. x_offset = vk_tensor_offset(x) + x->view_offs;
  8022. }
  8023. if (!G_uma) {
  8024. d_G = g_buf_ctx->dev_buffer;
  8025. g_offset = vk_tensor_offset(g) + g->view_offs;
  8026. }
  8027. if (!GM_uma) {
  8028. d_GM = gm_buf_ctx->dev_buffer;
  8029. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  8030. }
  8031. if (!GV_uma) {
  8032. d_GV = gv_buf_ctx->dev_buffer;
  8033. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  8034. }
  8035. if (!P_uma) {
  8036. d_P = p_buf_ctx->dev_buffer;
  8037. p_offset = vk_tensor_offset(p) + p->view_offs;
  8038. }
  8039. const uint64_t x_size = ggml_nbytes(x);
  8040. const uint64_t g_size = ggml_nbytes(g);
  8041. const uint64_t gm_size = ggml_nbytes(gm);
  8042. const uint64_t gv_size = ggml_nbytes(gv);
  8043. const uint64_t p_size = ggml_nbytes(p);
  8044. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  8045. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  8046. vk_subbuffer{ d_X, x_offset, x_size },
  8047. vk_subbuffer{ d_G, g_offset, g_size },
  8048. vk_subbuffer{ d_GM, gm_offset, gm_size },
  8049. vk_subbuffer{ d_GV, gv_offset, gv_size },
  8050. vk_subbuffer{ d_P, p_offset, p_size },
  8051. }, pc, elements);
  8052. }
  8053. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  8054. const size_t n = ggml_nelements(dst->src[0]);
  8055. ggml_vk_op_f32_opt_step_adamw(
  8056. ctx, subctx, dst,
  8057. { (uint32_t)n, 0, 0.0f, 0.0f },
  8058. dryrun
  8059. );
  8060. }
  8061. static void ggml_vk_opt_step_sgd(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) {
  8062. const size_t n = ggml_nelements(dst->src[0]);
  8063. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_OPT_STEP_SGD, { (uint32_t)n, 0, 0.0f, 0.0f }, dryrun);
  8064. }
  8065. 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) {
  8066. int * op_params = (int *)dst->op_params;
  8067. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8068. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8069. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8070. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  8071. (uint32_t)ggml_nelements(dst),
  8072. (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,
  8073. (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,
  8074. (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,
  8075. 0,
  8076. 0.0f, 0.0f, op_params[0],
  8077. }, dryrun);
  8078. }
  8079. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8080. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8081. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  8082. float sf0 = (float)dst->ne[0] / src0->ne[0];
  8083. float sf1 = (float)dst->ne[1] / src0->ne[1];
  8084. float sf2 = (float)dst->ne[2] / src0->ne[2];
  8085. float sf3 = (float)dst->ne[3] / src0->ne[3];
  8086. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  8087. sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
  8088. sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
  8089. }
  8090. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  8091. (uint32_t)ggml_nelements(dst), 0, 0,
  8092. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1],
  8093. (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,
  8094. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  8095. sf0, sf1, sf2, sf3,
  8096. }, dryrun);
  8097. }
  8098. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8099. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  8100. p.param1 = ggml_get_op_params_f32(dst, 0);
  8101. p.param2 = ggml_get_op_params_f32(dst, 1);
  8102. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  8103. }
  8104. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8105. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  8106. }
  8107. static void ggml_vk_sqrt(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8108. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQRT, vk_op_unary_push_constants_init(src0, dst), dryrun);
  8109. }
  8110. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8111. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  8112. }
  8113. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8114. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  8115. }
  8116. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8117. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  8118. p.param1 = ggml_get_op_params_f32(dst, 0);
  8119. p.param2 = ggml_get_op_params_f32(dst, 1);
  8120. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  8121. }
  8122. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8123. vk_op_pad_push_constants p = vk_op_pad_push_constants_init(src0, dst);
  8124. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  8125. }
  8126. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8127. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  8128. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  8129. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  8130. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  8131. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  8132. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  8133. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  8134. memcpy(&p.param1, &s01_packed, sizeof(float));
  8135. memcpy(&p.param2, &s23_packed, sizeof(float));
  8136. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  8137. }
  8138. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8139. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  8140. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  8141. }
  8142. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8143. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  8144. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  8145. }
  8146. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8147. uint32_t ne = (uint32_t)ggml_nelements(src0);
  8148. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  8149. // Convert from number of logical elements to 2- or 4-byte units.
  8150. ne /= ggml_blck_size(src0->type);
  8151. if ((ggml_type_size(src0->type) % 4) == 0) {
  8152. ne *= ggml_type_size(src0->type) / 4;
  8153. } else {
  8154. ne *= ggml_type_size(src0->type) / 2;
  8155. }
  8156. }
  8157. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  8158. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  8159. }
  8160. 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) {
  8161. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8162. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8163. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8164. // Skip empty skip_rows operations. For most ops the empty check at the start
  8165. // of ggml_vk_build_graph is sufficient, but set_rows can have a nonempty dst
  8166. // with empty srcs.
  8167. if (ggml_is_empty(src0) || ggml_is_empty(src1)) {
  8168. return;
  8169. }
  8170. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  8171. (uint32_t)ggml_nelements(src0),
  8172. (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,
  8173. (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,
  8174. (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,
  8175. 0,
  8176. 0.0f, 0.0f, 0,
  8177. }, dryrun);
  8178. }
  8179. 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) {
  8180. 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);
  8181. }
  8182. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8183. float * op_params = (float *)dst->op_params;
  8184. 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);
  8185. }
  8186. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8187. const int * int_op_params = (const int *)dst->op_params;
  8188. const float * float_op_params = (const float *)dst->op_params;
  8189. const uint32_t num_groups = int_op_params[0];
  8190. const float eps = float_op_params[1];
  8191. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  8192. 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);
  8193. }
  8194. static uint32_t ggml_vk_rms_num_partials(ggml_backend_vk_context * ctx, const ggml_tensor *node) {
  8195. const uint32_t ne = (uint32_t)node->ne[0];
  8196. const uint32_t denom = ctx->device->pipeline_add_rms[0][0][0]->wg_denoms[0];
  8197. const uint32_t num_partials = CEIL_DIV(ne, denom);
  8198. return num_partials;
  8199. }
  8200. static uint32_t ggml_vk_rms_partials_size(ggml_backend_vk_context * ctx, const ggml_tensor *node) {
  8201. const uint32_t num_partials = ggml_vk_rms_num_partials(ctx, node);
  8202. const uint32_t num_bytes = ROUNDUP_POW2(num_partials * sizeof(uint32_t), ctx->device->partials_binding_alignment);
  8203. return num_bytes;
  8204. }
  8205. 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) {
  8206. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8207. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8208. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8209. uint32_t param3 = ctx->do_add_rms_partials ? ggml_vk_rms_num_partials(ctx, dst) : 0;
  8210. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  8211. (uint32_t)ggml_nelements(src0),
  8212. (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,
  8213. (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,
  8214. (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,
  8215. 0,
  8216. op_params[0], 0.0f, (int32_t)param3,
  8217. }, dryrun);
  8218. if (ctx->do_add_rms_partials) {
  8219. ctx->prealloc_size_add_rms_partials_offset += ggml_vk_rms_partials_size(ctx, src0);
  8220. ctx->do_add_rms_partials = false;
  8221. }
  8222. }
  8223. 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) {
  8224. float * op_params = (float *)dst->op_params;
  8225. 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);
  8226. }
  8227. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8228. float * op_params = (float *)dst->op_params;
  8229. 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);
  8230. }
  8231. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8232. 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);
  8233. }
  8234. 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) {
  8235. const float * op_params_f = (const float *)dst->op_params;
  8236. const bool swapped = (bool)dst->op_params[1];
  8237. const bool split = src1 != nullptr;
  8238. const float alpha = op_params_f[2];
  8239. const float limit = op_params_f[3];
  8240. GGML_ASSERT(ggml_is_contiguous(src0));
  8241. if (!split) {
  8242. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  8243. } else {
  8244. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  8245. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  8246. GGML_ASSERT(src0->type == src1->type);
  8247. }
  8248. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  8249. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU,
  8250. {
  8251. (uint32_t)ggml_nelements(dst),
  8252. (uint32_t)src0->ne[0],
  8253. (uint32_t)dst->ne[0],
  8254. mode,
  8255. alpha,
  8256. limit
  8257. }, dryrun);
  8258. }
  8259. 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) {
  8260. int32_t * op_params = (int32_t *)dst->op_params;
  8261. 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);
  8262. }
  8263. 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) {
  8264. float * op_params = (float *)dst->op_params;
  8265. float scale = op_params[0];
  8266. float max_bias = op_params[1];
  8267. const uint32_t ncols = (uint32_t)src0->ne[0];
  8268. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  8269. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  8270. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  8271. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  8272. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  8273. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  8274. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  8275. const uint32_t n_head_kv = src0->ne[2];
  8276. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  8277. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  8278. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  8279. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_SOFT_MAX, {
  8280. ncols,
  8281. src1 != nullptr ? nrows_y : (uint32_t)0,
  8282. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  8283. ne12, ne13,
  8284. nb11, nb12, nb13,
  8285. scale, max_bias,
  8286. m0, m1,
  8287. n_head_log2,
  8288. nrows_x,
  8289. src2 != nullptr
  8290. }, dryrun);
  8291. }
  8292. 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) {
  8293. float * op_params = (float *)dst->op_params;
  8294. 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)ggml_nrows(src0), op_params[0], op_params[1] }, dryrun);
  8295. }
  8296. static void ggml_vk_topk_moe(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cgraph * cgraph, int node_idx, bool dryrun = false) {
  8297. bool with_norm = ctx->num_additional_fused_ops == topk_moe_norm.size() - 1;
  8298. ggml_tensor * logits = cgraph->nodes[node_idx + 0]->src[0];
  8299. ggml_tensor * weights = with_norm ? cgraph->nodes[node_idx + 8] : cgraph->nodes[node_idx + 4];
  8300. ggml_tensor * ids = cgraph->nodes[node_idx + 3];
  8301. GGML_ASSERT(logits->type == GGML_TYPE_F32);
  8302. GGML_ASSERT(weights->type == GGML_TYPE_F32);
  8303. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  8304. const int n_experts = logits->ne[0];
  8305. const int n_rows = logits->ne[1];
  8306. const int n_expert_used = weights->ne[1];
  8307. GGML_ASSERT(ids->nb[1] / ggml_type_size(ids->type) == (size_t) n_experts);
  8308. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, nullptr, nullptr, nullptr, cgraph->nodes[node_idx], GGML_OP_SOFT_MAX);
  8309. if (dryrun) {
  8310. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8311. return;
  8312. }
  8313. ggml_backend_vk_buffer_context * logits_buf_ctx = (ggml_backend_vk_buffer_context *)logits->buffer->context;
  8314. ggml_backend_vk_buffer_context * weights_buf_ctx = (ggml_backend_vk_buffer_context *)weights->buffer->context;
  8315. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  8316. vk_buffer d_logits = nullptr;
  8317. size_t logits_buf_offset = 0;
  8318. vk_buffer d_weights = nullptr;
  8319. size_t weights_buf_offset = 0;
  8320. vk_buffer d_ids = nullptr;
  8321. size_t ids_buf_offset = 0;
  8322. bool logits_uma = false;
  8323. bool weights_uma = false;
  8324. bool ids_uma = false;
  8325. if (ctx->device->uma) {
  8326. ggml_vk_host_get(ctx->device, logits->data, d_logits, logits_buf_offset);
  8327. ggml_vk_host_get(ctx->device, weights->data, d_weights, weights_buf_offset);
  8328. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  8329. logits_uma = d_logits != nullptr;
  8330. weights_uma = d_weights != nullptr;
  8331. ids_uma = d_ids != nullptr;
  8332. }
  8333. if (!logits_uma) {
  8334. d_logits = logits_buf_ctx->dev_buffer;
  8335. logits_buf_offset = vk_tensor_offset(logits) + logits->view_offs;
  8336. GGML_ASSERT(d_logits != nullptr);
  8337. }
  8338. if (!weights_uma) {
  8339. d_weights = weights_buf_ctx->dev_buffer;
  8340. weights_buf_offset = vk_tensor_offset(weights) + weights->view_offs;
  8341. GGML_ASSERT(d_weights != nullptr);
  8342. }
  8343. if (!ids_uma) {
  8344. d_ids = ids_buf_ctx->dev_buffer;
  8345. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  8346. GGML_ASSERT(d_ids != nullptr);
  8347. }
  8348. vk_op_topk_moe_push_constants pc;
  8349. pc.n_rows = n_rows;
  8350. pc.n_expert_used = n_expert_used;
  8351. GGML_ASSERT(n_expert_used <= n_experts);
  8352. const uint32_t rows_per_block = 4;
  8353. std::array<uint32_t, 3> elements = { CEIL_DIV(n_rows, rows_per_block), 1, 1 };
  8354. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  8355. {
  8356. ggml_vk_subbuffer(ctx, d_logits, logits_buf_offset),
  8357. ggml_vk_subbuffer(ctx, d_weights, weights_buf_offset),
  8358. ggml_vk_subbuffer(ctx, d_ids, ids_buf_offset),
  8359. }, pc, elements);
  8360. }
  8361. 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) {
  8362. const int n_dims = ((int32_t *) dst->op_params)[1];
  8363. const int mode = ((int32_t *) dst->op_params)[2];
  8364. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  8365. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  8366. const float freq_base = ((float *) dst->op_params)[5];
  8367. const float freq_scale = ((float *) dst->op_params)[6];
  8368. const float ext_factor = ((float *) dst->op_params)[7];
  8369. const float attn_factor = ((float *) dst->op_params)[8];
  8370. const float beta_fast = ((float *) dst->op_params)[9];
  8371. const float beta_slow = ((float *) dst->op_params)[10];
  8372. int sections[4] {};
  8373. if (mode & GGML_ROPE_TYPE_MROPE) {
  8374. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  8375. }
  8376. float corr_dims[2];
  8377. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  8378. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  8379. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  8380. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  8381. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  8382. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  8383. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  8384. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  8385. { sections[0], sections[1], sections[2], sections[3] }, backprop
  8386. }, dryrun);
  8387. }
  8388. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8389. int32_t * op_params = (int32_t *)dst->op_params;
  8390. uint32_t ncols = src0->ne[0];
  8391. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  8392. ncols,
  8393. op_params[0],
  8394. }, dryrun);
  8395. }
  8396. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8397. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, ggml_nelements(src0));
  8398. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, p, dryrun);
  8399. }
  8400. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8401. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, src0->ne[0]);
  8402. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, p, dryrun);
  8403. }
  8404. static void ggml_vk_mean(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8405. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, src0->ne[0]);
  8406. p.weight = 1.0f / (float)src0->ne[0];
  8407. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_MEAN, p, dryrun);
  8408. }
  8409. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8410. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], 0.0f, 0.0f }, dryrun);
  8411. }
  8412. 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) {
  8413. 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);
  8414. }
  8415. 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) {
  8416. const int32_t s0 = dst->op_params[0];
  8417. const int32_t s1 = dst->op_params[1];
  8418. const int32_t p0 = dst->op_params[2];
  8419. const int32_t p1 = dst->op_params[3];
  8420. const int32_t d0 = dst->op_params[4];
  8421. const int32_t d1 = dst->op_params[5];
  8422. const bool is_2D = dst->op_params[6] == 1;
  8423. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  8424. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  8425. const uint32_t IW = src1->ne[0];
  8426. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  8427. const uint32_t KW = src0->ne[0];
  8428. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  8429. const uint32_t OW = dst->ne[1];
  8430. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  8431. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  8432. const uint32_t pelements = OW * KW * KH;
  8433. const ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8434. const vk_buffer d_buf = d_buf_ctx->dev_buffer;
  8435. const vk::DeviceAddress dst_addr = d_buf->bda_addr + vk_tensor_offset(dst) + dst->view_offs;
  8436. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  8437. dst_addr,
  8438. batch_offset, offset_delta,
  8439. IC, IW, IH, OW, OH, KW, KH,
  8440. pelements,
  8441. IC * KH * KW,
  8442. s0, s1, p0, p1, d0, d1,
  8443. }, dryrun);
  8444. }
  8445. static void ggml_vk_im2col_3d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  8446. GGML_TENSOR_BINARY_OP_LOCALS
  8447. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8448. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8449. const int32_t s2 = ((const int32_t *)(dst->op_params))[2];
  8450. const int32_t p0 = ((const int32_t *)(dst->op_params))[3];
  8451. const int32_t p1 = ((const int32_t *)(dst->op_params))[4];
  8452. const int32_t p2 = ((const int32_t *)(dst->op_params))[5];
  8453. const int32_t d0 = ((const int32_t *)(dst->op_params))[6];
  8454. const int32_t d1 = ((const int32_t *)(dst->op_params))[7];
  8455. const int32_t d2 = ((const int32_t *)(dst->op_params))[8];
  8456. const int32_t IC = ((const int32_t *)(dst->op_params))[9];
  8457. const int64_t N = ne13 / IC;
  8458. const int64_t ID = ne12;
  8459. const int64_t IH = ne11;
  8460. const int64_t IW = ne10;
  8461. const int64_t KD = ne02;
  8462. const int64_t KH = ne01;
  8463. const int64_t KW = ne00;
  8464. const int64_t OD = ne3 / N;
  8465. const int64_t OH = ne2;
  8466. const int64_t OW = ne1;
  8467. const ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8468. const vk_buffer d_buf = d_buf_ctx->dev_buffer;
  8469. const vk::DeviceAddress dst_addr = d_buf->bda_addr + vk_tensor_offset(dst) + dst->view_offs;
  8470. vk_op_im2col_3d_push_constants pc {};
  8471. pc.dst_addr = dst_addr;
  8472. pc.nb10 = nb10 / ggml_type_size(src1->type);
  8473. pc.nb11 = nb11 / ggml_type_size(src1->type);
  8474. pc.nb12 = nb12 / ggml_type_size(src1->type);
  8475. pc.nb13 = nb13 / ggml_type_size(src1->type);
  8476. pc.s0 = s0;
  8477. pc.s1 = s1;
  8478. pc.s2 = s2;
  8479. pc.p0 = p0;
  8480. pc.p1 = p1;
  8481. pc.p2 = p2;
  8482. pc.d0 = d0;
  8483. pc.d1 = d1;
  8484. pc.d2 = d2;
  8485. pc.IW = IW;
  8486. pc.IH = IH;
  8487. pc.ID = ID;
  8488. pc.IC = IC;
  8489. pc.KW = KW;
  8490. pc.OH = OH;
  8491. pc.KD_KH_KW = KD*KH*KW;
  8492. pc.KH_KW = KH*KW;
  8493. pc.IC_KD_KH_KW = IC*KD*KH*KW;
  8494. pc.N_OD_OH = N*OD*OH;
  8495. pc.OD_OH = OD*OH;
  8496. pc.OD_OH_OW_IC_KD_KH_KW = OD*OH*OW*IC*KD*KH*KW;
  8497. pc.OH_OW_IC_KD_KH_KW = OH*OW*IC*KD*KH*KW;
  8498. pc.OW_IC_KD_KH_KW = OW*IC*KD*KH*KW;
  8499. ggml_vk_op_f32<vk_op_im2col_3d_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL_3D, std::move(pc), dryrun);
  8500. }
  8501. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8502. const uint32_t dim = dst->op_params[0];
  8503. const uint32_t max_period = dst->op_params[1];
  8504. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  8505. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  8506. nb1, dim, max_period,
  8507. }, dryrun);
  8508. }
  8509. 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) {
  8510. // src0: (K, Cout, Cin, 1) -- kernel
  8511. // src1: (L, Cin, 1, 1) -- input
  8512. // dst: (*, Cout, 1, 1)
  8513. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8514. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8515. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8516. GGML_TENSOR_BINARY_OP_LOCALS
  8517. GGML_ASSERT(nb00 == sizeof(float));
  8518. GGML_ASSERT(nb10 == sizeof(float));
  8519. const int32_t s0 = dst->op_params[0];
  8520. vk_op_conv_transpose_1d_push_constants p{};
  8521. p.Cout = static_cast<uint32_t>(ne01);
  8522. p.Cin = static_cast<uint32_t>(ne02);
  8523. p.K = static_cast<uint32_t>(ne00);
  8524. p.L = static_cast<uint32_t>(ne10);
  8525. p.KL = static_cast<uint32_t>(ne0);
  8526. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  8527. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  8528. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  8529. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  8530. p.s0 = static_cast<uint32_t>(s0);
  8531. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  8532. }
  8533. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8534. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  8535. const int32_t k1 = dst->op_params[1];
  8536. const int32_t k0 = dst->op_params[2];
  8537. const int32_t s1 = dst->op_params[3];
  8538. const int32_t s0 = dst->op_params[4];
  8539. const int32_t p1 = dst->op_params[5];
  8540. const int32_t p0 = dst->op_params[6];
  8541. const uint32_t IH = src0->ne[1];
  8542. const uint32_t IW = src0->ne[0];
  8543. const uint32_t N = dst->ne[3];
  8544. const uint32_t OC = dst->ne[2];
  8545. const uint32_t OH = dst->ne[1];
  8546. const uint32_t OW = dst->ne[0];
  8547. const uint32_t parallel_elements = N * OC * OH * OW;
  8548. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  8549. IW, IH, OW, OH, OC,
  8550. parallel_elements,
  8551. op,
  8552. k0, k1, s0, s1, p0, p1,
  8553. }, dryrun);
  8554. }
  8555. static void ggml_vk_conv_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  8556. const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  8557. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  8558. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8559. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  8560. GGML_TENSOR_BINARY_OP_LOCALS
  8561. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  8562. GGML_ASSERT(nb10 == sizeof(float));
  8563. GGML_ASSERT(nb0 == sizeof(float));
  8564. vk_op_conv2d_push_constants p{};
  8565. p.Cout = static_cast<uint32_t>(ne03);
  8566. p.Cin = static_cast<uint32_t>(ne02);
  8567. p.N = static_cast<uint32_t>(ne13);
  8568. p.KW = static_cast<uint32_t>(ne00);
  8569. p.KH = static_cast<uint32_t>(ne01);
  8570. p.W = static_cast<uint32_t>(ne10);
  8571. p.H = static_cast<uint32_t>(ne11);
  8572. p.OW = static_cast<uint32_t>(ne0);
  8573. p.OH = static_cast<uint32_t>(ne1);
  8574. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  8575. p.s1 = static_cast<uint32_t>(dst->op_params[1]);
  8576. p.p0 = static_cast<uint32_t>(dst->op_params[2]);
  8577. p.p1 = static_cast<uint32_t>(dst->op_params[3]);
  8578. p.d0 = static_cast<uint32_t>(dst->op_params[4]);
  8579. p.d1 = static_cast<uint32_t>(dst->op_params[5]);
  8580. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  8581. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  8582. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  8583. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  8584. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  8585. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  8586. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  8587. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  8588. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  8589. GGML_ASSERT(ne03 == ne2);
  8590. GGML_ASSERT(ne02 == ne12);
  8591. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D, std::move(p), dryrun);
  8592. }
  8593. static void ggml_vk_conv_transpose_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  8594. const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  8595. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  8596. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8597. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  8598. GGML_TENSOR_BINARY_OP_LOCALS
  8599. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  8600. GGML_ASSERT(nb10 == sizeof(float));
  8601. GGML_ASSERT(nb0 == sizeof(float));
  8602. vk_op_conv_transpose_2d_push_constants p{};
  8603. p.Cout = static_cast<uint32_t>(ne02);
  8604. p.Cin = static_cast<uint32_t>(ne03);
  8605. p.N = static_cast<uint32_t>(ne13);
  8606. p.KW = static_cast<uint32_t>(ne00);
  8607. p.KH = static_cast<uint32_t>(ne01);
  8608. p.W = static_cast<uint32_t>(ne10);
  8609. p.H = static_cast<uint32_t>(ne11);
  8610. p.OW = static_cast<uint32_t>(ne0);
  8611. p.OH = static_cast<uint32_t>(ne1);
  8612. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  8613. p.s1 = static_cast<uint32_t>(dst->op_params[0]);
  8614. p.p0 = 0;
  8615. p.p1 = 0;
  8616. p.d0 = 1;
  8617. p.d1 = 1;
  8618. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  8619. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  8620. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  8621. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  8622. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  8623. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  8624. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  8625. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  8626. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  8627. GGML_ASSERT(ne02 == ne2);
  8628. GGML_ASSERT(ne03 == ne12);
  8629. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_2D, std::move(p), dryrun);
  8630. }
  8631. 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) {
  8632. vk_op_conv2d_dw_push_constants p{};
  8633. p.ne = ggml_nelements(dst);
  8634. p.channels = dst->ne[2];
  8635. p.batches = dst->ne[3];
  8636. p.dst_w = dst->ne[0];
  8637. p.dst_h = dst->ne[1];
  8638. p.src_w = src1->ne[0];
  8639. p.src_h = src1->ne[1];
  8640. p.knl_w = src0->ne[0];
  8641. p.knl_h = src0->ne[1];
  8642. p.stride_x = dst->op_params[0];
  8643. p.stride_y = dst->op_params[1];
  8644. p.pad_x = dst->op_params[2];
  8645. p.pad_y = dst->op_params[3];
  8646. p.dilation_x = dst->op_params[4];
  8647. p.dilation_y = dst->op_params[5];
  8648. GGML_ASSERT(src0->ne[3] == p.channels);
  8649. GGML_ASSERT(src1->ne[3] == p.batches);
  8650. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  8651. }
  8652. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  8653. const float * op_params = (const float *)dst->op_params;
  8654. 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);
  8655. }
  8656. #ifdef GGML_VULKAN_RUN_TESTS
  8657. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  8658. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  8659. return;
  8660. }
  8661. i0 = std::max(i0, 5);
  8662. i1 = std::max(i1, 5);
  8663. i2 = std::max(i2, 0);
  8664. fprintf(stderr, " ");
  8665. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8666. fprintf(stderr, "%7d ", idx1);
  8667. }
  8668. fprintf(stderr, "\n");
  8669. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  8670. fprintf(stderr, "%7d: ", idx0);
  8671. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8672. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  8673. float val;
  8674. if (type == GGML_TYPE_F32) {
  8675. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  8676. } else if (type == GGML_TYPE_F16) {
  8677. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  8678. } else {
  8679. GGML_ABORT("fatal error");
  8680. }
  8681. fprintf(stderr, "% 7.2f ", val);
  8682. } else {
  8683. fprintf(stderr, " ");
  8684. }
  8685. }
  8686. fprintf(stderr, "\n");
  8687. }
  8688. }
  8689. template <typename X_TYPE, typename Y_TYPE>
  8690. 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) {
  8691. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  8692. const size_t x_ne = m * k * batch;
  8693. const size_t y_ne = k * n * batch;
  8694. const size_t d_ne = m * n * batch;
  8695. vk_pipeline p;
  8696. std::string shname;
  8697. if (shader_size == 0) {
  8698. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8699. p = ctx->device->pipeline_matmul_f32->a_s;
  8700. shname = "F32_ALIGNED_S";
  8701. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8702. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  8703. shname = "F32_F16_ALIGNED_S";
  8704. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8705. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  8706. shname = "F16_F32_ALIGNED_S";
  8707. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8708. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  8709. shname = "F16_ALIGNED_S";
  8710. } else {
  8711. GGML_ABORT("fatal error");
  8712. }
  8713. } else if (shader_size == 1) {
  8714. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8715. p = ctx->device->pipeline_matmul_f32->a_m;
  8716. shname = "F32_ALIGNED_M";
  8717. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8718. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  8719. shname = "F32_F16_ALIGNED_M";
  8720. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8721. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  8722. shname = "F16_F32_ALIGNED_M";
  8723. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8724. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  8725. shname = "F16_ALIGNED_M";
  8726. } else {
  8727. GGML_ABORT("fatal error");
  8728. }
  8729. } else if (shader_size == 2) {
  8730. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8731. p = ctx->device->pipeline_matmul_f32->a_l;
  8732. shname = "F32_ALIGNED_L";
  8733. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8734. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  8735. shname = "F32_F16_ALIGNED_L";
  8736. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8737. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  8738. shname = "F16_F32_ALIGNED_L";
  8739. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8740. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  8741. shname = "F16_ALIGNED_L";
  8742. } else {
  8743. GGML_ABORT("fatal error");
  8744. }
  8745. } else {
  8746. GGML_ASSERT(0);
  8747. }
  8748. const size_t kpad = ggml_vk_align_size(k, p->align);
  8749. if (k != kpad) {
  8750. if (shader_size == 0) {
  8751. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8752. p = ctx->device->pipeline_matmul_f32->s;
  8753. shname = "F32_S";
  8754. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8755. p = ctx->device->pipeline_matmul_f32_f16->s;
  8756. shname = "F32_F16_S";
  8757. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8758. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  8759. shname = "F16_F32_S";
  8760. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8761. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  8762. shname = "F16_S";
  8763. }
  8764. } else if (shader_size == 1) {
  8765. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8766. p = ctx->device->pipeline_matmul_f32->m;
  8767. shname = "F32_M";
  8768. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8769. p = ctx->device->pipeline_matmul_f32_f16->m;
  8770. shname = "F32_F16_M";
  8771. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8772. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  8773. shname = "F16_F32_M";
  8774. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8775. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  8776. shname = "F16_M";
  8777. }
  8778. } else if (shader_size == 2) {
  8779. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8780. p = ctx->device->pipeline_matmul_f32->l;
  8781. shname = "F32_L";
  8782. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8783. p = ctx->device->pipeline_matmul_f32_f16->l;
  8784. shname = "F32_F16_L";
  8785. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  8786. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  8787. shname = "F16_F32_L";
  8788. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8789. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  8790. shname = "F16_L";
  8791. }
  8792. }
  8793. }
  8794. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  8795. if (split_k > 1) {
  8796. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  8797. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  8798. // Resize buffer
  8799. if (ctx->prealloc_split_k != nullptr) {
  8800. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8801. }
  8802. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8803. }
  8804. }
  8805. if (ctx->device->need_compiles) {
  8806. ggml_vk_load_shaders(ctx->device);
  8807. }
  8808. ggml_pipeline_allocate_descriptor_sets(ctx);
  8809. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8810. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8811. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8812. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  8813. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  8814. float* d = (float *) malloc(sizeof(float) * d_ne);
  8815. for (size_t i = 0; i < x_ne; i++) {
  8816. if (std::is_same<float, X_TYPE>()) {
  8817. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8818. // x[i] = 1.0f;
  8819. // x[i] = i + 1;
  8820. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8821. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  8822. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  8823. // x[i] = ggml_fp32_to_fp16(1.0f);
  8824. // x[i] = ggml_fp32_to_fp16(i + 1);
  8825. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  8826. } else {
  8827. GGML_ABORT("fatal error");
  8828. }
  8829. }
  8830. for (size_t i = 0; i < y_ne; i++) {
  8831. if (std::is_same<float, Y_TYPE>()) {
  8832. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  8833. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  8834. // y[i] = i + 1;
  8835. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8836. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  8837. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  8838. // y[i] = ggml_fp32_to_fp16(i + 1);
  8839. } else {
  8840. GGML_ABORT("fatal error");
  8841. }
  8842. }
  8843. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  8844. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  8845. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8846. ggml_vk_ctx_begin(ctx->device, subctx);
  8847. for (size_t i = 0; i < num_it; i++) {
  8848. ggml_vk_matmul(
  8849. ctx, subctx, p, ggml_vk_subbuffer(ctx, d_X), ggml_vk_subbuffer(ctx, d_Y), ggml_vk_subbuffer(ctx, d_D), ggml_vk_subbuffer(ctx, ctx->prealloc_split_k),
  8850. m, n, k,
  8851. k, k, m, k*m, k*n, m*n,
  8852. split_k, batch, batch, batch, 1, 1, n
  8853. );
  8854. }
  8855. ggml_vk_ctx_end(subctx);
  8856. auto begin = std::chrono::high_resolution_clock::now();
  8857. ggml_vk_submit(subctx, ctx->fence);
  8858. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  8859. ctx->device->device.resetFences({ ctx->fence });
  8860. ggml_vk_queue_command_pools_cleanup(ctx->device);
  8861. auto end = std::chrono::high_resolution_clock::now();
  8862. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  8863. // copy dst to host
  8864. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  8865. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  8866. ggml_init_params iparams = {
  8867. /*.mem_size =*/ 1024*1024*1024,
  8868. /*.mem_buffer =*/ NULL,
  8869. /*.no_alloc =*/ true,
  8870. };
  8871. ggml_context * ggml_ctx = ggml_init(iparams);
  8872. ggml_type src0_type;
  8873. ggml_type src1_type;
  8874. if (std::is_same<float, X_TYPE>()) {
  8875. src0_type = GGML_TYPE_F32;
  8876. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  8877. src0_type = GGML_TYPE_F16;
  8878. } else {
  8879. GGML_ABORT("fatal error");
  8880. }
  8881. if (std::is_same<float, Y_TYPE>()) {
  8882. src1_type = GGML_TYPE_F32;
  8883. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  8884. src1_type = GGML_TYPE_F16;
  8885. } else {
  8886. GGML_ABORT("fatal error");
  8887. }
  8888. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  8889. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  8890. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  8891. src0_ggml->data = x;
  8892. src1_ggml->data = y;
  8893. tensor_ggml->data = d_chk;
  8894. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8895. ggml_build_forward_expand(cgraph, tensor_ggml);
  8896. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  8897. ggml_free(ggml_ctx);
  8898. double avg_err = 0.0;
  8899. int first_err_n = -1;
  8900. int first_err_m = -1;
  8901. int first_err_b = -1;
  8902. for (size_t i = 0; i < m*n*batch; i++) {
  8903. double err = std::fabs(d[i] - d_chk[i]);
  8904. avg_err += err;
  8905. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  8906. first_err_b = i / (m * n);
  8907. first_err_n = (i % (m * n)) / m;
  8908. first_err_m = (i % (m * n)) % m;
  8909. }
  8910. }
  8911. avg_err /= m * n;
  8912. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  8913. 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;
  8914. if (avg_err > 0.1 || std::isnan(avg_err)) {
  8915. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  8916. std::cerr << "Actual result: " << std::endl << std::endl;
  8917. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8918. std::cerr << "Expected result: " << std::endl << std::endl;
  8919. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8920. if (split_k > 1) {
  8921. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  8922. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  8923. std::cerr << "d_buf0: " << std::endl << std::endl;
  8924. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8925. std::cerr << "d_buf1: " << std::endl << std::endl;
  8926. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  8927. std::cerr << "d_buf2: " << std::endl << std::endl;
  8928. 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);
  8929. std::cerr << "d_buf3: " << std::endl << std::endl;
  8930. 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);
  8931. free(split_k_buf);
  8932. }
  8933. }
  8934. free(d_chk);
  8935. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8936. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8937. ggml_vk_destroy_buffer(d_X);
  8938. ggml_vk_destroy_buffer(d_Y);
  8939. ggml_vk_destroy_buffer(d_D);
  8940. free(x);
  8941. free(y);
  8942. free(d);
  8943. }
  8944. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  8945. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  8946. return;
  8947. }
  8948. i0 = std::max(i0, 5);
  8949. i1 = std::max(i1, 5);
  8950. i2 = std::max(i2, 0);
  8951. i3 = std::max(i3, 0);
  8952. fprintf(stderr, " ");
  8953. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8954. fprintf(stderr, "%7d ", idx1);
  8955. }
  8956. fprintf(stderr, "\n");
  8957. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  8958. fprintf(stderr, "%7d: ", idx0);
  8959. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8960. 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]) {
  8961. float val;
  8962. if (tensor->type == GGML_TYPE_F32) {
  8963. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  8964. } else if (tensor->type == GGML_TYPE_F16) {
  8965. 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]));
  8966. } else {
  8967. GGML_ABORT("fatal error");
  8968. }
  8969. fprintf(stderr, "% 7.2f ", val);
  8970. } else {
  8971. fprintf(stderr, " ");
  8972. }
  8973. }
  8974. fprintf(stderr, "\n");
  8975. }
  8976. }
  8977. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  8978. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  8979. }
  8980. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  8981. if (quant == GGML_TYPE_F32) {
  8982. memcpy(to, from, sizeof(float) * ne);
  8983. return;
  8984. }
  8985. const auto * tt = ggml_get_type_traits(quant);
  8986. ggml_to_float_t dequant_fn = tt->to_float;
  8987. dequant_fn(from, to, ne);
  8988. }
  8989. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  8990. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  8991. const size_t x_sz = sizeof(float) * ne;
  8992. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  8993. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  8994. float * x = (float *) malloc(x_sz);
  8995. void * qx = malloc(qx_sz);
  8996. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8997. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  8998. float * x_ref = (float *) malloc(x_sz);
  8999. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  9000. for (size_t i = 0; i < ne; i++) {
  9001. x[i] = rand() / (float)RAND_MAX;
  9002. }
  9003. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  9004. ggml_vk_quantize_data(x, qx, ne, quant);
  9005. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  9006. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  9007. if (ctx->device->need_compiles) {
  9008. ggml_vk_load_shaders(ctx->device);
  9009. }
  9010. ggml_pipeline_allocate_descriptor_sets(ctx);
  9011. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  9012. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9013. ggml_vk_ctx_begin(ctx->device, subctx);
  9014. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  9015. 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});
  9016. ggml_vk_ctx_end(subctx);
  9017. auto begin = std::chrono::high_resolution_clock::now();
  9018. ggml_vk_submit(subctx, ctx->fence);
  9019. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  9020. ctx->device->device.resetFences({ ctx->fence });
  9021. ggml_vk_queue_command_pools_cleanup(ctx->device);
  9022. auto end = std::chrono::high_resolution_clock::now();
  9023. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9024. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  9025. int first_err = -1;
  9026. double avg_err = 0.0;
  9027. for (size_t i = 0; i < ne; i++) {
  9028. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  9029. avg_err += error;
  9030. if (first_err < 0 && error > 0.05) {
  9031. first_err = i;
  9032. }
  9033. }
  9034. avg_err /= ne;
  9035. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  9036. if (avg_err > 0.1) {
  9037. std::cerr << "first_error = " << first_err << std::endl;
  9038. std::cerr << "Actual result: " << std::endl << std::endl;
  9039. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  9040. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  9041. }
  9042. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  9043. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  9044. std::cerr << x_ref[i] << ", ";
  9045. }
  9046. std::cerr << std::endl;
  9047. }
  9048. ggml_vk_destroy_buffer(x_buf);
  9049. ggml_vk_destroy_buffer(qx_buf);
  9050. free(x);
  9051. free(qx);
  9052. free(x_ref);
  9053. free(x_chk);
  9054. }
  9055. // This does not work without ggml q8_1 quantization support
  9056. //
  9057. // typedef uint16_t ggml_half;
  9058. // typedef uint32_t ggml_half2;
  9059. //
  9060. // #define QK8_1 32
  9061. // typedef struct {
  9062. // union {
  9063. // struct {
  9064. // ggml_half d; // delta
  9065. // ggml_half s; // d * sum(qs[i])
  9066. // } GGML_COMMON_AGGR_S;
  9067. // ggml_half2 ds;
  9068. // } GGML_COMMON_AGGR_U;
  9069. // int8_t qs[QK8_1]; // quants
  9070. // } block_q8_1;
  9071. //
  9072. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  9073. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  9074. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  9075. //
  9076. // const size_t x_sz = sizeof(float) * ne;
  9077. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  9078. // float * x = (float *) malloc(x_sz);
  9079. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  9080. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  9081. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9082. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9083. //
  9084. // for (size_t i = 0; i < ne; i++) {
  9085. // x[i] = rand() / (float)RAND_MAX;
  9086. // }
  9087. //
  9088. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  9089. //
  9090. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  9091. //
  9092. // if (ctx->device->need_compiles) {
  9093. // ggml_vk_load_shaders(ctx->device);
  9094. // }
  9095. //
  9096. // ggml_pipeline_allocate_descriptor_sets(ctx);
  9097. //
  9098. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  9099. //
  9100. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9101. // ggml_vk_ctx_begin(ctx->device, subctx);
  9102. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, x_buf), ggml_vk_subbuffer(ctx, qx_buf), ne);
  9103. // ggml_vk_ctx_end(subctx);
  9104. //
  9105. // auto begin = std::chrono::high_resolution_clock::now();
  9106. //
  9107. // ggml_vk_submit(subctx, ctx->fence);
  9108. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  9109. // ctx->device->device.resetFences({ ctx->fence });
  9110. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  9111. //
  9112. // auto end = std::chrono::high_resolution_clock::now();
  9113. //
  9114. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9115. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  9116. //
  9117. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  9118. //
  9119. // int first_err = -1;
  9120. //
  9121. // for (size_t i = 0; i < ne / 32; i++) {
  9122. // 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));
  9123. //
  9124. // if (first_err < 0 && error > 0.1) {
  9125. // first_err = i;
  9126. // }
  9127. //
  9128. // 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));
  9129. //
  9130. // if (first_err < 0 && error > 0.1) {
  9131. // first_err = i;
  9132. // }
  9133. //
  9134. // for (size_t j = 0; j < 32; j++) {
  9135. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  9136. //
  9137. // if (first_err < 0 && error > 1) {
  9138. // first_err = i;
  9139. // }
  9140. // }
  9141. // }
  9142. //
  9143. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  9144. //
  9145. // if (first_err != -1) {
  9146. // std::cerr << "first_error = " << first_err << std::endl;
  9147. // std::cerr << "Actual result: " << std::endl << std::endl;
  9148. // 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) << " ";
  9149. // for (size_t j = 0; j < 32; j++) {
  9150. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  9151. // }
  9152. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  9153. // 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) << " ";
  9154. // for (size_t j = 0; j < 32; j++) {
  9155. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  9156. // }
  9157. // std::cerr << std::endl;
  9158. // }
  9159. //
  9160. // ggml_vk_destroy_buffer(x_buf);
  9161. // ggml_vk_destroy_buffer(qx_buf);
  9162. //
  9163. // free(x);
  9164. // free(qx);
  9165. // free(qx_res);
  9166. // }
  9167. 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) {
  9168. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  9169. const size_t x_ne = m * k * batch;
  9170. const size_t y_ne = k * n * batch;
  9171. const size_t d_ne = m * n * batch;
  9172. vk_matmul_pipeline2 * pipelines;
  9173. if (mmq) {
  9174. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  9175. } else {
  9176. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  9177. }
  9178. const bool fp16acc = ctx->device->fp16;
  9179. vk_pipeline p;
  9180. std::string shname;
  9181. if (shader_size == 0) {
  9182. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  9183. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  9184. } else if (shader_size == 1) {
  9185. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  9186. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  9187. } else if (shader_size == 2) {
  9188. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  9189. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  9190. } else {
  9191. GGML_ASSERT(0);
  9192. }
  9193. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  9194. if (mmq || k != kpad) {
  9195. if (shader_size == 0) {
  9196. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  9197. shname = std::string(ggml_type_name(quant)) + "_S";
  9198. } else if (shader_size == 1) {
  9199. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  9200. shname = std::string(ggml_type_name(quant)) + "_M";
  9201. } else if (shader_size == 2) {
  9202. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  9203. shname = std::string(ggml_type_name(quant)) + "_L";
  9204. } else {
  9205. GGML_ASSERT(0);
  9206. }
  9207. }
  9208. if (p == nullptr) {
  9209. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  9210. return;
  9211. }
  9212. const size_t x_sz = sizeof(float) * x_ne;
  9213. const size_t y_sz = sizeof(float) * y_ne;
  9214. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  9215. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  9216. const size_t d_sz = sizeof(float) * d_ne;
  9217. float * x = (float *) malloc(x_sz);
  9218. float * y = (float *) malloc(y_sz);
  9219. void * qx = malloc(qx_sz);
  9220. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9221. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9222. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9223. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9224. float * d = (float *) malloc(d_sz);
  9225. float * d_chk = (float *) malloc(d_sz);
  9226. for (size_t i = 0; i < x_ne; i++) {
  9227. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  9228. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  9229. // x[i] = i % k;
  9230. }
  9231. ggml_vk_quantize_data(x, qx, x_ne, quant);
  9232. for (size_t i = 0; i < y_ne; i++) {
  9233. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  9234. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  9235. // y[i] = i % k;
  9236. }
  9237. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  9238. if (split_k > 1) {
  9239. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  9240. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  9241. // Resize buffer
  9242. if (ctx->prealloc_split_k != nullptr) {
  9243. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  9244. }
  9245. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9246. }
  9247. }
  9248. if (mmq) {
  9249. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  9250. }
  9251. if (ctx->device->need_compiles) {
  9252. ggml_vk_load_shaders(ctx->device);
  9253. }
  9254. ggml_pipeline_allocate_descriptor_sets(ctx);
  9255. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  9256. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  9257. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9258. ggml_vk_ctx_begin(ctx->device, subctx);
  9259. if (mmq) {
  9260. for (size_t i = 0; i < num_it; i++) {
  9261. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  9262. ggml_vk_matmul(
  9263. 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 },
  9264. m, n, k,
  9265. k, k, m, k*m, k*n, m*n,
  9266. split_k, batch, batch, batch, 1, 1, n
  9267. );
  9268. }
  9269. } else {
  9270. for (size_t i = 0; i < num_it; i++) {
  9271. ggml_vk_matmul(
  9272. 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 },
  9273. m, n, k,
  9274. k, k, m, k*m, k*n, m*n,
  9275. split_k, batch, batch, batch, 1, 1, n
  9276. );
  9277. }
  9278. }
  9279. ggml_vk_ctx_end(subctx);
  9280. auto begin = std::chrono::high_resolution_clock::now();
  9281. ggml_vk_submit(subctx, ctx->fence);
  9282. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  9283. ctx->device->device.resetFences({ ctx->fence });
  9284. ggml_vk_queue_command_pools_cleanup(ctx->device);
  9285. auto end = std::chrono::high_resolution_clock::now();
  9286. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9287. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  9288. ggml_init_params iparams = {
  9289. /*.mem_size =*/ 1024*1024*1024,
  9290. /*.mem_buffer =*/ NULL,
  9291. /*.no_alloc =*/ true,
  9292. };
  9293. ggml_context * ggml_ctx = ggml_init(iparams);
  9294. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  9295. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  9296. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  9297. src0_ggml->data = qx;
  9298. src1_ggml->data = y;
  9299. tensor_ggml->data = d_chk;
  9300. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  9301. ggml_build_forward_expand(cgraph, tensor_ggml);
  9302. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  9303. ggml_free(ggml_ctx);
  9304. double avg_err = 0.0;
  9305. int first_err_n = -1;
  9306. int first_err_m = -1;
  9307. int first_err_b = -1;
  9308. for (size_t i = 0; i < m*n*batch; i++) {
  9309. double err = std::fabs(d[i] - d_chk[i]);
  9310. avg_err += err;
  9311. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  9312. first_err_b = i / (m * n);
  9313. first_err_n = (i % (m * n)) / m;
  9314. first_err_m = (i % (m * n)) % m;
  9315. }
  9316. }
  9317. avg_err /= m * n;
  9318. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  9319. std::cerr << "TEST dequant matmul " << shname;
  9320. if (mmq) {
  9321. std::cerr << " mmq";
  9322. }
  9323. 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;
  9324. if (avg_err > 0.01 || std::isnan(avg_err)) {
  9325. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  9326. std::cerr << "Actual result: " << std::endl << std::endl;
  9327. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9328. std::cerr << std::endl;
  9329. std::cerr << "Expected result: " << std::endl << std::endl;
  9330. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9331. std::cerr << "src0: " << std::endl << std::endl;
  9332. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  9333. std::cerr << std::endl;
  9334. std::cerr << "src1: " << std::endl << std::endl;
  9335. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  9336. if (split_k > 1) {
  9337. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  9338. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  9339. std::cerr << "d_buf0: " << std::endl << std::endl;
  9340. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9341. std::cerr << "d_buf1: " << std::endl << std::endl;
  9342. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9343. std::cerr << "d_buf2: " << std::endl << std::endl;
  9344. 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);
  9345. std::cerr << "d_buf3: " << std::endl << std::endl;
  9346. 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);
  9347. free(split_k_buf);
  9348. }
  9349. }
  9350. ggml_vk_destroy_buffer(qx_buf);
  9351. ggml_vk_destroy_buffer(y_buf);
  9352. ggml_vk_destroy_buffer(qy_buf);
  9353. ggml_vk_destroy_buffer(d_buf);
  9354. free(x);
  9355. free(qx);
  9356. free(y);
  9357. free(d);
  9358. free(d_chk);
  9359. }
  9360. #endif
  9361. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  9362. #if defined(GGML_VULKAN_RUN_TESTS)
  9363. const std::vector<size_t> vals {
  9364. 512, 512, 128,
  9365. 128, 512, 512,
  9366. 4096, 512, 4096,
  9367. 11008, 512, 4096,
  9368. 4096, 512, 11008,
  9369. 32000, 512, 4096,
  9370. 8, 8, 8,
  9371. 100, 46, 576,
  9372. 623, 111, 128,
  9373. 100, 46, 558,
  9374. 512, 1, 256,
  9375. 128, 110, 622,
  9376. 511, 511, 127,
  9377. 511, 511, 7,
  9378. 511, 511, 17,
  9379. 49, 49, 128,
  9380. 128, 49, 49,
  9381. 4096, 49, 4096,
  9382. };
  9383. const size_t num_it = 100;
  9384. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  9385. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  9386. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  9387. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  9388. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  9389. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  9390. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  9391. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  9392. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  9393. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  9394. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  9395. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  9396. abort();
  9397. for (size_t i = 0; i < vals.size(); i += 3) {
  9398. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  9399. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  9400. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  9401. std::cerr << '\n';
  9402. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  9403. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  9404. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  9405. std::cerr << '\n';
  9406. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  9407. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  9408. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  9409. std::cerr << '\n' << std::endl;
  9410. if (vals[i + 2] % 32 == 0) {
  9411. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  9412. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  9413. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  9414. std::cerr << '\n';
  9415. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  9416. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  9417. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  9418. std::cerr << '\n';
  9419. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  9420. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  9421. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  9422. std::cerr << '\n' << std::endl;
  9423. }
  9424. if (vals[i + 2] % 256 == 0) {
  9425. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  9426. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  9427. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  9428. std::cerr << '\n';
  9429. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  9430. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  9431. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  9432. std::cerr << '\n';
  9433. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  9434. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  9435. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  9436. std::cerr << '\n' << std::endl;
  9437. }
  9438. }
  9439. GGML_ABORT("fatal error");
  9440. #endif
  9441. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  9442. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  9443. // Resize buffer
  9444. if (ctx->prealloc_x != nullptr) {
  9445. ggml_vk_destroy_buffer(ctx->prealloc_x);
  9446. }
  9447. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  9448. }
  9449. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  9450. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  9451. // Resize buffer
  9452. if (ctx->prealloc_y != nullptr) {
  9453. ggml_vk_destroy_buffer(ctx->prealloc_y);
  9454. }
  9455. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  9456. }
  9457. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  9458. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  9459. // Resize buffer
  9460. if (ctx->prealloc_split_k != nullptr) {
  9461. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  9462. }
  9463. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  9464. }
  9465. if (ctx->prealloc_add_rms_partials == nullptr || (ctx->prealloc_size_add_rms_partials > 0 && ctx->prealloc_add_rms_partials->size < ctx->prealloc_size_add_rms_partials)) {
  9466. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(add_partials_size: " << ctx->prealloc_add_rms_partials << ")");
  9467. // Resize buffer
  9468. if (ctx->prealloc_add_rms_partials != nullptr) {
  9469. ggml_vk_destroy_buffer(ctx->prealloc_add_rms_partials);
  9470. }
  9471. ctx->prealloc_add_rms_partials = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_add_rms_partials);
  9472. }
  9473. }
  9474. 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);
  9475. // Returns true if node has enqueued work into the queue, false otherwise
  9476. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  9477. 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){
  9478. ggml_tensor * node = cgraph->nodes[node_idx];
  9479. if (ggml_is_empty(node) || !node->buffer) {
  9480. return false;
  9481. }
  9482. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  9483. ctx->semaphore_idx = 0;
  9484. ggml_tensor * src0 = node->src[0];
  9485. ggml_tensor * src1 = node->src[1];
  9486. ggml_tensor * src2 = node->src[2];
  9487. ggml_tensor * src3 = node->src[3];
  9488. switch (node->op) {
  9489. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  9490. case GGML_OP_RESHAPE:
  9491. case GGML_OP_VIEW:
  9492. case GGML_OP_PERMUTE:
  9493. case GGML_OP_TRANSPOSE:
  9494. case GGML_OP_NONE:
  9495. return false;
  9496. case GGML_OP_UNARY:
  9497. switch (ggml_get_unary_op(node)) {
  9498. case GGML_UNARY_OP_EXP:
  9499. case GGML_UNARY_OP_SILU:
  9500. case GGML_UNARY_OP_GELU:
  9501. case GGML_UNARY_OP_GELU_ERF:
  9502. case GGML_UNARY_OP_GELU_QUICK:
  9503. case GGML_UNARY_OP_RELU:
  9504. case GGML_UNARY_OP_TANH:
  9505. case GGML_UNARY_OP_SIGMOID:
  9506. case GGML_UNARY_OP_HARDSIGMOID:
  9507. case GGML_UNARY_OP_HARDSWISH:
  9508. break;
  9509. default:
  9510. return false;
  9511. }
  9512. break;
  9513. case GGML_OP_GLU:
  9514. switch (ggml_get_glu_op(node)) {
  9515. case GGML_GLU_OP_GEGLU:
  9516. case GGML_GLU_OP_REGLU:
  9517. case GGML_GLU_OP_SWIGLU:
  9518. case GGML_GLU_OP_SWIGLU_OAI:
  9519. case GGML_GLU_OP_GEGLU_ERF:
  9520. case GGML_GLU_OP_GEGLU_QUICK:
  9521. break;
  9522. default:
  9523. return false;
  9524. }
  9525. break;
  9526. case GGML_OP_ADD:
  9527. {
  9528. int next_node_idx = node_idx + 1 + ctx->num_additional_fused_ops;
  9529. if (next_node_idx < cgraph->n_nodes &&
  9530. cgraph->nodes[next_node_idx]->op == GGML_OP_RMS_NORM &&
  9531. cgraph->nodes[next_node_idx]->src[0] == cgraph->nodes[next_node_idx - 1] &&
  9532. ggml_nrows(cgraph->nodes[next_node_idx]) == 1 &&
  9533. ctx->device->add_rms_fusion) {
  9534. if (dryrun) {
  9535. ctx->prealloc_size_add_rms_partials += ggml_vk_rms_partials_size(ctx, cgraph->nodes[node_idx]);
  9536. }
  9537. ctx->do_add_rms_partials = true;
  9538. }
  9539. } break;
  9540. case GGML_OP_REPEAT:
  9541. case GGML_OP_REPEAT_BACK:
  9542. case GGML_OP_GET_ROWS:
  9543. case GGML_OP_ADD_ID:
  9544. case GGML_OP_ACC:
  9545. case GGML_OP_SUB:
  9546. case GGML_OP_MUL:
  9547. case GGML_OP_DIV:
  9548. case GGML_OP_CONCAT:
  9549. case GGML_OP_UPSCALE:
  9550. case GGML_OP_SCALE:
  9551. case GGML_OP_SQR:
  9552. case GGML_OP_SQRT:
  9553. case GGML_OP_SIN:
  9554. case GGML_OP_COS:
  9555. case GGML_OP_CLAMP:
  9556. case GGML_OP_PAD:
  9557. case GGML_OP_ROLL:
  9558. case GGML_OP_CPY:
  9559. case GGML_OP_SET_ROWS:
  9560. case GGML_OP_CONT:
  9561. case GGML_OP_DUP:
  9562. case GGML_OP_SILU_BACK:
  9563. case GGML_OP_NORM:
  9564. case GGML_OP_GROUP_NORM:
  9565. case GGML_OP_RMS_NORM:
  9566. case GGML_OP_RMS_NORM_BACK:
  9567. case GGML_OP_L2_NORM:
  9568. case GGML_OP_DIAG_MASK_INF:
  9569. case GGML_OP_SOFT_MAX:
  9570. case GGML_OP_SOFT_MAX_BACK:
  9571. case GGML_OP_ROPE:
  9572. case GGML_OP_ROPE_BACK:
  9573. case GGML_OP_MUL_MAT:
  9574. case GGML_OP_MUL_MAT_ID:
  9575. case GGML_OP_ARGSORT:
  9576. case GGML_OP_SUM:
  9577. case GGML_OP_SUM_ROWS:
  9578. case GGML_OP_MEAN:
  9579. case GGML_OP_ARGMAX:
  9580. case GGML_OP_COUNT_EQUAL:
  9581. case GGML_OP_IM2COL:
  9582. case GGML_OP_IM2COL_3D:
  9583. case GGML_OP_TIMESTEP_EMBEDDING:
  9584. case GGML_OP_CONV_TRANSPOSE_1D:
  9585. case GGML_OP_POOL_2D:
  9586. case GGML_OP_CONV_2D:
  9587. case GGML_OP_CONV_TRANSPOSE_2D:
  9588. case GGML_OP_CONV_2D_DW:
  9589. case GGML_OP_RWKV_WKV6:
  9590. case GGML_OP_RWKV_WKV7:
  9591. case GGML_OP_SSM_SCAN:
  9592. case GGML_OP_SSM_CONV:
  9593. case GGML_OP_LEAKY_RELU:
  9594. case GGML_OP_FLASH_ATTN_EXT:
  9595. case GGML_OP_OPT_STEP_ADAMW:
  9596. case GGML_OP_OPT_STEP_SGD:
  9597. break;
  9598. default:
  9599. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  9600. GGML_ABORT("fatal error");
  9601. }
  9602. vk_context compute_ctx;
  9603. if (!dryrun) {
  9604. if (ctx->compute_ctx.expired()) {
  9605. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9606. ctx->compute_ctx = compute_ctx;
  9607. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9608. } else {
  9609. compute_ctx = ctx->compute_ctx.lock();
  9610. }
  9611. } else {
  9612. switch (node->op) {
  9613. case GGML_OP_REPEAT:
  9614. case GGML_OP_REPEAT_BACK:
  9615. case GGML_OP_ACC:
  9616. case GGML_OP_GET_ROWS:
  9617. case GGML_OP_ADD:
  9618. case GGML_OP_SUB:
  9619. case GGML_OP_MUL:
  9620. case GGML_OP_DIV:
  9621. case GGML_OP_CONCAT:
  9622. case GGML_OP_UPSCALE:
  9623. case GGML_OP_SCALE:
  9624. case GGML_OP_SQR:
  9625. case GGML_OP_SQRT:
  9626. case GGML_OP_SIN:
  9627. case GGML_OP_COS:
  9628. case GGML_OP_CLAMP:
  9629. case GGML_OP_PAD:
  9630. case GGML_OP_CPY:
  9631. case GGML_OP_SET_ROWS:
  9632. case GGML_OP_CONT:
  9633. case GGML_OP_DUP:
  9634. case GGML_OP_SILU_BACK:
  9635. case GGML_OP_NORM:
  9636. case GGML_OP_GROUP_NORM:
  9637. case GGML_OP_RMS_NORM:
  9638. case GGML_OP_RMS_NORM_BACK:
  9639. case GGML_OP_L2_NORM:
  9640. case GGML_OP_UNARY:
  9641. case GGML_OP_GLU:
  9642. case GGML_OP_DIAG_MASK_INF:
  9643. case GGML_OP_SOFT_MAX:
  9644. case GGML_OP_SOFT_MAX_BACK:
  9645. case GGML_OP_ROPE:
  9646. case GGML_OP_ROPE_BACK:
  9647. case GGML_OP_ARGSORT:
  9648. case GGML_OP_SUM:
  9649. case GGML_OP_SUM_ROWS:
  9650. case GGML_OP_MEAN:
  9651. case GGML_OP_ARGMAX:
  9652. case GGML_OP_COUNT_EQUAL:
  9653. case GGML_OP_IM2COL:
  9654. case GGML_OP_IM2COL_3D:
  9655. case GGML_OP_TIMESTEP_EMBEDDING:
  9656. case GGML_OP_CONV_TRANSPOSE_1D:
  9657. case GGML_OP_POOL_2D:
  9658. case GGML_OP_CONV_2D:
  9659. case GGML_OP_CONV_TRANSPOSE_2D:
  9660. case GGML_OP_CONV_2D_DW:
  9661. case GGML_OP_LEAKY_RELU:
  9662. case GGML_OP_OPT_STEP_SGD:
  9663. {
  9664. // These operations all go through ggml_vk_op_f32, so short-circuit and
  9665. // do the only thing needed for the dryrun.
  9666. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  9667. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  9668. if (node->op == GGML_OP_RMS_NORM) {
  9669. ctx->do_add_rms_partials = false;
  9670. }
  9671. return false;
  9672. }
  9673. default:
  9674. break;
  9675. }
  9676. }
  9677. if (!dryrun) {
  9678. // This logic detects dependencies between modes in the graph and calls ggml_vk_sync_buffers
  9679. // to synchronize them. This handles most "normal" synchronization when computing the graph, and when
  9680. // there is no auxiliary memory use, it shouldn't be necessary to call ggml_vk_sync_buffers
  9681. // outside of this logic. When a node uses one of the prealloc buffers for something like
  9682. // dequantization or split_k, additional synchronization is needed between those passes.
  9683. bool need_sync = false;
  9684. // Check whether "node" requires synchronization. The node requires synchronization if it
  9685. // overlaps in memory with another unsynchronized node and at least one of them is a write.
  9686. // Destination nodes are checked against both the written/read lists. Source nodes are only
  9687. // checked against the written list. Two nodes overlap in memory if they come from the same
  9688. // buffer and the tensor or view ranges overlap.
  9689. auto const &overlaps_unsynced = [&](const ggml_tensor *node, const std::vector<const ggml_tensor *> &unsynced_nodes) -> bool {
  9690. if (unsynced_nodes.size() == 0) {
  9691. return false;
  9692. }
  9693. auto n_base = vk_tensor_offset(node) + node->view_offs;
  9694. auto n_size = ggml_nbytes(node);
  9695. ggml_backend_vk_buffer_context * a_buf_ctx = (ggml_backend_vk_buffer_context *)node->buffer->context;
  9696. vk_buffer a_buf = a_buf_ctx->dev_buffer;
  9697. for (auto &other : unsynced_nodes) {
  9698. ggml_backend_vk_buffer_context * o_buf_ctx = (ggml_backend_vk_buffer_context *)other->buffer->context;
  9699. vk_buffer o_buf = o_buf_ctx->dev_buffer;
  9700. if (a_buf == o_buf) {
  9701. auto o_base = vk_tensor_offset(other) + other->view_offs;
  9702. auto o_size = ggml_nbytes(other);
  9703. if ((o_base <= n_base && n_base < o_base + o_size) ||
  9704. (n_base <= o_base && o_base < n_base + n_size)) {
  9705. return true;
  9706. }
  9707. }
  9708. }
  9709. return false;
  9710. };
  9711. // For all fused ops, check if the destination node or any of the source
  9712. // nodes require synchronization.
  9713. for (int32_t i = 0; i < ctx->num_additional_fused_ops + 1 && !need_sync; ++i) {
  9714. const ggml_tensor *cur_node = cgraph->nodes[node_idx + i];
  9715. if (overlaps_unsynced(cur_node, ctx->unsynced_nodes_read) || overlaps_unsynced(cur_node, ctx->unsynced_nodes_written)) {
  9716. need_sync = true;
  9717. break;
  9718. }
  9719. for (uint32_t j = 0; j < GGML_MAX_SRC; ++j) {
  9720. if (!cur_node->src[j]) {
  9721. continue;
  9722. }
  9723. if (overlaps_unsynced(cur_node->src[j], ctx->unsynced_nodes_written)) {
  9724. need_sync = true;
  9725. break;
  9726. }
  9727. }
  9728. }
  9729. if (need_sync) {
  9730. ctx->unsynced_nodes_written.clear();
  9731. ctx->unsynced_nodes_read.clear();
  9732. ggml_vk_sync_buffers(ctx, compute_ctx);
  9733. }
  9734. // Add all fused nodes to the unsynchronized lists.
  9735. for (int32_t i = 0; i < ctx->num_additional_fused_ops + 1; ++i) {
  9736. const ggml_tensor *cur_node = cgraph->nodes[node_idx + i];
  9737. // Multiple outputs could be written, e.g. in topk_moe. Add them all to the list.
  9738. ctx->unsynced_nodes_written.push_back(cur_node);
  9739. for (uint32_t j = 0; j < GGML_MAX_SRC; ++j) {
  9740. if (!cur_node->src[j]) {
  9741. continue;
  9742. }
  9743. ctx->unsynced_nodes_read.push_back(cur_node->src[j]);
  9744. }
  9745. }
  9746. }
  9747. switch (node->op) {
  9748. case GGML_OP_REPEAT:
  9749. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  9750. break;
  9751. case GGML_OP_REPEAT_BACK:
  9752. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  9753. break;
  9754. case GGML_OP_ACC:
  9755. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  9756. break;
  9757. case GGML_OP_GET_ROWS:
  9758. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  9759. break;
  9760. case GGML_OP_ADD:
  9761. if (ctx->num_additional_fused_ops) {
  9762. ggml_vk_multi_add(ctx, compute_ctx, cgraph, node_idx, dryrun);
  9763. } else {
  9764. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  9765. }
  9766. break;
  9767. case GGML_OP_SUB:
  9768. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  9769. break;
  9770. case GGML_OP_MUL:
  9771. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  9772. break;
  9773. case GGML_OP_DIV:
  9774. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  9775. break;
  9776. case GGML_OP_ADD_ID:
  9777. ggml_vk_add_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9778. break;
  9779. case GGML_OP_CONCAT:
  9780. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  9781. break;
  9782. case GGML_OP_UPSCALE:
  9783. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  9784. break;
  9785. case GGML_OP_SCALE:
  9786. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  9787. break;
  9788. case GGML_OP_SQR:
  9789. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  9790. break;
  9791. case GGML_OP_SQRT:
  9792. ggml_vk_sqrt(ctx, compute_ctx, src0, node, dryrun);
  9793. break;
  9794. case GGML_OP_SIN:
  9795. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  9796. break;
  9797. case GGML_OP_COS:
  9798. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  9799. break;
  9800. case GGML_OP_CLAMP:
  9801. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  9802. break;
  9803. case GGML_OP_PAD:
  9804. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  9805. break;
  9806. case GGML_OP_ROLL:
  9807. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  9808. break;
  9809. case GGML_OP_CPY:
  9810. case GGML_OP_CONT:
  9811. case GGML_OP_DUP:
  9812. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  9813. break;
  9814. case GGML_OP_SET_ROWS:
  9815. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  9816. break;
  9817. case GGML_OP_SILU_BACK:
  9818. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  9819. break;
  9820. case GGML_OP_NORM:
  9821. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  9822. break;
  9823. case GGML_OP_GROUP_NORM:
  9824. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  9825. break;
  9826. case GGML_OP_RMS_NORM:
  9827. if (ctx->num_additional_fused_ops > 0) {
  9828. // fused rms_norm + mul
  9829. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  9830. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  9831. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  9832. } else {
  9833. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  9834. }
  9835. break;
  9836. case GGML_OP_RMS_NORM_BACK:
  9837. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  9838. break;
  9839. case GGML_OP_L2_NORM:
  9840. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  9841. break;
  9842. case GGML_OP_UNARY:
  9843. switch (ggml_get_unary_op(node)) {
  9844. case GGML_UNARY_OP_EXP:
  9845. case GGML_UNARY_OP_SILU:
  9846. case GGML_UNARY_OP_GELU:
  9847. case GGML_UNARY_OP_GELU_ERF:
  9848. case GGML_UNARY_OP_GELU_QUICK:
  9849. case GGML_UNARY_OP_RELU:
  9850. case GGML_UNARY_OP_TANH:
  9851. case GGML_UNARY_OP_SIGMOID:
  9852. case GGML_UNARY_OP_HARDSIGMOID:
  9853. case GGML_UNARY_OP_HARDSWISH:
  9854. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  9855. break;
  9856. default:
  9857. return false;
  9858. }
  9859. break;
  9860. case GGML_OP_GLU:
  9861. switch (ggml_get_glu_op(node)) {
  9862. case GGML_GLU_OP_GEGLU:
  9863. case GGML_GLU_OP_REGLU:
  9864. case GGML_GLU_OP_SWIGLU:
  9865. case GGML_GLU_OP_SWIGLU_OAI:
  9866. case GGML_GLU_OP_GEGLU_ERF:
  9867. case GGML_GLU_OP_GEGLU_QUICK:
  9868. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  9869. break;
  9870. default:
  9871. return false;
  9872. }
  9873. break;
  9874. case GGML_OP_DIAG_MASK_INF:
  9875. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  9876. break;
  9877. case GGML_OP_SOFT_MAX:
  9878. if (ctx->num_additional_fused_ops) {
  9879. ggml_vk_topk_moe(ctx, compute_ctx, cgraph, node_idx, dryrun);
  9880. } else {
  9881. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9882. }
  9883. break;
  9884. case GGML_OP_SOFT_MAX_BACK:
  9885. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  9886. break;
  9887. case GGML_OP_ROPE:
  9888. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  9889. break;
  9890. case GGML_OP_ROPE_BACK:
  9891. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  9892. break;
  9893. case GGML_OP_ARGSORT:
  9894. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  9895. break;
  9896. case GGML_OP_SUM:
  9897. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  9898. break;
  9899. case GGML_OP_SUM_ROWS:
  9900. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  9901. break;
  9902. case GGML_OP_MEAN:
  9903. ggml_vk_mean(ctx, compute_ctx, src0, node, dryrun);
  9904. break;
  9905. case GGML_OP_ARGMAX:
  9906. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  9907. break;
  9908. case GGML_OP_COUNT_EQUAL:
  9909. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  9910. break;
  9911. case GGML_OP_IM2COL:
  9912. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  9913. break;
  9914. case GGML_OP_IM2COL_3D:
  9915. ggml_vk_im2col_3d(ctx, compute_ctx, src0, src1, node, dryrun);
  9916. break;
  9917. case GGML_OP_TIMESTEP_EMBEDDING:
  9918. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  9919. break;
  9920. case GGML_OP_CONV_TRANSPOSE_1D:
  9921. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  9922. break;
  9923. case GGML_OP_POOL_2D:
  9924. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  9925. break;
  9926. case GGML_OP_CONV_2D:
  9927. ggml_vk_conv_2d(ctx, compute_ctx, src0, src1, node, dryrun);
  9928. break;
  9929. case GGML_OP_CONV_TRANSPOSE_2D:
  9930. ggml_vk_conv_transpose_2d(ctx, compute_ctx, src0, src1, node, dryrun);
  9931. break;
  9932. case GGML_OP_CONV_2D_DW:
  9933. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  9934. break;
  9935. case GGML_OP_LEAKY_RELU:
  9936. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  9937. break;
  9938. case GGML_OP_MUL_MAT:
  9939. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  9940. break;
  9941. case GGML_OP_MUL_MAT_ID:
  9942. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9943. break;
  9944. case GGML_OP_FLASH_ATTN_EXT:
  9945. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node->src[4], node, dryrun);
  9946. break;
  9947. case GGML_OP_RWKV_WKV6:
  9948. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  9949. break;
  9950. case GGML_OP_RWKV_WKV7:
  9951. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  9952. break;
  9953. case GGML_OP_SSM_SCAN:
  9954. ggml_vk_ssm_scan(ctx, compute_ctx, node, dryrun);
  9955. break;
  9956. case GGML_OP_SSM_CONV:
  9957. ggml_vk_ssm_conv(ctx, compute_ctx, node, dryrun);
  9958. break;
  9959. case GGML_OP_OPT_STEP_ADAMW:
  9960. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  9961. break;
  9962. case GGML_OP_OPT_STEP_SGD:
  9963. ggml_vk_opt_step_sgd(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  9964. break;
  9965. default:
  9966. return false;
  9967. }
  9968. if (dryrun) {
  9969. return false;
  9970. }
  9971. ctx->tensor_ctxs[node_idx] = compute_ctx;
  9972. #if defined(GGML_VULKAN_CHECK_RESULTS)
  9973. // Force context reset on each node so that each tensor ends up in its own context
  9974. // and can be run and compared to its CPU equivalent separately
  9975. last_node = true;
  9976. #endif
  9977. if (submit || last_node) {
  9978. ggml_vk_ctx_end(compute_ctx);
  9979. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  9980. if (last_node) {
  9981. compute_ctx->exit_tensor_idx = node_idx_begin;
  9982. }
  9983. else {
  9984. compute_ctx->exit_tensor_idx = -1;
  9985. }
  9986. ctx->compute_ctx.reset();
  9987. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  9988. if (!ok) {
  9989. if (node->op == GGML_OP_UNARY) {
  9990. 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;
  9991. } else if (node->op == GGML_OP_GLU) {
  9992. 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;
  9993. } else {
  9994. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  9995. }
  9996. }
  9997. }
  9998. return true;
  9999. }
  10000. 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) {
  10001. GGML_UNUSED(cgraph);
  10002. ggml_backend_buffer * buf = nullptr;
  10003. switch (tensor->op) {
  10004. case GGML_OP_ADD:
  10005. case GGML_OP_ACC:
  10006. case GGML_OP_GET_ROWS:
  10007. case GGML_OP_SUB:
  10008. case GGML_OP_MUL:
  10009. case GGML_OP_DIV:
  10010. case GGML_OP_ADD_ID:
  10011. case GGML_OP_CONCAT:
  10012. case GGML_OP_UPSCALE:
  10013. case GGML_OP_SCALE:
  10014. case GGML_OP_SQR:
  10015. case GGML_OP_SQRT:
  10016. case GGML_OP_SIN:
  10017. case GGML_OP_COS:
  10018. case GGML_OP_CLAMP:
  10019. case GGML_OP_PAD:
  10020. case GGML_OP_ROLL:
  10021. case GGML_OP_CPY:
  10022. case GGML_OP_SET_ROWS:
  10023. case GGML_OP_CONT:
  10024. case GGML_OP_DUP:
  10025. case GGML_OP_SILU_BACK:
  10026. case GGML_OP_NORM:
  10027. case GGML_OP_GROUP_NORM:
  10028. case GGML_OP_RMS_NORM:
  10029. case GGML_OP_RMS_NORM_BACK:
  10030. case GGML_OP_L2_NORM:
  10031. case GGML_OP_DIAG_MASK_INF:
  10032. case GGML_OP_SOFT_MAX:
  10033. case GGML_OP_SOFT_MAX_BACK:
  10034. case GGML_OP_ROPE:
  10035. case GGML_OP_ROPE_BACK:
  10036. case GGML_OP_RESHAPE:
  10037. case GGML_OP_VIEW:
  10038. case GGML_OP_PERMUTE:
  10039. case GGML_OP_TRANSPOSE:
  10040. case GGML_OP_NONE:
  10041. case GGML_OP_ARGSORT:
  10042. case GGML_OP_SUM:
  10043. case GGML_OP_SUM_ROWS:
  10044. case GGML_OP_MEAN:
  10045. case GGML_OP_ARGMAX:
  10046. case GGML_OP_COUNT_EQUAL:
  10047. case GGML_OP_IM2COL:
  10048. case GGML_OP_IM2COL_3D:
  10049. case GGML_OP_TIMESTEP_EMBEDDING:
  10050. case GGML_OP_CONV_TRANSPOSE_1D:
  10051. case GGML_OP_POOL_2D:
  10052. case GGML_OP_CONV_2D:
  10053. case GGML_OP_CONV_TRANSPOSE_2D:
  10054. case GGML_OP_CONV_2D_DW:
  10055. case GGML_OP_RWKV_WKV6:
  10056. case GGML_OP_RWKV_WKV7:
  10057. case GGML_OP_SSM_SCAN:
  10058. case GGML_OP_SSM_CONV:
  10059. case GGML_OP_LEAKY_RELU:
  10060. case GGML_OP_REPEAT:
  10061. case GGML_OP_REPEAT_BACK:
  10062. case GGML_OP_OPT_STEP_ADAMW:
  10063. case GGML_OP_OPT_STEP_SGD:
  10064. buf = tensor->buffer;
  10065. break;
  10066. case GGML_OP_UNARY:
  10067. switch (ggml_get_unary_op(tensor)) {
  10068. case GGML_UNARY_OP_EXP:
  10069. case GGML_UNARY_OP_SILU:
  10070. case GGML_UNARY_OP_GELU:
  10071. case GGML_UNARY_OP_GELU_ERF:
  10072. case GGML_UNARY_OP_GELU_QUICK:
  10073. case GGML_UNARY_OP_RELU:
  10074. case GGML_UNARY_OP_TANH:
  10075. case GGML_UNARY_OP_SIGMOID:
  10076. case GGML_UNARY_OP_HARDSIGMOID:
  10077. case GGML_UNARY_OP_HARDSWISH:
  10078. buf = tensor->buffer;
  10079. break;
  10080. default:
  10081. return false;
  10082. }
  10083. break;
  10084. case GGML_OP_GLU:
  10085. switch (ggml_get_glu_op(tensor)) {
  10086. case GGML_GLU_OP_GEGLU:
  10087. case GGML_GLU_OP_REGLU:
  10088. case GGML_GLU_OP_SWIGLU:
  10089. case GGML_GLU_OP_SWIGLU_OAI:
  10090. case GGML_GLU_OP_GEGLU_ERF:
  10091. case GGML_GLU_OP_GEGLU_QUICK:
  10092. buf = tensor->buffer;
  10093. break;
  10094. default:
  10095. return false;
  10096. }
  10097. break;
  10098. case GGML_OP_MUL_MAT:
  10099. case GGML_OP_MUL_MAT_ID:
  10100. case GGML_OP_FLASH_ATTN_EXT:
  10101. buf = tensor->buffer;
  10102. break;
  10103. default:
  10104. return false;
  10105. }
  10106. if (buf == nullptr) {
  10107. return false;
  10108. }
  10109. 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 << ")");
  10110. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  10111. // always wait for the GPU work to be done for the last submit
  10112. if (tensor_idx == subctx->exit_tensor_idx) {
  10113. use_fence = true;
  10114. }
  10115. // Only run if ctx hasn't been submitted yet
  10116. if (!subctx->seqs.empty()) {
  10117. #ifdef GGML_VULKAN_CHECK_RESULTS
  10118. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  10119. use_fence = true;
  10120. #endif
  10121. // Do staging buffer copies
  10122. for (auto& cpy : subctx->in_memcpys) {
  10123. memcpy(cpy.dst, cpy.src, cpy.n);
  10124. }
  10125. for (auto& mset : subctx->memsets) {
  10126. memset(mset.dst, mset.val, mset.n);
  10127. }
  10128. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  10129. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  10130. ctx->almost_ready_fence_pending = true;
  10131. } else {
  10132. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  10133. }
  10134. if (use_fence) {
  10135. ggml_vk_wait_for_fence(ctx);
  10136. }
  10137. #ifdef GGML_VULKAN_CHECK_RESULTS
  10138. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  10139. #endif
  10140. }
  10141. if (tensor_idx == subctx->exit_tensor_idx) {
  10142. // Do staging buffer copies
  10143. for (auto& cpy : subctx->out_memcpys) {
  10144. memcpy(cpy.dst, cpy.src, cpy.n);
  10145. }
  10146. subctx->in_memcpys.clear();
  10147. subctx->out_memcpys.clear();
  10148. subctx->memsets.clear();
  10149. }
  10150. return true;
  10151. }
  10152. // Clean up after graph processing is done
  10153. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  10154. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  10155. for (auto& buffer : ctx->gc.temp_buffers) {
  10156. ggml_vk_pool_free(ctx, buffer);
  10157. }
  10158. ctx->gc.temp_buffers.clear();
  10159. ctx->prealloc_y_last_pipeline_used = {};
  10160. ctx->unsynced_nodes_written.clear();
  10161. ctx->unsynced_nodes_read.clear();
  10162. ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false;
  10163. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  10164. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  10165. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  10166. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  10167. }
  10168. ctx->gc.semaphores.clear();
  10169. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  10170. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  10171. }
  10172. ctx->gc.tl_semaphores.clear();
  10173. ctx->semaphore_idx = 0;
  10174. ctx->event_idx = 0;
  10175. for (auto& event : ctx->gc.events) {
  10176. ctx->device->device.resetEvent(event);
  10177. }
  10178. ctx->tensor_ctxs.clear();
  10179. ctx->gc.contexts.clear();
  10180. ctx->pipeline_descriptor_set_requirements = 0;
  10181. ctx->descriptor_set_idx = 0;
  10182. }
  10183. // Clean up on backend free
  10184. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  10185. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  10186. ggml_vk_graph_cleanup(ctx);
  10187. ggml_vk_destroy_buffer(ctx->prealloc_x);
  10188. ggml_vk_destroy_buffer(ctx->prealloc_y);
  10189. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  10190. ctx->prealloc_y_last_pipeline_used = nullptr;
  10191. for (auto& buffer : ctx->buffer_pool) {
  10192. ggml_vk_destroy_buffer(buffer);
  10193. }
  10194. ctx->prealloc_size_x = 0;
  10195. ctx->prealloc_size_y = 0;
  10196. ctx->prealloc_size_split_k = 0;
  10197. for (auto& event : ctx->gc.events) {
  10198. ctx->device->device.destroyEvent(event);
  10199. }
  10200. ctx->gc.events.clear();
  10201. ctx->device->device.destroyFence(ctx->fence);
  10202. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  10203. for (auto& pool : ctx->descriptor_pools) {
  10204. ctx->device->device.destroyDescriptorPool(pool);
  10205. }
  10206. ctx->descriptor_pools.clear();
  10207. ctx->descriptor_sets.clear();
  10208. ctx->compute_cmd_pool.destroy(ctx->device->device);
  10209. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  10210. }
  10211. static int ggml_vk_get_device_count() {
  10212. ggml_vk_instance_init();
  10213. return vk_instance.device_indices.size();
  10214. }
  10215. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  10216. ggml_vk_instance_init();
  10217. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  10218. vk::PhysicalDeviceProperties props;
  10219. devices[device].getProperties(&props);
  10220. snprintf(description, description_size, "%s", props.deviceName.data());
  10221. }
  10222. // backend interface
  10223. #define UNUSED GGML_UNUSED
  10224. // device backend
  10225. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  10226. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  10227. }
  10228. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  10229. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  10230. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10231. ggml_vk_destroy_buffer(ctx->dev_buffer);
  10232. delete ctx;
  10233. }
  10234. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  10235. return vk_ptr_base;
  10236. UNUSED(buffer);
  10237. }
  10238. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  10239. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  10240. if (tensor->view_src != nullptr) {
  10241. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  10242. }
  10243. return GGML_STATUS_SUCCESS;
  10244. }
  10245. 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) {
  10246. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  10247. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10248. vk_buffer buf = buf_ctx->dev_buffer;
  10249. uint32_t val32 = (uint32_t)value * 0x01010101;
  10250. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  10251. }
  10252. 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) {
  10253. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  10254. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10255. vk_buffer buf = buf_ctx->dev_buffer;
  10256. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10257. }
  10258. 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) {
  10259. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  10260. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10261. vk_buffer buf = buf_ctx->dev_buffer;
  10262. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10263. }
  10264. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  10265. if (ggml_backend_buffer_is_vk(src->buffer)) {
  10266. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  10267. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  10268. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  10269. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  10270. 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));
  10271. return true;
  10272. }
  10273. return false;
  10274. UNUSED(buffer);
  10275. }
  10276. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  10277. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10278. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  10279. }
  10280. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  10281. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  10282. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  10283. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  10284. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  10285. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  10286. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  10287. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  10288. /* .clear = */ ggml_backend_vk_buffer_clear,
  10289. /* .reset = */ NULL,
  10290. };
  10291. // vk buffer type
  10292. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  10293. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  10294. return ctx->name.c_str();
  10295. }
  10296. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  10297. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  10298. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  10299. vk_buffer dev_buffer = nullptr;
  10300. try {
  10301. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  10302. } catch (const vk::SystemError& e) {
  10303. return nullptr;
  10304. }
  10305. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  10306. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  10307. }
  10308. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  10309. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  10310. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  10311. }
  10312. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  10313. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  10314. return ctx->device->suballocation_block_size;
  10315. }
  10316. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  10317. return ggml_nbytes(tensor);
  10318. UNUSED(buft);
  10319. }
  10320. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  10321. ggml_vk_instance_init();
  10322. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  10323. vk_device dev = ggml_vk_get_device(dev_num);
  10324. return &dev->buffer_type;
  10325. }
  10326. // host buffer type
  10327. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  10328. return GGML_VK_NAME "_Host";
  10329. UNUSED(buft);
  10330. }
  10331. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  10332. return GGML_VK_NAME "_Host";
  10333. UNUSED(buffer);
  10334. }
  10335. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  10336. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  10337. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  10338. }
  10339. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  10340. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  10341. size += 32; // Behave like the CPU buffer type
  10342. void * ptr = nullptr;
  10343. try {
  10344. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  10345. } catch (vk::SystemError& e) {
  10346. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  10347. // fallback to cpu buffer
  10348. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  10349. }
  10350. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  10351. buffer->buft = buft;
  10352. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  10353. return buffer;
  10354. UNUSED(buft);
  10355. }
  10356. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  10357. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  10358. UNUSED(buft);
  10359. }
  10360. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  10361. return vk_instance.devices[0]->suballocation_block_size;
  10362. UNUSED(buft);
  10363. }
  10364. // Should be changed to return device-specific host buffer type
  10365. // but that probably requires changes in llama.cpp
  10366. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  10367. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  10368. /* .iface = */ {
  10369. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  10370. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  10371. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  10372. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  10373. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  10374. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  10375. },
  10376. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  10377. /* .context = */ nullptr,
  10378. };
  10379. // Make sure device 0 is initialized
  10380. ggml_vk_instance_init();
  10381. ggml_vk_get_device(0);
  10382. return &ggml_backend_vk_buffer_type_host;
  10383. }
  10384. // backend
  10385. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  10386. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10387. return ctx->name.c_str();
  10388. }
  10389. static void ggml_backend_vk_free(ggml_backend_t backend) {
  10390. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10391. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  10392. ggml_vk_cleanup(ctx);
  10393. delete ctx;
  10394. delete backend;
  10395. }
  10396. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  10397. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10398. return &ctx->device->buffer_type;
  10399. }
  10400. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  10401. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  10402. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10403. 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");
  10404. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10405. vk_context transfer_ctx;
  10406. if (ctx->transfer_ctx.expired()) {
  10407. // Initialize new transfer context
  10408. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  10409. ctx->transfer_ctx = transfer_ctx;
  10410. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  10411. } else {
  10412. transfer_ctx = ctx->transfer_ctx.lock();
  10413. }
  10414. vk_buffer buf = buf_ctx->dev_buffer;
  10415. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10416. }
  10417. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  10418. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  10419. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10420. 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");
  10421. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10422. vk_context transfer_ctx;
  10423. if (ctx->transfer_ctx.expired()) {
  10424. // Initialize new transfer context
  10425. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  10426. ctx->transfer_ctx = transfer_ctx;
  10427. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  10428. } else {
  10429. transfer_ctx = ctx->transfer_ctx.lock();
  10430. }
  10431. vk_buffer buf = buf_ctx->dev_buffer;
  10432. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10433. }
  10434. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  10435. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  10436. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10437. 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)) {
  10438. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  10439. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  10440. vk_context transfer_ctx;
  10441. if (ctx->transfer_ctx.expired()) {
  10442. // Initialize new transfer context
  10443. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  10444. ctx->transfer_ctx = transfer_ctx;
  10445. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  10446. } else {
  10447. transfer_ctx = ctx->transfer_ctx.lock();
  10448. }
  10449. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  10450. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  10451. 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));
  10452. return true;
  10453. }
  10454. return false;
  10455. }
  10456. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  10457. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  10458. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10459. if(ctx->transfer_ctx.expired()) {
  10460. return;
  10461. }
  10462. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  10463. ggml_vk_ctx_end(transfer_ctx);
  10464. for (auto& cpy : transfer_ctx->in_memcpys) {
  10465. memcpy(cpy.dst, cpy.src, cpy.n);
  10466. }
  10467. ggml_vk_submit(transfer_ctx, ctx->fence);
  10468. ggml_vk_wait_for_fence(ctx);
  10469. for (auto& cpy : transfer_ctx->out_memcpys) {
  10470. memcpy(cpy.dst, cpy.src, cpy.n);
  10471. }
  10472. ctx->transfer_ctx.reset();
  10473. }
  10474. static bool ggml_vk_is_empty(ggml_tensor * node) {
  10475. 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;
  10476. }
  10477. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  10478. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  10479. return false;
  10480. }
  10481. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  10482. // additional constraints specific to this fusion
  10483. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  10484. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  10485. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  10486. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  10487. // rms_norm only supports f32
  10488. if (mul->src[0]->type != GGML_TYPE_F32 ||
  10489. mul->src[1]->type != GGML_TYPE_F32 ||
  10490. mul->type != GGML_TYPE_F32) {
  10491. return false;
  10492. }
  10493. // if rms_norm is the B operand, then we don't handle broadcast
  10494. if (rms_norm == mul->src[1] &&
  10495. !ggml_are_same_shape(mul->src[0], rms_norm)) {
  10496. return false;
  10497. }
  10498. // rms_norm shader assumes contiguous rows
  10499. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  10500. return false;
  10501. }
  10502. }
  10503. return true;
  10504. }
  10505. static bool ggml_vk_can_fuse_topk_moe(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph,
  10506. int node_idx, bool with_norm) {
  10507. if (with_norm) {
  10508. if (node_idx + (int)topk_moe_norm.size() > cgraph->n_nodes) {
  10509. return false;
  10510. }
  10511. for (size_t i = 0; i < topk_moe_norm.size(); ++i) {
  10512. if (cgraph->nodes[node_idx + i]->op != topk_moe_norm[i]) {
  10513. return false;
  10514. }
  10515. }
  10516. } else {
  10517. if (node_idx + (int)topk_moe.size() > cgraph->n_nodes) {
  10518. return false;
  10519. }
  10520. for (size_t i = 0; i < topk_moe.size(); ++i) {
  10521. if (cgraph->nodes[node_idx + i]->op != topk_moe[i]) {
  10522. return false;
  10523. }
  10524. }
  10525. }
  10526. const ggml_tensor * softmax = cgraph->nodes[node_idx + 0];
  10527. const ggml_tensor * weights = with_norm ? cgraph->nodes[node_idx + 8] : cgraph->nodes[node_idx + 4];
  10528. const float * op_params = (const float *)softmax->op_params;
  10529. float scale = op_params[0];
  10530. float max_bias = op_params[1];
  10531. if (!ggml_is_contiguous(softmax->src[0]) || !ggml_is_contiguous(weights)) {
  10532. return false;
  10533. }
  10534. if (scale != 1.0f || max_bias != 0.0f) {
  10535. return false;
  10536. }
  10537. // don't fuse when masks or sinks are present
  10538. if (softmax->src[1] || softmax->src[2]) {
  10539. return false;
  10540. }
  10541. const int n_expert = softmax->ne[0];
  10542. // n_expert must be a power of 2
  10543. if (!is_pow2(n_expert) || n_expert > (1 << (num_topk_moe_pipelines-1))) {
  10544. return false;
  10545. }
  10546. // Check that the nodes don't have any unexpected uses
  10547. const ggml_tensor * reshape1 = cgraph->nodes[node_idx + 1];
  10548. const ggml_tensor * argsort = cgraph->nodes[node_idx + 2];
  10549. const ggml_tensor * view = cgraph->nodes[node_idx + 3];
  10550. const ggml_tensor * get_rows = cgraph->nodes[node_idx + 4];
  10551. const ggml_tensor * reshape5 = with_norm ? cgraph->nodes[node_idx + 5] : nullptr;
  10552. const ggml_tensor * sum_rows = with_norm ? cgraph->nodes[node_idx + 6] : nullptr;
  10553. const ggml_tensor * div = with_norm ? cgraph->nodes[node_idx + 7] : nullptr;
  10554. const ggml_tensor * reshape8 = with_norm ? cgraph->nodes[node_idx + 8] : nullptr;
  10555. // softmax is used by reshape and argsort
  10556. if (ggml_node_get_use_count(cgraph, node_idx) != 2 ||
  10557. reshape1->src[0] != softmax ||
  10558. argsort->src[0] != softmax) {
  10559. return false;
  10560. }
  10561. // reshape is used by get_rows
  10562. if (ggml_node_get_use_count(cgraph, node_idx + 1) != 1 ||
  10563. get_rows->src[0] != reshape1) {
  10564. return false;
  10565. }
  10566. // argsort is used by view
  10567. if (ggml_node_get_use_count(cgraph, node_idx + 2) != 1 ||
  10568. view->src[0] != argsort) {
  10569. return false;
  10570. }
  10571. // view is written (via argsort), we can skip checking it
  10572. if (with_norm) {
  10573. // get_rows is used by reshape
  10574. if (ggml_node_get_use_count(cgraph, node_idx + 4) != 1 ||
  10575. reshape5->src[0] != get_rows) {
  10576. return false;
  10577. }
  10578. // reshape is used by sum_rows and div
  10579. if (ggml_node_get_use_count(cgraph, node_idx + 5) != 2 ||
  10580. sum_rows->src[0] != reshape5 ||
  10581. div->src[0] != reshape5) {
  10582. return false;
  10583. }
  10584. // sum_rows is used by div
  10585. if (ggml_node_get_use_count(cgraph, node_idx + 6) != 1 ||
  10586. div->src[1] != sum_rows) {
  10587. return false;
  10588. }
  10589. // div/reshape are written
  10590. if (reshape8->src[0] != div) {
  10591. return false;
  10592. }
  10593. }
  10594. if (!ctx->device->subgroup_arithmetic ||
  10595. !ctx->device->subgroup_shuffle ||
  10596. !ctx->device->subgroup_require_full_support ||
  10597. ctx->device->disable_fusion) {
  10598. return false;
  10599. }
  10600. return true;
  10601. }
  10602. static uint32_t ggml_vk_fuse_multi_add(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph, int node_idx) {
  10603. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  10604. if (first_node->op != GGML_OP_ADD) {
  10605. return 0;
  10606. }
  10607. if (!ctx->device->multi_add) {
  10608. return 0;
  10609. }
  10610. int32_t num_adds = 1;
  10611. while (node_idx + num_adds < cgraph->n_nodes &&
  10612. cgraph->nodes[node_idx + num_adds]->op == GGML_OP_ADD &&
  10613. num_adds < MAX_FUSED_ADDS) {
  10614. num_adds++;
  10615. }
  10616. // The shader currently requires same shapes (but different strides are allowed),
  10617. // everything f32, and no misalignment
  10618. for (int32_t i = 0; i < num_adds; ++i) {
  10619. const ggml_tensor *next_node = cgraph->nodes[node_idx + i];
  10620. if (!ggml_are_same_shape(first_node, next_node->src[0]) ||
  10621. !ggml_are_same_shape(first_node, next_node->src[1]) ||
  10622. next_node->type != GGML_TYPE_F32 ||
  10623. next_node->src[0]->type != GGML_TYPE_F32 ||
  10624. next_node->src[1]->type != GGML_TYPE_F32 ||
  10625. get_misalign_bytes(ctx, next_node) ||
  10626. get_misalign_bytes(ctx, next_node->src[0]) ||
  10627. get_misalign_bytes(ctx, next_node->src[1])) {
  10628. num_adds = i;
  10629. }
  10630. }
  10631. // Verify we can fuse these
  10632. ggml_op adds[MAX_FUSED_ADDS];
  10633. for (int32_t i = 0; i < num_adds; ++i) {
  10634. adds[i] = GGML_OP_ADD;
  10635. }
  10636. // decrease num_adds if they can't all be fused
  10637. while (num_adds > 1 && !ggml_can_fuse(cgraph, node_idx, adds, num_adds)) {
  10638. num_adds--;
  10639. }
  10640. // a single add is not "fused", so just return zero
  10641. if (num_adds == 1) {
  10642. return 0;
  10643. }
  10644. return num_adds;
  10645. }
  10646. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  10647. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  10648. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10649. if (vk_instance.debug_utils_support) {
  10650. vk::DebugUtilsLabelEXT dul = {};
  10651. dul.pLabelName = "ggml_backend_vk_graph_compute";
  10652. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  10653. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  10654. }
  10655. ctx->prealloc_size_add_rms_partials = 0;
  10656. ctx->prealloc_size_add_rms_partials_offset = 0;
  10657. ctx->do_add_rms_partials = false;
  10658. uint64_t total_mat_mul_bytes = 0;
  10659. for (int i = 0; i < cgraph->n_nodes; i++) {
  10660. if (!ctx->device->disable_fusion) {
  10661. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  10662. if (num_adds) {
  10663. ctx->num_additional_fused_ops = num_adds - 1;
  10664. } else if (ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  10665. ctx->num_additional_fused_ops = 1;
  10666. } else if (ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, true)) {
  10667. ctx->num_additional_fused_ops = topk_moe_norm.size() - 1;
  10668. } else if (ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, false)) {
  10669. ctx->num_additional_fused_ops = topk_moe.size() - 1;
  10670. }
  10671. }
  10672. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  10673. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  10674. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  10675. } else if (cgraph->nodes[i]->op == GGML_OP_CONV_2D || cgraph->nodes[i]->op == GGML_OP_CONV_TRANSPOSE_2D) {
  10676. // Return CRSxNPQxsizeof(*) to account as many bytes as mul_mat has in im2col->mul_mat mode.
  10677. auto CRS_size =
  10678. cgraph->nodes[i]->src[0]->ne[0] * cgraph->nodes[i]->src[0]->ne[1] * cgraph->nodes[i]->src[1]->ne[2];
  10679. auto NPQ_size = cgraph->nodes[i]->ne[0] * cgraph->nodes[i]->ne[1] * cgraph->nodes[i]->ne[3];
  10680. total_mat_mul_bytes += NPQ_size * CRS_size * ggml_type_size(cgraph->nodes[i]->type);
  10681. }
  10682. i += ctx->num_additional_fused_ops;
  10683. ctx->num_additional_fused_ops = 0;
  10684. }
  10685. if (ctx->device->need_compiles) {
  10686. ggml_vk_load_shaders(ctx->device);
  10687. }
  10688. ggml_vk_preallocate_buffers(ctx);
  10689. ggml_pipeline_allocate_descriptor_sets(ctx);
  10690. int last_node = cgraph->n_nodes - 1;
  10691. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  10692. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  10693. last_node -= 1;
  10694. }
  10695. // Reserve tensor context space for all nodes
  10696. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  10697. bool first_node_in_batch = true; // true if next node will be first node in a batch
  10698. int submit_node_idx = 0; // index to first node in a batch
  10699. vk_context compute_ctx;
  10700. if (vk_perf_logger_enabled) {
  10701. // allocate/resize the query pool
  10702. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  10703. if (ctx->device->query_pool) {
  10704. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  10705. }
  10706. vk::QueryPoolCreateInfo query_create_info;
  10707. query_create_info.queryType = vk::QueryType::eTimestamp;
  10708. query_create_info.queryCount = cgraph->n_nodes + 100;
  10709. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  10710. ctx->device->num_queries = query_create_info.queryCount;
  10711. }
  10712. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  10713. GGML_ASSERT(ctx->compute_ctx.expired());
  10714. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  10715. ctx->compute_ctx = compute_ctx;
  10716. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  10717. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  10718. }
  10719. ctx->prealloc_y_last_pipeline_used = nullptr;
  10720. ctx->prealloc_y_last_tensor_used = nullptr;
  10721. if (ctx->prealloc_size_add_rms_partials) {
  10722. if (ctx->compute_ctx.expired()) {
  10723. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  10724. ctx->compute_ctx = compute_ctx;
  10725. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  10726. } else {
  10727. compute_ctx = ctx->compute_ctx.lock();
  10728. }
  10729. // initialize partial sums to zero.
  10730. ggml_vk_buffer_memset_async(compute_ctx, ctx->prealloc_add_rms_partials, 0, 0, ctx->prealloc_size_add_rms_partials);
  10731. ggml_vk_sync_buffers(ctx, compute_ctx);
  10732. }
  10733. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  10734. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  10735. // (and scaled down based on model size, so smaller models submit earlier).
  10736. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  10737. int nodes_per_submit = 100;
  10738. int submitted_nodes = 0;
  10739. int submit_count = 0;
  10740. uint64_t mul_mat_bytes = 0;
  10741. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  10742. for (int i = 0; i < cgraph->n_nodes; i++) {
  10743. if (first_node_in_batch) {
  10744. submit_node_idx = i;
  10745. }
  10746. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  10747. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  10748. }
  10749. if (!ctx->device->disable_fusion) {
  10750. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  10751. if (num_adds) {
  10752. ctx->num_additional_fused_ops = num_adds - 1;
  10753. } else if (ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  10754. ctx->num_additional_fused_ops = 1;
  10755. } else if (ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, true)) {
  10756. ctx->num_additional_fused_ops = topk_moe_norm.size() - 1;
  10757. } else if (ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, false)) {
  10758. ctx->num_additional_fused_ops = topk_moe.size() - 1;
  10759. }
  10760. }
  10761. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  10762. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  10763. bool submit = (submitted_nodes >= nodes_per_submit) ||
  10764. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  10765. (i + ctx->num_additional_fused_ops >= last_node) ||
  10766. (almost_ready && !ctx->almost_ready_fence_pending);
  10767. 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);
  10768. if (vk_perf_logger_enabled) {
  10769. if (ctx->compute_ctx.expired()) {
  10770. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  10771. ctx->compute_ctx = compute_ctx;
  10772. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  10773. } else {
  10774. compute_ctx = ctx->compute_ctx.lock();
  10775. }
  10776. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  10777. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  10778. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  10779. }
  10780. }
  10781. if (enqueued) {
  10782. ++submitted_nodes;
  10783. #ifndef GGML_VULKAN_CHECK_RESULTS
  10784. if (first_node_in_batch) {
  10785. first_node_in_batch = false;
  10786. }
  10787. #endif
  10788. }
  10789. if (submit && enqueued) {
  10790. first_node_in_batch = true;
  10791. submitted_nodes = 0;
  10792. mul_mat_bytes = 0;
  10793. if (submit_count < 3) {
  10794. mul_mat_bytes_per_submit *= 2;
  10795. }
  10796. submit_count++;
  10797. }
  10798. i += ctx->num_additional_fused_ops;
  10799. ctx->num_additional_fused_ops = 0;
  10800. }
  10801. if (vk_perf_logger_enabled) {
  10802. // End the command buffer and submit/wait
  10803. GGML_ASSERT(!ctx->compute_ctx.expired());
  10804. compute_ctx = ctx->compute_ctx.lock();
  10805. ggml_vk_ctx_end(compute_ctx);
  10806. ggml_vk_submit(compute_ctx, ctx->device->fence);
  10807. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  10808. ctx->device->device.resetFences({ ctx->device->fence });
  10809. // Get the results and pass them to the logger
  10810. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  10811. 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");
  10812. for (int i = 0; i < cgraph->n_nodes; i++) {
  10813. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  10814. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  10815. }
  10816. }
  10817. ctx->device->perf_logger->print_timings();
  10818. }
  10819. ggml_vk_graph_cleanup(ctx);
  10820. return GGML_STATUS_SUCCESS;
  10821. UNUSED(backend);
  10822. }
  10823. // Sort the graph for improved parallelism.
  10824. static void ggml_vk_graph_optimize(ggml_backend_t backend, struct ggml_cgraph * graph)
  10825. {
  10826. VK_LOG_DEBUG("ggml_vk_graph_optimize(" << graph->n_nodes << " nodes)");
  10827. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10828. if (ctx->device->disable_graph_optimize) {
  10829. return;
  10830. }
  10831. auto const &is_empty = [](ggml_tensor * node) -> bool {
  10832. return 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;
  10833. };
  10834. auto const &is_src_of = [](const ggml_tensor *dst, const ggml_tensor *src) -> bool {
  10835. for (uint32_t s = 0; s < GGML_MAX_SRC; ++s) {
  10836. if (dst->src[s] == src) {
  10837. return true;
  10838. }
  10839. }
  10840. // implicit dependency if they view the same tensor
  10841. const ggml_tensor *dst2 = dst->view_src ? dst->view_src : dst;
  10842. const ggml_tensor *src2 = src->view_src ? src->view_src : src;
  10843. if (dst2 == src2) {
  10844. return true;
  10845. }
  10846. return false;
  10847. };
  10848. // This function tries to reorder the graph to allow nodes to run in parallel.
  10849. // This helps with small batches, but for large batches its a slowdown, probably
  10850. // due to cache contention. So only reorder if the majority of nodes have few rows.
  10851. int num_small_nodes = 0;
  10852. int num_counted_nodes = 0;
  10853. for (int i = 0; i < graph->n_nodes; ++i) {
  10854. if (!is_empty(graph->nodes[i]) &&
  10855. graph->nodes[i]->op != GGML_OP_SET_ROWS) {
  10856. if (ggml_nrows(graph->nodes[i]) <= 8) {
  10857. num_small_nodes++;
  10858. }
  10859. num_counted_nodes++;
  10860. }
  10861. }
  10862. if (num_small_nodes < num_counted_nodes / 2) {
  10863. return;
  10864. }
  10865. std::vector<ggml_tensor *> new_order;
  10866. std::vector<bool> used(graph->n_nodes, false);
  10867. int first_unused = 0;
  10868. while (first_unused < graph->n_nodes) {
  10869. std::vector<int> current_set;
  10870. // Avoid reordering topk_moe_norm
  10871. if (first_unused + (int)topk_moe_norm.size() <= graph->n_nodes) {
  10872. bool is_topk_moe_norm = true;
  10873. for (size_t j = 0; j < topk_moe_norm.size(); ++j) {
  10874. if (graph->nodes[first_unused + j]->op != topk_moe_norm[j] || used[first_unused + j]) {
  10875. is_topk_moe_norm = false;
  10876. }
  10877. }
  10878. if (is_topk_moe_norm) {
  10879. for (size_t j = 0; j < topk_moe_norm.size(); ++j) {
  10880. new_order.push_back(graph->nodes[first_unused + j]);
  10881. used[first_unused + j] = true;
  10882. }
  10883. while (first_unused < graph->n_nodes && used[first_unused]) {
  10884. first_unused++;
  10885. }
  10886. continue;
  10887. }
  10888. }
  10889. // First, grab the next unused node.
  10890. current_set.push_back(first_unused);
  10891. // Loop through the next N nodes. Grab any that don't depend on other nodes that
  10892. // haven't already been run. Nodes that have already been run have used[i] set
  10893. // to true. Allow nodes that depend on the previous node if it's a fusion pattern
  10894. // that we support (e.g. RMS_NORM + MUL).
  10895. // This first pass only grabs "real" (non-view nodes). Second pass grabs view nodes.
  10896. // The goal is to not interleave real and view nodes in a way that breaks fusion.
  10897. const int NUM_TO_CHECK = 20;
  10898. for (int j = first_unused+1; j < std::min(first_unused + NUM_TO_CHECK, graph->n_nodes); ++j) {
  10899. if (used[j]) {
  10900. continue;
  10901. }
  10902. if (is_empty(graph->nodes[j])) {
  10903. continue;
  10904. }
  10905. bool ok = true;
  10906. for (int c = first_unused; c < j; ++c) {
  10907. if (!used[c] &&
  10908. is_src_of(graph->nodes[j], graph->nodes[c]) &&
  10909. !(j == c+1 && c == current_set.back() && graph->nodes[c]->op == GGML_OP_RMS_NORM && graph->nodes[j]->op == GGML_OP_MUL)) {
  10910. ok = false;
  10911. break;
  10912. }
  10913. }
  10914. if (ok) {
  10915. current_set.push_back(j);
  10916. }
  10917. }
  10918. // Second pass grabs view nodes.
  10919. // Skip this if it would break a fusion optimization (don't split up add->rms_norm or add->add).
  10920. if (graph->nodes[current_set.back()]->op != GGML_OP_ADD) {
  10921. for (int j = first_unused+1; j < std::min(first_unused + NUM_TO_CHECK, graph->n_nodes); ++j) {
  10922. if (used[j]) {
  10923. continue;
  10924. }
  10925. if (!is_empty(graph->nodes[j])) {
  10926. continue;
  10927. }
  10928. bool ok = true;
  10929. for (int c = first_unused; c < j; ++c) {
  10930. bool c_in_current_set = std::find(current_set.begin(), current_set.end(), c) != current_set.end();
  10931. // skip views whose srcs haven't been processed.
  10932. if (!used[c] &&
  10933. is_src_of(graph->nodes[j], graph->nodes[c]) &&
  10934. !c_in_current_set) {
  10935. ok = false;
  10936. break;
  10937. }
  10938. }
  10939. if (ok) {
  10940. current_set.push_back(j);
  10941. }
  10942. }
  10943. }
  10944. // Push the current set into new_order
  10945. for (auto c : current_set) {
  10946. new_order.push_back(graph->nodes[c]);
  10947. used[c] = true;
  10948. }
  10949. while (first_unused < graph->n_nodes && used[first_unused]) {
  10950. first_unused++;
  10951. }
  10952. }
  10953. // Replace the graph with the new order.
  10954. for (int i = 0; i < graph->n_nodes; ++i) {
  10955. graph->nodes[i] = new_order[i];
  10956. }
  10957. }
  10958. // TODO: enable async and synchronize
  10959. static ggml_backend_i ggml_backend_vk_interface = {
  10960. /* .get_name = */ ggml_backend_vk_name,
  10961. /* .free = */ ggml_backend_vk_free,
  10962. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  10963. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  10964. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  10965. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  10966. /* .graph_plan_create = */ NULL,
  10967. /* .graph_plan_free = */ NULL,
  10968. /* .graph_plan_update = */ NULL,
  10969. /* .graph_plan_compute = */ NULL,
  10970. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  10971. /* .event_record = */ NULL,
  10972. /* .event_wait = */ NULL,
  10973. /* .graph_optimize = */ ggml_vk_graph_optimize,
  10974. };
  10975. static ggml_guid_t ggml_backend_vk_guid() {
  10976. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  10977. return &guid;
  10978. }
  10979. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  10980. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  10981. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  10982. ggml_vk_init(ctx, dev_num);
  10983. ggml_backend_t vk_backend = new ggml_backend {
  10984. /* .guid = */ ggml_backend_vk_guid(),
  10985. /* .iface = */ ggml_backend_vk_interface,
  10986. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  10987. /* .context = */ ctx,
  10988. };
  10989. return vk_backend;
  10990. }
  10991. bool ggml_backend_is_vk(ggml_backend_t backend) {
  10992. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  10993. }
  10994. int ggml_backend_vk_get_device_count() {
  10995. return ggml_vk_get_device_count();
  10996. }
  10997. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  10998. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  10999. int dev_idx = vk_instance.device_indices[device];
  11000. ggml_vk_get_device_description(dev_idx, description, description_size);
  11001. }
  11002. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  11003. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  11004. GGML_ASSERT(device < (int) vk_instance.device_supports_membudget.size());
  11005. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  11006. vk::PhysicalDeviceMemoryBudgetPropertiesEXT budgetprops;
  11007. vk::PhysicalDeviceMemoryProperties2 memprops = {};
  11008. bool membudget_supported = vk_instance.device_supports_membudget[device];
  11009. if (membudget_supported) {
  11010. memprops.pNext = &budgetprops;
  11011. }
  11012. vkdev.getMemoryProperties2(&memprops);
  11013. for (uint32_t i = 0; i < memprops.memoryProperties.memoryHeapCount; ++i) {
  11014. const vk::MemoryHeap & heap = memprops.memoryProperties.memoryHeaps[i];
  11015. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  11016. *total = heap.size;
  11017. if (membudget_supported && i < budgetprops.heapUsage.size()) {
  11018. *free = budgetprops.heapBudget[i] - budgetprops.heapUsage[i];
  11019. } else {
  11020. *free = heap.size;
  11021. }
  11022. break;
  11023. }
  11024. }
  11025. }
  11026. static vk::PhysicalDeviceType ggml_backend_vk_get_device_type(int device_idx) {
  11027. GGML_ASSERT(device_idx >= 0 && device_idx < (int) vk_instance.device_indices.size());
  11028. vk::PhysicalDevice device = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device_idx]];
  11029. vk::PhysicalDeviceProperties2 props = {};
  11030. device.getProperties2(&props);
  11031. return props.properties.deviceType;
  11032. }
  11033. static std::string ggml_backend_vk_get_device_pci_id(int device_idx) {
  11034. GGML_ASSERT(device_idx >= 0 && device_idx < (int) vk_instance.device_indices.size());
  11035. vk::PhysicalDevice device = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device_idx]];
  11036. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  11037. bool ext_support = false;
  11038. for (const auto& properties : ext_props) {
  11039. if (strcmp("VK_EXT_pci_bus_info", properties.extensionName) == 0) {
  11040. ext_support = true;
  11041. break;
  11042. }
  11043. }
  11044. if (!ext_support) {
  11045. return "";
  11046. }
  11047. vk::PhysicalDeviceProperties2 props = {};
  11048. vk::PhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info = {};
  11049. props.pNext = &pci_bus_info;
  11050. device.getProperties2(&props);
  11051. const uint32_t pci_domain = pci_bus_info.pciDomain;
  11052. const uint32_t pci_bus = pci_bus_info.pciBus;
  11053. const uint32_t pci_device = pci_bus_info.pciDevice;
  11054. const uint8_t pci_function = (uint8_t) pci_bus_info.pciFunction; // pci function is between 0 and 7, prevent printf overflow warning
  11055. char pci_bus_id[16] = {};
  11056. snprintf(pci_bus_id, sizeof(pci_bus_id), "%04x:%02x:%02x.%x", pci_domain, pci_bus, pci_device, pci_function);
  11057. return std::string(pci_bus_id);
  11058. }
  11059. //////////////////////////
  11060. struct ggml_backend_vk_device_context {
  11061. size_t device;
  11062. std::string name;
  11063. std::string description;
  11064. bool is_integrated_gpu;
  11065. std::string pci_bus_id;
  11066. };
  11067. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  11068. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11069. return ctx->name.c_str();
  11070. }
  11071. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  11072. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11073. return ctx->description.c_str();
  11074. }
  11075. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  11076. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  11077. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  11078. }
  11079. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  11080. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11081. return ggml_backend_vk_buffer_type(ctx->device);
  11082. }
  11083. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  11084. UNUSED(dev);
  11085. return ggml_backend_vk_host_buffer_type();
  11086. }
  11087. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  11088. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11089. return ctx->is_integrated_gpu ? GGML_BACKEND_DEVICE_TYPE_IGPU : GGML_BACKEND_DEVICE_TYPE_GPU;
  11090. }
  11091. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  11092. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11093. props->name = ggml_backend_vk_device_get_name(dev);
  11094. props->description = ggml_backend_vk_device_get_description(dev);
  11095. props->type = ggml_backend_vk_device_get_type(dev);
  11096. props->device_id = ctx->pci_bus_id.empty() ? nullptr : ctx->pci_bus_id.c_str();
  11097. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  11098. props->caps = {
  11099. /* .async = */ false,
  11100. /* .host_buffer = */ true,
  11101. /* .buffer_from_host_ptr = */ false,
  11102. /* .events = */ false,
  11103. };
  11104. }
  11105. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  11106. UNUSED(params);
  11107. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11108. return ggml_backend_vk_init(ctx->device);
  11109. }
  11110. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  11111. switch (op->op) {
  11112. case GGML_OP_UNARY:
  11113. switch (ggml_get_unary_op(op)) {
  11114. case GGML_UNARY_OP_EXP:
  11115. case GGML_UNARY_OP_GELU:
  11116. case GGML_UNARY_OP_GELU_ERF:
  11117. case GGML_UNARY_OP_GELU_QUICK:
  11118. case GGML_UNARY_OP_SILU:
  11119. case GGML_UNARY_OP_RELU:
  11120. case GGML_UNARY_OP_TANH:
  11121. case GGML_UNARY_OP_SIGMOID:
  11122. case GGML_UNARY_OP_HARDSIGMOID:
  11123. case GGML_UNARY_OP_HARDSWISH:
  11124. return ggml_is_contiguous(op->src[0]) &&
  11125. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11126. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  11127. (op->src[0]->type == op->type);
  11128. default:
  11129. return false;
  11130. }
  11131. case GGML_OP_GLU:
  11132. switch (ggml_get_glu_op(op)) {
  11133. case GGML_GLU_OP_GEGLU:
  11134. case GGML_GLU_OP_REGLU:
  11135. case GGML_GLU_OP_SWIGLU:
  11136. case GGML_GLU_OP_SWIGLU_OAI:
  11137. case GGML_GLU_OP_GEGLU_ERF:
  11138. case GGML_GLU_OP_GEGLU_QUICK:
  11139. return ggml_is_contiguous(op->src[0]) &&
  11140. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11141. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  11142. (op->src[0]->type == op->type);
  11143. default:
  11144. return false;
  11145. }
  11146. case GGML_OP_MUL_MAT:
  11147. case GGML_OP_MUL_MAT_ID:
  11148. {
  11149. ggml_type src0_type = op->src[0]->type;
  11150. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11151. const vk_device& device = ggml_vk_get_device(ctx->device);
  11152. if (op->op == GGML_OP_MUL_MAT_ID) {
  11153. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  11154. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  11155. return false;
  11156. }
  11157. }
  11158. switch (src0_type) {
  11159. case GGML_TYPE_F32:
  11160. case GGML_TYPE_F16:
  11161. case GGML_TYPE_BF16:
  11162. case GGML_TYPE_Q4_0:
  11163. case GGML_TYPE_Q4_1:
  11164. case GGML_TYPE_Q5_0:
  11165. case GGML_TYPE_Q5_1:
  11166. case GGML_TYPE_Q8_0:
  11167. case GGML_TYPE_Q2_K:
  11168. case GGML_TYPE_Q3_K:
  11169. case GGML_TYPE_Q4_K:
  11170. case GGML_TYPE_Q5_K:
  11171. case GGML_TYPE_Q6_K:
  11172. case GGML_TYPE_IQ1_S:
  11173. case GGML_TYPE_IQ1_M:
  11174. case GGML_TYPE_IQ2_XXS:
  11175. case GGML_TYPE_IQ2_XS:
  11176. case GGML_TYPE_IQ2_S:
  11177. case GGML_TYPE_IQ3_XXS:
  11178. case GGML_TYPE_IQ3_S:
  11179. case GGML_TYPE_IQ4_XS:
  11180. case GGML_TYPE_IQ4_NL:
  11181. case GGML_TYPE_MXFP4:
  11182. break;
  11183. default:
  11184. return false;
  11185. }
  11186. struct ggml_tensor * a;
  11187. struct ggml_tensor * b;
  11188. if (op->op == GGML_OP_MUL_MAT) {
  11189. a = op->src[0];
  11190. b = op->src[1];
  11191. } else {
  11192. a = op->src[2];
  11193. b = op->src[1];
  11194. }
  11195. if (a->ne[3] != b->ne[3]) {
  11196. return false;
  11197. }
  11198. 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) ||
  11199. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  11200. return false;
  11201. }
  11202. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  11203. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  11204. // So don't support this combination for now.
  11205. return false;
  11206. }
  11207. return true;
  11208. }
  11209. case GGML_OP_FLASH_ATTN_EXT:
  11210. {
  11211. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11212. auto device = ggml_vk_get_device(ctx->device);
  11213. bool coopmat2 = device->coopmat2;
  11214. uint32_t HSK = op->src[1]->ne[0];
  11215. uint32_t HSV = op->src[2]->ne[0];
  11216. if ((HSK % 8) != 0 || (HSV % 8) != 0) {
  11217. return false;
  11218. }
  11219. if (op->src[4] && op->src[4]->type != GGML_TYPE_F32) {
  11220. return false;
  11221. }
  11222. if (op->src[0]->type != GGML_TYPE_F32) {
  11223. return false;
  11224. }
  11225. if (op->type != GGML_TYPE_F32) {
  11226. return false;
  11227. }
  11228. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  11229. return false;
  11230. }
  11231. // It's straightforward to support different K/V dequant, but would
  11232. // significantly increase the number of pipelines
  11233. if (op->src[1]->type != op->src[2]->type) {
  11234. return false;
  11235. }
  11236. switch (op->src[1]->type) {
  11237. case GGML_TYPE_F16:
  11238. case GGML_TYPE_F32:
  11239. case GGML_TYPE_Q4_0:
  11240. case GGML_TYPE_Q8_0:
  11241. // supported in scalar and coopmat2 paths
  11242. break;
  11243. case GGML_TYPE_Q4_1:
  11244. case GGML_TYPE_Q5_0:
  11245. case GGML_TYPE_Q5_1:
  11246. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  11247. //case GGML_TYPE_Q2_K:
  11248. //case GGML_TYPE_Q3_K:
  11249. //case GGML_TYPE_Q4_K:
  11250. //case GGML_TYPE_Q5_K:
  11251. //case GGML_TYPE_Q6_K:
  11252. //case GGML_TYPE_IQ1_S:
  11253. //case GGML_TYPE_IQ1_M:
  11254. //case GGML_TYPE_IQ2_XXS:
  11255. //case GGML_TYPE_IQ2_XS:
  11256. //case GGML_TYPE_IQ2_S:
  11257. //case GGML_TYPE_IQ3_XXS:
  11258. //case GGML_TYPE_IQ3_S:
  11259. //case GGML_TYPE_IQ4_XS:
  11260. case GGML_TYPE_IQ4_NL:
  11261. // currently supported only in coopmat2 path
  11262. if (!coopmat2) {
  11263. return false;
  11264. }
  11265. break;
  11266. default:
  11267. return false;
  11268. }
  11269. if (!coopmat2 && !device->subgroup_shuffle) {
  11270. // scalar FA uses subgroupShuffle
  11271. return false;
  11272. }
  11273. return true;
  11274. }
  11275. case GGML_OP_GET_ROWS:
  11276. {
  11277. switch (op->src[0]->type) {
  11278. case GGML_TYPE_F32:
  11279. case GGML_TYPE_F16:
  11280. case GGML_TYPE_BF16:
  11281. case GGML_TYPE_Q4_0:
  11282. case GGML_TYPE_Q4_1:
  11283. case GGML_TYPE_Q5_0:
  11284. case GGML_TYPE_Q5_1:
  11285. case GGML_TYPE_Q8_0:
  11286. case GGML_TYPE_Q2_K:
  11287. case GGML_TYPE_Q3_K:
  11288. case GGML_TYPE_Q4_K:
  11289. case GGML_TYPE_Q5_K:
  11290. case GGML_TYPE_Q6_K:
  11291. case GGML_TYPE_IQ1_S:
  11292. case GGML_TYPE_IQ1_M:
  11293. case GGML_TYPE_IQ2_XXS:
  11294. case GGML_TYPE_IQ2_XS:
  11295. case GGML_TYPE_IQ2_S:
  11296. case GGML_TYPE_IQ3_XXS:
  11297. case GGML_TYPE_IQ3_S:
  11298. case GGML_TYPE_IQ4_XS:
  11299. case GGML_TYPE_IQ4_NL:
  11300. case GGML_TYPE_MXFP4:
  11301. return true;
  11302. default:
  11303. return false;
  11304. }
  11305. }
  11306. case GGML_OP_SET_ROWS:
  11307. {
  11308. switch (op->type) {
  11309. case GGML_TYPE_F32:
  11310. case GGML_TYPE_F16:
  11311. case GGML_TYPE_BF16:
  11312. case GGML_TYPE_Q4_0:
  11313. case GGML_TYPE_Q4_1:
  11314. case GGML_TYPE_Q5_0:
  11315. case GGML_TYPE_Q5_1:
  11316. case GGML_TYPE_Q8_0:
  11317. case GGML_TYPE_IQ4_NL:
  11318. return true;
  11319. default:
  11320. return false;
  11321. }
  11322. }
  11323. case GGML_OP_CONT:
  11324. case GGML_OP_CPY:
  11325. case GGML_OP_DUP:
  11326. {
  11327. ggml_type src0_type = op->src[0]->type;
  11328. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  11329. if (src0_type == GGML_TYPE_F32) {
  11330. switch (src1_type) {
  11331. case GGML_TYPE_F32:
  11332. case GGML_TYPE_F16:
  11333. case GGML_TYPE_BF16:
  11334. case GGML_TYPE_Q4_0:
  11335. case GGML_TYPE_Q4_1:
  11336. case GGML_TYPE_Q5_0:
  11337. case GGML_TYPE_Q5_1:
  11338. case GGML_TYPE_Q8_0:
  11339. case GGML_TYPE_IQ4_NL:
  11340. return true;
  11341. default:
  11342. break;
  11343. }
  11344. }
  11345. if (src1_type == GGML_TYPE_F32) {
  11346. switch (src0_type) {
  11347. case GGML_TYPE_F16:
  11348. case GGML_TYPE_Q4_0:
  11349. case GGML_TYPE_Q4_1:
  11350. case GGML_TYPE_Q5_0:
  11351. case GGML_TYPE_Q5_1:
  11352. case GGML_TYPE_Q8_0:
  11353. case GGML_TYPE_IQ4_NL:
  11354. return true;
  11355. default:
  11356. break;
  11357. }
  11358. }
  11359. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  11360. return true;
  11361. }
  11362. if (
  11363. (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_I32) ||
  11364. (src0_type == GGML_TYPE_I32 && src1_type == GGML_TYPE_F32)
  11365. ) {
  11366. return true;
  11367. }
  11368. // We can handle copying from a type to the same type if it's
  11369. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  11370. // so the type/block size must be a multiple of 4.
  11371. if (src0_type == src1_type &&
  11372. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  11373. (ggml_type_size(src0_type) % 2) == 0) {
  11374. return true;
  11375. }
  11376. return false;
  11377. }
  11378. case GGML_OP_REPEAT:
  11379. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  11380. case GGML_OP_REPEAT_BACK:
  11381. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  11382. case GGML_OP_ROPE:
  11383. case GGML_OP_ROPE_BACK:
  11384. case GGML_OP_NONE:
  11385. case GGML_OP_RESHAPE:
  11386. case GGML_OP_VIEW:
  11387. case GGML_OP_PERMUTE:
  11388. case GGML_OP_TRANSPOSE:
  11389. case GGML_OP_RMS_NORM:
  11390. return true;
  11391. case GGML_OP_NORM:
  11392. case GGML_OP_GROUP_NORM:
  11393. case GGML_OP_L2_NORM:
  11394. return ggml_is_contiguous(op->src[0]);
  11395. case GGML_OP_ADD:
  11396. case GGML_OP_SUB:
  11397. case GGML_OP_MUL:
  11398. case GGML_OP_DIV:
  11399. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11400. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  11401. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  11402. case GGML_OP_ADD_ID:
  11403. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32 && op->src[2]->type == GGML_TYPE_I32 &&
  11404. op->type == GGML_TYPE_F32;
  11405. case GGML_OP_SILU_BACK:
  11406. case GGML_OP_RMS_NORM_BACK:
  11407. case GGML_OP_SQR:
  11408. case GGML_OP_SQRT:
  11409. case GGML_OP_SIN:
  11410. case GGML_OP_COS:
  11411. case GGML_OP_CLAMP:
  11412. case GGML_OP_LEAKY_RELU:
  11413. case GGML_OP_OPT_STEP_ADAMW:
  11414. case GGML_OP_OPT_STEP_SGD:
  11415. return op->src[0]->type == GGML_TYPE_F32;
  11416. case GGML_OP_ARGSORT:
  11417. return op->ne[0] <= max_argsort_cols;
  11418. case GGML_OP_UPSCALE:
  11419. case GGML_OP_ACC:
  11420. case GGML_OP_CONCAT:
  11421. case GGML_OP_SCALE:
  11422. case GGML_OP_PAD:
  11423. case GGML_OP_ROLL:
  11424. case GGML_OP_DIAG_MASK_INF:
  11425. case GGML_OP_SOFT_MAX:
  11426. case GGML_OP_SOFT_MAX_BACK:
  11427. return true;
  11428. case GGML_OP_SUM:
  11429. case GGML_OP_SUM_ROWS:
  11430. case GGML_OP_MEAN:
  11431. return op->src[0]->type == GGML_TYPE_F32 && ggml_is_contiguous_rows(op->src[0]);
  11432. case GGML_OP_ARGMAX:
  11433. case GGML_OP_COUNT_EQUAL:
  11434. case GGML_OP_IM2COL:
  11435. case GGML_OP_IM2COL_3D:
  11436. case GGML_OP_TIMESTEP_EMBEDDING:
  11437. case GGML_OP_CONV_2D_DW:
  11438. case GGML_OP_POOL_2D:
  11439. case GGML_OP_RWKV_WKV6:
  11440. case GGML_OP_RWKV_WKV7:
  11441. return true;
  11442. case GGML_OP_SSM_SCAN:
  11443. {
  11444. for (int i = 0; i < 6; i++) {
  11445. if (op->src[i] && ggml_is_quantized(op->src[i]->type)) {
  11446. return false;
  11447. }
  11448. }
  11449. if (op->src[6] && op->src[6]->type != GGML_TYPE_I32) {
  11450. return false;
  11451. }
  11452. if (op->src[0]->type != GGML_TYPE_F32 || op->type != GGML_TYPE_F32) {
  11453. return false;
  11454. }
  11455. const uint32_t d_state = op->src[0]->ne[0];
  11456. const uint32_t head_dim = op->src[0]->ne[1];
  11457. bool is_mamba2 = (op->src[3] && op->src[3]->nb[1] == sizeof(float));
  11458. if (!is_mamba2) {
  11459. return false;
  11460. }
  11461. if ((d_state != 128 && d_state != 256) || head_dim % 16 != 0) {
  11462. return false;
  11463. }
  11464. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11465. const vk_device& device = ggml_vk_get_device(ctx->device);
  11466. const uint32_t SPLIT_H = 16;
  11467. size_t stateC_size = SPLIT_H * d_state * sizeof(float);
  11468. if (stateC_size > device->properties.limits.maxComputeSharedMemorySize) {
  11469. return false;
  11470. }
  11471. return true;
  11472. }
  11473. case GGML_OP_SSM_CONV:
  11474. return true;
  11475. case GGML_OP_CONV_TRANSPOSE_1D:
  11476. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  11477. case GGML_OP_CONV_2D:
  11478. case GGML_OP_CONV_TRANSPOSE_2D:
  11479. {
  11480. // Op is disabled for Apple because it segfaults at pipeline create time on MoltenVK
  11481. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11482. const vk_device& device = ggml_vk_get_device(ctx->device);
  11483. if (op->op == GGML_OP_CONV_TRANSPOSE_2D &&
  11484. device->properties.limits.maxPushConstantsSize < sizeof(vk_op_conv_transpose_2d_push_constants)) {
  11485. return false;
  11486. }
  11487. // Channel-contiguous format is not supported yet.
  11488. return ((op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11489. op->src[1]->type == GGML_TYPE_F32 &&
  11490. op->type == GGML_TYPE_F32 &&
  11491. ggml_is_contiguous(op->src[0]) &&
  11492. ggml_is_contiguous(op->src[1]) &&
  11493. ggml_is_contiguous(op));
  11494. }
  11495. default:
  11496. return false;
  11497. }
  11498. UNUSED(dev);
  11499. }
  11500. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  11501. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  11502. return false;
  11503. }
  11504. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11505. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  11506. return buft_ctx->device->idx == ctx->device;
  11507. }
  11508. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  11509. const int min_batch_size = 32;
  11510. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  11511. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  11512. UNUSED(dev);
  11513. }
  11514. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  11515. /* .get_name = */ ggml_backend_vk_device_get_name,
  11516. /* .get_description = */ ggml_backend_vk_device_get_description,
  11517. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  11518. /* .get_type = */ ggml_backend_vk_device_get_type,
  11519. /* .get_props = */ ggml_backend_vk_device_get_props,
  11520. /* .init_backend = */ ggml_backend_vk_device_init,
  11521. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  11522. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  11523. /* .buffer_from_host_ptr = */ NULL,
  11524. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  11525. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  11526. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  11527. /* .event_new = */ NULL,
  11528. /* .event_free = */ NULL,
  11529. /* .event_synchronize = */ NULL,
  11530. };
  11531. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  11532. UNUSED(reg);
  11533. return GGML_VK_NAME;
  11534. }
  11535. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  11536. UNUSED(reg);
  11537. return ggml_backend_vk_get_device_count();
  11538. }
  11539. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  11540. static std::vector<ggml_backend_dev_t> devices;
  11541. static bool initialized = false;
  11542. {
  11543. static std::mutex mutex;
  11544. std::lock_guard<std::mutex> lock(mutex);
  11545. if (!initialized) {
  11546. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  11547. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  11548. char desc[256];
  11549. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  11550. ctx->device = i;
  11551. ctx->name = GGML_VK_NAME + std::to_string(i);
  11552. ctx->description = desc;
  11553. ctx->is_integrated_gpu = ggml_backend_vk_get_device_type(i) == vk::PhysicalDeviceType::eIntegratedGpu;
  11554. ctx->pci_bus_id = ggml_backend_vk_get_device_pci_id(i);
  11555. devices.push_back(new ggml_backend_device {
  11556. /* .iface = */ ggml_backend_vk_device_i,
  11557. /* .reg = */ reg,
  11558. /* .context = */ ctx,
  11559. });
  11560. }
  11561. initialized = true;
  11562. }
  11563. }
  11564. GGML_ASSERT(device < devices.size());
  11565. return devices[device];
  11566. }
  11567. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  11568. /* .get_name = */ ggml_backend_vk_reg_get_name,
  11569. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  11570. /* .get_device = */ ggml_backend_vk_reg_get_device,
  11571. /* .get_proc_address = */ NULL,
  11572. };
  11573. ggml_backend_reg_t ggml_backend_vk_reg() {
  11574. static ggml_backend_reg reg = {
  11575. /* .api_version = */ GGML_BACKEND_API_VERSION,
  11576. /* .iface = */ ggml_backend_vk_reg_i,
  11577. /* .context = */ nullptr,
  11578. };
  11579. try {
  11580. ggml_vk_instance_init();
  11581. return &reg;
  11582. } catch (const vk::SystemError& e) {
  11583. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  11584. return nullptr;
  11585. } catch (const std::exception &e) {
  11586. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: " << e.what());
  11587. return nullptr;
  11588. } catch (...) {
  11589. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: unknown exception during Vulkan init");
  11590. return nullptr;
  11591. }
  11592. }
  11593. // Extension availability
  11594. static bool ggml_vk_instance_validation_ext_available() {
  11595. #ifdef GGML_VULKAN_VALIDATE
  11596. // Check if validation layer provides the extension
  11597. const std::string layer_name = "VK_LAYER_KHRONOS_validation";
  11598. for (const auto& layer : vk::enumerateInstanceLayerProperties()) {
  11599. if (layer_name == layer.layerName.data()) {
  11600. for (const auto& ext : vk::enumerateInstanceExtensionProperties(layer_name)) {
  11601. if (strcmp("VK_EXT_validation_features", ext.extensionName.data()) == 0) {
  11602. return true;
  11603. }
  11604. }
  11605. }
  11606. }
  11607. std::cerr << "ggml_vulkan: WARNING: Validation layer or layer extension VK_EXT_validation_features not found." << std::endl;
  11608. #endif
  11609. return false;
  11610. }
  11611. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  11612. #ifdef __APPLE__
  11613. // Check for portability enumeration extension for MoltenVK support
  11614. for (const auto& properties : instance_extensions) {
  11615. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  11616. return true;
  11617. }
  11618. }
  11619. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  11620. #endif
  11621. return false;
  11622. UNUSED(instance_extensions);
  11623. }
  11624. // Extension availability
  11625. static bool ggml_vk_instance_debug_utils_ext_available(
  11626. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  11627. // Check for portability enumeration extension for MoltenVK support
  11628. for (const auto & properties : instance_extensions) {
  11629. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  11630. return true;
  11631. }
  11632. }
  11633. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  11634. return false;
  11635. UNUSED(instance_extensions);
  11636. }
  11637. static bool ggml_vk_device_is_supported(const vk::PhysicalDevice & vkdev) {
  11638. VkPhysicalDeviceFeatures2 device_features2;
  11639. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  11640. VkPhysicalDeviceVulkan11Features vk11_features;
  11641. vk11_features.pNext = nullptr;
  11642. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  11643. device_features2.pNext = &vk11_features;
  11644. vkGetPhysicalDeviceFeatures2(vkdev, &device_features2);
  11645. return vk11_features.storageBuffer16BitAccess;
  11646. }
  11647. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  11648. switch (props.vendorID) {
  11649. case VK_VENDOR_ID_INTEL:
  11650. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  11651. // while some older hardware (ex. Arc A770) has performance regressions
  11652. return arch == vk_device_architecture::INTEL_XE2;
  11653. case VK_VENDOR_ID_AMD:
  11654. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  11655. // Workaround for AMD proprietary driver reporting support on all GPUs
  11656. return arch == vk_device_architecture::AMD_RDNA3;
  11657. }
  11658. return true;
  11659. default:
  11660. return true;
  11661. }
  11662. }
  11663. // checks
  11664. #ifdef GGML_VULKAN_CHECK_RESULTS
  11665. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  11666. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  11667. return;
  11668. }
  11669. for (int j = 0; j < level; j++) {
  11670. std::cerr << " ";
  11671. }
  11672. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  11673. done.push_back(tensor);
  11674. for (int i = 0; i < GGML_MAX_SRC; i++) {
  11675. if (tensor->src[i] != nullptr) {
  11676. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  11677. }
  11678. }
  11679. }
  11680. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  11681. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  11682. return;
  11683. }
  11684. i0 = std::max(i0, 5);
  11685. i1 = std::max(i1, 5);
  11686. i2 = std::max(i2, 0);
  11687. i3 = std::max(i3, 0);
  11688. fprintf(stderr, " ");
  11689. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  11690. fprintf(stderr, "%7d ", idx1);
  11691. }
  11692. fprintf(stderr, "\n");
  11693. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  11694. fprintf(stderr, "%7d: ", idx0);
  11695. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  11696. 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]) {
  11697. float val;
  11698. if (tensor->type == GGML_TYPE_F32) {
  11699. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  11700. } else if (tensor->type == GGML_TYPE_F16) {
  11701. 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]));
  11702. } else if (tensor->type == GGML_TYPE_I32) {
  11703. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  11704. } else {
  11705. GGML_ABORT("fatal error");
  11706. }
  11707. fprintf(stderr, "% 7.2f ", val);
  11708. } else {
  11709. fprintf(stderr, " ");
  11710. }
  11711. }
  11712. fprintf(stderr, "\n");
  11713. }
  11714. }
  11715. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  11716. void * tensor_data = tensor->data;
  11717. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  11718. if (is_gpu) {
  11719. const size_t tensor_size = ggml_nbytes(tensor);
  11720. tensor_data = malloc(tensor_size);
  11721. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  11722. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  11723. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  11724. }
  11725. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  11726. 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;
  11727. if (tensor->src[0] != nullptr) {
  11728. 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;
  11729. }
  11730. if (tensor->src[1] != nullptr) {
  11731. 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;
  11732. }
  11733. std::cerr << std::endl << "Result:" << std::endl;
  11734. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  11735. std::cerr << std::endl;
  11736. std::vector<const ggml_tensor *> done;
  11737. ggml_vk_print_graph_origin(tensor, done);
  11738. if (is_gpu) {
  11739. free(tensor_data);
  11740. }
  11741. }
  11742. void * comp_result;
  11743. size_t comp_size;
  11744. size_t comp_nb[GGML_MAX_DIMS];
  11745. size_t check_counter = 0;
  11746. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  11747. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  11748. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  11749. return;
  11750. }
  11751. bool fused_rms_norm_mul = false;
  11752. int rms_norm_idx = -1;
  11753. if (ctx->num_additional_fused_ops == 1 &&
  11754. tensor->op == GGML_OP_RMS_NORM &&
  11755. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  11756. fused_rms_norm_mul = true;
  11757. tensor = cgraph->nodes[tensor_idx + 1];
  11758. }
  11759. check_counter++;
  11760. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  11761. return;
  11762. }
  11763. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  11764. ggml_tensor * src0 = tensor->src[0];
  11765. ggml_tensor * src1 = tensor->src[1];
  11766. struct ggml_init_params iparams = {
  11767. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  11768. /*.mem_buffer =*/ NULL,
  11769. /*.no_alloc =*/ false,
  11770. };
  11771. struct ggml_context * ggml_ctx = ggml_init(iparams);
  11772. std::array<struct ggml_tensor *, GGML_MAX_SRC> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  11773. std::array<size_t, GGML_MAX_SRC> src_size = {};
  11774. std::array<void *, GGML_MAX_SRC> src_buffer = {};
  11775. const char * srci_name[GGML_MAX_SRC] = {"src0", "src1", "src2", "src3", "src4", "src5", "src6", "src7", "src8", "src9"};
  11776. struct ggml_tensor * tensor_clone = nullptr;
  11777. for (int i = 0; i < GGML_MAX_SRC; i++) {
  11778. ggml_tensor * srci = tensor->src[i];
  11779. if (fused_rms_norm_mul) {
  11780. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  11781. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  11782. switch (i) {
  11783. case 0: srci = rms_norm->src[0]; break;
  11784. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  11785. default: continue;
  11786. }
  11787. }
  11788. if (srci == nullptr) {
  11789. continue;
  11790. }
  11791. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  11792. size_t srci_size = ggml_nbytes(srci);
  11793. src_clone[i] = srci_clone;
  11794. src_size[i] = ggml_nbytes(srci);
  11795. src_buffer[i] = malloc(srci_size);
  11796. srci_clone->data = src_buffer[i];
  11797. if (ggml_backend_buffer_is_host(srci->buffer)) {
  11798. memcpy(srci_clone->data, srci->data, srci_size);
  11799. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  11800. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  11801. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  11802. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  11803. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  11804. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  11805. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  11806. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  11807. const int idx = i3*srci->ne[2] + i2;
  11808. 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]);
  11809. }
  11810. }
  11811. srci_clone->nb[0] = srci->nb[0];
  11812. srci_clone->nb[1] = srci->nb[1];
  11813. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  11814. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  11815. }
  11816. } else {
  11817. if (offset + srci_size >= buffer_gpu->size) {
  11818. srci_size = buffer_gpu->size - offset;
  11819. }
  11820. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  11821. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  11822. }
  11823. } else {
  11824. GGML_ABORT("fatal error");
  11825. }
  11826. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  11827. ggml_vk_print_tensor(srci, srci_name[i]);
  11828. }
  11829. }
  11830. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  11831. const float * params = (const float *)tensor->op_params;
  11832. 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]);
  11833. if (src_clone[4]) {
  11834. ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]);
  11835. }
  11836. } else if (tensor->op == GGML_OP_MUL_MAT) {
  11837. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  11838. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  11839. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  11840. } else if (tensor->op == GGML_OP_SUB) {
  11841. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  11842. } else if (tensor->op == GGML_OP_MUL) {
  11843. if (fused_rms_norm_mul) {
  11844. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  11845. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  11846. } else {
  11847. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  11848. }
  11849. } else if (tensor->op == GGML_OP_DIV) {
  11850. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  11851. } else if (tensor->op == GGML_OP_CONCAT) {
  11852. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  11853. } else if (tensor->op == GGML_OP_UPSCALE) {
  11854. tensor_clone = ggml_interpolate(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  11855. } else if (tensor->op == GGML_OP_SCALE) {
  11856. const float * params = (const float *)tensor->op_params;
  11857. tensor_clone = ggml_scale_bias(ggml_ctx, src_clone[0], params[0], params[1]);
  11858. } else if (tensor->op == GGML_OP_SQR) {
  11859. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  11860. } else if (tensor->op == GGML_OP_SQRT) {
  11861. tensor_clone = ggml_sqrt(ggml_ctx, src_clone[0]);
  11862. } else if (tensor->op == GGML_OP_SIN) {
  11863. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  11864. } else if (tensor->op == GGML_OP_COS) {
  11865. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  11866. } else if (tensor->op == GGML_OP_CLAMP) {
  11867. const float * params = (const float *)tensor->op_params;
  11868. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  11869. } else if (tensor->op == GGML_OP_PAD) {
  11870. tensor_clone = ggml_pad_ext(ggml_ctx, src_clone[0], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3],
  11871. tensor->op_params[4], tensor->op_params[5], tensor->op_params[6], tensor->op_params[7]);
  11872. } else if (tensor->op == GGML_OP_REPEAT) {
  11873. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  11874. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  11875. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  11876. } else if (tensor->op == GGML_OP_ADD) {
  11877. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  11878. } else if (tensor->op == GGML_OP_ACC) {
  11879. 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]);
  11880. } else if (tensor->op == GGML_OP_NORM) {
  11881. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  11882. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  11883. const float * float_params = (const float *)tensor->op_params;
  11884. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  11885. } else if (tensor->op == GGML_OP_RMS_NORM) {
  11886. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  11887. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  11888. const float eps = ((float *) tensor->op_params)[0];
  11889. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  11890. } else if (tensor->op == GGML_OP_SILU_BACK) {
  11891. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  11892. } else if (tensor->op == GGML_OP_L2_NORM) {
  11893. const float eps = ((float *) tensor->op_params)[0];
  11894. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  11895. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  11896. if (src1 != nullptr) {
  11897. const float * params = (const float *)tensor->op_params;
  11898. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  11899. } else {
  11900. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  11901. }
  11902. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  11903. 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]);
  11904. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  11905. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  11906. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  11907. const int n_dims = ((int32_t *) tensor->op_params)[1];
  11908. const int mode = ((int32_t *) tensor->op_params)[2];
  11909. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  11910. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  11911. const float freq_base = ((float *) tensor->op_params)[5];
  11912. const float freq_scale = ((float *) tensor->op_params)[6];
  11913. const float ext_factor = ((float *) tensor->op_params)[7];
  11914. const float attn_factor = ((float *) tensor->op_params)[8];
  11915. const float beta_fast = ((float *) tensor->op_params)[9];
  11916. const float beta_slow = ((float *) tensor->op_params)[10];
  11917. if (mode & GGML_ROPE_TYPE_MROPE) {
  11918. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  11919. if (tensor->op == GGML_OP_ROPE) {
  11920. 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);
  11921. } else {
  11922. 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);
  11923. }
  11924. } else {
  11925. if (tensor->op == GGML_OP_ROPE) {
  11926. 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);
  11927. } else {
  11928. 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);
  11929. }
  11930. }
  11931. } else if (tensor->op == GGML_OP_UNARY) {
  11932. switch (ggml_get_unary_op(tensor)) {
  11933. case GGML_UNARY_OP_EXP:
  11934. tensor_clone = ggml_exp(ggml_ctx, src_clone[0]);
  11935. break;
  11936. case GGML_UNARY_OP_SILU:
  11937. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  11938. break;
  11939. case GGML_UNARY_OP_GELU:
  11940. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  11941. break;
  11942. case GGML_UNARY_OP_GELU_ERF:
  11943. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  11944. break;
  11945. case GGML_UNARY_OP_GELU_QUICK:
  11946. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  11947. break;
  11948. case GGML_UNARY_OP_RELU:
  11949. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  11950. break;
  11951. case GGML_UNARY_OP_TANH:
  11952. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  11953. break;
  11954. case GGML_UNARY_OP_SIGMOID:
  11955. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  11956. break;
  11957. case GGML_UNARY_OP_HARDSIGMOID:
  11958. tensor_clone = ggml_hardsigmoid(ggml_ctx, src_clone[0]);
  11959. break;
  11960. case GGML_UNARY_OP_HARDSWISH:
  11961. tensor_clone = ggml_hardswish(ggml_ctx, src_clone[0]);
  11962. break;
  11963. default:
  11964. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  11965. GGML_ABORT("fatal error");
  11966. }
  11967. } else if (tensor->op == GGML_OP_GLU) {
  11968. if (src_clone[1] == nullptr) {
  11969. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  11970. } else {
  11971. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  11972. }
  11973. ggml_set_op_params_i32(tensor_clone, 2, ggml_get_op_params_i32(tensor, 2));
  11974. ggml_set_op_params_i32(tensor_clone, 3, ggml_get_op_params_i32(tensor, 3));
  11975. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  11976. if (src1 == nullptr) {
  11977. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  11978. tensor_clone->type = tensor->type;
  11979. } else {
  11980. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  11981. }
  11982. } else if (tensor->op == GGML_OP_CONT) {
  11983. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  11984. } else if (tensor->op == GGML_OP_RESHAPE) {
  11985. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  11986. } else if (tensor->op == GGML_OP_VIEW) {
  11987. 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]);
  11988. } else if (tensor->op == GGML_OP_PERMUTE) {
  11989. int32_t * params = (int32_t *)tensor->op_params;
  11990. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  11991. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  11992. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  11993. } else if (tensor->op == GGML_OP_GET_ROWS) {
  11994. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  11995. } else if (tensor->op == GGML_OP_ARGSORT) {
  11996. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  11997. } else if (tensor->op == GGML_OP_SUM) {
  11998. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  11999. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  12000. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  12001. } else if (tensor->op == GGML_OP_MEAN) {
  12002. tensor_clone = ggml_mean(ggml_ctx, src_clone[0]);
  12003. } else if (tensor->op == GGML_OP_ARGMAX) {
  12004. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  12005. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  12006. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  12007. } else if (tensor->op == GGML_OP_IM2COL) {
  12008. const int32_t s0 = tensor->op_params[0];
  12009. const int32_t s1 = tensor->op_params[1];
  12010. const int32_t p0 = tensor->op_params[2];
  12011. const int32_t p1 = tensor->op_params[3];
  12012. const int32_t d0 = tensor->op_params[4];
  12013. const int32_t d1 = tensor->op_params[5];
  12014. const bool is_2D = tensor->op_params[6] == 1;
  12015. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  12016. } else if (tensor->op == GGML_OP_IM2COL_3D) {
  12017. const int32_t s0 = tensor->op_params[0];
  12018. const int32_t s1 = tensor->op_params[1];
  12019. const int32_t s2 = tensor->op_params[2];
  12020. const int32_t p0 = tensor->op_params[3];
  12021. const int32_t p1 = tensor->op_params[4];
  12022. const int32_t p2 = tensor->op_params[5];
  12023. const int32_t d0 = tensor->op_params[6];
  12024. const int32_t d1 = tensor->op_params[7];
  12025. const int32_t d2 = tensor->op_params[8];
  12026. const int32_t IC = tensor->op_params[9];
  12027. tensor_clone = ggml_im2col_3d(ggml_ctx, src_clone[0], src_clone[1], IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, tensor->type);
  12028. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  12029. const int32_t dim = tensor->op_params[0];
  12030. const int32_t max_period = tensor->op_params[1];
  12031. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  12032. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  12033. const int32_t s0 = tensor->op_params[0];
  12034. const int32_t p0 = tensor->op_params[1];
  12035. const int32_t d0 = tensor->op_params[2];
  12036. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  12037. } else if (tensor->op == GGML_OP_POOL_2D) {
  12038. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  12039. const int32_t k0 = tensor->op_params[1];
  12040. const int32_t k1 = tensor->op_params[2];
  12041. const int32_t s0 = tensor->op_params[3];
  12042. const int32_t s1 = tensor->op_params[4];
  12043. const int32_t p0 = tensor->op_params[5];
  12044. const int32_t p1 = tensor->op_params[6];
  12045. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  12046. } else if (tensor->op == GGML_OP_CONV_2D) {
  12047. const int32_t s0 = tensor->op_params[0];
  12048. const int32_t s1 = tensor->op_params[1];
  12049. const int32_t p0 = tensor->op_params[2];
  12050. const int32_t p1 = tensor->op_params[3];
  12051. const int32_t d0 = tensor->op_params[4];
  12052. const int32_t d1 = tensor->op_params[5];
  12053. tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1);
  12054. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_2D) {
  12055. const int32_t s = tensor->op_params[0];
  12056. tensor_clone = ggml_conv_transpose_2d_p0(ggml_ctx, src_clone[0], src_clone[1], s);
  12057. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  12058. const float * op_params = (const float *)tensor->op_params;
  12059. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  12060. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  12061. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  12062. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  12063. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  12064. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  12065. src_clone[4], src_clone[5], src_clone[6]);
  12066. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  12067. src_clone[0]->flags = src0->flags;
  12068. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  12069. src_clone[2], src_clone[3], src_clone[4]);
  12070. } else if (tensor->op == GGML_OP_OPT_STEP_SGD) {
  12071. src_clone[0]->flags = src0->flags;
  12072. tensor_clone = ggml_opt_step_sgd(ggml_ctx, src_clone[0], src_clone[1],
  12073. src_clone[2]);
  12074. } else if (tensor->op == GGML_OP_ADD_ID) {
  12075. tensor_clone = ggml_add_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  12076. } else if (tensor->op == GGML_OP_SSM_SCAN) {
  12077. tensor_clone = ggml_ssm_scan(ggml_ctx, src_clone[0], src_clone[1], src_clone[2],
  12078. src_clone[3], src_clone[4], src_clone[5], src_clone[6]);
  12079. } else if (tensor->op == GGML_OP_SSM_CONV) {
  12080. tensor_clone = ggml_ssm_conv(ggml_ctx, src_clone[0], src_clone[1]);
  12081. }
  12082. else {
  12083. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  12084. GGML_ABORT("fatal error");
  12085. }
  12086. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  12087. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  12088. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  12089. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  12090. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  12091. }
  12092. comp_size = ggml_nbytes(tensor_clone);
  12093. comp_result = malloc(comp_size);
  12094. memcpy(comp_result, tensor_clone->data, comp_size);
  12095. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  12096. for (int i = 0; i < GGML_MAX_SRC; i++) {
  12097. if (src_buffer[i] != nullptr) {
  12098. free(src_buffer[i]);
  12099. }
  12100. }
  12101. ggml_free(ggml_ctx);
  12102. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  12103. }
  12104. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  12105. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  12106. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  12107. return;
  12108. }
  12109. if (ctx->num_additional_fused_ops == 1 &&
  12110. tensor->op == GGML_OP_RMS_NORM &&
  12111. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  12112. tensor = cgraph->nodes[tensor_idx + 1];
  12113. }
  12114. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  12115. return;
  12116. }
  12117. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  12118. ggml_tensor * src0 = tensor->src[0];
  12119. ggml_tensor * src1 = tensor->src[1];
  12120. ggml_tensor * src2 = tensor->src[2];
  12121. ggml_tensor * src3 = tensor->src[3];
  12122. void * tensor_data = tensor->data;
  12123. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  12124. size_t tensor_size = ggml_nbytes(tensor);
  12125. tensor_data = malloc(tensor_size);
  12126. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  12127. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  12128. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  12129. if (offset + tensor_size >= buffer_gpu->size) {
  12130. tensor_size = buffer_gpu->size - offset;
  12131. }
  12132. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  12133. }
  12134. float first_error_result = -1.0f;
  12135. float first_error_correct = -1.0f;
  12136. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  12137. double avg_err = 0.0;
  12138. size_t counter = 0;
  12139. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  12140. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  12141. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  12142. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  12143. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  12144. float correct = 0.0f;
  12145. float result = 0.0f;
  12146. if (buffer_size_fit) {
  12147. if (tensor->type == GGML_TYPE_F32) {
  12148. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  12149. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  12150. } else if (tensor->type == GGML_TYPE_F16) {
  12151. 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]));
  12152. 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]));
  12153. } else if (tensor->type == GGML_TYPE_BF16) {
  12154. 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]));
  12155. 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]));
  12156. } else if (tensor->type == GGML_TYPE_I32) {
  12157. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  12158. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  12159. } else if (tensor->type == GGML_TYPE_I64) {
  12160. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  12161. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  12162. } else {
  12163. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  12164. }
  12165. } else {
  12166. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  12167. GGML_ABORT("fatal error");
  12168. }
  12169. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  12170. 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;
  12171. 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;
  12172. if (src0 != nullptr) {
  12173. 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;
  12174. }
  12175. if (src1 != nullptr) {
  12176. 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;
  12177. }
  12178. if (src2 != nullptr) {
  12179. 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;
  12180. }
  12181. if (src3 != nullptr) {
  12182. 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;
  12183. }
  12184. 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;
  12185. std::cerr << std::endl << "Result:" << std::endl;
  12186. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  12187. std::cerr << std::endl << "Correct:" << std::endl;
  12188. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  12189. std::cerr << std::endl;
  12190. std::vector<const ggml_tensor *> done;
  12191. ggml_vk_print_graph_origin(tensor, done);
  12192. GGML_ABORT("fatal error");
  12193. }
  12194. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  12195. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  12196. first_error[0] = i0;
  12197. first_error[1] = i1;
  12198. first_error[2] = i2;
  12199. first_error[3] = i3;
  12200. first_error_result = result;
  12201. first_error_correct = correct;
  12202. }
  12203. // Special case, value is infinite, avoid NaN result in avg_err
  12204. // NaN also appears in results, if both are nan error is 0
  12205. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  12206. avg_err += std::fabs(correct - result) / denom;
  12207. }
  12208. counter++;
  12209. }
  12210. }
  12211. }
  12212. }
  12213. avg_err /= counter;
  12214. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  12215. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  12216. 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;
  12217. if (src0 != nullptr) {
  12218. 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;
  12219. }
  12220. if (src1 != nullptr) {
  12221. 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;
  12222. }
  12223. if (src2 != nullptr) {
  12224. 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;
  12225. }
  12226. if (src3 != nullptr) {
  12227. 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;
  12228. }
  12229. 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;
  12230. std::cerr << std::endl << "Result:" << std::endl;
  12231. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  12232. std::cerr << std::endl << "Correct:" << std::endl;
  12233. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  12234. std::cerr << std::endl;
  12235. std::vector<const ggml_tensor *> done;
  12236. ggml_vk_print_graph_origin(tensor, done);
  12237. }
  12238. if (avg_err > 0.5 || std::isnan(avg_err)) {
  12239. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  12240. 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;
  12241. if (src0 != nullptr) {
  12242. 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;
  12243. }
  12244. if (src1 != nullptr) {
  12245. 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;
  12246. }
  12247. if (src2 != nullptr) {
  12248. 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;
  12249. }
  12250. if (src3 != nullptr) {
  12251. 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;
  12252. }
  12253. 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;
  12254. std::cerr << std::endl << "Result:" << std::endl;
  12255. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  12256. std::cerr << std::endl << "Correct:" << std::endl;
  12257. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  12258. std::cerr << std::endl;
  12259. std::vector<const ggml_tensor *> done;
  12260. ggml_vk_print_graph_origin(tensor, done);
  12261. GGML_ABORT("fatal error");
  12262. } else {
  12263. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  12264. }
  12265. free(comp_result);
  12266. comp_result = nullptr;
  12267. comp_size = 0;
  12268. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  12269. free(tensor_data);
  12270. }
  12271. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  12272. }
  12273. #endif
  12274. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)