ggml-vulkan.cpp 568 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #if defined(_MSC_VER)
  25. # define NOMINMAX 1
  26. # include <windows.h>
  27. # define YIELD() YieldProcessor()
  28. #elif defined(__clang__) || defined(__GNUC__)
  29. # if defined(__x86_64__) ||defined(__i386__)
  30. # include <immintrin.h>
  31. # define YIELD() _mm_pause()
  32. # elif defined(__arm__) || defined(__aarch64__)
  33. # if defined(__clang__)
  34. # include <arm_acle.h>
  35. # define YIELD() __yield()
  36. # else
  37. # define YIELD() asm volatile("yield")
  38. # endif
  39. # endif
  40. #endif
  41. #if !defined(YIELD)
  42. #define YIELD()
  43. #endif
  44. #include "ggml-impl.h"
  45. #include "ggml-backend-impl.h"
  46. #include "ggml-vulkan-shaders.hpp"
  47. // remove this once it's more widely available in the SDK
  48. #if !defined(VK_KHR_shader_bfloat16)
  49. #define VK_KHR_shader_bfloat16 1
  50. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  51. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  52. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  53. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  54. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  55. VkStructureType sType;
  56. void* pNext;
  57. VkBool32 shaderBFloat16Type;
  58. VkBool32 shaderBFloat16DotProduct;
  59. VkBool32 shaderBFloat16CooperativeMatrix;
  60. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  61. #endif
  62. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  63. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  64. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  65. #define VK_VENDOR_ID_AMD 0x1002
  66. #define VK_VENDOR_ID_APPLE 0x106b
  67. #define VK_VENDOR_ID_INTEL 0x8086
  68. #define VK_VENDOR_ID_NVIDIA 0x10de
  69. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  70. #define GGML_VK_MAX_NODES 8192
  71. #define MAX_VK_BUFFERS 256
  72. #define VK_CHECK(err, msg) \
  73. do { \
  74. vk::Result err_ = (err); \
  75. if (err_ != vk::Result::eSuccess) { \
  76. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  77. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  78. exit(1); \
  79. } \
  80. } while (0)
  81. #ifdef GGML_VULKAN_DEBUG
  82. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  83. #else
  84. #define VK_LOG_DEBUG(msg) ((void) 0)
  85. #endif // GGML_VULKAN_DEBUG
  86. struct ggml_backend_vk_context;
  87. #define MAX_PARAMETER_COUNT 8
  88. struct vk_pipeline_struct {
  89. std::string name;
  90. vk::ShaderModule shader_module;
  91. vk::PipelineLayout layout;
  92. vk::Pipeline pipeline;
  93. uint32_t push_constant_size;
  94. uint32_t parameter_count;
  95. std::array<uint32_t, 3> wg_denoms;
  96. uint32_t align;
  97. // set to true to request the pipeline is compiled after the dryrun
  98. bool needed {};
  99. // set to true when the shader has been compiled
  100. bool compiled {};
  101. };
  102. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  103. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  104. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  105. struct vk_matmul_pipeline_struct {
  106. vk_pipeline l, m, s;
  107. vk_pipeline a_l, a_m, a_s;
  108. };
  109. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  110. struct vk_matmul_pipeline2 {
  111. vk_matmul_pipeline2() {
  112. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  113. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  114. }
  115. vk_matmul_pipeline f32acc;
  116. vk_matmul_pipeline f16acc;
  117. };
  118. struct vk_device_struct;
  119. typedef std::shared_ptr<vk_device_struct> vk_device;
  120. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  121. struct vk_buffer_struct;
  122. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  123. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  124. struct ggml_backend_vk_buffer_type_context {
  125. std::string name;
  126. vk_device device;
  127. };
  128. struct vk_queue;
  129. // Stores command pool/buffers. There's an instance of this
  130. // for each (context,queue) pair and for each (device,queue) pair.
  131. struct vk_command_pool {
  132. void init(vk_device& device, vk_queue *q_);
  133. void destroy(vk::Device& device);
  134. vk::CommandPool pool;
  135. uint32_t cmd_buffer_idx;
  136. std::vector<vk::CommandBuffer> cmd_buffers;
  137. vk_queue *q;
  138. };
  139. // Prevent simultaneous submissions to the same queue.
  140. // This could be per vk_queue if we stopped having two vk_queue structures
  141. // sharing the same vk::Queue.
  142. static std::mutex queue_mutex;
  143. struct vk_queue {
  144. uint32_t queue_family_index;
  145. vk::Queue queue;
  146. vk_command_pool cmd_pool;
  147. vk::PipelineStageFlags stage_flags;
  148. bool transfer_only;
  149. // copy everything except the cmd_pool
  150. void copyFrom(vk_queue &other) {
  151. queue_family_index = other.queue_family_index;
  152. queue = other.queue;
  153. stage_flags = other.stage_flags;
  154. transfer_only = other.transfer_only;
  155. }
  156. };
  157. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  158. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  159. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  160. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  161. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  162. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  163. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  164. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  165. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  166. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  167. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  168. /* .is_host = */ NULL,
  169. };
  170. #ifdef GGML_VULKAN_MEMORY_DEBUG
  171. class vk_memory_logger;
  172. #endif
  173. class vk_perf_logger;
  174. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  175. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  176. static constexpr uint32_t p021_max_gqa_ratio = 8;
  177. enum vk_device_architecture {
  178. OTHER,
  179. AMD_GCN,
  180. AMD_RDNA1,
  181. AMD_RDNA2,
  182. AMD_RDNA3,
  183. INTEL_XE2,
  184. };
  185. // HSK x HSV
  186. enum FaHeadSizes {
  187. FA_HEAD_SIZE_64,
  188. FA_HEAD_SIZE_80,
  189. FA_HEAD_SIZE_96,
  190. FA_HEAD_SIZE_112,
  191. FA_HEAD_SIZE_128,
  192. FA_HEAD_SIZE_192,
  193. FA_HEAD_SIZE_192_128,
  194. FA_HEAD_SIZE_256,
  195. FA_HEAD_SIZE_576_512,
  196. FA_HEAD_SIZE_UNSUPPORTED,
  197. FA_HEAD_SIZE_COUNT = FA_HEAD_SIZE_UNSUPPORTED,
  198. };
  199. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  200. vk::PhysicalDeviceProperties props = device.getProperties();
  201. if (props.vendorID == VK_VENDOR_ID_AMD) {
  202. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  203. bool amd_shader_core_properties = false;
  204. bool integer_dot_product = false;
  205. bool subgroup_size_control = false;
  206. for (const auto& properties : ext_props) {
  207. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  208. amd_shader_core_properties = true;
  209. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  210. integer_dot_product = true;
  211. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  212. subgroup_size_control = true;
  213. }
  214. }
  215. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  216. return vk_device_architecture::OTHER;
  217. }
  218. vk::PhysicalDeviceProperties2 props2;
  219. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  220. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  221. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  222. props2.pNext = &shader_core_props_amd;
  223. shader_core_props_amd.pNext = &integer_dot_props;
  224. integer_dot_props.pNext = &subgroup_size_control_props;
  225. device.getProperties2(&props2);
  226. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  227. return vk_device_architecture::AMD_GCN;
  228. }
  229. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  230. // RDNA
  231. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  232. return vk_device_architecture::AMD_RDNA1;
  233. }
  234. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  235. return vk_device_architecture::AMD_RDNA3;
  236. }
  237. return vk_device_architecture::AMD_RDNA2;
  238. }
  239. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  240. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  241. bool subgroup_size_control = false;
  242. for (const auto& properties : ext_props) {
  243. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  244. subgroup_size_control = true;
  245. }
  246. }
  247. if (!subgroup_size_control) {
  248. return vk_device_architecture::OTHER;
  249. }
  250. vk::PhysicalDeviceProperties2 props2;
  251. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  252. props2.pNext = &subgroup_size_control_props;
  253. device.getProperties2(&props2);
  254. if (subgroup_size_control_props.minSubgroupSize == 16) {
  255. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  256. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  257. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  258. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  259. return vk_device_architecture::INTEL_XE2;
  260. }
  261. }
  262. return vk_device_architecture::OTHER;
  263. }
  264. struct vk_device_struct {
  265. std::recursive_mutex mutex;
  266. vk::PhysicalDevice physical_device;
  267. vk::PhysicalDeviceProperties properties;
  268. std::string name;
  269. uint64_t max_memory_allocation_size;
  270. uint64_t suballocation_block_size;
  271. bool fp16;
  272. bool pipeline_robustness;
  273. vk::Device device;
  274. uint32_t vendor_id;
  275. vk::DriverId driver_id;
  276. vk_device_architecture architecture;
  277. vk_queue compute_queue;
  278. vk_queue transfer_queue;
  279. bool single_queue;
  280. uint32_t subgroup_size;
  281. uint32_t shader_core_count;
  282. bool uma;
  283. bool prefer_host_memory;
  284. bool float_controls_rte_fp16;
  285. bool subgroup_add;
  286. bool subgroup_shuffle;
  287. bool integer_dot_product;
  288. bool subgroup_size_control;
  289. uint32_t subgroup_min_size;
  290. uint32_t subgroup_max_size;
  291. bool subgroup_require_full_support;
  292. bool coopmat_support;
  293. bool coopmat_acc_f32_support {};
  294. bool coopmat_acc_f16_support {};
  295. bool coopmat_bf16_support {};
  296. bool coopmat_support_16x16x16_f16acc {};
  297. bool coopmat_support_16x16x16_f32acc {};
  298. bool coopmat1_fa_support {};
  299. uint32_t coopmat_m;
  300. uint32_t coopmat_n;
  301. uint32_t coopmat_k;
  302. bool coopmat_int_support;
  303. uint32_t coopmat_int_m;
  304. uint32_t coopmat_int_n;
  305. uint32_t coopmat_int_k;
  306. bool coopmat2;
  307. size_t idx;
  308. bool mul_mat_l[GGML_TYPE_COUNT];
  309. bool mul_mat_m[GGML_TYPE_COUNT];
  310. bool mul_mat_s[GGML_TYPE_COUNT];
  311. bool mul_mat_id_l[GGML_TYPE_COUNT];
  312. bool mul_mat_id_m[GGML_TYPE_COUNT];
  313. bool mul_mat_id_s[GGML_TYPE_COUNT];
  314. // set to true to indicate that some shaders need to be compiled after the dryrun
  315. bool need_compiles {};
  316. vk::DescriptorSetLayout dsl;
  317. vk_matmul_pipeline pipeline_matmul_f32 {};
  318. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  319. vk_matmul_pipeline pipeline_matmul_bf16 {};
  320. vk_matmul_pipeline2 pipeline_matmul_f16;
  321. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  322. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  323. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  324. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  325. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  326. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  327. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  328. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  329. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  330. vk_pipeline pipeline_matmul_split_k_reduce;
  331. vk_pipeline pipeline_quantize_q8_1;
  332. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  333. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  334. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  335. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  336. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  337. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  338. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  339. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  340. vk_pipeline pipeline_acc_f32;
  341. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  342. vk_pipeline pipeline_add[2][2][2];
  343. vk_pipeline pipeline_add_norepeat[2][2][2];
  344. vk_pipeline pipeline_sub[2][2][2];
  345. vk_pipeline pipeline_sub_norepeat[2][2][2];
  346. vk_pipeline pipeline_mul[2][2][2];
  347. vk_pipeline pipeline_mul_norepeat[2][2][2];
  348. vk_pipeline pipeline_div[2][2][2];
  349. vk_pipeline pipeline_div_norepeat[2][2][2];
  350. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  351. vk_pipeline pipeline_upscale_f32;
  352. vk_pipeline pipeline_scale_f32;
  353. vk_pipeline pipeline_sqr_f32;
  354. vk_pipeline pipeline_sin_f32;
  355. vk_pipeline pipeline_cos_f32;
  356. vk_pipeline pipeline_clamp_f32;
  357. vk_pipeline pipeline_pad_f32;
  358. vk_pipeline pipeline_roll_f32;
  359. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  360. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  361. 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;
  362. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  363. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  364. vk_pipeline pipeline_set_rows[GGML_TYPE_COUNT];
  365. vk_pipeline pipeline_norm_f32;
  366. vk_pipeline pipeline_group_norm_f32;
  367. vk_pipeline pipeline_rms_norm_f32;
  368. vk_pipeline pipeline_rms_norm_mul_f32;
  369. vk_pipeline pipeline_rms_norm_back_f32;
  370. vk_pipeline pipeline_l2_norm_f32;
  371. // [src/dst 0=fp32,1=fp16]
  372. vk_pipeline pipeline_gelu[2];
  373. vk_pipeline pipeline_gelu_erf[2];
  374. vk_pipeline pipeline_gelu_quick[2];
  375. vk_pipeline pipeline_silu[2];
  376. vk_pipeline pipeline_relu[2];
  377. vk_pipeline pipeline_tanh[2];
  378. vk_pipeline pipeline_sigmoid[2];
  379. vk_pipeline pipeline_geglu[2];
  380. vk_pipeline pipeline_reglu[2];
  381. vk_pipeline pipeline_swiglu[2];
  382. vk_pipeline pipeline_geglu_erf[2];
  383. vk_pipeline pipeline_geglu_quick[2];
  384. vk_pipeline pipeline_leaky_relu_f32;
  385. vk_pipeline pipeline_silu_back_f32;
  386. vk_pipeline pipeline_diag_mask_inf_f32;
  387. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  388. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  389. vk_pipeline pipeline_soft_max_back_f32;
  390. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  391. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  392. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  393. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  394. vk_pipeline pipeline_argsort_f32;
  395. vk_pipeline pipeline_sum_rows_f32;
  396. vk_pipeline pipeline_argmax_f32;
  397. vk_pipeline pipeline_count_equal_i32;
  398. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  399. vk_pipeline pipeline_timestep_embedding_f32;
  400. vk_pipeline pipeline_conv_transpose_1d_f32;
  401. vk_pipeline pipeline_pool2d_f32;
  402. vk_pipeline pipeline_rwkv_wkv6_f32;
  403. vk_pipeline pipeline_rwkv_wkv7_f32;
  404. vk_pipeline pipeline_opt_step_adamw_f32;
  405. vk_pipeline pipeline_conv2d_dw_whcn_f32;
  406. vk_pipeline pipeline_conv2d_dw_cwhn_f32;
  407. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  408. vk_pipeline pipeline_flash_attn_f32_f16_cm2[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  409. vk_pipeline pipeline_flash_attn_f32_f16_cm1[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  410. vk_pipeline pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  411. vk_pipeline pipeline_flash_attn_split_k_reduce;
  412. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  413. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  414. vk::Fence fence;
  415. vk_buffer sync_staging;
  416. ggml_backend_buffer_type buffer_type;
  417. bool disable_fusion;
  418. #ifdef GGML_VULKAN_MEMORY_DEBUG
  419. std::unique_ptr<vk_memory_logger> memory_logger;
  420. #endif
  421. // for GGML_VK_PERF_LOGGER
  422. std::unique_ptr<vk_perf_logger> perf_logger;
  423. vk::QueryPool query_pool;
  424. int32_t num_queries;
  425. ~vk_device_struct() {
  426. VK_LOG_DEBUG("destroy device " << name);
  427. device.destroyFence(fence);
  428. ggml_vk_destroy_buffer(sync_staging);
  429. compute_queue.cmd_pool.destroy(device);
  430. transfer_queue.cmd_pool.destroy(device);
  431. for (auto& pipeline : pipelines) {
  432. if (pipeline.second.expired()) {
  433. continue;
  434. }
  435. vk_pipeline pl = pipeline.second.lock();
  436. ggml_vk_destroy_pipeline(device, pl);
  437. }
  438. pipelines.clear();
  439. device.destroyDescriptorSetLayout(dsl);
  440. device.destroy();
  441. }
  442. };
  443. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  444. cmd_buffer_idx = 0;
  445. q = q_;
  446. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  447. pool = device->device.createCommandPool(command_pool_create_info);
  448. }
  449. void vk_command_pool::destroy(vk::Device& device) {
  450. device.destroyCommandPool(pool);
  451. pool = nullptr;
  452. cmd_buffers.clear();
  453. }
  454. struct vk_buffer_struct {
  455. vk::Buffer buffer = VK_NULL_HANDLE;
  456. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  457. vk::MemoryPropertyFlags memory_property_flags;
  458. void * ptr;
  459. size_t size = 0;
  460. vk_device device;
  461. ~vk_buffer_struct() {
  462. if (size == 0) {
  463. return;
  464. }
  465. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  466. device->device.freeMemory(device_memory);
  467. device->device.destroyBuffer(buffer);
  468. }
  469. };
  470. struct vk_subbuffer {
  471. vk_buffer buffer;
  472. uint64_t offset;
  473. uint64_t size;
  474. operator vk::DescriptorBufferInfo() const {
  475. return { buffer->buffer, offset, size };
  476. }
  477. };
  478. struct vk_semaphore {
  479. vk::Semaphore s;
  480. uint64_t value;
  481. };
  482. struct vk_submission {
  483. vk::CommandBuffer buffer;
  484. std::vector<vk_semaphore> wait_semaphores;
  485. std::vector<vk_semaphore> signal_semaphores;
  486. };
  487. typedef std::vector<vk_submission> vk_sequence;
  488. struct vk_mat_mat_push_constants {
  489. uint32_t M; uint32_t N; uint32_t K;
  490. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  491. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  492. uint32_t k_split;
  493. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  494. uint32_t padded_N;
  495. };
  496. struct vk_mat_vec_push_constants {
  497. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  498. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  499. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  500. };
  501. struct vk_mat_mat_id_push_constants {
  502. uint32_t M; uint32_t N; uint32_t K;
  503. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  504. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  505. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  506. uint32_t padded_N;
  507. };
  508. struct vk_mat_vec_id_push_constants {
  509. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  510. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  511. uint32_t nei0; uint32_t ne11;
  512. };
  513. struct vk_flash_attn_push_constants {
  514. uint32_t N;
  515. uint32_t KV;
  516. uint32_t ne1;
  517. uint32_t ne2;
  518. uint32_t ne3;
  519. uint32_t neq2;
  520. uint32_t neq3;
  521. uint32_t nek2;
  522. uint32_t nek3;
  523. uint32_t nev2;
  524. uint32_t nev3;
  525. uint32_t nem1;
  526. uint32_t nem2;
  527. uint32_t nem3;
  528. uint32_t nb01;
  529. uint32_t nb02;
  530. uint32_t nb03;
  531. uint32_t nb11;
  532. uint32_t nb12;
  533. uint32_t nb13;
  534. uint32_t nb21;
  535. uint32_t nb22;
  536. uint32_t nb23;
  537. float scale;
  538. float max_bias;
  539. float logit_softcap;
  540. uint32_t mask_n_head_log2;
  541. float m0;
  542. float m1;
  543. uint32_t gqa_ratio;
  544. uint32_t split_kv;
  545. uint32_t k_num;
  546. };
  547. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  548. struct vk_op_push_constants {
  549. uint32_t KX;
  550. uint32_t KY;
  551. float param1;
  552. float param2;
  553. };
  554. struct vk_op_glu_push_constants {
  555. uint32_t N;
  556. uint32_t ne00;
  557. uint32_t ne20;
  558. uint32_t mode; // 0: default, 1: swapped, 2: split
  559. };
  560. struct vk_op_unary_push_constants {
  561. uint32_t ne;
  562. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  563. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  564. uint32_t misalign_offsets;
  565. float param1; float param2;
  566. uint32_t ne0_012mp; uint32_t ne0_012L;
  567. uint32_t ne0_01mp; uint32_t ne0_01L;
  568. uint32_t ne0_0mp; uint32_t ne0_0L;
  569. uint32_t ne1_012mp; uint32_t ne1_012L;
  570. uint32_t ne1_01mp; uint32_t ne1_01L;
  571. uint32_t ne1_0mp; uint32_t ne1_0L;
  572. };
  573. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  574. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  575. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  576. ne = ne != 0 ? ne : ggml_nelements(dst);
  577. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  578. vk_op_unary_push_constants p{};
  579. p.ne = (uint32_t)ne;
  580. size_t src0_tsize = ggml_type_size(src0->type);
  581. p.ne00 = (uint32_t)src0->ne[0];
  582. p.ne01 = (uint32_t)src0->ne[1];
  583. p.ne02 = (uint32_t)src0->ne[2];
  584. p.ne03 = (uint32_t)src0->ne[3];
  585. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  586. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  587. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  588. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  589. size_t dst_tsize = ggml_type_size(dst->type);
  590. p.ne10 = (uint32_t)dst->ne[0];
  591. p.ne11 = (uint32_t)dst->ne[1];
  592. p.ne12 = (uint32_t)dst->ne[2];
  593. p.ne13 = (uint32_t)dst->ne[3];
  594. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  595. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  596. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  597. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  598. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  599. }
  600. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  601. // Precompute mp (m' in the paper) and L such that division
  602. // can be computed using a multiply (high 32b of 64b result)
  603. // and a shift:
  604. //
  605. // n/d = (mulhi(n, mp) + n) >> L;
  606. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  607. {
  608. // compute L = ceil(log2(d));
  609. L = 0;
  610. while (L < 32 && (uint32_t{1} << L) < d) {
  611. L++;
  612. }
  613. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  614. }
  615. template <typename T> void init_pushconst_fastdiv(T &p) {
  616. GGML_UNUSED(p);
  617. static_assert(!std::is_const<T>::value, "unexpected type");
  618. }
  619. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  620. // Compute magic values to divide by these six numbers.
  621. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  622. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  623. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  624. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  625. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  626. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  627. }
  628. struct vk_op_binary_push_constants {
  629. uint32_t ne;
  630. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  631. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  632. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  633. uint32_t misalign_offsets;
  634. float param1; float param2; int32_t param3;
  635. };
  636. struct vk_op_diag_mask_push_constants {
  637. uint32_t ncols;
  638. uint32_t rows_per_channel;
  639. int32_t n_past;
  640. };
  641. struct vk_op_rope_push_constants {
  642. uint32_t ncols;
  643. uint32_t n_dims;
  644. float freq_scale;
  645. uint32_t p_delta_rows;
  646. float freq_base;
  647. float ext_factor;
  648. float attn_factor;
  649. float corr_dims[2];
  650. float theta_scale;
  651. uint32_t has_ff;
  652. uint32_t ne02;
  653. uint32_t s1;
  654. uint32_t s2;
  655. int32_t sections[4];
  656. uint32_t is_back;
  657. };
  658. struct vk_op_soft_max_push_constants {
  659. uint32_t KX;
  660. uint32_t KY;
  661. uint32_t ne00;
  662. uint32_t ne01;
  663. uint32_t ne02;
  664. uint32_t ne12;
  665. uint32_t ne13;
  666. uint32_t nb11;
  667. uint32_t nb12;
  668. uint32_t nb13;
  669. float scale;
  670. float max_bias;
  671. float m0;
  672. float m1;
  673. uint32_t n_head_log2;
  674. uint32_t nrows_x;
  675. };
  676. struct vk_op_argsort_push_constants {
  677. uint32_t ncols;
  678. uint32_t ncols_pad;
  679. int32_t order;
  680. };
  681. struct vk_op_im2col_push_constants {
  682. uint32_t batch_offset; uint32_t offset_delta;
  683. uint32_t IC;
  684. uint32_t IW; uint32_t IH;
  685. uint32_t OW; uint32_t OH;
  686. uint32_t KW; uint32_t KH;
  687. uint32_t pelements;
  688. uint32_t CHW;
  689. int32_t s0; int32_t s1;
  690. int32_t p0; int32_t p1;
  691. int32_t d0; int32_t d1;
  692. };
  693. struct vk_op_timestep_embedding_push_constants {
  694. uint32_t nb1;
  695. uint32_t dim;
  696. uint32_t max_period;
  697. };
  698. struct vk_op_conv_transpose_1d_push_constants {
  699. uint32_t Cout;
  700. uint32_t Cin;
  701. uint32_t K;
  702. uint32_t L;
  703. uint32_t KL;
  704. uint32_t nb01;
  705. uint32_t nb02;
  706. uint32_t nb11;
  707. uint32_t nb1;
  708. int32_t s0;
  709. };
  710. struct vk_op_pool2d_push_constants {
  711. uint32_t IW; uint32_t IH;
  712. uint32_t OW; uint32_t OH;
  713. uint32_t OC;
  714. uint32_t pelements;
  715. uint32_t op;
  716. int32_t k0; int32_t k1;
  717. int32_t s0; int32_t s1;
  718. int32_t p0; int32_t p1;
  719. };
  720. struct vk_op_rwkv_wkv6_push_constants {
  721. uint32_t B;
  722. uint32_t T;
  723. uint32_t C;
  724. uint32_t H;
  725. };
  726. struct vk_op_rwkv_wkv7_push_constants {
  727. uint32_t B;
  728. uint32_t T;
  729. uint32_t C;
  730. uint32_t H;
  731. };
  732. struct vk_op_conv2d_dw_push_constants {
  733. uint32_t ne;
  734. uint32_t batches;
  735. uint32_t channels;
  736. uint32_t dst_w;
  737. uint32_t dst_h;
  738. uint32_t src_w;
  739. uint32_t src_h;
  740. uint32_t knl_w;
  741. uint32_t knl_h;
  742. int32_t stride_x;
  743. int32_t stride_y;
  744. int32_t pad_x;
  745. int32_t pad_y;
  746. int32_t dilation_x;
  747. int32_t dilation_y;
  748. };
  749. struct vk_op_upscale_push_constants {
  750. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  751. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  752. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  753. float sf0; float sf1; float sf2; float sf3;
  754. };
  755. // Allow pre-recording command buffers
  756. struct vk_staging_memcpy {
  757. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  758. void * dst;
  759. const void * src;
  760. size_t n;
  761. };
  762. struct vk_context_struct {
  763. vk_submission * s;
  764. std::vector<vk_sequence> seqs;
  765. int exit_tensor_idx;
  766. std::vector<vk_staging_memcpy> in_memcpys;
  767. std::vector<vk_staging_memcpy> out_memcpys;
  768. vk_command_pool * p {};
  769. };
  770. typedef std::shared_ptr<vk_context_struct> vk_context;
  771. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  772. struct ggml_vk_garbage_collector {
  773. std::vector<vk_semaphore> tl_semaphores;
  774. std::vector<vk_semaphore> semaphores;
  775. std::vector<vk::Event> events;
  776. std::vector<vk_buffer> temp_buffers;
  777. std::vector<vk_context> contexts;
  778. };
  779. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  780. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  781. static std::string format_size(size_t size) {
  782. const size_t kib = 1024;
  783. const size_t mib = kib * 1024;
  784. const size_t gib = mib * 1024;
  785. std::ostringstream oss;
  786. oss << std::fixed << std::setprecision(2);
  787. if (size >= gib) {
  788. oss << static_cast<double>(size) / gib << " GiB";
  789. } else if (size >= mib) {
  790. oss << static_cast<double>(size) / mib << " MiB";
  791. } else if (size >= kib) {
  792. oss << static_cast<double>(size) / kib << " KiB";
  793. } else {
  794. oss << size << " B";
  795. }
  796. return oss.str();
  797. }
  798. static std::mutex log_mutex;
  799. class vk_memory_logger {
  800. public:
  801. vk_memory_logger(): total_device(0), total_host(0) {}
  802. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  803. void log_deallocation(vk_buffer_ref buf_ref);
  804. private:
  805. std::map<vk::Buffer, size_t> allocations; // Track allocations
  806. size_t total_device;
  807. size_t total_host;
  808. };
  809. #else
  810. #define VK_LOG_MEMORY(msg) ((void) 0)
  811. #endif // GGML_VULKAN_MEMORY_DEBUG
  812. class vk_perf_logger {
  813. public:
  814. void print_timings() {
  815. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  816. for (const auto& t : timings) {
  817. uint64_t total = 0;
  818. for (const auto& time : t.second) {
  819. total += time;
  820. }
  821. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " us" << std::endl;
  822. }
  823. timings.clear();
  824. }
  825. void log_timing(const ggml_tensor * node, uint64_t time) {
  826. if (node->op == GGML_OP_UNARY) {
  827. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  828. return;
  829. }
  830. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  831. const uint64_t m = node->src[0]->ne[1];
  832. const uint64_t n = node->src[1]->ne[1];
  833. const uint64_t k = node->src[1]->ne[0];
  834. std::string name = ggml_op_name(node->op);
  835. if (n == 1) {
  836. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  837. } else {
  838. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  839. }
  840. timings[name].push_back(time);
  841. return;
  842. }
  843. timings[ggml_op_name(node->op)].push_back(time);
  844. }
  845. private:
  846. std::map<std::string, std::vector<uint64_t>> timings;
  847. };
  848. struct ggml_backend_vk_context {
  849. std::string name;
  850. vk_device device;
  851. size_t semaphore_idx, event_idx;
  852. ggml_vk_garbage_collector gc;
  853. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  854. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  855. vk::Fence fence, almost_ready_fence;
  856. bool almost_ready_fence_pending {};
  857. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  858. vk_context_ref compute_ctx;
  859. vk_context_ref transfer_ctx;
  860. std::vector<vk_context_ref> tensor_ctxs;
  861. std::vector<vk::DescriptorPool> descriptor_pools;
  862. std::vector<vk::DescriptorSet> descriptor_sets;
  863. uint32_t descriptor_set_idx {};
  864. uint32_t pipeline_descriptor_set_requirements {};
  865. vk_command_pool compute_cmd_pool;
  866. vk_command_pool transfer_cmd_pool;
  867. // number of additional consecutive nodes that are being fused with the
  868. // node currently being processed
  869. int num_additional_fused_ops {};
  870. };
  871. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  872. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  873. if (tensor->view_src) {
  874. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  875. }
  876. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  877. }
  878. struct ggml_backend_vk_buffer_context {
  879. vk_device_ref device;
  880. vk_buffer dev_buffer;
  881. std::string name;
  882. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  883. device(device),
  884. dev_buffer(dev_buffer),
  885. name(name) {
  886. }
  887. ~ggml_backend_vk_buffer_context() {
  888. ggml_vk_destroy_buffer(dev_buffer);
  889. }
  890. };
  891. #ifdef GGML_VULKAN_MEMORY_DEBUG
  892. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  893. std::lock_guard<std::mutex> guard(log_mutex);
  894. vk_buffer buf = buf_ref.lock();
  895. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  896. const std::string type = device ? "device" : "host";
  897. allocations[buf->buffer] = size;
  898. total_device += device ? size : 0;
  899. total_host += device ? 0 : size;
  900. 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));
  901. }
  902. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  903. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  904. return;
  905. }
  906. std::lock_guard<std::mutex> guard(log_mutex);
  907. vk_buffer buf = buf_ref.lock();
  908. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  909. std::string type = device ? "device" : "host";
  910. auto it = allocations.find(buf->buffer);
  911. total_device -= device ? it->second : 0;
  912. total_host -= device ? 0 : it->second;
  913. if (it != allocations.end()) {
  914. 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));
  915. allocations.erase(it);
  916. } else {
  917. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  918. }
  919. }
  920. #endif // GGML_VULKAN_MEMORY_DEBUG
  921. struct vk_instance_t {
  922. vk::Instance instance;
  923. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  924. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  925. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  926. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  927. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  928. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  929. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  930. std::vector<size_t> device_indices;
  931. vk_device devices[GGML_VK_MAX_DEVICES];
  932. };
  933. static bool vk_instance_initialized = false;
  934. static vk_instance_t vk_instance;
  935. static bool vk_perf_logger_enabled = false;
  936. #ifdef GGML_VULKAN_CHECK_RESULTS
  937. static size_t vk_skip_checks;
  938. static size_t vk_output_tensor;
  939. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  940. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  941. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  942. #endif
  943. 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);
  944. static void ggml_backend_vk_free(ggml_backend_t backend);
  945. // Wait for ctx->fence to be signaled.
  946. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  947. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  948. // during this wait.
  949. if (ctx->almost_ready_fence_pending) {
  950. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  951. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  952. ctx->almost_ready_fence_pending = false;
  953. }
  954. // Spin (w/pause) waiting for the graph to finish executing.
  955. vk::Result result;
  956. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  957. if (result != vk::Result::eNotReady) {
  958. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  959. exit(1);
  960. }
  961. for (uint32_t i = 0; i < 100; ++i) {
  962. YIELD();
  963. YIELD();
  964. YIELD();
  965. YIELD();
  966. YIELD();
  967. YIELD();
  968. YIELD();
  969. YIELD();
  970. YIELD();
  971. YIELD();
  972. }
  973. }
  974. ctx->device->device.resetFences({ ctx->fence });
  975. }
  976. // variables to track number of compiles in progress
  977. static uint32_t compile_count = 0;
  978. static std::mutex compile_count_mutex;
  979. static std::condition_variable compile_count_cond;
  980. 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,
  981. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  982. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  983. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  984. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  985. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  986. GGML_ASSERT(parameter_count > 0);
  987. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  988. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  989. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  990. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  991. vk::PushConstantRange pcr(
  992. vk::ShaderStageFlagBits::eCompute,
  993. 0,
  994. pipeline->push_constant_size
  995. );
  996. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  997. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  998. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  999. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1000. specialization_entries[i].constantID = i;
  1001. specialization_entries[i].offset = i * sizeof(uint32_t);
  1002. specialization_entries[i].size = sizeof(uint32_t);
  1003. }
  1004. vk::SpecializationInfo specialization_info(
  1005. specialization_entries.size(),
  1006. specialization_entries.data(),
  1007. specialization_constants.size() * sizeof(uint32_t),
  1008. specialization_constants.data()
  1009. );
  1010. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1011. if (device->subgroup_require_full_support && require_full_subgroups) {
  1012. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1013. }
  1014. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1015. pipeline_shader_stage_create_flags,
  1016. vk::ShaderStageFlagBits::eCompute,
  1017. pipeline->shader_module,
  1018. entrypoint.c_str(),
  1019. &specialization_info);
  1020. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1021. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1022. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1023. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1024. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1025. }
  1026. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1027. vk::PipelineCreateFlags{},
  1028. pipeline_shader_create_info,
  1029. pipeline->layout);
  1030. vk::PipelineRobustnessCreateInfoEXT rci;
  1031. if (device->pipeline_robustness && disable_robustness) {
  1032. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1033. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1034. compute_pipeline_create_info.setPNext(&rci);
  1035. }
  1036. try {
  1037. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1038. } catch (const vk::SystemError& e) {
  1039. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1040. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1041. throw e;
  1042. }
  1043. pipeline->compiled = true;
  1044. if (vk_instance.debug_utils_support) {
  1045. vk::DebugUtilsObjectNameInfoEXT duoni;
  1046. duoni.objectType = vk::ObjectType::ePipeline;
  1047. duoni.pObjectName = pipeline->name.c_str();
  1048. duoni.objectHandle = reinterpret_cast<uint64_t>(static_cast<VkPipeline_T*>(pipeline->pipeline));
  1049. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1050. }
  1051. {
  1052. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1053. device->pipelines.insert({ pipeline->name, pipeline });
  1054. }
  1055. {
  1056. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1057. assert(compile_count > 0);
  1058. compile_count--;
  1059. }
  1060. compile_count_cond.notify_all();
  1061. }
  1062. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1063. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1064. device.destroyPipelineLayout(pipeline->layout);
  1065. device.destroyShaderModule(pipeline->shader_module);
  1066. device.destroyPipeline(pipeline->pipeline);
  1067. }
  1068. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1069. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1070. ctx->pipeline_descriptor_set_requirements += n;
  1071. if (!pipeline->compiled) {
  1072. pipeline->needed = true;
  1073. ctx->device->need_compiles = true;
  1074. }
  1075. }
  1076. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1077. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1078. // Enough descriptors are available
  1079. return;
  1080. }
  1081. vk_device& device = ctx->device;
  1082. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1083. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1084. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1085. while (to_alloc > 0) {
  1086. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1087. to_alloc -= alloc_count;
  1088. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1089. if (pool_idx >= ctx->descriptor_pools.size()) {
  1090. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1091. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1092. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1093. }
  1094. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1095. for (uint32_t i = 0; i < alloc_count; i++) {
  1096. layouts[i] = device->dsl;
  1097. }
  1098. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1099. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1100. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1101. pool_idx++;
  1102. }
  1103. }
  1104. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1105. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1106. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1107. // Reuse command buffer
  1108. return p.cmd_buffers[p.cmd_buffer_idx++];
  1109. }
  1110. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1111. p.pool,
  1112. vk::CommandBufferLevel::ePrimary,
  1113. 1);
  1114. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1115. auto buf = cmd_buffers.front();
  1116. p.cmd_buffers.push_back(buf);
  1117. p.cmd_buffer_idx++;
  1118. return buf;
  1119. }
  1120. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1121. if (ctx->seqs.empty()) {
  1122. if (fence) {
  1123. std::lock_guard<std::mutex> guard(queue_mutex);
  1124. ctx->p->q->queue.submit({}, fence);
  1125. }
  1126. return;
  1127. }
  1128. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1129. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1130. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1131. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1132. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1133. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1134. std::vector<vk::SubmitInfo> submit_infos;
  1135. int idx = -1;
  1136. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1137. size_t reserve = 0;
  1138. for (const auto& sequence : ctx->seqs) {
  1139. reserve += sequence.size();
  1140. }
  1141. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1142. tl_wait_semaphores.reserve(reserve);
  1143. tl_wait_vals.reserve(reserve);
  1144. tl_signal_semaphores.reserve(reserve);
  1145. tl_signal_vals.reserve(reserve);
  1146. tl_submit_infos.reserve(reserve);
  1147. submit_infos.reserve(reserve);
  1148. stage_flags.reserve(reserve);
  1149. for (const auto& sequence : ctx->seqs) {
  1150. for (const auto& submission : sequence) {
  1151. stage_flags.push_back({});
  1152. idx++;
  1153. tl_wait_vals.push_back({});
  1154. tl_wait_semaphores.push_back({});
  1155. tl_signal_vals.push_back({});
  1156. tl_signal_semaphores.push_back({});
  1157. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1158. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1159. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1160. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1161. }
  1162. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1163. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1164. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1165. }
  1166. tl_submit_infos.push_back({
  1167. (uint32_t) submission.wait_semaphores.size(),
  1168. tl_wait_vals[idx].data(),
  1169. (uint32_t) submission.signal_semaphores.size(),
  1170. tl_signal_vals[idx].data(),
  1171. });
  1172. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1173. tl_submit_infos[idx].pNext = nullptr;
  1174. vk::SubmitInfo si{
  1175. (uint32_t) submission.wait_semaphores.size(),
  1176. tl_wait_semaphores[idx].data(),
  1177. stage_flags[idx].data(),
  1178. 1,
  1179. &submission.buffer,
  1180. (uint32_t) submission.signal_semaphores.size(),
  1181. tl_signal_semaphores[idx].data(),
  1182. };
  1183. si.setPNext(&tl_submit_infos[idx]);
  1184. submit_infos.push_back(si);
  1185. }
  1186. }
  1187. std::lock_guard<std::mutex> guard(queue_mutex);
  1188. ctx->p->q->queue.submit(submit_infos, fence);
  1189. ctx->seqs.clear();
  1190. }
  1191. 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) {
  1192. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1193. const uint32_t qfsize = queue_family_props.size();
  1194. // Try with avoid preferences first
  1195. for (uint32_t i = 0; i < qfsize; i++) {
  1196. 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)) {
  1197. return i;
  1198. }
  1199. }
  1200. // Fall back to only required
  1201. for (size_t i = 0; i < qfsize; i++) {
  1202. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1203. return i;
  1204. }
  1205. }
  1206. // Fall back to reusing compute queue
  1207. for (size_t i = 0; i < qfsize; i++) {
  1208. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1209. return i;
  1210. }
  1211. }
  1212. // Fall back to ignoring min_num_queries
  1213. for (size_t i = 0; i < qfsize; i++) {
  1214. if (queue_family_props[i].queueFlags & required) {
  1215. return i;
  1216. }
  1217. }
  1218. // 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.
  1219. // 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.
  1220. if (compute_index >= 0) {
  1221. return compute_index;
  1222. }
  1223. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1224. for(auto &q_family : queue_family_props) {
  1225. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1226. }
  1227. abort();
  1228. }
  1229. 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) {
  1230. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1231. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1232. q.queue_family_index = queue_family_index;
  1233. q.transfer_only = transfer_only;
  1234. q.cmd_pool.init(device, &q);
  1235. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1236. q.stage_flags = stage_flags;
  1237. }
  1238. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1239. vk_context result = std::make_shared<vk_context_struct>();
  1240. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1241. ctx->gc.contexts.emplace_back(result);
  1242. result->p = &p;
  1243. return result;
  1244. }
  1245. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1246. vk_context result = std::make_shared<vk_context_struct>();
  1247. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1248. result->p = &p;
  1249. return result;
  1250. }
  1251. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1252. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1253. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1254. vk::SemaphoreCreateInfo ci{};
  1255. ci.setPNext(&tci);
  1256. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1257. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1258. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1259. }
  1260. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1261. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1262. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1263. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1264. vk::SemaphoreCreateInfo ci{};
  1265. ci.setPNext(&tci);
  1266. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1267. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1268. }
  1269. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1270. }
  1271. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1272. if (ctx->event_idx >= ctx->gc.events.size()) {
  1273. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1274. }
  1275. return ctx->gc.events[ctx->event_idx++];
  1276. }
  1277. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1278. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1279. // Requires command buffers to be done
  1280. device->device.resetCommandPool(p.pool);
  1281. p.cmd_buffer_idx = 0;
  1282. }
  1283. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1284. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1285. // Arbitrary frequency to cleanup/reuse command buffers
  1286. static constexpr uint32_t cleanup_frequency = 10;
  1287. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1288. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1289. }
  1290. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1291. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1292. }
  1293. }
  1294. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1295. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1296. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1297. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1298. (flags & memory_type.propertyFlags) == flags &&
  1299. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1300. return static_cast<int32_t>(i);
  1301. }
  1302. }
  1303. return UINT32_MAX;
  1304. }
  1305. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1306. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1307. if (size > device->max_memory_allocation_size) {
  1308. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1309. }
  1310. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1311. if (size == 0) {
  1312. buf->size = 0;
  1313. return buf;
  1314. }
  1315. vk::BufferCreateInfo buffer_create_info{
  1316. vk::BufferCreateFlags(),
  1317. size,
  1318. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1319. vk::SharingMode::eExclusive,
  1320. 0,
  1321. nullptr,
  1322. };
  1323. buf->buffer = device->device.createBuffer(buffer_create_info);
  1324. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1325. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1326. uint32_t memory_type_index = UINT32_MAX;
  1327. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1328. buf->memory_property_flags = req_flags;
  1329. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1330. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1331. buf->memory_property_flags = fallback_flags;
  1332. }
  1333. if (memory_type_index == UINT32_MAX) {
  1334. device->device.destroyBuffer(buf->buffer);
  1335. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1336. }
  1337. try {
  1338. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1339. } catch (const vk::SystemError& e) {
  1340. if (buf->memory_property_flags != fallback_flags) {
  1341. // Try again with fallback flags
  1342. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1343. buf->memory_property_flags = fallback_flags;
  1344. try {
  1345. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1346. }
  1347. catch (const vk::SystemError& e) {
  1348. device->device.destroyBuffer(buf->buffer);
  1349. throw e;
  1350. }
  1351. } else {
  1352. // Out of Host/Device memory, clean up buffer
  1353. device->device.destroyBuffer(buf->buffer);
  1354. throw e;
  1355. }
  1356. }
  1357. buf->ptr = nullptr;
  1358. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1359. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1360. }
  1361. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1362. buf->device = device;
  1363. buf->size = size;
  1364. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1365. device->memory_logger->log_allocation(buf, size);
  1366. #endif
  1367. return buf;
  1368. }
  1369. 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)) {
  1370. try {
  1371. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1372. } catch (const vk::SystemError& e) {
  1373. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1374. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1375. throw e;
  1376. }
  1377. }
  1378. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1379. vk_buffer buf;
  1380. try {
  1381. if (device->prefer_host_memory) {
  1382. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1383. } else if (device->uma) {
  1384. // Fall back to host memory type
  1385. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1386. } else {
  1387. // use rebar if available, otherwise fallback to device only visible memory
  1388. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1389. }
  1390. } catch (const vk::SystemError& e) {
  1391. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1392. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1393. throw e;
  1394. }
  1395. return buf;
  1396. }
  1397. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1398. if (buf == nullptr) {
  1399. return;
  1400. }
  1401. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1402. if (buf->device != nullptr) {
  1403. buf->device->memory_logger->log_deallocation(buf);
  1404. }
  1405. #endif
  1406. buf.reset();
  1407. }
  1408. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1409. return { buf, 0, VK_WHOLE_SIZE };
  1410. }
  1411. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1412. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1413. const bool transfer_queue = ctx->p->q->transfer_only;
  1414. ctx->s->buffer.pipelineBarrier(
  1415. ctx->p->q->stage_flags,
  1416. ctx->p->q->stage_flags,
  1417. {},
  1418. { {
  1419. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1420. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1421. } },
  1422. {},
  1423. {}
  1424. );
  1425. }
  1426. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1427. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1428. if (events.empty()) {
  1429. return;
  1430. }
  1431. ctx->s->buffer.waitEvents(
  1432. events,
  1433. ctx->p->q->stage_flags,
  1434. ctx->p->q->stage_flags,
  1435. {},
  1436. {},
  1437. {}
  1438. );
  1439. }
  1440. enum FaCodePath {
  1441. FA_SCALAR,
  1442. FA_COOPMAT1,
  1443. FA_COOPMAT2,
  1444. };
  1445. static FaHeadSizes fa_get_head_sizes(uint32_t hsk, uint32_t hsv) {
  1446. if (hsk != 192 && hsk != 576 && hsk != hsv) {
  1447. return FA_HEAD_SIZE_UNSUPPORTED;
  1448. }
  1449. switch (hsk) {
  1450. case 64: return FA_HEAD_SIZE_64;
  1451. case 80: return FA_HEAD_SIZE_80;
  1452. case 96: return FA_HEAD_SIZE_96;
  1453. case 112: return FA_HEAD_SIZE_112;
  1454. case 128: return FA_HEAD_SIZE_128;
  1455. case 192:
  1456. if (hsv == 192) {
  1457. return FA_HEAD_SIZE_192;
  1458. } else if (hsv == 128) {
  1459. return FA_HEAD_SIZE_192_128;
  1460. } else {
  1461. return FA_HEAD_SIZE_UNSUPPORTED;
  1462. }
  1463. case 256: return FA_HEAD_SIZE_256;
  1464. case 576:
  1465. if (hsv == 512) {
  1466. return FA_HEAD_SIZE_576_512;
  1467. } else {
  1468. return FA_HEAD_SIZE_UNSUPPORTED;
  1469. }
  1470. default: return FA_HEAD_SIZE_UNSUPPORTED;
  1471. }
  1472. }
  1473. // number of rows/cols for flash attention shader
  1474. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1475. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1476. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1477. if (hsv >= 512) {
  1478. return 2;
  1479. } else {
  1480. return 8;
  1481. }
  1482. }
  1483. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1484. // 128 threads split into four subgroups, each subgroup does 1/4
  1485. // of the Bc dimension.
  1486. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1487. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1488. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1489. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1490. if (path == FA_COOPMAT2) {
  1491. return flash_attention_num_small_rows;
  1492. } else {
  1493. return scalar_flash_attention_num_small_rows;
  1494. }
  1495. }
  1496. 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) {
  1497. GGML_UNUSED(clamp);
  1498. GGML_UNUSED(hsv);
  1499. if (path == FA_SCALAR) {
  1500. if (small_rows) {
  1501. return {scalar_flash_attention_num_small_rows, 64};
  1502. } else {
  1503. return {get_fa_scalar_num_large_rows(hsv), 32};
  1504. }
  1505. }
  1506. if (path == FA_COOPMAT1) {
  1507. if (small_rows) {
  1508. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1509. } else {
  1510. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1511. }
  1512. }
  1513. // small rows, large cols
  1514. if (small_rows) {
  1515. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1516. }
  1517. // small cols to reduce register count
  1518. if (ggml_is_quantized(type) || hsk >= 256) {
  1519. if (hsk >= 512) {
  1520. return {32, 32};
  1521. } else {
  1522. return {64, 32};
  1523. }
  1524. }
  1525. return {64, 64};
  1526. }
  1527. 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) {
  1528. uint32_t lut_size = 0;
  1529. switch (src0_type) {
  1530. case GGML_TYPE_IQ1_S:
  1531. case GGML_TYPE_IQ1_M:
  1532. lut_size = 2*2048;
  1533. break;
  1534. case GGML_TYPE_IQ2_XXS:
  1535. lut_size = 8*256;
  1536. break;
  1537. case GGML_TYPE_IQ2_XS:
  1538. lut_size = 8*512;
  1539. break;
  1540. case GGML_TYPE_IQ2_S:
  1541. lut_size = 8*1024;
  1542. break;
  1543. case GGML_TYPE_IQ3_XXS:
  1544. lut_size = 4*256;
  1545. break;
  1546. case GGML_TYPE_IQ3_S:
  1547. lut_size = 4*512;
  1548. break;
  1549. case GGML_TYPE_IQ4_NL:
  1550. case GGML_TYPE_IQ4_XS:
  1551. lut_size = 4*16;
  1552. break;
  1553. default:
  1554. break;
  1555. }
  1556. // Needs to be kept up to date on shader changes
  1557. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1558. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1559. const uint32_t warps = warptile[0] / warptile[10];
  1560. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1561. const uint32_t mmid_row_ids = mul_mat_id ? (4096 * sizeof(uint32_t) + 4/*_ne1*/) : 0;
  1562. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1563. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1564. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1565. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1566. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1567. return supported;
  1568. }
  1569. struct GpuPipelineConfig {
  1570. // GPU architecture identifier.
  1571. // Example: vk_device_architecture::AMD_GCN
  1572. vk_device_architecture arch;
  1573. // Mapping of pipeline names to their specific subgroup sizes.
  1574. // Example: {"soft_max_f32", 64}
  1575. std::unordered_map<std::string, uint32_t> pipelines;
  1576. // Default subgroup size for this GPU.
  1577. // Defaults to 0 if not explicitly provided.
  1578. uint32_t default_subgroup_size = 0;
  1579. };
  1580. // Pipeline configuration for RDNA1 GPUs.
  1581. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1582. {"soft_max", 64}, {"im2col", 64},
  1583. {"argmax", 64}, {"mul_mat_vec", 64},
  1584. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1585. };
  1586. // Pipeline configuration for RDNA2 GPUs.
  1587. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1588. {"soft_max", 64}, {"im2col", 64},
  1589. };
  1590. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1591. // Define configurations for different GPUs.
  1592. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1593. {
  1594. vk_device_architecture::AMD_RDNA1,
  1595. {
  1596. rdna1_pipelines,
  1597. },
  1598. RDNA_DEFAULT_SUBGROUP_SIZE
  1599. },
  1600. {
  1601. vk_device_architecture::AMD_RDNA2,
  1602. {
  1603. rdna2_pipelines,
  1604. },
  1605. RDNA_DEFAULT_SUBGROUP_SIZE
  1606. },
  1607. };
  1608. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1609. for (const auto &config : gpu_pipeline_configs) {
  1610. if (config.arch == arch) {
  1611. auto pipIt = config.pipelines.find(pipeline_name);
  1612. if (pipIt != config.pipelines.end()) {
  1613. return pipIt->second;
  1614. }
  1615. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1616. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1617. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1618. for (const auto &entry : sorted_pipelines) {
  1619. if (pipeline_name.find(entry.first) != std::string::npos) {
  1620. return entry.second;
  1621. }
  1622. }
  1623. return config.default_subgroup_size;
  1624. }
  1625. }
  1626. return 0; // If no matching configuration is found
  1627. }
  1628. static void ggml_vk_load_shaders(vk_device& device) {
  1629. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1630. // some shaders have a minimum subgroup size
  1631. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1632. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1633. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1634. // mulmat
  1635. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1636. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1637. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1638. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1639. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1640. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1641. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1642. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1643. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1644. uint32_t l_align, m_align, s_align;
  1645. if (device->coopmat2) {
  1646. // spec constants and tile sizes for non-quant matmul/matmul_id
  1647. l_warptile = { 256, 128, 256, 64, 1 };
  1648. m_warptile = { 256, 128, 128, 64, 0 };
  1649. s_warptile = { 128, 64, 64, 64, 0 };
  1650. l_wg_denoms = {128, 256, 1 };
  1651. m_wg_denoms = {128, 128, 1 };
  1652. s_wg_denoms = { 64, 64, 1 };
  1653. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1654. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1655. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1656. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1657. l_mmq_wg_denoms = { 128, 256, 1 };
  1658. m_mmq_wg_denoms = { 128, 128, 1 };
  1659. s_mmq_wg_denoms = { 32, 64, 1 };
  1660. // spec constants and tile sizes for quant matmul (Qi_K)
  1661. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1662. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1663. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1664. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1665. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1666. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1667. // spec constants and tile sizes for quant matmul_id
  1668. l_warptile_mmqid = { 256, 128, 128, 16, 0 };
  1669. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1670. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1671. l_mmqid_wg_denoms = { 128, 128, 1 };
  1672. m_mmqid_wg_denoms = { 128, 64, 1 };
  1673. s_mmqid_wg_denoms = { 128, 64, 1 };
  1674. l_align = 128;
  1675. m_align = 64;
  1676. s_align = 32;
  1677. } else {
  1678. // Matrix cores require different warp group sizes
  1679. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1680. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1681. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1682. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1683. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1684. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1685. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1686. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1687. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1688. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1689. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1690. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1691. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1692. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1693. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1694. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1695. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1696. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1697. // chip specific tuning
  1698. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1699. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1700. }
  1701. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1702. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1703. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1704. l_align = 128;
  1705. m_align = 64;
  1706. s_align = 32;
  1707. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1708. ggml_type t = (ggml_type)i;
  1709. // Disable medium and large matrix multiplication if not enough shared memory is available
  1710. // Check mmq warptiles as the largest configuration
  1711. // Throw an error if not enough for any matrix multiplication is available
  1712. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1713. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1714. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1715. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1716. device->mul_mat_m[i] = false;
  1717. device->mul_mat_l[i] = false;
  1718. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1719. device->mul_mat_l[i] = false;
  1720. }
  1721. // Disable mul_mat_id if not enough shared memory is available
  1722. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1723. device->mul_mat_id_s[i] = false;
  1724. device->mul_mat_id_m[i] = false;
  1725. device->mul_mat_id_l[i] = false;
  1726. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1727. device->mul_mat_id_m[i] = false;
  1728. device->mul_mat_id_l[i] = false;
  1729. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1730. device->mul_mat_id_l[i] = false;
  1731. }
  1732. }
  1733. }
  1734. if (!device->pipeline_matmul_f32) {
  1735. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1736. }
  1737. if (!device->pipeline_matmul_f32_f16) {
  1738. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1739. }
  1740. if (!device->pipeline_matmul_id_f32) {
  1741. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1742. }
  1743. if (!device->pipeline_matmul_bf16) {
  1744. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1745. }
  1746. if (!device->pipeline_matmul_id_bf16) {
  1747. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1748. }
  1749. std::vector<std::future<void>> compiles;
  1750. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const std::string &entrypoint,
  1751. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1752. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1753. if (!require_full_subgroups && required_subgroup_size == 0) {
  1754. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1755. }
  1756. if (!pipeline) {
  1757. pipeline = std::make_shared<vk_pipeline_struct>();
  1758. pipeline->name = name;
  1759. pipeline->parameter_count = parameter_count;
  1760. pipeline->push_constant_size = push_constant_size;
  1761. pipeline->wg_denoms = wg_denoms;
  1762. pipeline->align = align;
  1763. }
  1764. if (!pipeline->needed || pipeline->compiled) {
  1765. return;
  1766. }
  1767. {
  1768. // wait until fewer than N compiles are in progress
  1769. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1770. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1771. while (compile_count >= N) {
  1772. compile_count_cond.wait(guard);
  1773. }
  1774. compile_count++;
  1775. }
  1776. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1777. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1778. };
  1779. 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> {
  1780. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  1781. };
  1782. 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> {
  1783. // For large number of rows, 128 invocations seems to work best.
  1784. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1785. // can't use 256 for D==80.
  1786. // For scalar, use 128 (arbitrary)
  1787. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  1788. const uint32_t D = (hsk|hsv);
  1789. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  1790. ? scalar_flash_attention_workgroup_size
  1791. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  1792. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  1793. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  1794. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  1795. const uint32_t D_lsb = D ^ (D & (D-1));
  1796. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  1797. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1798. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1799. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  1800. };
  1801. #define CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, HSK, HSV, HEAD_SIZES) \
  1802. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1803. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1804. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1805. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1806. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1807. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1808. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1809. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1810. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  1811. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 64, 64, 64) \
  1812. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 80, 80, 80) \
  1813. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 96, 96, 96) \
  1814. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 112, 112, 112) \
  1815. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 128, 128, 128) \
  1816. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 192, 192) \
  1817. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 128, 192_128) \
  1818. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 256, 256, 256) \
  1819. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 576, 512, 576_512)
  1820. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  1821. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  1822. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  1823. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1824. if (device->coopmat1_fa_support) {
  1825. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  1826. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  1827. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  1828. }
  1829. #endif
  1830. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1831. if (device->coopmat2) {
  1832. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  1833. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  1834. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  1835. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  1836. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  1837. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  1838. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  1839. }
  1840. #endif
  1841. #undef CREATE_FA2
  1842. #undef CREATE_FA
  1843. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1844. if (device->coopmat2) {
  1845. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1846. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1847. 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); \
  1848. 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); \
  1849. 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); \
  1850. 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); \
  1851. 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); \
  1852. 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); \
  1853. // Create 2 variants, {f16,f32} accumulator
  1854. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1855. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1856. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1857. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1858. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1859. if (device->coopmat_bf16_support) {
  1860. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1861. }
  1862. #endif
  1863. 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)
  1864. 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)
  1865. 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)
  1866. 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)
  1867. 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)
  1868. 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)
  1869. 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)
  1870. 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)
  1871. 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)
  1872. 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)
  1873. 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)
  1874. 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)
  1875. 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)
  1876. 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)
  1877. 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)
  1878. 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)
  1879. 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)
  1880. 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)
  1881. 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)
  1882. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1883. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1884. if (device->coopmat_bf16_support) {
  1885. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1886. }
  1887. #endif
  1888. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1889. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1890. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1891. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1892. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1893. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1894. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1895. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1896. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1897. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1898. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1899. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1900. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1901. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1902. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1903. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1904. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1905. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1906. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1907. #undef CREATE_MM
  1908. #undef CREATE_MM2
  1909. } else
  1910. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1911. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1912. if (device->coopmat_support) {
  1913. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1914. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1915. if (device->mul_mat ## ID ## _l[TYPE]) \
  1916. 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); \
  1917. if (device->mul_mat ## ID ## _m[TYPE]) \
  1918. 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); \
  1919. if (device->mul_mat ## ID ## _s[TYPE]) \
  1920. 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); \
  1921. if (device->mul_mat ## ID ## _l[TYPE]) \
  1922. 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); \
  1923. if (device->mul_mat ## ID ## _m[TYPE]) \
  1924. 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); \
  1925. if (device->mul_mat ## ID ## _s[TYPE]) \
  1926. 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); \
  1927. // Create 2 variants, {f16,f32} accumulator
  1928. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1929. if (device->coopmat_acc_f16_support) { \
  1930. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1931. } \
  1932. if (device->coopmat_acc_f32_support) { \
  1933. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1934. } \
  1935. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1936. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1937. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1938. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1939. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1940. if (device->coopmat_bf16_support) {
  1941. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  1942. }
  1943. #endif
  1944. if (device->coopmat_acc_f16_support) {
  1945. 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, );
  1946. 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, );
  1947. 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, );
  1948. 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, );
  1949. 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, );
  1950. 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, );
  1951. 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, );
  1952. 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, );
  1953. 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, );
  1954. 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, );
  1955. 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, );
  1956. 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, );
  1957. 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, );
  1958. 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, );
  1959. 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, );
  1960. 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, );
  1961. 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, );
  1962. 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, );
  1963. 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, );
  1964. } else {
  1965. 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, );
  1966. 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, );
  1967. 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, );
  1968. 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, );
  1969. 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, );
  1970. 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, );
  1971. 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, );
  1972. 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, );
  1973. 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, );
  1974. 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, );
  1975. 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, );
  1976. 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, );
  1977. 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, );
  1978. 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, );
  1979. 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, );
  1980. 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, );
  1981. 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, );
  1982. 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, );
  1983. 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, );
  1984. }
  1985. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1986. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1987. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1988. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1989. if (device->coopmat_bf16_support) {
  1990. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1991. }
  1992. #endif
  1993. if (device->coopmat_acc_f16_support) {
  1994. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1995. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1996. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1997. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1998. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1999. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2000. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2001. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2002. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2003. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2004. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2005. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2006. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2007. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2008. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2009. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2010. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2011. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2012. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2013. } else {
  2014. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2015. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2016. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2017. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2018. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2019. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2020. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2021. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2022. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2023. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2024. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2025. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2026. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2027. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2028. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2029. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2030. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2031. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2032. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2033. }
  2034. #undef CREATE_MM2
  2035. #undef CREATE_MM
  2036. } else
  2037. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2038. if (device->fp16) {
  2039. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2040. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2041. if (device->mul_mat ## ID ## _l[TYPE]) \
  2042. 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); \
  2043. if (device->mul_mat ## ID ## _m[TYPE]) \
  2044. 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); \
  2045. if (device->mul_mat ## ID ## _s[TYPE]) \
  2046. 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); \
  2047. if (device->mul_mat ## ID ## _l[TYPE]) \
  2048. 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); \
  2049. if (device->mul_mat ## ID ## _m[TYPE]) \
  2050. 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); \
  2051. if (device->mul_mat ## ID ## _s[TYPE]) \
  2052. 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); \
  2053. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2054. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2055. 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); \
  2056. 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); \
  2057. } \
  2058. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2059. 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); \
  2060. 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); \
  2061. } \
  2062. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2063. 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); \
  2064. 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); \
  2065. } \
  2066. // Create 2 variants, {f16,f32} accumulator
  2067. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2068. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2069. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2070. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2071. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2072. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2073. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2074. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2075. 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, );
  2076. 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, );
  2077. 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, );
  2078. 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, );
  2079. 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, );
  2080. 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, );
  2081. 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, );
  2082. 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, );
  2083. 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, );
  2084. 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, );
  2085. 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, );
  2086. 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, );
  2087. 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, );
  2088. 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, );
  2089. 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, );
  2090. 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, );
  2091. 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, );
  2092. 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, );
  2093. 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, );
  2094. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2095. if (device->integer_dot_product) {
  2096. 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, );
  2097. 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, );
  2098. 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, );
  2099. 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, );
  2100. 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, );
  2101. }
  2102. #endif
  2103. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2104. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2105. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2106. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2107. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2108. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2109. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2110. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2111. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2112. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2113. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2114. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2115. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2116. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2117. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2118. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2119. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2120. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2121. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2122. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2123. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2124. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2125. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2126. #undef CREATE_MM2
  2127. #undef CREATE_MMQ
  2128. #undef CREATE_MM
  2129. } else {
  2130. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2131. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2132. if (device->mul_mat ## ID ## _l[TYPE]) \
  2133. 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); \
  2134. if (device->mul_mat ## ID ## _m[TYPE]) \
  2135. 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); \
  2136. if (device->mul_mat ## ID ## _s[TYPE]) \
  2137. 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); \
  2138. if (device->mul_mat ## ID ## _l[TYPE]) \
  2139. 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); \
  2140. if (device->mul_mat ## ID ## _m[TYPE]) \
  2141. 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); \
  2142. if (device->mul_mat ## ID ## _s[TYPE]) \
  2143. 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); \
  2144. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2145. if (device->mul_mat ## ID ## _l[TYPE]) \
  2146. 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); \
  2147. if (device->mul_mat ## ID ## _m[TYPE]) \
  2148. 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); \
  2149. if (device->mul_mat ## ID ## _s[TYPE]) \
  2150. 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); \
  2151. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2152. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2153. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2154. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2155. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2156. 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, );
  2157. 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, );
  2158. 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, );
  2159. 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, );
  2160. 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, );
  2161. 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, );
  2162. 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, );
  2163. 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, );
  2164. 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, );
  2165. 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, );
  2166. 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, );
  2167. 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, );
  2168. 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, );
  2169. 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, );
  2170. 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, );
  2171. 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, );
  2172. 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, );
  2173. 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, );
  2174. 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, );
  2175. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2176. if (device->integer_dot_product) {
  2177. 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, );
  2178. 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, );
  2179. 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, );
  2180. 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, );
  2181. 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, );
  2182. }
  2183. #endif
  2184. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2185. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2186. 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);
  2187. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2188. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2189. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2190. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2191. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2192. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2193. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2194. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2195. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2196. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2197. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2198. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2199. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2200. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2201. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2202. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2203. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2204. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2205. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2206. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2207. }
  2208. // reusing CREATE_MM from the fp32 path
  2209. if ((device->coopmat2 || device->coopmat_support)
  2210. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2211. && !device->coopmat_bf16_support
  2212. #endif
  2213. ) {
  2214. // use scalar tile sizes
  2215. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2216. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2217. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2218. l_wg_denoms = {128, 128, 1 };
  2219. m_wg_denoms = { 64, 64, 1 };
  2220. s_wg_denoms = { 32, 32, 1 };
  2221. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2222. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2223. }
  2224. #undef CREATE_MM
  2225. // mul mat vec
  2226. // the number of rows computed per shader depends on GPU model and quant
  2227. uint32_t rm_stdq = 1;
  2228. uint32_t rm_kq = 2;
  2229. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2230. if (device->architecture == AMD_GCN) {
  2231. rm_stdq = 2;
  2232. rm_kq = 4;
  2233. }
  2234. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2235. rm_stdq = 2;
  2236. uint32_t rm_iq = 2 * rm_kq;
  2237. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2238. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2239. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2240. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f32_f32_len, mul_mat_vec_bf16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2241. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2242. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2243. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2244. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2245. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f32_f32_len, mul_mat_vec_q8_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2246. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2247. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2248. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2249. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2250. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2251. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2252. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2253. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2254. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2255. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2256. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2257. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2258. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2259. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2260. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2261. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2262. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f16_f32_len, mul_mat_vec_bf16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2263. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2264. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2265. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2266. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2267. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f16_f32_len, mul_mat_vec_q8_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2268. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2269. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2270. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2271. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2272. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2273. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2274. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2275. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2276. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2277. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2278. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2279. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2280. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2281. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2282. }
  2283. 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);
  2284. 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);
  2285. 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);
  2286. 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);
  2287. 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);
  2288. 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);
  2289. 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);
  2290. 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);
  2291. 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);
  2292. 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);
  2293. 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);
  2294. 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);
  2295. 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);
  2296. 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);
  2297. 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);
  2298. 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);
  2299. 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);
  2300. 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);
  2301. 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);
  2302. 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);
  2303. 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);
  2304. 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);
  2305. // dequant shaders
  2306. 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);
  2307. 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);
  2308. 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);
  2309. 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);
  2310. 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);
  2311. 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);
  2312. 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);
  2313. 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);
  2314. 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);
  2315. 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);
  2316. 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);
  2317. 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);
  2318. 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);
  2319. 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);
  2320. 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);
  2321. 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);
  2322. 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);
  2323. 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);
  2324. 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);
  2325. 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);
  2326. // get_rows
  2327. 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);
  2328. 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);
  2329. 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);
  2330. 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);
  2331. 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);
  2332. 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);
  2333. 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);
  2334. 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);
  2335. 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);
  2336. 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);
  2337. 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);
  2338. 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);
  2339. 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);
  2340. 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);
  2341. 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);
  2342. 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);
  2343. 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);
  2344. 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);
  2345. 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);
  2346. 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);
  2347. 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);
  2348. 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);
  2349. 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);
  2350. 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);
  2351. 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);
  2352. 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);
  2353. 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);
  2354. 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);
  2355. 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);
  2356. 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);
  2357. 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);
  2358. 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);
  2359. 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);
  2360. 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);
  2361. 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);
  2362. 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", 2, 4 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  2363. 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);
  2364. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2365. if (device->subgroup_add && device->subgroup_require_full_support) {
  2366. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  2367. } else {
  2368. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  2369. }
  2370. }
  2371. 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, 9 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2372. 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);
  2373. 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);
  2374. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1);
  2375. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1);
  2376. 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);
  2377. 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);
  2378. 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);
  2379. 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);
  2380. 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);
  2381. 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);
  2382. 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);
  2383. 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);
  2384. 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);
  2385. 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);
  2386. 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);
  2387. 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);
  2388. if (device->float_controls_rte_fp16) {
  2389. 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);
  2390. 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);
  2391. 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);
  2392. 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);
  2393. 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);
  2394. 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);
  2395. } else {
  2396. 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);
  2397. 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);
  2398. 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);
  2399. 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);
  2400. 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);
  2401. 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);
  2402. }
  2403. if (device->float_controls_rte_fp16) {
  2404. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_rte_len, set_rows_f32_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2405. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_rte_len, set_rows_f16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2406. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_rte_len, set_rows_bf16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2407. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_rte_len, set_rows_q4_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2408. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_rte_len, set_rows_q4_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2409. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_rte_len, set_rows_q5_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2410. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_rte_len, set_rows_q5_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2411. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_rte_len, set_rows_q8_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2412. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_rte_len, set_rows_iq4_nl_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2413. } else {
  2414. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_len, set_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2415. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_len, set_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2416. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_len, set_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2417. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_len, set_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2418. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_len, set_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2419. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_len, set_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2420. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_len, set_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2421. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_len, set_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2422. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_len, set_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2423. }
  2424. 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);
  2425. 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);
  2426. 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);
  2427. 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);
  2428. 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);
  2429. 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);
  2430. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2431. std::string s;
  2432. s += std::string(src0_f16 ? "_f16" : "_f32");
  2433. s += std::string(src1_f16 ? "_f16" : "_f32");
  2434. s += std::string(dst_f16 ? "_f16" : "_f32");
  2435. return s;
  2436. };
  2437. #define CREATE_BINARY(name, namemod, spec) \
  2438. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2439. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2440. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d], name ## _data[s0][s1][d], \
  2441. "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2442. CREATE_BINARY(add, , {0})
  2443. CREATE_BINARY(add, _norepeat, {1})
  2444. CREATE_BINARY(sub, , {0})
  2445. CREATE_BINARY(sub, _norepeat, {1})
  2446. CREATE_BINARY(mul, , {0})
  2447. CREATE_BINARY(mul, _norepeat, {1})
  2448. CREATE_BINARY(div, , {0})
  2449. CREATE_BINARY(div, _norepeat, {1})
  2450. #undef CREATE_BINARY
  2451. 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);
  2452. 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);
  2453. 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);
  2454. 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);
  2455. ggml_vk_create_pipeline(device, device->pipeline_upscale_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {}, 1);
  2456. 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);
  2457. 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);
  2458. 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);
  2459. 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);
  2460. 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);
  2461. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2462. 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);
  2463. 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);
  2464. 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);
  2465. #define CREATE_UNARY(name) \
  2466. 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); \
  2467. 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);
  2468. CREATE_UNARY(gelu)
  2469. CREATE_UNARY(gelu_erf)
  2470. CREATE_UNARY(gelu_quick)
  2471. CREATE_UNARY(silu)
  2472. CREATE_UNARY(relu)
  2473. CREATE_UNARY(tanh)
  2474. CREATE_UNARY(sigmoid)
  2475. #undef CREATE_UNARY
  2476. #define CREATE_GLU(name) \
  2477. 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); \
  2478. 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);
  2479. CREATE_GLU(geglu)
  2480. CREATE_GLU(reglu)
  2481. CREATE_GLU(swiglu)
  2482. CREATE_GLU(geglu_erf)
  2483. CREATE_GLU(geglu_quick)
  2484. #undef CREATE_GLU
  2485. 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);
  2486. 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);
  2487. 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);
  2488. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2489. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2490. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2491. 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", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2492. 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);
  2493. 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);
  2494. 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);
  2495. 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);
  2496. 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);
  2497. if (device->float_controls_rte_fp16) {
  2498. 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);
  2499. 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);
  2500. 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);
  2501. 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);
  2502. } else {
  2503. 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);
  2504. 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);
  2505. 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);
  2506. 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);
  2507. }
  2508. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  2509. 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);
  2510. ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2511. 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);
  2512. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2513. if (device->float_controls_rte_fp16) {
  2514. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2515. } else {
  2516. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2517. }
  2518. 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);
  2519. 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);
  2520. 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);
  2521. 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);
  2522. 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);
  2523. 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);
  2524. 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);
  2525. 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);
  2526. for (auto &c : compiles) {
  2527. c.wait();
  2528. }
  2529. device->need_compiles = false;
  2530. }
  2531. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2532. static vk_device ggml_vk_get_device(size_t idx) {
  2533. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2534. if (vk_instance.devices[idx] == nullptr) {
  2535. VK_LOG_DEBUG("Initializing new vk_device");
  2536. vk_device device = std::make_shared<vk_device_struct>();
  2537. vk_instance.devices[idx] = device;
  2538. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2539. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2540. #endif
  2541. if (vk_perf_logger_enabled) {
  2542. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2543. }
  2544. size_t dev_num = vk_instance.device_indices[idx];
  2545. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2546. if (dev_num >= physical_devices.size()) {
  2547. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2548. throw std::runtime_error("Device not found");
  2549. }
  2550. device->physical_device = physical_devices[dev_num];
  2551. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2552. device->architecture = get_device_architecture(device->physical_device);
  2553. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2554. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2555. bool fp16_storage = false;
  2556. bool fp16_compute = false;
  2557. bool maintenance4_support = false;
  2558. bool sm_builtins = false;
  2559. bool amd_shader_core_properties2 = false;
  2560. bool pipeline_robustness = false;
  2561. bool coopmat2_support = false;
  2562. device->coopmat_support = false;
  2563. device->integer_dot_product = false;
  2564. bool bfloat16_support = false;
  2565. for (const auto& properties : ext_props) {
  2566. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2567. maintenance4_support = true;
  2568. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2569. fp16_storage = true;
  2570. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2571. fp16_compute = true;
  2572. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2573. sm_builtins = true;
  2574. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2575. amd_shader_core_properties2 = true;
  2576. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2577. pipeline_robustness = true;
  2578. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2579. device->subgroup_size_control = true;
  2580. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2581. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2582. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2583. device->coopmat_support = true;
  2584. device->coopmat_m = 0;
  2585. device->coopmat_n = 0;
  2586. device->coopmat_k = 0;
  2587. #endif
  2588. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2589. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2590. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2591. coopmat2_support = true;
  2592. #endif
  2593. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2594. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2595. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2596. device->integer_dot_product = true;
  2597. #endif
  2598. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2599. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  2600. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2601. bfloat16_support = true;
  2602. #endif
  2603. }
  2604. }
  2605. vk::PhysicalDeviceProperties2 props2;
  2606. vk::PhysicalDeviceMaintenance3Properties props3;
  2607. vk::PhysicalDeviceMaintenance4Properties props4;
  2608. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2609. vk::PhysicalDeviceDriverProperties driver_props;
  2610. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2611. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2612. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2613. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2614. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2615. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2616. props2.pNext = &props3;
  2617. props3.pNext = &subgroup_props;
  2618. subgroup_props.pNext = &driver_props;
  2619. driver_props.pNext = &vk11_props;
  2620. vk11_props.pNext = &vk12_props;
  2621. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2622. if (maintenance4_support) {
  2623. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2624. last_struct = (VkBaseOutStructure *)&props4;
  2625. }
  2626. if (sm_builtins) {
  2627. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2628. last_struct = (VkBaseOutStructure *)&sm_props;
  2629. }
  2630. if (amd_shader_core_properties2) {
  2631. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2632. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2633. }
  2634. if (device->subgroup_size_control) {
  2635. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2636. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2637. }
  2638. #if defined(VK_NV_cooperative_matrix2)
  2639. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2640. if (coopmat2_support) {
  2641. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2642. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2643. }
  2644. #endif
  2645. if (device->integer_dot_product) {
  2646. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2647. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2648. }
  2649. device->physical_device.getProperties2(&props2);
  2650. device->properties = props2.properties;
  2651. device->vendor_id = device->properties.vendorID;
  2652. device->driver_id = driver_props.driverID;
  2653. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2654. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2655. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2656. } else if (maintenance4_support) {
  2657. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2658. } else {
  2659. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2660. }
  2661. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2662. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2663. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2664. } else {
  2665. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2666. device->suballocation_block_size = 1024*1024*1024;
  2667. }
  2668. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2669. device->subgroup_size = subgroup_props.subgroupSize;
  2670. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2671. if (sm_builtins) {
  2672. device->shader_core_count = sm_props.shaderSMCount;
  2673. } else if (amd_shader_core_properties2) {
  2674. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2675. } else {
  2676. device->shader_core_count = 0;
  2677. }
  2678. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2679. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2680. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2681. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2682. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  2683. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2684. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2685. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2686. device->coopmat_support = false;
  2687. }
  2688. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2689. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2690. // Try to find a non-graphics compute queue and transfer-focused queues
  2691. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2692. 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);
  2693. const float priorities[] = { 1.0f, 1.0f };
  2694. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2695. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2696. if (compute_queue_family_index != transfer_queue_family_index) {
  2697. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2698. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2699. } else if(!device->single_queue) {
  2700. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2701. } else {
  2702. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2703. }
  2704. vk::DeviceCreateInfo device_create_info;
  2705. std::vector<const char *> device_extensions;
  2706. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2707. VkPhysicalDeviceFeatures2 device_features2;
  2708. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2709. device_features2.pNext = nullptr;
  2710. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2711. VkPhysicalDeviceVulkan11Features vk11_features;
  2712. vk11_features.pNext = nullptr;
  2713. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2714. device_features2.pNext = &vk11_features;
  2715. VkPhysicalDeviceVulkan12Features vk12_features;
  2716. vk12_features.pNext = nullptr;
  2717. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2718. vk11_features.pNext = &vk12_features;
  2719. last_struct = (VkBaseOutStructure *)&vk12_features;
  2720. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2721. pl_robustness_features.pNext = nullptr;
  2722. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2723. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2724. if (pipeline_robustness) {
  2725. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2726. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2727. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2728. }
  2729. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2730. subgroup_size_control_features.pNext = nullptr;
  2731. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2732. subgroup_size_control_features.computeFullSubgroups = false;
  2733. subgroup_size_control_features.subgroupSizeControl = false;
  2734. if (device->subgroup_size_control) {
  2735. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2736. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2737. }
  2738. #if defined(VK_KHR_cooperative_matrix)
  2739. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2740. coopmat_features.pNext = nullptr;
  2741. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2742. coopmat_features.cooperativeMatrix = VK_FALSE;
  2743. if (device->coopmat_support) {
  2744. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2745. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2746. }
  2747. #endif
  2748. #if defined(VK_NV_cooperative_matrix2)
  2749. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2750. coopmat2_features.pNext = nullptr;
  2751. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2752. if (coopmat2_support) {
  2753. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2754. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2755. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2756. }
  2757. #endif
  2758. #if defined(VK_KHR_shader_bfloat16)
  2759. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  2760. bfloat16_features.pNext = nullptr;
  2761. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  2762. if (bfloat16_support) {
  2763. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  2764. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  2765. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2766. }
  2767. #endif
  2768. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2769. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2770. if (maintenance4_support) {
  2771. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2772. last_struct = (VkBaseOutStructure *)&maint4_features;
  2773. device_extensions.push_back("VK_KHR_maintenance4");
  2774. }
  2775. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2776. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2777. if (device->integer_dot_product) {
  2778. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2779. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2780. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  2781. }
  2782. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2783. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2784. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2785. if (device->subgroup_size_control) {
  2786. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2787. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2788. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2789. }
  2790. device->subgroup_size_control = device->subgroup_size_control &&
  2791. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2792. subgroup_size_control_features.subgroupSizeControl;
  2793. if (device->subgroup_size_control) {
  2794. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2795. }
  2796. #if defined(VK_KHR_cooperative_matrix)
  2797. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2798. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  2799. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  2800. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  2801. device->subgroup_max_size >= 32;
  2802. #endif
  2803. if (coopmat2_support) {
  2804. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2805. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2806. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2807. coopmat2_features.cooperativeMatrixReductions &&
  2808. coopmat2_features.cooperativeMatrixConversions &&
  2809. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2810. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2811. coopmat2_features.cooperativeMatrixBlockLoads &&
  2812. vk12_features.bufferDeviceAddress) {
  2813. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2814. uint32_t count = 0;
  2815. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2816. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2817. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2818. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2819. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2820. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2821. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2822. flexible_dimensions.resize(count, empty_prop);
  2823. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2824. bool found_fp16_128 = false,
  2825. found_fp16_256 = false,
  2826. found_fp32_128 = false,
  2827. found_fp32_256 = false;
  2828. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2829. // with 32x16x16 and 256 with 32x32x16.
  2830. for (auto &prop : flexible_dimensions) {
  2831. if (prop.saturatingAccumulation == VK_FALSE &&
  2832. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2833. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2834. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2835. if (prop.workgroupInvocations == 128 &&
  2836. prop.MGranularity <= 32 &&
  2837. prop.NGranularity <= 16 &&
  2838. prop.KGranularity <= 16) {
  2839. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2840. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2841. found_fp16_128 = true;
  2842. }
  2843. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2844. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2845. found_fp32_128 = true;
  2846. }
  2847. }
  2848. if (prop.workgroupInvocations == 256 &&
  2849. prop.MGranularity <= 32 &&
  2850. prop.NGranularity <= 32 &&
  2851. prop.KGranularity <= 16) {
  2852. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2853. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2854. found_fp16_256 = true;
  2855. }
  2856. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2857. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2858. found_fp32_256 = true;
  2859. }
  2860. }
  2861. }
  2862. }
  2863. if (found_fp16_128 && found_fp16_256 &&
  2864. found_fp32_128 && found_fp32_256 &&
  2865. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2866. device->coopmat2 = true;
  2867. }
  2868. }
  2869. #endif
  2870. }
  2871. if (!vk11_features.storageBuffer16BitAccess) {
  2872. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2873. throw std::runtime_error("Unsupported device");
  2874. }
  2875. device_extensions.push_back("VK_KHR_16bit_storage");
  2876. #ifdef GGML_VULKAN_VALIDATE
  2877. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2878. #endif
  2879. if (device->fp16) {
  2880. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2881. }
  2882. #if defined(VK_KHR_cooperative_matrix)
  2883. if (device->coopmat_support) {
  2884. // Query supported shapes
  2885. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2886. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2887. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2888. uint32_t cm_props_num;
  2889. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2890. cm_props.resize(cm_props_num);
  2891. for (auto& prop : cm_props) {
  2892. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2893. }
  2894. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2895. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2896. for (auto& prop : cm_props) {
  2897. 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));
  2898. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2899. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2900. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2901. ) {
  2902. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2903. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2904. // coopmat sizes not set yet
  2905. if (device->coopmat_m == 0) {
  2906. device->coopmat_acc_f32_support = true;
  2907. device->coopmat_m = prop.MSize;
  2908. device->coopmat_n = prop.NSize;
  2909. device->coopmat_k = prop.KSize;
  2910. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2911. // Only enable if shape is identical
  2912. device->coopmat_acc_f32_support = true;
  2913. }
  2914. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2915. device->coopmat_support_16x16x16_f32acc = true;
  2916. }
  2917. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2918. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2919. // coopmat sizes not set yet
  2920. if (device->coopmat_m == 0) {
  2921. device->coopmat_acc_f16_support = true;
  2922. device->coopmat_m = prop.MSize;
  2923. device->coopmat_n = prop.NSize;
  2924. device->coopmat_k = prop.KSize;
  2925. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2926. // Only enable if shape is identical
  2927. device->coopmat_acc_f16_support = true;
  2928. }
  2929. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2930. device->coopmat_support_16x16x16_f16acc = true;
  2931. }
  2932. }
  2933. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  2934. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  2935. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  2936. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  2937. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  2938. device->coopmat_int_m == 0
  2939. ) {
  2940. device->coopmat_int_support = true;
  2941. device->coopmat_int_m = prop.MSize;
  2942. device->coopmat_int_n = prop.NSize;
  2943. device->coopmat_int_k = prop.KSize;
  2944. }
  2945. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2946. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2947. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2948. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2949. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2950. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2951. ) {
  2952. // coopmat sizes not set yet
  2953. if (device->coopmat_m == 0) {
  2954. device->coopmat_bf16_support = true;
  2955. device->coopmat_m = prop.MSize;
  2956. device->coopmat_n = prop.NSize;
  2957. device->coopmat_k = prop.KSize;
  2958. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2959. // Only enable if shape is identical
  2960. device->coopmat_bf16_support = true;
  2961. }
  2962. }
  2963. #endif
  2964. }
  2965. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2966. // No suitable matmul mode found
  2967. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2968. device->coopmat_support = false;
  2969. }
  2970. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2971. device->coopmat_bf16_support = false;
  2972. }
  2973. }
  2974. if (device->coopmat_support) {
  2975. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2976. }
  2977. #if defined(VK_KHR_shader_bfloat16)
  2978. if (device->coopmat_bf16_support) {
  2979. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2980. }
  2981. #endif
  2982. #endif
  2983. device->name = GGML_VK_NAME + std::to_string(idx);
  2984. device_create_info = {
  2985. vk::DeviceCreateFlags(),
  2986. device_queue_create_infos,
  2987. {},
  2988. device_extensions
  2989. };
  2990. device_create_info.setPNext(&device_features2);
  2991. device->device = device->physical_device.createDevice(device_create_info);
  2992. // Queues
  2993. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2994. // Shaders
  2995. // Disable matmul tile sizes early if performance low or not supported
  2996. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2997. switch (device->vendor_id) {
  2998. #ifndef GGML_VULKAN_RUN_TESTS
  2999. case VK_VENDOR_ID_AMD:
  3000. case VK_VENDOR_ID_INTEL:
  3001. device->mul_mat_l[i] = false;
  3002. device->mul_mat_m[i] = true;
  3003. device->mul_mat_s[i] = true;
  3004. device->mul_mat_id_l[i] = false;
  3005. device->mul_mat_id_m[i] = true;
  3006. device->mul_mat_id_s[i] = true;
  3007. break;
  3008. case VK_VENDOR_ID_APPLE:
  3009. device->mul_mat_l[i] = false;
  3010. device->mul_mat_m[i] = true;
  3011. device->mul_mat_s[i] = false;
  3012. device->mul_mat_id_l[i] = false;
  3013. device->mul_mat_id_m[i] = true;
  3014. device->mul_mat_id_s[i] = false;
  3015. break;
  3016. #endif
  3017. default:
  3018. device->mul_mat_l[i] = true;
  3019. device->mul_mat_m[i] = true;
  3020. device->mul_mat_s[i] = true;
  3021. device->mul_mat_id_l[i] = true;
  3022. device->mul_mat_id_m[i] = true;
  3023. device->mul_mat_id_s[i] = true;
  3024. break;
  3025. }
  3026. }
  3027. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3028. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3029. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3030. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3031. dsl_binding_flags.push_back({});
  3032. }
  3033. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3034. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3035. {},
  3036. dsl_binding);
  3037. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3038. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3039. ggml_vk_load_shaders(device);
  3040. if (!device->single_queue) {
  3041. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3042. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3043. } else {
  3044. // TODO: Use pointer or reference to avoid copy
  3045. device->transfer_queue.copyFrom(device->compute_queue);
  3046. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3047. }
  3048. device->buffer_type = {
  3049. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3050. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3051. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3052. };
  3053. device->fence = device->device.createFence({});
  3054. device->idx = idx;
  3055. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3056. return device;
  3057. }
  3058. return vk_instance.devices[idx];
  3059. }
  3060. static void ggml_vk_print_gpu_info(size_t idx) {
  3061. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3062. size_t dev_num = vk_instance.device_indices[idx];
  3063. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3064. GGML_ASSERT(vk_instance_initialized);
  3065. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3066. if (dev_num >= devices.size()) {
  3067. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3068. throw std::runtime_error("Device not found");
  3069. }
  3070. vk::PhysicalDevice physical_device = devices[dev_num];
  3071. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3072. bool fp16_storage = false;
  3073. bool fp16_compute = false;
  3074. bool coopmat_support = false;
  3075. bool coopmat2_support = false;
  3076. bool integer_dot_product = false;
  3077. for (auto properties : ext_props) {
  3078. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3079. fp16_storage = true;
  3080. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3081. fp16_compute = true;
  3082. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3083. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3084. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3085. coopmat_support = true;
  3086. #endif
  3087. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3088. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3089. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3090. coopmat2_support = true;
  3091. #endif
  3092. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3093. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3094. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3095. integer_dot_product = true;
  3096. #endif
  3097. }
  3098. }
  3099. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3100. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3101. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3102. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3103. vk::PhysicalDeviceProperties2 props2;
  3104. vk::PhysicalDeviceMaintenance3Properties props3;
  3105. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3106. vk::PhysicalDeviceDriverProperties driver_props;
  3107. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3108. props2.pNext = &props3;
  3109. props3.pNext = &subgroup_props;
  3110. subgroup_props.pNext = &driver_props;
  3111. // Pointer to the last chain element
  3112. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3113. if (integer_dot_product) {
  3114. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3115. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3116. }
  3117. physical_device.getProperties2(&props2);
  3118. VkPhysicalDeviceFeatures2 device_features2;
  3119. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3120. device_features2.pNext = nullptr;
  3121. VkPhysicalDeviceVulkan11Features vk11_features;
  3122. vk11_features.pNext = nullptr;
  3123. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3124. device_features2.pNext = &vk11_features;
  3125. VkPhysicalDeviceVulkan12Features vk12_features;
  3126. vk12_features.pNext = nullptr;
  3127. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3128. vk11_features.pNext = &vk12_features;
  3129. // Pointer to the last chain element
  3130. last_struct = (VkBaseOutStructure *)&vk12_features;
  3131. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3132. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3133. coopmat_features.pNext = nullptr;
  3134. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3135. coopmat_features.cooperativeMatrix = VK_FALSE;
  3136. if (coopmat_support) {
  3137. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3138. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3139. }
  3140. #endif
  3141. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3142. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3143. if (integer_dot_product) {
  3144. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3145. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3146. }
  3147. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3148. fp16 = fp16 && vk12_features.shaderFloat16;
  3149. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3150. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3151. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3152. integer_dot_product = integer_dot_product
  3153. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3154. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3155. coopmat_support = coopmat_support
  3156. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3157. && coopmat_features.cooperativeMatrix
  3158. #endif
  3159. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3160. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3161. std::string device_name = props2.properties.deviceName.data();
  3162. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  3163. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  3164. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3165. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3166. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3167. }
  3168. }
  3169. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3170. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3171. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3172. static void ggml_vk_instance_init() {
  3173. if (vk_instance_initialized) {
  3174. return;
  3175. }
  3176. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3177. uint32_t api_version = vk::enumerateInstanceVersion();
  3178. if (api_version < VK_API_VERSION_1_2) {
  3179. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3180. GGML_ABORT("fatal error");
  3181. }
  3182. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3183. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3184. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3185. #ifdef __APPLE__
  3186. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3187. #endif
  3188. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3189. std::vector<const char*> layers;
  3190. if (validation_ext) {
  3191. layers.push_back("VK_LAYER_KHRONOS_validation");
  3192. }
  3193. std::vector<const char*> extensions;
  3194. if (validation_ext) {
  3195. extensions.push_back("VK_EXT_validation_features");
  3196. }
  3197. #ifdef __APPLE__
  3198. if (portability_enumeration_ext) {
  3199. extensions.push_back("VK_KHR_portability_enumeration");
  3200. }
  3201. #endif
  3202. if (debug_utils_ext) {
  3203. extensions.push_back("VK_EXT_debug_utils");
  3204. }
  3205. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3206. #ifdef __APPLE__
  3207. if (portability_enumeration_ext) {
  3208. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3209. }
  3210. #endif
  3211. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3212. vk::ValidationFeaturesEXT validation_features;
  3213. if (validation_ext) {
  3214. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3215. validation_features = {
  3216. features_enable,
  3217. {},
  3218. };
  3219. validation_features.setPNext(nullptr);
  3220. instance_create_info.setPNext(&validation_features);
  3221. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3222. }
  3223. vk_instance.instance = vk::createInstance(instance_create_info);
  3224. vk_instance_initialized = true;
  3225. if (debug_utils_ext) {
  3226. vk_instance.debug_utils_support = true;
  3227. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3228. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3229. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3230. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3231. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3232. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3233. }
  3234. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3235. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3236. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3237. if (devices_env != nullptr) {
  3238. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  3239. std::string devices(devices_env);
  3240. std::replace(devices.begin(), devices.end(), ',', ' ');
  3241. std::stringstream ss(devices);
  3242. size_t tmp;
  3243. while (ss >> tmp) {
  3244. if(tmp >= num_available_devices) {
  3245. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3246. throw std::runtime_error("Invalid Vulkan device index");
  3247. }
  3248. vk_instance.device_indices.push_back(tmp);
  3249. }
  3250. } else {
  3251. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3252. // If no vulkan devices are found, return early
  3253. if (devices.empty()) {
  3254. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3255. return;
  3256. }
  3257. // Default to using all dedicated GPUs
  3258. for (size_t i = 0; i < devices.size(); i++) {
  3259. vk::PhysicalDeviceProperties2 new_props;
  3260. vk::PhysicalDeviceDriverProperties new_driver;
  3261. vk::PhysicalDeviceIDProperties new_id;
  3262. new_props.pNext = &new_driver;
  3263. new_driver.pNext = &new_id;
  3264. devices[i].getProperties2(&new_props);
  3265. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3266. // Check if there are two physical devices corresponding to the same GPU
  3267. auto old_device = std::find_if(
  3268. vk_instance.device_indices.begin(),
  3269. vk_instance.device_indices.end(),
  3270. [&devices, &new_id](const size_t k){
  3271. vk::PhysicalDeviceProperties2 old_props;
  3272. vk::PhysicalDeviceIDProperties old_id;
  3273. old_props.pNext = &old_id;
  3274. devices[k].getProperties2(&old_props);
  3275. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3276. }
  3277. );
  3278. if (old_device == vk_instance.device_indices.end()) {
  3279. vk_instance.device_indices.push_back(i);
  3280. } else {
  3281. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3282. // This can cause error when splitting layers aross the devices, need to keep only 1
  3283. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3284. vk::PhysicalDeviceProperties2 old_props;
  3285. vk::PhysicalDeviceDriverProperties old_driver;
  3286. old_props.pNext = &old_driver;
  3287. devices[*old_device].getProperties2(&old_props);
  3288. std::map<vk::DriverId, int> driver_priorities {};
  3289. int old_priority = std::numeric_limits<int>::max();
  3290. int new_priority = std::numeric_limits<int>::max();
  3291. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3292. // Smaller number -> higher priority
  3293. switch (old_props.properties.vendorID) {
  3294. case VK_VENDOR_ID_AMD:
  3295. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3296. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3297. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3298. break;
  3299. case VK_VENDOR_ID_INTEL:
  3300. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3301. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3302. break;
  3303. case VK_VENDOR_ID_NVIDIA:
  3304. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3305. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3306. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3307. #endif
  3308. break;
  3309. }
  3310. if (driver_priorities.count(old_driver.driverID)) {
  3311. old_priority = driver_priorities[old_driver.driverID];
  3312. }
  3313. if (driver_priorities.count(new_driver.driverID)) {
  3314. new_priority = driver_priorities[new_driver.driverID];
  3315. }
  3316. if (new_priority < old_priority) {
  3317. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3318. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3319. vk_instance.device_indices.push_back(i);
  3320. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3321. }
  3322. else {
  3323. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3324. }
  3325. }
  3326. }
  3327. }
  3328. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3329. // If only CPU devices are available, return without devices.
  3330. if (vk_instance.device_indices.empty()) {
  3331. for (size_t i = 0; i < devices.size(); i++) {
  3332. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3333. vk_instance.device_indices.push_back(i);
  3334. break;
  3335. }
  3336. }
  3337. }
  3338. if (vk_instance.device_indices.empty()) {
  3339. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3340. return;
  3341. }
  3342. }
  3343. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3344. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3345. ggml_vk_print_gpu_info(i);
  3346. }
  3347. }
  3348. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3349. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3350. ggml_vk_instance_init();
  3351. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3352. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3353. ctx->device = ggml_vk_get_device(idx);
  3354. ctx->semaphore_idx = 0;
  3355. ctx->event_idx = 0;
  3356. ctx->prealloc_size_x = 0;
  3357. ctx->prealloc_size_y = 0;
  3358. ctx->prealloc_size_split_k = 0;
  3359. ctx->fence = ctx->device->device.createFence({});
  3360. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3361. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3362. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3363. #ifdef GGML_VULKAN_CHECK_RESULTS
  3364. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3365. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3366. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3367. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3368. #endif
  3369. }
  3370. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3371. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3372. switch (type) {
  3373. case GGML_TYPE_F32:
  3374. case GGML_TYPE_Q4_0:
  3375. case GGML_TYPE_Q4_1:
  3376. case GGML_TYPE_Q5_0:
  3377. case GGML_TYPE_Q5_1:
  3378. case GGML_TYPE_Q8_0:
  3379. case GGML_TYPE_Q2_K:
  3380. case GGML_TYPE_Q3_K:
  3381. case GGML_TYPE_Q4_K:
  3382. case GGML_TYPE_Q5_K:
  3383. case GGML_TYPE_Q6_K:
  3384. case GGML_TYPE_IQ1_S:
  3385. case GGML_TYPE_IQ1_M:
  3386. case GGML_TYPE_IQ2_XXS:
  3387. case GGML_TYPE_IQ2_XS:
  3388. case GGML_TYPE_IQ2_S:
  3389. case GGML_TYPE_IQ3_XXS:
  3390. case GGML_TYPE_IQ3_S:
  3391. case GGML_TYPE_IQ4_XS:
  3392. case GGML_TYPE_IQ4_NL:
  3393. break;
  3394. default:
  3395. return nullptr;
  3396. }
  3397. return ctx->device->pipeline_dequant[type];
  3398. }
  3399. 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) {
  3400. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3401. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3402. return ctx->device->pipeline_matmul_f32;
  3403. }
  3404. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3405. return ctx->device->pipeline_matmul_f32_f16;
  3406. }
  3407. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3408. return ctx->device->pipeline_matmul_bf16;
  3409. }
  3410. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3411. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3412. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3413. }
  3414. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3415. return ctx->device->pipeline_matmul_f16.f16acc;
  3416. }
  3417. } else {
  3418. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3419. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3420. }
  3421. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3422. return ctx->device->pipeline_matmul_f16.f32acc;
  3423. }
  3424. }
  3425. // MMQ
  3426. if (src1_type == GGML_TYPE_Q8_1) {
  3427. 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;
  3428. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3429. return nullptr;
  3430. }
  3431. return pipelines;
  3432. }
  3433. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3434. return nullptr;
  3435. }
  3436. switch (src0_type) {
  3437. case GGML_TYPE_Q4_0:
  3438. case GGML_TYPE_Q4_1:
  3439. case GGML_TYPE_Q5_0:
  3440. case GGML_TYPE_Q5_1:
  3441. case GGML_TYPE_Q8_0:
  3442. case GGML_TYPE_Q2_K:
  3443. case GGML_TYPE_Q3_K:
  3444. case GGML_TYPE_Q4_K:
  3445. case GGML_TYPE_Q5_K:
  3446. case GGML_TYPE_Q6_K:
  3447. case GGML_TYPE_IQ1_S:
  3448. case GGML_TYPE_IQ1_M:
  3449. case GGML_TYPE_IQ2_XXS:
  3450. case GGML_TYPE_IQ2_XS:
  3451. case GGML_TYPE_IQ2_S:
  3452. case GGML_TYPE_IQ3_XXS:
  3453. case GGML_TYPE_IQ3_S:
  3454. case GGML_TYPE_IQ4_XS:
  3455. case GGML_TYPE_IQ4_NL:
  3456. break;
  3457. default:
  3458. return nullptr;
  3459. }
  3460. if (ctx->device->coopmat2) {
  3461. assert(src1_type == GGML_TYPE_F16);
  3462. 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;
  3463. }
  3464. if (ctx->device->coopmat_support) {
  3465. 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;
  3466. }
  3467. 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;
  3468. }
  3469. 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) {
  3470. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3471. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3472. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3473. switch (a_type) {
  3474. case GGML_TYPE_F32:
  3475. case GGML_TYPE_F16:
  3476. case GGML_TYPE_BF16:
  3477. case GGML_TYPE_Q4_0:
  3478. case GGML_TYPE_Q4_1:
  3479. case GGML_TYPE_Q5_0:
  3480. case GGML_TYPE_Q5_1:
  3481. case GGML_TYPE_Q8_0:
  3482. case GGML_TYPE_Q2_K:
  3483. case GGML_TYPE_Q3_K:
  3484. case GGML_TYPE_Q4_K:
  3485. case GGML_TYPE_Q5_K:
  3486. case GGML_TYPE_Q6_K:
  3487. case GGML_TYPE_IQ1_S:
  3488. case GGML_TYPE_IQ1_M:
  3489. case GGML_TYPE_IQ2_XXS:
  3490. case GGML_TYPE_IQ2_XS:
  3491. case GGML_TYPE_IQ2_S:
  3492. case GGML_TYPE_IQ3_XXS:
  3493. case GGML_TYPE_IQ3_S:
  3494. case GGML_TYPE_IQ4_XS:
  3495. case GGML_TYPE_IQ4_NL:
  3496. break;
  3497. default:
  3498. return nullptr;
  3499. }
  3500. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  3501. }
  3502. 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) {
  3503. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3504. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3505. return ctx->device->pipeline_matmul_id_f32;
  3506. }
  3507. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3508. return ctx->device->pipeline_matmul_id_bf16;
  3509. }
  3510. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3511. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3512. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3513. }
  3514. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3515. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3516. }
  3517. } else {
  3518. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3519. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3520. }
  3521. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3522. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3523. }
  3524. }
  3525. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3526. switch (src0_type) {
  3527. case GGML_TYPE_Q4_0:
  3528. case GGML_TYPE_Q4_1:
  3529. case GGML_TYPE_Q5_0:
  3530. case GGML_TYPE_Q5_1:
  3531. case GGML_TYPE_Q8_0:
  3532. case GGML_TYPE_Q2_K:
  3533. case GGML_TYPE_Q3_K:
  3534. case GGML_TYPE_Q4_K:
  3535. case GGML_TYPE_Q5_K:
  3536. case GGML_TYPE_Q6_K:
  3537. case GGML_TYPE_IQ1_S:
  3538. case GGML_TYPE_IQ1_M:
  3539. case GGML_TYPE_IQ2_XXS:
  3540. case GGML_TYPE_IQ2_XS:
  3541. case GGML_TYPE_IQ2_S:
  3542. case GGML_TYPE_IQ3_XXS:
  3543. case GGML_TYPE_IQ3_S:
  3544. case GGML_TYPE_IQ4_XS:
  3545. case GGML_TYPE_IQ4_NL:
  3546. break;
  3547. default:
  3548. return nullptr;
  3549. }
  3550. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3551. }
  3552. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3553. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3554. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3555. switch (a_type) {
  3556. case GGML_TYPE_F32:
  3557. case GGML_TYPE_F16:
  3558. case GGML_TYPE_BF16:
  3559. case GGML_TYPE_Q4_0:
  3560. case GGML_TYPE_Q4_1:
  3561. case GGML_TYPE_Q5_0:
  3562. case GGML_TYPE_Q5_1:
  3563. case GGML_TYPE_Q8_0:
  3564. case GGML_TYPE_Q2_K:
  3565. case GGML_TYPE_Q3_K:
  3566. case GGML_TYPE_Q4_K:
  3567. case GGML_TYPE_Q5_K:
  3568. case GGML_TYPE_Q6_K:
  3569. case GGML_TYPE_IQ1_S:
  3570. case GGML_TYPE_IQ1_M:
  3571. case GGML_TYPE_IQ2_XXS:
  3572. case GGML_TYPE_IQ2_XS:
  3573. case GGML_TYPE_IQ2_S:
  3574. case GGML_TYPE_IQ3_XXS:
  3575. case GGML_TYPE_IQ3_S:
  3576. case GGML_TYPE_IQ4_XS:
  3577. case GGML_TYPE_IQ4_NL:
  3578. break;
  3579. default:
  3580. return nullptr;
  3581. }
  3582. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3583. }
  3584. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3585. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3586. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3587. int best_i = -1;
  3588. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3589. int worst_i = -1;
  3590. size_t worst_size = 0; //largest unused buffer seen so far
  3591. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3592. vk_buffer &b = ctx->buffer_pool[i];
  3593. if (b != nullptr && b->size >= size && b->size < best_size) {
  3594. best_i = i;
  3595. best_size = b->size;
  3596. }
  3597. if (b != nullptr && b->size > worst_size) {
  3598. worst_i = i;
  3599. worst_size = b->size;
  3600. }
  3601. }
  3602. if(best_i != -1) {
  3603. //found the smallest buffer that fits our needs
  3604. vk_buffer b = ctx->buffer_pool[best_i];
  3605. ctx->buffer_pool[best_i].reset();
  3606. return b;
  3607. }
  3608. if(worst_i != -1) {
  3609. //no buffer that fits our needs, resize largest one to save memory
  3610. vk_buffer& b = ctx->buffer_pool[worst_i];
  3611. ggml_vk_destroy_buffer(b);
  3612. }
  3613. return ggml_vk_create_buffer_device(ctx->device, size);
  3614. }
  3615. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3616. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3617. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3618. vk_buffer& b = ctx->buffer_pool[i];
  3619. if (b == nullptr) {
  3620. b = buffer;
  3621. return;
  3622. }
  3623. }
  3624. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3625. ggml_vk_destroy_buffer(buffer);
  3626. }
  3627. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3628. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3629. // Try to find existing temp buffer with enough capacity
  3630. for (auto& buffer : ctx->gc.temp_buffers) {
  3631. if (buffer->size >= size) {
  3632. return buffer;
  3633. }
  3634. }
  3635. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3636. // Otherwise create new buffer
  3637. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3638. ctx->gc.temp_buffers.push_back(buf);
  3639. return buf;
  3640. }
  3641. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3642. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3643. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3644. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3645. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3646. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3647. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3648. size/1024.0/1024.0);
  3649. device->device.freeMemory(buf->device_memory);
  3650. device->device.destroyBuffer(buf->buffer);
  3651. return nullptr;
  3652. }
  3653. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3654. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3655. return buf->ptr;
  3656. }
  3657. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3658. if (ptr == nullptr) {
  3659. return;
  3660. }
  3661. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3662. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3663. vk_buffer buf;
  3664. size_t index;
  3665. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3666. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3667. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3668. if (ptr >= addr && ptr < endr) {
  3669. buf = std::get<2>(device->pinned_memory[i]);
  3670. index = i;
  3671. break;
  3672. }
  3673. }
  3674. if (buf == nullptr) {
  3675. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3676. return;
  3677. }
  3678. ggml_vk_destroy_buffer(buf);
  3679. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3680. }
  3681. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3682. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3683. buf = nullptr;
  3684. buf_offset = 0;
  3685. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3686. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3687. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3688. if (ptr >= addr && ptr < endr) {
  3689. buf = std::get<2>(device->pinned_memory[i]);
  3690. buf_offset = ((const uint8_t *)ptr) - addr;
  3691. break;
  3692. }
  3693. }
  3694. }
  3695. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  3696. vk_submission s;
  3697. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  3698. if (one_time) {
  3699. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3700. } else {
  3701. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3702. }
  3703. return s;
  3704. }
  3705. template <typename T> size_t push_constant_size(const T &t) {
  3706. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3707. GGML_UNUSED(t);
  3708. return sizeof(T);
  3709. }
  3710. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  3711. GGML_UNUSED(t);
  3712. return sizeof(T) * t.size();
  3713. }
  3714. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  3715. GGML_UNUSED(t);
  3716. return sizeof(T) * N;
  3717. }
  3718. template <typename T> const T *push_constant_data(const T &t) {
  3719. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3720. return &t;
  3721. }
  3722. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  3723. return t.data();
  3724. }
  3725. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  3726. return t.data();
  3727. }
  3728. template <typename T>
  3729. 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) {
  3730. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3731. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3732. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3733. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3734. for (auto& buffer : descriptor_buffer_infos) {
  3735. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3736. }
  3737. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3738. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  3739. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  3740. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  3741. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3742. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3743. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  3744. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3745. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3746. pipeline->layout,
  3747. 0,
  3748. { descriptor_set },
  3749. {});
  3750. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3751. }
  3752. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3753. s.buffer.end();
  3754. s.wait_semaphores = std::move(wait_semaphores);
  3755. s.signal_semaphores = std::move(signal_semaphores);
  3756. }
  3757. static void ggml_vk_ctx_end(vk_context& ctx) {
  3758. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3759. if (ctx->s == nullptr) {
  3760. return;
  3761. }
  3762. ctx->s->buffer.end();
  3763. ctx->s = nullptr;
  3764. }
  3765. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3766. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3767. if (subctx->s != nullptr) {
  3768. ggml_vk_ctx_end(subctx);
  3769. }
  3770. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  3771. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3772. }
  3773. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3774. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3775. return CEIL_DIV(width, align) * align;
  3776. }
  3777. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3778. if (memcpys == nullptr) {
  3779. memcpy(dst, src, size);
  3780. } else {
  3781. memcpys->emplace_back(dst, src, size);
  3782. }
  3783. }
  3784. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3785. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3786. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3787. ggml_vk_destroy_buffer(device->sync_staging);
  3788. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3789. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3790. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3791. }
  3792. }
  3793. 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) {
  3794. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3795. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3796. // Buffer is already mapped
  3797. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3798. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3799. GGML_ABORT("fatal error");
  3800. }
  3801. // Check if src is pinned memory
  3802. vk_buffer buf = nullptr;
  3803. size_t buf_offset = 0;
  3804. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3805. const uint64_t ne0 = tensor->ne[0];
  3806. const uint64_t ne1 = tensor->ne[1];
  3807. const uint64_t ne2 = tensor->ne[2];
  3808. const uint64_t ne3 = tensor->ne[3];
  3809. const uint64_t nb0 = tensor->nb[0];
  3810. const uint64_t nb1 = tensor->nb[1];
  3811. const uint64_t nb2 = tensor->nb[2];
  3812. const uint64_t nb3 = tensor->nb[3];
  3813. const ggml_type type = tensor->type;
  3814. const uint64_t ts = ggml_type_size(type);
  3815. const uint64_t bs = ggml_blck_size(type);
  3816. const uint64_t dstnb0 = ts;
  3817. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3818. const uint64_t dstnb2 = dstnb1*ne1;
  3819. const uint64_t dstnb3 = dstnb2*ne2;
  3820. const uint64_t ne = ggml_nelements(tensor);
  3821. if (buf != nullptr) {
  3822. // Memory is pinned, use as staging buffer
  3823. std::vector<vk::BufferCopy> slices;
  3824. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3825. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3826. // Find longest contiguous slice
  3827. if (ne1*nb1 == dstnb2) {
  3828. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3829. } else {
  3830. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3831. if (ne0*nb0/bs == dstnb1) {
  3832. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3833. } else {
  3834. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3835. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3836. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3837. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3838. }
  3839. }
  3840. }
  3841. }
  3842. }
  3843. }
  3844. ggml_vk_sync_buffers(subctx);
  3845. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3846. return;
  3847. }
  3848. if (!sync_staging) {
  3849. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3850. }
  3851. // Staging buffer required
  3852. vk_buffer& staging = ctx->device->sync_staging;
  3853. const uint64_t copy_size = ts*ne/bs;
  3854. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3855. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3856. ggml_vk_sync_buffers(subctx);
  3857. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3858. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3859. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3860. // Find longest contiguous slice
  3861. if (ne1*nb1 == dstnb2) {
  3862. 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);
  3863. } else {
  3864. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3865. if (ne0*nb0/bs == dstnb1) {
  3866. 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);
  3867. } else {
  3868. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3869. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3870. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3871. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3872. }
  3873. }
  3874. }
  3875. }
  3876. }
  3877. }
  3878. }
  3879. 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) {
  3880. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3881. // Buffer is already mapped
  3882. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3883. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3884. GGML_ABORT("fatal error");
  3885. }
  3886. // Check if src is pinned memory
  3887. vk_buffer buf = nullptr;
  3888. size_t buf_offset = 0;
  3889. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3890. if (buf != nullptr) {
  3891. // Memory is pinned, use as staging buffer
  3892. std::vector<vk::BufferCopy> slices(1);
  3893. if (width == spitch) {
  3894. // Only do single write if stride is equal
  3895. slices[0].srcOffset = buf_offset;
  3896. slices[0].dstOffset = offset;
  3897. slices[0].size = width * height;
  3898. } else {
  3899. slices.resize(height);
  3900. for (size_t i = 0; i < height; i++) {
  3901. slices[i].srcOffset = buf_offset + i * spitch;
  3902. slices[i].dstOffset = offset + i * width;
  3903. slices[i].size = width;
  3904. }
  3905. }
  3906. ggml_vk_sync_buffers(subctx);
  3907. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3908. return;
  3909. }
  3910. VK_LOG_DEBUG("STAGING");
  3911. if (!sync_staging) {
  3912. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3913. }
  3914. // Staging buffer required
  3915. const size_t copy_size = width*height;
  3916. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3917. vk_buffer& staging_buffer = dst->device->sync_staging;
  3918. VkBufferCopy buf_copy = {
  3919. 0,
  3920. offset,
  3921. copy_size};
  3922. ggml_vk_sync_buffers(subctx);
  3923. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3924. if (width == spitch) {
  3925. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3926. } else {
  3927. for (size_t i = 0; i < height; i++) {
  3928. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3929. }
  3930. }
  3931. }
  3932. 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) {
  3933. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3934. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3935. }
  3936. 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) {
  3937. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3938. // Buffer is already mapped
  3939. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3940. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3941. for (size_t i = 0; i < height; i++) {
  3942. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3943. }
  3944. } else {
  3945. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  3946. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  3947. ggml_vk_ctx_begin(dst->device, subctx);
  3948. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3949. ggml_vk_ctx_end(subctx);
  3950. for (auto& cpy : subctx->in_memcpys) {
  3951. memcpy(cpy.dst, cpy.src, cpy.n);
  3952. }
  3953. ggml_vk_submit(subctx, dst->device->fence);
  3954. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3955. dst->device->device.resetFences({ dst->device->fence });
  3956. ggml_vk_queue_command_pools_cleanup(dst->device);
  3957. }
  3958. }
  3959. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3960. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3961. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3962. }
  3963. 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) {
  3964. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3965. GGML_ASSERT(width > 0);
  3966. GGML_ASSERT(height > 0);
  3967. GGML_ASSERT(src != nullptr);
  3968. // TODO: staging_offset is not used
  3969. // Check if dst is pinned memory
  3970. vk_buffer buf = nullptr;
  3971. size_t buf_offset = 0;
  3972. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3973. std::vector<vk::BufferCopy> slices(1);
  3974. if (width == spitch && width == dpitch) {
  3975. // Only do single write if stride is equal
  3976. slices[0].srcOffset = offset;
  3977. slices[0].dstOffset = buf_offset;
  3978. slices[0].size = width * height;
  3979. } else {
  3980. slices.resize(height);
  3981. for (size_t i = 0; i < height; i++) {
  3982. slices[i].srcOffset = offset + i * spitch;
  3983. slices[i].dstOffset = buf_offset + i * dpitch;
  3984. slices[i].size = width;
  3985. }
  3986. }
  3987. if (buf != nullptr) {
  3988. // Memory is pinned, use as staging buffer
  3989. ggml_vk_sync_buffers(subctx);
  3990. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3991. return;
  3992. }
  3993. VK_LOG_DEBUG("STAGING");
  3994. if (!sync_staging) {
  3995. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3996. }
  3997. // Fall back to staging buffer
  3998. const size_t copy_size = dpitch * height;
  3999. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4000. vk_buffer& staging_buffer = src->device->sync_staging;
  4001. ggml_vk_sync_buffers(subctx);
  4002. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4003. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4004. }
  4005. 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) {
  4006. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4007. }
  4008. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4009. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4010. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4011. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4012. // the HW device to host copy path.
  4013. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4014. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4015. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4016. } else {
  4017. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4018. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4019. ggml_vk_ctx_begin(src->device, subctx);
  4020. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4021. ggml_vk_ctx_end(subctx);
  4022. ggml_vk_submit(subctx, src->device->fence);
  4023. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4024. src->device->device.resetFences({ src->device->fence });
  4025. ggml_vk_queue_command_pools_cleanup(src->device);
  4026. for (auto& cpy : subctx->out_memcpys) {
  4027. memcpy(cpy.dst, cpy.src, cpy.n);
  4028. }
  4029. }
  4030. }
  4031. 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) {
  4032. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4033. // Make sure both buffers are on same device
  4034. GGML_ASSERT(src->device == dst->device);
  4035. VkBufferCopy bc{ src_offset, dst_offset, size };
  4036. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4037. }
  4038. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4039. if (src->device == dst->device) {
  4040. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4041. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4042. // Copy within the device
  4043. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4044. ggml_vk_ctx_begin(src->device, subctx);
  4045. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4046. ggml_vk_ctx_end(subctx);
  4047. ggml_vk_submit(subctx, src->device->fence);
  4048. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4049. src->device->device.resetFences({ src->device->fence });
  4050. ggml_vk_queue_command_pools_cleanup(src->device);
  4051. } else {
  4052. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4053. // Copy device to device
  4054. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4055. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4056. // Copy to src staging buffer
  4057. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4058. // memcpy to dst staging buffer
  4059. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4060. // Copy to dst buffer
  4061. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4062. }
  4063. }
  4064. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4065. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4066. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4067. }
  4068. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4069. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4070. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4071. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4072. ggml_vk_ctx_begin(dst->device, subctx);
  4073. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4074. ggml_vk_ctx_end(subctx);
  4075. ggml_vk_submit(subctx, dst->device->fence);
  4076. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4077. dst->device->device.resetFences({ dst->device->fence });
  4078. ggml_vk_queue_command_pools_cleanup(dst->device);
  4079. }
  4080. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  4081. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4082. uint32_t split_k = 1;
  4083. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  4084. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4085. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4086. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4087. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  4088. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4089. // Clamp to 2 or 4
  4090. split_k = std::min(split_k, 4u);
  4091. if (split_k == 3) {
  4092. split_k = 2;
  4093. }
  4094. if (ctx->device->coopmat2) {
  4095. // coopmat2 shader expects splits to be aligned to 256
  4096. while (split_k > 1 && ((k / split_k) % 256) != 0) {
  4097. split_k /= 2;
  4098. }
  4099. }
  4100. }
  4101. }
  4102. return split_k;
  4103. }
  4104. 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) {
  4105. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4106. if (ctx->device->coopmat2) {
  4107. // Use large shader when the N dimension is greater than the medium shader's tile size
  4108. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4109. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  4110. return aligned ? mmp->a_l : mmp->l;
  4111. }
  4112. // Use medium shader when the N dimension is greater than the small shader's tile size
  4113. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4114. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4115. return aligned ? mmp->a_m : mmp->m;
  4116. }
  4117. return aligned ? mmp->a_s : mmp->s;
  4118. }
  4119. 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])) {
  4120. return aligned ? mmp->a_s : mmp->s;
  4121. }
  4122. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4123. return aligned ? mmp->a_m : mmp->m;
  4124. }
  4125. return aligned ? mmp->a_l : mmp->l;
  4126. GGML_UNUSED(src1_type);
  4127. }
  4128. 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) {
  4129. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4130. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4131. }
  4132. static void ggml_vk_matmul(
  4133. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4134. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4135. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4136. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4137. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4138. uint32_t padded_n) {
  4139. 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 << ")");
  4140. ggml_vk_sync_buffers(subctx);
  4141. if (split_k == 1) {
  4142. 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 };
  4143. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4144. return;
  4145. }
  4146. GGML_ASSERT(batch_stride_d == m * n);
  4147. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, padded_n };
  4148. // Make sure enough workgroups get assigned for split k to work
  4149. 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 });
  4150. ggml_vk_sync_buffers(subctx);
  4151. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4152. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4153. }
  4154. 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) {
  4155. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4156. if (ctx->device->coopmat2) {
  4157. // Use large shader when the N dimension is greater than the medium shader's tile size
  4158. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4159. 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])) {
  4160. return aligned ? mmp->a_l : mmp->l;
  4161. }
  4162. // Use medium shader when the N dimension is greater than the small shader's tile size
  4163. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4164. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4165. return aligned ? mmp->a_m : mmp->m;
  4166. }
  4167. return aligned ? mmp->a_s : mmp->s;
  4168. }
  4169. 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])) {
  4170. return aligned ? mmp->a_s : mmp->s;
  4171. }
  4172. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4173. return aligned ? mmp->a_m : mmp->m;
  4174. }
  4175. return aligned ? mmp->a_l : mmp->l;
  4176. }
  4177. 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) {
  4178. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4179. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4180. }
  4181. static void ggml_vk_matmul_id(
  4182. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4183. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4184. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4185. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4186. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4187. uint32_t padded_n) {
  4188. 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 << "), " <<
  4189. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4190. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4191. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4192. ggml_vk_sync_buffers(subctx);
  4193. 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,
  4194. nei0, nei1, nbi1, ne11, padded_n };
  4195. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4196. }
  4197. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4198. return
  4199. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4200. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4201. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  4202. }
  4203. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4204. // Choose "contiguous copy" shader if src/dst are contiguous
  4205. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4206. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4207. if (contig) {
  4208. return ctx->device->pipeline_contig_cpy_f32_f32;
  4209. } else {
  4210. return ctx->device->pipeline_cpy_f32_f32;
  4211. }
  4212. }
  4213. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4214. if (contig) {
  4215. return ctx->device->pipeline_contig_cpy_f32_f16;
  4216. } else {
  4217. return ctx->device->pipeline_cpy_f32_f16;
  4218. }
  4219. }
  4220. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4221. if (contig) {
  4222. return ctx->device->pipeline_contig_cpy_f16_f16;
  4223. } else {
  4224. return ctx->device->pipeline_cpy_f16_f16;
  4225. }
  4226. }
  4227. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4228. if (contig) {
  4229. return ctx->device->pipeline_contig_cpy_f16_f32;
  4230. } else {
  4231. return ctx->device->pipeline_cpy_f16_f32;
  4232. }
  4233. }
  4234. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4235. if (contig) {
  4236. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4237. } else {
  4238. return ctx->device->pipeline_cpy_f32_bf16;
  4239. }
  4240. }
  4241. if (src->type == GGML_TYPE_F32) {
  4242. switch (to) {
  4243. case GGML_TYPE_Q4_0:
  4244. case GGML_TYPE_Q4_1:
  4245. case GGML_TYPE_Q5_0:
  4246. case GGML_TYPE_Q5_1:
  4247. case GGML_TYPE_Q8_0:
  4248. case GGML_TYPE_IQ4_NL:
  4249. return ctx->device->pipeline_cpy_f32_quant[to];
  4250. default:
  4251. break;
  4252. }
  4253. }
  4254. if (to == GGML_TYPE_F32) {
  4255. switch (src->type) {
  4256. case GGML_TYPE_Q4_0:
  4257. case GGML_TYPE_Q4_1:
  4258. case GGML_TYPE_Q5_0:
  4259. case GGML_TYPE_Q5_1:
  4260. case GGML_TYPE_Q8_0:
  4261. case GGML_TYPE_IQ4_NL:
  4262. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4263. default:
  4264. break;
  4265. }
  4266. }
  4267. if (src->type == to) {
  4268. // Copy two or four bytes at a time, depending on block size.
  4269. // For quantized types, we scale by block size/type size. But
  4270. // this path is also used for bf16->bf16 for example, where the
  4271. // type size must be exactly 2 or 4.
  4272. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4273. if ((ggml_type_size(src->type) % 4) == 0) {
  4274. if (contig) {
  4275. return ctx->device->pipeline_contig_cpy_f32_f32;
  4276. } else {
  4277. return ctx->device->pipeline_cpy_f32_f32;
  4278. }
  4279. } else {
  4280. if (contig) {
  4281. return ctx->device->pipeline_contig_cpy_f16_f16;
  4282. } else {
  4283. return ctx->device->pipeline_cpy_f16_f16;
  4284. }
  4285. }
  4286. }
  4287. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4288. GGML_ABORT("fatal error");
  4289. }
  4290. 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) {
  4291. 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] << "), ";
  4292. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4293. const int tensor_type_size = ggml_type_size(tensor->type);
  4294. const uint32_t ne = ggml_nelements(tensor);
  4295. std::array<uint32_t, 3> elements;
  4296. if (ne > 262144) {
  4297. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4298. } else if (ne > 512) {
  4299. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4300. } else {
  4301. elements = { ne, 1, 1 };
  4302. }
  4303. vk_op_unary_push_constants pc = {
  4304. (uint32_t)ne,
  4305. (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,
  4306. (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]),
  4307. 0,
  4308. 0.0f, 0.0f,
  4309. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4310. };
  4311. init_pushconst_fastdiv(pc);
  4312. ggml_vk_sync_buffers(subctx);
  4313. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4314. }
  4315. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  4316. switch(type) {
  4317. case GGML_TYPE_Q8_1:
  4318. return ctx->device->pipeline_quantize_q8_1;
  4319. default:
  4320. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4321. GGML_ABORT("fatal error");
  4322. }
  4323. }
  4324. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  4325. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4326. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4327. ggml_vk_sync_buffers(subctx);
  4328. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4329. }
  4330. static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4331. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4332. 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];
  4333. 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];
  4334. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4335. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4336. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4337. const uint64_t ne00 = src0->ne[0];
  4338. const uint64_t ne01 = src0->ne[1];
  4339. const uint64_t ne02 = src0->ne[2];
  4340. const uint64_t ne03 = src0->ne[3];
  4341. const uint64_t ne10 = src1->ne[0];
  4342. const uint64_t ne11 = src1->ne[1];
  4343. const uint64_t ne12 = src1->ne[2];
  4344. const uint64_t ne13 = src1->ne[3];
  4345. const uint64_t ne20 = dst->ne[0];
  4346. const uint64_t ne21 = dst->ne[1];
  4347. const uint64_t r2 = ne12 / ne02;
  4348. const uint64_t r3 = ne13 / ne03;
  4349. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4350. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4351. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4352. vk_buffer d_Qx = nullptr;
  4353. size_t qx_buf_offset = 0;
  4354. vk_buffer d_Qy = nullptr;
  4355. size_t qy_buf_offset = 0;
  4356. bool src0_uma = false;
  4357. bool src1_uma = false;
  4358. if (ctx->device->uma) {
  4359. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4360. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4361. src0_uma = d_Qx != nullptr;
  4362. src1_uma = d_Qy != nullptr;
  4363. }
  4364. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4365. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4366. !ggml_vk_dim01_contiguous(src0);
  4367. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4368. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4369. !ggml_vk_dim01_contiguous(src1);
  4370. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4371. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4372. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4373. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4374. // Check for mmq first
  4375. 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;
  4376. if (mmp == nullptr) {
  4377. // Fall back to f16 dequant mul mat
  4378. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4379. quantize_y = false;
  4380. }
  4381. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4382. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4383. if (qx_needs_dequant) {
  4384. // Fall back to dequant + f16 mulmat
  4385. 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]);
  4386. }
  4387. // Not implemented
  4388. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4389. 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)));
  4390. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4391. 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));
  4392. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4393. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4394. const int x_ne = ne01 * ne00;
  4395. const int y_ne = padded_n * ne10;
  4396. const int d_ne = ne11 * ne01;
  4397. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4398. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4399. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4400. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4401. 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);
  4402. const uint64_t d_sz = sizeof(float) * d_ne;
  4403. vk_pipeline to_fp16_vk_0 = nullptr;
  4404. vk_pipeline to_fp16_vk_1 = nullptr;
  4405. vk_pipeline to_q8_1 = nullptr;
  4406. if (x_non_contig) {
  4407. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4408. } else {
  4409. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4410. }
  4411. if (y_non_contig) {
  4412. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4413. } else {
  4414. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4415. }
  4416. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4417. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4418. if (quantize_y) {
  4419. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4420. }
  4421. if (dryrun) {
  4422. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4423. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4424. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4425. if (
  4426. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4427. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4428. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4429. GGML_ABORT("Requested preallocation size is too large");
  4430. }
  4431. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4432. ctx->prealloc_size_x = x_sz_upd;
  4433. }
  4434. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  4435. ctx->prealloc_size_y = y_sz_upd;
  4436. }
  4437. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  4438. ctx->prealloc_size_split_k = split_k_size;
  4439. }
  4440. // Request descriptor sets
  4441. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4442. if (qx_needs_dequant) {
  4443. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4444. }
  4445. if (qy_needs_dequant) {
  4446. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4447. }
  4448. if (quantize_y) {
  4449. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  4450. }
  4451. if (split_k > 1) {
  4452. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  4453. }
  4454. return;
  4455. }
  4456. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4457. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4458. GGML_ASSERT(d_D != nullptr);
  4459. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  4460. vk_buffer d_X;
  4461. uint64_t x_buf_offset = 0;
  4462. vk_buffer d_Y;
  4463. uint64_t y_buf_offset = 0;
  4464. if (!src0_uma) {
  4465. d_Qx = src0_buf_ctx->dev_buffer;
  4466. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4467. GGML_ASSERT(d_Qx != nullptr);
  4468. }
  4469. if (!src1_uma) {
  4470. d_Qy = src1_buf_ctx->dev_buffer;
  4471. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4472. GGML_ASSERT(d_Qy != nullptr);
  4473. }
  4474. if (qx_needs_dequant) {
  4475. d_X = ctx->prealloc_x;
  4476. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4477. } else {
  4478. d_X = d_Qx;
  4479. x_buf_offset = qx_buf_offset;
  4480. GGML_ASSERT(qx_sz == x_sz);
  4481. }
  4482. if (qy_needs_dequant) {
  4483. d_Y = ctx->prealloc_y;
  4484. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4485. } else if (quantize_y) {
  4486. d_Y = ctx->prealloc_y;
  4487. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  4488. } else {
  4489. d_Y = d_Qy;
  4490. y_buf_offset = qy_buf_offset;
  4491. GGML_ASSERT(qy_sz == y_sz);
  4492. }
  4493. if (x_non_contig) {
  4494. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  4495. } else if (qx_needs_dequant) {
  4496. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4497. ggml_vk_sync_buffers(subctx);
  4498. 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});
  4499. }
  4500. if (y_non_contig) {
  4501. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  4502. }
  4503. if (quantize_y) {
  4504. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13);
  4505. }
  4506. uint32_t stride_batch_x = ne00*ne01;
  4507. uint32_t stride_batch_y = ne10*ne11;
  4508. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4509. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4510. }
  4511. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  4512. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4513. }
  4514. // compute
  4515. ggml_vk_matmul(
  4516. ctx, subctx, pipeline,
  4517. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4518. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  4519. ne01, ne11, ne10,
  4520. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  4521. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  4522. ); // NOLINT
  4523. }
  4524. 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) {
  4525. 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];
  4526. 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];
  4527. 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];
  4528. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  4529. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4530. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4531. const uint64_t ne00 = src0->ne[0];
  4532. const uint64_t ne01 = src0->ne[1];
  4533. const uint64_t ne02 = src0->ne[2];
  4534. const uint64_t ne03 = src0->ne[3];
  4535. const uint64_t ne10 = src1->ne[0];
  4536. const uint64_t ne11 = src1->ne[1];
  4537. const uint64_t ne12 = src1->ne[2];
  4538. const uint64_t ne13 = src1->ne[3];
  4539. const uint64_t ne20 = dst->ne[0];
  4540. const uint64_t ne21 = dst->ne[1];
  4541. const uint64_t ne22 = dst->ne[2];
  4542. const uint64_t ne23 = dst->ne[3];
  4543. const uint64_t r2 = ne12 / ne02;
  4544. const uint64_t r3 = ne13 / ne03;
  4545. // batch_n indicates that we need to compute a few vector results, and this assumes
  4546. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4547. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4548. bool batch_n = ne11 > 1;
  4549. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4550. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4551. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4552. vk_buffer d_Qx = nullptr;
  4553. size_t qx_buf_offset = 0;
  4554. vk_buffer d_Qy = nullptr;
  4555. size_t qy_buf_offset = 0;
  4556. bool src0_uma = false;
  4557. bool src1_uma = false;
  4558. if (ctx->device->uma) {
  4559. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4560. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4561. src0_uma = d_Qx != nullptr;
  4562. src1_uma = d_Qy != nullptr;
  4563. }
  4564. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4565. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4566. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4567. const bool qx_needs_dequant = x_non_contig;
  4568. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4569. // Not implemented
  4570. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4571. const uint64_t x_ne = ne01 * ne00;
  4572. const uint64_t y_ne = ne11 * ne10;
  4573. const uint64_t d_ne = ne11 * ne01;
  4574. 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);
  4575. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4576. 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;
  4577. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4578. const uint64_t d_sz = sizeof(float) * d_ne;
  4579. vk_pipeline to_fp16_vk_0 = nullptr;
  4580. vk_pipeline to_fp16_vk_1 = nullptr;
  4581. if (x_non_contig) {
  4582. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4583. }
  4584. if (y_non_contig) {
  4585. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4586. } else {
  4587. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4588. }
  4589. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  4590. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4591. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4592. GGML_ASSERT(dmmv != nullptr);
  4593. if (dryrun) {
  4594. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4595. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4596. if (
  4597. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4598. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4599. GGML_ABORT("Requested preallocation size is too large");
  4600. }
  4601. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4602. ctx->prealloc_size_x = x_sz_upd;
  4603. }
  4604. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4605. ctx->prealloc_size_y = y_sz_upd;
  4606. }
  4607. // Request descriptor sets
  4608. if (qx_needs_dequant) {
  4609. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4610. }
  4611. if (qy_needs_dequant) {
  4612. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4613. }
  4614. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  4615. return;
  4616. }
  4617. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4618. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4619. GGML_ASSERT(d_D != nullptr);
  4620. vk_buffer d_X;
  4621. uint64_t x_buf_offset = 0;
  4622. vk_buffer d_Y;
  4623. uint64_t y_buf_offset = 0;
  4624. if(!src0_uma) {
  4625. d_Qx = src0_buf_ctx->dev_buffer;
  4626. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4627. GGML_ASSERT(d_Qx != nullptr);
  4628. }
  4629. if(!src1_uma) {
  4630. d_Qy = src1_buf_ctx->dev_buffer;
  4631. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4632. GGML_ASSERT(d_Qy != nullptr);
  4633. }
  4634. if (qx_needs_dequant) {
  4635. d_X = ctx->prealloc_x;
  4636. } else {
  4637. d_X = d_Qx;
  4638. x_buf_offset = qx_buf_offset;
  4639. GGML_ASSERT(qx_sz == x_sz);
  4640. }
  4641. if (qy_needs_dequant) {
  4642. d_Y = ctx->prealloc_y;
  4643. } else {
  4644. d_Y = d_Qy;
  4645. y_buf_offset = qy_buf_offset;
  4646. GGML_ASSERT(qy_sz == y_sz);
  4647. }
  4648. if (x_non_contig) {
  4649. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4650. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  4651. }
  4652. if (y_non_contig) {
  4653. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4654. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  4655. }
  4656. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4657. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4658. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4659. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4660. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4661. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4662. }
  4663. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4664. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4665. }
  4666. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4667. uint32_t groups_x = ne01;
  4668. uint32_t groups_z = 1;
  4669. if (ne01 > max_groups_x) {
  4670. groups_z = 64;
  4671. groups_x = CEIL_DIV(groups_x, groups_z);
  4672. }
  4673. // compute
  4674. const vk_mat_vec_push_constants pc = {
  4675. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4676. stride_batch_x, stride_batch_y, stride_batch_d,
  4677. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4678. };
  4679. ggml_vk_sync_buffers(subctx);
  4680. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4681. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  4682. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4683. }
  4684. 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) {
  4685. 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];
  4686. 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];
  4687. 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];
  4688. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4689. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4690. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4691. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4692. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4693. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4694. const uint64_t ne00 = src0->ne[0];
  4695. const uint64_t ne01 = src0->ne[1];
  4696. const uint64_t ne02 = src0->ne[2];
  4697. // const uint64_t ne03 = src0->ne[3];
  4698. const uint64_t ne10 = src1->ne[0];
  4699. const uint64_t ne11 = src1->ne[1];
  4700. const uint64_t ne12 = src1->ne[2];
  4701. // const uint64_t ne13 = src1->ne[3];
  4702. GGML_ASSERT(ne11 == 1);
  4703. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4704. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4705. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4706. vk_buffer d_Qy = nullptr;
  4707. size_t qy_buf_offset = 0;
  4708. bool src1_uma = false;
  4709. if (ctx->device->uma) {
  4710. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4711. src1_uma = d_Qy != nullptr;
  4712. }
  4713. const uint64_t x_ne = ne00 * ne01 * ne02;
  4714. const uint64_t y_ne = ne10 * ne11 * ne12;
  4715. const uint64_t d_ne = ne01 * ne11 * ne12;
  4716. 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);
  4717. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4718. const uint64_t d_sz = sizeof(float) * d_ne;
  4719. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  4720. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  4721. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  4722. gqa_ratio = 1;
  4723. }
  4724. if (dryrun) {
  4725. // Request descriptor sets
  4726. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  4727. return;
  4728. }
  4729. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4730. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4731. GGML_ASSERT(d_D != nullptr);
  4732. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4733. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4734. GGML_ASSERT(d_Qx != nullptr);
  4735. if (!src1_uma) {
  4736. d_Qy = src1_buf_ctx->dev_buffer;
  4737. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4738. GGML_ASSERT(d_Qx != nullptr);
  4739. }
  4740. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4741. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4742. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4743. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4744. // compute
  4745. 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)) };
  4746. uint32_t workgroups_z = (uint32_t)ne12;
  4747. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  4748. if (gqa_ratio > 1) {
  4749. workgroups_z /= gqa_ratio;
  4750. }
  4751. ggml_vk_sync_buffers(subctx);
  4752. 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 });
  4753. }
  4754. 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) {
  4755. 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];
  4756. 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];
  4757. 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];
  4758. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4759. GGML_ASSERT(!ggml_is_transposed(src0));
  4760. GGML_ASSERT(!ggml_is_transposed(src1));
  4761. GGML_ASSERT(!ggml_is_permuted(src0));
  4762. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4763. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4764. const uint64_t ne00 = src0->ne[0];
  4765. const uint64_t ne01 = src0->ne[1];
  4766. const uint64_t ne02 = src0->ne[2];
  4767. // const uint64_t ne03 = src0->ne[3];
  4768. const uint64_t nb01 = src0->nb[1];
  4769. const uint64_t nb02 = src0->nb[2];
  4770. const uint64_t nb12 = src1->nb[2];
  4771. // const uint64_t ne10 = src1->ne[0];
  4772. const uint64_t ne11 = src1->ne[1];
  4773. const uint64_t ne12 = src1->ne[2];
  4774. // const uint64_t ne13 = src1->ne[3];
  4775. GGML_ASSERT(ne11 == 1);
  4776. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4777. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4778. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4779. vk_buffer d_Qy = nullptr;
  4780. size_t qy_buf_offset = 0;
  4781. bool src1_uma = false;
  4782. if (ctx->device->uma) {
  4783. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4784. src1_uma = d_Qy != nullptr;
  4785. }
  4786. const uint64_t d_ne = ne01 * ne11 * ne12;
  4787. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4788. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4789. const uint32_t channel_stride_y = nb12 / sizeof(float);
  4790. const uint64_t qx_sz = ggml_nbytes(src0);
  4791. const uint64_t qy_sz = ggml_nbytes(src1);
  4792. const uint64_t d_sz = sizeof(float) * d_ne;
  4793. if (dryrun) {
  4794. // Request descriptor sets
  4795. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4796. return;
  4797. }
  4798. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4799. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4800. GGML_ASSERT(d_D != nullptr);
  4801. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4802. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4803. GGML_ASSERT(d_Qx != nullptr);
  4804. if (!src1_uma) {
  4805. d_Qy = src1_buf_ctx->dev_buffer;
  4806. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4807. GGML_ASSERT(d_Qx != nullptr);
  4808. }
  4809. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4810. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4811. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4812. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4813. // compute
  4814. const std::array<uint32_t, 9> 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)) };
  4815. ggml_vk_sync_buffers(subctx);
  4816. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4817. { 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, (uint32_t)ne12 });
  4818. }
  4819. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4820. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4821. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4822. // detect 0213 permutation, and batch size of 1
  4823. src0->nb[0] <= src0->nb[2] &&
  4824. src0->nb[2] <= src0->nb[1] &&
  4825. src0->nb[1] <= src0->nb[3] &&
  4826. src1->nb[0] <= src1->nb[2] &&
  4827. src1->nb[2] <= src1->nb[1] &&
  4828. src1->nb[1] <= src1->nb[3] &&
  4829. src0->ne[3] == 1 &&
  4830. src1->ne[3] == 1) {
  4831. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4832. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4833. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4834. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4835. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4836. // when ne12 and ne13 are one.
  4837. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4838. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  4839. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4840. } else {
  4841. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4842. }
  4843. }
  4844. 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) {
  4845. 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];
  4846. 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];
  4847. 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];
  4848. 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] << "),)");
  4849. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4850. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4851. const uint64_t ne00 = src0->ne[0];
  4852. const uint64_t ne01 = src0->ne[1];
  4853. const uint64_t ne02 = src0->ne[2];
  4854. const uint64_t ne03 = src0->ne[3];
  4855. const uint64_t ne10 = src1->ne[0];
  4856. const uint64_t ne11 = src1->ne[1];
  4857. const uint64_t ne12 = src1->ne[2];
  4858. const uint64_t ne13 = src1->ne[3];
  4859. const uint64_t nei0 = ids->ne[0];
  4860. const uint64_t nei1 = ids->ne[1];
  4861. GGML_ASSERT(nei0 * nei1 <= 4096);
  4862. const uint32_t nbi1 = ids->nb[1];
  4863. const uint32_t nbi2 = ids->nb[2];
  4864. const uint64_t ne20 = dst->ne[0];
  4865. const uint64_t ne21 = dst->ne[1];
  4866. const uint64_t ne22 = dst->ne[2];
  4867. const uint64_t ne23 = dst->ne[3];
  4868. const uint64_t n_as = ne02;
  4869. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4870. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4871. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4872. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4873. vk_buffer d_Qx = nullptr;
  4874. size_t qx_buf_offset = 0;
  4875. vk_buffer d_Qy = nullptr;
  4876. size_t qy_buf_offset = 0;
  4877. vk_buffer d_ids = nullptr;
  4878. size_t ids_buf_offset = 0;
  4879. bool src0_uma = false;
  4880. bool src1_uma = false;
  4881. bool ids_uma = false;
  4882. if (ctx->device->uma) {
  4883. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4884. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4885. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4886. src0_uma = d_Qx != nullptr;
  4887. src1_uma = d_Qy != nullptr;
  4888. ids_uma = d_ids != nullptr;
  4889. }
  4890. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4891. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4892. !ggml_vk_dim01_contiguous(src0);
  4893. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4894. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4895. !ggml_vk_dim01_contiguous(src1);
  4896. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4897. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4898. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4899. 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]);
  4900. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4901. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  4902. if (qx_needs_dequant) {
  4903. // Fall back to dequant + f16 mulmat
  4904. 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]);
  4905. }
  4906. // Not implemented
  4907. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4908. 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));
  4909. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4910. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  4911. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4912. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4913. const uint64_t x_ne = ne01 * ne00;
  4914. const uint64_t y_ne = padded_n * ne10;
  4915. const uint64_t d_ne = ne21 * ne20;
  4916. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4917. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4918. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4919. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4920. const uint64_t ids_sz = nbi2;
  4921. const uint64_t d_sz = sizeof(float) * d_ne;
  4922. vk_pipeline to_fp16_vk_0 = nullptr;
  4923. vk_pipeline to_fp16_vk_1 = nullptr;
  4924. if (x_non_contig) {
  4925. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4926. } else {
  4927. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4928. }
  4929. if (y_non_contig) {
  4930. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4931. } else {
  4932. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4933. }
  4934. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4935. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4936. if (dryrun) {
  4937. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4938. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4939. if (
  4940. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4941. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4942. GGML_ABORT("Requested preallocation size is too large");
  4943. }
  4944. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4945. ctx->prealloc_size_x = x_sz_upd;
  4946. }
  4947. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4948. ctx->prealloc_size_y = y_sz_upd;
  4949. }
  4950. // Request descriptor sets
  4951. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4952. if (qx_needs_dequant) {
  4953. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4954. }
  4955. if (qy_needs_dequant) {
  4956. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4957. }
  4958. return;
  4959. }
  4960. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4961. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4962. GGML_ASSERT(d_D != nullptr);
  4963. vk_buffer d_X;
  4964. uint64_t x_buf_offset = 0;
  4965. vk_buffer d_Y;
  4966. uint64_t y_buf_offset = 0;
  4967. if (!src0_uma) {
  4968. d_Qx = src0_buf_ctx->dev_buffer;
  4969. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4970. GGML_ASSERT(d_Qx != nullptr);
  4971. }
  4972. if (!src1_uma) {
  4973. d_Qy = src1_buf_ctx->dev_buffer;
  4974. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4975. GGML_ASSERT(d_Qy != nullptr);
  4976. }
  4977. if (!ids_uma) {
  4978. d_ids = ids_buf_ctx->dev_buffer;
  4979. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4980. GGML_ASSERT(d_ids != nullptr);
  4981. }
  4982. if (qx_needs_dequant) {
  4983. d_X = ctx->prealloc_x;
  4984. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4985. } else {
  4986. d_X = d_Qx;
  4987. x_buf_offset = qx_buf_offset;
  4988. GGML_ASSERT(qx_sz == x_sz);
  4989. }
  4990. if (qy_needs_dequant) {
  4991. d_Y = ctx->prealloc_y;
  4992. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4993. } else {
  4994. d_Y = d_Qy;
  4995. y_buf_offset = qy_buf_offset;
  4996. GGML_ASSERT(qy_sz == y_sz);
  4997. }
  4998. if (x_non_contig) {
  4999. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5000. } else if (qx_needs_dequant) {
  5001. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5002. ggml_vk_sync_buffers(subctx);
  5003. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  5004. { 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});
  5005. }
  5006. if (y_non_contig) {
  5007. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5008. }
  5009. uint32_t stride_batch_x = ne00*ne01;
  5010. uint32_t stride_batch_y = ne10*ne11;
  5011. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5012. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5013. }
  5014. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5015. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5016. }
  5017. // compute
  5018. ggml_vk_matmul_id(
  5019. ctx, subctx, pipeline,
  5020. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  5021. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  5022. ne01, ne21, ne10, ne10, ne10, ne01,
  5023. stride_batch_x, stride_batch_y, ne20*ne21,
  5024. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  5025. ); // NOLINT
  5026. }
  5027. 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) {
  5028. 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];
  5029. 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];
  5030. 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];
  5031. 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];
  5032. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5033. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5034. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5035. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5036. const uint64_t ne00 = src0->ne[0];
  5037. const uint64_t ne01 = src0->ne[1];
  5038. const uint64_t ne02 = src0->ne[2];
  5039. const uint64_t ne03 = src0->ne[3];
  5040. const uint64_t ne10 = src1->ne[0];
  5041. const uint64_t ne11 = src1->ne[1];
  5042. const uint64_t ne12 = src1->ne[2];
  5043. const uint64_t ne13 = src1->ne[3];
  5044. const uint64_t nei0 = ids->ne[0];
  5045. const uint64_t nei1 = ids->ne[1];
  5046. const uint64_t nbi2 = ids->nb[2];
  5047. GGML_ASSERT(nei1 == 1);
  5048. const uint64_t ne20 = dst->ne[0];
  5049. const uint64_t ne21 = dst->ne[1];
  5050. const uint64_t ne22 = dst->ne[2];
  5051. const uint64_t ne23 = dst->ne[3];
  5052. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5053. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5054. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5055. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5056. vk_buffer d_Qx = nullptr;
  5057. size_t qx_buf_offset = 0;
  5058. vk_buffer d_Qy = nullptr;
  5059. size_t qy_buf_offset = 0;
  5060. vk_buffer d_ids = nullptr;
  5061. size_t ids_buf_offset = 0;
  5062. bool src0_uma = false;
  5063. bool src1_uma = false;
  5064. bool ids_uma = false;
  5065. if (ctx->device->uma) {
  5066. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5067. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5068. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5069. src0_uma = d_Qx != nullptr;
  5070. src1_uma = d_Qy != nullptr;
  5071. ids_uma = d_ids != nullptr;
  5072. }
  5073. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5074. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5075. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5076. const bool qx_needs_dequant = x_non_contig;
  5077. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5078. // Not implemented
  5079. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5080. const uint64_t x_ne = ne01 * ne00;
  5081. const uint64_t y_ne = ne11 * ne10;
  5082. const uint64_t d_ne = ne21 * ne20;
  5083. 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);
  5084. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5085. 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;
  5086. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5087. const uint64_t ids_sz = nbi2;
  5088. const uint64_t d_sz = sizeof(float) * d_ne;
  5089. vk_pipeline to_fp16_vk_0 = nullptr;
  5090. vk_pipeline to_fp16_vk_1 = nullptr;
  5091. if (x_non_contig) {
  5092. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5093. }
  5094. if (y_non_contig) {
  5095. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5096. } else {
  5097. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5098. }
  5099. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5100. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5101. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5102. GGML_ASSERT(dmmv != nullptr);
  5103. if (dryrun) {
  5104. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5105. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5106. if (
  5107. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5108. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5109. GGML_ABORT("Requested preallocation size is too large");
  5110. }
  5111. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5112. ctx->prealloc_size_x = x_sz_upd;
  5113. }
  5114. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5115. ctx->prealloc_size_y = y_sz_upd;
  5116. }
  5117. // Request descriptor sets
  5118. if (qx_needs_dequant) {
  5119. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5120. }
  5121. if (qy_needs_dequant) {
  5122. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5123. }
  5124. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5125. return;
  5126. }
  5127. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5128. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5129. GGML_ASSERT(d_D != nullptr);
  5130. vk_buffer d_X;
  5131. uint64_t x_buf_offset = 0;
  5132. vk_buffer d_Y;
  5133. uint64_t y_buf_offset = 0;
  5134. if(!src0_uma) {
  5135. d_Qx = src0_buf_ctx->dev_buffer;
  5136. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5137. GGML_ASSERT(d_Qx != nullptr);
  5138. }
  5139. if(!src1_uma) {
  5140. d_Qy = src1_buf_ctx->dev_buffer;
  5141. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5142. GGML_ASSERT(d_Qy != nullptr);
  5143. }
  5144. if(!ids_uma) {
  5145. d_ids = ids_buf_ctx->dev_buffer;
  5146. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5147. GGML_ASSERT(d_ids != nullptr);
  5148. }
  5149. if (qx_needs_dequant) {
  5150. d_X = ctx->prealloc_x;
  5151. } else {
  5152. d_X = d_Qx;
  5153. x_buf_offset = qx_buf_offset;
  5154. GGML_ASSERT(qx_sz == x_sz);
  5155. }
  5156. if (qy_needs_dequant) {
  5157. d_Y = ctx->prealloc_y;
  5158. } else {
  5159. d_Y = d_Qy;
  5160. y_buf_offset = qy_buf_offset;
  5161. GGML_ASSERT(qy_sz == y_sz);
  5162. }
  5163. if (x_non_contig) {
  5164. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5165. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5166. }
  5167. if (y_non_contig) {
  5168. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5169. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5170. }
  5171. uint32_t stride_batch_y = ne10*ne11;
  5172. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5173. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5174. }
  5175. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5176. uint32_t groups_x = ne01;
  5177. uint32_t groups_z = 1;
  5178. if (ne01 > max_groups_x) {
  5179. groups_z = 64;
  5180. groups_x = CEIL_DIV(groups_x, groups_z);
  5181. }
  5182. // compute
  5183. const vk_mat_vec_id_push_constants pc = {
  5184. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5185. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5186. (uint32_t)nei0, (uint32_t)ne11,
  5187. };
  5188. ggml_vk_sync_buffers(subctx);
  5189. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5190. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5191. 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 } },
  5192. pc, { groups_x, (uint32_t)nei0, groups_z });
  5193. }
  5194. 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) {
  5195. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5196. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5197. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5198. } else {
  5199. // Split based on number of ids, to fit in shared memory
  5200. const uint32_t nei0 = (uint32_t)src2->ne[0];
  5201. const uint32_t nei1 = (uint32_t)src2->ne[1];
  5202. GGML_ASSERT(nei0 <= 4096);
  5203. const uint32_t split_size = std::min(nei1, 4096u / nei0);
  5204. ggml_tensor src1_copy = *src1;
  5205. ggml_tensor src2_copy = *src2;
  5206. ggml_tensor dst_copy = *dst;
  5207. for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
  5208. const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
  5209. src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
  5210. src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
  5211. dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
  5212. src1_copy.ne[2] = n_tokens;
  5213. src2_copy.ne[1] = n_tokens;
  5214. dst_copy.ne[2] = n_tokens;
  5215. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
  5216. }
  5217. }
  5218. }
  5219. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5220. // Needs to be kept up to date on shader changes
  5221. GGML_UNUSED(hsv);
  5222. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5223. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  5224. const uint32_t Bc = scalar_flash_attention_Bc;
  5225. const uint32_t tmpsh = wg_size * sizeof(float);
  5226. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5227. const uint32_t masksh = Bc * Br * sizeof(float);
  5228. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5229. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5230. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5231. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5232. return supported;
  5233. }
  5234. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5235. // Needs to be kept up to date on shader changes
  5236. GGML_UNUSED(hsv);
  5237. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5238. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5239. const uint32_t Bc = scalar_flash_attention_Bc;
  5240. const uint32_t acctype = f32acc ? 4 : 2;
  5241. const uint32_t f16vec4 = 8;
  5242. const uint32_t tmpsh = wg_size * sizeof(float);
  5243. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5244. const uint32_t Qf = Br * (hsk / 4 + 2) * f16vec4;
  5245. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5246. const uint32_t sfsh = Bc * sfshstride * acctype;
  5247. const uint32_t kshstride = hsk / 4 + 2;
  5248. const uint32_t ksh = Bc * kshstride * f16vec4;
  5249. const uint32_t slope = Br * sizeof(float);
  5250. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5251. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5252. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5253. return supported;
  5254. }
  5255. 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, ggml_tensor * dst, bool dryrun = false) {
  5256. 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];
  5257. 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];
  5258. 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];
  5259. 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];
  5260. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5261. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5262. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5263. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5264. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5265. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5266. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5267. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5268. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5269. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5270. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5271. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  5272. const uint32_t HSK = nek0;
  5273. const uint32_t HSV = nev0;
  5274. uint32_t N = neq1;
  5275. const uint32_t KV = nek1;
  5276. GGML_ASSERT(ne0 == HSV);
  5277. GGML_ASSERT(ne2 == N);
  5278. // input tensor rows must be contiguous
  5279. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  5280. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  5281. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  5282. GGML_ASSERT(neq0 == HSK);
  5283. GGML_ASSERT(neq1 == N);
  5284. GGML_ASSERT(nev1 == nek1);
  5285. // dst cannot be transposed or permuted
  5286. GGML_ASSERT(nb0 == sizeof(float));
  5287. GGML_ASSERT(nb0 <= nb1);
  5288. GGML_ASSERT(nb1 <= nb2);
  5289. GGML_ASSERT(nb2 <= nb3);
  5290. assert(dst->type == GGML_TYPE_F32);
  5291. assert(q->type == GGML_TYPE_F32);
  5292. assert(k->type == v->type);
  5293. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  5294. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  5295. if (path == FA_COOPMAT1) {
  5296. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  5297. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  5298. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  5299. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  5300. path = FA_SCALAR;
  5301. }
  5302. }
  5303. uint32_t gqa_ratio = 1;
  5304. uint32_t qk_ratio = neq2 / nek2;
  5305. uint32_t workgroups_x = (uint32_t)neq1;
  5306. uint32_t workgroups_y = (uint32_t)neq2;
  5307. uint32_t workgroups_z = (uint32_t)neq3;
  5308. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  5309. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  5310. uint32_t max_gqa;
  5311. switch (path) {
  5312. case FA_SCALAR:
  5313. case FA_COOPMAT1:
  5314. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  5315. max_gqa = get_fa_scalar_num_large_rows(HSV);
  5316. break;
  5317. case FA_COOPMAT2:
  5318. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  5319. break;
  5320. default:
  5321. GGML_ASSERT(0);
  5322. }
  5323. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  5324. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  5325. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  5326. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  5327. // and change addressing calculations to index Q's dimension 2.
  5328. gqa_ratio = qk_ratio;
  5329. N = gqa_ratio;
  5330. workgroups_y /= N;
  5331. }
  5332. vk_pipeline *pipelines;
  5333. bool small_rows = N <= get_fa_num_small_rows(path);
  5334. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  5335. // So use scalar instead.
  5336. if (small_rows && path == FA_COOPMAT1) {
  5337. path = FA_SCALAR;
  5338. }
  5339. // scalar is faster than coopmat2 when N==1
  5340. if (N == 1 && path == FA_COOPMAT2) {
  5341. path = FA_SCALAR;
  5342. }
  5343. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  5344. if (path == FA_SCALAR &&
  5345. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  5346. small_rows = true;
  5347. }
  5348. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  5349. FaHeadSizes head_sizes = fa_get_head_sizes(k->ne[0], v->ne[0]);
  5350. switch (path) {
  5351. case FA_SCALAR:
  5352. pipelines = &ctx->device->pipeline_flash_attn_f32_f16[k->type][head_sizes][f32acc][small_rows][0];
  5353. break;
  5354. case FA_COOPMAT1:
  5355. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm1[k->type][head_sizes][f32acc][small_rows][0];
  5356. break;
  5357. case FA_COOPMAT2:
  5358. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm2[k->type][head_sizes][f32acc][small_rows][0];
  5359. break;
  5360. default:
  5361. GGML_ASSERT(0);
  5362. }
  5363. assert(pipelines);
  5364. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  5365. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  5366. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  5367. bool aligned = (KV % pipelines[1]->align) == 0 &&
  5368. // the "aligned" shader variant will forcibly align strides, for performance
  5369. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  5370. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  5371. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  5372. vk_pipeline pipeline = pipelines[aligned];
  5373. assert(pipeline);
  5374. uint32_t split_kv = KV;
  5375. uint32_t split_k = 1;
  5376. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  5377. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  5378. // Try to use split_k when KV is large enough to be worth the overhead
  5379. if (workgroups_x == 1 && shader_core_count > 0) {
  5380. // Try to run two workgroups per SM.
  5381. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  5382. if (split_k > 1) {
  5383. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  5384. // of "align", so recompute split_k based on that.
  5385. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), pipelines[1]->align);
  5386. split_k = CEIL_DIV(KV, split_kv);
  5387. workgroups_x = split_k;
  5388. }
  5389. }
  5390. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  5391. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  5392. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  5393. if (split_k_size > ctx->device->max_memory_allocation_size) {
  5394. GGML_ABORT("Requested preallocation size is too large");
  5395. }
  5396. if (ctx->prealloc_size_split_k < split_k_size) {
  5397. ctx->prealloc_size_split_k = split_k_size;
  5398. }
  5399. if (dryrun) {
  5400. // Request descriptor sets
  5401. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5402. if (split_k > 1) {
  5403. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  5404. }
  5405. return;
  5406. }
  5407. float scale = 1.0f;
  5408. float max_bias = 0.0f;
  5409. float logit_softcap = 0.0f;
  5410. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  5411. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  5412. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  5413. if (logit_softcap != 0) {
  5414. scale /= logit_softcap;
  5415. }
  5416. const uint32_t n_head_kv = neq2;
  5417. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5418. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5419. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5420. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  5421. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  5422. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  5423. if (ctx->device->uma) {
  5424. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  5425. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  5426. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  5427. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  5428. Q_uma = d_Q != nullptr;
  5429. K_uma = d_K != nullptr;
  5430. V_uma = d_V != nullptr;
  5431. D_uma = d_D != nullptr;
  5432. if (mask) {
  5433. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  5434. M_uma = d_M != nullptr;
  5435. }
  5436. }
  5437. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5438. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  5439. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  5440. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  5441. if (!Q_uma) {
  5442. d_Q = q_buf_ctx->dev_buffer;
  5443. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  5444. }
  5445. if (!K_uma) {
  5446. d_K = k_buf_ctx->dev_buffer;
  5447. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  5448. }
  5449. if (!V_uma) {
  5450. d_V = v_buf_ctx->dev_buffer;
  5451. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  5452. }
  5453. if (!D_uma) {
  5454. d_D = d_buf_ctx->dev_buffer;
  5455. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5456. }
  5457. if (!M_uma) {
  5458. d_M = d_Q;
  5459. m_buf_offset = q_buf_offset;
  5460. if (mask) {
  5461. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  5462. d_M = m_buf_ctx->dev_buffer;
  5463. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  5464. }
  5465. }
  5466. uint32_t mask_n_head_log2 = ((mask != nullptr) << 16) | n_head_log2;
  5467. const vk_flash_attn_push_constants pc = { N, KV,
  5468. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  5469. (uint32_t)neq2, (uint32_t)neq3,
  5470. (uint32_t)nek2, (uint32_t)nek3,
  5471. (uint32_t)nev2, (uint32_t)nev3,
  5472. nem1, nem2, nem3,
  5473. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  5474. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  5475. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  5476. scale, max_bias, logit_softcap,
  5477. mask_n_head_log2, m0, m1,
  5478. gqa_ratio, split_kv, split_k };
  5479. ggml_vk_sync_buffers(subctx);
  5480. if (split_k > 1) {
  5481. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5482. {
  5483. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5484. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5485. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5486. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5487. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5488. },
  5489. // We only use split_k when group query attention is enabled, which means
  5490. // there's no more than one tile of rows (i.e. workgroups_x would have been
  5491. // one). We reuse workgroups_x to mean the number of splits, so we need to
  5492. // cancel out the divide by wg_denoms[0].
  5493. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  5494. ggml_vk_sync_buffers(subctx);
  5495. const std::array<uint32_t, 4> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k };
  5496. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  5497. {
  5498. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5499. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5500. },
  5501. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  5502. } else {
  5503. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5504. {
  5505. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5506. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5507. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5508. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5509. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5510. },
  5511. pc, { workgroups_x, workgroups_y, workgroups_z });
  5512. }
  5513. }
  5514. static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op) {
  5515. switch (op) {
  5516. case GGML_OP_GET_ROWS:
  5517. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5518. if (dst->type == GGML_TYPE_F16) {
  5519. return ctx->device->pipeline_get_rows[src0->type];
  5520. }
  5521. if (dst->type == GGML_TYPE_F32) {
  5522. return ctx->device->pipeline_get_rows_f32[src0->type];
  5523. }
  5524. return nullptr;
  5525. case GGML_OP_ACC:
  5526. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5527. return ctx->device->pipeline_acc_f32;
  5528. }
  5529. return nullptr;
  5530. case GGML_OP_ADD:
  5531. case GGML_OP_SUB:
  5532. case GGML_OP_MUL:
  5533. case GGML_OP_DIV:
  5534. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5535. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  5536. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  5537. return nullptr;
  5538. }
  5539. switch (op) {
  5540. case GGML_OP_ADD:
  5541. {
  5542. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  5543. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5544. }
  5545. case GGML_OP_SUB:
  5546. {
  5547. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  5548. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5549. }
  5550. case GGML_OP_MUL:
  5551. {
  5552. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  5553. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5554. }
  5555. case GGML_OP_DIV:
  5556. {
  5557. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  5558. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5559. }
  5560. default:
  5561. break;
  5562. }
  5563. return nullptr;
  5564. case GGML_OP_CONCAT:
  5565. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5566. return ctx->device->pipeline_concat_f32;
  5567. }
  5568. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5569. return ctx->device->pipeline_concat_f16;
  5570. }
  5571. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  5572. return ctx->device->pipeline_concat_i32;
  5573. }
  5574. return nullptr;
  5575. case GGML_OP_UPSCALE:
  5576. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 && dst->op_params[0] == GGML_SCALE_MODE_NEAREST) {
  5577. return ctx->device->pipeline_upscale_f32;
  5578. }
  5579. return nullptr;
  5580. case GGML_OP_SCALE:
  5581. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5582. return ctx->device->pipeline_scale_f32;
  5583. }
  5584. return nullptr;
  5585. case GGML_OP_SQR:
  5586. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5587. return ctx->device->pipeline_sqr_f32;
  5588. }
  5589. return nullptr;
  5590. case GGML_OP_SIN:
  5591. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5592. return ctx->device->pipeline_sin_f32;
  5593. }
  5594. return nullptr;
  5595. case GGML_OP_COS:
  5596. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5597. return ctx->device->pipeline_cos_f32;
  5598. }
  5599. return nullptr;
  5600. case GGML_OP_CLAMP:
  5601. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5602. return ctx->device->pipeline_clamp_f32;
  5603. }
  5604. return nullptr;
  5605. case GGML_OP_PAD:
  5606. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5607. return ctx->device->pipeline_pad_f32;
  5608. }
  5609. return nullptr;
  5610. case GGML_OP_ROLL:
  5611. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5612. return ctx->device->pipeline_roll_f32;
  5613. }
  5614. return nullptr;
  5615. case GGML_OP_REPEAT:
  5616. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  5617. return ctx->device->pipeline_repeat_f32;
  5618. }
  5619. return nullptr;
  5620. case GGML_OP_REPEAT_BACK:
  5621. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5622. return ctx->device->pipeline_repeat_back_f32;
  5623. }
  5624. return nullptr;
  5625. case GGML_OP_CPY:
  5626. case GGML_OP_CONT:
  5627. case GGML_OP_DUP:
  5628. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  5629. case GGML_OP_SET_ROWS:
  5630. return ctx->device->pipeline_set_rows[dst->type];
  5631. case GGML_OP_SILU_BACK:
  5632. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5633. return ctx->device->pipeline_silu_back_f32;
  5634. }
  5635. return nullptr;
  5636. case GGML_OP_NORM:
  5637. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5638. return ctx->device->pipeline_norm_f32;
  5639. }
  5640. return nullptr;
  5641. case GGML_OP_GROUP_NORM:
  5642. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5643. return ctx->device->pipeline_group_norm_f32;
  5644. }
  5645. return nullptr;
  5646. case GGML_OP_RMS_NORM:
  5647. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5648. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  5649. }
  5650. return nullptr;
  5651. case GGML_OP_RMS_NORM_BACK:
  5652. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5653. return ctx->device->pipeline_rms_norm_back_f32;
  5654. }
  5655. return nullptr;
  5656. case GGML_OP_L2_NORM:
  5657. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5658. return ctx->device->pipeline_l2_norm_f32;
  5659. }
  5660. return nullptr;
  5661. case GGML_OP_UNARY:
  5662. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5663. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5664. (src0->type != dst->type)) {
  5665. return nullptr;
  5666. }
  5667. switch (ggml_get_unary_op(dst)) {
  5668. case GGML_UNARY_OP_SILU:
  5669. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  5670. case GGML_UNARY_OP_GELU:
  5671. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  5672. case GGML_UNARY_OP_GELU_ERF:
  5673. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  5674. case GGML_UNARY_OP_GELU_QUICK:
  5675. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  5676. case GGML_UNARY_OP_RELU:
  5677. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  5678. case GGML_UNARY_OP_TANH:
  5679. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  5680. case GGML_UNARY_OP_SIGMOID:
  5681. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  5682. default:
  5683. break;
  5684. }
  5685. return nullptr;
  5686. case GGML_OP_GLU:
  5687. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5688. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5689. (src0->type != dst->type)) {
  5690. return nullptr;
  5691. }
  5692. switch (ggml_get_glu_op(dst)) {
  5693. case GGML_GLU_OP_GEGLU:
  5694. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  5695. case GGML_GLU_OP_REGLU:
  5696. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  5697. case GGML_GLU_OP_SWIGLU:
  5698. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  5699. case GGML_GLU_OP_GEGLU_ERF:
  5700. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  5701. case GGML_GLU_OP_GEGLU_QUICK:
  5702. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  5703. default:
  5704. break;
  5705. }
  5706. return nullptr;
  5707. case GGML_OP_DIAG_MASK_INF:
  5708. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5709. return ctx->device->pipeline_diag_mask_inf_f32;
  5710. }
  5711. return nullptr;
  5712. case GGML_OP_SOFT_MAX:
  5713. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  5714. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  5715. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  5716. }
  5717. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5718. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  5719. }
  5720. return nullptr;
  5721. case GGML_OP_SOFT_MAX_BACK:
  5722. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5723. return ctx->device->pipeline_soft_max_back_f32;
  5724. }
  5725. return nullptr;
  5726. case GGML_OP_ROPE:
  5727. case GGML_OP_ROPE_BACK:
  5728. {
  5729. const int mode = ((const int32_t *) dst->op_params)[2];
  5730. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  5731. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  5732. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  5733. if (is_neox) {
  5734. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5735. return ctx->device->pipeline_rope_neox_f32;
  5736. }
  5737. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5738. return ctx->device->pipeline_rope_neox_f16;
  5739. }
  5740. } else if (is_mrope && !is_vision) {
  5741. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5742. return ctx->device->pipeline_rope_multi_f32;
  5743. }
  5744. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5745. return ctx->device->pipeline_rope_multi_f16;
  5746. }
  5747. } else if (is_vision) {
  5748. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5749. return ctx->device->pipeline_rope_vision_f32;
  5750. }
  5751. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5752. return ctx->device->pipeline_rope_vision_f16;
  5753. }
  5754. } else {
  5755. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5756. return ctx->device->pipeline_rope_norm_f32;
  5757. }
  5758. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5759. return ctx->device->pipeline_rope_norm_f16;
  5760. }
  5761. }
  5762. return nullptr;
  5763. }
  5764. case GGML_OP_ARGSORT:
  5765. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5766. return ctx->device->pipeline_argsort_f32;
  5767. }
  5768. return nullptr;
  5769. case GGML_OP_SUM:
  5770. case GGML_OP_SUM_ROWS:
  5771. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5772. return ctx->device->pipeline_sum_rows_f32;
  5773. }
  5774. return nullptr;
  5775. case GGML_OP_ARGMAX:
  5776. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5777. return ctx->device->pipeline_argmax_f32;
  5778. }
  5779. return nullptr;
  5780. case GGML_OP_COUNT_EQUAL:
  5781. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  5782. return ctx->device->pipeline_count_equal_i32;
  5783. }
  5784. return nullptr;
  5785. case GGML_OP_IM2COL:
  5786. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5787. return ctx->device->pipeline_im2col_f32;
  5788. }
  5789. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  5790. return ctx->device->pipeline_im2col_f32_f16;
  5791. }
  5792. return nullptr;
  5793. case GGML_OP_TIMESTEP_EMBEDDING:
  5794. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5795. return ctx->device->pipeline_timestep_embedding_f32;
  5796. }
  5797. return nullptr;
  5798. case GGML_OP_CONV_TRANSPOSE_1D:
  5799. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5800. return ctx->device->pipeline_conv_transpose_1d_f32;
  5801. }
  5802. return nullptr;
  5803. case GGML_OP_POOL_2D:
  5804. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5805. return ctx->device->pipeline_pool2d_f32;
  5806. }
  5807. return nullptr;
  5808. case GGML_OP_RWKV_WKV6:
  5809. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5810. return ctx->device->pipeline_rwkv_wkv6_f32;
  5811. }
  5812. return nullptr;
  5813. case GGML_OP_RWKV_WKV7:
  5814. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5815. return ctx->device->pipeline_rwkv_wkv7_f32;
  5816. }
  5817. return nullptr;
  5818. case GGML_OP_OPT_STEP_ADAMW:
  5819. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5820. return ctx->device->pipeline_opt_step_adamw_f32;
  5821. }
  5822. return nullptr;
  5823. case GGML_OP_LEAKY_RELU:
  5824. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5825. return ctx->device->pipeline_leaky_relu_f32;
  5826. }
  5827. return nullptr;
  5828. case GGML_OP_CONV_2D_DW:
  5829. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5830. if (ggml_is_contiguous(src1)) {
  5831. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  5832. } else if (ggml_is_contiguous_channels(src1)) {
  5833. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  5834. }
  5835. }
  5836. return nullptr;
  5837. default:
  5838. return nullptr;
  5839. }
  5840. GGML_UNUSED(src2);
  5841. }
  5842. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  5843. switch (op) {
  5844. case GGML_OP_CPY:
  5845. case GGML_OP_GET_ROWS:
  5846. case GGML_OP_ADD:
  5847. case GGML_OP_SUB:
  5848. case GGML_OP_MUL:
  5849. case GGML_OP_DIV:
  5850. case GGML_OP_CONCAT:
  5851. case GGML_OP_UPSCALE:
  5852. case GGML_OP_SQR:
  5853. case GGML_OP_SIN:
  5854. case GGML_OP_COS:
  5855. case GGML_OP_CLAMP:
  5856. case GGML_OP_PAD:
  5857. case GGML_OP_REPEAT:
  5858. case GGML_OP_REPEAT_BACK:
  5859. case GGML_OP_ROPE:
  5860. case GGML_OP_RMS_NORM:
  5861. case GGML_OP_CONV_2D_DW:
  5862. case GGML_OP_IM2COL:
  5863. case GGML_OP_SET_ROWS:
  5864. return true;
  5865. default:
  5866. return false;
  5867. }
  5868. }
  5869. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  5870. {
  5871. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  5872. }
  5873. 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) {
  5874. GGML_UNUSED(p);
  5875. GGML_UNUSED(src0);
  5876. GGML_UNUSED(src1);
  5877. GGML_UNUSED(src2);
  5878. GGML_UNUSED(dst);
  5879. static_assert(!std::is_const<T>::value, "unexpected type");
  5880. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  5881. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  5882. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  5883. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  5884. }
  5885. 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) {
  5886. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5887. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5888. p.misalign_offsets = (a_offset << 16) | d_offset;
  5889. GGML_UNUSED(src1);
  5890. GGML_UNUSED(src2);
  5891. }
  5892. 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) {
  5893. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5894. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  5895. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5896. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  5897. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  5898. GGML_UNUSED(src2);
  5899. }
  5900. 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) {
  5901. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5902. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5903. p.a_offset = a_offset;
  5904. p.d_offset = d_offset;
  5905. GGML_UNUSED(src1);
  5906. GGML_UNUSED(src2);
  5907. }
  5908. template<typename PC>
  5909. 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) {
  5910. 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];
  5911. if (src1 != nullptr) {
  5912. 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];
  5913. }
  5914. if (src2 != nullptr) {
  5915. 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];
  5916. }
  5917. 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];
  5918. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  5919. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  5920. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  5921. GGML_ASSERT(dst->buffer != nullptr);
  5922. const uint64_t ne00 = src0->ne[0];
  5923. const uint64_t ne01 = src0->ne[1];
  5924. const uint64_t ne02 = src0->ne[2];
  5925. const uint64_t ne03 = src0->ne[3];
  5926. const uint64_t ne0 = ne00 * ne01;
  5927. const bool use_src1 = src1 != nullptr;
  5928. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  5929. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  5930. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  5931. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  5932. const uint64_t ne1 = ne10 * ne11;
  5933. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  5934. const bool use_src2 = src2 != nullptr;
  5935. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  5936. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  5937. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  5938. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  5939. const uint64_t ne2 = ne20 * ne21;
  5940. const uint64_t ned0 = dst->ne[0];
  5941. const uint64_t ned1 = dst->ne[1];
  5942. const uint64_t ned2 = dst->ne[2];
  5943. const uint64_t ned3 = dst->ne[3];
  5944. const uint64_t ned = ned0 * ned1;
  5945. init_pushconst_fastdiv(pc);
  5946. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  5947. if (pipeline == nullptr) {
  5948. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  5949. if (src1 != nullptr) {
  5950. std::cerr << " and " << ggml_type_name(src1->type);
  5951. }
  5952. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  5953. GGML_ABORT("fatal error");
  5954. }
  5955. if (dryrun) {
  5956. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5957. return;
  5958. }
  5959. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  5960. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5961. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5962. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  5963. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  5964. vk_buffer d_X = nullptr;
  5965. size_t x_buf_offset = 0;
  5966. vk_buffer d_Y = nullptr;
  5967. size_t y_buf_offset = 0;
  5968. vk_buffer d_Z = nullptr;
  5969. size_t z_buf_offset = 0;
  5970. bool src0_uma = false;
  5971. bool src1_uma = false;
  5972. bool src2_uma = false;
  5973. if (ctx->device->uma) {
  5974. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  5975. src0_uma = d_X != nullptr;
  5976. if (use_src1) {
  5977. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  5978. src1_uma = d_Y != nullptr;
  5979. }
  5980. if (use_src2) {
  5981. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  5982. src2_uma = d_Z != nullptr;
  5983. }
  5984. }
  5985. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  5986. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  5987. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  5988. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  5989. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5990. // Workaround for tiny tensor inputs on ROPE
  5991. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  5992. y_sz = VK_WHOLE_SIZE;
  5993. }
  5994. GGML_ASSERT(d_D != nullptr);
  5995. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5996. if(!src0_uma) {
  5997. d_X = src0_buf_ctx->dev_buffer;
  5998. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5999. GGML_ASSERT(d_X != nullptr);
  6000. }
  6001. if (use_src1 && !src1_uma) {
  6002. d_Y = src1_buf_ctx->dev_buffer;
  6003. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6004. GGML_ASSERT(d_Y != nullptr);
  6005. }
  6006. if (use_src2 && !src2_uma) {
  6007. d_Z = src2_buf_ctx->dev_buffer;
  6008. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  6009. GGML_ASSERT(d_Z != nullptr);
  6010. }
  6011. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  6012. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  6013. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6014. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6015. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6016. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6017. if (op_supports_incontiguous) {
  6018. x_sz = ggml_nbytes(src0);
  6019. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  6020. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  6021. d_sz = ggml_nbytes(dst);
  6022. if (x_buf_offset + x_sz >= d_X->size) {
  6023. x_sz = VK_WHOLE_SIZE;
  6024. }
  6025. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  6026. y_sz = VK_WHOLE_SIZE;
  6027. }
  6028. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  6029. z_sz = VK_WHOLE_SIZE;
  6030. }
  6031. if (d_buf_offset + d_sz >= d_D->size) {
  6032. d_sz = VK_WHOLE_SIZE;
  6033. }
  6034. }
  6035. std::array<uint32_t, 3> elements;
  6036. // Single call if dimension 2 is contiguous
  6037. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  6038. switch (op) {
  6039. case GGML_OP_NORM:
  6040. case GGML_OP_RMS_NORM_BACK:
  6041. case GGML_OP_L2_NORM:
  6042. case GGML_OP_SOFT_MAX:
  6043. case GGML_OP_SOFT_MAX_BACK:
  6044. case GGML_OP_SUM_ROWS:
  6045. case GGML_OP_ARGMAX:
  6046. {
  6047. const uint32_t nr = ggml_nrows(src0);
  6048. if (nr > 262144) {
  6049. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  6050. } else if (nr > 512) {
  6051. elements = { 512, CEIL_DIV(nr, 512), 1 };
  6052. } else {
  6053. elements = { nr, 1, 1 };
  6054. }
  6055. } break;
  6056. case GGML_OP_RMS_NORM:
  6057. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  6058. break;
  6059. case GGML_OP_SUM:
  6060. // We use GGML_OP_SUM_ROWS with 1 row.
  6061. elements = { 1, 1, 1 };
  6062. break;
  6063. case GGML_OP_GROUP_NORM:
  6064. {
  6065. const uint32_t num_groups = dst->op_params[0];
  6066. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  6067. } break;
  6068. case GGML_OP_DIAG_MASK_INF:
  6069. case GGML_OP_ROPE:
  6070. case GGML_OP_ROPE_BACK:
  6071. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6072. break;
  6073. case GGML_OP_GET_ROWS:
  6074. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6075. break;
  6076. case GGML_OP_ARGSORT:
  6077. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6078. break;
  6079. case GGML_OP_IM2COL:
  6080. {
  6081. const bool is_2D = dst->op_params[6] == 1;
  6082. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6083. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6084. const uint32_t KW = src0->ne[0];
  6085. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6086. const uint32_t OW = dst->ne[1];
  6087. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6088. elements = { OW * KW * KH, OH, batch * IC };
  6089. } break;
  6090. case GGML_OP_TIMESTEP_EMBEDDING:
  6091. {
  6092. const uint32_t dim = dst->op_params[0];
  6093. uint32_t half_ceil = (dim + 1) / 2;
  6094. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6095. } break;
  6096. case GGML_OP_CONV_TRANSPOSE_1D:
  6097. {
  6098. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6099. } break;
  6100. case GGML_OP_POOL_2D:
  6101. {
  6102. const uint32_t N = dst->ne[3];
  6103. const uint32_t OC = dst->ne[2];
  6104. const uint32_t OH = dst->ne[1];
  6105. const uint32_t OW = dst->ne[0];
  6106. elements = { N * OC * OH * OW, 1, 1};
  6107. } break;
  6108. case GGML_OP_ADD:
  6109. case GGML_OP_SUB:
  6110. case GGML_OP_DIV:
  6111. case GGML_OP_MUL:
  6112. case GGML_OP_SCALE:
  6113. case GGML_OP_SQR:
  6114. case GGML_OP_SIN:
  6115. case GGML_OP_COS:
  6116. case GGML_OP_CLAMP:
  6117. case GGML_OP_PAD:
  6118. case GGML_OP_ROLL:
  6119. case GGML_OP_REPEAT:
  6120. case GGML_OP_REPEAT_BACK:
  6121. case GGML_OP_CPY:
  6122. case GGML_OP_CONCAT:
  6123. case GGML_OP_UPSCALE:
  6124. case GGML_OP_UNARY:
  6125. case GGML_OP_GLU:
  6126. case GGML_OP_CONV_2D_DW:
  6127. {
  6128. uint32_t ne = ggml_nelements(dst);
  6129. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6130. // Convert from number of logical elements to 2- or 4-byte units.
  6131. ne /= ggml_blck_size(src0->type);
  6132. if ((ggml_type_size(src0->type) % 4) == 0) {
  6133. ne *= ggml_type_size(src0->type) / 4;
  6134. } else {
  6135. ne *= ggml_type_size(src0->type) / 2;
  6136. }
  6137. }
  6138. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  6139. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  6140. // So divide by block size here before splitting into 512x512 groups.
  6141. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6142. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  6143. }
  6144. if (ne > 262144) {
  6145. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6146. } else if (ne > 512) {
  6147. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6148. } else {
  6149. elements = { ne, 1, 1 };
  6150. }
  6151. } break;
  6152. case GGML_OP_SET_ROWS:
  6153. {
  6154. uint32_t ne = ggml_nelements(src0);
  6155. if (ggml_is_quantized(dst->type)) {
  6156. // quants run 32 threads each doing QUANT_K elements
  6157. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  6158. } else {
  6159. // scalar types do one element per thread, running 512 threads
  6160. ne = CEIL_DIV(ne, 512);
  6161. }
  6162. if (ne > 262144) {
  6163. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6164. } else if (ne > 512) {
  6165. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6166. } else {
  6167. elements = { ne, 1, 1 };
  6168. }
  6169. }
  6170. break;
  6171. default:
  6172. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  6173. break;
  6174. }
  6175. if (!op_supports_incontiguous) {
  6176. if (x_sz != VK_WHOLE_SIZE) {
  6177. x_sz *= ne02 * ne03;
  6178. }
  6179. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  6180. y_sz *= ne12 * ne13;
  6181. }
  6182. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  6183. z_sz *= ne22 * ne23;
  6184. }
  6185. if (d_sz != VK_WHOLE_SIZE) {
  6186. d_sz *= ned2 * ned3;
  6187. }
  6188. }
  6189. if (op == GGML_OP_SOFT_MAX || op == GGML_OP_GLU) {
  6190. // Empty src1 is possible in soft_max, but the shader needs a buffer
  6191. vk_subbuffer subbuf_y;
  6192. if (use_src1) {
  6193. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6194. } else {
  6195. subbuf_y = { d_X, 0, x_sz };
  6196. }
  6197. ggml_vk_sync_buffers(subctx);
  6198. 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);
  6199. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  6200. // Empty src2 is possible in rope, but the shader needs a buffer
  6201. vk_subbuffer subbuf_z;
  6202. if (use_src2) {
  6203. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6204. } else {
  6205. subbuf_z = { d_X, 0, x_sz };
  6206. }
  6207. ggml_vk_sync_buffers(subctx);
  6208. 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);
  6209. } else if (op == GGML_OP_IM2COL) {
  6210. // im2col uses only src1 and dst buffers
  6211. ggml_vk_sync_buffers(subctx);
  6212. 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);
  6213. } else if (op == GGML_OP_COUNT_EQUAL) {
  6214. ggml_vk_sync_buffers(subctx);
  6215. // count_equal assumes that destination buffer is initialized with zeroes
  6216. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  6217. ggml_vk_sync_buffers(subctx);
  6218. 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);
  6219. } else if (use_src2) {
  6220. ggml_vk_sync_buffers(subctx);
  6221. 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);
  6222. } else if (use_src1) {
  6223. ggml_vk_sync_buffers(subctx);
  6224. 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);
  6225. } else {
  6226. ggml_vk_sync_buffers(subctx);
  6227. 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);
  6228. }
  6229. }
  6230. 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) {
  6231. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6232. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6233. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6234. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  6235. (uint32_t)ggml_nelements(src0),
  6236. (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,
  6237. (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,
  6238. (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,
  6239. 0,
  6240. 0.0f, 0.0f, 0,
  6241. }, dryrun);
  6242. }
  6243. 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) {
  6244. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6245. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6246. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6247. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6248. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6249. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6250. int offset = dst->op_params[3] / 4; // offset in bytes
  6251. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  6252. (uint32_t)ggml_nelements(src0),
  6253. (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,
  6254. (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,
  6255. (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,
  6256. 0,
  6257. 0.0f, 0.0f, offset,
  6258. }, dryrun);
  6259. }
  6260. 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) {
  6261. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6262. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6263. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6264. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  6265. (uint32_t)ggml_nelements(src0),
  6266. (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,
  6267. (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,
  6268. (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,
  6269. 0,
  6270. 0.0f, 0.0f, 0,
  6271. }, dryrun);
  6272. }
  6273. 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) {
  6274. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6275. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6276. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6277. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  6278. (uint32_t)ggml_nelements(src0),
  6279. (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,
  6280. (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,
  6281. (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,
  6282. 0,
  6283. 0.0f, 0.0f, 0,
  6284. }, dryrun);
  6285. }
  6286. 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) {
  6287. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6288. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6289. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6290. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  6291. (uint32_t)ggml_nelements(src0),
  6292. (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,
  6293. (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,
  6294. (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,
  6295. 0,
  6296. 0.0f, 0.0f, 0,
  6297. }, dryrun);
  6298. }
  6299. 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) {
  6300. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6301. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6302. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6303. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  6304. (uint32_t)ggml_nelements(src0),
  6305. (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,
  6306. (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,
  6307. (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,
  6308. 0,
  6309. 0.0f, 0.0f, 0,
  6310. }, dryrun);
  6311. }
  6312. 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) {
  6313. GGML_ASSERT(version == 6 || version == 7);
  6314. int num_srcs = version == 6 ? 6 : 7;
  6315. for (int i = 0; i < num_srcs; i++) {
  6316. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  6317. }
  6318. GGML_ASSERT(dst->buffer != nullptr);
  6319. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  6320. GGML_ASSERT(pipeline != nullptr);
  6321. if (dryrun) {
  6322. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6323. return;
  6324. }
  6325. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6326. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6327. for (int i = 0; i < num_srcs; i++) {
  6328. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  6329. }
  6330. ggml_vk_sync_buffers(subctx);
  6331. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6332. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6333. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  6334. if (ctx->device->uma) {
  6335. for (int i = 0; i < num_srcs; i++) {
  6336. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  6337. srcs_uma[i] = d_srcs[i] != nullptr;
  6338. }
  6339. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  6340. dst_uma = d_D != nullptr;
  6341. }
  6342. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6343. for (int i = 0; i < num_srcs; i++) {
  6344. src_sizes[i] = ggml_nbytes(dst->src[i]);
  6345. if (!srcs_uma[i]) {
  6346. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  6347. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  6348. }
  6349. }
  6350. const uint64_t dst_size = ggml_nbytes(dst);
  6351. if (!dst_uma) {
  6352. d_D = dst_buf_ctx->dev_buffer;
  6353. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  6354. }
  6355. std::array<uint32_t, 3> elements = {
  6356. (uint32_t)(pc.B * pc.H),
  6357. 1,
  6358. 1
  6359. };
  6360. if (version == 6) {
  6361. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6362. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6363. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6364. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6365. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6366. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6367. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6368. vk_subbuffer{ d_D, dst_offset, dst_size }
  6369. }, pc, elements);
  6370. } else if (version == 7) {
  6371. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6372. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6373. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6374. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6375. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6376. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6377. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6378. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  6379. vk_subbuffer{ d_D, dst_offset, dst_size }
  6380. }, pc, elements);
  6381. } else {
  6382. // shouldn't happen
  6383. GGML_ASSERT(false);
  6384. }
  6385. }
  6386. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6387. const size_t seq_length = dst->src[0]->ne[2];
  6388. const size_t n_embed = dst->ne[0];
  6389. const size_t n_heads = dst->src[0]->ne[1];
  6390. const size_t n_seqs = dst->src[5]->ne[1];
  6391. ggml_vk_op_f32_wkv(
  6392. ctx, subctx, dst,
  6393. {
  6394. (uint32_t)n_seqs,
  6395. (uint32_t)seq_length,
  6396. (uint32_t)n_embed,
  6397. (uint32_t)n_heads,
  6398. },
  6399. 6,
  6400. dryrun
  6401. );
  6402. }
  6403. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6404. const size_t seq_length = dst->src[0]->ne[2];
  6405. const size_t n_embed = dst->ne[0];
  6406. const size_t n_heads = dst->src[0]->ne[1];
  6407. const size_t n_seqs = dst->src[6]->ne[1];
  6408. ggml_vk_op_f32_wkv(
  6409. ctx, subctx, dst,
  6410. {
  6411. (uint32_t)n_seqs,
  6412. (uint32_t)seq_length,
  6413. (uint32_t)n_embed,
  6414. (uint32_t)n_heads,
  6415. },
  6416. 7,
  6417. dryrun
  6418. );
  6419. }
  6420. 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) {
  6421. const ggml_tensor * x = dst->src[0];
  6422. const ggml_tensor * g = dst->src[1];
  6423. const ggml_tensor * gm = dst->src[2];
  6424. const ggml_tensor * gv = dst->src[3];
  6425. const ggml_tensor * p = dst->src[4];
  6426. GGML_ASSERT(x->type == GGML_TYPE_F32);
  6427. GGML_ASSERT(g->type == GGML_TYPE_F32);
  6428. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  6429. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  6430. GGML_ASSERT(p->type == GGML_TYPE_F32);
  6431. GGML_ASSERT(dst->buffer != nullptr);
  6432. GGML_ASSERT(ggml_is_contiguous(x));
  6433. GGML_ASSERT(ggml_is_contiguous(g));
  6434. GGML_ASSERT(ggml_is_contiguous(gm));
  6435. GGML_ASSERT(ggml_is_contiguous(gv));
  6436. GGML_ASSERT(ggml_is_contiguous(p));
  6437. GGML_ASSERT(ggml_are_same_shape(x, g));
  6438. GGML_ASSERT(ggml_are_same_shape(x, gm));
  6439. GGML_ASSERT(ggml_are_same_shape(x, gv));
  6440. GGML_ASSERT(ggml_nelements(p) == 7);
  6441. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  6442. GGML_ASSERT(pipeline != nullptr);
  6443. if (dryrun) {
  6444. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6445. return;
  6446. }
  6447. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  6448. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  6449. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  6450. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  6451. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  6452. ggml_vk_sync_buffers(subctx);
  6453. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  6454. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  6455. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  6456. if (ctx->device->uma) {
  6457. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  6458. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  6459. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  6460. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  6461. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  6462. X_uma = d_X != nullptr;
  6463. G_uma = d_G != nullptr;
  6464. GM_uma = d_GM != nullptr;
  6465. GV_uma = d_GV != nullptr;
  6466. P_uma = d_P != nullptr;
  6467. }
  6468. if (!X_uma) {
  6469. d_X = x_buf_ctx->dev_buffer;
  6470. x_offset = vk_tensor_offset(x) + x->view_offs;
  6471. }
  6472. if (!G_uma) {
  6473. d_G = g_buf_ctx->dev_buffer;
  6474. g_offset = vk_tensor_offset(g) + g->view_offs;
  6475. }
  6476. if (!GM_uma) {
  6477. d_GM = gm_buf_ctx->dev_buffer;
  6478. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  6479. }
  6480. if (!GV_uma) {
  6481. d_GV = gv_buf_ctx->dev_buffer;
  6482. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  6483. }
  6484. if (!P_uma) {
  6485. d_P = p_buf_ctx->dev_buffer;
  6486. p_offset = vk_tensor_offset(p) + p->view_offs;
  6487. }
  6488. const uint64_t x_size = ggml_nbytes(x);
  6489. const uint64_t g_size = ggml_nbytes(g);
  6490. const uint64_t gm_size = ggml_nbytes(gm);
  6491. const uint64_t gv_size = ggml_nbytes(gv);
  6492. const uint64_t p_size = ggml_nbytes(p);
  6493. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  6494. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6495. vk_subbuffer{ d_X, x_offset, x_size },
  6496. vk_subbuffer{ d_G, g_offset, g_size },
  6497. vk_subbuffer{ d_GM, gm_offset, gm_size },
  6498. vk_subbuffer{ d_GV, gv_offset, gv_size },
  6499. vk_subbuffer{ d_P, p_offset, p_size },
  6500. }, pc, elements);
  6501. }
  6502. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6503. const size_t n = ggml_nelements(dst->src[0]);
  6504. ggml_vk_op_f32_opt_step_adamw(
  6505. ctx, subctx, dst,
  6506. { (uint32_t)n, 0, 0.0f, 0.0f },
  6507. dryrun
  6508. );
  6509. }
  6510. 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) {
  6511. int * op_params = (int *)dst->op_params;
  6512. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6513. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6514. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6515. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  6516. (uint32_t)ggml_nelements(dst),
  6517. (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,
  6518. (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,
  6519. (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,
  6520. 0,
  6521. 0.0f, 0.0f, op_params[0],
  6522. }, dryrun);
  6523. }
  6524. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6525. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6526. const float sf0 = (float)dst->ne[0] / src0->ne[0];
  6527. const float sf1 = (float)dst->ne[1] / src0->ne[1];
  6528. const float sf2 = (float)dst->ne[2] / src0->ne[2];
  6529. const float sf3 = (float)dst->ne[3] / src0->ne[3];
  6530. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  6531. (uint32_t)ggml_nelements(dst), 0, 0,
  6532. (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,
  6533. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  6534. sf0, sf1, sf2, sf3,
  6535. }, dryrun);
  6536. }
  6537. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6538. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6539. p.param1 = ggml_get_op_params_f32(dst, 0);
  6540. p.param2 = ggml_get_op_params_f32(dst, 1);
  6541. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  6542. }
  6543. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6544. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6545. }
  6546. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6547. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6548. }
  6549. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6550. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6551. }
  6552. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6553. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6554. p.param1 = ggml_get_op_params_f32(dst, 0);
  6555. p.param2 = ggml_get_op_params_f32(dst, 1);
  6556. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  6557. }
  6558. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6559. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6560. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  6561. }
  6562. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6563. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  6564. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  6565. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  6566. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  6567. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  6568. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  6569. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6570. memcpy(&p.param1, &s01_packed, sizeof(float));
  6571. memcpy(&p.param2, &s23_packed, sizeof(float));
  6572. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  6573. }
  6574. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6575. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6576. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  6577. }
  6578. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6579. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6580. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  6581. }
  6582. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6583. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6584. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6585. uint32_t ne = (uint32_t)ggml_nelements(src0);
  6586. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6587. // Convert from number of logical elements to 2- or 4-byte units.
  6588. ne /= ggml_blck_size(src0->type);
  6589. if ((ggml_type_size(src0->type) % 4) == 0) {
  6590. ne *= ggml_type_size(src0->type) / 4;
  6591. } else {
  6592. ne *= ggml_type_size(src0->type) / 2;
  6593. }
  6594. }
  6595. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  6596. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  6597. }
  6598. 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) {
  6599. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6600. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6601. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6602. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  6603. (uint32_t)ggml_nelements(src0),
  6604. (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,
  6605. (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,
  6606. (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,
  6607. 0,
  6608. 0.0f, 0.0f, 0,
  6609. }, dryrun);
  6610. }
  6611. 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) {
  6612. 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);
  6613. }
  6614. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6615. float * op_params = (float *)dst->op_params;
  6616. 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);
  6617. }
  6618. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6619. const int * int_op_params = (const int *)dst->op_params;
  6620. const float * float_op_params = (const float *)dst->op_params;
  6621. const uint32_t num_groups = int_op_params[0];
  6622. const float eps = float_op_params[1];
  6623. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6624. 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);
  6625. }
  6626. 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) {
  6627. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6628. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6629. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6630. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  6631. (uint32_t)ggml_nelements(src0),
  6632. (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,
  6633. (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,
  6634. (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,
  6635. 0,
  6636. op_params[0], 0.0f, 0,
  6637. }, dryrun);
  6638. }
  6639. 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) {
  6640. float * op_params = (float *)dst->op_params;
  6641. 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);
  6642. }
  6643. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6644. float * op_params = (float *)dst->op_params;
  6645. 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);
  6646. }
  6647. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6648. 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);
  6649. }
  6650. 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) {
  6651. const bool swapped = (bool)dst->op_params[1];
  6652. const bool split = src1 != nullptr;
  6653. GGML_ASSERT(ggml_is_contiguous(src0));
  6654. if (!split) {
  6655. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  6656. } else {
  6657. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  6658. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  6659. GGML_ASSERT(src0->type == src1->type);
  6660. }
  6661. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  6662. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU, { (uint32_t)ggml_nelements(dst), (uint32_t)src0->ne[0], (uint32_t)dst->ne[0], mode }, dryrun);
  6663. }
  6664. 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) {
  6665. int32_t * op_params = (int32_t *)dst->op_params;
  6666. 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);
  6667. }
  6668. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6669. float * op_params = (float *)dst->op_params;
  6670. float scale = op_params[0];
  6671. float max_bias = op_params[1];
  6672. const uint32_t ncols = (uint32_t)src0->ne[0];
  6673. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  6674. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  6675. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  6676. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  6677. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  6678. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  6679. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  6680. const uint32_t n_head_kv = src0->ne[2];
  6681. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6682. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6683. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6684. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  6685. ncols,
  6686. src1 != nullptr ? nrows_y : (uint32_t)0,
  6687. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  6688. ne12, ne13,
  6689. nb11, nb12, nb13,
  6690. scale, max_bias,
  6691. m0, m1,
  6692. n_head_log2,
  6693. nrows_x,
  6694. }, dryrun);
  6695. }
  6696. 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) {
  6697. float * op_params = (float *)dst->op_params;
  6698. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], op_params[1] }, dryrun);
  6699. }
  6700. 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) {
  6701. const int n_dims = ((int32_t *) dst->op_params)[1];
  6702. const int mode = ((int32_t *) dst->op_params)[2];
  6703. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  6704. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  6705. const float freq_base = ((float *) dst->op_params)[5];
  6706. const float freq_scale = ((float *) dst->op_params)[6];
  6707. const float ext_factor = ((float *) dst->op_params)[7];
  6708. const float attn_factor = ((float *) dst->op_params)[8];
  6709. const float beta_fast = ((float *) dst->op_params)[9];
  6710. const float beta_slow = ((float *) dst->op_params)[10];
  6711. int sections[4] {};
  6712. if (mode & GGML_ROPE_TYPE_MROPE) {
  6713. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  6714. }
  6715. float corr_dims[2];
  6716. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  6717. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6718. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  6719. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  6720. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  6721. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  6722. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  6723. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  6724. sections[0], sections[1], sections[2], sections[3], backprop
  6725. }, dryrun);
  6726. }
  6727. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6728. int32_t * op_params = (int32_t *)dst->op_params;
  6729. uint32_t ncols = src0->ne[0];
  6730. uint32_t ncols_pad = 1;
  6731. while (ncols_pad < ncols) {
  6732. ncols_pad *= 2;
  6733. }
  6734. GGML_ASSERT(ncols_pad <= 1024);
  6735. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  6736. ncols,
  6737. ncols_pad,
  6738. op_params[0],
  6739. }, dryrun);
  6740. }
  6741. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6742. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  6743. }
  6744. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6745. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6746. }
  6747. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6748. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6749. }
  6750. 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) {
  6751. 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);
  6752. }
  6753. 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) {
  6754. const int32_t s0 = dst->op_params[0];
  6755. const int32_t s1 = dst->op_params[1];
  6756. const int32_t p0 = dst->op_params[2];
  6757. const int32_t p1 = dst->op_params[3];
  6758. const int32_t d0 = dst->op_params[4];
  6759. const int32_t d1 = dst->op_params[5];
  6760. const bool is_2D = dst->op_params[6] == 1;
  6761. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6762. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  6763. const uint32_t IW = src1->ne[0];
  6764. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6765. const uint32_t KW = src0->ne[0];
  6766. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6767. const uint32_t OW = dst->ne[1];
  6768. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6769. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  6770. const uint32_t pelements = OW * KW * KH;
  6771. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  6772. batch_offset, offset_delta,
  6773. IC, IW, IH, OW, OH, KW, KH,
  6774. pelements,
  6775. IC * KH * KW,
  6776. s0, s1, p0, p1, d0, d1,
  6777. }, dryrun);
  6778. }
  6779. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6780. const uint32_t dim = dst->op_params[0];
  6781. const uint32_t max_period = dst->op_params[1];
  6782. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  6783. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  6784. nb1, dim, max_period,
  6785. }, dryrun);
  6786. }
  6787. 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) {
  6788. // src0: (K, Cout, Cin, 1) -- kernel
  6789. // src1: (L, Cin, 1, 1) -- input
  6790. // dst: (*, Cout, 1, 1)
  6791. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6792. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6793. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6794. GGML_TENSOR_BINARY_OP_LOCALS
  6795. GGML_ASSERT(nb00 == sizeof(float));
  6796. GGML_ASSERT(nb10 == sizeof(float));
  6797. const int32_t s0 = dst->op_params[0];
  6798. vk_op_conv_transpose_1d_push_constants p{};
  6799. p.Cout = static_cast<uint32_t>(ne01);
  6800. p.Cin = static_cast<uint32_t>(ne02);
  6801. p.K = static_cast<uint32_t>(ne00);
  6802. p.L = static_cast<uint32_t>(ne10);
  6803. p.KL = static_cast<uint32_t>(ne0);
  6804. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  6805. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  6806. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  6807. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  6808. p.s0 = static_cast<uint32_t>(s0);
  6809. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  6810. }
  6811. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6812. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  6813. const int32_t k1 = dst->op_params[1];
  6814. const int32_t k0 = dst->op_params[2];
  6815. const int32_t s1 = dst->op_params[3];
  6816. const int32_t s0 = dst->op_params[4];
  6817. const int32_t p1 = dst->op_params[5];
  6818. const int32_t p0 = dst->op_params[6];
  6819. const uint32_t IH = src0->ne[1];
  6820. const uint32_t IW = src0->ne[0];
  6821. const uint32_t N = dst->ne[3];
  6822. const uint32_t OC = dst->ne[2];
  6823. const uint32_t OH = dst->ne[1];
  6824. const uint32_t OW = dst->ne[0];
  6825. const uint32_t parallel_elements = N * OC * OH * OW;
  6826. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  6827. IW, IH, OW, OH, OC,
  6828. parallel_elements,
  6829. op,
  6830. k0, k1, s0, s1, p0, p1,
  6831. }, dryrun);
  6832. }
  6833. 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) {
  6834. vk_op_conv2d_dw_push_constants p{};
  6835. p.ne = ggml_nelements(dst);
  6836. p.channels = dst->ne[2];
  6837. p.batches = dst->ne[3];
  6838. p.dst_w = dst->ne[0];
  6839. p.dst_h = dst->ne[1];
  6840. p.src_w = src1->ne[0];
  6841. p.src_h = src1->ne[1];
  6842. p.knl_w = src0->ne[0];
  6843. p.knl_h = src0->ne[1];
  6844. p.stride_x = dst->op_params[0];
  6845. p.stride_y = dst->op_params[1];
  6846. p.pad_x = dst->op_params[2];
  6847. p.pad_y = dst->op_params[3];
  6848. p.dilation_x = dst->op_params[4];
  6849. p.dilation_y = dst->op_params[5];
  6850. GGML_ASSERT(src0->ne[3] == p.channels);
  6851. GGML_ASSERT(src1->ne[3] == p.batches);
  6852. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  6853. }
  6854. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6855. const float * op_params = (const float *)dst->op_params;
  6856. 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);
  6857. }
  6858. #ifdef GGML_VULKAN_RUN_TESTS
  6859. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  6860. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  6861. return;
  6862. }
  6863. i0 = std::max(i0, 5);
  6864. i1 = std::max(i1, 5);
  6865. i2 = std::max(i2, 0);
  6866. fprintf(stderr, " ");
  6867. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6868. fprintf(stderr, "%7d ", idx1);
  6869. }
  6870. fprintf(stderr, "\n");
  6871. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6872. fprintf(stderr, "%7d: ", idx0);
  6873. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6874. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  6875. float val;
  6876. if (type == GGML_TYPE_F32) {
  6877. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  6878. } else if (type == GGML_TYPE_F16) {
  6879. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  6880. } else {
  6881. GGML_ABORT("fatal error");
  6882. }
  6883. fprintf(stderr, "% 7.2f ", val);
  6884. } else {
  6885. fprintf(stderr, " ");
  6886. }
  6887. }
  6888. fprintf(stderr, "\n");
  6889. }
  6890. }
  6891. template <typename X_TYPE, typename Y_TYPE>
  6892. 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) {
  6893. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  6894. const size_t x_ne = m * k * batch;
  6895. const size_t y_ne = k * n * batch;
  6896. const size_t d_ne = m * n * batch;
  6897. vk_pipeline p;
  6898. std::string shname;
  6899. if (shader_size == 0) {
  6900. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6901. p = ctx->device->pipeline_matmul_f32->a_s;
  6902. shname = "F32_ALIGNED_S";
  6903. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6904. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  6905. shname = "F32_F16_ALIGNED_S";
  6906. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6907. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  6908. shname = "F16_F32_ALIGNED_S";
  6909. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6910. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  6911. shname = "F16_ALIGNED_S";
  6912. } else {
  6913. GGML_ABORT("fatal error");
  6914. }
  6915. } else if (shader_size == 1) {
  6916. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6917. p = ctx->device->pipeline_matmul_f32->a_m;
  6918. shname = "F32_ALIGNED_M";
  6919. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6920. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  6921. shname = "F32_F16_ALIGNED_M";
  6922. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6923. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  6924. shname = "F16_F32_ALIGNED_M";
  6925. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6926. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  6927. shname = "F16_ALIGNED_M";
  6928. } else {
  6929. GGML_ABORT("fatal error");
  6930. }
  6931. } else if (shader_size == 2) {
  6932. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6933. p = ctx->device->pipeline_matmul_f32->a_l;
  6934. shname = "F32_ALIGNED_L";
  6935. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6936. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  6937. shname = "F32_F16_ALIGNED_L";
  6938. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6939. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  6940. shname = "F16_F32_ALIGNED_L";
  6941. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6942. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  6943. shname = "F16_ALIGNED_L";
  6944. } else {
  6945. GGML_ABORT("fatal error");
  6946. }
  6947. } else {
  6948. GGML_ASSERT(0);
  6949. }
  6950. const size_t kpad = ggml_vk_align_size(k, p->align);
  6951. if (k != kpad) {
  6952. if (shader_size == 0) {
  6953. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6954. p = ctx->device->pipeline_matmul_f32->s;
  6955. shname = "F32_S";
  6956. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6957. p = ctx->device->pipeline_matmul_f32_f16->s;
  6958. shname = "F32_F16_S";
  6959. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6960. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  6961. shname = "F16_F32_S";
  6962. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6963. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  6964. shname = "F16_S";
  6965. }
  6966. } else if (shader_size == 1) {
  6967. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6968. p = ctx->device->pipeline_matmul_f32->m;
  6969. shname = "F32_M";
  6970. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6971. p = ctx->device->pipeline_matmul_f32_f16->m;
  6972. shname = "F32_F16_M";
  6973. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6974. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  6975. shname = "F16_F32_M";
  6976. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6977. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  6978. shname = "F16_M";
  6979. }
  6980. } else if (shader_size == 2) {
  6981. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6982. p = ctx->device->pipeline_matmul_f32->l;
  6983. shname = "F32_L";
  6984. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6985. p = ctx->device->pipeline_matmul_f32_f16->l;
  6986. shname = "F32_F16_L";
  6987. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6988. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  6989. shname = "F16_F32_L";
  6990. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6991. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  6992. shname = "F16_L";
  6993. }
  6994. }
  6995. }
  6996. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  6997. if (split_k > 1) {
  6998. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6999. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7000. // Resize buffer
  7001. if (ctx->prealloc_split_k != nullptr) {
  7002. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7003. }
  7004. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7005. }
  7006. }
  7007. if (ctx->device->need_compiles) {
  7008. ggml_vk_load_shaders(ctx->device);
  7009. }
  7010. ggml_pipeline_allocate_descriptor_sets(ctx);
  7011. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7012. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7013. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7014. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  7015. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  7016. float* d = (float *) malloc(sizeof(float) * d_ne);
  7017. for (size_t i = 0; i < x_ne; i++) {
  7018. if (std::is_same<float, X_TYPE>()) {
  7019. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7020. // x[i] = 1.0f;
  7021. // x[i] = i + 1;
  7022. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7023. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7024. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7025. // x[i] = ggml_fp32_to_fp16(1.0f);
  7026. // x[i] = ggml_fp32_to_fp16(i + 1);
  7027. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7028. } else {
  7029. GGML_ABORT("fatal error");
  7030. }
  7031. }
  7032. for (size_t i = 0; i < y_ne; i++) {
  7033. if (std::is_same<float, Y_TYPE>()) {
  7034. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7035. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7036. // y[i] = i + 1;
  7037. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7038. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7039. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7040. // y[i] = ggml_fp32_to_fp16(i + 1);
  7041. } else {
  7042. GGML_ABORT("fatal error");
  7043. }
  7044. }
  7045. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  7046. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  7047. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7048. ggml_vk_ctx_begin(ctx->device, subctx);
  7049. for (size_t i = 0; i < num_it; i++) {
  7050. ggml_vk_matmul(
  7051. ctx, subctx, p, ggml_vk_subbuffer(d_X), ggml_vk_subbuffer(d_Y), ggml_vk_subbuffer(d_D), ggml_vk_subbuffer(ctx->prealloc_split_k),
  7052. m, n, k,
  7053. k, k, m, k*m, k*n, m*n,
  7054. split_k, batch, batch, batch, 1, 1, n
  7055. );
  7056. }
  7057. ggml_vk_ctx_end(subctx);
  7058. auto begin = std::chrono::high_resolution_clock::now();
  7059. ggml_vk_submit(subctx, ctx->fence);
  7060. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  7061. ctx->device->device.resetFences({ ctx->fence });
  7062. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7063. auto end = std::chrono::high_resolution_clock::now();
  7064. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7065. // copy dst to host
  7066. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  7067. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  7068. ggml_init_params iparams = {
  7069. /*.mem_size =*/ 1024*1024*1024,
  7070. /*.mem_buffer =*/ NULL,
  7071. /*.no_alloc =*/ true,
  7072. };
  7073. ggml_context * ggml_ctx = ggml_init(iparams);
  7074. ggml_type src0_type;
  7075. ggml_type src1_type;
  7076. if (std::is_same<float, X_TYPE>()) {
  7077. src0_type = GGML_TYPE_F32;
  7078. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7079. src0_type = GGML_TYPE_F16;
  7080. } else {
  7081. GGML_ABORT("fatal error");
  7082. }
  7083. if (std::is_same<float, Y_TYPE>()) {
  7084. src1_type = GGML_TYPE_F32;
  7085. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7086. src1_type = GGML_TYPE_F16;
  7087. } else {
  7088. GGML_ABORT("fatal error");
  7089. }
  7090. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  7091. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  7092. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7093. src0_ggml->data = x;
  7094. src1_ggml->data = y;
  7095. tensor_ggml->data = d_chk;
  7096. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7097. ggml_build_forward_expand(cgraph, tensor_ggml);
  7098. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7099. ggml_free(ggml_ctx);
  7100. double avg_err = 0.0;
  7101. int first_err_n = -1;
  7102. int first_err_m = -1;
  7103. int first_err_b = -1;
  7104. for (size_t i = 0; i < m*n*batch; i++) {
  7105. double err = std::fabs(d[i] - d_chk[i]);
  7106. avg_err += err;
  7107. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7108. first_err_b = i / (m * n);
  7109. first_err_n = (i % (m * n)) / m;
  7110. first_err_m = (i % (m * n)) % m;
  7111. }
  7112. }
  7113. avg_err /= m * n;
  7114. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7115. 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;
  7116. if (avg_err > 0.1 || std::isnan(avg_err)) {
  7117. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7118. std::cerr << "Actual result: " << std::endl << std::endl;
  7119. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7120. std::cerr << "Expected result: " << std::endl << std::endl;
  7121. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7122. if (split_k > 1) {
  7123. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7124. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7125. std::cerr << "d_buf0: " << std::endl << std::endl;
  7126. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7127. std::cerr << "d_buf1: " << std::endl << std::endl;
  7128. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7129. std::cerr << "d_buf2: " << std::endl << std::endl;
  7130. 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);
  7131. std::cerr << "d_buf3: " << std::endl << std::endl;
  7132. 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);
  7133. free(split_k_buf);
  7134. }
  7135. }
  7136. free(d_chk);
  7137. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  7138. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  7139. ggml_vk_destroy_buffer(d_X);
  7140. ggml_vk_destroy_buffer(d_Y);
  7141. ggml_vk_destroy_buffer(d_D);
  7142. free(x);
  7143. free(y);
  7144. free(d);
  7145. }
  7146. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  7147. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  7148. return;
  7149. }
  7150. i0 = std::max(i0, 5);
  7151. i1 = std::max(i1, 5);
  7152. i2 = std::max(i2, 0);
  7153. i3 = std::max(i3, 0);
  7154. fprintf(stderr, " ");
  7155. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7156. fprintf(stderr, "%7d ", idx1);
  7157. }
  7158. fprintf(stderr, "\n");
  7159. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7160. fprintf(stderr, "%7d: ", idx0);
  7161. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7162. 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]) {
  7163. float val;
  7164. if (tensor->type == GGML_TYPE_F32) {
  7165. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7166. } else if (tensor->type == GGML_TYPE_F16) {
  7167. 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]));
  7168. } else {
  7169. GGML_ABORT("fatal error");
  7170. }
  7171. fprintf(stderr, "% 7.2f ", val);
  7172. } else {
  7173. fprintf(stderr, " ");
  7174. }
  7175. }
  7176. fprintf(stderr, "\n");
  7177. }
  7178. }
  7179. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  7180. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  7181. }
  7182. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  7183. if (quant == GGML_TYPE_F32) {
  7184. memcpy(to, from, sizeof(float) * ne);
  7185. return;
  7186. }
  7187. const auto * tt = ggml_get_type_traits(quant);
  7188. ggml_to_float_t dequant_fn = tt->to_float;
  7189. dequant_fn(from, to, ne);
  7190. }
  7191. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7192. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  7193. const size_t x_sz = sizeof(float) * ne;
  7194. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  7195. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7196. float * x = (float *) malloc(x_sz);
  7197. void * qx = malloc(qx_sz);
  7198. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7199. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7200. float * x_ref = (float *) malloc(x_sz);
  7201. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  7202. for (size_t i = 0; i < ne; i++) {
  7203. x[i] = rand() / (float)RAND_MAX;
  7204. }
  7205. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  7206. ggml_vk_quantize_data(x, qx, ne, quant);
  7207. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  7208. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7209. if (ctx->device->need_compiles) {
  7210. ggml_vk_load_shaders(ctx->device);
  7211. }
  7212. ggml_pipeline_allocate_descriptor_sets(ctx);
  7213. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7214. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7215. ggml_vk_ctx_begin(ctx->device, subctx);
  7216. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  7217. 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});
  7218. ggml_vk_ctx_end(subctx);
  7219. auto begin = std::chrono::high_resolution_clock::now();
  7220. ggml_vk_submit(subctx, ctx->fence);
  7221. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7222. ctx->device->device.resetFences({ ctx->fence });
  7223. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7224. auto end = std::chrono::high_resolution_clock::now();
  7225. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7226. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  7227. int first_err = -1;
  7228. double avg_err = 0.0;
  7229. for (size_t i = 0; i < ne; i++) {
  7230. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  7231. avg_err += error;
  7232. if (first_err < 0 && error > 0.05) {
  7233. first_err = i;
  7234. }
  7235. }
  7236. avg_err /= ne;
  7237. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  7238. if (avg_err > 0.1) {
  7239. std::cerr << "first_error = " << first_err << std::endl;
  7240. std::cerr << "Actual result: " << std::endl << std::endl;
  7241. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7242. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  7243. }
  7244. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  7245. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7246. std::cerr << x_ref[i] << ", ";
  7247. }
  7248. std::cerr << std::endl;
  7249. }
  7250. ggml_vk_destroy_buffer(x_buf);
  7251. ggml_vk_destroy_buffer(qx_buf);
  7252. free(x);
  7253. free(qx);
  7254. free(x_ref);
  7255. free(x_chk);
  7256. }
  7257. // This does not work without ggml q8_1 quantization support
  7258. //
  7259. // typedef uint16_t ggml_half;
  7260. // typedef uint32_t ggml_half2;
  7261. //
  7262. // #define QK8_1 32
  7263. // typedef struct {
  7264. // union {
  7265. // struct {
  7266. // ggml_half d; // delta
  7267. // ggml_half s; // d * sum(qs[i])
  7268. // } GGML_COMMON_AGGR_S;
  7269. // ggml_half2 ds;
  7270. // } GGML_COMMON_AGGR_U;
  7271. // int8_t qs[QK8_1]; // quants
  7272. // } block_q8_1;
  7273. //
  7274. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7275. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  7276. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  7277. //
  7278. // const size_t x_sz = sizeof(float) * ne;
  7279. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7280. // float * x = (float *) malloc(x_sz);
  7281. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  7282. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  7283. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7284. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7285. //
  7286. // for (size_t i = 0; i < ne; i++) {
  7287. // x[i] = rand() / (float)RAND_MAX;
  7288. // }
  7289. //
  7290. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  7291. //
  7292. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7293. //
  7294. // if (ctx->device->need_compiles) {
  7295. // ggml_vk_load_shaders(ctx->device);
  7296. // }
  7297. //
  7298. // ggml_pipeline_allocate_descriptor_sets(ctx);
  7299. //
  7300. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  7301. //
  7302. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7303. // ggml_vk_ctx_begin(ctx->device, subctx);
  7304. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  7305. // ggml_vk_ctx_end(subctx);
  7306. //
  7307. // auto begin = std::chrono::high_resolution_clock::now();
  7308. //
  7309. // ggml_vk_submit(subctx, ctx->fence);
  7310. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  7311. // ctx->device->device.resetFences({ ctx->fence });
  7312. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  7313. //
  7314. // auto end = std::chrono::high_resolution_clock::now();
  7315. //
  7316. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7317. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  7318. //
  7319. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  7320. //
  7321. // int first_err = -1;
  7322. //
  7323. // for (size_t i = 0; i < ne / 32; i++) {
  7324. // 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));
  7325. //
  7326. // if (first_err < 0 && error > 0.1) {
  7327. // first_err = i;
  7328. // }
  7329. //
  7330. // 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));
  7331. //
  7332. // if (first_err < 0 && error > 0.1) {
  7333. // first_err = i;
  7334. // }
  7335. //
  7336. // for (size_t j = 0; j < 32; j++) {
  7337. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  7338. //
  7339. // if (first_err < 0 && error > 1) {
  7340. // first_err = i;
  7341. // }
  7342. // }
  7343. // }
  7344. //
  7345. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  7346. //
  7347. // if (first_err != -1) {
  7348. // std::cerr << "first_error = " << first_err << std::endl;
  7349. // std::cerr << "Actual result: " << std::endl << std::endl;
  7350. // 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) << " ";
  7351. // for (size_t j = 0; j < 32; j++) {
  7352. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  7353. // }
  7354. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  7355. // 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) << " ";
  7356. // for (size_t j = 0; j < 32; j++) {
  7357. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  7358. // }
  7359. // std::cerr << std::endl;
  7360. // }
  7361. //
  7362. // ggml_vk_destroy_buffer(x_buf);
  7363. // ggml_vk_destroy_buffer(qx_buf);
  7364. //
  7365. // free(x);
  7366. // free(qx);
  7367. // free(qx_res);
  7368. // }
  7369. 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) {
  7370. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  7371. const size_t x_ne = m * k * batch;
  7372. const size_t y_ne = k * n * batch;
  7373. const size_t d_ne = m * n * batch;
  7374. vk_matmul_pipeline2 * pipelines;
  7375. if (mmq) {
  7376. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  7377. } else {
  7378. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  7379. }
  7380. const bool fp16acc = ctx->device->fp16;
  7381. vk_pipeline p;
  7382. std::string shname;
  7383. if (shader_size == 0) {
  7384. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  7385. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  7386. } else if (shader_size == 1) {
  7387. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  7388. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  7389. } else if (shader_size == 2) {
  7390. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  7391. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  7392. } else {
  7393. GGML_ASSERT(0);
  7394. }
  7395. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  7396. if (mmq || k != kpad) {
  7397. if (shader_size == 0) {
  7398. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  7399. shname = std::string(ggml_type_name(quant)) + "_S";
  7400. } else if (shader_size == 1) {
  7401. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  7402. shname = std::string(ggml_type_name(quant)) + "_M";
  7403. } else if (shader_size == 2) {
  7404. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  7405. shname = std::string(ggml_type_name(quant)) + "_L";
  7406. } else {
  7407. GGML_ASSERT(0);
  7408. }
  7409. }
  7410. if (p == nullptr) {
  7411. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  7412. return;
  7413. }
  7414. const size_t x_sz = sizeof(float) * x_ne;
  7415. const size_t y_sz = sizeof(float) * y_ne;
  7416. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7417. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  7418. const size_t d_sz = sizeof(float) * d_ne;
  7419. float * x = (float *) malloc(x_sz);
  7420. float * y = (float *) malloc(y_sz);
  7421. void * qx = malloc(qx_sz);
  7422. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7423. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7424. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7425. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7426. float * d = (float *) malloc(d_sz);
  7427. float * d_chk = (float *) malloc(d_sz);
  7428. for (size_t i = 0; i < x_ne; i++) {
  7429. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7430. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7431. // x[i] = i % k;
  7432. }
  7433. ggml_vk_quantize_data(x, qx, x_ne, quant);
  7434. for (size_t i = 0; i < y_ne; i++) {
  7435. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7436. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7437. // y[i] = i % k;
  7438. }
  7439. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7440. if (split_k > 1) {
  7441. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7442. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7443. // Resize buffer
  7444. if (ctx->prealloc_split_k != nullptr) {
  7445. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7446. }
  7447. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7448. }
  7449. }
  7450. if (mmq) {
  7451. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  7452. }
  7453. if (ctx->device->need_compiles) {
  7454. ggml_vk_load_shaders(ctx->device);
  7455. }
  7456. ggml_pipeline_allocate_descriptor_sets(ctx);
  7457. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7458. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  7459. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7460. ggml_vk_ctx_begin(ctx->device, subctx);
  7461. if (mmq) {
  7462. for (size_t i = 0; i < num_it; i++) {
  7463. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  7464. ggml_vk_matmul(
  7465. 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 },
  7466. m, n, k,
  7467. k, k, m, k*m, k*n, m*n,
  7468. split_k, batch, batch, batch, 1, 1, n
  7469. );
  7470. }
  7471. } else {
  7472. for (size_t i = 0; i < num_it; i++) {
  7473. ggml_vk_matmul(
  7474. 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 },
  7475. m, n, k,
  7476. k, k, m, k*m, k*n, m*n,
  7477. split_k, batch, batch, batch, 1, 1, n
  7478. );
  7479. }
  7480. }
  7481. ggml_vk_ctx_end(subctx);
  7482. auto begin = std::chrono::high_resolution_clock::now();
  7483. ggml_vk_submit(subctx, ctx->fence);
  7484. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7485. ctx->device->device.resetFences({ ctx->fence });
  7486. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7487. auto end = std::chrono::high_resolution_clock::now();
  7488. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7489. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  7490. ggml_init_params iparams = {
  7491. /*.mem_size =*/ 1024*1024*1024,
  7492. /*.mem_buffer =*/ NULL,
  7493. /*.no_alloc =*/ true,
  7494. };
  7495. ggml_context * ggml_ctx = ggml_init(iparams);
  7496. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  7497. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  7498. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7499. src0_ggml->data = qx;
  7500. src1_ggml->data = y;
  7501. tensor_ggml->data = d_chk;
  7502. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7503. ggml_build_forward_expand(cgraph, tensor_ggml);
  7504. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7505. ggml_free(ggml_ctx);
  7506. double avg_err = 0.0;
  7507. int first_err_n = -1;
  7508. int first_err_m = -1;
  7509. int first_err_b = -1;
  7510. for (size_t i = 0; i < m*n*batch; i++) {
  7511. double err = std::fabs(d[i] - d_chk[i]);
  7512. avg_err += err;
  7513. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7514. first_err_b = i / (m * n);
  7515. first_err_n = (i % (m * n)) / m;
  7516. first_err_m = (i % (m * n)) % m;
  7517. }
  7518. }
  7519. avg_err /= m * n;
  7520. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7521. std::cerr << "TEST dequant matmul " << shname;
  7522. if (mmq) {
  7523. std::cerr << " mmq";
  7524. }
  7525. 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;
  7526. if (avg_err > 0.01 || std::isnan(avg_err)) {
  7527. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7528. std::cerr << "Actual result: " << std::endl << std::endl;
  7529. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7530. std::cerr << std::endl;
  7531. std::cerr << "Expected result: " << std::endl << std::endl;
  7532. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7533. std::cerr << "src0: " << std::endl << std::endl;
  7534. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  7535. std::cerr << std::endl;
  7536. std::cerr << "src1: " << std::endl << std::endl;
  7537. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  7538. if (split_k > 1) {
  7539. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7540. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7541. std::cerr << "d_buf0: " << std::endl << std::endl;
  7542. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7543. std::cerr << "d_buf1: " << std::endl << std::endl;
  7544. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7545. std::cerr << "d_buf2: " << std::endl << std::endl;
  7546. 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);
  7547. std::cerr << "d_buf3: " << std::endl << std::endl;
  7548. 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);
  7549. free(split_k_buf);
  7550. }
  7551. }
  7552. ggml_vk_destroy_buffer(qx_buf);
  7553. ggml_vk_destroy_buffer(y_buf);
  7554. ggml_vk_destroy_buffer(qy_buf);
  7555. ggml_vk_destroy_buffer(d_buf);
  7556. free(x);
  7557. free(qx);
  7558. free(y);
  7559. free(d);
  7560. free(d_chk);
  7561. }
  7562. #endif
  7563. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  7564. #if defined(GGML_VULKAN_RUN_TESTS)
  7565. const std::vector<size_t> vals {
  7566. 512, 512, 128,
  7567. 128, 512, 512,
  7568. 4096, 512, 4096,
  7569. 11008, 512, 4096,
  7570. 4096, 512, 11008,
  7571. 32000, 512, 4096,
  7572. 8, 8, 8,
  7573. 100, 46, 576,
  7574. 623, 111, 128,
  7575. 100, 46, 558,
  7576. 512, 1, 256,
  7577. 128, 110, 622,
  7578. 511, 511, 127,
  7579. 511, 511, 7,
  7580. 511, 511, 17,
  7581. 49, 49, 128,
  7582. 128, 49, 49,
  7583. 4096, 49, 4096,
  7584. };
  7585. const size_t num_it = 100;
  7586. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7587. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7588. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7589. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  7590. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  7591. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  7592. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  7593. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  7594. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  7595. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  7596. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  7597. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  7598. abort();
  7599. for (size_t i = 0; i < vals.size(); i += 3) {
  7600. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  7601. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  7602. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  7603. std::cerr << '\n';
  7604. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  7605. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  7606. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  7607. std::cerr << '\n';
  7608. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  7609. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  7610. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  7611. std::cerr << '\n' << std::endl;
  7612. if (vals[i + 2] % 32 == 0) {
  7613. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7614. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7615. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7616. std::cerr << '\n';
  7617. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  7618. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  7619. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  7620. std::cerr << '\n';
  7621. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  7622. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  7623. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  7624. std::cerr << '\n' << std::endl;
  7625. }
  7626. if (vals[i + 2] % 256 == 0) {
  7627. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  7628. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  7629. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  7630. std::cerr << '\n';
  7631. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  7632. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  7633. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  7634. std::cerr << '\n';
  7635. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  7636. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  7637. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  7638. std::cerr << '\n' << std::endl;
  7639. }
  7640. }
  7641. GGML_ABORT("fatal error");
  7642. #endif
  7643. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  7644. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  7645. // Resize buffer
  7646. if (ctx->prealloc_x != nullptr) {
  7647. ggml_vk_destroy_buffer(ctx->prealloc_x);
  7648. }
  7649. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  7650. }
  7651. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  7652. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  7653. // Resize buffer
  7654. if (ctx->prealloc_y != nullptr) {
  7655. ggml_vk_destroy_buffer(ctx->prealloc_y);
  7656. }
  7657. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  7658. }
  7659. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  7660. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  7661. // Resize buffer
  7662. if (ctx->prealloc_split_k != nullptr) {
  7663. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7664. }
  7665. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  7666. }
  7667. }
  7668. 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);
  7669. // Returns true if node has enqueued work into the queue, false otherwise
  7670. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  7671. 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){
  7672. ggml_tensor * node = cgraph->nodes[node_idx];
  7673. if (ggml_is_empty(node) || !node->buffer) {
  7674. return false;
  7675. }
  7676. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  7677. ctx->semaphore_idx = 0;
  7678. const ggml_tensor * src0 = node->src[0];
  7679. const ggml_tensor * src1 = node->src[1];
  7680. const ggml_tensor * src2 = node->src[2];
  7681. const ggml_tensor * src3 = node->src[3];
  7682. switch (node->op) {
  7683. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  7684. case GGML_OP_RESHAPE:
  7685. case GGML_OP_VIEW:
  7686. case GGML_OP_PERMUTE:
  7687. case GGML_OP_TRANSPOSE:
  7688. case GGML_OP_NONE:
  7689. return false;
  7690. case GGML_OP_UNARY:
  7691. switch (ggml_get_unary_op(node)) {
  7692. case GGML_UNARY_OP_SILU:
  7693. case GGML_UNARY_OP_GELU:
  7694. case GGML_UNARY_OP_GELU_ERF:
  7695. case GGML_UNARY_OP_GELU_QUICK:
  7696. case GGML_UNARY_OP_RELU:
  7697. case GGML_UNARY_OP_TANH:
  7698. case GGML_UNARY_OP_SIGMOID:
  7699. break;
  7700. default:
  7701. return false;
  7702. }
  7703. break;
  7704. case GGML_OP_GLU:
  7705. switch (ggml_get_glu_op(node)) {
  7706. case GGML_GLU_OP_GEGLU:
  7707. case GGML_GLU_OP_REGLU:
  7708. case GGML_GLU_OP_SWIGLU:
  7709. case GGML_GLU_OP_GEGLU_ERF:
  7710. case GGML_GLU_OP_GEGLU_QUICK:
  7711. break;
  7712. default:
  7713. return false;
  7714. }
  7715. break;
  7716. case GGML_OP_REPEAT:
  7717. case GGML_OP_REPEAT_BACK:
  7718. case GGML_OP_GET_ROWS:
  7719. case GGML_OP_ADD:
  7720. case GGML_OP_ACC:
  7721. case GGML_OP_SUB:
  7722. case GGML_OP_MUL:
  7723. case GGML_OP_DIV:
  7724. case GGML_OP_CONCAT:
  7725. case GGML_OP_UPSCALE:
  7726. case GGML_OP_SCALE:
  7727. case GGML_OP_SQR:
  7728. case GGML_OP_SIN:
  7729. case GGML_OP_COS:
  7730. case GGML_OP_CLAMP:
  7731. case GGML_OP_PAD:
  7732. case GGML_OP_ROLL:
  7733. case GGML_OP_CPY:
  7734. case GGML_OP_SET_ROWS:
  7735. case GGML_OP_CONT:
  7736. case GGML_OP_DUP:
  7737. case GGML_OP_SILU_BACK:
  7738. case GGML_OP_NORM:
  7739. case GGML_OP_GROUP_NORM:
  7740. case GGML_OP_RMS_NORM:
  7741. case GGML_OP_RMS_NORM_BACK:
  7742. case GGML_OP_L2_NORM:
  7743. case GGML_OP_DIAG_MASK_INF:
  7744. case GGML_OP_SOFT_MAX:
  7745. case GGML_OP_SOFT_MAX_BACK:
  7746. case GGML_OP_ROPE:
  7747. case GGML_OP_ROPE_BACK:
  7748. case GGML_OP_MUL_MAT:
  7749. case GGML_OP_MUL_MAT_ID:
  7750. case GGML_OP_ARGSORT:
  7751. case GGML_OP_SUM:
  7752. case GGML_OP_SUM_ROWS:
  7753. case GGML_OP_ARGMAX:
  7754. case GGML_OP_COUNT_EQUAL:
  7755. case GGML_OP_IM2COL:
  7756. case GGML_OP_TIMESTEP_EMBEDDING:
  7757. case GGML_OP_CONV_TRANSPOSE_1D:
  7758. case GGML_OP_POOL_2D:
  7759. case GGML_OP_CONV_2D_DW:
  7760. case GGML_OP_RWKV_WKV6:
  7761. case GGML_OP_RWKV_WKV7:
  7762. case GGML_OP_LEAKY_RELU:
  7763. case GGML_OP_FLASH_ATTN_EXT:
  7764. case GGML_OP_OPT_STEP_ADAMW:
  7765. break;
  7766. default:
  7767. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  7768. GGML_ABORT("fatal error");
  7769. return false;
  7770. }
  7771. vk_context compute_ctx;
  7772. if (!dryrun) {
  7773. if (ctx->compute_ctx.expired()) {
  7774. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7775. ctx->compute_ctx = compute_ctx;
  7776. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  7777. } else {
  7778. compute_ctx = ctx->compute_ctx.lock();
  7779. }
  7780. } else {
  7781. switch (node->op) {
  7782. case GGML_OP_REPEAT:
  7783. case GGML_OP_REPEAT_BACK:
  7784. case GGML_OP_ACC:
  7785. case GGML_OP_GET_ROWS:
  7786. case GGML_OP_ADD:
  7787. case GGML_OP_SUB:
  7788. case GGML_OP_MUL:
  7789. case GGML_OP_DIV:
  7790. case GGML_OP_CONCAT:
  7791. case GGML_OP_UPSCALE:
  7792. case GGML_OP_SCALE:
  7793. case GGML_OP_SQR:
  7794. case GGML_OP_SIN:
  7795. case GGML_OP_COS:
  7796. case GGML_OP_CLAMP:
  7797. case GGML_OP_PAD:
  7798. case GGML_OP_CPY:
  7799. case GGML_OP_SET_ROWS:
  7800. case GGML_OP_CONT:
  7801. case GGML_OP_DUP:
  7802. case GGML_OP_SILU_BACK:
  7803. case GGML_OP_NORM:
  7804. case GGML_OP_GROUP_NORM:
  7805. case GGML_OP_RMS_NORM:
  7806. case GGML_OP_RMS_NORM_BACK:
  7807. case GGML_OP_L2_NORM:
  7808. case GGML_OP_UNARY:
  7809. case GGML_OP_GLU:
  7810. case GGML_OP_DIAG_MASK_INF:
  7811. case GGML_OP_SOFT_MAX:
  7812. case GGML_OP_SOFT_MAX_BACK:
  7813. case GGML_OP_ROPE:
  7814. case GGML_OP_ROPE_BACK:
  7815. case GGML_OP_ARGSORT:
  7816. case GGML_OP_SUM:
  7817. case GGML_OP_SUM_ROWS:
  7818. case GGML_OP_ARGMAX:
  7819. case GGML_OP_COUNT_EQUAL:
  7820. case GGML_OP_IM2COL:
  7821. case GGML_OP_TIMESTEP_EMBEDDING:
  7822. case GGML_OP_CONV_TRANSPOSE_1D:
  7823. case GGML_OP_POOL_2D:
  7824. case GGML_OP_CONV_2D_DW:
  7825. case GGML_OP_LEAKY_RELU:
  7826. {
  7827. // These operations all go through ggml_vk_op_f32, so short-circuit and
  7828. // do the only thing needed for the dryrun.
  7829. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  7830. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7831. return false;
  7832. }
  7833. default:
  7834. break;
  7835. }
  7836. }
  7837. switch (node->op) {
  7838. case GGML_OP_REPEAT:
  7839. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  7840. break;
  7841. case GGML_OP_REPEAT_BACK:
  7842. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  7843. break;
  7844. case GGML_OP_ACC:
  7845. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  7846. break;
  7847. case GGML_OP_GET_ROWS:
  7848. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7849. break;
  7850. case GGML_OP_ADD:
  7851. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  7852. break;
  7853. case GGML_OP_SUB:
  7854. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  7855. break;
  7856. case GGML_OP_MUL:
  7857. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  7858. break;
  7859. case GGML_OP_DIV:
  7860. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  7861. break;
  7862. case GGML_OP_CONCAT:
  7863. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  7864. break;
  7865. case GGML_OP_UPSCALE:
  7866. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  7867. break;
  7868. case GGML_OP_SCALE:
  7869. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  7870. break;
  7871. case GGML_OP_SQR:
  7872. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  7873. break;
  7874. case GGML_OP_SIN:
  7875. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  7876. break;
  7877. case GGML_OP_COS:
  7878. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  7879. break;
  7880. case GGML_OP_CLAMP:
  7881. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  7882. break;
  7883. case GGML_OP_PAD:
  7884. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  7885. break;
  7886. case GGML_OP_ROLL:
  7887. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  7888. break;
  7889. case GGML_OP_CPY:
  7890. case GGML_OP_CONT:
  7891. case GGML_OP_DUP:
  7892. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  7893. break;
  7894. case GGML_OP_SET_ROWS:
  7895. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7896. break;
  7897. case GGML_OP_SILU_BACK:
  7898. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7899. break;
  7900. case GGML_OP_NORM:
  7901. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  7902. break;
  7903. case GGML_OP_GROUP_NORM:
  7904. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  7905. break;
  7906. case GGML_OP_RMS_NORM:
  7907. if (ctx->num_additional_fused_ops > 0) {
  7908. // fused rms_norm + mul
  7909. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  7910. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  7911. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  7912. } else {
  7913. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  7914. }
  7915. break;
  7916. case GGML_OP_RMS_NORM_BACK:
  7917. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7918. break;
  7919. case GGML_OP_L2_NORM:
  7920. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  7921. break;
  7922. case GGML_OP_UNARY:
  7923. switch (ggml_get_unary_op(node)) {
  7924. case GGML_UNARY_OP_SILU:
  7925. case GGML_UNARY_OP_GELU:
  7926. case GGML_UNARY_OP_GELU_ERF:
  7927. case GGML_UNARY_OP_GELU_QUICK:
  7928. case GGML_UNARY_OP_RELU:
  7929. case GGML_UNARY_OP_TANH:
  7930. case GGML_UNARY_OP_SIGMOID:
  7931. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  7932. break;
  7933. default:
  7934. return false;
  7935. }
  7936. break;
  7937. case GGML_OP_GLU:
  7938. switch (ggml_get_glu_op(node)) {
  7939. case GGML_GLU_OP_GEGLU:
  7940. case GGML_GLU_OP_REGLU:
  7941. case GGML_GLU_OP_SWIGLU:
  7942. case GGML_GLU_OP_GEGLU_ERF:
  7943. case GGML_GLU_OP_GEGLU_QUICK:
  7944. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  7945. break;
  7946. default:
  7947. return false;
  7948. }
  7949. break;
  7950. case GGML_OP_DIAG_MASK_INF:
  7951. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  7952. break;
  7953. case GGML_OP_SOFT_MAX:
  7954. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  7955. break;
  7956. case GGML_OP_SOFT_MAX_BACK:
  7957. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7958. break;
  7959. case GGML_OP_ROPE:
  7960. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  7961. break;
  7962. case GGML_OP_ROPE_BACK:
  7963. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  7964. break;
  7965. case GGML_OP_ARGSORT:
  7966. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  7967. break;
  7968. case GGML_OP_SUM:
  7969. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  7970. break;
  7971. case GGML_OP_SUM_ROWS:
  7972. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  7973. break;
  7974. case GGML_OP_ARGMAX:
  7975. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  7976. break;
  7977. case GGML_OP_COUNT_EQUAL:
  7978. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  7979. break;
  7980. case GGML_OP_IM2COL:
  7981. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  7982. break;
  7983. case GGML_OP_TIMESTEP_EMBEDDING:
  7984. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  7985. break;
  7986. case GGML_OP_CONV_TRANSPOSE_1D:
  7987. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  7988. break;
  7989. case GGML_OP_POOL_2D:
  7990. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  7991. break;
  7992. case GGML_OP_CONV_2D_DW:
  7993. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  7994. break;
  7995. case GGML_OP_LEAKY_RELU:
  7996. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  7997. break;
  7998. case GGML_OP_MUL_MAT:
  7999. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  8000. break;
  8001. case GGML_OP_MUL_MAT_ID:
  8002. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8003. break;
  8004. case GGML_OP_FLASH_ATTN_EXT:
  8005. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  8006. break;
  8007. case GGML_OP_RWKV_WKV6:
  8008. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  8009. break;
  8010. case GGML_OP_RWKV_WKV7:
  8011. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  8012. break;
  8013. case GGML_OP_OPT_STEP_ADAMW:
  8014. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  8015. break;
  8016. default:
  8017. return false;
  8018. }
  8019. if (dryrun) {
  8020. return false;
  8021. }
  8022. ctx->tensor_ctxs[node_idx] = compute_ctx;
  8023. #if defined(GGML_VULKAN_CHECK_RESULTS)
  8024. // Force context reset on each node so that each tensor ends up in its own context
  8025. // and can be run and compared to its CPU equivalent separately
  8026. last_node = true;
  8027. #endif
  8028. if (submit || last_node) {
  8029. ggml_vk_ctx_end(compute_ctx);
  8030. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  8031. if (last_node) {
  8032. compute_ctx->exit_tensor_idx = node_idx_begin;
  8033. }
  8034. else {
  8035. compute_ctx->exit_tensor_idx = -1;
  8036. }
  8037. ctx->compute_ctx.reset();
  8038. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  8039. if (!ok) {
  8040. if (node->op == GGML_OP_UNARY) {
  8041. 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;
  8042. } else if (node->op == GGML_OP_GLU) {
  8043. 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;
  8044. } else {
  8045. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  8046. }
  8047. }
  8048. }
  8049. return true;
  8050. }
  8051. 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) {
  8052. GGML_UNUSED(cgraph);
  8053. ggml_backend_buffer * buf = nullptr;
  8054. switch (tensor->op) {
  8055. case GGML_OP_ADD:
  8056. case GGML_OP_ACC:
  8057. case GGML_OP_GET_ROWS:
  8058. case GGML_OP_SUB:
  8059. case GGML_OP_MUL:
  8060. case GGML_OP_DIV:
  8061. case GGML_OP_CONCAT:
  8062. case GGML_OP_UPSCALE:
  8063. case GGML_OP_SCALE:
  8064. case GGML_OP_SQR:
  8065. case GGML_OP_SIN:
  8066. case GGML_OP_COS:
  8067. case GGML_OP_CLAMP:
  8068. case GGML_OP_PAD:
  8069. case GGML_OP_ROLL:
  8070. case GGML_OP_CPY:
  8071. case GGML_OP_SET_ROWS:
  8072. case GGML_OP_CONT:
  8073. case GGML_OP_DUP:
  8074. case GGML_OP_SILU_BACK:
  8075. case GGML_OP_NORM:
  8076. case GGML_OP_GROUP_NORM:
  8077. case GGML_OP_RMS_NORM:
  8078. case GGML_OP_RMS_NORM_BACK:
  8079. case GGML_OP_L2_NORM:
  8080. case GGML_OP_DIAG_MASK_INF:
  8081. case GGML_OP_SOFT_MAX:
  8082. case GGML_OP_SOFT_MAX_BACK:
  8083. case GGML_OP_ROPE:
  8084. case GGML_OP_ROPE_BACK:
  8085. case GGML_OP_RESHAPE:
  8086. case GGML_OP_VIEW:
  8087. case GGML_OP_PERMUTE:
  8088. case GGML_OP_TRANSPOSE:
  8089. case GGML_OP_NONE:
  8090. case GGML_OP_ARGSORT:
  8091. case GGML_OP_SUM:
  8092. case GGML_OP_SUM_ROWS:
  8093. case GGML_OP_ARGMAX:
  8094. case GGML_OP_COUNT_EQUAL:
  8095. case GGML_OP_IM2COL:
  8096. case GGML_OP_TIMESTEP_EMBEDDING:
  8097. case GGML_OP_CONV_TRANSPOSE_1D:
  8098. case GGML_OP_POOL_2D:
  8099. case GGML_OP_CONV_2D_DW:
  8100. case GGML_OP_RWKV_WKV6:
  8101. case GGML_OP_RWKV_WKV7:
  8102. case GGML_OP_LEAKY_RELU:
  8103. case GGML_OP_REPEAT:
  8104. case GGML_OP_REPEAT_BACK:
  8105. case GGML_OP_OPT_STEP_ADAMW:
  8106. buf = tensor->buffer;
  8107. break;
  8108. case GGML_OP_UNARY:
  8109. switch (ggml_get_unary_op(tensor)) {
  8110. case GGML_UNARY_OP_SILU:
  8111. case GGML_UNARY_OP_GELU:
  8112. case GGML_UNARY_OP_GELU_ERF:
  8113. case GGML_UNARY_OP_GELU_QUICK:
  8114. case GGML_UNARY_OP_RELU:
  8115. case GGML_UNARY_OP_TANH:
  8116. case GGML_UNARY_OP_SIGMOID:
  8117. buf = tensor->buffer;
  8118. break;
  8119. default:
  8120. return false;
  8121. }
  8122. break;
  8123. case GGML_OP_GLU:
  8124. switch (ggml_get_glu_op(tensor)) {
  8125. case GGML_GLU_OP_GEGLU:
  8126. case GGML_GLU_OP_REGLU:
  8127. case GGML_GLU_OP_SWIGLU:
  8128. case GGML_GLU_OP_GEGLU_ERF:
  8129. case GGML_GLU_OP_GEGLU_QUICK:
  8130. buf = tensor->buffer;
  8131. break;
  8132. default:
  8133. return false;
  8134. }
  8135. break;
  8136. case GGML_OP_MUL_MAT:
  8137. case GGML_OP_MUL_MAT_ID:
  8138. case GGML_OP_FLASH_ATTN_EXT:
  8139. buf = tensor->buffer;
  8140. break;
  8141. default:
  8142. return false;
  8143. }
  8144. if (buf == nullptr) {
  8145. return false;
  8146. }
  8147. 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 << ")");
  8148. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  8149. // always wait for the GPU work to be done for the last submit
  8150. if (tensor_idx == subctx->exit_tensor_idx) {
  8151. use_fence = true;
  8152. }
  8153. // Only run if ctx hasn't been submitted yet
  8154. if (!subctx->seqs.empty()) {
  8155. #ifdef GGML_VULKAN_CHECK_RESULTS
  8156. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  8157. use_fence = true;
  8158. #endif
  8159. // Do staging buffer copies
  8160. for (auto& cpy : subctx->in_memcpys) {
  8161. memcpy(cpy.dst, cpy.src, cpy.n);
  8162. }
  8163. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  8164. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  8165. ctx->almost_ready_fence_pending = true;
  8166. } else {
  8167. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  8168. }
  8169. if (use_fence) {
  8170. ggml_vk_wait_for_fence(ctx);
  8171. }
  8172. #ifdef GGML_VULKAN_CHECK_RESULTS
  8173. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  8174. #endif
  8175. }
  8176. if (tensor_idx == subctx->exit_tensor_idx) {
  8177. // Do staging buffer copies
  8178. for (auto& cpy : subctx->out_memcpys) {
  8179. memcpy(cpy.dst, cpy.src, cpy.n);
  8180. }
  8181. subctx->in_memcpys.clear();
  8182. subctx->out_memcpys.clear();
  8183. }
  8184. return true;
  8185. }
  8186. // Clean up after graph processing is done
  8187. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  8188. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  8189. for (auto& buffer : ctx->gc.temp_buffers) {
  8190. ggml_vk_pool_free(ctx, buffer);
  8191. }
  8192. ctx->gc.temp_buffers.clear();
  8193. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8194. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8195. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  8196. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  8197. }
  8198. ctx->gc.semaphores.clear();
  8199. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  8200. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  8201. }
  8202. ctx->gc.tl_semaphores.clear();
  8203. ctx->semaphore_idx = 0;
  8204. ctx->event_idx = 0;
  8205. for (auto& event : ctx->gc.events) {
  8206. ctx->device->device.resetEvent(event);
  8207. }
  8208. ctx->tensor_ctxs.clear();
  8209. ctx->gc.contexts.clear();
  8210. ctx->pipeline_descriptor_set_requirements = 0;
  8211. ctx->descriptor_set_idx = 0;
  8212. }
  8213. // Clean up on backend free
  8214. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  8215. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  8216. ggml_vk_graph_cleanup(ctx);
  8217. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8218. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8219. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8220. for (auto& buffer : ctx->buffer_pool) {
  8221. ggml_vk_destroy_buffer(buffer);
  8222. }
  8223. ctx->prealloc_size_x = 0;
  8224. ctx->prealloc_size_y = 0;
  8225. ctx->prealloc_size_split_k = 0;
  8226. for (auto& event : ctx->gc.events) {
  8227. ctx->device->device.destroyEvent(event);
  8228. }
  8229. ctx->gc.events.clear();
  8230. ctx->device->device.destroyFence(ctx->fence);
  8231. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  8232. for (auto& pool : ctx->descriptor_pools) {
  8233. ctx->device->device.destroyDescriptorPool(pool);
  8234. }
  8235. ctx->descriptor_pools.clear();
  8236. ctx->descriptor_sets.clear();
  8237. ctx->compute_cmd_pool.destroy(ctx->device->device);
  8238. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  8239. }
  8240. static int ggml_vk_get_device_count() {
  8241. ggml_vk_instance_init();
  8242. return vk_instance.device_indices.size();
  8243. }
  8244. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  8245. ggml_vk_instance_init();
  8246. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  8247. vk::PhysicalDeviceProperties props;
  8248. devices[device].getProperties(&props);
  8249. snprintf(description, description_size, "%s", props.deviceName.data());
  8250. }
  8251. // backend interface
  8252. #define UNUSED GGML_UNUSED
  8253. // device backend
  8254. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  8255. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  8256. }
  8257. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8258. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  8259. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8260. ggml_vk_destroy_buffer(ctx->dev_buffer);
  8261. delete ctx;
  8262. }
  8263. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  8264. return vk_ptr_base;
  8265. UNUSED(buffer);
  8266. }
  8267. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8268. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  8269. if (tensor->view_src != nullptr) {
  8270. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  8271. }
  8272. return GGML_STATUS_SUCCESS;
  8273. }
  8274. 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) {
  8275. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  8276. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8277. vk_buffer buf = buf_ctx->dev_buffer;
  8278. uint32_t val32 = (uint32_t)value * 0x01010101;
  8279. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  8280. }
  8281. 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) {
  8282. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8283. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8284. vk_buffer buf = buf_ctx->dev_buffer;
  8285. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8286. }
  8287. 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) {
  8288. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8289. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8290. vk_buffer buf = buf_ctx->dev_buffer;
  8291. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8292. }
  8293. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8294. if (ggml_backend_buffer_is_vk(src->buffer)) {
  8295. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8296. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8297. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8298. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8299. 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));
  8300. return true;
  8301. }
  8302. return false;
  8303. UNUSED(buffer);
  8304. }
  8305. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8306. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8307. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  8308. }
  8309. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  8310. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  8311. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  8312. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  8313. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  8314. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  8315. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  8316. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  8317. /* .clear = */ ggml_backend_vk_buffer_clear,
  8318. /* .reset = */ NULL,
  8319. };
  8320. // vk buffer type
  8321. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8322. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8323. return ctx->name.c_str();
  8324. }
  8325. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8326. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  8327. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8328. vk_buffer dev_buffer = nullptr;
  8329. try {
  8330. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  8331. } catch (const vk::SystemError& e) {
  8332. return nullptr;
  8333. }
  8334. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  8335. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  8336. }
  8337. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8338. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8339. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  8340. }
  8341. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8342. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8343. return ctx->device->suballocation_block_size;
  8344. }
  8345. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8346. return ggml_nbytes(tensor);
  8347. UNUSED(buft);
  8348. }
  8349. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  8350. ggml_vk_instance_init();
  8351. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  8352. vk_device dev = ggml_vk_get_device(dev_num);
  8353. return &dev->buffer_type;
  8354. }
  8355. // host buffer type
  8356. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8357. return GGML_VK_NAME "_Host";
  8358. UNUSED(buft);
  8359. }
  8360. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  8361. return GGML_VK_NAME "_Host";
  8362. UNUSED(buffer);
  8363. }
  8364. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8365. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  8366. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  8367. }
  8368. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8369. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  8370. size += 32; // Behave like the CPU buffer type
  8371. void * ptr = nullptr;
  8372. try {
  8373. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  8374. } catch (vk::SystemError& e) {
  8375. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  8376. // fallback to cpu buffer
  8377. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8378. }
  8379. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8380. buffer->buft = buft;
  8381. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  8382. return buffer;
  8383. UNUSED(buft);
  8384. }
  8385. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8386. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  8387. UNUSED(buft);
  8388. }
  8389. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8390. return vk_instance.devices[0]->suballocation_block_size;
  8391. UNUSED(buft);
  8392. }
  8393. // Should be changed to return device-specific host buffer type
  8394. // but that probably requires changes in llama.cpp
  8395. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  8396. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  8397. /* .iface = */ {
  8398. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  8399. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  8400. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  8401. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  8402. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8403. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8404. },
  8405. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  8406. /* .context = */ nullptr,
  8407. };
  8408. // Make sure device 0 is initialized
  8409. ggml_vk_instance_init();
  8410. ggml_vk_get_device(0);
  8411. return &ggml_backend_vk_buffer_type_host;
  8412. }
  8413. // backend
  8414. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  8415. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8416. return ctx->name.c_str();
  8417. }
  8418. static void ggml_backend_vk_free(ggml_backend_t backend) {
  8419. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8420. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  8421. ggml_vk_cleanup(ctx);
  8422. delete ctx;
  8423. delete backend;
  8424. }
  8425. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  8426. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8427. return &ctx->device->buffer_type;
  8428. }
  8429. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8430. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  8431. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8432. 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");
  8433. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8434. vk_context transfer_ctx;
  8435. if (ctx->transfer_ctx.expired()) {
  8436. // Initialize new transfer context
  8437. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8438. ctx->transfer_ctx = transfer_ctx;
  8439. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8440. } else {
  8441. transfer_ctx = ctx->transfer_ctx.lock();
  8442. }
  8443. vk_buffer buf = buf_ctx->dev_buffer;
  8444. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8445. }
  8446. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8447. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  8448. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8449. 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");
  8450. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8451. vk_context transfer_ctx;
  8452. if (ctx->transfer_ctx.expired()) {
  8453. // Initialize new transfer context
  8454. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8455. ctx->transfer_ctx = transfer_ctx;
  8456. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8457. } else {
  8458. transfer_ctx = ctx->transfer_ctx.lock();
  8459. }
  8460. vk_buffer buf = buf_ctx->dev_buffer;
  8461. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8462. }
  8463. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  8464. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  8465. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8466. 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)) {
  8467. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8468. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8469. vk_context transfer_ctx;
  8470. if (ctx->transfer_ctx.expired()) {
  8471. // Initialize new transfer context
  8472. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8473. ctx->transfer_ctx = transfer_ctx;
  8474. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8475. } else {
  8476. transfer_ctx = ctx->transfer_ctx.lock();
  8477. }
  8478. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8479. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8480. 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));
  8481. return true;
  8482. }
  8483. return false;
  8484. }
  8485. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  8486. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  8487. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8488. if(ctx->transfer_ctx.expired()) {
  8489. return;
  8490. }
  8491. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  8492. ggml_vk_ctx_end(transfer_ctx);
  8493. for (auto& cpy : transfer_ctx->in_memcpys) {
  8494. memcpy(cpy.dst, cpy.src, cpy.n);
  8495. }
  8496. ggml_vk_submit(transfer_ctx, ctx->fence);
  8497. ggml_vk_wait_for_fence(ctx);
  8498. for (auto& cpy : transfer_ctx->out_memcpys) {
  8499. memcpy(cpy.dst, cpy.src, cpy.n);
  8500. }
  8501. ctx->transfer_ctx.reset();
  8502. }
  8503. static bool ggml_vk_is_empty(ggml_tensor * node) {
  8504. 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;
  8505. }
  8506. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  8507. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  8508. return false;
  8509. }
  8510. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  8511. // additional constraints specific to this fusion
  8512. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  8513. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  8514. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  8515. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  8516. // rms_norm only supports f32
  8517. if (mul->src[0]->type != GGML_TYPE_F32 ||
  8518. mul->src[1]->type != GGML_TYPE_F32 ||
  8519. mul->type != GGML_TYPE_F32) {
  8520. return false;
  8521. }
  8522. // if rms_norm is the B operand, then we don't handle broadcast
  8523. if (rms_norm == mul->src[1] &&
  8524. mul->src[0]->ne[1] != rms_norm->ne[1]) {
  8525. return false;
  8526. }
  8527. // rms_norm shader assumes contiguous rows
  8528. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  8529. return false;
  8530. }
  8531. }
  8532. return true;
  8533. }
  8534. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8535. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  8536. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8537. if (vk_instance.debug_utils_support) {
  8538. vk::DebugUtilsLabelEXT dul = {};
  8539. dul.pLabelName = "ggml_backend_vk_graph_compute";
  8540. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  8541. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  8542. }
  8543. uint64_t total_mat_mul_bytes = 0;
  8544. for (int i = 0; i < cgraph->n_nodes; i++) {
  8545. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8546. ctx->num_additional_fused_ops = 1;
  8547. }
  8548. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  8549. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8550. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8551. }
  8552. i += ctx->num_additional_fused_ops;
  8553. ctx->num_additional_fused_ops = 0;
  8554. }
  8555. if (ctx->device->need_compiles) {
  8556. ggml_vk_load_shaders(ctx->device);
  8557. }
  8558. ggml_vk_preallocate_buffers(ctx);
  8559. ggml_pipeline_allocate_descriptor_sets(ctx);
  8560. int last_node = cgraph->n_nodes - 1;
  8561. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  8562. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  8563. last_node -= 1;
  8564. }
  8565. // Reserve tensor context space for all nodes
  8566. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  8567. bool first_node_in_batch = true; // true if next node will be first node in a batch
  8568. int submit_node_idx = 0; // index to first node in a batch
  8569. vk_context compute_ctx;
  8570. if (vk_perf_logger_enabled) {
  8571. // allocate/resize the query pool
  8572. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  8573. if (ctx->device->query_pool) {
  8574. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  8575. }
  8576. vk::QueryPoolCreateInfo query_create_info;
  8577. query_create_info.queryType = vk::QueryType::eTimestamp;
  8578. query_create_info.queryCount = cgraph->n_nodes + 100;
  8579. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  8580. ctx->device->num_queries = query_create_info.queryCount;
  8581. }
  8582. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  8583. GGML_ASSERT(ctx->compute_ctx.expired());
  8584. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8585. ctx->compute_ctx = compute_ctx;
  8586. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8587. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  8588. }
  8589. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  8590. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  8591. // (and scaled down based on model size, so smaller models submit earlier).
  8592. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  8593. int nodes_per_submit = 100;
  8594. int submitted_nodes = 0;
  8595. int submit_count = 0;
  8596. uint64_t mul_mat_bytes = 0;
  8597. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  8598. for (int i = 0; i < cgraph->n_nodes; i++) {
  8599. if (first_node_in_batch) {
  8600. submit_node_idx = i;
  8601. }
  8602. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8603. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8604. }
  8605. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8606. ctx->num_additional_fused_ops = 1;
  8607. }
  8608. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  8609. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  8610. bool submit = (submitted_nodes >= nodes_per_submit) ||
  8611. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  8612. (i + ctx->num_additional_fused_ops == last_node) ||
  8613. (almost_ready && !ctx->almost_ready_fence_pending);
  8614. 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);
  8615. if (vk_perf_logger_enabled) {
  8616. if (ctx->compute_ctx.expired()) {
  8617. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8618. ctx->compute_ctx = compute_ctx;
  8619. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8620. } else {
  8621. compute_ctx = ctx->compute_ctx.lock();
  8622. }
  8623. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  8624. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  8625. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  8626. }
  8627. }
  8628. if (enqueued) {
  8629. ++submitted_nodes;
  8630. #ifndef GGML_VULKAN_CHECK_RESULTS
  8631. if (first_node_in_batch) {
  8632. first_node_in_batch = false;
  8633. }
  8634. #endif
  8635. }
  8636. if (submit && enqueued) {
  8637. first_node_in_batch = true;
  8638. submitted_nodes = 0;
  8639. mul_mat_bytes = 0;
  8640. if (submit_count < 3) {
  8641. mul_mat_bytes_per_submit *= 2;
  8642. }
  8643. submit_count++;
  8644. }
  8645. i += ctx->num_additional_fused_ops;
  8646. ctx->num_additional_fused_ops = 0;
  8647. }
  8648. if (vk_perf_logger_enabled) {
  8649. // End the command buffer and submit/wait
  8650. GGML_ASSERT(!ctx->compute_ctx.expired());
  8651. compute_ctx = ctx->compute_ctx.lock();
  8652. ggml_vk_ctx_end(compute_ctx);
  8653. ggml_vk_submit(compute_ctx, ctx->device->fence);
  8654. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  8655. ctx->device->device.resetFences({ ctx->device->fence });
  8656. // Get the results and pass them to the logger
  8657. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  8658. 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");
  8659. for (int i = 0; i < cgraph->n_nodes; i++) {
  8660. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  8661. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  8662. }
  8663. }
  8664. ctx->device->perf_logger->print_timings();
  8665. }
  8666. ggml_vk_graph_cleanup(ctx);
  8667. return GGML_STATUS_SUCCESS;
  8668. UNUSED(backend);
  8669. }
  8670. // TODO: enable async and synchronize
  8671. static ggml_backend_i ggml_backend_vk_interface = {
  8672. /* .get_name = */ ggml_backend_vk_name,
  8673. /* .free = */ ggml_backend_vk_free,
  8674. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  8675. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  8676. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  8677. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  8678. /* .graph_plan_create = */ NULL,
  8679. /* .graph_plan_free = */ NULL,
  8680. /* .graph_plan_update = */ NULL,
  8681. /* .graph_plan_compute = */ NULL,
  8682. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  8683. /* .event_record = */ NULL,
  8684. /* .event_wait = */ NULL,
  8685. };
  8686. static ggml_guid_t ggml_backend_vk_guid() {
  8687. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  8688. return &guid;
  8689. }
  8690. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  8691. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  8692. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  8693. ggml_vk_init(ctx, dev_num);
  8694. ggml_backend_t vk_backend = new ggml_backend {
  8695. /* .guid = */ ggml_backend_vk_guid(),
  8696. /* .interface = */ ggml_backend_vk_interface,
  8697. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  8698. /* .context = */ ctx,
  8699. };
  8700. return vk_backend;
  8701. }
  8702. bool ggml_backend_is_vk(ggml_backend_t backend) {
  8703. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  8704. }
  8705. int ggml_backend_vk_get_device_count() {
  8706. return ggml_vk_get_device_count();
  8707. }
  8708. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  8709. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8710. int dev_idx = vk_instance.device_indices[device];
  8711. ggml_vk_get_device_description(dev_idx, description, description_size);
  8712. }
  8713. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  8714. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8715. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  8716. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  8717. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  8718. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  8719. *total = heap.size;
  8720. *free = heap.size;
  8721. break;
  8722. }
  8723. }
  8724. }
  8725. //////////////////////////
  8726. struct ggml_backend_vk_device_context {
  8727. size_t device;
  8728. std::string name;
  8729. std::string description;
  8730. };
  8731. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  8732. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8733. return ctx->name.c_str();
  8734. }
  8735. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  8736. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8737. return ctx->description.c_str();
  8738. }
  8739. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  8740. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  8741. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  8742. }
  8743. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  8744. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8745. return ggml_backend_vk_buffer_type(ctx->device);
  8746. }
  8747. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  8748. UNUSED(dev);
  8749. return ggml_backend_vk_host_buffer_type();
  8750. }
  8751. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  8752. UNUSED(dev);
  8753. return GGML_BACKEND_DEVICE_TYPE_GPU;
  8754. }
  8755. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  8756. props->name = ggml_backend_vk_device_get_name(dev);
  8757. props->description = ggml_backend_vk_device_get_description(dev);
  8758. props->type = ggml_backend_vk_device_get_type(dev);
  8759. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  8760. props->caps = {
  8761. /* .async = */ false,
  8762. /* .host_buffer = */ true,
  8763. /* .buffer_from_host_ptr = */ false,
  8764. /* .events = */ false,
  8765. };
  8766. }
  8767. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  8768. UNUSED(params);
  8769. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8770. return ggml_backend_vk_init(ctx->device);
  8771. }
  8772. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  8773. switch (op->op) {
  8774. case GGML_OP_UNARY:
  8775. switch (ggml_get_unary_op(op)) {
  8776. case GGML_UNARY_OP_GELU:
  8777. case GGML_UNARY_OP_GELU_ERF:
  8778. case GGML_UNARY_OP_GELU_QUICK:
  8779. case GGML_UNARY_OP_SILU:
  8780. case GGML_UNARY_OP_RELU:
  8781. case GGML_UNARY_OP_TANH:
  8782. case GGML_UNARY_OP_SIGMOID:
  8783. return ggml_is_contiguous(op->src[0]) &&
  8784. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8785. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8786. (op->src[0]->type == op->type);
  8787. default:
  8788. return false;
  8789. }
  8790. break;
  8791. case GGML_OP_GLU:
  8792. switch (ggml_get_glu_op(op)) {
  8793. case GGML_GLU_OP_GEGLU:
  8794. case GGML_GLU_OP_REGLU:
  8795. case GGML_GLU_OP_SWIGLU:
  8796. case GGML_GLU_OP_GEGLU_ERF:
  8797. case GGML_GLU_OP_GEGLU_QUICK:
  8798. return ggml_is_contiguous(op->src[0]) &&
  8799. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8800. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8801. (op->src[0]->type == op->type);
  8802. default:
  8803. return false;
  8804. }
  8805. break;
  8806. case GGML_OP_MUL_MAT:
  8807. case GGML_OP_MUL_MAT_ID:
  8808. {
  8809. ggml_type src0_type = op->src[0]->type;
  8810. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8811. const vk_device& device = ggml_vk_get_device(ctx->device);
  8812. if (op->op == GGML_OP_MUL_MAT_ID) {
  8813. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  8814. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  8815. return false;
  8816. }
  8817. // Check against size of shared memory variable
  8818. if (op->src[2]->ne[0] > 4096) {
  8819. return false;
  8820. }
  8821. }
  8822. switch (src0_type) {
  8823. case GGML_TYPE_F32:
  8824. case GGML_TYPE_F16:
  8825. case GGML_TYPE_BF16:
  8826. case GGML_TYPE_Q4_0:
  8827. case GGML_TYPE_Q4_1:
  8828. case GGML_TYPE_Q5_0:
  8829. case GGML_TYPE_Q5_1:
  8830. case GGML_TYPE_Q8_0:
  8831. case GGML_TYPE_Q2_K:
  8832. case GGML_TYPE_Q3_K:
  8833. case GGML_TYPE_Q4_K:
  8834. case GGML_TYPE_Q5_K:
  8835. case GGML_TYPE_Q6_K:
  8836. case GGML_TYPE_IQ1_S:
  8837. case GGML_TYPE_IQ1_M:
  8838. case GGML_TYPE_IQ2_XXS:
  8839. case GGML_TYPE_IQ2_XS:
  8840. case GGML_TYPE_IQ2_S:
  8841. case GGML_TYPE_IQ3_XXS:
  8842. case GGML_TYPE_IQ3_S:
  8843. case GGML_TYPE_IQ4_XS:
  8844. case GGML_TYPE_IQ4_NL:
  8845. break;
  8846. default:
  8847. return false;
  8848. }
  8849. struct ggml_tensor * a;
  8850. struct ggml_tensor * b;
  8851. if (op->op == GGML_OP_MUL_MAT) {
  8852. a = op->src[0];
  8853. b = op->src[1];
  8854. } else {
  8855. a = op->src[2];
  8856. b = op->src[1];
  8857. }
  8858. if (a->ne[3] != b->ne[3]) {
  8859. return false;
  8860. }
  8861. 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) ||
  8862. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  8863. return false;
  8864. }
  8865. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  8866. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  8867. // So don't support this combination for now.
  8868. return false;
  8869. }
  8870. return true;
  8871. } break;
  8872. case GGML_OP_FLASH_ATTN_EXT:
  8873. {
  8874. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8875. auto device = ggml_vk_get_device(ctx->device);
  8876. bool coopmat2 = device->coopmat2;
  8877. FaHeadSizes head_sizes = fa_get_head_sizes(op->src[1]->ne[0], op->src[2]->ne[0]);
  8878. if (head_sizes == FA_HEAD_SIZE_UNSUPPORTED) {
  8879. return false;
  8880. }
  8881. if (op->src[0]->type != GGML_TYPE_F32) {
  8882. return false;
  8883. }
  8884. if (op->type != GGML_TYPE_F32) {
  8885. return false;
  8886. }
  8887. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  8888. return false;
  8889. }
  8890. // It's straightforward to support different K/V dequant, but would
  8891. // significantly increase the number of pipelines
  8892. if (op->src[1]->type != op->src[2]->type) {
  8893. return false;
  8894. }
  8895. switch (op->src[1]->type) {
  8896. case GGML_TYPE_F16:
  8897. case GGML_TYPE_Q4_0:
  8898. case GGML_TYPE_Q8_0:
  8899. // supported in scalar and coopmat2 paths
  8900. break;
  8901. case GGML_TYPE_Q4_1:
  8902. case GGML_TYPE_Q5_0:
  8903. case GGML_TYPE_Q5_1:
  8904. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  8905. //case GGML_TYPE_Q2_K:
  8906. //case GGML_TYPE_Q3_K:
  8907. //case GGML_TYPE_Q4_K:
  8908. //case GGML_TYPE_Q5_K:
  8909. //case GGML_TYPE_Q6_K:
  8910. //case GGML_TYPE_IQ1_S:
  8911. //case GGML_TYPE_IQ1_M:
  8912. //case GGML_TYPE_IQ2_XXS:
  8913. //case GGML_TYPE_IQ2_XS:
  8914. //case GGML_TYPE_IQ2_S:
  8915. //case GGML_TYPE_IQ3_XXS:
  8916. //case GGML_TYPE_IQ3_S:
  8917. //case GGML_TYPE_IQ4_XS:
  8918. case GGML_TYPE_IQ4_NL:
  8919. // currently supported only in coopmat2 path
  8920. if (!coopmat2) {
  8921. return false;
  8922. }
  8923. break;
  8924. default:
  8925. return false;
  8926. }
  8927. if (!coopmat2 && !device->subgroup_shuffle) {
  8928. // scalar FA uses subgroupShuffle
  8929. return false;
  8930. }
  8931. return true;
  8932. }
  8933. case GGML_OP_GET_ROWS:
  8934. {
  8935. switch (op->src[0]->type) {
  8936. case GGML_TYPE_F32:
  8937. case GGML_TYPE_F16:
  8938. case GGML_TYPE_BF16:
  8939. case GGML_TYPE_Q4_0:
  8940. case GGML_TYPE_Q4_1:
  8941. case GGML_TYPE_Q5_0:
  8942. case GGML_TYPE_Q5_1:
  8943. case GGML_TYPE_Q8_0:
  8944. case GGML_TYPE_IQ1_S:
  8945. case GGML_TYPE_IQ1_M:
  8946. case GGML_TYPE_IQ2_XXS:
  8947. case GGML_TYPE_IQ2_XS:
  8948. case GGML_TYPE_IQ2_S:
  8949. case GGML_TYPE_IQ3_XXS:
  8950. case GGML_TYPE_IQ3_S:
  8951. case GGML_TYPE_IQ4_XS:
  8952. case GGML_TYPE_IQ4_NL:
  8953. return true;
  8954. default:
  8955. return false;
  8956. }
  8957. } break;
  8958. case GGML_OP_SET_ROWS:
  8959. {
  8960. switch (op->type) {
  8961. case GGML_TYPE_F32:
  8962. case GGML_TYPE_F16:
  8963. case GGML_TYPE_BF16:
  8964. case GGML_TYPE_Q4_0:
  8965. case GGML_TYPE_Q4_1:
  8966. case GGML_TYPE_Q5_0:
  8967. case GGML_TYPE_Q5_1:
  8968. case GGML_TYPE_Q8_0:
  8969. case GGML_TYPE_IQ4_NL:
  8970. return true;
  8971. default:
  8972. return false;
  8973. }
  8974. } break;
  8975. case GGML_OP_CONT:
  8976. case GGML_OP_CPY:
  8977. case GGML_OP_DUP:
  8978. {
  8979. ggml_type src0_type = op->src[0]->type;
  8980. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  8981. if (src0_type == GGML_TYPE_F32) {
  8982. switch (src1_type) {
  8983. case GGML_TYPE_F32:
  8984. case GGML_TYPE_F16:
  8985. case GGML_TYPE_BF16:
  8986. case GGML_TYPE_Q4_0:
  8987. case GGML_TYPE_Q4_1:
  8988. case GGML_TYPE_Q5_0:
  8989. case GGML_TYPE_Q5_1:
  8990. case GGML_TYPE_Q8_0:
  8991. case GGML_TYPE_IQ4_NL:
  8992. return true;
  8993. default:
  8994. break;
  8995. }
  8996. }
  8997. if (src1_type == GGML_TYPE_F32) {
  8998. switch (src0_type) {
  8999. case GGML_TYPE_F16:
  9000. case GGML_TYPE_Q4_0:
  9001. case GGML_TYPE_Q4_1:
  9002. case GGML_TYPE_Q5_0:
  9003. case GGML_TYPE_Q5_1:
  9004. case GGML_TYPE_Q8_0:
  9005. case GGML_TYPE_IQ4_NL:
  9006. return true;
  9007. default:
  9008. break;
  9009. }
  9010. }
  9011. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9012. return true;
  9013. }
  9014. // We can handle copying from a type to the same type if it's
  9015. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  9016. // so the type/block size must be a multiple of 4.
  9017. if (src0_type == src1_type &&
  9018. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  9019. (ggml_type_size(src0_type) % 2) == 0) {
  9020. return true;
  9021. }
  9022. return false;
  9023. } break;
  9024. case GGML_OP_REPEAT:
  9025. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  9026. case GGML_OP_REPEAT_BACK:
  9027. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  9028. case GGML_OP_ROPE:
  9029. case GGML_OP_ROPE_BACK:
  9030. case GGML_OP_NONE:
  9031. case GGML_OP_RESHAPE:
  9032. case GGML_OP_VIEW:
  9033. case GGML_OP_PERMUTE:
  9034. case GGML_OP_TRANSPOSE:
  9035. case GGML_OP_RMS_NORM:
  9036. return true;
  9037. case GGML_OP_NORM:
  9038. case GGML_OP_GROUP_NORM:
  9039. case GGML_OP_L2_NORM:
  9040. return ggml_is_contiguous(op->src[0]);
  9041. case GGML_OP_ADD:
  9042. case GGML_OP_SUB:
  9043. case GGML_OP_MUL:
  9044. case GGML_OP_DIV:
  9045. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9046. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  9047. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  9048. case GGML_OP_SILU_BACK:
  9049. case GGML_OP_RMS_NORM_BACK:
  9050. case GGML_OP_SQR:
  9051. case GGML_OP_SIN:
  9052. case GGML_OP_COS:
  9053. case GGML_OP_CLAMP:
  9054. return op->src[0]->type == GGML_TYPE_F32;
  9055. case GGML_OP_UPSCALE:
  9056. return op->op_params[0] == GGML_SCALE_MODE_NEAREST;
  9057. case GGML_OP_ACC:
  9058. case GGML_OP_CONCAT:
  9059. case GGML_OP_SCALE:
  9060. case GGML_OP_PAD:
  9061. case GGML_OP_DIAG_MASK_INF:
  9062. return true;
  9063. case GGML_OP_SOFT_MAX:
  9064. case GGML_OP_SOFT_MAX_BACK:
  9065. case GGML_OP_ARGSORT:
  9066. case GGML_OP_SUM:
  9067. case GGML_OP_SUM_ROWS:
  9068. case GGML_OP_ARGMAX:
  9069. case GGML_OP_COUNT_EQUAL:
  9070. case GGML_OP_IM2COL:
  9071. case GGML_OP_TIMESTEP_EMBEDDING:
  9072. case GGML_OP_CONV_2D_DW:
  9073. case GGML_OP_POOL_2D:
  9074. case GGML_OP_RWKV_WKV6:
  9075. case GGML_OP_RWKV_WKV7:
  9076. case GGML_OP_LEAKY_RELU:
  9077. case GGML_OP_OPT_STEP_ADAMW:
  9078. return true;
  9079. case GGML_OP_CONV_TRANSPOSE_1D:
  9080. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  9081. default:
  9082. return false;
  9083. }
  9084. UNUSED(dev);
  9085. }
  9086. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  9087. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  9088. return false;
  9089. }
  9090. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9091. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  9092. return buft_ctx->device->idx == ctx->device;
  9093. }
  9094. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9095. const int min_batch_size = 32;
  9096. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  9097. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  9098. UNUSED(dev);
  9099. }
  9100. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  9101. /* .get_name = */ ggml_backend_vk_device_get_name,
  9102. /* .get_description = */ ggml_backend_vk_device_get_description,
  9103. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  9104. /* .get_type = */ ggml_backend_vk_device_get_type,
  9105. /* .get_props = */ ggml_backend_vk_device_get_props,
  9106. /* .init_backend = */ ggml_backend_vk_device_init,
  9107. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  9108. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  9109. /* .buffer_from_host_ptr = */ NULL,
  9110. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  9111. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  9112. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  9113. /* .event_new = */ NULL,
  9114. /* .event_free = */ NULL,
  9115. /* .event_synchronize = */ NULL,
  9116. };
  9117. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  9118. UNUSED(reg);
  9119. return GGML_VK_NAME;
  9120. }
  9121. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  9122. UNUSED(reg);
  9123. return ggml_backend_vk_get_device_count();
  9124. }
  9125. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  9126. static std::vector<ggml_backend_dev_t> devices;
  9127. static bool initialized = false;
  9128. {
  9129. static std::mutex mutex;
  9130. std::lock_guard<std::mutex> lock(mutex);
  9131. if (!initialized) {
  9132. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  9133. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  9134. char desc[256];
  9135. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  9136. ctx->device = i;
  9137. ctx->name = GGML_VK_NAME + std::to_string(i);
  9138. ctx->description = desc;
  9139. devices.push_back(new ggml_backend_device {
  9140. /* .iface = */ ggml_backend_vk_device_i,
  9141. /* .reg = */ reg,
  9142. /* .context = */ ctx,
  9143. });
  9144. }
  9145. initialized = true;
  9146. }
  9147. }
  9148. GGML_ASSERT(device < devices.size());
  9149. return devices[device];
  9150. }
  9151. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  9152. /* .get_name = */ ggml_backend_vk_reg_get_name,
  9153. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  9154. /* .get_device = */ ggml_backend_vk_reg_get_device,
  9155. /* .get_proc_address = */ NULL,
  9156. };
  9157. ggml_backend_reg_t ggml_backend_vk_reg() {
  9158. static ggml_backend_reg reg = {
  9159. /* .api_version = */ GGML_BACKEND_API_VERSION,
  9160. /* .iface = */ ggml_backend_vk_reg_i,
  9161. /* .context = */ nullptr,
  9162. };
  9163. try {
  9164. ggml_vk_instance_init();
  9165. return &reg;
  9166. } catch (const vk::SystemError& e) {
  9167. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  9168. return nullptr;
  9169. }
  9170. }
  9171. // Extension availability
  9172. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9173. #ifdef GGML_VULKAN_VALIDATE
  9174. bool portability_enumeration_ext = false;
  9175. // Check for portability enumeration extension for MoltenVK support
  9176. for (const auto& properties : instance_extensions) {
  9177. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9178. return true;
  9179. }
  9180. }
  9181. if (!portability_enumeration_ext) {
  9182. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9183. }
  9184. #endif
  9185. return false;
  9186. UNUSED(instance_extensions);
  9187. }
  9188. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9189. #ifdef __APPLE__
  9190. bool portability_enumeration_ext = false;
  9191. // Check for portability enumeration extension for MoltenVK support
  9192. for (const auto& properties : instance_extensions) {
  9193. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9194. return true;
  9195. }
  9196. }
  9197. if (!portability_enumeration_ext) {
  9198. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9199. }
  9200. #endif
  9201. return false;
  9202. UNUSED(instance_extensions);
  9203. }
  9204. // Extension availability
  9205. static bool ggml_vk_instance_debug_utils_ext_available(
  9206. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  9207. // Check for portability enumeration extension for MoltenVK support
  9208. for (const auto & properties : instance_extensions) {
  9209. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  9210. return true;
  9211. }
  9212. }
  9213. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  9214. return false;
  9215. UNUSED(instance_extensions);
  9216. }
  9217. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  9218. switch (props.vendorID) {
  9219. case VK_VENDOR_ID_INTEL:
  9220. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  9221. // while some older hardware (ex. Arc A770) has performance regressions
  9222. return arch == vk_device_architecture::INTEL_XE2;
  9223. case VK_VENDOR_ID_AMD:
  9224. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  9225. // Workaround for AMD proprietary driver reporting support on all GPUs
  9226. return arch == vk_device_architecture::AMD_RDNA3;
  9227. }
  9228. return true;
  9229. default:
  9230. return true;
  9231. }
  9232. }
  9233. // checks
  9234. #ifdef GGML_VULKAN_CHECK_RESULTS
  9235. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  9236. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  9237. return;
  9238. }
  9239. for (int j = 0; j < level; j++) {
  9240. std::cerr << " ";
  9241. }
  9242. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  9243. done.push_back(tensor);
  9244. for (int i = 0; i < GGML_MAX_SRC; i++) {
  9245. if (tensor->src[i] != nullptr) {
  9246. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  9247. }
  9248. }
  9249. }
  9250. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  9251. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  9252. return;
  9253. }
  9254. i0 = std::max(i0, 5);
  9255. i1 = std::max(i1, 5);
  9256. i2 = std::max(i2, 0);
  9257. i3 = std::max(i3, 0);
  9258. fprintf(stderr, " ");
  9259. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9260. fprintf(stderr, "%7d ", idx1);
  9261. }
  9262. fprintf(stderr, "\n");
  9263. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9264. fprintf(stderr, "%7d: ", idx0);
  9265. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9266. 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]) {
  9267. float val;
  9268. if (tensor->type == GGML_TYPE_F32) {
  9269. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9270. } else if (tensor->type == GGML_TYPE_F16) {
  9271. 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]));
  9272. } else if (tensor->type == GGML_TYPE_I32) {
  9273. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9274. } else {
  9275. GGML_ABORT("fatal error");
  9276. }
  9277. fprintf(stderr, "% 7.2f ", val);
  9278. } else {
  9279. fprintf(stderr, " ");
  9280. }
  9281. }
  9282. fprintf(stderr, "\n");
  9283. }
  9284. }
  9285. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  9286. void * tensor_data = tensor->data;
  9287. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  9288. if (is_gpu) {
  9289. const size_t tensor_size = ggml_nbytes(tensor);
  9290. tensor_data = malloc(tensor_size);
  9291. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9292. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  9293. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  9294. }
  9295. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  9296. 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;
  9297. if (tensor->src[0] != nullptr) {
  9298. 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;
  9299. }
  9300. if (tensor->src[1] != nullptr) {
  9301. 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;
  9302. }
  9303. std::cerr << std::endl << "Result:" << std::endl;
  9304. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9305. std::cerr << std::endl;
  9306. std::vector<const ggml_tensor *> done;
  9307. ggml_vk_print_graph_origin(tensor, done);
  9308. if (is_gpu) {
  9309. free(tensor_data);
  9310. }
  9311. }
  9312. void * comp_result;
  9313. size_t comp_size;
  9314. size_t comp_nb[GGML_MAX_DIMS];
  9315. size_t check_counter = 0;
  9316. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9317. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9318. if (tensor->op == GGML_OP_TRANSPOSE) {
  9319. return;
  9320. }
  9321. bool fused_rms_norm_mul = false;
  9322. int rms_norm_idx = -1;
  9323. if (ctx->num_additional_fused_ops == 1 &&
  9324. tensor->op == GGML_OP_RMS_NORM &&
  9325. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9326. fused_rms_norm_mul = true;
  9327. tensor = cgraph->nodes[tensor_idx + 1];
  9328. }
  9329. check_counter++;
  9330. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9331. return;
  9332. }
  9333. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  9334. ggml_tensor * src0 = tensor->src[0];
  9335. ggml_tensor * src1 = tensor->src[1];
  9336. struct ggml_init_params iparams = {
  9337. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  9338. /*.mem_buffer =*/ NULL,
  9339. /*.no_alloc =*/ false,
  9340. };
  9341. struct ggml_context * ggml_ctx = ggml_init(iparams);
  9342. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9343. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  9344. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9345. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  9346. struct ggml_tensor * tensor_clone = nullptr;
  9347. for (int i = 0; i < 6; i++) {
  9348. ggml_tensor * srci = tensor->src[i];
  9349. if (fused_rms_norm_mul) {
  9350. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  9351. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  9352. switch (i) {
  9353. case 0: srci = rms_norm->src[0]; break;
  9354. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  9355. default: continue;
  9356. }
  9357. }
  9358. if (srci == nullptr) {
  9359. continue;
  9360. }
  9361. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  9362. size_t srci_size = ggml_nbytes(srci);
  9363. src_clone[i] = srci_clone;
  9364. src_size[i] = ggml_nbytes(srci);
  9365. src_buffer[i] = malloc(srci_size);
  9366. srci_clone->data = src_buffer[i];
  9367. if (ggml_backend_buffer_is_host(srci->buffer)) {
  9368. memcpy(srci_clone->data, srci->data, srci_size);
  9369. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9370. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  9371. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  9372. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9373. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  9374. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  9375. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  9376. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  9377. const int idx = i3*srci->ne[2] + i2;
  9378. 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]);
  9379. }
  9380. }
  9381. srci_clone->nb[0] = srci->nb[0];
  9382. srci_clone->nb[1] = srci->nb[1];
  9383. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  9384. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  9385. }
  9386. } else {
  9387. if (offset + srci_size >= buffer_gpu->size) {
  9388. srci_size = buffer_gpu->size - offset;
  9389. }
  9390. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  9391. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9392. }
  9393. } else {
  9394. GGML_ABORT("fatal error");
  9395. }
  9396. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9397. ggml_vk_print_tensor(srci, srci_name[i]);
  9398. }
  9399. }
  9400. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  9401. const float * params = (const float *)tensor->op_params;
  9402. 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]);
  9403. } else if (tensor->op == GGML_OP_MUL_MAT) {
  9404. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  9405. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  9406. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  9407. } else if (tensor->op == GGML_OP_SUB) {
  9408. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  9409. } else if (tensor->op == GGML_OP_MUL) {
  9410. if (fused_rms_norm_mul) {
  9411. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  9412. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  9413. } else {
  9414. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  9415. }
  9416. } else if (tensor->op == GGML_OP_DIV) {
  9417. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  9418. } else if (tensor->op == GGML_OP_CONCAT) {
  9419. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  9420. } else if (tensor->op == GGML_OP_UPSCALE) {
  9421. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  9422. } else if (tensor->op == GGML_OP_SCALE) {
  9423. const float * params = (const float *)tensor->op_params;
  9424. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], params[0]);
  9425. } else if (tensor->op == GGML_OP_SQR) {
  9426. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  9427. } else if (tensor->op == GGML_OP_SIN) {
  9428. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  9429. } else if (tensor->op == GGML_OP_COS) {
  9430. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  9431. } else if (tensor->op == GGML_OP_CLAMP) {
  9432. const float * params = (const float *)tensor->op_params;
  9433. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  9434. } else if (tensor->op == GGML_OP_PAD) {
  9435. tensor_clone = ggml_pad(ggml_ctx, src_clone[0], tensor->ne[0] - src_clone[0]->ne[0], tensor->ne[1] - src_clone[0]->ne[1], tensor->ne[2] - src_clone[0]->ne[2], tensor->ne[3] - src_clone[0]->ne[3]);
  9436. } else if (tensor->op == GGML_OP_REPEAT) {
  9437. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  9438. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  9439. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  9440. } else if (tensor->op == GGML_OP_ADD) {
  9441. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  9442. } else if (tensor->op == GGML_OP_ACC) {
  9443. 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]);
  9444. } else if (tensor->op == GGML_OP_NORM) {
  9445. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9446. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  9447. const float * float_params = (const float *)tensor->op_params;
  9448. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  9449. } else if (tensor->op == GGML_OP_RMS_NORM) {
  9450. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9451. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  9452. const float eps = ((float *) tensor->op_params)[0];
  9453. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  9454. } else if (tensor->op == GGML_OP_SILU_BACK) {
  9455. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  9456. } else if (tensor->op == GGML_OP_L2_NORM) {
  9457. const float eps = ((float *) tensor->op_params)[0];
  9458. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  9459. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  9460. if (src1 != nullptr) {
  9461. const float * params = (const float *)tensor->op_params;
  9462. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  9463. } else {
  9464. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  9465. }
  9466. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  9467. 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]);
  9468. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  9469. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  9470. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  9471. const int n_dims = ((int32_t *) tensor->op_params)[1];
  9472. const int mode = ((int32_t *) tensor->op_params)[2];
  9473. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  9474. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  9475. const float freq_base = ((float *) tensor->op_params)[5];
  9476. const float freq_scale = ((float *) tensor->op_params)[6];
  9477. const float ext_factor = ((float *) tensor->op_params)[7];
  9478. const float attn_factor = ((float *) tensor->op_params)[8];
  9479. const float beta_fast = ((float *) tensor->op_params)[9];
  9480. const float beta_slow = ((float *) tensor->op_params)[10];
  9481. if (mode & GGML_ROPE_TYPE_MROPE) {
  9482. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  9483. if (tensor->op == GGML_OP_ROPE) {
  9484. 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);
  9485. } else {
  9486. 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);
  9487. }
  9488. } else {
  9489. if (tensor->op == GGML_OP_ROPE) {
  9490. 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);
  9491. } else {
  9492. 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);
  9493. }
  9494. }
  9495. } else if (tensor->op == GGML_OP_UNARY) {
  9496. switch (ggml_get_unary_op(tensor)) {
  9497. case GGML_UNARY_OP_SILU:
  9498. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  9499. break;
  9500. case GGML_UNARY_OP_GELU:
  9501. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  9502. break;
  9503. case GGML_UNARY_OP_GELU_ERF:
  9504. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  9505. break;
  9506. case GGML_UNARY_OP_GELU_QUICK:
  9507. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  9508. break;
  9509. case GGML_UNARY_OP_RELU:
  9510. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  9511. break;
  9512. case GGML_UNARY_OP_TANH:
  9513. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  9514. break;
  9515. case GGML_UNARY_OP_SIGMOID:
  9516. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  9517. break;
  9518. default:
  9519. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9520. GGML_ABORT("fatal error");
  9521. }
  9522. } else if (tensor->op == GGML_OP_GLU) {
  9523. if (src_clone[1] == nullptr) {
  9524. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  9525. } else {
  9526. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  9527. }
  9528. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  9529. if (src1 == nullptr) {
  9530. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  9531. tensor_clone->type = tensor->type;
  9532. } else {
  9533. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  9534. }
  9535. } else if (tensor->op == GGML_OP_SET_ROWS) {
  9536. tensor_clone = ggml_set_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9537. } else if (tensor->op == GGML_OP_CONT) {
  9538. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9539. } else if (tensor->op == GGML_OP_RESHAPE) {
  9540. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9541. } else if (tensor->op == GGML_OP_VIEW) {
  9542. 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]);
  9543. } else if (tensor->op == GGML_OP_PERMUTE) {
  9544. int32_t * params = (int32_t *)tensor->op_params;
  9545. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  9546. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  9547. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  9548. } else if (tensor->op == GGML_OP_GET_ROWS) {
  9549. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9550. } else if (tensor->op == GGML_OP_ARGSORT) {
  9551. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  9552. } else if (tensor->op == GGML_OP_SUM) {
  9553. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  9554. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  9555. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  9556. } else if (tensor->op == GGML_OP_ARGMAX) {
  9557. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  9558. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  9559. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  9560. } else if (tensor->op == GGML_OP_IM2COL) {
  9561. const int32_t s0 = tensor->op_params[0];
  9562. const int32_t s1 = tensor->op_params[1];
  9563. const int32_t p0 = tensor->op_params[2];
  9564. const int32_t p1 = tensor->op_params[3];
  9565. const int32_t d0 = tensor->op_params[4];
  9566. const int32_t d1 = tensor->op_params[5];
  9567. const bool is_2D = tensor->op_params[6] == 1;
  9568. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  9569. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  9570. const int32_t dim = tensor->op_params[0];
  9571. const int32_t max_period = tensor->op_params[1];
  9572. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  9573. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  9574. const int32_t s0 = tensor->op_params[0];
  9575. const int32_t p0 = tensor->op_params[1];
  9576. const int32_t d0 = tensor->op_params[2];
  9577. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  9578. } else if (tensor->op == GGML_OP_POOL_2D) {
  9579. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  9580. const int32_t k0 = tensor->op_params[1];
  9581. const int32_t k1 = tensor->op_params[2];
  9582. const int32_t s0 = tensor->op_params[3];
  9583. const int32_t s1 = tensor->op_params[4];
  9584. const int32_t p0 = tensor->op_params[5];
  9585. const int32_t p1 = tensor->op_params[6];
  9586. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  9587. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  9588. const float * op_params = (const float *)tensor->op_params;
  9589. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  9590. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  9591. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  9592. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  9593. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  9594. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  9595. src_clone[4], src_clone[5], src_clone[6]);
  9596. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  9597. src_clone[0]->flags = src0->flags;
  9598. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  9599. src_clone[2], src_clone[3], src_clone[4]);
  9600. }
  9601. else {
  9602. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9603. GGML_ABORT("fatal error");
  9604. }
  9605. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  9606. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  9607. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  9608. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9609. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  9610. }
  9611. comp_size = ggml_nbytes(tensor_clone);
  9612. comp_result = malloc(comp_size);
  9613. memcpy(comp_result, tensor_clone->data, comp_size);
  9614. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9615. for (int i = 0; i < 6; i++) {
  9616. if (src_buffer[i] != nullptr) {
  9617. free(src_buffer[i]);
  9618. }
  9619. }
  9620. ggml_free(ggml_ctx);
  9621. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  9622. }
  9623. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9624. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9625. if (tensor->op == GGML_OP_TRANSPOSE) {
  9626. return;
  9627. }
  9628. bool fused_rms_norm_mul = false;
  9629. if (ctx->num_additional_fused_ops == 1 &&
  9630. tensor->op == GGML_OP_RMS_NORM &&
  9631. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9632. fused_rms_norm_mul = true;
  9633. tensor = cgraph->nodes[tensor_idx + 1];
  9634. }
  9635. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9636. return;
  9637. }
  9638. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  9639. ggml_tensor * src0 = tensor->src[0];
  9640. ggml_tensor * src1 = tensor->src[1];
  9641. ggml_tensor * src2 = tensor->src[2];
  9642. ggml_tensor * src3 = tensor->src[3];
  9643. void * tensor_data = tensor->data;
  9644. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9645. size_t tensor_size = ggml_nbytes(tensor);
  9646. tensor_data = malloc(tensor_size);
  9647. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9648. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9649. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  9650. if (offset + tensor_size >= buffer_gpu->size) {
  9651. tensor_size = buffer_gpu->size - offset;
  9652. }
  9653. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  9654. }
  9655. float first_error_result = -1.0f;
  9656. float first_error_correct = -1.0f;
  9657. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  9658. double avg_err = 0.0;
  9659. size_t counter = 0;
  9660. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  9661. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  9662. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  9663. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  9664. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  9665. float correct = 0.0f;
  9666. float result = 0.0f;
  9667. if (buffer_size_fit) {
  9668. if (tensor->type == GGML_TYPE_F32) {
  9669. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9670. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9671. } else if (tensor->type == GGML_TYPE_F16) {
  9672. 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]));
  9673. 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]));
  9674. } else if (tensor->type == GGML_TYPE_I32) {
  9675. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9676. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9677. } else if (tensor->type == GGML_TYPE_I64) {
  9678. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9679. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9680. } else {
  9681. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  9682. }
  9683. } else {
  9684. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  9685. GGML_ABORT("fatal error");
  9686. }
  9687. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  9688. 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;
  9689. 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;
  9690. if (src0 != nullptr) {
  9691. 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;
  9692. }
  9693. if (src1 != nullptr) {
  9694. 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;
  9695. }
  9696. if (src2 != nullptr) {
  9697. 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;
  9698. }
  9699. if (src3 != nullptr) {
  9700. 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;
  9701. }
  9702. 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;
  9703. std::cerr << std::endl << "Result:" << std::endl;
  9704. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  9705. std::cerr << std::endl << "Correct:" << std::endl;
  9706. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  9707. std::cerr << std::endl;
  9708. std::vector<const ggml_tensor *> done;
  9709. ggml_vk_print_graph_origin(tensor, done);
  9710. GGML_ABORT("fatal error");
  9711. }
  9712. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  9713. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  9714. first_error[0] = i0;
  9715. first_error[1] = i1;
  9716. first_error[2] = i2;
  9717. first_error[3] = i3;
  9718. first_error_result = result;
  9719. first_error_correct = correct;
  9720. }
  9721. // Special case, value is infinite, avoid NaN result in avg_err
  9722. // NaN also appears in results, if both are nan error is 0
  9723. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  9724. avg_err += std::fabs(correct - result) / denom;
  9725. }
  9726. counter++;
  9727. }
  9728. }
  9729. }
  9730. }
  9731. avg_err /= counter;
  9732. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9733. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9734. 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;
  9735. if (src0 != nullptr) {
  9736. 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;
  9737. }
  9738. if (src1 != nullptr) {
  9739. 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;
  9740. }
  9741. if (src2 != nullptr) {
  9742. 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;
  9743. }
  9744. if (src3 != nullptr) {
  9745. 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;
  9746. }
  9747. 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;
  9748. std::cerr << std::endl << "Result:" << std::endl;
  9749. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9750. std::cerr << std::endl << "Correct:" << std::endl;
  9751. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  9752. std::cerr << std::endl;
  9753. std::vector<const ggml_tensor *> done;
  9754. ggml_vk_print_graph_origin(tensor, done);
  9755. }
  9756. if (avg_err > 0.5 || std::isnan(avg_err)) {
  9757. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9758. 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;
  9759. if (src0 != nullptr) {
  9760. 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;
  9761. }
  9762. if (src1 != nullptr) {
  9763. 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;
  9764. }
  9765. if (src2 != nullptr) {
  9766. 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;
  9767. }
  9768. if (src3 != nullptr) {
  9769. 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;
  9770. }
  9771. 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;
  9772. std::cerr << std::endl << "Result:" << std::endl;
  9773. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  9774. std::cerr << std::endl << "Correct:" << std::endl;
  9775. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  9776. std::cerr << std::endl;
  9777. std::vector<const ggml_tensor *> done;
  9778. ggml_vk_print_graph_origin(tensor, done);
  9779. GGML_ABORT("fatal error");
  9780. } else {
  9781. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  9782. }
  9783. free(comp_result);
  9784. comp_result = nullptr;
  9785. comp_size = 0;
  9786. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9787. free(tensor_data);
  9788. }
  9789. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  9790. }
  9791. #endif
  9792. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)