ggml-vulkan.cpp 727 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. // See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
  8. #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
  9. // We use VULKAN_HPP_DEFAULT_DISPATCHER, but not VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
  10. // to avoid conflicts with applications or other libraries who might use it.
  11. #if VK_HEADER_VERSION >= 301
  12. namespace vk::detail { class DispatchLoaderDynamic; }
  13. using vk::detail::DispatchLoaderDynamic;
  14. #else
  15. namespace vk { class DispatchLoaderDynamic; }
  16. using vk::DispatchLoaderDynamic;
  17. #endif
  18. DispatchLoaderDynamic & ggml_vk_default_dispatcher();
  19. #define VULKAN_HPP_DEFAULT_DISPATCHER ggml_vk_default_dispatcher()
  20. #include <vulkan/vulkan.hpp>
  21. #include <algorithm>
  22. #include <cmath>
  23. #include <iomanip>
  24. #include <iostream>
  25. #include <tuple>
  26. #include <vector>
  27. #include <sstream>
  28. #include <utility>
  29. #include <memory>
  30. #include <limits>
  31. #include <map>
  32. #include <unordered_map>
  33. #include <memory>
  34. #include <mutex>
  35. #include <future>
  36. #include <thread>
  37. #if defined(_MSC_VER)
  38. # define NOMINMAX 1
  39. # include <windows.h>
  40. # define YIELD() YieldProcessor()
  41. #elif defined(__clang__) || defined(__GNUC__)
  42. # if defined(__x86_64__) ||defined(__i386__)
  43. # include <immintrin.h>
  44. # define YIELD() _mm_pause()
  45. # elif defined(__arm__) || defined(__aarch64__)
  46. # if defined(__clang__)
  47. # include <arm_acle.h>
  48. # define YIELD() __yield()
  49. # else
  50. # define YIELD() asm volatile("yield")
  51. # endif
  52. # endif
  53. #endif
  54. #if !defined(YIELD)
  55. #define YIELD()
  56. #endif
  57. #include "ggml-impl.h"
  58. #include "ggml-backend-impl.h"
  59. #include "ggml-vulkan-shaders.hpp"
  60. // remove this once it's more widely available in the SDK
  61. #if !defined(VK_KHR_shader_bfloat16)
  62. #define VK_KHR_shader_bfloat16 1
  63. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  64. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  65. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  66. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  67. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  68. VkStructureType sType;
  69. void* pNext;
  70. VkBool32 shaderBFloat16Type;
  71. VkBool32 shaderBFloat16DotProduct;
  72. VkBool32 shaderBFloat16CooperativeMatrix;
  73. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  74. #endif
  75. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  76. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  77. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  78. #define VK_VENDOR_ID_AMD 0x1002
  79. #define VK_VENDOR_ID_APPLE 0x106b
  80. #define VK_VENDOR_ID_INTEL 0x8086
  81. #define VK_VENDOR_ID_NVIDIA 0x10de
  82. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  83. #define GGML_VK_MAX_NODES 8192
  84. #define VK_CHECK(err, msg) \
  85. do { \
  86. vk::Result err_ = (err); \
  87. if (err_ != vk::Result::eSuccess) { \
  88. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  89. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  90. exit(1); \
  91. } \
  92. } while (0)
  93. #ifdef GGML_VULKAN_DEBUG
  94. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  95. #else
  96. #define VK_LOG_DEBUG(msg) ((void) 0)
  97. #endif // GGML_VULKAN_DEBUG
  98. struct ggml_backend_vk_context;
  99. #define MAX_PARAMETER_COUNT 12
  100. // Max number of adds that can be fused without exceeding MAX_PARAMETER_COUNT.
  101. #define MAX_FUSED_ADDS (MAX_PARAMETER_COUNT - 3)
  102. struct vk_pipeline_struct {
  103. std::string name;
  104. vk::ShaderModule shader_module;
  105. vk::PipelineLayout layout;
  106. vk::Pipeline pipeline;
  107. uint32_t push_constant_size;
  108. uint32_t parameter_count;
  109. std::array<uint32_t, 3> wg_denoms;
  110. uint32_t align;
  111. // true if fields have been set by ggml_vk_create_pipeline
  112. bool initialized {};
  113. // set to true to request the pipeline is compiled
  114. bool needed {};
  115. // set to true when the shader has been compiled
  116. bool compiled {};
  117. // number of registers used, extracted from pipeline executable properties
  118. uint32_t register_count {};
  119. };
  120. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  121. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  122. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  123. struct vk_matmul_pipeline_struct {
  124. vk_pipeline l, m, s;
  125. vk_pipeline a_l, a_m, a_s;
  126. // Returns true when all unaligned pipelines are null.
  127. // We only check for unaligned variants since one of the unaligned pipelines must exist
  128. // while aligned pipelines are optional
  129. bool is_empty() const {
  130. return l == nullptr && m == nullptr && s == nullptr;
  131. }
  132. };
  133. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  134. struct vk_matmul_pipeline2 {
  135. vk_matmul_pipeline2() {
  136. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  137. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  138. }
  139. vk_matmul_pipeline f32acc;
  140. vk_matmul_pipeline f16acc;
  141. };
  142. struct vk_device_struct;
  143. typedef std::shared_ptr<vk_device_struct> vk_device;
  144. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  145. struct vk_buffer_struct;
  146. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  147. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  148. struct ggml_backend_vk_buffer_type_context {
  149. std::string name;
  150. vk_device device;
  151. };
  152. struct vk_queue;
  153. // Stores command pool/buffers. There's an instance of this
  154. // for each (context,queue) pair and for each (device,queue) pair.
  155. struct vk_command_pool {
  156. void init(vk_device& device, vk_queue *q_);
  157. void destroy(vk::Device& device);
  158. vk::CommandPool pool;
  159. uint32_t cmd_buffer_idx;
  160. std::vector<vk::CommandBuffer> cmd_buffers;
  161. vk_queue *q;
  162. };
  163. // Prevent simultaneous submissions to the same queue.
  164. // This could be per vk_queue if we stopped having two vk_queue structures
  165. // sharing the same vk::Queue.
  166. static std::mutex queue_mutex;
  167. struct vk_queue {
  168. uint32_t queue_family_index;
  169. vk::Queue queue;
  170. vk_command_pool cmd_pool;
  171. vk::PipelineStageFlags stage_flags;
  172. bool transfer_only;
  173. // copy everything except the cmd_pool
  174. void copyFrom(vk_queue &other) {
  175. queue_family_index = other.queue_family_index;
  176. queue = other.queue;
  177. stage_flags = other.stage_flags;
  178. transfer_only = other.transfer_only;
  179. }
  180. };
  181. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  182. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  183. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  184. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  185. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  186. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  187. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  188. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  189. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  190. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  191. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  192. /* .is_host = */ NULL,
  193. };
  194. #ifdef GGML_VULKAN_MEMORY_DEBUG
  195. class vk_memory_logger;
  196. #endif
  197. class vk_perf_logger;
  198. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  199. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  200. static constexpr uint32_t p021_max_gqa_ratio = 8;
  201. enum vk_device_architecture {
  202. OTHER,
  203. AMD_GCN,
  204. AMD_RDNA1,
  205. AMD_RDNA2,
  206. AMD_RDNA3,
  207. INTEL_XE2,
  208. NVIDIA_PRE_TURING,
  209. };
  210. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  211. vk::PhysicalDeviceProperties props = device.getProperties();
  212. if (props.vendorID == VK_VENDOR_ID_AMD) {
  213. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  214. bool amd_shader_core_properties = false;
  215. bool integer_dot_product = false;
  216. bool subgroup_size_control = false;
  217. for (const auto& properties : ext_props) {
  218. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  219. amd_shader_core_properties = true;
  220. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  221. integer_dot_product = true;
  222. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  223. subgroup_size_control = true;
  224. }
  225. }
  226. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  227. return vk_device_architecture::OTHER;
  228. }
  229. vk::PhysicalDeviceProperties2 props2;
  230. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  231. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  232. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  233. props2.pNext = &shader_core_props_amd;
  234. shader_core_props_amd.pNext = &integer_dot_props;
  235. integer_dot_props.pNext = &subgroup_size_control_props;
  236. device.getProperties2(&props2);
  237. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  238. return vk_device_architecture::AMD_GCN;
  239. }
  240. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  241. // RDNA
  242. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  243. return vk_device_architecture::AMD_RDNA1;
  244. }
  245. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  246. return vk_device_architecture::AMD_RDNA3;
  247. }
  248. return vk_device_architecture::AMD_RDNA2;
  249. }
  250. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  251. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  252. bool subgroup_size_control = false;
  253. for (const auto& properties : ext_props) {
  254. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  255. subgroup_size_control = true;
  256. }
  257. }
  258. if (!subgroup_size_control) {
  259. return vk_device_architecture::OTHER;
  260. }
  261. vk::PhysicalDeviceProperties2 props2;
  262. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  263. props2.pNext = &subgroup_size_control_props;
  264. device.getProperties2(&props2);
  265. if (subgroup_size_control_props.minSubgroupSize == 16) {
  266. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  267. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  268. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  269. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  270. return vk_device_architecture::INTEL_XE2;
  271. }
  272. } else if (props.vendorID == VK_VENDOR_ID_NVIDIA) {
  273. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  274. bool cooperative_matrix = false;
  275. // Detect "pre-turing" based on lack of coopmat support.
  276. for (const auto& properties : ext_props) {
  277. if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0) {
  278. cooperative_matrix = true;
  279. break;
  280. }
  281. }
  282. if (!cooperative_matrix) {
  283. return vk_device_architecture::NVIDIA_PRE_TURING;
  284. }
  285. }
  286. return vk_device_architecture::OTHER;
  287. }
  288. enum vk_conv_shapes {
  289. CONV_SHAPE_128x128,
  290. CONV_SHAPE_64x32,
  291. CONV_SHAPE_32x256,
  292. CONV_SHAPE_COUNT,
  293. };
  294. enum dmmv_wg_sizes {
  295. DMMV_WG_SIZE_SUBGROUP,
  296. DMMV_WG_SIZE_LARGE,
  297. DMMV_WG_SIZE_COUNT,
  298. };
  299. enum FaCodePath {
  300. FA_SCALAR,
  301. FA_COOPMAT1,
  302. FA_COOPMAT2,
  303. };
  304. struct vk_fa_pipeline_state {
  305. vk_fa_pipeline_state(uint32_t HSK, uint32_t HSV, bool small_rows, FaCodePath path, bool aligned, bool f32acc)
  306. : HSK(HSK), HSV(HSV), small_rows(small_rows), path(path), aligned(aligned), f32acc(f32acc) {}
  307. uint32_t HSK, HSV;
  308. bool small_rows;
  309. FaCodePath path;
  310. bool aligned;
  311. bool f32acc;
  312. bool operator<(const vk_fa_pipeline_state &b) const {
  313. return std::tie(HSK, HSV, small_rows, path, aligned, f32acc) <
  314. std::tie(b.HSK, b.HSV, b.small_rows, b.path, b.aligned, b.f32acc);
  315. }
  316. };
  317. enum shader_reduction_mode {
  318. SHADER_REDUCTION_MODE_SHMEM,
  319. SHADER_REDUCTION_MODE_HYBRID,
  320. SHADER_REDUCTION_MODE_SUBGROUP,
  321. SHADER_REDUCTION_MODE_COUNT,
  322. };
  323. static constexpr uint32_t num_argsort_pipelines = 11;
  324. static constexpr uint32_t max_argsort_cols = 1 << (num_argsort_pipelines-1);
  325. static constexpr uint32_t num_topk_moe_pipelines = 10;
  326. static constexpr std::initializer_list<ggml_op> topk_moe_early_softmax_norm{ GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT,
  327. GGML_OP_VIEW, GGML_OP_GET_ROWS, GGML_OP_RESHAPE,
  328. GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV,
  329. GGML_OP_RESHAPE };
  330. static constexpr std::initializer_list<ggml_op> topk_moe_early_softmax { GGML_OP_SOFT_MAX, GGML_OP_RESHAPE, GGML_OP_ARGSORT,
  331. GGML_OP_VIEW, GGML_OP_GET_ROWS };
  332. static constexpr std::initializer_list<ggml_op> topk_moe_late_softmax { GGML_OP_ARGSORT, GGML_OP_VIEW,
  333. GGML_OP_GET_ROWS, GGML_OP_RESHAPE,
  334. GGML_OP_SOFT_MAX, GGML_OP_RESHAPE };
  335. //node #978 ( SOFT_MAX): ffn_moe_probs-15 ( 0K) [Vulka ] use=2: ffn_moe_logits-15 ( 0K) [Vulka ]
  336. //node #979 ( RESHAPE): ffn_moe_probs-15 (re ( 0K) [Vulka ] use=1: ffn_moe_probs-15 ( 0K) [Vulka ]
  337. //node #980 ( ARGSORT): ffn_moe_argsort-15 ( 0K) [Vulka ] use=1: ffn_moe_probs-15 ( 0K) [Vulka ]
  338. //node #981 ( VIEW): ffn_moe_topk-15 ( 0K) [Vulka ] use=4: ffn_moe_argsort-15 ( 0K) [Vulka ]
  339. //node #982 ( GET_ROWS): ffn_moe_weights-15 ( 0K) [Vulka ] use=1: ffn_moe_probs-15 (re ( 0K) [Vulka ] ffn_moe_topk-15 ( 0K) [Vulka ]
  340. //node #983 ( RESHAPE): ffn_moe_weights-15 ( ( 0K) [Vulka ] use=2: ffn_moe_weights-15 ( 0K) [Vulka ]
  341. //node #984 ( SUM_ROWS): ffn_moe_weights_sum- ( 0K) [Vulka ] use=1: ffn_moe_weights-15 ( ( 0K) [Vulka ]
  342. //node #985 ( CLAMP): ffn_moe_weights_sum_ ( 0K) [Vulka ] use=1: ffn_moe_weights_sum- ( 0K) [Vulka ]
  343. //node #986 ( DIV): ffn_moe_weights_norm ( 0K) [Vulka ] use=1: ffn_moe_weights-15 ( ( 0K) [Vulka ] ffn_moe_weights_sum_ ( 0K) [Vulka ]
  344. //node #987 ( RESHAPE): ffn_moe_weights_norm ( 0K) [Vulka ] use=1: ffn_moe_weights_norm ( 0K) [Vulka ]
  345. static constexpr std::initializer_list<std::array<int, 3>> topk_moe_early_softmax_norm_edges {
  346. { 1, 0, 0 }, // reshape->src[0] == softmax
  347. { 2, 0, 0 }, // argsort->src[0] == softmax
  348. { 3, 0, 2 }, // view->src[0] == argsort
  349. { 4, 0, 1 }, // get_rows->src[0] == reshape
  350. { 4, 1, 3 }, // get_rows->src[1] == view
  351. { 5, 0, 4 }, // reshape->src[0] == get_rows
  352. { 6, 0, 5 }, // sum_rows->src[0] == reshape
  353. { 7, 0, 6 }, // clamp->src[0] == sum_rows
  354. { 8, 0, 5 }, // div->src[0] == reshape
  355. { 8, 1, 7 }, // div->src[1] == clamp
  356. { 9, 0, 8 }, // reshape->src[0] == div
  357. };
  358. // same as early_softmax_norm but ending after the get_rows
  359. static constexpr std::initializer_list<std::array<int, 3>> topk_moe_early_softmax_edges {
  360. { 1, 0, 0 }, // reshape->src[0] == softmax
  361. { 2, 0, 0 }, // argsort->src[0] == softmax
  362. { 3, 0, 2 }, // view->src[0] == argsort
  363. { 4, 0, 1 }, // get_rows->src[0] == reshape
  364. { 4, 1, 3 }, // get_rows->src[1] == view
  365. };
  366. //node #652 ( ARGSORT): ffn_moe_argsort-11 ( 0K) [Vulka ] use=1: ffn_moe_probs-11 ( 0K) [Vulka ]
  367. //node #653 ( VIEW): ffn_moe_topk-11 ( 0K) [Vulka ] use=7: ffn_moe_argsort-11 ( 0K) [Vulka ]
  368. //node #654 ( GET_ROWS): ffn_moe_weights-11 ( 0K) [Vulka ] use=1: ffn_moe_probs-11 (re ( 0K) [Vulka ] ffn_moe_topk-11 ( 0K) [Vulka ]
  369. //node #655 ( RESHAPE): ffn_moe_weights-11 ( ( 0K) [Vulka ] use=1: ffn_moe_weights-11 ( 0K) [Vulka ]
  370. //node #656 ( SOFT_MAX): node_656 ( 0K) [Vulka ] use=1: ffn_moe_weights-11 ( ( 0K) [Vulka ]
  371. //node #657 ( RESHAPE): ffn_moe_weights_soft ( 0K) [Vulka ] use=1: node_656 ( 0K) [Vulka ]
  372. static constexpr std::initializer_list<std::array<int, 3>> topk_moe_late_softmax_edges {
  373. { 1, 0, 0 }, // view->src[0] == argsort
  374. { 2, 1, 1 }, // get_rows->src[1] == view
  375. { 3, 0, 2 }, // reshape->src[0] == get_rows
  376. { 4, 0, 3 }, // soft_max->src[0] == reshape
  377. { 5, 0, 4 }, // reshape->src[0] == soft_max
  378. };
  379. enum topk_moe_mode {
  380. TOPK_MOE_EARLY_SOFTMAX,
  381. TOPK_MOE_EARLY_SOFTMAX_NORM,
  382. TOPK_MOE_LATE_SOFTMAX,
  383. TOPK_MOE_COUNT,
  384. };
  385. static topk_moe_mode ggml_vk_num_additional_ops_to_topk_moe_mode(uint32_t num) {
  386. topk_moe_mode mode = num == topk_moe_early_softmax_norm.size() - 1 ? TOPK_MOE_EARLY_SOFTMAX_NORM :
  387. num == topk_moe_early_softmax.size() - 1 ? TOPK_MOE_EARLY_SOFTMAX :
  388. TOPK_MOE_LATE_SOFTMAX;
  389. return mode;
  390. }
  391. static constexpr std::initializer_list<std::array<int, 3>> rope_view_set_rows_edges {
  392. { 1, 0, 0 }, // view->src[0] == rope
  393. { 2, 0, 1 }, // set_rows->src[0] == view
  394. };
  395. struct vk_device_struct {
  396. std::recursive_mutex mutex;
  397. vk::PhysicalDevice physical_device;
  398. vk::PhysicalDeviceProperties properties;
  399. std::string name;
  400. uint64_t max_memory_allocation_size;
  401. uint64_t max_buffer_size;
  402. uint64_t suballocation_block_size;
  403. bool fp16;
  404. bool bf16;
  405. bool pipeline_robustness;
  406. vk::Device device;
  407. uint32_t vendor_id;
  408. vk::DriverId driver_id;
  409. vk_device_architecture architecture;
  410. vk_queue compute_queue;
  411. vk_queue transfer_queue;
  412. bool single_queue;
  413. uint32_t subgroup_size;
  414. uint32_t shader_core_count;
  415. bool uma;
  416. bool prefer_host_memory;
  417. bool float_controls_rte_fp16;
  418. bool subgroup_arithmetic;
  419. bool subgroup_shuffle;
  420. bool subgroup_ballot;
  421. bool subgroup_clustered;
  422. bool multi_add;
  423. bool shader_int64;
  424. bool buffer_device_address;
  425. bool add_rms_fusion;
  426. uint32_t partials_binding_alignment;
  427. bool integer_dot_product;
  428. // 0: default, 1: force mmvq, -1: disable mmvq
  429. int32_t mmvq_mode;
  430. bool subgroup_size_control;
  431. uint32_t subgroup_min_size;
  432. uint32_t subgroup_max_size;
  433. bool subgroup_require_full_support;
  434. bool coopmat_support;
  435. bool coopmat_acc_f32_support {};
  436. bool coopmat_acc_f16_support {};
  437. bool coopmat_bf16_support {};
  438. bool coopmat_support_16x16x16_f16acc {};
  439. bool coopmat_support_16x16x16_f32acc {};
  440. bool coopmat1_fa_support {};
  441. uint32_t coopmat_m;
  442. uint32_t coopmat_n;
  443. uint32_t coopmat_k;
  444. bool coopmat_int_support;
  445. uint32_t coopmat_int_m;
  446. uint32_t coopmat_int_n;
  447. uint32_t coopmat_int_k;
  448. bool coopmat2;
  449. bool pipeline_executable_properties_support {};
  450. size_t idx;
  451. bool mul_mat_l[GGML_TYPE_COUNT];
  452. bool mul_mat_m[GGML_TYPE_COUNT];
  453. bool mul_mat_s[GGML_TYPE_COUNT];
  454. bool mul_mat_id_l[GGML_TYPE_COUNT];
  455. bool mul_mat_id_m[GGML_TYPE_COUNT];
  456. bool mul_mat_id_s[GGML_TYPE_COUNT];
  457. vk::DescriptorSetLayout dsl;
  458. vk_matmul_pipeline pipeline_matmul_f32 {};
  459. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  460. vk_matmul_pipeline pipeline_matmul_bf16 {};
  461. vk_matmul_pipeline2 pipeline_matmul_f16;
  462. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  463. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  464. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  465. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  466. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  467. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  468. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  469. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  470. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  471. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_COUNT];
  472. vk_pipeline pipeline_matmul_split_k_reduce;
  473. vk_pipeline pipeline_quantize_q8_1;
  474. vk_pipeline pipeline_quantize_q8_1_x4;
  475. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  476. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  477. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  478. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  479. vk_pipeline pipeline_dequant_mul_mat_vec_q8_1_f32[DMMV_WG_SIZE_COUNT][GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  480. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  481. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  482. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  483. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  484. vk_pipeline pipeline_acc_f32;
  485. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  486. vk_pipeline pipeline_add[2][2][2];
  487. vk_pipeline pipeline_add_norepeat[2][2][2];
  488. vk_pipeline pipeline_sub[2][2][2];
  489. vk_pipeline pipeline_sub_norepeat[2][2][2];
  490. vk_pipeline pipeline_mul[2][2][2];
  491. vk_pipeline pipeline_mul_norepeat[2][2][2];
  492. vk_pipeline pipeline_div[2][2][2];
  493. vk_pipeline pipeline_div_norepeat[2][2][2];
  494. vk_pipeline pipeline_add_rms[2][2][2];
  495. vk_pipeline pipeline_add_rms_norepeat[2][2][2];
  496. // indexed by num_additional_fused_ops == num_adds - 1
  497. vk_pipeline pipeline_multi_add[MAX_FUSED_ADDS];
  498. vk_pipeline pipeline_multi_add_rms[MAX_FUSED_ADDS];
  499. vk_pipeline pipeline_add_id_f32;
  500. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  501. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32;
  502. vk_pipeline pipeline_scale_f32;
  503. vk_pipeline pipeline_sqr_f32;
  504. vk_pipeline pipeline_sqrt_f32;
  505. vk_pipeline pipeline_sin_f32;
  506. vk_pipeline pipeline_cos_f32;
  507. vk_pipeline pipeline_clamp_f32;
  508. vk_pipeline pipeline_pad_f32;
  509. vk_pipeline pipeline_roll_f32;
  510. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  511. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16, pipeline_cpy_f32_i32, pipeline_cpy_i32_f32;
  512. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16, pipeline_contig_cpy_f32_i32, pipeline_contig_cpy_i32_f32;
  513. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  514. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  515. vk_pipeline pipeline_set_rows_i32[GGML_TYPE_COUNT];
  516. vk_pipeline pipeline_set_rows_i64[GGML_TYPE_COUNT];
  517. vk_pipeline pipeline_norm_f32;
  518. vk_pipeline pipeline_group_norm_f32;
  519. vk_pipeline pipeline_rms_norm_f32;
  520. vk_pipeline pipeline_rms_norm_mul_f32;
  521. vk_pipeline pipeline_rms_norm_partials_f32;
  522. vk_pipeline pipeline_rms_norm_mul_partials_f32;
  523. vk_pipeline pipeline_rms_norm_back_f32;
  524. vk_pipeline pipeline_l2_norm_f32;
  525. // [src/dst 0=fp32,1=fp16]
  526. vk_pipeline pipeline_exp[2];
  527. vk_pipeline pipeline_gelu[2];
  528. vk_pipeline pipeline_gelu_erf[2];
  529. vk_pipeline pipeline_gelu_quick[2];
  530. vk_pipeline pipeline_silu[2];
  531. vk_pipeline pipeline_relu[2];
  532. vk_pipeline pipeline_tanh[2];
  533. vk_pipeline pipeline_sigmoid[2];
  534. vk_pipeline pipeline_hardsigmoid[2];
  535. vk_pipeline pipeline_hardswish[2];
  536. vk_pipeline pipeline_geglu[2];
  537. vk_pipeline pipeline_reglu[2];
  538. vk_pipeline pipeline_swiglu[2];
  539. vk_pipeline pipeline_swiglu_oai[2];
  540. vk_pipeline pipeline_geglu_erf[2];
  541. vk_pipeline pipeline_geglu_quick[2];
  542. vk_pipeline pipeline_leaky_relu_f32;
  543. vk_pipeline pipeline_silu_back_f32;
  544. vk_pipeline pipeline_diag_mask_inf_f32;
  545. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  546. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  547. vk_pipeline pipeline_soft_max_back_f32;
  548. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16, pipeline_rope_norm_f32_f16;
  549. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16, pipeline_rope_neox_f32_f16;
  550. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  551. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  552. vk_pipeline pipeline_argsort_f32[num_argsort_pipelines];
  553. vk_pipeline pipeline_sum_rows_f32;
  554. vk_pipeline pipeline_argmax_f32;
  555. vk_pipeline pipeline_count_equal_i32;
  556. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  557. vk_pipeline pipeline_im2col_3d_f32, pipeline_im2col_3d_f32_f16;
  558. vk_pipeline pipeline_timestep_embedding_f32;
  559. vk_pipeline pipeline_conv_transpose_1d_f32;
  560. vk_pipeline pipeline_pool2d_f32;
  561. vk_pipeline pipeline_rwkv_wkv6_f32;
  562. vk_pipeline pipeline_rwkv_wkv7_f32;
  563. vk_pipeline pipeline_ssm_scan_f32_d128;
  564. vk_pipeline pipeline_ssm_scan_f32_d256;
  565. vk_pipeline pipeline_ssm_conv_f32;
  566. vk_pipeline pipeline_opt_step_adamw_f32;
  567. vk_pipeline pipeline_opt_step_sgd_f32;
  568. vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
  569. vk_pipeline pipeline_conv2d_f16_f32[CONV_SHAPE_COUNT];
  570. vk_pipeline pipeline_conv_transpose_2d_f32[CONV_SHAPE_COUNT];
  571. vk_pipeline pipeline_conv_transpose_2d_f16_f32[CONV_SHAPE_COUNT];
  572. vk_pipeline pipeline_conv2d_dw_whcn_f32, pipeline_conv2d_dw_whcn_f16_f32;
  573. vk_pipeline pipeline_conv2d_dw_cwhn_f32, pipeline_conv2d_dw_cwhn_f16_f32;
  574. std::map<vk_fa_pipeline_state, vk_pipeline> pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT];
  575. vk_pipeline pipeline_flash_attn_split_k_reduce;
  576. vk_pipeline pipeline_topk_moe[num_topk_moe_pipelines][TOPK_MOE_COUNT];
  577. std::vector<vk_pipeline_ref> all_pipelines;
  578. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  579. vk::Fence fence;
  580. vk_buffer sync_staging;
  581. ggml_backend_buffer_type buffer_type;
  582. bool disable_fusion;
  583. bool disable_host_visible_vidmem;
  584. bool allow_sysmem_fallback;
  585. bool disable_graph_optimize;
  586. #ifdef GGML_VULKAN_MEMORY_DEBUG
  587. std::unique_ptr<vk_memory_logger> memory_logger;
  588. #endif
  589. // for GGML_VK_PERF_LOGGER
  590. std::unique_ptr<vk_perf_logger> perf_logger;
  591. vk::QueryPool query_pool;
  592. int32_t num_queries;
  593. ~vk_device_struct() {
  594. VK_LOG_DEBUG("destroy device " << name);
  595. device.destroyFence(fence);
  596. ggml_vk_destroy_buffer(sync_staging);
  597. compute_queue.cmd_pool.destroy(device);
  598. transfer_queue.cmd_pool.destroy(device);
  599. for (auto& pipeline : all_pipelines) {
  600. if (pipeline.expired()) {
  601. continue;
  602. }
  603. vk_pipeline pl = pipeline.lock();
  604. ggml_vk_destroy_pipeline(device, pl);
  605. }
  606. all_pipelines.clear();
  607. device.destroyDescriptorSetLayout(dsl);
  608. device.destroy();
  609. }
  610. };
  611. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  612. cmd_buffer_idx = 0;
  613. q = q_;
  614. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  615. pool = device->device.createCommandPool(command_pool_create_info);
  616. }
  617. void vk_command_pool::destroy(vk::Device& device) {
  618. device.destroyCommandPool(pool);
  619. pool = nullptr;
  620. cmd_buffers.clear();
  621. }
  622. struct vk_buffer_struct {
  623. vk::Buffer buffer = VK_NULL_HANDLE;
  624. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  625. vk::MemoryPropertyFlags memory_property_flags;
  626. void * ptr;
  627. size_t size = 0;
  628. vk::DeviceAddress bda_addr {};
  629. vk_device device;
  630. ~vk_buffer_struct() {
  631. if (size == 0) {
  632. return;
  633. }
  634. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  635. device->device.freeMemory(device_memory);
  636. device->device.destroyBuffer(buffer);
  637. }
  638. };
  639. struct vk_subbuffer {
  640. vk_buffer buffer;
  641. uint64_t offset;
  642. uint64_t size;
  643. operator vk::DescriptorBufferInfo() const {
  644. return { buffer->buffer, offset, size };
  645. }
  646. };
  647. struct vk_semaphore {
  648. vk::Semaphore s;
  649. uint64_t value;
  650. };
  651. struct vk_submission {
  652. vk::CommandBuffer buffer;
  653. std::vector<vk_semaphore> wait_semaphores;
  654. std::vector<vk_semaphore> signal_semaphores;
  655. };
  656. typedef std::vector<vk_submission> vk_sequence;
  657. struct vk_mat_mat_push_constants {
  658. uint32_t M; uint32_t N; uint32_t K;
  659. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  660. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  661. uint32_t k_split;
  662. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  663. uint32_t padded_N;
  664. };
  665. struct vk_mat_vec_push_constants {
  666. uint32_t ncols;
  667. uint32_t stride_a;
  668. uint32_t stride_b;
  669. uint32_t stride_d;
  670. uint32_t batch_stride_a;
  671. uint32_t batch_stride_b;
  672. uint32_t batch_stride_d;
  673. uint32_t enable_bias;
  674. uint32_t ne02;
  675. uint32_t ne12;
  676. uint32_t broadcast2;
  677. uint32_t broadcast3;
  678. };
  679. struct vk_mat_mat_id_push_constants {
  680. uint32_t M; uint32_t N; uint32_t K;
  681. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  682. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  683. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  684. uint32_t padded_N;
  685. };
  686. struct vk_mat_vec_id_push_constants {
  687. uint32_t ncols;
  688. uint32_t stride_a;
  689. uint32_t stride_b;
  690. uint32_t stride_d;
  691. uint32_t batch_stride_a;
  692. uint32_t batch_stride_b;
  693. uint32_t batch_stride_d;
  694. uint32_t enable_bias;
  695. uint32_t nei0;
  696. uint32_t ne11;
  697. };
  698. struct vk_flash_attn_push_constants {
  699. uint32_t N;
  700. uint32_t KV;
  701. uint32_t ne1;
  702. uint32_t ne2;
  703. uint32_t ne3;
  704. uint32_t neq2;
  705. uint32_t neq3;
  706. uint32_t nek2;
  707. uint32_t nek3;
  708. uint32_t nev2;
  709. uint32_t nev3;
  710. uint32_t nem1;
  711. uint32_t nem2;
  712. uint32_t nem3;
  713. uint32_t nb01;
  714. uint32_t nb02;
  715. uint32_t nb03;
  716. uint32_t nb11;
  717. uint32_t nb12;
  718. uint32_t nb13;
  719. uint32_t nb21;
  720. uint32_t nb22;
  721. uint32_t nb23;
  722. float scale;
  723. float max_bias;
  724. float logit_softcap;
  725. uint32_t mask_n_head_log2;
  726. float m0;
  727. float m1;
  728. uint32_t gqa_ratio;
  729. uint32_t split_kv;
  730. uint32_t k_num;
  731. };
  732. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  733. struct vk_op_push_constants {
  734. uint32_t KX;
  735. uint32_t KY;
  736. float param1;
  737. float param2;
  738. };
  739. struct vk_op_glu_push_constants {
  740. uint32_t N;
  741. uint32_t ne00;
  742. uint32_t ne20;
  743. uint32_t mode; // 0: default, 1: swapped, 2: split
  744. float alpha; // for swiglu_oai
  745. float limit;
  746. };
  747. struct vk_op_unary_push_constants {
  748. uint32_t ne;
  749. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  750. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  751. uint32_t misalign_offsets;
  752. float param1; float param2;
  753. uint32_t ne0_012mp; uint32_t ne0_012L;
  754. uint32_t ne0_01mp; uint32_t ne0_01L;
  755. uint32_t ne0_0mp; uint32_t ne0_0L;
  756. uint32_t ne1_012mp; uint32_t ne1_012L;
  757. uint32_t ne1_01mp; uint32_t ne1_01L;
  758. uint32_t ne1_0mp; uint32_t ne1_0L;
  759. };
  760. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  761. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  762. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  763. ne = ne != 0 ? ne : ggml_nelements(dst);
  764. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  765. vk_op_unary_push_constants p{};
  766. p.ne = (uint32_t)ne;
  767. size_t src0_tsize = ggml_type_size(src0->type);
  768. p.ne00 = (uint32_t)src0->ne[0];
  769. p.ne01 = (uint32_t)src0->ne[1];
  770. p.ne02 = (uint32_t)src0->ne[2];
  771. p.ne03 = (uint32_t)src0->ne[3];
  772. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  773. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  774. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  775. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  776. size_t dst_tsize = ggml_type_size(dst->type);
  777. p.ne10 = (uint32_t)dst->ne[0];
  778. p.ne11 = (uint32_t)dst->ne[1];
  779. p.ne12 = (uint32_t)dst->ne[2];
  780. p.ne13 = (uint32_t)dst->ne[3];
  781. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  782. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  783. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  784. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  785. return p; // offsets are initialized later in ggml_vk_op
  786. }
  787. struct vk_op_pad_push_constants {
  788. uint32_t ne;
  789. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  790. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  791. uint32_t misalign_offsets;
  792. uint32_t lp0; uint32_t rp0;
  793. uint32_t lp1; uint32_t rp1;
  794. uint32_t lp2; uint32_t rp2;
  795. uint32_t lp3; uint32_t rp3;
  796. };
  797. static vk_op_pad_push_constants vk_op_pad_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst) {
  798. int64_t ne = ggml_nelements(dst);
  799. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  800. vk_op_pad_push_constants p{};
  801. p.ne = (uint32_t)ne;
  802. size_t src0_tsize = ggml_type_size(src0->type);
  803. p.ne00 = (uint32_t)src0->ne[0];
  804. p.ne01 = (uint32_t)src0->ne[1];
  805. p.ne02 = (uint32_t)src0->ne[2];
  806. p.ne03 = (uint32_t)src0->ne[3];
  807. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  808. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  809. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  810. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  811. size_t dst_tsize = ggml_type_size(dst->type);
  812. p.ne10 = (uint32_t)dst->ne[0];
  813. p.ne11 = (uint32_t)dst->ne[1];
  814. p.ne12 = (uint32_t)dst->ne[2];
  815. p.ne13 = (uint32_t)dst->ne[3];
  816. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  817. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  818. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  819. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  820. p.lp0 = dst->op_params[0];
  821. p.rp0 = dst->op_params[1];
  822. p.lp1 = dst->op_params[2];
  823. p.rp1 = dst->op_params[3];
  824. p.lp2 = dst->op_params[4];
  825. p.rp2 = dst->op_params[5];
  826. p.lp3 = dst->op_params[6];
  827. p.rp3 = dst->op_params[7];
  828. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  829. }
  830. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  831. // Precompute mp (m' in the paper) and L such that division
  832. // can be computed using a multiply (high 32b of 64b result)
  833. // and a shift:
  834. //
  835. // n/d = (mulhi(n, mp) + n) >> L;
  836. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  837. {
  838. // compute L = ceil(log2(d));
  839. L = 0;
  840. while (L < 32 && (uint32_t{1} << L) < d) {
  841. L++;
  842. }
  843. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  844. }
  845. template <typename T> void init_pushconst_fastdiv(T &p) {
  846. GGML_UNUSED(p);
  847. static_assert(!std::is_const<T>::value, "unexpected type");
  848. }
  849. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  850. // Compute magic values to divide by these six numbers.
  851. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  852. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  853. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  854. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  855. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  856. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  857. }
  858. struct vk_op_binary_push_constants {
  859. uint32_t ne;
  860. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  861. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  862. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  863. uint32_t misalign_offsets;
  864. float param1; float param2; int32_t param3;
  865. };
  866. struct vk_op_multi_add_push_constants {
  867. // shape for dst
  868. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23;
  869. // strides for srcs+dst
  870. uint32_t nb[MAX_PARAMETER_COUNT][4];
  871. uint32_t rms_partials;
  872. };
  873. // update multi_add.comp if this changes
  874. static_assert(MAX_PARAMETER_COUNT == 12);
  875. static_assert(sizeof(vk_op_multi_add_push_constants) <= 256);
  876. struct vk_op_topk_moe_push_constants {
  877. uint32_t n_rows;
  878. uint32_t n_expert_used;
  879. float clamp_min;
  880. float clamp_max;
  881. };
  882. struct vk_op_add_id_push_constants {
  883. uint32_t ne0;
  884. uint32_t ne1;
  885. uint32_t s01;
  886. uint32_t s02;
  887. uint32_t s11;
  888. uint32_t s21;
  889. };
  890. struct vk_op_diag_mask_push_constants {
  891. uint32_t ncols;
  892. uint32_t rows_per_channel;
  893. int32_t n_past;
  894. };
  895. struct vk_op_rope_push_constants {
  896. uint32_t ncols;
  897. uint32_t n_dims;
  898. float freq_scale;
  899. uint32_t p_delta_rows;
  900. float freq_base;
  901. float ext_factor;
  902. float attn_factor;
  903. float corr_dims[2];
  904. float theta_scale;
  905. uint32_t has_ff;
  906. uint32_t ne02;
  907. uint32_t s1;
  908. uint32_t s2;
  909. int32_t sections[4];
  910. uint32_t is_imrope;
  911. uint32_t is_back;
  912. uint32_t set_rows_stride;
  913. };
  914. struct vk_op_soft_max_push_constants {
  915. uint32_t KX;
  916. uint32_t KY;
  917. uint32_t ne00;
  918. uint32_t ne01;
  919. uint32_t ne02;
  920. uint32_t ne12;
  921. uint32_t ne13;
  922. uint32_t nb11;
  923. uint32_t nb12;
  924. uint32_t nb13;
  925. float scale;
  926. float max_bias;
  927. float m0;
  928. float m1;
  929. uint32_t n_head_log2;
  930. uint32_t nrows_x;
  931. uint32_t has_sinks;
  932. };
  933. struct vk_op_argsort_push_constants {
  934. uint32_t ncols;
  935. uint32_t nrows;
  936. int32_t order;
  937. };
  938. struct vk_op_im2col_push_constants {
  939. uint64_t dst_addr;
  940. uint32_t batch_offset; uint32_t offset_delta;
  941. uint32_t IC;
  942. uint32_t IW; uint32_t IH;
  943. uint32_t OW; uint32_t OH;
  944. uint32_t KW; uint32_t KH;
  945. uint32_t pelements;
  946. uint32_t CHW;
  947. int32_t s0; int32_t s1;
  948. int32_t p0; int32_t p1;
  949. int32_t d0; int32_t d1;
  950. };
  951. struct vk_op_im2col_3d_push_constants {
  952. uint64_t dst_addr;
  953. uint32_t nb10;
  954. uint32_t nb11;
  955. uint32_t nb12;
  956. uint32_t nb13;
  957. uint32_t s0;
  958. uint32_t s1;
  959. uint32_t s2;
  960. uint32_t p0;
  961. uint32_t p1;
  962. uint32_t p2;
  963. uint32_t d0;
  964. uint32_t d1;
  965. uint32_t d2;
  966. uint32_t IW;
  967. uint32_t IH;
  968. uint32_t ID;
  969. uint32_t IC;
  970. uint32_t KW;
  971. uint32_t OH;
  972. uint32_t KD_KH_KW;
  973. uint32_t KH_KW;
  974. uint32_t IC_KD_KH_KW;
  975. uint32_t N_OD_OH;
  976. uint32_t OD_OH;
  977. uint32_t OD_OH_OW_IC_KD_KH_KW;
  978. uint32_t OH_OW_IC_KD_KH_KW;
  979. uint32_t OW_IC_KD_KH_KW;
  980. uint32_t misalign_offsets;
  981. };
  982. struct vk_op_timestep_embedding_push_constants {
  983. uint32_t nb1;
  984. uint32_t dim;
  985. uint32_t max_period;
  986. };
  987. struct vk_op_conv_transpose_1d_push_constants {
  988. uint32_t Cout;
  989. uint32_t Cin;
  990. uint32_t K;
  991. uint32_t L;
  992. uint32_t KL;
  993. uint32_t nb01;
  994. uint32_t nb02;
  995. uint32_t nb11;
  996. uint32_t nb1;
  997. int32_t s0;
  998. };
  999. struct vk_op_pool2d_push_constants {
  1000. uint32_t IW; uint32_t IH;
  1001. uint32_t OW; uint32_t OH;
  1002. uint32_t OC;
  1003. uint32_t pelements;
  1004. uint32_t op;
  1005. int32_t k0; int32_t k1;
  1006. int32_t s0; int32_t s1;
  1007. int32_t p0; int32_t p1;
  1008. };
  1009. struct vk_op_rwkv_wkv6_push_constants {
  1010. uint32_t B;
  1011. uint32_t T;
  1012. uint32_t C;
  1013. uint32_t H;
  1014. };
  1015. struct vk_op_rwkv_wkv7_push_constants {
  1016. uint32_t B;
  1017. uint32_t T;
  1018. uint32_t C;
  1019. uint32_t H;
  1020. };
  1021. struct vk_op_ssm_scan_push_constants {
  1022. uint32_t nb02, nb03, nb12, nb13;
  1023. uint32_t nb21, nb22, nb31;
  1024. uint32_t nb42, nb43, nb52, nb53;
  1025. uint32_t s_off;
  1026. uint32_t n_head, d_head, n_group, n_tok;
  1027. };
  1028. struct vk_op_ssm_conv_push_constants {
  1029. uint32_t nb01, nb02;
  1030. uint32_t nb11;
  1031. uint32_t dst_nb0, dst_nb1, dst_nb2;
  1032. uint32_t nc, ncs, nr, n_t, n_s;
  1033. };
  1034. struct vk_op_conv2d_push_constants {
  1035. uint32_t Cout;
  1036. uint32_t Cin;
  1037. uint32_t N;
  1038. uint32_t KW;
  1039. uint32_t KH;
  1040. uint32_t W;
  1041. uint32_t H;
  1042. uint32_t OW;
  1043. uint32_t OH;
  1044. uint32_t s0;
  1045. uint32_t s1;
  1046. uint32_t p0;
  1047. uint32_t p1;
  1048. uint32_t d0;
  1049. uint32_t d1;
  1050. uint32_t nb01;
  1051. uint32_t nb02;
  1052. uint32_t nb03;
  1053. uint32_t nb11;
  1054. uint32_t nb12;
  1055. uint32_t nb13;
  1056. uint32_t nb1;
  1057. uint32_t nb2;
  1058. uint32_t nb3;
  1059. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH
  1060. uint32_t KWmp; uint32_t KWL;
  1061. uint32_t KWKHmp; uint32_t KWKHL;
  1062. uint32_t OWmp; uint32_t OWL;
  1063. uint32_t OWOHmp; uint32_t OWOHL;
  1064. };
  1065. template <> void init_pushconst_fastdiv(vk_op_conv2d_push_constants &p) {
  1066. // Compute magic values to divide by KW, KW*KH, OW, OW*OH
  1067. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  1068. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  1069. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  1070. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  1071. }
  1072. struct vk_op_conv_transpose_2d_push_constants {
  1073. uint32_t Cout;
  1074. uint32_t Cin;
  1075. uint32_t N;
  1076. uint32_t KW;
  1077. uint32_t KH;
  1078. uint32_t W;
  1079. uint32_t H;
  1080. uint32_t OW;
  1081. uint32_t OH;
  1082. uint32_t s0;
  1083. uint32_t s1;
  1084. uint32_t p0;
  1085. uint32_t p1;
  1086. uint32_t d0;
  1087. uint32_t d1;
  1088. uint32_t nb01;
  1089. uint32_t nb02;
  1090. uint32_t nb03;
  1091. uint32_t nb11;
  1092. uint32_t nb12;
  1093. uint32_t nb13;
  1094. uint32_t nb1;
  1095. uint32_t nb2;
  1096. uint32_t nb3;
  1097. // init_fastdiv_values constants for dividing by KW, KW*KH, OW, OW*OH, s0, s1
  1098. uint32_t KWmp; uint32_t KWL;
  1099. uint32_t KWKHmp; uint32_t KWKHL;
  1100. uint32_t OWmp; uint32_t OWL;
  1101. uint32_t OWOHmp; uint32_t OWOHL;
  1102. uint32_t s0mp; uint32_t s0L;
  1103. uint32_t s1mp; uint32_t s1L;
  1104. };
  1105. template <> void init_pushconst_fastdiv(vk_op_conv_transpose_2d_push_constants &p) {
  1106. // Compute magic values to divide by KW, KW*KH, OW, OW*OH, s0, s1
  1107. init_fastdiv_values(p.KW, p.KWmp, p.KWL);
  1108. init_fastdiv_values(p.KW*p.KH, p.KWKHmp, p.KWKHL);
  1109. init_fastdiv_values(p.OW, p.OWmp, p.OWL);
  1110. init_fastdiv_values(p.OW*p.OH, p.OWOHmp, p.OWOHL);
  1111. init_fastdiv_values(p.s0, p.s0mp, p.s0L);
  1112. init_fastdiv_values(p.s1, p.s1mp, p.s1L);
  1113. }
  1114. struct vk_op_conv2d_dw_push_constants {
  1115. uint32_t ne;
  1116. uint32_t batches;
  1117. uint32_t channels;
  1118. uint32_t dst_w;
  1119. uint32_t dst_h;
  1120. uint32_t src_w;
  1121. uint32_t src_h;
  1122. uint32_t knl_w;
  1123. uint32_t knl_h;
  1124. int32_t stride_x;
  1125. int32_t stride_y;
  1126. int32_t pad_x;
  1127. int32_t pad_y;
  1128. int32_t dilation_x;
  1129. int32_t dilation_y;
  1130. };
  1131. struct vk_op_upscale_push_constants {
  1132. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  1133. uint32_t ne00; uint32_t ne01;
  1134. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  1135. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  1136. float sf0; float sf1; float sf2; float sf3;
  1137. float pixel_offset;
  1138. };
  1139. struct vk_op_sum_rows_push_constants
  1140. {
  1141. uint32_t n_cols;
  1142. uint32_t ne01, ne02;
  1143. uint32_t nb01, nb02, nb03;
  1144. uint32_t nb11, nb12, nb13;
  1145. float weight;
  1146. uint32_t misalign_offsets;
  1147. uint32_t ne0_12mp, ne0_12L;
  1148. uint32_t ne0_1mp, ne0_1L;
  1149. };
  1150. static vk_op_sum_rows_push_constants vk_op_sum_rows_push_constants_init(const ggml_tensor * src, const ggml_tensor * dst, int64_t n_cols) {
  1151. uint32_t type_size = (uint32_t)ggml_type_size(src->type);
  1152. vk_op_sum_rows_push_constants p = {};
  1153. p.n_cols = (uint32_t)n_cols;
  1154. p.ne01 = (uint32_t)src->ne[1];
  1155. p.ne02 = (uint32_t)src->ne[2];
  1156. p.nb01 = (uint32_t)src->nb[1] / type_size;
  1157. p.nb02 = (uint32_t)src->nb[2] / type_size;
  1158. p.nb03 = (uint32_t)src->nb[3] / type_size;
  1159. p.nb11 = (uint32_t)dst->nb[1] / type_size;
  1160. p.nb12 = (uint32_t)dst->nb[2] / type_size;
  1161. p.nb13 = (uint32_t)dst->nb[3] / type_size;
  1162. p.weight = 1.0f;
  1163. return p;
  1164. }
  1165. template <> void init_pushconst_fastdiv(vk_op_sum_rows_push_constants &p) {
  1166. init_fastdiv_values(p.ne01*p.ne02, p.ne0_12mp, p.ne0_12L);
  1167. init_fastdiv_values(p.ne01, p.ne0_1mp, p.ne0_1L);
  1168. }
  1169. // Allow pre-recording command buffers
  1170. struct vk_staging_memcpy {
  1171. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  1172. void * dst;
  1173. const void * src;
  1174. size_t n;
  1175. };
  1176. struct vk_staging_memset {
  1177. vk_staging_memset(void * _dst, uint32_t _val, size_t _n) : dst(_dst), val(_val), n(_n) {}
  1178. void * dst;
  1179. uint32_t val;
  1180. size_t n;
  1181. };
  1182. struct vk_context_struct {
  1183. vk_submission * s;
  1184. std::vector<vk_sequence> seqs;
  1185. int exit_tensor_idx;
  1186. std::vector<vk_staging_memcpy> in_memcpys;
  1187. std::vector<vk_staging_memcpy> out_memcpys;
  1188. std::vector<vk_staging_memset> memsets;
  1189. vk_command_pool * p {};
  1190. };
  1191. typedef std::shared_ptr<vk_context_struct> vk_context;
  1192. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  1193. struct ggml_vk_garbage_collector {
  1194. std::vector<vk_semaphore> tl_semaphores;
  1195. std::vector<vk_semaphore> semaphores;
  1196. std::vector<vk::Event> events;
  1197. std::vector<vk_context> contexts;
  1198. };
  1199. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx, vk_context subctx);
  1200. static void ggml_vk_load_shaders(vk_device& device);
  1201. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx);
  1202. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  1203. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  1204. static std::string format_size(size_t size) {
  1205. const size_t kib = 1024;
  1206. const size_t mib = kib * 1024;
  1207. const size_t gib = mib * 1024;
  1208. std::ostringstream oss;
  1209. oss << std::fixed << std::setprecision(2);
  1210. if (size >= gib) {
  1211. oss << static_cast<double>(size) / gib << " GiB";
  1212. } else if (size >= mib) {
  1213. oss << static_cast<double>(size) / mib << " MiB";
  1214. } else if (size >= kib) {
  1215. oss << static_cast<double>(size) / kib << " KiB";
  1216. } else {
  1217. oss << size << " B";
  1218. }
  1219. return oss.str();
  1220. }
  1221. class vk_memory_logger {
  1222. public:
  1223. vk_memory_logger(): total_device(0), total_host(0) {}
  1224. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  1225. void log_deallocation(vk_buffer_ref buf_ref);
  1226. private:
  1227. std::map<vk::Buffer, size_t> allocations; // Track allocations
  1228. size_t total_device;
  1229. size_t total_host;
  1230. };
  1231. #else
  1232. #define VK_LOG_MEMORY(msg) ((void) 0)
  1233. #endif // GGML_VULKAN_MEMORY_DEBUG
  1234. class vk_perf_logger {
  1235. public:
  1236. void print_timings() {
  1237. if (timings.empty()) {
  1238. return;
  1239. }
  1240. uint64_t total_all_op_times = 0;
  1241. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  1242. for (const auto & t : timings) {
  1243. uint64_t total_op_times = 0;
  1244. for (const auto & time : t.second) {
  1245. total_op_times += time;
  1246. }
  1247. std::cerr << t.first << ": " << t.second.size() << " x " << (total_op_times / t.second.size() / 1000.0)
  1248. << " us";
  1249. // If we have as many flops entries as timing entries for the op, then compute and log the flops/S.
  1250. auto it = flops.find(t.first);
  1251. if (it != flops.end() && (it->second).size() == t.second.size()) {
  1252. uint64_t total_op_flops = 0;
  1253. for (const auto & elem : it->second) {
  1254. total_op_flops += elem;
  1255. }
  1256. std::cerr << " ("
  1257. << (double(total_op_flops) / (1000.0 * 1000.0 * 1000.0)) /
  1258. (double(total_op_times) / (1000.0 * 1000.0 * 1000.0))
  1259. << " GFLOPS/s)";
  1260. }
  1261. total_all_op_times += total_op_times;
  1262. std::cerr << std::endl;
  1263. }
  1264. if (timings.size() > 0) {
  1265. std::cerr << "Total time: " << total_all_op_times / 1000.0 << " us." << std::endl;
  1266. }
  1267. timings.clear();
  1268. flops.clear();
  1269. }
  1270. void log_timing(const ggml_tensor * node, uint64_t time) {
  1271. if (node->op == GGML_OP_UNARY) {
  1272. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  1273. return;
  1274. }
  1275. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  1276. const uint64_t m = node->src[0]->ne[1];
  1277. const uint64_t n = node->ne[1];
  1278. const uint64_t k = node->src[1]->ne[0];
  1279. const uint64_t batch = node->src[1]->ne[2] * node->src[1]->ne[3];
  1280. std::string name = ggml_op_name(node->op);
  1281. if ((node->op == GGML_OP_MUL_MAT && n <= mul_mat_vec_max_cols) ||
  1282. (node->op == GGML_OP_MUL_MAT_ID && node->src[2]->ne[1] == 1)) {
  1283. name += "_VEC";
  1284. }
  1285. name += " ";
  1286. name += ggml_type_name(node->src[0]->type);
  1287. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  1288. if (batch > 1) {
  1289. name += " batch=" + std::to_string(batch);
  1290. }
  1291. timings[name].push_back(time);
  1292. flops[name].push_back(m * n * (k + (k - 1)) * batch);
  1293. return;
  1294. }
  1295. if (node->op == GGML_OP_CONV_2D || node->op == GGML_OP_CONV_TRANSPOSE_2D) {
  1296. std::string name = ggml_op_name(node->op);
  1297. ggml_tensor * knl = node->src[0];
  1298. uint64_t OW = node->ne[0];
  1299. uint64_t OH = node->ne[1];
  1300. uint64_t N = node->ne[3];
  1301. uint64_t Cout = node->ne[2];
  1302. uint64_t KW = knl->ne[0];
  1303. uint64_t KH = knl->ne[1];
  1304. uint64_t Cin = node->src[1]->ne[2];
  1305. // KxCRS @ CRSxNPQ = KxNPQ -> M=K, K=CRS, N=NPQ
  1306. uint64_t size_M = Cout;
  1307. uint64_t size_K = Cin * KW * KH;
  1308. uint64_t size_N = N * OW * OH;
  1309. uint64_t n_flops = size_M * size_N * (size_K + (size_K - 1));
  1310. name += " M=Cout=" + std::to_string(size_M) + ", K=Cin*KW*KH=" + std::to_string(size_K) +
  1311. ", N=N*OW*OH=" + std::to_string(size_N);
  1312. flops[name].push_back(n_flops);
  1313. timings[name].push_back(time);
  1314. return;
  1315. }
  1316. if (node->op == GGML_OP_RMS_NORM) {
  1317. std::string name = ggml_op_name(node->op);
  1318. name += "(" + std::to_string(node->ne[0]) + "," + std::to_string(node->ne[1]) + "," + std::to_string(node->ne[2]) + "," + std::to_string(node->ne[3]) + ")";
  1319. timings[name].push_back(time);
  1320. return;
  1321. }
  1322. timings[ggml_op_name(node->op)].push_back(time);
  1323. }
  1324. private:
  1325. std::map<std::string, std::vector<uint64_t>> timings;
  1326. std::map<std::string, std::vector<uint64_t>> flops;
  1327. };
  1328. struct ggml_backend_vk_context {
  1329. std::string name;
  1330. vk_device device;
  1331. size_t semaphore_idx, event_idx;
  1332. ggml_vk_garbage_collector gc;
  1333. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k, prealloc_size_add_rms_partials, prealloc_size_add_rms_partials_offset;
  1334. vk_buffer prealloc_x, prealloc_y, prealloc_split_k, prealloc_add_rms_partials;
  1335. vk::Fence fence, almost_ready_fence;
  1336. bool almost_ready_fence_pending {};
  1337. // Set before op_add and unset after op_rms_norm to indicate that the add should
  1338. // write partial sums to accumulate the square of the vector components
  1339. bool do_add_rms_partials_offset_calculation;
  1340. bool do_add_rms_partials;
  1341. uint64_t last_total_mul_mat_bytes {};
  1342. // Cache most recent tensor that was converted into prealloc_y, and what pipeline it used to convert.
  1343. vk_pipeline_struct * prealloc_y_last_pipeline_used {};
  1344. const ggml_tensor * prealloc_y_last_tensor_used {};
  1345. // Track which nodes have been used since the last sync, and whether they were written to
  1346. std::vector<const ggml_tensor *> unsynced_nodes_written;
  1347. std::vector<const ggml_tensor *> unsynced_nodes_read;
  1348. // Track which prealloc buffers have pending reads that need to be synchronized.
  1349. // These are checked before writing to the buffer (and call ggml_vk_sync_buffers if set),
  1350. // and set to true after the buffer contents are consumed.
  1351. bool prealloc_x_need_sync, prealloc_y_need_sync, prealloc_split_k_need_sync;
  1352. vk_context_ref compute_ctx;
  1353. vk_context_ref transfer_ctx;
  1354. std::vector<vk_context_ref> tensor_ctxs;
  1355. std::vector<vk::DescriptorPool> descriptor_pools;
  1356. std::vector<vk::DescriptorSet> descriptor_sets;
  1357. uint32_t descriptor_set_idx {};
  1358. uint32_t pipeline_descriptor_set_requirements {};
  1359. vk_command_pool compute_cmd_pool;
  1360. vk_command_pool transfer_cmd_pool;
  1361. // number of additional consecutive nodes that are being fused with the
  1362. // node currently being processed
  1363. int num_additional_fused_ops {};
  1364. // Bitmask of which fused ops need to write an intermediate value to memory.
  1365. // Bit 'i' means nodes[start_of_fusion + i] writes to memory.
  1366. // If there's no fusion, bit 0 is still set.
  1367. int fused_ops_write_mask {};
  1368. };
  1369. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  1370. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  1371. if (tensor->view_src) {
  1372. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  1373. }
  1374. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  1375. }
  1376. struct ggml_backend_vk_buffer_context {
  1377. vk_device_ref device;
  1378. vk_buffer dev_buffer;
  1379. std::string name;
  1380. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  1381. device(device),
  1382. dev_buffer(dev_buffer),
  1383. name(name) {
  1384. }
  1385. ~ggml_backend_vk_buffer_context() {
  1386. ggml_vk_destroy_buffer(dev_buffer);
  1387. }
  1388. };
  1389. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1390. static std::mutex log_mutex;
  1391. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  1392. std::lock_guard<std::mutex> guard(log_mutex);
  1393. vk_buffer buf = buf_ref.lock();
  1394. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1395. const std::string type = device ? "device" : "host";
  1396. allocations[buf->buffer] = size;
  1397. total_device += device ? size : 0;
  1398. total_host += device ? 0 : size;
  1399. 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));
  1400. }
  1401. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  1402. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  1403. return;
  1404. }
  1405. std::lock_guard<std::mutex> guard(log_mutex);
  1406. vk_buffer buf = buf_ref.lock();
  1407. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  1408. std::string type = device ? "device" : "host";
  1409. auto it = allocations.find(buf->buffer);
  1410. total_device -= device ? it->second : 0;
  1411. total_host -= device ? 0 : it->second;
  1412. if (it != allocations.end()) {
  1413. 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));
  1414. allocations.erase(it);
  1415. } else {
  1416. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  1417. }
  1418. }
  1419. #endif // GGML_VULKAN_MEMORY_DEBUG
  1420. struct vk_instance_t {
  1421. vk::Instance instance;
  1422. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  1423. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  1424. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  1425. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  1426. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  1427. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  1428. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  1429. std::vector<size_t> device_indices;
  1430. std::vector<bool> device_supports_membudget;
  1431. vk_device devices[GGML_VK_MAX_DEVICES];
  1432. };
  1433. static bool vk_instance_initialized = false;
  1434. static vk_instance_t vk_instance;
  1435. static bool vk_perf_logger_enabled = false;
  1436. #ifdef GGML_VULKAN_CHECK_RESULTS
  1437. static size_t vk_skip_checks;
  1438. static size_t vk_output_tensor;
  1439. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  1440. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1441. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  1442. #endif
  1443. 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);
  1444. static void ggml_backend_vk_free(ggml_backend_t backend);
  1445. static VkDeviceSize ggml_vk_get_max_buffer_range(const ggml_backend_vk_context * ctx, const vk_buffer &buf, const VkDeviceSize offset) {
  1446. const VkDeviceSize range = std::min(VkDeviceSize{buf->size - offset},
  1447. VkDeviceSize{ctx->device->properties.limits.maxStorageBufferRange});
  1448. return range;
  1449. }
  1450. // Wait for ctx->fence to be signaled.
  1451. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  1452. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  1453. // during this wait.
  1454. if (ctx->almost_ready_fence_pending) {
  1455. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  1456. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  1457. ctx->almost_ready_fence_pending = false;
  1458. }
  1459. // Spin (w/pause) waiting for the graph to finish executing.
  1460. vk::Result result;
  1461. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  1462. if (result != vk::Result::eNotReady) {
  1463. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  1464. exit(1);
  1465. }
  1466. for (uint32_t i = 0; i < 100; ++i) {
  1467. YIELD();
  1468. YIELD();
  1469. YIELD();
  1470. YIELD();
  1471. YIELD();
  1472. YIELD();
  1473. YIELD();
  1474. YIELD();
  1475. YIELD();
  1476. YIELD();
  1477. }
  1478. }
  1479. ctx->device->device.resetFences({ ctx->fence });
  1480. }
  1481. // variables to track number of compiles in progress
  1482. static uint32_t compile_count = 0;
  1483. static std::mutex compile_count_mutex;
  1484. static std::condition_variable compile_count_cond;
  1485. 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,
  1486. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  1487. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  1488. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  1489. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  1490. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  1491. GGML_ASSERT(parameter_count > 0);
  1492. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  1493. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  1494. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  1495. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  1496. vk::PushConstantRange pcr(
  1497. vk::ShaderStageFlagBits::eCompute,
  1498. 0,
  1499. pipeline->push_constant_size
  1500. );
  1501. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  1502. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  1503. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1504. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1505. specialization_entries[i].constantID = i;
  1506. specialization_entries[i].offset = i * sizeof(uint32_t);
  1507. specialization_entries[i].size = sizeof(uint32_t);
  1508. }
  1509. vk::SpecializationInfo specialization_info(
  1510. specialization_entries.size(),
  1511. specialization_entries.data(),
  1512. specialization_constants.size() * sizeof(uint32_t),
  1513. specialization_constants.data()
  1514. );
  1515. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1516. if (device->subgroup_require_full_support && require_full_subgroups) {
  1517. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1518. }
  1519. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1520. pipeline_shader_stage_create_flags,
  1521. vk::ShaderStageFlagBits::eCompute,
  1522. pipeline->shader_module,
  1523. entrypoint.c_str(),
  1524. &specialization_info);
  1525. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1526. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1527. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1528. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1529. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1530. }
  1531. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1532. device->pipeline_executable_properties_support ?
  1533. vk::PipelineCreateFlagBits::eCaptureStatisticsKHR :
  1534. vk::PipelineCreateFlags{},
  1535. pipeline_shader_create_info,
  1536. pipeline->layout);
  1537. vk::PipelineRobustnessCreateInfoEXT rci;
  1538. if (device->pipeline_robustness && disable_robustness) {
  1539. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1540. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1541. compute_pipeline_create_info.setPNext(&rci);
  1542. }
  1543. try {
  1544. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1545. } catch (const vk::SystemError& e) {
  1546. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1547. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1548. throw e;
  1549. }
  1550. pipeline->compiled = true;
  1551. if (vk_instance.debug_utils_support) {
  1552. vk::DebugUtilsObjectNameInfoEXT duoni;
  1553. duoni.objectType = vk::ObjectType::ePipeline;
  1554. duoni.pObjectName = pipeline->name.c_str();
  1555. duoni.objectHandle = /*reinterpret_cast*/(uint64_t)(static_cast<VkPipeline>(pipeline->pipeline));
  1556. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1557. }
  1558. if (device->pipeline_executable_properties_support) {
  1559. vk::PipelineExecutableInfoKHR executableInfo;
  1560. executableInfo.pipeline = pipeline->pipeline;
  1561. auto statistics = device->device.getPipelineExecutableStatisticsKHR(executableInfo);
  1562. for (auto & s : statistics) {
  1563. // "Register Count" is reported by NVIDIA drivers.
  1564. if (strcmp(s.name, "Register Count") == 0) {
  1565. VK_LOG_DEBUG(pipeline->name << " " << s.name << ": " << s.value.u64 << " registers");
  1566. pipeline->register_count = (uint32_t)s.value.u64;
  1567. }
  1568. }
  1569. }
  1570. {
  1571. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1572. device->all_pipelines.push_back(pipeline);
  1573. }
  1574. {
  1575. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1576. assert(compile_count > 0);
  1577. compile_count--;
  1578. }
  1579. compile_count_cond.notify_all();
  1580. }
  1581. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1582. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1583. device.destroyPipelineLayout(pipeline->layout);
  1584. device.destroyShaderModule(pipeline->shader_module);
  1585. device.destroyPipeline(pipeline->pipeline);
  1586. }
  1587. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1588. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1589. ctx->pipeline_descriptor_set_requirements += n;
  1590. if (!pipeline->compiled) {
  1591. pipeline->needed = true;
  1592. ggml_vk_load_shaders(ctx->device);
  1593. }
  1594. ggml_pipeline_allocate_descriptor_sets(ctx);
  1595. }
  1596. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1597. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1598. // Enough descriptors are available
  1599. return;
  1600. }
  1601. vk_device& device = ctx->device;
  1602. // Grow by 50% to avoid frequent allocations
  1603. uint32_t needed = std::max(3 * ctx->descriptor_sets.size() / 2, size_t{ctx->pipeline_descriptor_set_requirements});
  1604. uint32_t to_alloc = needed - ctx->descriptor_sets.size();
  1605. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1606. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1607. while (to_alloc > 0) {
  1608. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1609. to_alloc -= alloc_count;
  1610. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1611. if (pool_idx >= ctx->descriptor_pools.size()) {
  1612. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1613. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1614. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1615. }
  1616. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1617. for (uint32_t i = 0; i < alloc_count; i++) {
  1618. layouts[i] = device->dsl;
  1619. }
  1620. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1621. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1622. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1623. pool_idx++;
  1624. }
  1625. }
  1626. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1627. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1628. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1629. // Reuse command buffer
  1630. return p.cmd_buffers[p.cmd_buffer_idx++];
  1631. }
  1632. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1633. p.pool,
  1634. vk::CommandBufferLevel::ePrimary,
  1635. 1);
  1636. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1637. auto buf = cmd_buffers.front();
  1638. p.cmd_buffers.push_back(buf);
  1639. p.cmd_buffer_idx++;
  1640. return buf;
  1641. }
  1642. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1643. if (ctx->seqs.empty()) {
  1644. if (fence) {
  1645. std::lock_guard<std::mutex> guard(queue_mutex);
  1646. ctx->p->q->queue.submit({}, fence);
  1647. }
  1648. return;
  1649. }
  1650. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1651. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1652. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1653. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1654. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1655. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1656. std::vector<vk::SubmitInfo> submit_infos;
  1657. int idx = -1;
  1658. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1659. size_t reserve = 0;
  1660. for (const auto& sequence : ctx->seqs) {
  1661. reserve += sequence.size();
  1662. }
  1663. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1664. tl_wait_semaphores.reserve(reserve);
  1665. tl_wait_vals.reserve(reserve);
  1666. tl_signal_semaphores.reserve(reserve);
  1667. tl_signal_vals.reserve(reserve);
  1668. tl_submit_infos.reserve(reserve);
  1669. submit_infos.reserve(reserve);
  1670. stage_flags.reserve(reserve);
  1671. for (const auto& sequence : ctx->seqs) {
  1672. for (const auto& submission : sequence) {
  1673. stage_flags.push_back({});
  1674. idx++;
  1675. tl_wait_vals.push_back({});
  1676. tl_wait_semaphores.push_back({});
  1677. tl_signal_vals.push_back({});
  1678. tl_signal_semaphores.push_back({});
  1679. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1680. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1681. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1682. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1683. }
  1684. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1685. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1686. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1687. }
  1688. tl_submit_infos.push_back({
  1689. (uint32_t) submission.wait_semaphores.size(),
  1690. tl_wait_vals[idx].data(),
  1691. (uint32_t) submission.signal_semaphores.size(),
  1692. tl_signal_vals[idx].data(),
  1693. });
  1694. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1695. tl_submit_infos[idx].pNext = nullptr;
  1696. vk::SubmitInfo si{
  1697. (uint32_t) submission.wait_semaphores.size(),
  1698. tl_wait_semaphores[idx].data(),
  1699. stage_flags[idx].data(),
  1700. 1,
  1701. &submission.buffer,
  1702. (uint32_t) submission.signal_semaphores.size(),
  1703. tl_signal_semaphores[idx].data(),
  1704. };
  1705. si.setPNext(&tl_submit_infos[idx]);
  1706. submit_infos.push_back(si);
  1707. }
  1708. }
  1709. std::lock_guard<std::mutex> guard(queue_mutex);
  1710. ctx->p->q->queue.submit(submit_infos, fence);
  1711. ctx->seqs.clear();
  1712. }
  1713. 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) {
  1714. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1715. const uint32_t qfsize = queue_family_props.size();
  1716. // Try with avoid preferences first
  1717. for (uint32_t i = 0; i < qfsize; i++) {
  1718. 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)) {
  1719. return i;
  1720. }
  1721. }
  1722. // Fall back to only required
  1723. for (size_t i = 0; i < qfsize; i++) {
  1724. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1725. return i;
  1726. }
  1727. }
  1728. // Fall back to reusing compute queue
  1729. for (size_t i = 0; i < qfsize; i++) {
  1730. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1731. return i;
  1732. }
  1733. }
  1734. // Fall back to ignoring min_num_queries
  1735. for (size_t i = 0; i < qfsize; i++) {
  1736. if (queue_family_props[i].queueFlags & required) {
  1737. return i;
  1738. }
  1739. }
  1740. // 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.
  1741. // 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.
  1742. if (compute_index >= 0) {
  1743. return compute_index;
  1744. }
  1745. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1746. for(auto &q_family : queue_family_props) {
  1747. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1748. }
  1749. abort();
  1750. }
  1751. 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) {
  1752. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1753. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1754. q.queue_family_index = queue_family_index;
  1755. q.transfer_only = transfer_only;
  1756. q.cmd_pool.init(device, &q);
  1757. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1758. q.stage_flags = stage_flags;
  1759. }
  1760. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1761. vk_context result = std::make_shared<vk_context_struct>();
  1762. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1763. ctx->gc.contexts.emplace_back(result);
  1764. result->p = &p;
  1765. return result;
  1766. }
  1767. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1768. vk_context result = std::make_shared<vk_context_struct>();
  1769. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1770. result->p = &p;
  1771. return result;
  1772. }
  1773. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1774. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1775. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1776. vk::SemaphoreCreateInfo ci{};
  1777. ci.setPNext(&tci);
  1778. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1779. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1780. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1781. }
  1782. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1783. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1784. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1785. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1786. vk::SemaphoreCreateInfo ci{};
  1787. ci.setPNext(&tci);
  1788. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1789. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1790. }
  1791. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1792. }
  1793. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1794. if (ctx->event_idx >= ctx->gc.events.size()) {
  1795. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1796. }
  1797. return ctx->gc.events[ctx->event_idx++];
  1798. }
  1799. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1800. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1801. // Requires command buffers to be done
  1802. device->device.resetCommandPool(p.pool);
  1803. p.cmd_buffer_idx = 0;
  1804. }
  1805. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1806. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1807. // Arbitrary frequency to cleanup/reuse command buffers
  1808. static constexpr uint32_t cleanup_frequency = 10;
  1809. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1810. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1811. }
  1812. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1813. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1814. }
  1815. }
  1816. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1817. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1818. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1819. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1820. (flags & memory_type.propertyFlags) == flags &&
  1821. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1822. return static_cast<int32_t>(i);
  1823. }
  1824. }
  1825. return UINT32_MAX;
  1826. }
  1827. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, const std::initializer_list<vk::MemoryPropertyFlags> & req_flags_list) {
  1828. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags_list.begin()[0]) << ", " << to_string(req_flags_list.begin()[req_flags_list.size()-1]) << ")");
  1829. if (size > device->max_buffer_size) {
  1830. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device buffer size limit");
  1831. }
  1832. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1833. if (size == 0) {
  1834. buf->size = 0;
  1835. return buf;
  1836. }
  1837. vk::BufferUsageFlags usage_flags = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst;
  1838. vk::MemoryAllocateFlags mem_flags {};
  1839. if (device->buffer_device_address) {
  1840. usage_flags |= vk::BufferUsageFlagBits::eShaderDeviceAddress;
  1841. mem_flags |= vk::MemoryAllocateFlagBits::eDeviceAddress;
  1842. }
  1843. vk::BufferCreateInfo buffer_create_info{
  1844. vk::BufferCreateFlags(),
  1845. size,
  1846. usage_flags,
  1847. vk::SharingMode::eExclusive,
  1848. 0,
  1849. nullptr,
  1850. };
  1851. buf->buffer = device->device.createBuffer(buffer_create_info);
  1852. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1853. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1854. const vk::MemoryAllocateFlagsInfo mem_flags_info { mem_flags };
  1855. for (auto it = req_flags_list.begin(); it != req_flags_list.end(); it++) {
  1856. const auto & req_flags = *it;
  1857. uint32_t memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1858. if (memory_type_index == UINT32_MAX) {
  1859. continue;
  1860. }
  1861. buf->memory_property_flags = req_flags;
  1862. try {
  1863. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index, &mem_flags_info });
  1864. break;
  1865. } catch (const vk::SystemError& e) {
  1866. // loop and retry
  1867. // during last attempt throw the exception
  1868. if (it + 1 == req_flags_list.end()) {
  1869. device->device.destroyBuffer(buf->buffer);
  1870. throw e;
  1871. }
  1872. }
  1873. }
  1874. if (!buf->device_memory) {
  1875. device->device.destroyBuffer(buf->buffer);
  1876. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1877. }
  1878. buf->ptr = nullptr;
  1879. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1880. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1881. }
  1882. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1883. buf->device = device;
  1884. buf->size = size;
  1885. if (device->buffer_device_address) {
  1886. const vk::BufferDeviceAddressInfo addressInfo(buf->buffer);
  1887. buf->bda_addr = device->device.getBufferAddress(addressInfo);
  1888. }
  1889. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1890. device->memory_logger->log_allocation(buf, size);
  1891. #endif
  1892. return buf;
  1893. }
  1894. 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)) {
  1895. try {
  1896. return ggml_vk_create_buffer(device, size, {req_flags, fallback_flags});
  1897. } catch (const vk::SystemError& e) {
  1898. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1899. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1900. throw e;
  1901. }
  1902. }
  1903. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1904. vk_buffer buf;
  1905. try {
  1906. if (device->prefer_host_memory) {
  1907. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1908. vk::MemoryPropertyFlagBits::eDeviceLocal});
  1909. } else if (device->uma) {
  1910. // Fall back to host memory type
  1911. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal,
  1912. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1913. } else if (device->disable_host_visible_vidmem) {
  1914. if (device->allow_sysmem_fallback) {
  1915. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal,
  1916. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1917. } else {
  1918. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  1919. }
  1920. } else {
  1921. // use rebar if available, otherwise fallback to device only visible memory
  1922. if (device->allow_sysmem_fallback) {
  1923. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1924. vk::MemoryPropertyFlagBits::eDeviceLocal,
  1925. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  1926. } else {
  1927. buf = ggml_vk_create_buffer(device, size, {vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent,
  1928. vk::MemoryPropertyFlagBits::eDeviceLocal});
  1929. }
  1930. }
  1931. } catch (const vk::SystemError& e) {
  1932. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1933. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1934. throw e;
  1935. }
  1936. return buf;
  1937. }
  1938. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1939. if (buf == nullptr) {
  1940. return;
  1941. }
  1942. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1943. if (buf->device != nullptr) {
  1944. buf->device->memory_logger->log_deallocation(buf);
  1945. }
  1946. #endif
  1947. buf.reset();
  1948. }
  1949. static vk_subbuffer ggml_vk_subbuffer(const ggml_backend_vk_context* ctx, const vk_buffer& buf, size_t offset = 0) {
  1950. return { buf, offset, ggml_vk_get_max_buffer_range(ctx, buf, offset) };
  1951. }
  1952. static void ggml_vk_sync_buffers(ggml_backend_vk_context* ctx, vk_context& subctx) {
  1953. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1954. const bool transfer_queue = subctx->p->q->transfer_only;
  1955. if (ctx) {
  1956. ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false;
  1957. }
  1958. subctx->s->buffer.pipelineBarrier(
  1959. subctx->p->q->stage_flags,
  1960. subctx->p->q->stage_flags,
  1961. {},
  1962. { {
  1963. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1964. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1965. } },
  1966. {},
  1967. {}
  1968. );
  1969. }
  1970. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1971. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1972. if (events.empty()) {
  1973. return;
  1974. }
  1975. ctx->s->buffer.waitEvents(
  1976. events,
  1977. ctx->p->q->stage_flags,
  1978. ctx->p->q->stage_flags,
  1979. {},
  1980. {},
  1981. {}
  1982. );
  1983. }
  1984. // number of rows/cols for flash attention shader
  1985. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1986. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1987. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1988. if (hsv >= 192) {
  1989. return 2;
  1990. } else {
  1991. return 8;
  1992. }
  1993. }
  1994. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1995. // 128 threads split into four subgroups, each subgroup does 1/4
  1996. // of the Bc dimension.
  1997. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1998. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1999. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  2000. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  2001. if (path == FA_COOPMAT2) {
  2002. return flash_attention_num_small_rows;
  2003. } else {
  2004. return scalar_flash_attention_num_small_rows;
  2005. }
  2006. }
  2007. 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) {
  2008. GGML_UNUSED(clamp);
  2009. GGML_UNUSED(hsv);
  2010. if (path == FA_SCALAR) {
  2011. if (small_rows) {
  2012. return {scalar_flash_attention_num_small_rows, 64};
  2013. } else {
  2014. if ((hsv | hsk) & 8) {
  2015. // HSV/HSK not being a multiple of 16 makes D_split smaller, which makes cols_per_iter
  2016. // larger, and Bc needs to be >= cols_per_thread. 64 is large enough, 32 is not.
  2017. return {get_fa_scalar_num_large_rows(hsv), 64};
  2018. } else {
  2019. return {get_fa_scalar_num_large_rows(hsv), 32};
  2020. }
  2021. }
  2022. }
  2023. if (path == FA_COOPMAT1) {
  2024. if (small_rows) {
  2025. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  2026. } else {
  2027. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  2028. }
  2029. }
  2030. // small rows, large cols
  2031. if (small_rows) {
  2032. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  2033. }
  2034. // small cols to reduce register count
  2035. if (ggml_is_quantized(type) || hsk >= 256 || hsv >= 256) {
  2036. if (hsk >= 512 || hsv >= 512) {
  2037. return {32, 32};
  2038. } else {
  2039. return {64, 32};
  2040. }
  2041. }
  2042. return {64, 64};
  2043. }
  2044. static uint32_t fa_align(FaCodePath path, uint32_t hsk, uint32_t hsv, ggml_type type, bool small_rows) {
  2045. return fa_rows_cols(path, hsk, hsv, 0, type, small_rows)[1];
  2046. }
  2047. 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) {
  2048. uint32_t lut_size = 0;
  2049. switch (src0_type) {
  2050. case GGML_TYPE_IQ1_S:
  2051. case GGML_TYPE_IQ1_M:
  2052. lut_size = 2*2048;
  2053. break;
  2054. case GGML_TYPE_IQ2_XXS:
  2055. lut_size = 8*256;
  2056. break;
  2057. case GGML_TYPE_IQ2_XS:
  2058. lut_size = 8*512;
  2059. break;
  2060. case GGML_TYPE_IQ2_S:
  2061. lut_size = 8*1024;
  2062. break;
  2063. case GGML_TYPE_IQ3_XXS:
  2064. lut_size = 4*256;
  2065. break;
  2066. case GGML_TYPE_IQ3_S:
  2067. lut_size = 4*512;
  2068. break;
  2069. case GGML_TYPE_IQ4_NL:
  2070. case GGML_TYPE_IQ4_XS:
  2071. case GGML_TYPE_MXFP4:
  2072. lut_size = 4*16;
  2073. break;
  2074. default:
  2075. break;
  2076. }
  2077. // Needs to be kept up to date on shader changes
  2078. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  2079. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  2080. const uint32_t warps = warptile[0] / warptile[10];
  2081. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  2082. const uint32_t mmid_row_ids = mul_mat_id ? (warptile[2] * 2 * sizeof(uint16_t)) : 0;
  2083. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  2084. const uint32_t ballots_sh = mul_mat_id ? (warps * 4 * sizeof(uint32_t)) : 0;
  2085. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size + ballots_sh;
  2086. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  2087. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  2088. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  2089. return supported;
  2090. }
  2091. struct GpuPipelineConfig {
  2092. // GPU architecture identifier.
  2093. // Example: vk_device_architecture::AMD_GCN
  2094. vk_device_architecture arch;
  2095. // Mapping of pipeline names to their specific subgroup sizes.
  2096. // Example: {"soft_max_f32", 64}
  2097. std::unordered_map<std::string, uint32_t> pipelines;
  2098. // Default subgroup size for this GPU.
  2099. // Defaults to 0 if not explicitly provided.
  2100. uint32_t default_subgroup_size = 0;
  2101. };
  2102. // Pipeline configuration for RDNA1 GPUs.
  2103. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  2104. {"soft_max", 64}, {"im2col", 64},
  2105. {"argmax", 64}, {"mul_mat_vec", 64},
  2106. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  2107. };
  2108. // Pipeline configuration for RDNA2 GPUs.
  2109. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  2110. {"soft_max", 64}, {"im2col", 64},
  2111. };
  2112. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  2113. // Define configurations for different GPUs.
  2114. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  2115. {
  2116. vk_device_architecture::AMD_RDNA1,
  2117. {
  2118. rdna1_pipelines,
  2119. },
  2120. RDNA_DEFAULT_SUBGROUP_SIZE
  2121. },
  2122. {
  2123. vk_device_architecture::AMD_RDNA2,
  2124. {
  2125. rdna2_pipelines,
  2126. },
  2127. RDNA_DEFAULT_SUBGROUP_SIZE
  2128. },
  2129. };
  2130. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  2131. for (const auto &config : gpu_pipeline_configs) {
  2132. if (config.arch == arch) {
  2133. auto pipIt = config.pipelines.find(pipeline_name);
  2134. if (pipIt != config.pipelines.end()) {
  2135. return pipIt->second;
  2136. }
  2137. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  2138. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  2139. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  2140. for (const auto &entry : sorted_pipelines) {
  2141. if (pipeline_name.find(entry.first) != std::string::npos) {
  2142. return entry.second;
  2143. }
  2144. }
  2145. return config.default_subgroup_size;
  2146. }
  2147. }
  2148. return 0; // If no matching configuration is found
  2149. }
  2150. static void ggml_vk_load_shaders(vk_device& device) {
  2151. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  2152. // some shaders have a minimum subgroup size
  2153. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  2154. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  2155. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  2156. const uint32_t mul_mat_subgroup_size = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control) ? device->subgroup_min_size : device->subgroup_size;
  2157. const uint32_t mul_mat_subgroup_size_8 = std::max(mul_mat_subgroup_size, 8u);
  2158. const uint32_t mul_mat_subgroup_size_16 = std::max(mul_mat_subgroup_size, 16u);
  2159. const uint32_t mul_mat_subgroup_size_32 = std::max(mul_mat_subgroup_size, 32u);
  2160. const bool subgroup_min_size_16 = (!device->subgroup_size_control && device->subgroup_size >= 16) ||
  2161. (device->subgroup_size_control && device->subgroup_max_size >= 16);
  2162. // mulmat
  2163. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  2164. l_warptile_id, m_warptile_id, s_warptile_id,
  2165. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  2166. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  2167. l_warptile_mmq_int_k, m_warptile_mmq_int_k, s_warptile_mmq_int_k,
  2168. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  2169. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid,
  2170. l_warptile_mmqid_int, m_warptile_mmqid_int, s_warptile_mmqid_int,
  2171. l_warptile_mmqid_int_k, m_warptile_mmqid_int_k, s_warptile_mmqid_int_k;
  2172. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  2173. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  2174. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  2175. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  2176. uint32_t l_align, m_align, s_align;
  2177. if (device->coopmat2) {
  2178. // spec constants and tile sizes for non-quant matmul/matmul_id
  2179. l_warptile = { 256, 128, 256, 64, 1 };
  2180. m_warptile = { 256, 128, 128, 64, 0 };
  2181. s_warptile = { 128, 64, 64, 64, 0 };
  2182. l_wg_denoms = {128, 256, 1 };
  2183. m_wg_denoms = {128, 128, 1 };
  2184. s_wg_denoms = { 64, 64, 1 };
  2185. // spec constants and tile sizes for quant matmul (non-Qi_K)
  2186. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  2187. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  2188. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  2189. l_mmq_wg_denoms = { 128, 256, 1 };
  2190. m_mmq_wg_denoms = { 128, 128, 1 };
  2191. s_mmq_wg_denoms = { 32, 64, 1 };
  2192. // spec constants and tile sizes for quant matmul (Qi_K)
  2193. l_warptile_mmq_k = { 256, 128, 256, 64, 1 };
  2194. m_warptile_mmq_k = { 256, 128, 128, 64, 1 };
  2195. s_warptile_mmq_k = { 256, 32, 64, 128, 0 };
  2196. l_mmq_wg_denoms_k = { 128, 256, 1 };
  2197. m_mmq_wg_denoms_k = { 128, 128, 1 };
  2198. s_mmq_wg_denoms_k = { 32, 64, 1 };
  2199. // spec constants and tile sizes for quant matmul_id
  2200. l_warptile_mmqid = { 256, 128, 128, 16, 1, device->subgroup_size };
  2201. m_warptile_mmqid = { 256, 128, 64, 16, 0, device->subgroup_size };
  2202. s_warptile_mmqid = { 256, 128, 64, 16, 0, device->subgroup_size };
  2203. l_mmqid_wg_denoms = { 128, 128, 1 };
  2204. m_mmqid_wg_denoms = { 128, 64, 1 };
  2205. s_mmqid_wg_denoms = { 128, 64, 1 };
  2206. l_align = 128;
  2207. m_align = 64;
  2208. s_align = 32;
  2209. } else {
  2210. // Matrix cores require different warp group sizes
  2211. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  2212. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  2213. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  2214. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  2215. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  2216. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  2217. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  2218. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  2219. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  2220. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  2221. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  2222. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  2223. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  2224. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  2225. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  2226. // Integer MMQ has a smaller shared memory profile, but heavier register use
  2227. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2228. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  2229. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  2230. // K-quants use even more registers, mitigate by setting WMITER to 1
  2231. l_warptile_mmq_int_k = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 1, 4, 4, 1, subgroup_size_8 };
  2232. m_warptile_mmq_int_k = { 128, 64, 64, 32, subgroup_size_8, 32, 1, 2, 2, 1, subgroup_size_8 };
  2233. s_warptile_mmq_int_k = { subgroup_size_32, 32, 32, 32, 32, 32, 1, 2, 1, 1, subgroup_size_8 };
  2234. l_warptile_id = { 128, 128, 128, 16, mul_mat_subgroup_size_16 * 2, 64, 2, tm_l, tn_l, tk_l, mul_mat_subgroup_size_16 };
  2235. m_warptile_id = { 128, 64, 64, 16, mul_mat_subgroup_size_16, 32, 2, tm_m, tn_m, tk_m, mul_mat_subgroup_size_16 };
  2236. s_warptile_id = { mul_mat_subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, mul_mat_subgroup_size_16 };
  2237. l_warptile_mmqid = { 128, 128, 128, 32, mul_mat_subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, mul_mat_subgroup_size_8 };
  2238. m_warptile_mmqid = { 128, 64, 64, 32, mul_mat_subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, mul_mat_subgroup_size_8 };
  2239. s_warptile_mmqid = { mul_mat_subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, mul_mat_subgroup_size_8 };
  2240. l_warptile_mmqid_int = { 128, 128, 128, 32, mul_mat_subgroup_size_8 * 2, 64, 2, 4, 4, 1, mul_mat_subgroup_size_8 };
  2241. m_warptile_mmqid_int = { 128, 64, 64, 32, mul_mat_subgroup_size_8, 32, 2, 2, 2, 1, mul_mat_subgroup_size_8 };
  2242. s_warptile_mmqid_int = { mul_mat_subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, mul_mat_subgroup_size_8 };
  2243. l_warptile_mmqid_int_k = { 128, 128, 128, 32, mul_mat_subgroup_size_16 * 2, 64, 1, 4, 4, 1, mul_mat_subgroup_size_16 };
  2244. m_warptile_mmqid_int_k = { 128, 64, 64, 32, mul_mat_subgroup_size_16, 32, 1, 2, 2, 1, mul_mat_subgroup_size_16 };
  2245. s_warptile_mmqid_int_k = { mul_mat_subgroup_size_32, 32, 32, 32, 32, 32, 1, 2, 1, 1, mul_mat_subgroup_size_16 };
  2246. // chip specific tuning
  2247. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  2248. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  2249. m_warptile_mmqid = m_warptile_mmqid_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  2250. }
  2251. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  2252. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  2253. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  2254. l_align = 128;
  2255. m_align = 64;
  2256. s_align = 32;
  2257. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2258. ggml_type t = (ggml_type)i;
  2259. // Disable medium and large matrix multiplication if not enough shared memory is available
  2260. // Check mmq warptiles as the largest configuration
  2261. // Throw an error if not enough for any matrix multiplication is available
  2262. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  2263. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  2264. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  2265. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  2266. device->mul_mat_m[i] = false;
  2267. device->mul_mat_l[i] = false;
  2268. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  2269. device->mul_mat_l[i] = false;
  2270. }
  2271. // Disable mul_mat_id if not enough shared memory is available
  2272. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmqid, true, t)) {
  2273. device->mul_mat_id_s[i] = false;
  2274. device->mul_mat_id_m[i] = false;
  2275. device->mul_mat_id_l[i] = false;
  2276. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmqid, true, t)) {
  2277. device->mul_mat_id_m[i] = false;
  2278. device->mul_mat_id_l[i] = false;
  2279. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmqid, true, t)) {
  2280. device->mul_mat_id_l[i] = false;
  2281. }
  2282. }
  2283. }
  2284. if (!device->pipeline_matmul_f32) {
  2285. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  2286. }
  2287. if (!device->pipeline_matmul_f32_f16) {
  2288. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  2289. }
  2290. if (!device->pipeline_matmul_id_f32) {
  2291. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  2292. }
  2293. if (!device->pipeline_matmul_bf16) {
  2294. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  2295. }
  2296. if (!device->pipeline_matmul_id_bf16) {
  2297. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  2298. }
  2299. std::vector<std::future<void>> compiles;
  2300. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const char *name, size_t spv_size, const void* spv_data, const char *entrypoint,
  2301. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  2302. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  2303. if (!require_full_subgroups && required_subgroup_size == 0) {
  2304. required_subgroup_size = get_subgroup_size(name, device->architecture);
  2305. }
  2306. if (!pipeline) {
  2307. pipeline = std::make_shared<vk_pipeline_struct>();
  2308. }
  2309. if (!pipeline->initialized) {
  2310. pipeline->name = name;
  2311. pipeline->parameter_count = parameter_count;
  2312. pipeline->push_constant_size = push_constant_size;
  2313. pipeline->wg_denoms = wg_denoms;
  2314. pipeline->align = align;
  2315. pipeline->initialized = true;
  2316. }
  2317. if (!pipeline->needed || pipeline->compiled) {
  2318. return;
  2319. }
  2320. {
  2321. // wait until fewer than N compiles are in progress
  2322. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  2323. std::unique_lock<std::mutex> guard(compile_count_mutex);
  2324. while (compile_count >= N) {
  2325. compile_count_cond.wait(guard);
  2326. }
  2327. compile_count++;
  2328. }
  2329. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  2330. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  2331. };
  2332. auto const &ggml_vk_create_pipeline2 = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const char *entrypoint,
  2333. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  2334. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  2335. return ggml_vk_create_pipeline(device, pipeline, name.c_str(), spv_size, spv_data, entrypoint,
  2336. parameter_count, push_constant_size, wg_denoms, specialization_constants,
  2337. align, disable_robustness, require_full_subgroups, required_subgroup_size);
  2338. };
  2339. 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> {
  2340. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  2341. };
  2342. 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> {
  2343. // For large number of rows, 128 invocations seems to work best.
  2344. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  2345. // can't use 256 for D==80.
  2346. // For scalar, use 128 (arbitrary)
  2347. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  2348. const uint32_t D = (hsk|hsv);
  2349. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  2350. ? scalar_flash_attention_workgroup_size
  2351. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  2352. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  2353. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  2354. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  2355. const uint32_t D_lsb = D ^ (D & (D-1));
  2356. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  2357. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  2358. };
  2359. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  2360. for (auto &fa : device->pipeline_flash_attn_f32_f16[TYPE]) { \
  2361. uint32_t HSK = fa.first.HSK; \
  2362. uint32_t HSV = fa.first.HSV; \
  2363. bool small_rows = fa.first.small_rows; \
  2364. FaCodePath path = fa.first.path; \
  2365. bool aligned = fa.first.aligned; \
  2366. bool f32acc = fa.first.f32acc; \
  2367. if (path == FAPATH) { \
  2368. if (aligned) { \
  2369. if (f32acc) { \
  2370. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_aligned_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_align(FAPATH,HSK,HSV,TYPE,small_rows), true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2371. } else { \
  2372. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_aligned_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,small_rows), fa_align(FAPATH,HSK,HSV,TYPE,small_rows), true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2373. } \
  2374. } else { \
  2375. if (f32acc) { \
  2376. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,small_rows), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2377. } else { \
  2378. ggml_vk_create_pipeline(device, fa.second, "flash_attn_f32_f16_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 6, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,small_rows), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,small_rows), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  2379. } \
  2380. } \
  2381. } \
  2382. }
  2383. CREATE_FA(GGML_TYPE_F32, f32, FA_SCALAR, )
  2384. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  2385. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  2386. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  2387. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2388. if (device->coopmat1_fa_support) {
  2389. CREATE_FA(GGML_TYPE_F32, f32, FA_COOPMAT1, _cm1)
  2390. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  2391. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  2392. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  2393. }
  2394. #endif
  2395. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2396. if (device->coopmat2) {
  2397. CREATE_FA(GGML_TYPE_F32, f32, FA_COOPMAT2, _cm2)
  2398. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  2399. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  2400. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  2401. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  2402. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  2403. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  2404. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  2405. }
  2406. #endif
  2407. #undef CREATE_FA
  2408. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2409. if (device->coopmat2) {
  2410. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2411. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2412. 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); \
  2413. 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); \
  2414. 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); \
  2415. 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); \
  2416. 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); \
  2417. 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); \
  2418. // Create 2 variants, {f16,f32} accumulator
  2419. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2420. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2421. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  2422. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2423. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2424. if (device->coopmat_bf16_support) {
  2425. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  2426. }
  2427. #endif
  2428. 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)
  2429. 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)
  2430. 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)
  2431. 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)
  2432. 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)
  2433. 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)
  2434. 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)
  2435. 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)
  2436. 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)
  2437. 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)
  2438. 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)
  2439. 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)
  2440. 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)
  2441. 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)
  2442. 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)
  2443. 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)
  2444. 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)
  2445. 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)
  2446. 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)
  2447. CREATE_MM2(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_MXFP4], matmul_mxfp4_f16, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  2448. GGML_ASSERT(device->subgroup_ballot);
  2449. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2450. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2451. if (device->coopmat_bf16_support) {
  2452. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  2453. }
  2454. #endif
  2455. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2456. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2457. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2458. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2459. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2460. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2461. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2462. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2463. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2464. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2465. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2466. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2467. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2468. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2469. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2470. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2471. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2472. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2473. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2474. CREATE_MM2(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f16, mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  2475. #undef CREATE_MM
  2476. #undef CREATE_MM2
  2477. } else
  2478. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2479. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2480. if (device->coopmat_support) {
  2481. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2482. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2483. if (device->mul_mat ## ID ## _l[TYPE]) \
  2484. 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); \
  2485. if (device->mul_mat ## ID ## _m[TYPE]) \
  2486. 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); \
  2487. if (device->mul_mat ## ID ## _s[TYPE]) \
  2488. 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); \
  2489. if (device->mul_mat ## ID ## _l[TYPE]) \
  2490. 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); \
  2491. if (device->mul_mat ## ID ## _m[TYPE]) \
  2492. 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); \
  2493. if (device->mul_mat ## ID ## _s[TYPE]) \
  2494. 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); \
  2495. // Create 2 variants, {f16,f32} accumulator
  2496. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2497. if (device->coopmat_acc_f16_support) { \
  2498. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2499. } \
  2500. if (device->coopmat_acc_f32_support) { \
  2501. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2502. } \
  2503. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2504. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2505. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2506. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2507. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2508. if (device->coopmat_bf16_support) {
  2509. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  2510. }
  2511. #endif
  2512. if (device->coopmat_acc_f16_support) {
  2513. 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, );
  2514. 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, );
  2515. 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, );
  2516. 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, );
  2517. 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, );
  2518. 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, );
  2519. 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, );
  2520. 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, );
  2521. 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, );
  2522. 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, );
  2523. 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, );
  2524. 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, );
  2525. 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, );
  2526. 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, );
  2527. 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, );
  2528. 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, );
  2529. 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, );
  2530. 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, );
  2531. 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, );
  2532. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2533. } else {
  2534. 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, );
  2535. 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, );
  2536. 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, );
  2537. 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, );
  2538. 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, );
  2539. 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, );
  2540. 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, );
  2541. 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, );
  2542. 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, );
  2543. 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, );
  2544. 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, );
  2545. 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, );
  2546. 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, );
  2547. 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, );
  2548. 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, );
  2549. 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, );
  2550. 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, );
  2551. 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, );
  2552. 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, );
  2553. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  2554. }
  2555. GGML_ASSERT(device->subgroup_ballot);
  2556. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2557. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2558. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_subgroup_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2559. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2560. if (device->coopmat_bf16_support) {
  2561. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2562. }
  2563. #endif
  2564. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2565. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2566. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2567. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2568. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2569. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2570. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2571. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2572. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2573. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2574. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2575. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2576. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2577. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2578. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2579. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2580. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2581. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2582. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2583. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2584. #undef CREATE_MM2
  2585. #undef CREATE_MM
  2586. } else
  2587. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2588. if (device->fp16) {
  2589. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2590. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2591. if (device->mul_mat ## ID ## _l[TYPE]) \
  2592. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2593. if (device->mul_mat ## ID ## _m[TYPE]) \
  2594. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2595. if (device->mul_mat ## ID ## _s[TYPE]) \
  2596. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2597. if (device->mul_mat ## ID ## _l[TYPE]) \
  2598. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2599. if (device->mul_mat ## ID ## _m[TYPE]) \
  2600. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2601. if (device->mul_mat ## ID ## _s[TYPE]) \
  2602. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2603. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2604. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2605. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2606. } \
  2607. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2608. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2609. } \
  2610. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2611. 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, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2612. } \
  2613. // Create 2 variants, {f16,f32} accumulator
  2614. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2615. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2616. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2617. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2618. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2619. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2620. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2621. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2622. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0], matmul_q4_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2623. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1], matmul_q4_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2624. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0], matmul_q5_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2625. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1], matmul_q5_1_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2626. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0], matmul_q8_0_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2627. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K], matmul_q2_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2628. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K], matmul_q3_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2629. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K], matmul_q4_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2630. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K], matmul_q5_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2631. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K], matmul_q6_k_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2632. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S], matmul_iq1_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2633. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M], matmul_iq1_m_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2634. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS], matmul_iq2_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2635. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS], matmul_iq2_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2636. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S], matmul_iq2_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2637. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS], matmul_iq3_xxs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2638. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S], matmul_iq3_s_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2639. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS], matmul_iq4_xs_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2640. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL], matmul_iq4_nl_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2641. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4], matmul_mxfp4_f32, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2642. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2643. if (device->integer_dot_product) {
  2644. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0], matmul_q4_0_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, , 0);
  2645. 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, , 0);
  2646. 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, , 0);
  2647. 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, , 0);
  2648. 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, , 0);
  2649. CREATE_MMQ(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_MXFP4], matmul_mxfp4_q8_1, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, , 0);
  2650. CREATE_MMQ(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q2_K], matmul_q2_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, , 0);
  2651. CREATE_MMQ(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q3_K], matmul_q3_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, , 0);
  2652. CREATE_MMQ(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_K], matmul_q4_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, , 0);
  2653. CREATE_MMQ(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_K], matmul_q5_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, , 0);
  2654. CREATE_MMQ(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q6_K], matmul_q6_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, , 0);
  2655. }
  2656. #endif
  2657. if (device->subgroup_ballot && device->subgroup_require_full_support && subgroup_min_size_16) {
  2658. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2659. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_subgroup_f16, wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2660. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_subgroup_f16_f32, wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2661. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile_id, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2662. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2663. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2664. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2665. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2666. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2667. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2668. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2669. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2670. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2671. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2672. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_subgroup_iq1_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2673. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_subgroup_iq1_m_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2674. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_subgroup_iq2_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2675. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_subgroup_iq2_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2676. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_subgroup_iq2_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2677. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_subgroup_iq3_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2678. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_subgroup_iq3_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2679. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_subgroup_iq4_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2680. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_subgroup_iq4_nl_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2681. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2682. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2683. if (device->integer_dot_product) {
  2684. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q4_0], matmul_id_subgroup_q4_0_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2685. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q4_1], matmul_id_subgroup_q4_1_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2686. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q5_0], matmul_id_subgroup_q5_0_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2687. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q5_1], matmul_id_subgroup_q5_1_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2688. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q8_0], matmul_id_subgroup_q8_0_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2689. CREATE_MMQ(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_MXFP4], matmul_id_subgroup_mxfp4_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2690. CREATE_MMQ(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q2_K], matmul_id_subgroup_q2_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2691. CREATE_MMQ(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q3_K], matmul_id_subgroup_q3_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2692. CREATE_MMQ(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q4_K], matmul_id_subgroup_q4_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2693. CREATE_MMQ(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q5_K], matmul_id_subgroup_q5_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2694. CREATE_MMQ(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q6_K], matmul_id_subgroup_q6_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2695. }
  2696. #endif
  2697. } else {
  2698. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2699. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2700. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2701. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2702. CREATE_MM2(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0], matmul_id_q4_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2703. CREATE_MM2(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1], matmul_id_q4_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2704. CREATE_MM2(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0], matmul_id_q5_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2705. CREATE_MM2(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1], matmul_id_q5_1_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2706. CREATE_MM2(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0], matmul_id_q8_0_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2707. CREATE_MM2(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K], matmul_id_q2_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2708. CREATE_MM2(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K], matmul_id_q3_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2709. CREATE_MM2(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K], matmul_id_q4_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2710. CREATE_MM2(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K], matmul_id_q5_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2711. CREATE_MM2(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K], matmul_id_q6_k_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2712. CREATE_MM2(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S], matmul_id_iq1_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2713. CREATE_MM2(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M], matmul_id_iq1_m_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2714. CREATE_MM2(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS], matmul_id_iq2_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2715. CREATE_MM2(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS], matmul_id_iq2_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2716. CREATE_MM2(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S], matmul_id_iq2_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2717. CREATE_MM2(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS], matmul_id_iq3_xxs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2718. CREATE_MM2(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S], matmul_id_iq3_s_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2719. CREATE_MM2(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS], matmul_id_iq4_xs_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2720. CREATE_MM2(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL], matmul_id_iq4_nl_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2721. CREATE_MM2(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4], matmul_id_mxfp4_f32, mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2722. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2723. if (device->integer_dot_product) {
  2724. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q4_0], matmul_id_q4_0_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, 0);
  2725. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q4_1], matmul_id_q4_1_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, 0);
  2726. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q5_0], matmul_id_q5_0_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, 0);
  2727. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q5_1], matmul_id_q5_1_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, 0);
  2728. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q8_0], matmul_id_q8_0_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, 0);
  2729. CREATE_MMQ(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_MXFP4], matmul_id_mxfp4_q8_1, mmq_wg_denoms, warptile_mmqid_int, vk_mat_mat_id_push_constants, 4, _id, 0);
  2730. CREATE_MMQ(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q2_K], matmul_id_q2_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, 0);
  2731. CREATE_MMQ(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q3_K], matmul_id_q3_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, 0);
  2732. CREATE_MMQ(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q4_K], matmul_id_q4_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, 0);
  2733. CREATE_MMQ(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q5_K], matmul_id_q5_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, 0);
  2734. CREATE_MMQ(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id_q8_1[GGML_TYPE_Q6_K], matmul_id_q6_k_q8_1, mmq_wg_denoms, warptile_mmqid_int_k, vk_mat_mat_id_push_constants, 4, _id, 0);
  2735. }
  2736. #endif
  2737. }
  2738. #undef CREATE_MM2
  2739. #undef CREATE_MMQ
  2740. #undef CREATE_MM
  2741. } else {
  2742. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2743. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID, REQSUBGROUPSIZE) \
  2744. if (device->mul_mat ## ID ## _l[TYPE]) \
  2745. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2746. if (device->mul_mat ## ID ## _m[TYPE]) \
  2747. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2748. if (device->mul_mat ## ID ## _s[TYPE]) \
  2749. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2750. if (device->mul_mat ## ID ## _l[TYPE]) \
  2751. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2752. if (device->mul_mat ## ID ## _m[TYPE]) \
  2753. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2754. if (device->mul_mat ## ID ## _s[TYPE]) \
  2755. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, REQSUBGROUPSIZE > 0, REQSUBGROUPSIZE); \
  2756. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2757. if (device->mul_mat ## ID ## _l[TYPE]) \
  2758. 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); \
  2759. if (device->mul_mat ## ID ## _m[TYPE]) \
  2760. 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); \
  2761. if (device->mul_mat ## ID ## _s[TYPE]) \
  2762. 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); \
  2763. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2764. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2765. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2766. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2767. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2768. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2769. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2770. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2771. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2772. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2773. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2774. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2775. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2776. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2777. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2778. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2779. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2780. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2781. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2782. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2783. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2784. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2785. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2786. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2787. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat[GGML_TYPE_MXFP4].f32acc, matmul_mxfp4_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, , 0);
  2788. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2789. if (device->integer_dot_product) {
  2790. 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, );
  2791. 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, );
  2792. 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, );
  2793. 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, );
  2794. 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, );
  2795. CREATE_MMQ(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, );
  2796. CREATE_MMQ(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, );
  2797. CREATE_MMQ(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, );
  2798. CREATE_MMQ(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, );
  2799. CREATE_MMQ(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_q8_1, mmq_wg_denoms, warptile_mmq_int_k, vk_mat_mat_push_constants, 3, );
  2800. }
  2801. #endif
  2802. if (device->subgroup_ballot && device->subgroup_require_full_support && subgroup_min_size_16) {
  2803. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_subgroup_f32_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2804. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_subgroup_f16, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2805. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_subgroup_f16_f32, , wg_denoms, warptile_id, vk_mat_mat_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2806. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_subgroup_bf16, , wg_denoms, warptile_id, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size_16);
  2807. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_subgroup_q4_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2808. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_subgroup_q4_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2809. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_subgroup_q5_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2810. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_subgroup_q5_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2811. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_subgroup_q8_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2812. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_subgroup_q2_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2813. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_subgroup_q3_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2814. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_subgroup_q4_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2815. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_subgroup_q5_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2816. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_subgroup_q6_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2817. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_subgroup_iq1_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2818. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_subgroup_iq1_m_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2819. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_subgroup_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2820. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_subgroup_iq2_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2821. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_subgroup_iq2_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2822. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_subgroup_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2823. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_subgroup_iq3_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2824. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_subgroup_iq4_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2825. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_subgroup_iq4_nl_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2826. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_subgroup_mxfp4_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, mul_mat_subgroup_size);
  2827. } else {
  2828. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2829. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2830. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id, 0);
  2831. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2832. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2833. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2834. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2835. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2836. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2837. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2838. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2839. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2840. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2841. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2842. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2843. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2844. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2845. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2846. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2847. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2848. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2849. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2850. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2851. CREATE_MM(GGML_TYPE_MXFP4, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_MXFP4].f32acc, matmul_id_mxfp4_f32, , mmq_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4, _id, 0);
  2852. }
  2853. }
  2854. // reusing CREATE_MM from the fp32 path
  2855. if ((device->coopmat2 || device->coopmat_support)
  2856. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2857. && !device->coopmat_bf16_support
  2858. #endif
  2859. ) {
  2860. // use scalar tile sizes
  2861. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2862. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2863. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2864. l_wg_denoms = {128, 128, 1 };
  2865. m_wg_denoms = { 64, 64, 1 };
  2866. s_wg_denoms = { 32, 32, 1 };
  2867. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, , 0);
  2868. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id, 0);
  2869. }
  2870. #undef CREATE_MM
  2871. // mul mat vec
  2872. // the number of rows computed per shader depends on GPU model and quant
  2873. uint32_t rm_stdq = 1;
  2874. uint32_t rm_kq = 2;
  2875. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2876. if (device->architecture == AMD_GCN) {
  2877. rm_stdq = 2;
  2878. rm_kq = 4;
  2879. }
  2880. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2881. rm_stdq = 2;
  2882. uint32_t rm_iq = 2 * rm_kq;
  2883. const bool use_subgroups = device->subgroup_arithmetic && device->architecture != vk_device_architecture::AMD_GCN;
  2884. // Ensure a subgroup size >= 16 is available
  2885. const bool use_subgroups16 = use_subgroups && subgroup_min_size_16;
  2886. const uint32_t subgroup_size = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control && device->subgroup_min_size <= 16 && device->subgroup_max_size >= 16) ? 16 : device->subgroup_size;
  2887. const uint32_t subgroup_size16 = std::max(subgroup_size, 16u);
  2888. const uint32_t force_subgroup_size = use_subgroups ? subgroup_size : 0;
  2889. const uint32_t force_subgroup_size16 = use_subgroups16 ? subgroup_size16 : 0;
  2890. for (uint32_t w = 0; w < DMMV_WG_SIZE_COUNT; ++w) {
  2891. const uint32_t wg_size_subgroup = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size : (subgroup_size * 4);
  2892. const uint32_t wg_size_subgroup16 = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size16 : (subgroup_size16 * 4);
  2893. const shader_reduction_mode reduc = (use_subgroups && w == DMMV_WG_SIZE_SUBGROUP) ? SHADER_REDUCTION_MODE_SUBGROUP :
  2894. (use_subgroups && w == DMMV_WG_SIZE_LARGE) ? SHADER_REDUCTION_MODE_HYBRID :
  2895. SHADER_REDUCTION_MODE_SHMEM;
  2896. const shader_reduction_mode reduc16 = (use_subgroups16 && w == DMMV_WG_SIZE_SUBGROUP) ? SHADER_REDUCTION_MODE_SUBGROUP :
  2897. (use_subgroups16 && w == DMMV_WG_SIZE_LARGE) ? SHADER_REDUCTION_MODE_HYBRID :
  2898. SHADER_REDUCTION_MODE_SHMEM;
  2899. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2900. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32", arr_dmmv_f32_f32_f32_len[reduc], arr_dmmv_f32_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2901. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32", arr_dmmv_f16_f32_f32_len[reduc], arr_dmmv_f16_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2902. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32", arr_dmmv_bf16_f32_f32_len[reduc], arr_dmmv_bf16_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2903. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32", arr_dmmv_q4_0_f32_f32_len[reduc], arr_dmmv_q4_0_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2904. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32", arr_dmmv_q4_1_f32_f32_len[reduc], arr_dmmv_q4_1_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2905. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32", arr_dmmv_q5_0_f32_f32_len[reduc], arr_dmmv_q5_0_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2906. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32", arr_dmmv_q5_1_f32_f32_len[reduc], arr_dmmv_q5_1_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2907. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32", arr_dmmv_q8_0_f32_f32_len[reduc], arr_dmmv_q8_0_f32_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2908. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32", arr_dmmv_q2_k_f32_f32_len[reduc16], arr_dmmv_q2_k_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2909. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32", arr_dmmv_q3_k_f32_f32_len[reduc16], arr_dmmv_q3_k_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2910. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32", arr_dmmv_q4_k_f32_f32_len[reduc16], arr_dmmv_q4_k_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2911. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32", arr_dmmv_q5_k_f32_f32_len[reduc16], arr_dmmv_q5_k_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2912. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32", arr_dmmv_q6_k_f32_f32_len[reduc16], arr_dmmv_q6_k_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2913. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32", arr_dmmv_iq1_s_f32_f32_len[reduc16], arr_dmmv_iq1_s_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2914. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32", arr_dmmv_iq1_m_f32_f32_len[reduc16], arr_dmmv_iq1_m_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2915. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32", arr_dmmv_iq2_xxs_f32_f32_len[reduc16], arr_dmmv_iq2_xxs_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2916. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32", arr_dmmv_iq2_xs_f32_f32_len[reduc16], arr_dmmv_iq2_xs_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2917. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32", arr_dmmv_iq2_s_f32_f32_len[reduc16], arr_dmmv_iq2_s_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2918. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32", arr_dmmv_iq3_xxs_f32_f32_len[reduc16], arr_dmmv_iq3_xxs_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2919. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32", arr_dmmv_iq3_s_f32_f32_len[reduc16], arr_dmmv_iq3_s_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2920. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32", arr_dmmv_iq4_xs_f32_f32_len[reduc16], arr_dmmv_iq4_xs_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2921. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32", arr_dmmv_iq4_nl_f32_f32_len[reduc16], arr_dmmv_iq4_nl_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2922. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f32_f32", arr_dmmv_mxfp4_f32_f32_len[reduc16], arr_dmmv_mxfp4_f32_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2923. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32", arr_dmmv_f32_f16_f32_len[reduc], arr_dmmv_f32_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2924. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32", arr_dmmv_f16_f16_f32_len[reduc], arr_dmmv_f16_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2925. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32", arr_dmmv_bf16_f16_f32_len[reduc], arr_dmmv_bf16_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
  2926. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32", arr_dmmv_q4_0_f16_f32_len[reduc], arr_dmmv_q4_0_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2927. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32", arr_dmmv_q4_1_f16_f32_len[reduc], arr_dmmv_q4_1_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2928. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32", arr_dmmv_q5_0_f16_f32_len[reduc], arr_dmmv_q5_0_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2929. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32", arr_dmmv_q5_1_f16_f32_len[reduc], arr_dmmv_q5_1_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup, 2*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2930. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32", arr_dmmv_q8_0_f16_f32_len[reduc], arr_dmmv_q8_0_f16_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup, 1*rm_stdq, i+1}, 1, true, use_subgroups, force_subgroup_size);
  2931. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32", arr_dmmv_q2_k_f16_f32_len[reduc16], arr_dmmv_q2_k_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2932. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32", arr_dmmv_q3_k_f16_f32_len[reduc16], arr_dmmv_q3_k_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2933. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32", arr_dmmv_q4_k_f16_f32_len[reduc16], arr_dmmv_q4_k_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2934. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32", arr_dmmv_q5_k_f16_f32_len[reduc16], arr_dmmv_q5_k_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2935. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32", arr_dmmv_q6_k_f16_f32_len[reduc16], arr_dmmv_q6_k_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {wg_size_subgroup16, rm_kq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2936. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32", arr_dmmv_iq1_s_f16_f32_len[reduc16], arr_dmmv_iq1_s_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2937. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32", arr_dmmv_iq1_m_f16_f32_len[reduc16], arr_dmmv_iq1_m_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2938. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32", arr_dmmv_iq2_xxs_f16_f32_len[reduc16], arr_dmmv_iq2_xxs_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2939. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32", arr_dmmv_iq2_xs_f16_f32_len[reduc16], arr_dmmv_iq2_xs_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2940. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32", arr_dmmv_iq2_s_f16_f32_len[reduc16], arr_dmmv_iq2_s_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2941. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32", arr_dmmv_iq3_xxs_f16_f32_len[reduc16], arr_dmmv_iq3_xxs_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2942. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32", arr_dmmv_iq3_s_f16_f32_len[reduc16], arr_dmmv_iq3_s_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2943. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32", arr_dmmv_iq4_xs_f16_f32_len[reduc16], arr_dmmv_iq4_xs_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2944. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32", arr_dmmv_iq4_nl_f16_f32_len[reduc16], arr_dmmv_iq4_nl_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2945. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f16_f32", arr_dmmv_mxfp4_f16_f32_len[reduc16], arr_dmmv_mxfp4_f16_f32_data[reduc16], "main", 4, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
  2946. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2947. if (device->integer_dot_product) {
  2948. const uint32_t subgroup_size_int = (device->vendor_id == VK_VENDOR_ID_INTEL && device->subgroup_size_control) ? device->subgroup_min_size : device->subgroup_size;
  2949. const uint32_t wg_size_subgroup_int = (w == DMMV_WG_SIZE_SUBGROUP) ? subgroup_size_int : (subgroup_size_int * 4);
  2950. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_q8_1_f32", arr_dmmv_q4_0_q8_1_f32_len[reduc], arr_dmmv_q4_0_q8_1_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2951. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_q8_1_f32", arr_dmmv_q4_1_q8_1_f32_len[reduc], arr_dmmv_q4_1_q8_1_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2952. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_q8_1_f32", arr_dmmv_q5_0_q8_1_f32_len[reduc], arr_dmmv_q5_0_q8_1_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2953. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_q8_1_f32", arr_dmmv_q5_1_q8_1_f32_len[reduc], arr_dmmv_q5_1_q8_1_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {wg_size_subgroup_int, 2*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2954. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_q8_1_f32[w][GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_q8_1_f32", arr_dmmv_q8_0_q8_1_f32_len[reduc], arr_dmmv_q8_0_q8_1_f32_data[reduc], "main", 4, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {wg_size_subgroup_int, 1*rm_stdq, i+1}, 1, true, use_subgroups, subgroup_size_int);
  2955. }
  2956. #endif // GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT
  2957. }
  2958. }
  2959. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2960. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2961. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2962. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2963. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2964. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2965. 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", 5, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2966. 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", 5, sizeof(vk_mat_vec_id_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq}, 1, true);
  2967. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2968. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2969. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2970. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2971. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2972. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2973. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2974. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2975. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2976. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2977. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2978. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2979. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2980. 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", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2981. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_MXFP4], "mul_mat_vec_id_mxfp4_f32", mul_mat_vec_id_mxfp4_f32_len, mul_mat_vec_id_mxfp4_f32_data, "main", 5, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2982. // dequant shaders
  2983. 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);
  2984. 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);
  2985. 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);
  2986. 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);
  2987. 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);
  2988. 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);
  2989. 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);
  2990. 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);
  2991. 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);
  2992. 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);
  2993. 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);
  2994. 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);
  2995. 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);
  2996. 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);
  2997. 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);
  2998. 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);
  2999. 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);
  3000. 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);
  3001. 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);
  3002. 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);
  3003. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_MXFP4], "dequant_mxfp4", dequant_mxfp4_len, dequant_mxfp4_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  3004. // get_rows
  3005. 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);
  3006. 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);
  3007. 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);
  3008. 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);
  3009. 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);
  3010. 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);
  3011. 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);
  3012. 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);
  3013. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q2_K], "get_rows_q2_k", get_rows_q2_k_len, get_rows_q2_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3014. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q3_K], "get_rows_q3_k", get_rows_q3_k_len, get_rows_q3_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3015. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_K], "get_rows_q4_k", get_rows_q4_k_len, get_rows_q4_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3016. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_K], "get_rows_q5_k", get_rows_q5_k_len, get_rows_q5_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3017. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q6_K], "get_rows_q6_k", get_rows_q6_k_len, get_rows_q6_k_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3018. 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);
  3019. 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);
  3020. 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);
  3021. 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);
  3022. 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);
  3023. 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);
  3024. 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);
  3025. 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);
  3026. 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);
  3027. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_MXFP4], "get_rows_mxfp4", get_rows_mxfp4_len, get_rows_mxfp4_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3028. 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);
  3029. 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);
  3030. 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);
  3031. 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);
  3032. 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);
  3033. 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);
  3034. 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);
  3035. 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);
  3036. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q2_K], "get_rows_q2_k_f32", get_rows_q2_k_f32_len, get_rows_q2_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3037. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q3_K], "get_rows_q3_k_f32", get_rows_q3_k_f32_len, get_rows_q3_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3038. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_K], "get_rows_q4_k_f32", get_rows_q4_k_f32_len, get_rows_q4_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3039. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_K], "get_rows_q5_k_f32", get_rows_q5_k_f32_len, get_rows_q5_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3040. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q6_K], "get_rows_q6_k_f32", get_rows_q6_k_f32_len, get_rows_q6_k_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3041. 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);
  3042. 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);
  3043. 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);
  3044. 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);
  3045. 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);
  3046. 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);
  3047. 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);
  3048. 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);
  3049. 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);
  3050. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_MXFP4], "get_rows_mxfp4_f32", get_rows_mxfp4_f32_len, get_rows_mxfp4_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  3051. 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);
  3052. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_split_k_reduce, "fa_split_k_reduce", fa_split_k_reduce_len, fa_split_k_reduce_data, "main", 3, 5 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  3053. if (device->subgroup_clustered && device->subgroup_require_full_support) {
  3054. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_subgroup_len, quantize_q8_1_subgroup_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1, true, true);
  3055. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1_x4, "quantize_q8_1_x4", quantize_q8_1_x4_subgroup_len, quantize_q8_1_x4_subgroup_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1, true, true);
  3056. } else {
  3057. 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);
  3058. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1_x4, "quantize_q8_1_x4", quantize_q8_1_x4_len, quantize_q8_1_x4_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  3059. }
  3060. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  3061. if (device->subgroup_arithmetic && device->subgroup_require_full_support) {
  3062. ggml_vk_create_pipeline2(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 4, 7 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  3063. } else {
  3064. ggml_vk_create_pipeline2(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 4, 7 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  3065. }
  3066. }
  3067. 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", 4, 13 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  3068. 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);
  3069. 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);
  3070. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1, true);
  3071. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1, true);
  3072. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_partials_f32, "rms_norm_partials_f32", rms_norm_partials_f32_len, rms_norm_partials_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1, true);
  3073. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_partials_f32, "rms_norm_mul_partials_f32", rms_norm_partials_f32_len, rms_norm_partials_f32_data, "main", 4, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1, true);
  3074. 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);
  3075. 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);
  3076. 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);
  3077. 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);
  3078. 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);
  3079. 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);
  3080. 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);
  3081. ggml_vk_create_pipeline(device, device->pipeline_cpy_i32_f32, "cpy_i32_f32", cpy_i32_f32_len, cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  3082. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_i32, "cpy_f32_i32", cpy_f32_i32_len, cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  3083. 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);
  3084. 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);
  3085. 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);
  3086. 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);
  3087. 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);
  3088. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_i32_f32, "contig_cpy_i32_f32", contig_cpy_i32_f32_len, contig_cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  3089. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_i32, "contig_cpy_f32_i32", contig_cpy_f32_i32_len, contig_cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  3090. if (device->float_controls_rte_fp16) {
  3091. 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);
  3092. 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);
  3093. 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);
  3094. 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);
  3095. 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);
  3096. 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);
  3097. } else {
  3098. 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);
  3099. 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);
  3100. 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);
  3101. 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);
  3102. 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);
  3103. 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);
  3104. }
  3105. #define SET_ROWS(itype, rte) \
  3106. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_F32], "set_rows_f32" #itype, set_rows_f32 ## itype ## rte ## _len, set_rows_f32 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3107. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_F16], "set_rows_f16" #itype, set_rows_f16 ## itype ## rte ## _len, set_rows_f16 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3108. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_BF16], "set_rows_bf16" #itype, set_rows_bf16 ## itype ## rte ## _len, set_rows_bf16 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3109. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q4_0], "set_rows_q4_0" #itype, set_rows_q4_0 ## itype ## rte ## _len, set_rows_q4_0 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3110. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q4_1], "set_rows_q4_1" #itype, set_rows_q4_1 ## itype ## rte ## _len, set_rows_q4_1 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3111. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q5_0], "set_rows_q5_0" #itype, set_rows_q5_0 ## itype ## rte ## _len, set_rows_q5_0 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3112. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q5_1], "set_rows_q5_1" #itype, set_rows_q5_1 ## itype ## rte ## _len, set_rows_q5_1 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3113. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_Q8_0], "set_rows_q8_0" #itype, set_rows_q8_0 ## itype ## rte ## _len, set_rows_q8_0 ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true); \
  3114. ggml_vk_create_pipeline(device, device->pipeline_set_rows ## itype [GGML_TYPE_IQ4_NL], "set_rows_iq4_nl" #itype, set_rows_iq4_nl ## itype ## rte ## _len, set_rows_iq4_nl ## itype ## rte ## _data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  3115. if (device->float_controls_rte_fp16) {
  3116. SET_ROWS(_i32, _rte)
  3117. SET_ROWS(_i64, _rte)
  3118. } else {
  3119. SET_ROWS(_i32, )
  3120. SET_ROWS(_i64, )
  3121. }
  3122. #undef SET_ROWS
  3123. 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);
  3124. 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);
  3125. 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);
  3126. 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);
  3127. 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);
  3128. 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);
  3129. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  3130. std::string s;
  3131. s += std::string(src0_f16 ? "_f16" : "_f32");
  3132. s += std::string(src1_f16 ? "_f16" : "_f32");
  3133. s += std::string(dst_f16 ? "_f16" : "_f32");
  3134. return s;
  3135. };
  3136. bool rte = device->float_controls_rte_fp16;
  3137. #define CREATE_BINARY(name, namemod, spec, bindings) \
  3138. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  3139. ggml_vk_create_pipeline2(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  3140. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d][rte], name ## _data[s0][s1][d][rte], \
  3141. "main", (bindings), sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  3142. CREATE_BINARY(add, , {0}, 4)
  3143. CREATE_BINARY(add, _norepeat, {1}, 4)
  3144. CREATE_BINARY(sub, , {0}, 3)
  3145. CREATE_BINARY(sub, _norepeat, {1}, 3)
  3146. CREATE_BINARY(mul, , {0}, 3)
  3147. CREATE_BINARY(mul, _norepeat, {1}, 3)
  3148. CREATE_BINARY(div, , {0}, 3)
  3149. CREATE_BINARY(div, _norepeat, {1}, 3)
  3150. CREATE_BINARY(add_rms, , {0}, 4)
  3151. CREATE_BINARY(add_rms, _norepeat, {1}, 4)
  3152. #undef CREATE_BINARY
  3153. if (device->multi_add) {
  3154. for (uint32_t i = 0; i < MAX_FUSED_ADDS; ++i) {
  3155. ggml_vk_create_pipeline2(device, device->pipeline_multi_add[i], "multi_add_f32_" + std::to_string(i+1), multi_add_f32_len, multi_add_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  3156. ggml_vk_create_pipeline2(device, device->pipeline_multi_add_rms[i], "multi_add_rms_f32_" + std::to_string(i+1), multi_add_rms_f32_len, multi_add_rms_f32_data, "main", MAX_PARAMETER_COUNT, sizeof(vk_op_multi_add_push_constants), {512, 1, 1}, {i+2}, 1);
  3157. }
  3158. }
  3159. ggml_vk_create_pipeline(device, device->pipeline_add_id_f32, "add_id_f32", add_id_f32_len, add_id_f32_data, "main", 4, sizeof(vk_op_add_id_push_constants), {1, 1, 1}, {}, 1);
  3160. 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);
  3161. 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);
  3162. 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);
  3163. 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);
  3164. ggml_vk_create_pipeline(device, device->pipeline_upscale_nearest_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_NEAREST}, 1);
  3165. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR}, 1);
  3166. 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);
  3167. 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);
  3168. ggml_vk_create_pipeline(device, device->pipeline_sqrt_f32, "sqrt_f32", sqrt_f32_len, sqrt_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  3169. 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);
  3170. 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);
  3171. 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);
  3172. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_pad_push_constants), {512, 1, 1}, {}, 1);
  3173. 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);
  3174. 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);
  3175. 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);
  3176. #define CREATE_UNARY(name) \
  3177. 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); \
  3178. 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);
  3179. CREATE_UNARY(gelu)
  3180. CREATE_UNARY(gelu_erf)
  3181. CREATE_UNARY(gelu_quick)
  3182. CREATE_UNARY(silu)
  3183. CREATE_UNARY(relu)
  3184. CREATE_UNARY(tanh)
  3185. CREATE_UNARY(sigmoid)
  3186. CREATE_UNARY(hardsigmoid)
  3187. CREATE_UNARY(hardswish)
  3188. #undef CREATE_UNARY
  3189. #define CREATE_UNARY_RTE(name) \
  3190. if (device->float_controls_rte_fp16) { \
  3191. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32_rte", name ## _f32_rte_len, name ## _f32_rte_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  3192. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16_rte", name ## _f16_rte_len, name ## _f16_rte_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1); \
  3193. } else { \
  3194. 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); \
  3195. 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); \
  3196. }
  3197. CREATE_UNARY_RTE(exp)
  3198. #undef CREATE_UNARY_RTE
  3199. #define CREATE_GLU(name) \
  3200. if (device->float_controls_rte_fp16) { \
  3201. ggml_vk_create_pipeline(device, device->pipeline_ ## name [0], #name "_f32_rte", name ## _f32_rte_len, name ## _f32_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  3202. ggml_vk_create_pipeline(device, device->pipeline_ ## name [1], #name "_f16_rte", name ## _f16_rte_len, name ## _f16_rte_data, "main", 3, sizeof(vk_op_glu_push_constants), {512, 1, 1}, {}, 1, true); \
  3203. } else { \
  3204. 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); \
  3205. 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); \
  3206. }
  3207. CREATE_GLU(geglu)
  3208. CREATE_GLU(reglu)
  3209. CREATE_GLU(swiglu)
  3210. CREATE_GLU(swiglu_oai)
  3211. CREATE_GLU(geglu_erf)
  3212. CREATE_GLU(geglu_quick)
  3213. #undef CREATE_GLU
  3214. 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);
  3215. 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);
  3216. 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);
  3217. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  3218. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  3219. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  3220. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 4, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  3221. ggml_vk_create_pipeline(device, device->pipeline_soft_max_back_f32, "soft_max_back_f32", soft_max_back_f32_len, soft_max_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1, true);
  3222. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3223. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32, "rope_neox_f32", rope_neox_f32_len, rope_neox_f32_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3224. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f32, "rope_multi_f32", rope_multi_f32_len, rope_multi_f32_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3225. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f32, "rope_vision_f32", rope_vision_f32_len, rope_vision_f32_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3226. if (device->float_controls_rte_fp16) {
  3227. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_rte_len, rope_norm_f16_rte_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3228. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_rte_len, rope_neox_f16_rte_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3229. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_rte_len, rope_multi_f16_rte_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3230. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_rte_len, rope_vision_f16_rte_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3231. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32_f16, "rope_norm_f32_f16", rope_norm_f32_f16_rte_len, rope_norm_f32_f16_rte_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3232. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32_f16, "rope_neox_f32_f16", rope_neox_f32_f16_rte_len, rope_neox_f32_f16_rte_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3233. } else {
  3234. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3235. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_len, rope_neox_f16_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3236. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_len, rope_multi_f16_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3237. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_len, rope_vision_f16_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3238. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32_f16, "rope_norm_f32_f16", rope_norm_f32_f16_len, rope_norm_f32_f16_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3239. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32_f16, "rope_neox_f32_f16", rope_neox_f32_f16_len, rope_neox_f32_f16_data, "main", 5, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  3240. }
  3241. for (uint32_t i = 0; i < num_argsort_pipelines; ++i) {
  3242. ggml_vk_create_pipeline2(device, device->pipeline_argsort_f32[i], "argsort_f32_"+std::to_string(i), argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1u<<i, 1, 1}, {1u<<i, i}, 1, true);
  3243. }
  3244. 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);
  3245. ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_sum_rows_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  3246. 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);
  3247. #define IM2COL(bda) \
  3248. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32 ## bda ## _len, im2col_f32 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true); \
  3249. ggml_vk_create_pipeline(device, device->pipeline_im2col_3d_f32, "im2col_3d_f32", im2col_3d_f32 ## bda ## _len, im2col_3d_f32 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_3d_push_constants), {512, 1, 1}, { 512 }, 1, true); \
  3250. if (device->float_controls_rte_fp16) { \
  3251. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte ## bda ## _len, im2col_f32_f16_rte ## bda ## _data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true); \
  3252. ggml_vk_create_pipeline(device, device->pipeline_im2col_3d_f32_f16, "im2col_3d_f32_f16", im2col_3d_f32_f16_rte ## bda ## _len, im2col_3d_f32_f16_rte ## bda ## _data, "main", 2, sizeof(vk_op_im2col_3d_push_constants), {512, 1, 1}, { 512 }, 1, true); \
  3253. } else { \
  3254. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16 ## bda ## _len, im2col_f32_f16 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true); \
  3255. ggml_vk_create_pipeline(device, device->pipeline_im2col_3d_f32_f16, "im2col_3d_f32_f16", im2col_3d_f32_f16 ## bda ## _len, im2col_3d_f32_f16 ## bda ## _data, "main", 2, sizeof(vk_op_im2col_3d_push_constants), {512, 1, 1}, { 512 }, 1, true); \
  3256. }
  3257. if (device->shader_int64 && device->buffer_device_address) {
  3258. IM2COL(_bda)
  3259. } else {
  3260. IM2COL()
  3261. }
  3262. 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);
  3263. 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);
  3264. 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);
  3265. 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);
  3266. 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);
  3267. if (device->subgroup_arithmetic && device->subgroup_require_full_support) {
  3268. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 16}, 1, true, true);
  3269. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 16}, 1, true, true);
  3270. } else {
  3271. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 16}, 1, true, true);
  3272. ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 16}, 1, true, true);
  3273. }
  3274. ggml_vk_create_pipeline(device, device->pipeline_ssm_conv_f32, "ssm_conv_f32", ssm_conv_f32_len, ssm_conv_f32_data, "main", 3, sizeof(vk_op_ssm_conv_push_constants), {32, 1, 1}, {32}, 1);
  3275. 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);
  3276. ggml_vk_create_pipeline(device, device->pipeline_opt_step_sgd_f32, "opt_step_sgd_f32", opt_step_sgd_f32_len, opt_step_sgd_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  3277. // conv2d, conv_transpose_2d
  3278. for (uint32_t s = 0; s < CONV_SHAPE_COUNT; ++s) {
  3279. uint32_t conv2d_WG_SIZE = 256;
  3280. uint32_t conv2d_BS_K = 128;
  3281. uint32_t conv2d_BS_CRS = 16;
  3282. uint32_t use_collectives = 0; // Enables subgroup ops for preventing the re-calculation of indices.
  3283. uint32_t conv2d_BS_NPQ = 128;
  3284. uint32_t conv2d_TS_K = 8;
  3285. uint32_t conv2d_SHMEM_PAD = 4;
  3286. bool conv2d_UNROLL = true;
  3287. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3288. if (device->coopmat2) {
  3289. conv2d_SHMEM_PAD = 8; // 8 float16_t
  3290. }
  3291. #endif
  3292. if (device->vendor_id == VK_VENDOR_ID_INTEL) {
  3293. conv2d_SHMEM_PAD = 0;
  3294. conv2d_UNROLL = false;
  3295. } else if (device->vendor_id == VK_VENDOR_ID_AMD) {
  3296. conv2d_SHMEM_PAD = device->architecture == vk_device_architecture::AMD_GCN ? 1 : 4;
  3297. }
  3298. switch (s) {
  3299. default:
  3300. case CONV_SHAPE_128x128:
  3301. conv2d_BS_K = 128;
  3302. conv2d_BS_NPQ = 128;
  3303. conv2d_BS_CRS = 16;
  3304. if (device->vendor_id == VK_VENDOR_ID_AMD && device->architecture != vk_device_architecture::AMD_GCN) {
  3305. conv2d_UNROLL = false;
  3306. }
  3307. break;
  3308. case CONV_SHAPE_64x32:
  3309. conv2d_BS_K = 64;
  3310. conv2d_BS_NPQ = 32;
  3311. conv2d_BS_CRS = 32;
  3312. conv2d_TS_K = 4;
  3313. break;
  3314. case CONV_SHAPE_32x256:
  3315. conv2d_BS_K = 32;
  3316. conv2d_BS_NPQ = 256;
  3317. conv2d_BS_CRS = 16;
  3318. break;
  3319. }
  3320. // Use collectives on pre-Turing NVIDIA GPUs and GCN AMD cards, which had slower integer math.
  3321. bool allow_collectives_nv = device->vendor_id != VK_VENDOR_ID_NVIDIA ||
  3322. device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  3323. bool allow_collectives_amd = device->vendor_id != VK_VENDOR_ID_AMD ||
  3324. device->architecture == vk_device_architecture::AMD_GCN;
  3325. if (device->subgroup_shuffle &&
  3326. device->vendor_id != VK_VENDOR_ID_INTEL && // Do not enable collectives on Intel, see PR 14316.
  3327. allow_collectives_nv &&
  3328. allow_collectives_amd) {
  3329. use_collectives = 1;
  3330. conv2d_BS_CRS = std::min(
  3331. device->subgroup_size,
  3332. conv2d_BS_CRS); // CRS block size should be capped at subgroup size for correctness when shuffle is used.
  3333. }
  3334. uint32_t conv2d_shmem_req =
  3335. (conv2d_BS_K * (conv2d_BS_CRS + conv2d_SHMEM_PAD) + conv2d_BS_CRS * (conv2d_BS_NPQ + conv2d_SHMEM_PAD)) * sizeof(float);
  3336. if (device->properties.limits.maxComputeSharedMemorySize < conv2d_shmem_req) {
  3337. conv2d_BS_CRS = 8;
  3338. if (use_collectives) {
  3339. conv2d_BS_CRS = std::min(device->subgroup_size, conv2d_BS_CRS);
  3340. }
  3341. }
  3342. std::array<uint32_t, 3> wg_denoms = { conv2d_BS_K, conv2d_BS_NPQ, 1 };
  3343. std::vector<uint32_t> spec_constants = { conv2d_WG_SIZE, conv2d_BS_K, conv2d_BS_CRS, conv2d_BS_NPQ, conv2d_TS_K, use_collectives, conv2d_SHMEM_PAD };
  3344. #define CREATE_CONV(name, type_suffix, spv_suffix) \
  3345. ggml_vk_create_pipeline( \
  3346. device, device->pipeline_##name##type_suffix[s], #name #type_suffix, \
  3347. name##type_suffix##spv_suffix##_len, name##type_suffix##spv_suffix##_data, "main", 3, \
  3348. sizeof(vk_op_##name##_push_constants), wg_denoms, spec_constants, 1, true, use_collectives);
  3349. #define CREATE_CONVS(spv_suffix) \
  3350. CREATE_CONV(conv2d, _f32, spv_suffix) \
  3351. CREATE_CONV(conv2d, _f16_f32, spv_suffix) \
  3352. if (device->properties.limits.maxPushConstantsSize >= sizeof(vk_op_conv_transpose_2d_push_constants)) { \
  3353. CREATE_CONV(conv_transpose_2d, _f32, spv_suffix) \
  3354. CREATE_CONV(conv_transpose_2d, _f16_f32, spv_suffix) \
  3355. }
  3356. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3357. if (device->coopmat2) {
  3358. CREATE_CONVS(_cm2)
  3359. } else
  3360. #endif
  3361. if (conv2d_UNROLL) {
  3362. CREATE_CONVS(_unroll)
  3363. } else {
  3364. CREATE_CONVS( )
  3365. }
  3366. #undef CREATE_CONV
  3367. #undef CREATE_CONVS
  3368. }
  3369. 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);
  3370. 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);
  3371. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_whcn_f16_f32, "conv2d_dw_whcn_f16_f32", conv2d_dw_whcn_f16_f32_len, conv2d_dw_whcn_f16_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  3372. ggml_vk_create_pipeline(device, device->pipeline_conv2d_dw_cwhn_f16_f32, "conv2d_dw_cwhn_f16_f32", conv2d_dw_cwhn_f16_f32_len, conv2d_dw_cwhn_f16_f32_data, "main", 3, sizeof(vk_op_conv2d_dw_push_constants), {512, 1, 1}, {}, 1);
  3373. for (uint32_t i = 0; i < num_topk_moe_pipelines; ++i) {
  3374. ggml_vk_create_pipeline2(device, device->pipeline_topk_moe[i][TOPK_MOE_EARLY_SOFTMAX], "topk_moe_f32_early_softmax_"+std::to_string(i), topk_moe_f32_len, topk_moe_f32_data, "main", 3, sizeof(vk_op_topk_moe_push_constants), {1, 1, 1}, {device->subgroup_size, 1u<<i, 0, 0}, 1, true, true);
  3375. ggml_vk_create_pipeline2(device, device->pipeline_topk_moe[i][TOPK_MOE_EARLY_SOFTMAX_NORM], "topk_moe_f32_early_softmax_norm"+std::to_string(i), topk_moe_f32_len, topk_moe_f32_data, "main", 3, sizeof(vk_op_topk_moe_push_constants), {1, 1, 1}, {device->subgroup_size, 1u<<i, 1, 0}, 1, true, true);
  3376. ggml_vk_create_pipeline2(device, device->pipeline_topk_moe[i][TOPK_MOE_LATE_SOFTMAX], "topk_moe_f32_late_softmax"+std::to_string(i), topk_moe_f32_len, topk_moe_f32_data, "main", 3, sizeof(vk_op_topk_moe_push_constants), {1, 1, 1}, {device->subgroup_size, 1u<<i, 0, 1}, 1, true, true);
  3377. }
  3378. for (auto &c : compiles) {
  3379. c.wait();
  3380. }
  3381. }
  3382. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  3383. static vk_device ggml_vk_get_device(size_t idx) {
  3384. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  3385. if (vk_instance.devices[idx] == nullptr) {
  3386. VK_LOG_DEBUG("Initializing new vk_device");
  3387. vk_device device = std::make_shared<vk_device_struct>();
  3388. vk_instance.devices[idx] = device;
  3389. #ifdef GGML_VULKAN_MEMORY_DEBUG
  3390. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  3391. #endif
  3392. if (vk_perf_logger_enabled) {
  3393. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  3394. }
  3395. size_t dev_num = vk_instance.device_indices[idx];
  3396. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  3397. if (dev_num >= physical_devices.size()) {
  3398. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3399. throw std::runtime_error("Device not found");
  3400. }
  3401. device->physical_device = physical_devices[dev_num];
  3402. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  3403. device->architecture = get_device_architecture(device->physical_device);
  3404. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  3405. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  3406. const char* GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM = getenv("GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM");
  3407. device->disable_host_visible_vidmem = GGML_VK_DISABLE_HOST_VISIBLE_VIDMEM != nullptr;
  3408. const char* GGML_VK_ALLOW_SYSMEM_FALLBACK = getenv("GGML_VK_ALLOW_SYSMEM_FALLBACK");
  3409. device->allow_sysmem_fallback = GGML_VK_ALLOW_SYSMEM_FALLBACK != nullptr;
  3410. const char* GGML_VK_DISABLE_GRAPH_OPTIMIZE = getenv("GGML_VK_DISABLE_GRAPH_OPTIMIZE");
  3411. device->disable_graph_optimize = GGML_VK_DISABLE_GRAPH_OPTIMIZE != nullptr;
  3412. bool fp16_storage = false;
  3413. bool fp16_compute = false;
  3414. bool maintenance4_support = false;
  3415. bool sm_builtins = false;
  3416. bool amd_shader_core_properties2 = false;
  3417. bool pipeline_robustness = false;
  3418. bool coopmat2_support = false;
  3419. bool pipeline_executable_properties_support = false;
  3420. device->coopmat_support = false;
  3421. device->integer_dot_product = false;
  3422. bool bfloat16_support = false;
  3423. for (const auto& properties : ext_props) {
  3424. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  3425. maintenance4_support = true;
  3426. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3427. fp16_storage = true;
  3428. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3429. fp16_compute = true;
  3430. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  3431. sm_builtins = true;
  3432. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  3433. amd_shader_core_properties2 = true;
  3434. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  3435. pipeline_robustness = true;
  3436. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  3437. device->subgroup_size_control = true;
  3438. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3439. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3440. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3441. device->coopmat_support = true;
  3442. device->coopmat_m = 0;
  3443. device->coopmat_n = 0;
  3444. device->coopmat_k = 0;
  3445. #endif
  3446. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3447. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3448. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3449. coopmat2_support = true;
  3450. #endif
  3451. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3452. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3453. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3454. device->integer_dot_product = true;
  3455. #endif
  3456. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3457. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  3458. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3459. bfloat16_support = true;
  3460. #endif
  3461. } else if (strcmp("VK_KHR_pipeline_executable_properties", properties.extensionName) == 0) {
  3462. pipeline_executable_properties_support = true;
  3463. }
  3464. }
  3465. vk::PhysicalDeviceProperties2 props2;
  3466. vk::PhysicalDeviceMaintenance3Properties props3;
  3467. vk::PhysicalDeviceMaintenance4Properties props4;
  3468. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3469. vk::PhysicalDeviceDriverProperties driver_props;
  3470. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  3471. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  3472. vk::PhysicalDeviceVulkan11Properties vk11_props;
  3473. vk::PhysicalDeviceVulkan12Properties vk12_props;
  3474. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  3475. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3476. props2.pNext = &props3;
  3477. props3.pNext = &subgroup_props;
  3478. subgroup_props.pNext = &driver_props;
  3479. driver_props.pNext = &vk11_props;
  3480. vk11_props.pNext = &vk12_props;
  3481. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  3482. if (maintenance4_support) {
  3483. last_struct->pNext = (VkBaseOutStructure *)&props4;
  3484. last_struct = (VkBaseOutStructure *)&props4;
  3485. }
  3486. if (sm_builtins) {
  3487. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  3488. last_struct = (VkBaseOutStructure *)&sm_props;
  3489. }
  3490. if (amd_shader_core_properties2) {
  3491. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  3492. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  3493. }
  3494. if (device->subgroup_size_control) {
  3495. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  3496. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  3497. }
  3498. #if defined(VK_NV_cooperative_matrix2)
  3499. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  3500. if (coopmat2_support) {
  3501. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  3502. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  3503. }
  3504. #endif
  3505. if (device->integer_dot_product) {
  3506. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3507. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3508. }
  3509. device->physical_device.getProperties2(&props2);
  3510. device->properties = props2.properties;
  3511. device->vendor_id = device->properties.vendorID;
  3512. device->driver_id = driver_props.driverID;
  3513. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  3514. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  3515. device->max_memory_allocation_size = std::stoull(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  3516. } else if (maintenance4_support) {
  3517. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  3518. } else {
  3519. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  3520. }
  3521. const char* GGML_VK_FORCE_MAX_BUFFER_SIZE = getenv("GGML_VK_FORCE_MAX_BUFFER_SIZE");
  3522. if (GGML_VK_FORCE_MAX_BUFFER_SIZE != nullptr) {
  3523. device->max_buffer_size = std::stoull(GGML_VK_FORCE_MAX_BUFFER_SIZE);
  3524. } else if (maintenance4_support) {
  3525. device->max_buffer_size = props4.maxBufferSize;
  3526. } else {
  3527. device->max_buffer_size = device->max_memory_allocation_size;
  3528. }
  3529. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  3530. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  3531. device->suballocation_block_size = std::stoull(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  3532. } else {
  3533. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  3534. device->suballocation_block_size = 1024*1024*1024;
  3535. }
  3536. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  3537. device->subgroup_size = subgroup_props.subgroupSize;
  3538. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3539. if (sm_builtins) {
  3540. device->shader_core_count = sm_props.shaderSMCount;
  3541. } else if (amd_shader_core_properties2) {
  3542. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  3543. } else {
  3544. device->shader_core_count = 0;
  3545. }
  3546. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  3547. device->subgroup_arithmetic = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3548. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  3549. #ifdef __APPLE__
  3550. // Workaround for subgroup arithmetic failing on MoltenVK with AMD GPUs (issue 15846)
  3551. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  3552. device->subgroup_arithmetic = false;
  3553. }
  3554. #endif
  3555. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3556. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  3557. device->subgroup_clustered = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3558. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eClustered);
  3559. device->subgroup_ballot = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  3560. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eBallot);
  3561. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  3562. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3563. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  3564. device->coopmat_support = false;
  3565. }
  3566. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  3567. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  3568. // Try to find a non-graphics compute queue and transfer-focused queues
  3569. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  3570. 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);
  3571. const float priorities[] = { 1.0f, 1.0f };
  3572. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  3573. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  3574. if (compute_queue_family_index != transfer_queue_family_index) {
  3575. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  3576. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  3577. } else if(!device->single_queue) {
  3578. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  3579. } else {
  3580. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  3581. }
  3582. vk::DeviceCreateInfo device_create_info;
  3583. std::vector<const char *> device_extensions;
  3584. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  3585. VkPhysicalDeviceFeatures2 device_features2;
  3586. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3587. device_features2.pNext = nullptr;
  3588. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  3589. VkPhysicalDeviceVulkan11Features vk11_features;
  3590. vk11_features.pNext = nullptr;
  3591. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3592. device_features2.pNext = &vk11_features;
  3593. VkPhysicalDeviceVulkan12Features vk12_features;
  3594. vk12_features.pNext = nullptr;
  3595. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3596. vk11_features.pNext = &vk12_features;
  3597. last_struct = (VkBaseOutStructure *)&vk12_features;
  3598. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  3599. pl_robustness_features.pNext = nullptr;
  3600. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  3601. pl_robustness_features.pipelineRobustness = VK_FALSE;
  3602. if (pipeline_robustness) {
  3603. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  3604. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  3605. device_extensions.push_back("VK_EXT_pipeline_robustness");
  3606. }
  3607. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  3608. subgroup_size_control_features.pNext = nullptr;
  3609. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  3610. subgroup_size_control_features.computeFullSubgroups = false;
  3611. subgroup_size_control_features.subgroupSizeControl = false;
  3612. if (device->subgroup_size_control) {
  3613. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  3614. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  3615. }
  3616. #if defined(VK_KHR_cooperative_matrix)
  3617. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3618. coopmat_features.pNext = nullptr;
  3619. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3620. coopmat_features.cooperativeMatrix = VK_FALSE;
  3621. if (device->coopmat_support) {
  3622. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3623. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3624. }
  3625. #endif
  3626. #if defined(VK_NV_cooperative_matrix2)
  3627. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  3628. coopmat2_features.pNext = nullptr;
  3629. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  3630. if (coopmat2_support) {
  3631. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  3632. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  3633. device_extensions.push_back("VK_NV_cooperative_matrix2");
  3634. }
  3635. #endif
  3636. #if defined(VK_KHR_shader_bfloat16)
  3637. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  3638. bfloat16_features.pNext = nullptr;
  3639. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  3640. if (bfloat16_support) {
  3641. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  3642. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  3643. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3644. }
  3645. #endif
  3646. VkPhysicalDeviceMaintenance4Features maint4_features {};
  3647. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  3648. if (maintenance4_support) {
  3649. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  3650. last_struct = (VkBaseOutStructure *)&maint4_features;
  3651. device_extensions.push_back("VK_KHR_maintenance4");
  3652. }
  3653. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3654. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3655. if (device->integer_dot_product) {
  3656. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3657. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3658. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  3659. }
  3660. VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR pep_features {};
  3661. pep_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR;
  3662. if (pipeline_executable_properties_support) {
  3663. last_struct->pNext = (VkBaseOutStructure *)&pep_features;
  3664. last_struct = (VkBaseOutStructure *)&pep_features;
  3665. device_extensions.push_back("VK_KHR_pipeline_executable_properties");
  3666. }
  3667. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  3668. device->pipeline_executable_properties_support = pipeline_executable_properties_support;
  3669. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  3670. #if defined(VK_KHR_shader_bfloat16)
  3671. device->bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  3672. #else
  3673. device->bf16 = false;
  3674. #endif
  3675. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  3676. device->multi_add = vk12_props.shaderRoundingModeRTEFloat16 &&
  3677. device->properties.limits.maxPushConstantsSize >= sizeof(vk_op_multi_add_push_constants) &&
  3678. getenv("GGML_VK_DISABLE_MULTI_ADD") == nullptr;
  3679. device->shader_int64 = device_features2.features.shaderInt64;
  3680. device->buffer_device_address = vk12_features.bufferDeviceAddress;
  3681. if (device->subgroup_size_control) {
  3682. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  3683. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  3684. device_extensions.push_back("VK_EXT_subgroup_size_control");
  3685. }
  3686. device->subgroup_size_control = device->subgroup_size_control &&
  3687. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  3688. subgroup_size_control_features.subgroupSizeControl;
  3689. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  3690. #if defined(VK_KHR_cooperative_matrix)
  3691. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  3692. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  3693. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  3694. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  3695. device->subgroup_max_size >= 32;
  3696. #endif
  3697. if (coopmat2_support) {
  3698. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3699. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  3700. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  3701. coopmat2_features.cooperativeMatrixReductions &&
  3702. coopmat2_features.cooperativeMatrixConversions &&
  3703. coopmat2_features.cooperativeMatrixPerElementOperations &&
  3704. coopmat2_features.cooperativeMatrixTensorAddressing &&
  3705. coopmat2_features.cooperativeMatrixBlockLoads &&
  3706. vk12_features.bufferDeviceAddress) {
  3707. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  3708. uint32_t count = 0;
  3709. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  3710. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  3711. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  3712. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  3713. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  3714. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  3715. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  3716. flexible_dimensions.resize(count, empty_prop);
  3717. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  3718. bool found_fp16_128 = false,
  3719. found_fp16_256 = false,
  3720. found_fp32_128 = false,
  3721. found_fp32_256 = false;
  3722. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  3723. // with 32x16x16 and 256 with 32x32x16.
  3724. for (auto &prop : flexible_dimensions) {
  3725. if (prop.saturatingAccumulation == VK_FALSE &&
  3726. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  3727. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3728. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3729. if (prop.workgroupInvocations == 128 &&
  3730. prop.MGranularity <= 32 &&
  3731. prop.NGranularity <= 16 &&
  3732. prop.KGranularity <= 16) {
  3733. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3734. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3735. found_fp16_128 = true;
  3736. }
  3737. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3738. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3739. found_fp32_128 = true;
  3740. }
  3741. }
  3742. if (prop.workgroupInvocations == 256 &&
  3743. prop.MGranularity <= 32 &&
  3744. prop.NGranularity <= 32 &&
  3745. prop.KGranularity <= 16) {
  3746. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  3747. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  3748. found_fp16_256 = true;
  3749. }
  3750. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3751. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  3752. found_fp32_256 = true;
  3753. }
  3754. }
  3755. }
  3756. }
  3757. if (found_fp16_128 && found_fp16_256 &&
  3758. found_fp32_128 && found_fp32_256 &&
  3759. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  3760. device->coopmat2 = true;
  3761. }
  3762. }
  3763. #endif
  3764. }
  3765. if (!vk11_features.storageBuffer16BitAccess) {
  3766. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  3767. throw std::runtime_error("Unsupported device");
  3768. }
  3769. device_extensions.push_back("VK_KHR_16bit_storage");
  3770. #ifdef GGML_VULKAN_VALIDATE
  3771. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  3772. #endif
  3773. if (device->fp16) {
  3774. device_extensions.push_back("VK_KHR_shader_float16_int8");
  3775. }
  3776. #if defined(VK_KHR_cooperative_matrix)
  3777. if (device->coopmat_support) {
  3778. // Query supported shapes
  3779. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  3780. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  3781. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  3782. uint32_t cm_props_num;
  3783. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  3784. cm_props.resize(cm_props_num);
  3785. for (auto& prop : cm_props) {
  3786. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  3787. }
  3788. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  3789. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  3790. for (auto& prop : cm_props) {
  3791. 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));
  3792. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  3793. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  3794. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3795. ) {
  3796. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  3797. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  3798. // coopmat sizes not set yet
  3799. if (device->coopmat_m == 0) {
  3800. device->coopmat_acc_f32_support = true;
  3801. device->coopmat_m = prop.MSize;
  3802. device->coopmat_n = prop.NSize;
  3803. device->coopmat_k = prop.KSize;
  3804. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3805. // Only enable if shape is identical
  3806. device->coopmat_acc_f32_support = true;
  3807. }
  3808. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3809. device->coopmat_support_16x16x16_f32acc = true;
  3810. }
  3811. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  3812. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  3813. // coopmat sizes not set yet
  3814. if (device->coopmat_m == 0) {
  3815. device->coopmat_acc_f16_support = true;
  3816. device->coopmat_m = prop.MSize;
  3817. device->coopmat_n = prop.NSize;
  3818. device->coopmat_k = prop.KSize;
  3819. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3820. // Only enable if shape is identical
  3821. device->coopmat_acc_f16_support = true;
  3822. }
  3823. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  3824. device->coopmat_support_16x16x16_f16acc = true;
  3825. }
  3826. }
  3827. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  3828. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  3829. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  3830. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  3831. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  3832. device->coopmat_int_m == 0
  3833. ) {
  3834. device->coopmat_int_support = true;
  3835. device->coopmat_int_m = prop.MSize;
  3836. device->coopmat_int_n = prop.NSize;
  3837. device->coopmat_int_k = prop.KSize;
  3838. }
  3839. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  3840. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3841. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  3842. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3843. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  3844. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  3845. ) {
  3846. // coopmat sizes not set yet
  3847. if (device->coopmat_m == 0) {
  3848. device->coopmat_bf16_support = true;
  3849. device->coopmat_m = prop.MSize;
  3850. device->coopmat_n = prop.NSize;
  3851. device->coopmat_k = prop.KSize;
  3852. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  3853. // Only enable if shape is identical
  3854. device->coopmat_bf16_support = true;
  3855. }
  3856. }
  3857. #endif
  3858. }
  3859. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  3860. // No suitable matmul mode found
  3861. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  3862. device->coopmat_support = false;
  3863. }
  3864. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  3865. device->coopmat_bf16_support = false;
  3866. }
  3867. }
  3868. if (device->coopmat_support) {
  3869. device_extensions.push_back("VK_KHR_cooperative_matrix");
  3870. }
  3871. #if defined(VK_KHR_shader_bfloat16)
  3872. if (device->coopmat_bf16_support) {
  3873. device_extensions.push_back("VK_KHR_shader_bfloat16");
  3874. }
  3875. #endif
  3876. #endif
  3877. device->name = GGML_VK_NAME + std::to_string(idx);
  3878. device_create_info = {
  3879. vk::DeviceCreateFlags(),
  3880. device_queue_create_infos,
  3881. {},
  3882. device_extensions
  3883. };
  3884. device_create_info.setPNext(&device_features2);
  3885. device->device = device->physical_device.createDevice(device_create_info);
  3886. // Queues
  3887. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  3888. // Shaders
  3889. // Disable matmul tile sizes early if performance low or not supported
  3890. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3891. switch (device->vendor_id) {
  3892. #ifndef GGML_VULKAN_RUN_TESTS
  3893. case VK_VENDOR_ID_AMD:
  3894. case VK_VENDOR_ID_INTEL:
  3895. device->mul_mat_l[i] = false;
  3896. device->mul_mat_m[i] = true;
  3897. device->mul_mat_s[i] = true;
  3898. device->mul_mat_id_l[i] = false;
  3899. device->mul_mat_id_m[i] = true;
  3900. device->mul_mat_id_s[i] = true;
  3901. break;
  3902. case VK_VENDOR_ID_APPLE:
  3903. device->mul_mat_l[i] = false;
  3904. device->mul_mat_m[i] = true;
  3905. device->mul_mat_s[i] = false;
  3906. device->mul_mat_id_l[i] = false;
  3907. device->mul_mat_id_m[i] = true;
  3908. device->mul_mat_id_s[i] = false;
  3909. break;
  3910. #endif
  3911. default:
  3912. device->mul_mat_l[i] = true;
  3913. device->mul_mat_m[i] = true;
  3914. device->mul_mat_s[i] = true;
  3915. device->mul_mat_id_l[i] = true;
  3916. device->mul_mat_id_m[i] = true;
  3917. device->mul_mat_id_s[i] = true;
  3918. break;
  3919. }
  3920. }
  3921. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3922. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3923. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3924. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3925. dsl_binding_flags.push_back({});
  3926. }
  3927. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3928. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3929. {},
  3930. dsl_binding);
  3931. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3932. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3933. ggml_vk_load_shaders(device);
  3934. if (!device->single_queue) {
  3935. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3936. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3937. } else {
  3938. // TODO: Use pointer or reference to avoid copy
  3939. device->transfer_queue.copyFrom(device->compute_queue);
  3940. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3941. }
  3942. device->buffer_type = {
  3943. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3944. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3945. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3946. };
  3947. device->fence = device->device.createFence({});
  3948. device->idx = idx;
  3949. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3950. device->add_rms_fusion = !device->disable_fusion &&
  3951. device->subgroup_arithmetic &&
  3952. device->vendor_id != VK_VENDOR_ID_INTEL;
  3953. device->partials_binding_alignment =
  3954. std::max(4u, (uint32_t)device->properties.limits.minStorageBufferOffsetAlignment);
  3955. device->mmvq_mode = 0;
  3956. if (getenv("GGML_VK_DISABLE_MMVQ")) {
  3957. device->mmvq_mode = -1;
  3958. } else if (getenv("GGML_VK_FORCE_MMVQ")) {
  3959. device->mmvq_mode = 1;
  3960. }
  3961. return device;
  3962. }
  3963. return vk_instance.devices[idx];
  3964. }
  3965. static void ggml_vk_print_gpu_info(size_t idx) {
  3966. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3967. size_t dev_num = vk_instance.device_indices[idx];
  3968. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3969. GGML_ASSERT(vk_instance_initialized);
  3970. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3971. if (dev_num >= devices.size()) {
  3972. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3973. throw std::runtime_error("Device not found");
  3974. }
  3975. vk::PhysicalDevice physical_device = devices[dev_num];
  3976. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3977. bool fp16_storage = false;
  3978. bool fp16_compute = false;
  3979. bool coopmat_support = false;
  3980. bool coopmat2_support = false;
  3981. bool integer_dot_product = false;
  3982. bool bfloat16_support = false;
  3983. for (auto properties : ext_props) {
  3984. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3985. fp16_storage = true;
  3986. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3987. fp16_compute = true;
  3988. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3989. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3990. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3991. coopmat_support = true;
  3992. #endif
  3993. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3994. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3995. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3996. coopmat2_support = true;
  3997. #endif
  3998. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3999. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  4000. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  4001. integer_dot_product = true;
  4002. #endif
  4003. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  4004. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  4005. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  4006. bfloat16_support = true;
  4007. #endif
  4008. }
  4009. }
  4010. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  4011. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  4012. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  4013. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  4014. vk::PhysicalDeviceProperties2 props2;
  4015. vk::PhysicalDeviceMaintenance3Properties props3;
  4016. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  4017. vk::PhysicalDeviceDriverProperties driver_props;
  4018. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  4019. props2.pNext = &props3;
  4020. props3.pNext = &subgroup_props;
  4021. subgroup_props.pNext = &driver_props;
  4022. // Pointer to the last chain element
  4023. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  4024. if (integer_dot_product) {
  4025. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  4026. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  4027. }
  4028. physical_device.getProperties2(&props2);
  4029. VkPhysicalDeviceFeatures2 device_features2;
  4030. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  4031. device_features2.pNext = nullptr;
  4032. VkPhysicalDeviceVulkan11Features vk11_features;
  4033. vk11_features.pNext = nullptr;
  4034. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  4035. device_features2.pNext = &vk11_features;
  4036. VkPhysicalDeviceVulkan12Features vk12_features;
  4037. vk12_features.pNext = nullptr;
  4038. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  4039. vk11_features.pNext = &vk12_features;
  4040. // Pointer to the last chain element
  4041. last_struct = (VkBaseOutStructure *)&vk12_features;
  4042. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  4043. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  4044. coopmat_features.pNext = nullptr;
  4045. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  4046. coopmat_features.cooperativeMatrix = VK_FALSE;
  4047. if (coopmat_support) {
  4048. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  4049. last_struct = (VkBaseOutStructure *)&coopmat_features;
  4050. }
  4051. #endif
  4052. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  4053. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  4054. if (integer_dot_product) {
  4055. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  4056. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  4057. }
  4058. #if defined(VK_KHR_shader_bfloat16)
  4059. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  4060. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  4061. if (bfloat16_support) {
  4062. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  4063. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  4064. }
  4065. #endif
  4066. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  4067. fp16 = fp16 && vk12_features.shaderFloat16;
  4068. #if defined(VK_KHR_shader_bfloat16)
  4069. bool bf16 = bfloat16_support && bfloat16_features.shaderBFloat16Type;
  4070. #else
  4071. bool bf16 = false;
  4072. #endif
  4073. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  4074. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  4075. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  4076. integer_dot_product = integer_dot_product
  4077. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  4078. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  4079. coopmat_support = coopmat_support
  4080. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  4081. && coopmat_features.cooperativeMatrix
  4082. #endif
  4083. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  4084. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  4085. std::string device_name = props2.properties.deviceName.data();
  4086. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | bf16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  4087. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, bf16, subgroup_size,
  4088. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  4089. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  4090. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  4091. }
  4092. }
  4093. static bool ggml_vk_instance_validation_ext_available();
  4094. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  4095. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  4096. static bool ggml_vk_device_is_supported(const vk::PhysicalDevice & vkdev);
  4097. static DispatchLoaderDynamic ggml_vk_default_dispatcher_instance;
  4098. DispatchLoaderDynamic & ggml_vk_default_dispatcher() {
  4099. return ggml_vk_default_dispatcher_instance;
  4100. }
  4101. static void ggml_vk_instance_init() {
  4102. if (vk_instance_initialized) {
  4103. return;
  4104. }
  4105. VK_LOG_DEBUG("ggml_vk_instance_init()");
  4106. // See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
  4107. ggml_vk_default_dispatcher_instance.init(vkGetInstanceProcAddr);
  4108. uint32_t api_version = vk::enumerateInstanceVersion();
  4109. if (api_version < VK_API_VERSION_1_2) {
  4110. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  4111. throw vk::SystemError(vk::Result::eErrorFeatureNotPresent, "Vulkan 1.2 required");
  4112. }
  4113. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  4114. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  4115. const bool validation_ext = ggml_vk_instance_validation_ext_available();
  4116. #ifdef __APPLE__
  4117. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  4118. #endif
  4119. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  4120. std::vector<const char*> layers;
  4121. if (validation_ext) {
  4122. layers.push_back("VK_LAYER_KHRONOS_validation");
  4123. }
  4124. std::vector<const char*> extensions;
  4125. if (validation_ext) {
  4126. extensions.push_back("VK_EXT_validation_features");
  4127. }
  4128. #ifdef __APPLE__
  4129. if (portability_enumeration_ext) {
  4130. extensions.push_back("VK_KHR_portability_enumeration");
  4131. }
  4132. #endif
  4133. if (debug_utils_ext) {
  4134. extensions.push_back("VK_EXT_debug_utils");
  4135. }
  4136. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  4137. #ifdef __APPLE__
  4138. if (portability_enumeration_ext) {
  4139. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  4140. }
  4141. #endif
  4142. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  4143. vk::ValidationFeaturesEXT validation_features;
  4144. if (validation_ext) {
  4145. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  4146. validation_features = {
  4147. features_enable,
  4148. {},
  4149. };
  4150. validation_features.setPNext(nullptr);
  4151. instance_create_info.setPNext(&validation_features);
  4152. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  4153. }
  4154. vk_instance.instance = vk::createInstance(instance_create_info);
  4155. vk_instance_initialized = true;
  4156. if (debug_utils_ext) {
  4157. vk_instance.debug_utils_support = true;
  4158. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  4159. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  4160. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  4161. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  4162. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  4163. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  4164. }
  4165. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  4166. // See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
  4167. VULKAN_HPP_DEFAULT_DISPATCHER.init(vk_instance.instance);
  4168. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  4169. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  4170. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  4171. if (devices_env != nullptr) {
  4172. size_t num_available_devices = devices.size();
  4173. std::string devices(devices_env);
  4174. std::replace(devices.begin(), devices.end(), ',', ' ');
  4175. std::stringstream ss(devices);
  4176. size_t tmp;
  4177. while (ss >> tmp) {
  4178. if(tmp >= num_available_devices) {
  4179. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  4180. throw std::runtime_error("Invalid Vulkan device index");
  4181. }
  4182. vk_instance.device_indices.push_back(tmp);
  4183. }
  4184. } else {
  4185. // If no vulkan devices are found, return early
  4186. if (devices.empty()) {
  4187. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  4188. return;
  4189. }
  4190. // Default to using all dedicated GPUs
  4191. for (size_t i = 0; i < devices.size(); i++) {
  4192. vk::PhysicalDeviceProperties2 new_props;
  4193. vk::PhysicalDeviceDriverProperties new_driver;
  4194. vk::PhysicalDeviceIDProperties new_id;
  4195. new_props.pNext = &new_driver;
  4196. new_driver.pNext = &new_id;
  4197. devices[i].getProperties2(&new_props);
  4198. if ((new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu || new_props.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu) && ggml_vk_device_is_supported(devices[i])) {
  4199. // Check if there are two physical devices corresponding to the same GPU
  4200. auto old_device = std::find_if(
  4201. vk_instance.device_indices.begin(),
  4202. vk_instance.device_indices.end(),
  4203. [&devices, &new_id](const size_t k){
  4204. vk::PhysicalDeviceProperties2 old_props;
  4205. vk::PhysicalDeviceIDProperties old_id;
  4206. old_props.pNext = &old_id;
  4207. devices[k].getProperties2(&old_props);
  4208. bool equals = std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  4209. equals = equals || (
  4210. old_id.deviceLUIDValid && new_id.deviceLUIDValid &&
  4211. std::equal(std::begin(old_id.deviceLUID), std::end(old_id.deviceLUID), std::begin(new_id.deviceLUID))
  4212. );
  4213. return equals;
  4214. }
  4215. );
  4216. if (old_device == vk_instance.device_indices.end()) {
  4217. vk_instance.device_indices.push_back(i);
  4218. } else {
  4219. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  4220. // This can cause error when splitting layers aross the devices, need to keep only 1
  4221. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  4222. vk::PhysicalDeviceProperties2 old_props;
  4223. vk::PhysicalDeviceDriverProperties old_driver;
  4224. old_props.pNext = &old_driver;
  4225. devices[*old_device].getProperties2(&old_props);
  4226. std::map<vk::DriverId, int> driver_priorities {};
  4227. int old_priority = std::numeric_limits<int>::max();
  4228. int new_priority = std::numeric_limits<int>::max();
  4229. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  4230. // Smaller number -> higher priority
  4231. switch (old_props.properties.vendorID) {
  4232. case VK_VENDOR_ID_AMD:
  4233. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  4234. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  4235. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  4236. break;
  4237. case VK_VENDOR_ID_INTEL:
  4238. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  4239. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  4240. break;
  4241. case VK_VENDOR_ID_NVIDIA:
  4242. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  4243. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  4244. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  4245. #endif
  4246. break;
  4247. }
  4248. driver_priorities[vk::DriverId::eMesaDozen] = 100;
  4249. if (driver_priorities.count(old_driver.driverID)) {
  4250. old_priority = driver_priorities[old_driver.driverID];
  4251. }
  4252. if (driver_priorities.count(new_driver.driverID)) {
  4253. new_priority = driver_priorities[new_driver.driverID];
  4254. }
  4255. if (new_priority < old_priority) {
  4256. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  4257. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  4258. vk_instance.device_indices.push_back(i);
  4259. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  4260. }
  4261. else {
  4262. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  4263. }
  4264. }
  4265. }
  4266. }
  4267. // If no GPUs found, fall back to the first non-CPU device.
  4268. // If only CPU devices are available, return without devices.
  4269. if (vk_instance.device_indices.empty()) {
  4270. for (size_t i = 0; i < devices.size(); i++) {
  4271. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  4272. vk_instance.device_indices.push_back(i);
  4273. break;
  4274. }
  4275. }
  4276. }
  4277. if (vk_instance.device_indices.empty()) {
  4278. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  4279. return;
  4280. }
  4281. }
  4282. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  4283. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  4284. vk::PhysicalDevice vkdev = devices[vk_instance.device_indices[i]];
  4285. std::vector<vk::ExtensionProperties> extensionprops = vkdev.enumerateDeviceExtensionProperties();
  4286. bool membudget_supported = false;
  4287. for (const auto & ext : extensionprops) {
  4288. if (strcmp(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, ext.extensionName) == 0) {
  4289. membudget_supported = true;
  4290. break;
  4291. }
  4292. }
  4293. vk_instance.device_supports_membudget.push_back(membudget_supported);
  4294. ggml_vk_print_gpu_info(i);
  4295. }
  4296. }
  4297. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  4298. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  4299. ggml_vk_instance_init();
  4300. GGML_ASSERT(idx < vk_instance.device_indices.size());
  4301. ctx->name = GGML_VK_NAME + std::to_string(idx);
  4302. ctx->device = ggml_vk_get_device(idx);
  4303. ctx->semaphore_idx = 0;
  4304. ctx->event_idx = 0;
  4305. ctx->prealloc_size_x = 0;
  4306. ctx->prealloc_size_y = 0;
  4307. ctx->prealloc_size_split_k = 0;
  4308. ctx->prealloc_size_add_rms_partials = 0;
  4309. ctx->fence = ctx->device->device.createFence({});
  4310. ctx->almost_ready_fence = ctx->device->device.createFence({});
  4311. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  4312. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  4313. #ifdef GGML_VULKAN_CHECK_RESULTS
  4314. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  4315. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  4316. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  4317. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  4318. #endif
  4319. }
  4320. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  4321. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  4322. switch (type) {
  4323. case GGML_TYPE_F32:
  4324. case GGML_TYPE_Q4_0:
  4325. case GGML_TYPE_Q4_1:
  4326. case GGML_TYPE_Q5_0:
  4327. case GGML_TYPE_Q5_1:
  4328. case GGML_TYPE_Q8_0:
  4329. case GGML_TYPE_Q2_K:
  4330. case GGML_TYPE_Q3_K:
  4331. case GGML_TYPE_Q4_K:
  4332. case GGML_TYPE_Q5_K:
  4333. case GGML_TYPE_Q6_K:
  4334. case GGML_TYPE_IQ1_S:
  4335. case GGML_TYPE_IQ1_M:
  4336. case GGML_TYPE_IQ2_XXS:
  4337. case GGML_TYPE_IQ2_XS:
  4338. case GGML_TYPE_IQ2_S:
  4339. case GGML_TYPE_IQ3_XXS:
  4340. case GGML_TYPE_IQ3_S:
  4341. case GGML_TYPE_IQ4_XS:
  4342. case GGML_TYPE_IQ4_NL:
  4343. case GGML_TYPE_MXFP4:
  4344. break;
  4345. default:
  4346. return nullptr;
  4347. }
  4348. return ctx->device->pipeline_dequant[type];
  4349. }
  4350. 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) {
  4351. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  4352. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4353. return ctx->device->pipeline_matmul_f32;
  4354. }
  4355. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  4356. return ctx->device->pipeline_matmul_f32_f16;
  4357. }
  4358. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  4359. return ctx->device->pipeline_matmul_bf16;
  4360. }
  4361. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  4362. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4363. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  4364. }
  4365. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4366. return ctx->device->pipeline_matmul_f16.f16acc;
  4367. }
  4368. } else {
  4369. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4370. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  4371. }
  4372. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4373. return ctx->device->pipeline_matmul_f16.f32acc;
  4374. }
  4375. }
  4376. // MMQ
  4377. if (src1_type == GGML_TYPE_Q8_1) {
  4378. vk_matmul_pipeline pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f32acc;
  4379. if (pipelines->is_empty()) {
  4380. return nullptr;
  4381. }
  4382. return pipelines;
  4383. }
  4384. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  4385. return nullptr;
  4386. }
  4387. switch (src0_type) {
  4388. case GGML_TYPE_Q4_0:
  4389. case GGML_TYPE_Q4_1:
  4390. case GGML_TYPE_Q5_0:
  4391. case GGML_TYPE_Q5_1:
  4392. case GGML_TYPE_Q8_0:
  4393. case GGML_TYPE_Q2_K:
  4394. case GGML_TYPE_Q3_K:
  4395. case GGML_TYPE_Q4_K:
  4396. case GGML_TYPE_Q5_K:
  4397. case GGML_TYPE_Q6_K:
  4398. case GGML_TYPE_IQ1_S:
  4399. case GGML_TYPE_IQ1_M:
  4400. case GGML_TYPE_IQ2_XXS:
  4401. case GGML_TYPE_IQ2_XS:
  4402. case GGML_TYPE_IQ2_S:
  4403. case GGML_TYPE_IQ3_XXS:
  4404. case GGML_TYPE_IQ3_S:
  4405. case GGML_TYPE_IQ4_XS:
  4406. case GGML_TYPE_IQ4_NL:
  4407. case GGML_TYPE_MXFP4:
  4408. break;
  4409. default:
  4410. return nullptr;
  4411. }
  4412. if (ctx->device->coopmat2) {
  4413. assert(src1_type == GGML_TYPE_F16);
  4414. 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;
  4415. }
  4416. if (ctx->device->coopmat_support) {
  4417. 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;
  4418. }
  4419. 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;
  4420. }
  4421. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type, uint32_t num_cols, uint32_t m, uint32_t k) {
  4422. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  4423. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16 || b_type == GGML_TYPE_Q8_1);
  4424. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  4425. if (b_type == GGML_TYPE_Q8_1) {
  4426. switch (a_type) {
  4427. case GGML_TYPE_Q4_0:
  4428. case GGML_TYPE_Q4_1:
  4429. case GGML_TYPE_Q5_0:
  4430. case GGML_TYPE_Q5_1:
  4431. case GGML_TYPE_Q8_0:
  4432. break;
  4433. default:
  4434. return nullptr;
  4435. }
  4436. }
  4437. switch (a_type) {
  4438. case GGML_TYPE_F32:
  4439. case GGML_TYPE_F16:
  4440. case GGML_TYPE_BF16:
  4441. case GGML_TYPE_Q4_0:
  4442. case GGML_TYPE_Q4_1:
  4443. case GGML_TYPE_Q5_0:
  4444. case GGML_TYPE_Q5_1:
  4445. case GGML_TYPE_Q8_0:
  4446. case GGML_TYPE_Q2_K:
  4447. case GGML_TYPE_Q3_K:
  4448. case GGML_TYPE_Q4_K:
  4449. case GGML_TYPE_Q5_K:
  4450. case GGML_TYPE_Q6_K:
  4451. case GGML_TYPE_IQ1_S:
  4452. case GGML_TYPE_IQ1_M:
  4453. case GGML_TYPE_IQ2_XXS:
  4454. case GGML_TYPE_IQ2_XS:
  4455. case GGML_TYPE_IQ2_S:
  4456. case GGML_TYPE_IQ3_XXS:
  4457. case GGML_TYPE_IQ3_S:
  4458. case GGML_TYPE_IQ4_XS:
  4459. case GGML_TYPE_IQ4_NL:
  4460. case GGML_TYPE_MXFP4:
  4461. break;
  4462. default:
  4463. return nullptr;
  4464. }
  4465. // heuristic to choose workgroup size
  4466. uint32_t dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  4467. if ((ctx->device->vendor_id == VK_VENDOR_ID_NVIDIA && ctx->device->architecture != vk_device_architecture::NVIDIA_PRE_TURING) || ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  4468. // Prefer larger workgroups when M is small, to spread the work out more
  4469. // and keep more SMs busy.
  4470. // q6_k seems to prefer small workgroup size even for "medium" values of M.
  4471. if (a_type == GGML_TYPE_Q6_K) {
  4472. if (m < 4096 && k >= 1024) {
  4473. dmmv_wg = DMMV_WG_SIZE_LARGE;
  4474. }
  4475. } else {
  4476. if (m <= 8192 && k >= 1024) {
  4477. dmmv_wg = DMMV_WG_SIZE_LARGE;
  4478. }
  4479. }
  4480. }
  4481. if (b_type == GGML_TYPE_Q8_1) {
  4482. if (ctx->device->vendor_id == VK_VENDOR_ID_INTEL) {
  4483. dmmv_wg = DMMV_WG_SIZE_SUBGROUP;
  4484. }
  4485. return ctx->device->pipeline_dequant_mul_mat_vec_q8_1_f32[dmmv_wg][a_type][num_cols-1];
  4486. }
  4487. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[dmmv_wg][a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[dmmv_wg][a_type][num_cols-1];
  4488. }
  4489. 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) {
  4490. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  4491. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4492. return ctx->device->pipeline_matmul_id_f32;
  4493. }
  4494. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  4495. return ctx->device->pipeline_matmul_id_bf16;
  4496. }
  4497. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  4498. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4499. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  4500. }
  4501. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4502. return ctx->device->pipeline_matmul_id_f16.f16acc;
  4503. }
  4504. } else {
  4505. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4506. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  4507. }
  4508. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4509. return ctx->device->pipeline_matmul_id_f16.f32acc;
  4510. }
  4511. }
  4512. // MMQ
  4513. if (src1_type == GGML_TYPE_Q8_1) {
  4514. vk_matmul_pipeline pipelines = ctx->device->pipeline_dequant_mul_mat_mat_id_q8_1[src0_type].f32acc;
  4515. if (pipelines->is_empty()) {
  4516. return nullptr;
  4517. }
  4518. return pipelines;
  4519. }
  4520. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  4521. switch (src0_type) {
  4522. case GGML_TYPE_Q4_0:
  4523. case GGML_TYPE_Q4_1:
  4524. case GGML_TYPE_Q5_0:
  4525. case GGML_TYPE_Q5_1:
  4526. case GGML_TYPE_Q8_0:
  4527. case GGML_TYPE_Q2_K:
  4528. case GGML_TYPE_Q3_K:
  4529. case GGML_TYPE_Q4_K:
  4530. case GGML_TYPE_Q5_K:
  4531. case GGML_TYPE_Q6_K:
  4532. case GGML_TYPE_IQ1_S:
  4533. case GGML_TYPE_IQ1_M:
  4534. case GGML_TYPE_IQ2_XXS:
  4535. case GGML_TYPE_IQ2_XS:
  4536. case GGML_TYPE_IQ2_S:
  4537. case GGML_TYPE_IQ3_XXS:
  4538. case GGML_TYPE_IQ3_S:
  4539. case GGML_TYPE_IQ4_XS:
  4540. case GGML_TYPE_IQ4_NL:
  4541. case GGML_TYPE_MXFP4:
  4542. break;
  4543. default:
  4544. return nullptr;
  4545. }
  4546. vk_matmul_pipeline2& mmp = ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type];
  4547. // XXX TODO 'prec' is not actually allowed in mul_mat_id.
  4548. bool prefer_fp16acc = ctx->device->fp16 /*&& prec == GGML_PREC_DEFAULT*/;
  4549. bool support_fp16acc = !mmp.f16acc->is_empty();
  4550. bool support_fp32acc = !mmp.f32acc->is_empty();
  4551. if (support_fp16acc && (prefer_fp16acc || !support_fp32acc)) {
  4552. return mmp.f16acc;
  4553. } else {
  4554. GGML_ASSERT(support_fp32acc);
  4555. return mmp.f32acc;
  4556. }
  4557. }
  4558. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  4559. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec_id()");
  4560. GGML_ASSERT(b_type == GGML_TYPE_F32);
  4561. switch (a_type) {
  4562. case GGML_TYPE_F32:
  4563. case GGML_TYPE_F16:
  4564. case GGML_TYPE_BF16:
  4565. case GGML_TYPE_Q4_0:
  4566. case GGML_TYPE_Q4_1:
  4567. case GGML_TYPE_Q5_0:
  4568. case GGML_TYPE_Q5_1:
  4569. case GGML_TYPE_Q8_0:
  4570. case GGML_TYPE_Q2_K:
  4571. case GGML_TYPE_Q3_K:
  4572. case GGML_TYPE_Q4_K:
  4573. case GGML_TYPE_Q5_K:
  4574. case GGML_TYPE_Q6_K:
  4575. case GGML_TYPE_IQ1_S:
  4576. case GGML_TYPE_IQ1_M:
  4577. case GGML_TYPE_IQ2_XXS:
  4578. case GGML_TYPE_IQ2_XS:
  4579. case GGML_TYPE_IQ2_S:
  4580. case GGML_TYPE_IQ3_XXS:
  4581. case GGML_TYPE_IQ3_S:
  4582. case GGML_TYPE_IQ4_XS:
  4583. case GGML_TYPE_IQ4_NL:
  4584. case GGML_TYPE_MXFP4:
  4585. break;
  4586. default:
  4587. return nullptr;
  4588. }
  4589. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  4590. }
  4591. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  4592. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  4593. vk_buffer buf = ggml_vk_create_buffer(device, size,
  4594. {vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4595. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent});
  4596. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  4597. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  4598. size/1024.0/1024.0);
  4599. device->device.freeMemory(buf->device_memory);
  4600. device->device.destroyBuffer(buf->buffer);
  4601. return nullptr;
  4602. }
  4603. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4604. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  4605. return buf->ptr;
  4606. }
  4607. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  4608. if (ptr == nullptr) {
  4609. return;
  4610. }
  4611. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  4612. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4613. vk_buffer buf;
  4614. size_t index;
  4615. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4616. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4617. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4618. if (ptr >= addr && ptr < endr) {
  4619. buf = std::get<2>(device->pinned_memory[i]);
  4620. index = i;
  4621. break;
  4622. }
  4623. }
  4624. if (buf == nullptr) {
  4625. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  4626. return;
  4627. }
  4628. ggml_vk_destroy_buffer(buf);
  4629. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  4630. }
  4631. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  4632. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  4633. buf = nullptr;
  4634. buf_offset = 0;
  4635. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  4636. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  4637. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  4638. if (ptr >= addr && ptr < endr) {
  4639. buf = std::get<2>(device->pinned_memory[i]);
  4640. buf_offset = ((const uint8_t *)ptr) - addr;
  4641. break;
  4642. }
  4643. }
  4644. }
  4645. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  4646. vk_submission s;
  4647. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  4648. if (one_time) {
  4649. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  4650. } else {
  4651. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  4652. }
  4653. return s;
  4654. }
  4655. template <typename T> size_t push_constant_size(const T &t) {
  4656. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4657. GGML_UNUSED(t);
  4658. return sizeof(T);
  4659. }
  4660. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  4661. GGML_UNUSED(t);
  4662. return sizeof(T) * t.size();
  4663. }
  4664. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  4665. GGML_UNUSED(t);
  4666. return sizeof(T) * N;
  4667. }
  4668. template <typename T> const T *push_constant_data(const T &t) {
  4669. static_assert(std::is_class<T>::value, "T must be a struct/class");
  4670. return &t;
  4671. }
  4672. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  4673. return t.data();
  4674. }
  4675. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  4676. return t.data();
  4677. }
  4678. template <typename T>
  4679. 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) {
  4680. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  4681. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  4682. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  4683. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  4684. for (auto& buffer : descriptor_buffer_infos) {
  4685. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  4686. }
  4687. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  4688. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  4689. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  4690. GGML_ASSERT(pipeline->parameter_count == descriptor_buffer_infos.size());
  4691. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  4692. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  4693. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  4694. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  4695. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  4696. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  4697. pipeline->layout,
  4698. 0,
  4699. { descriptor_set },
  4700. {});
  4701. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  4702. }
  4703. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  4704. s.buffer.end();
  4705. s.wait_semaphores = std::move(wait_semaphores);
  4706. s.signal_semaphores = std::move(signal_semaphores);
  4707. }
  4708. static void ggml_vk_ctx_end(vk_context& ctx) {
  4709. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  4710. if (ctx->s == nullptr) {
  4711. return;
  4712. }
  4713. ctx->s->buffer.end();
  4714. ctx->s = nullptr;
  4715. }
  4716. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  4717. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  4718. if (subctx->s != nullptr) {
  4719. ggml_vk_ctx_end(subctx);
  4720. }
  4721. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  4722. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  4723. }
  4724. static size_t ggml_vk_align_size(size_t width, size_t align) {
  4725. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  4726. return CEIL_DIV(width, align) * align;
  4727. }
  4728. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  4729. if (memcpys == nullptr) {
  4730. memcpy(dst, src, size);
  4731. } else {
  4732. memcpys->emplace_back(dst, src, size);
  4733. }
  4734. }
  4735. static void deferred_memset(void * dst, uint32_t val, size_t size, std::vector<vk_staging_memset>* memsets = nullptr) {
  4736. if (memsets == nullptr) {
  4737. memset(dst, val, size);
  4738. } else {
  4739. memsets->emplace_back(dst, val, size);
  4740. }
  4741. }
  4742. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  4743. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  4744. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  4745. ggml_vk_destroy_buffer(device->sync_staging);
  4746. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  4747. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4748. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4749. }
  4750. }
  4751. 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) {
  4752. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  4753. GGML_ASSERT(!ggml_is_contiguous(tensor));
  4754. // Buffer is already mapped
  4755. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4756. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4757. GGML_ABORT("fatal error");
  4758. }
  4759. // Check if src is pinned memory
  4760. vk_buffer buf = nullptr;
  4761. size_t buf_offset = 0;
  4762. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  4763. const uint64_t ne0 = tensor->ne[0];
  4764. const uint64_t ne1 = tensor->ne[1];
  4765. const uint64_t ne2 = tensor->ne[2];
  4766. const uint64_t ne3 = tensor->ne[3];
  4767. const uint64_t nb0 = tensor->nb[0];
  4768. const uint64_t nb1 = tensor->nb[1];
  4769. const uint64_t nb2 = tensor->nb[2];
  4770. const uint64_t nb3 = tensor->nb[3];
  4771. const ggml_type type = tensor->type;
  4772. const uint64_t ts = ggml_type_size(type);
  4773. const uint64_t bs = ggml_blck_size(type);
  4774. const uint64_t dstnb0 = ts;
  4775. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  4776. const uint64_t dstnb2 = dstnb1*ne1;
  4777. const uint64_t dstnb3 = dstnb2*ne2;
  4778. const uint64_t ne = ggml_nelements(tensor);
  4779. if (buf != nullptr) {
  4780. // Memory is pinned, use as staging buffer
  4781. std::vector<vk::BufferCopy> slices;
  4782. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4783. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4784. // Find longest contiguous slice
  4785. if (ne1*nb1 == dstnb2) {
  4786. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  4787. } else {
  4788. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4789. if (ne0*nb0/bs == dstnb1) {
  4790. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  4791. } else {
  4792. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4793. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4794. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4795. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  4796. }
  4797. }
  4798. }
  4799. }
  4800. }
  4801. }
  4802. ggml_vk_sync_buffers(ctx, subctx);
  4803. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4804. return;
  4805. }
  4806. if (!sync_staging) {
  4807. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4808. }
  4809. // Staging buffer required
  4810. vk_buffer& staging = ctx->device->sync_staging;
  4811. const uint64_t copy_size = ts*ne/bs;
  4812. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  4813. VkBufferCopy buf_copy{ 0, offset, copy_size };
  4814. ggml_vk_sync_buffers(ctx, subctx);
  4815. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4816. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  4817. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  4818. // Find longest contiguous slice
  4819. if (ne1*nb1 == dstnb2) {
  4820. 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);
  4821. } else {
  4822. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  4823. if (ne0*nb0/bs == dstnb1) {
  4824. 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);
  4825. } else {
  4826. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  4827. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  4828. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  4829. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  4830. }
  4831. }
  4832. }
  4833. }
  4834. }
  4835. }
  4836. }
  4837. 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) {
  4838. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  4839. // Buffer is already mapped
  4840. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4841. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  4842. GGML_ABORT("fatal error");
  4843. }
  4844. // Check if src is pinned memory
  4845. vk_buffer buf = nullptr;
  4846. size_t buf_offset = 0;
  4847. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  4848. if (buf != nullptr) {
  4849. // Memory is pinned, use as staging buffer
  4850. std::vector<vk::BufferCopy> slices(1);
  4851. if (width == spitch) {
  4852. // Only do single write if stride is equal
  4853. slices[0].srcOffset = buf_offset;
  4854. slices[0].dstOffset = offset;
  4855. slices[0].size = width * height;
  4856. } else {
  4857. slices.resize(height);
  4858. for (size_t i = 0; i < height; i++) {
  4859. slices[i].srcOffset = buf_offset + i * spitch;
  4860. slices[i].dstOffset = offset + i * width;
  4861. slices[i].size = width;
  4862. }
  4863. }
  4864. ggml_vk_sync_buffers(nullptr, subctx);
  4865. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  4866. return;
  4867. }
  4868. VK_LOG_DEBUG("STAGING");
  4869. if (!sync_staging) {
  4870. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  4871. }
  4872. // Staging buffer required
  4873. const size_t copy_size = width*height;
  4874. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  4875. vk_buffer& staging_buffer = dst->device->sync_staging;
  4876. VkBufferCopy buf_copy = {
  4877. 0,
  4878. offset,
  4879. copy_size};
  4880. ggml_vk_sync_buffers(nullptr, subctx);
  4881. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  4882. if (width == spitch) {
  4883. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  4884. } else {
  4885. for (size_t i = 0; i < height; i++) {
  4886. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  4887. }
  4888. }
  4889. }
  4890. 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) {
  4891. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  4892. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  4893. }
  4894. 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) {
  4895. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  4896. // Buffer is already mapped
  4897. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  4898. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4899. for (size_t i = 0; i < height; i++) {
  4900. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  4901. }
  4902. } else {
  4903. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4904. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4905. ggml_vk_ctx_begin(dst->device, subctx);
  4906. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  4907. ggml_vk_ctx_end(subctx);
  4908. for (auto& cpy : subctx->in_memcpys) {
  4909. memcpy(cpy.dst, cpy.src, cpy.n);
  4910. }
  4911. for (auto& mset : subctx->memsets) {
  4912. memset(mset.dst, mset.val, mset.n);
  4913. }
  4914. ggml_vk_submit(subctx, dst->device->fence);
  4915. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  4916. dst->device->device.resetFences({ dst->device->fence });
  4917. ggml_vk_queue_command_pools_cleanup(dst->device);
  4918. }
  4919. }
  4920. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  4921. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  4922. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  4923. }
  4924. 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) {
  4925. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  4926. GGML_ASSERT(width > 0);
  4927. GGML_ASSERT(height > 0);
  4928. GGML_ASSERT(src != nullptr);
  4929. // TODO: staging_offset is not used
  4930. // Check if dst is pinned memory
  4931. vk_buffer buf = nullptr;
  4932. size_t buf_offset = 0;
  4933. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  4934. std::vector<vk::BufferCopy> slices(1);
  4935. if (width == spitch && width == dpitch) {
  4936. // Only do single write if stride is equal
  4937. slices[0].srcOffset = offset;
  4938. slices[0].dstOffset = buf_offset;
  4939. slices[0].size = width * height;
  4940. } else {
  4941. slices.resize(height);
  4942. for (size_t i = 0; i < height; i++) {
  4943. slices[i].srcOffset = offset + i * spitch;
  4944. slices[i].dstOffset = buf_offset + i * dpitch;
  4945. slices[i].size = width;
  4946. }
  4947. }
  4948. if (buf != nullptr) {
  4949. // Memory is pinned, use as staging buffer
  4950. ggml_vk_sync_buffers(nullptr, subctx);
  4951. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  4952. return;
  4953. }
  4954. VK_LOG_DEBUG("STAGING");
  4955. if (!sync_staging) {
  4956. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  4957. }
  4958. // Fall back to staging buffer
  4959. const size_t copy_size = dpitch * height;
  4960. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4961. vk_buffer& staging_buffer = src->device->sync_staging;
  4962. ggml_vk_sync_buffers(nullptr, subctx);
  4963. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4964. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4965. }
  4966. 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) {
  4967. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4968. }
  4969. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4970. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4971. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4972. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4973. // the HW device to host copy path.
  4974. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4975. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4976. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4977. } else {
  4978. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4979. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4980. ggml_vk_ctx_begin(src->device, subctx);
  4981. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4982. ggml_vk_ctx_end(subctx);
  4983. ggml_vk_submit(subctx, src->device->fence);
  4984. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4985. src->device->device.resetFences({ src->device->fence });
  4986. ggml_vk_queue_command_pools_cleanup(src->device);
  4987. for (auto& cpy : subctx->out_memcpys) {
  4988. memcpy(cpy.dst, cpy.src, cpy.n);
  4989. }
  4990. }
  4991. }
  4992. 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) {
  4993. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4994. // Make sure both buffers are on same device
  4995. GGML_ASSERT(src->device == dst->device);
  4996. VkBufferCopy bc{ src_offset, dst_offset, size };
  4997. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4998. }
  4999. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  5000. if (src->device == dst->device) {
  5001. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  5002. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  5003. // Copy within the device
  5004. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  5005. ggml_vk_ctx_begin(src->device, subctx);
  5006. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  5007. ggml_vk_ctx_end(subctx);
  5008. ggml_vk_submit(subctx, src->device->fence);
  5009. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  5010. src->device->device.resetFences({ src->device->fence });
  5011. ggml_vk_queue_command_pools_cleanup(src->device);
  5012. } else {
  5013. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  5014. // Copy device to device
  5015. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  5016. // Copy to src staging buffer
  5017. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  5018. // Copy to dst buffer
  5019. ggml_vk_buffer_write_2d(dst, dst_offset, src->device->sync_staging->ptr, 0, size, 1);
  5020. }
  5021. }
  5022. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  5023. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  5024. if (dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible &&
  5025. dst->device->uma) {
  5026. deferred_memset((uint8_t*)dst->ptr + offset, c, size, &ctx->memsets);
  5027. return;
  5028. }
  5029. // Fall back to GPU fillBuffer for non-UMA or non-host-visible buffers
  5030. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  5031. }
  5032. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  5033. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  5034. if (dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible &&
  5035. dst->device->uma) {
  5036. memset((uint8_t*)dst->ptr + offset, c, size);
  5037. return;
  5038. }
  5039. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  5040. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  5041. ggml_vk_ctx_begin(dst->device, subctx);
  5042. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  5043. ggml_vk_ctx_end(subctx);
  5044. ggml_vk_submit(subctx, dst->device->fence);
  5045. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  5046. dst->device->device.resetFences({ dst->device->fence });
  5047. ggml_vk_queue_command_pools_cleanup(dst->device);
  5048. }
  5049. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, uint32_t m, uint32_t n, uint32_t k, bool disable_split_k, const vk_pipeline& pipeline) {
  5050. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ", " << disable_split_k << ")");
  5051. if (disable_split_k) {
  5052. return 1;
  5053. }
  5054. uint32_t split_k = 1;
  5055. if (ctx->device->shader_core_count != 0 && m >= pipeline->wg_denoms[0] && n >= pipeline->wg_denoms[1]) {
  5056. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  5057. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  5058. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  5059. if (k >= 2048) {
  5060. if (m_tiles * n_tiles <= ctx->device->shader_core_count / 2) {
  5061. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  5062. } else if (m_tiles * n_tiles <= ctx->device->shader_core_count * 2 / 3) {
  5063. split_k = 3;
  5064. }
  5065. // Cap the split at 8x. Unless k is huge this is a lot of overhead.
  5066. split_k = std::min(split_k, 8u);
  5067. // ggml_vk_matmul will align the splits to be a multiple of 256.
  5068. // If this rounded up size would cause the last split to be empty,
  5069. // then reduce the split count.
  5070. while (true) {
  5071. if (split_k == 1) {
  5072. break;
  5073. }
  5074. uint32_t k_split = CEIL_DIV(k, split_k);
  5075. k_split = ROUNDUP_POW2(k_split, 256);
  5076. if (k_split * (split_k - 1) < k) {
  5077. break;
  5078. }
  5079. split_k--;
  5080. }
  5081. }
  5082. }
  5083. return split_k;
  5084. }
  5085. 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) {
  5086. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  5087. if (ctx->device->coopmat2) {
  5088. const uint32_t shader_core_count = ctx->device->shader_core_count;
  5089. const uint32_t tiles_l = CEIL_DIV(m, mmp->a_l->wg_denoms[0]) * CEIL_DIV(n, mmp->a_l->wg_denoms[1]);
  5090. const uint32_t tiles_m = CEIL_DIV(m, mmp->a_m->wg_denoms[0]) * CEIL_DIV(n, mmp->a_m->wg_denoms[1]);
  5091. // Use large shader when the N dimension is greater than the medium shader's tile size
  5092. uint32_t crossover_large = mmp->m->wg_denoms[1];
  5093. // Prefer large over medium if either:
  5094. // - medium or large tiles would overfill the GPU
  5095. // - large tiles with a split_k==3 fits in the GPU and medium tiles with split_k==2 does not
  5096. // (medium with split_k==2 is probably better if it fits - more workgroups running and less split_k overhead)
  5097. bool prefer_large = tiles_m > shader_core_count || tiles_l > shader_core_count ||
  5098. // split_k==3 with large tiles likely better than medium tiles with no split_k.
  5099. (tiles_l <= shader_core_count / 3 && tiles_m > shader_core_count / 2);
  5100. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large && prefer_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  5101. return aligned ? mmp->a_l : mmp->l;
  5102. }
  5103. // Use medium shader when the N dimension is greater than the small shader's tile size
  5104. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  5105. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  5106. return aligned ? mmp->a_m : mmp->m;
  5107. }
  5108. return aligned ? mmp->a_s : mmp->s;
  5109. }
  5110. 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])) {
  5111. return aligned ? mmp->a_s : mmp->s;
  5112. }
  5113. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  5114. return aligned ? mmp->a_m : mmp->m;
  5115. }
  5116. return aligned ? mmp->a_l : mmp->l;
  5117. GGML_UNUSED(src1_type);
  5118. }
  5119. 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) {
  5120. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  5121. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  5122. }
  5123. static void ggml_vk_matmul(
  5124. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  5125. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  5126. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  5127. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  5128. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  5129. uint32_t padded_n) {
  5130. 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 << ")");
  5131. if (split_k == 1) {
  5132. 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 };
  5133. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  5134. return;
  5135. }
  5136. if (ctx->prealloc_split_k_need_sync) {
  5137. ggml_vk_sync_buffers(ctx, subctx);
  5138. }
  5139. GGML_ASSERT(batch_stride_d == m * n);
  5140. // Round the split size up to a multiple of 256 (k-quant alignment)
  5141. uint32_t k_split = CEIL_DIV(k, split_k);
  5142. k_split = ROUNDUP_POW2(k_split, 256);
  5143. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, k_split, ne02, ne12, broadcast2, broadcast3, padded_n };
  5144. // Make sure enough workgroups get assigned for split k to work
  5145. 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 });
  5146. ggml_vk_sync_buffers(ctx, subctx);
  5147. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  5148. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  5149. ctx->prealloc_split_k_need_sync = true;
  5150. }
  5151. 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) {
  5152. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  5153. if (ctx->device->coopmat2) {
  5154. // Use large shader when the N dimension is greater than the medium shader's tile size
  5155. uint32_t crossover_large = mmp->m->wg_denoms[1];
  5156. 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])) {
  5157. return aligned ? mmp->a_l : mmp->l;
  5158. }
  5159. // Use medium shader when the N dimension is greater than the small shader's tile size
  5160. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  5161. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  5162. return aligned ? mmp->a_m : mmp->m;
  5163. }
  5164. return aligned ? mmp->a_s : mmp->s;
  5165. }
  5166. 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])) {
  5167. return aligned ? mmp->a_s : mmp->s;
  5168. }
  5169. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  5170. return aligned ? mmp->a_m : mmp->m;
  5171. }
  5172. return aligned ? mmp->a_l : mmp->l;
  5173. }
  5174. 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) {
  5175. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  5176. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  5177. }
  5178. static void ggml_vk_matmul_id(
  5179. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  5180. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  5181. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  5182. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  5183. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  5184. uint32_t padded_n) {
  5185. 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 << "), " <<
  5186. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  5187. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  5188. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  5189. 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,
  5190. nei0, nei1, nbi1, ne11, padded_n };
  5191. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  5192. }
  5193. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  5194. return
  5195. tensor->nb[0] == ggml_type_size(tensor->type) &&
  5196. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  5197. (tensor->ne[3] == 1 || tensor->nb[3] == tensor->nb[2]*tensor->ne[2]);
  5198. }
  5199. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  5200. // Choose "contiguous copy" shader if src/dst are contiguous
  5201. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  5202. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  5203. if (contig) {
  5204. return ctx->device->pipeline_contig_cpy_f32_f32;
  5205. } else {
  5206. return ctx->device->pipeline_cpy_f32_f32;
  5207. }
  5208. }
  5209. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  5210. if (contig) {
  5211. return ctx->device->pipeline_contig_cpy_f32_f16;
  5212. } else {
  5213. return ctx->device->pipeline_cpy_f32_f16;
  5214. }
  5215. }
  5216. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  5217. if (contig) {
  5218. return ctx->device->pipeline_contig_cpy_f16_f16;
  5219. } else {
  5220. return ctx->device->pipeline_cpy_f16_f16;
  5221. }
  5222. }
  5223. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  5224. if (contig) {
  5225. return ctx->device->pipeline_contig_cpy_f16_f32;
  5226. } else {
  5227. return ctx->device->pipeline_cpy_f16_f32;
  5228. }
  5229. }
  5230. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  5231. if (contig) {
  5232. return ctx->device->pipeline_contig_cpy_f32_bf16;
  5233. } else {
  5234. return ctx->device->pipeline_cpy_f32_bf16;
  5235. }
  5236. }
  5237. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_I32) {
  5238. if (contig) {
  5239. return ctx->device->pipeline_contig_cpy_f32_i32;
  5240. } else {
  5241. return ctx->device->pipeline_cpy_f32_i32;
  5242. }
  5243. }
  5244. if (src->type == GGML_TYPE_I32 && to == GGML_TYPE_F32) {
  5245. if (contig) {
  5246. return ctx->device->pipeline_contig_cpy_i32_f32;
  5247. } else {
  5248. return ctx->device->pipeline_cpy_i32_f32;
  5249. }
  5250. }
  5251. if (src->type == GGML_TYPE_F32) {
  5252. switch (to) {
  5253. case GGML_TYPE_Q4_0:
  5254. case GGML_TYPE_Q4_1:
  5255. case GGML_TYPE_Q5_0:
  5256. case GGML_TYPE_Q5_1:
  5257. case GGML_TYPE_Q8_0:
  5258. case GGML_TYPE_IQ4_NL:
  5259. return ctx->device->pipeline_cpy_f32_quant[to];
  5260. default:
  5261. break;
  5262. }
  5263. }
  5264. if (to == GGML_TYPE_F32) {
  5265. switch (src->type) {
  5266. case GGML_TYPE_Q4_0:
  5267. case GGML_TYPE_Q4_1:
  5268. case GGML_TYPE_Q5_0:
  5269. case GGML_TYPE_Q5_1:
  5270. case GGML_TYPE_Q8_0:
  5271. case GGML_TYPE_IQ4_NL:
  5272. return ctx->device->pipeline_cpy_quant_f32[src->type];
  5273. default:
  5274. break;
  5275. }
  5276. }
  5277. if (src->type == to) {
  5278. // Copy two or four bytes at a time, depending on block size.
  5279. // For quantized types, we scale by block size/type size. But
  5280. // this path is also used for bf16->bf16 for example, where the
  5281. // type size must be exactly 2 or 4.
  5282. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  5283. if ((ggml_type_size(src->type) % 4) == 0) {
  5284. if (contig) {
  5285. return ctx->device->pipeline_contig_cpy_f32_f32;
  5286. } else {
  5287. return ctx->device->pipeline_cpy_f32_f32;
  5288. }
  5289. } else {
  5290. if (contig) {
  5291. return ctx->device->pipeline_contig_cpy_f16_f16;
  5292. } else {
  5293. return ctx->device->pipeline_cpy_f16_f16;
  5294. }
  5295. }
  5296. }
  5297. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  5298. GGML_ABORT("fatal error");
  5299. }
  5300. 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) {
  5301. 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] << "), ";
  5302. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  5303. const int tensor_type_size = ggml_type_size(tensor->type);
  5304. const uint32_t ne = ggml_nelements(tensor);
  5305. std::array<uint32_t, 3> elements;
  5306. if (ne > 262144) {
  5307. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  5308. } else if (ne > 512) {
  5309. elements = { 512, CEIL_DIV(ne, 512), 1 };
  5310. } else {
  5311. elements = { ne, 1, 1 };
  5312. }
  5313. vk_op_unary_push_constants pc = {
  5314. (uint32_t)ne,
  5315. (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,
  5316. (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]),
  5317. 0,
  5318. 0.0f, 0.0f,
  5319. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5320. };
  5321. init_pushconst_fastdiv(pc);
  5322. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  5323. ggml_vk_sync_buffers(ctx, subctx);
  5324. }
  5325. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type, bool use_x4_blocks) {
  5326. switch(type) {
  5327. case GGML_TYPE_Q8_1:
  5328. return use_x4_blocks ? ctx->device->pipeline_quantize_q8_1_x4 : ctx->device->pipeline_quantize_q8_1;
  5329. default:
  5330. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  5331. GGML_ABORT("fatal error");
  5332. }
  5333. }
  5334. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne, bool use_x4_blocks = false) {
  5335. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  5336. vk_pipeline pipeline = use_x4_blocks ? ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true) : ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, false);
  5337. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  5338. ggml_vk_sync_buffers(ctx, subctx);
  5339. }
  5340. static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool disable_split_k) {
  5341. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << ggml_type_name(src0->type) << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5342. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << ggml_type_name(src1->type) << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5343. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << ggml_type_name(dst->type) << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5344. std::cerr << "))");
  5345. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5346. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5347. const uint64_t ne00 = src0->ne[0];
  5348. const uint64_t ne01 = src0->ne[1];
  5349. const uint64_t ne02 = src0->ne[2];
  5350. const uint64_t ne03 = src0->ne[3];
  5351. const uint64_t ne10 = src1->ne[0];
  5352. const uint64_t ne11 = src1->ne[1];
  5353. const uint64_t ne12 = src1->ne[2];
  5354. const uint64_t ne13 = src1->ne[3];
  5355. const uint64_t ne21 = dst->ne[1];
  5356. const uint32_t stride_d = dst->nb[1] / ggml_type_size(dst->type);
  5357. const uint32_t stride_batch_d = stride_d*ne21;
  5358. const uint64_t r2 = ne12 / ne02;
  5359. const uint64_t r3 = ne13 / ne03;
  5360. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5361. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5362. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5363. vk_buffer d_Qx = nullptr;
  5364. size_t qx_buf_offset = 0;
  5365. vk_buffer d_Qy = nullptr;
  5366. size_t qy_buf_offset = 0;
  5367. bool src0_uma = false;
  5368. bool src1_uma = false;
  5369. if (ctx->device->uma) {
  5370. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5371. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5372. src0_uma = d_Qx != nullptr;
  5373. src1_uma = d_Qy != nullptr;
  5374. }
  5375. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  5376. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  5377. !ggml_vk_dim01_contiguous(src0);
  5378. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  5379. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  5380. !ggml_vk_dim01_contiguous(src1);
  5381. // If src0 is BF16, try to use a BF16 x BF16 multiply
  5382. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  5383. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  5384. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  5385. // Check for mmq first
  5386. 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;
  5387. if (mmp == nullptr) {
  5388. // Fall back to f16 dequant mul mat
  5389. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  5390. quantize_y = false;
  5391. }
  5392. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  5393. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  5394. if (qx_needs_dequant) {
  5395. // Fall back to dequant + f16 mulmat
  5396. 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]);
  5397. }
  5398. // Not implemented
  5399. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5400. 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)));
  5401. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  5402. 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));
  5403. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  5404. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  5405. const int x_ne = ne01 * ne00;
  5406. const int y_ne = padded_n * ne10;
  5407. const int d_ne = ne11 * ne01;
  5408. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, disable_split_k, pipeline);
  5409. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  5410. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5411. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  5412. 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);
  5413. const uint64_t d_sz = sizeof(float) * d_ne;
  5414. vk_pipeline to_fp16_vk_0 = nullptr;
  5415. vk_pipeline to_fp16_vk_1 = nullptr;
  5416. vk_pipeline to_q8_1 = nullptr;
  5417. if (x_non_contig) {
  5418. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  5419. } else {
  5420. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  5421. }
  5422. if (y_non_contig) {
  5423. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  5424. } else {
  5425. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5426. }
  5427. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5428. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5429. if (quantize_y) {
  5430. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  5431. }
  5432. {
  5433. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5434. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5435. if (quantize_y) {
  5436. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  5437. }
  5438. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  5439. if (
  5440. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5441. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5442. (split_k > 1 && split_k_size > ctx->device->properties.limits.maxStorageBufferRange)) {
  5443. GGML_ABORT("Requested preallocation size is too large");
  5444. }
  5445. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5446. ctx->prealloc_size_x = x_sz_upd;
  5447. ggml_vk_preallocate_buffers(ctx, subctx);
  5448. }
  5449. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  5450. ctx->prealloc_size_y = y_sz_upd;
  5451. ggml_vk_preallocate_buffers(ctx, subctx);
  5452. }
  5453. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  5454. ctx->prealloc_size_split_k = split_k_size;
  5455. ggml_vk_preallocate_buffers(ctx, subctx);
  5456. }
  5457. // Request descriptor sets
  5458. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5459. if (qx_needs_dequant) {
  5460. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5461. }
  5462. if (qy_needs_dequant) {
  5463. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5464. }
  5465. if (quantize_y) {
  5466. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  5467. }
  5468. if (split_k > 1) {
  5469. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  5470. }
  5471. }
  5472. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5473. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5474. GGML_ASSERT(d_D != nullptr);
  5475. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  5476. vk_buffer d_X;
  5477. uint64_t x_buf_offset = 0;
  5478. vk_buffer d_Y;
  5479. uint64_t y_buf_offset = 0;
  5480. if (!src0_uma) {
  5481. d_Qx = src0_buf_ctx->dev_buffer;
  5482. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5483. GGML_ASSERT(d_Qx != nullptr);
  5484. }
  5485. if (!src1_uma) {
  5486. d_Qy = src1_buf_ctx->dev_buffer;
  5487. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5488. GGML_ASSERT(d_Qy != nullptr);
  5489. }
  5490. if (qx_needs_dequant) {
  5491. d_X = ctx->prealloc_x;
  5492. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  5493. } else {
  5494. d_X = d_Qx;
  5495. x_buf_offset = qx_buf_offset;
  5496. GGML_ASSERT(qx_sz == x_sz);
  5497. }
  5498. if (qy_needs_dequant) {
  5499. d_Y = ctx->prealloc_y;
  5500. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5501. } else if (quantize_y) {
  5502. d_Y = ctx->prealloc_y;
  5503. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  5504. } else {
  5505. d_Y = d_Qy;
  5506. y_buf_offset = qy_buf_offset;
  5507. GGML_ASSERT(qy_sz == y_sz);
  5508. }
  5509. if (x_non_contig || qx_needs_dequant) {
  5510. if (ctx->prealloc_x_need_sync) {
  5511. ggml_vk_sync_buffers(ctx, subctx);
  5512. }
  5513. }
  5514. if (x_non_contig) {
  5515. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  5516. } else if (qx_needs_dequant) {
  5517. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5518. 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});
  5519. ggml_vk_sync_buffers(ctx, subctx);
  5520. }
  5521. if (y_non_contig) {
  5522. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5523. ctx->prealloc_y_last_tensor_used != src1) {
  5524. if (ctx->prealloc_y_need_sync) {
  5525. ggml_vk_sync_buffers(ctx, subctx);
  5526. }
  5527. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  5528. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5529. ctx->prealloc_y_last_tensor_used = src1;
  5530. }
  5531. }
  5532. if (quantize_y) {
  5533. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  5534. ctx->prealloc_y_last_tensor_used != src1) {
  5535. if (ctx->prealloc_y_need_sync) {
  5536. ggml_vk_sync_buffers(ctx, subctx);
  5537. }
  5538. ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0), y_ne * ne12 * ne13, true);
  5539. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  5540. ctx->prealloc_y_last_tensor_used = src1;
  5541. }
  5542. }
  5543. uint32_t stride_batch_x = ne00*ne01;
  5544. uint32_t stride_batch_y = ne10*ne11;
  5545. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5546. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5547. }
  5548. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  5549. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5550. }
  5551. uint32_t y_sz_total = y_sz * ne12 * ne13;
  5552. if (quantize_y) {
  5553. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  5554. }
  5555. // compute
  5556. ggml_vk_matmul(
  5557. ctx, subctx, pipeline,
  5558. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz_total },
  5559. ggml_vk_subbuffer(ctx, d_D, d_buf_offset), { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  5560. ne01, ne11, ne10,
  5561. ne10, ne10, stride_d, stride_batch_x, stride_batch_y, stride_batch_d,
  5562. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  5563. ); // NOLINT
  5564. if (x_non_contig || qx_needs_dequant) {
  5565. ctx->prealloc_x_need_sync = true;
  5566. }
  5567. if (y_non_contig || quantize_y) {
  5568. ctx->prealloc_y_need_sync = true;
  5569. }
  5570. }
  5571. // Device tuning
  5572. static bool ggml_vk_should_use_mmvq(const vk_device& device, uint32_t m, uint32_t n, uint32_t k, ggml_type src0_type) {
  5573. if (device->mmvq_mode == 1) {
  5574. return true;
  5575. } else if (device->mmvq_mode == -1) {
  5576. return false;
  5577. }
  5578. // MMVQ is generally good for batches
  5579. if (n > 1) {
  5580. return true;
  5581. }
  5582. switch (device->vendor_id) {
  5583. case VK_VENDOR_ID_NVIDIA:
  5584. switch (src0_type) {
  5585. case GGML_TYPE_Q8_0:
  5586. return device->architecture == vk_device_architecture::NVIDIA_PRE_TURING;
  5587. default:
  5588. return true;
  5589. }
  5590. case VK_VENDOR_ID_AMD:
  5591. switch (src0_type) {
  5592. case GGML_TYPE_Q8_0:
  5593. return device->architecture == vk_device_architecture::AMD_GCN;
  5594. default:
  5595. return true;
  5596. }
  5597. case VK_VENDOR_ID_INTEL:
  5598. switch (src0_type) {
  5599. // From tests on A770 Linux, may need more tuning
  5600. case GGML_TYPE_Q4_0:
  5601. case GGML_TYPE_Q5_1:
  5602. return false;
  5603. default:
  5604. return true;
  5605. }
  5606. default:
  5607. return true;
  5608. }
  5609. GGML_UNUSED(m);
  5610. GGML_UNUSED(k);
  5611. }
  5612. static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
  5613. ggml_tensor * dst = cgraph->nodes[node_idx];
  5614. const ggml_tensor * src0 = dst->src[0];
  5615. const ggml_tensor * src1 = dst->src[1];
  5616. 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];
  5617. 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];
  5618. 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];
  5619. std::cerr << ")),)");
  5620. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5621. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5622. const uint64_t ne00 = src0->ne[0];
  5623. const uint64_t ne01 = src0->ne[1];
  5624. const uint64_t ne02 = src0->ne[2];
  5625. const uint64_t ne03 = src0->ne[3];
  5626. const uint64_t ne10 = src1->ne[0];
  5627. const uint64_t ne11 = src1->ne[1];
  5628. const uint64_t ne12 = src1->ne[2];
  5629. const uint64_t ne13 = src1->ne[3];
  5630. const uint64_t ne20 = dst->ne[0];
  5631. const uint64_t ne21 = dst->ne[1];
  5632. const uint64_t ne22 = dst->ne[2];
  5633. const uint64_t ne23 = dst->ne[3];
  5634. const uint64_t r2 = ne12 / ne02;
  5635. const uint64_t r3 = ne13 / ne03;
  5636. // batch_n indicates that we need to compute a few vector results, and this assumes
  5637. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  5638. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  5639. bool batch_n = ne11 > 1;
  5640. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5641. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5642. vk_buffer d_Qx = nullptr;
  5643. size_t qx_buf_offset = 0;
  5644. vk_buffer d_Qy = nullptr;
  5645. size_t qy_buf_offset = 0;
  5646. bool src0_uma = false;
  5647. bool src1_uma = false;
  5648. if (ctx->device->uma) {
  5649. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5650. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5651. src0_uma = d_Qx != nullptr;
  5652. src1_uma = d_Qy != nullptr;
  5653. }
  5654. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5655. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5656. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5657. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0 && ggml_vk_should_use_mmvq(ctx->device, ne01, ne11, ne10, src0->type);
  5658. vk_pipeline to_fp16_vk_0 = nullptr;
  5659. vk_pipeline to_fp16_vk_1 = nullptr;
  5660. if (x_non_contig) {
  5661. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5662. }
  5663. if (y_non_contig) {
  5664. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5665. } else {
  5666. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5667. }
  5668. // Check for mmq first
  5669. vk_pipeline dmmv = quantize_y ? ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, GGML_TYPE_Q8_1, ne11, ne20, ne00) : nullptr;
  5670. vk_pipeline to_q8_1 = nullptr;
  5671. if (dmmv == nullptr) {
  5672. // Fall back to f16 dequant mul mat
  5673. dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11, ne20, ne00);
  5674. quantize_y = false;
  5675. }
  5676. if (quantize_y) {
  5677. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  5678. }
  5679. const bool qx_needs_dequant = x_non_contig;
  5680. const bool qy_needs_dequant = !quantize_y && ((src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig);
  5681. // Not implemented
  5682. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5683. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5684. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5685. GGML_ASSERT(dmmv != nullptr);
  5686. const uint64_t x_ne = ne01 * ne00;
  5687. const uint64_t y_ne = ne11 * ne10;
  5688. const uint64_t d_ne = ne11 * ne01;
  5689. 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);
  5690. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5691. 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;
  5692. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  5693. const uint64_t d_sz = sizeof(float) * d_ne;
  5694. {
  5695. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5696. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5697. if (quantize_y) {
  5698. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  5699. }
  5700. if (
  5701. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  5702. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange)) {
  5703. GGML_ABORT("Requested preallocation size is too large");
  5704. }
  5705. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5706. ctx->prealloc_size_x = x_sz_upd;
  5707. ggml_vk_preallocate_buffers(ctx, subctx);
  5708. }
  5709. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  5710. ctx->prealloc_size_y = y_sz_upd;
  5711. ggml_vk_preallocate_buffers(ctx, subctx);
  5712. }
  5713. // Request descriptor sets
  5714. if (qx_needs_dequant) {
  5715. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5716. }
  5717. if (qy_needs_dequant) {
  5718. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5719. }
  5720. if (quantize_y) {
  5721. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  5722. }
  5723. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5724. }
  5725. vk_buffer d_D;
  5726. uint64_t d_buf_offset = 0;
  5727. if (ctx->num_additional_fused_ops > 0) {
  5728. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  5729. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)add->buffer->context;
  5730. d_D = dst_buf_ctx->dev_buffer;
  5731. d_buf_offset = vk_tensor_offset(add) + add->view_offs;
  5732. } else {
  5733. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5734. d_D = dst_buf_ctx->dev_buffer;
  5735. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5736. }
  5737. GGML_ASSERT(d_D != nullptr);
  5738. vk_buffer d_X;
  5739. uint64_t x_buf_offset = 0;
  5740. vk_buffer d_Y;
  5741. uint64_t y_buf_offset = 0;
  5742. if(!src0_uma) {
  5743. d_Qx = src0_buf_ctx->dev_buffer;
  5744. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5745. GGML_ASSERT(d_Qx != nullptr);
  5746. }
  5747. if(!src1_uma) {
  5748. d_Qy = src1_buf_ctx->dev_buffer;
  5749. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5750. GGML_ASSERT(d_Qy != nullptr);
  5751. }
  5752. if (qx_needs_dequant) {
  5753. d_X = ctx->prealloc_x;
  5754. } else {
  5755. d_X = d_Qx;
  5756. x_buf_offset = qx_buf_offset;
  5757. GGML_ASSERT(qx_sz == x_sz);
  5758. }
  5759. if (qy_needs_dequant) {
  5760. d_Y = ctx->prealloc_y;
  5761. } else if (quantize_y) {
  5762. d_Y = ctx->prealloc_y;
  5763. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  5764. } else {
  5765. d_Y = d_Qy;
  5766. y_buf_offset = qy_buf_offset;
  5767. GGML_ASSERT(qy_sz == y_sz);
  5768. }
  5769. if (x_non_contig) {
  5770. if (ctx->prealloc_x_need_sync) {
  5771. ggml_vk_sync_buffers(ctx, subctx);
  5772. }
  5773. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5774. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  5775. }
  5776. if (y_non_contig) {
  5777. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5778. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  5779. ctx->prealloc_y_last_tensor_used != src1) {
  5780. if (ctx->prealloc_y_need_sync) {
  5781. ggml_vk_sync_buffers(ctx, subctx);
  5782. }
  5783. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  5784. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  5785. ctx->prealloc_y_last_tensor_used = src1;
  5786. }
  5787. }
  5788. if (quantize_y) {
  5789. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  5790. ctx->prealloc_y_last_tensor_used != src1) {
  5791. if (ctx->prealloc_y_need_sync) {
  5792. ggml_vk_sync_buffers(ctx, subctx);
  5793. }
  5794. ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0), y_ne * ne12 * ne13, true);
  5795. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  5796. ctx->prealloc_y_last_tensor_used = src1;
  5797. }
  5798. }
  5799. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  5800. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  5801. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  5802. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  5803. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5804. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5805. }
  5806. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5807. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5808. }
  5809. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5810. uint32_t groups_x = ne01;
  5811. uint32_t groups_z = 1;
  5812. if (ne01 > max_groups_x) {
  5813. groups_z = 64;
  5814. groups_x = CEIL_DIV(groups_x, groups_z);
  5815. }
  5816. // TODO: Clean up this whole sz * ne_2 * ne_3 thing, it hasn't been necessary for a long time
  5817. uint32_t y_sz_total = y_sz * ne12 * ne13;
  5818. if (quantize_y) {
  5819. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  5820. }
  5821. uint32_t enable_bias = ctx->num_additional_fused_ops > 0;
  5822. vk_buffer d_B = d_D;
  5823. size_t b_buf_offset = 0;
  5824. uint64_t b_sz = 0;
  5825. if (enable_bias) {
  5826. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  5827. const ggml_tensor * bias = add->src[0] == dst ? add->src[1] : add->src[0];
  5828. bool b_uma = false;
  5829. if (ctx->device->uma) {
  5830. ggml_vk_host_get(ctx->device, bias->data, d_B, b_buf_offset);
  5831. b_uma = d_B != nullptr;
  5832. }
  5833. if(!b_uma) {
  5834. ggml_backend_vk_buffer_context * bias_buf_ctx = (ggml_backend_vk_buffer_context *)bias->buffer->context;
  5835. d_B = bias_buf_ctx->dev_buffer;
  5836. b_buf_offset = vk_tensor_offset(bias) + bias->view_offs;
  5837. GGML_ASSERT(d_B != nullptr);
  5838. b_sz = ggml_nbytes(bias);
  5839. }
  5840. }
  5841. // compute
  5842. const vk_mat_vec_push_constants pc = {
  5843. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5844. stride_batch_x, stride_batch_y, stride_batch_d, enable_bias,
  5845. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  5846. };
  5847. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5848. {
  5849. vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5850. vk_subbuffer{ d_Y, y_buf_offset, y_sz_total },
  5851. vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23},
  5852. vk_subbuffer{ d_B, b_buf_offset, b_sz },
  5853. },
  5854. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  5855. if (x_non_contig) {
  5856. ctx->prealloc_x_need_sync = true;
  5857. }
  5858. if (y_non_contig || quantize_y) {
  5859. ctx->prealloc_y_need_sync = true;
  5860. }
  5861. }
  5862. static void ggml_vk_mul_mat_vec_p021_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
  5863. ggml_tensor * dst = cgraph->nodes[node_idx];
  5864. const ggml_tensor * src0 = dst->src[0];
  5865. const ggml_tensor * src1 = dst->src[1];
  5866. 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];
  5867. 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];
  5868. 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];
  5869. std::cerr << "))");
  5870. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  5871. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  5872. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  5873. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5874. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5875. const uint64_t ne00 = src0->ne[0];
  5876. const uint64_t ne01 = src0->ne[1];
  5877. const uint64_t ne02 = src0->ne[2];
  5878. // const uint64_t ne03 = src0->ne[3];
  5879. const uint64_t ne10 = src1->ne[0];
  5880. const uint64_t ne11 = src1->ne[1];
  5881. const uint64_t ne12 = src1->ne[2];
  5882. // const uint64_t ne13 = src1->ne[3];
  5883. GGML_ASSERT(ne11 == 1);
  5884. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5885. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5886. vk_buffer d_Qy = nullptr;
  5887. size_t qy_buf_offset = 0;
  5888. bool src1_uma = false;
  5889. if (ctx->device->uma) {
  5890. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5891. src1_uma = d_Qy != nullptr;
  5892. }
  5893. const uint64_t x_ne = ne00 * ne01 * ne02;
  5894. const uint64_t y_ne = ne10 * ne11 * ne12;
  5895. const uint64_t d_ne = ne01 * ne11 * ne12;
  5896. 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);
  5897. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5898. const uint64_t d_sz = sizeof(float) * d_ne;
  5899. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  5900. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  5901. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  5902. gqa_ratio = 1;
  5903. }
  5904. {
  5905. // Request descriptor sets
  5906. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  5907. }
  5908. vk_buffer d_D;
  5909. uint64_t d_buf_offset = 0;
  5910. if (ctx->num_additional_fused_ops > 0) {
  5911. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  5912. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)add->buffer->context;
  5913. d_D = dst_buf_ctx->dev_buffer;
  5914. d_buf_offset = vk_tensor_offset(add) + add->view_offs;
  5915. } else {
  5916. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5917. d_D = dst_buf_ctx->dev_buffer;
  5918. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5919. }
  5920. GGML_ASSERT(d_D != nullptr);
  5921. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  5922. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5923. GGML_ASSERT(d_Qx != nullptr);
  5924. if (!src1_uma) {
  5925. d_Qy = src1_buf_ctx->dev_buffer;
  5926. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5927. GGML_ASSERT(d_Qx != nullptr);
  5928. }
  5929. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5930. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  5931. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5932. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  5933. uint32_t enable_bias = ctx->num_additional_fused_ops > 0;
  5934. vk_buffer d_B = d_D;
  5935. size_t b_buf_offset = 0;
  5936. uint64_t b_sz = 0;
  5937. if (enable_bias) {
  5938. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  5939. const ggml_tensor * bias = add->src[0] == dst ? add->src[1] : add->src[0];
  5940. bool b_uma = false;
  5941. if (ctx->device->uma) {
  5942. ggml_vk_host_get(ctx->device, bias->data, d_B, b_buf_offset);
  5943. b_uma = d_B != nullptr;
  5944. }
  5945. if(!b_uma) {
  5946. ggml_backend_vk_buffer_context * bias_buf_ctx = (ggml_backend_vk_buffer_context *)bias->buffer->context;
  5947. d_B = bias_buf_ctx->dev_buffer;
  5948. b_buf_offset = vk_tensor_offset(bias) + bias->view_offs;
  5949. GGML_ASSERT(d_B != nullptr);
  5950. b_sz = ggml_nbytes(bias);
  5951. }
  5952. }
  5953. // compute
  5954. const std::array<uint32_t, 7> 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)), enable_bias };
  5955. uint32_t workgroups_z = (uint32_t)ne12;
  5956. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  5957. if (gqa_ratio > 1) {
  5958. workgroups_z /= gqa_ratio;
  5959. }
  5960. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1],
  5961. {
  5962. vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz },
  5963. vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset },
  5964. vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset },
  5965. vk_subbuffer{ d_B, b_buf_offset, b_sz },
  5966. }, pc, { 1, (uint32_t)ne01, workgroups_z });
  5967. }
  5968. static void ggml_vk_mul_mat_vec_nc_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
  5969. ggml_tensor * dst = cgraph->nodes[node_idx];
  5970. const ggml_tensor * src0 = dst->src[0];
  5971. const ggml_tensor * src1 = dst->src[1];
  5972. 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];
  5973. 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];
  5974. 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];
  5975. std::cerr << "))");
  5976. GGML_ASSERT(!ggml_is_transposed(src0));
  5977. GGML_ASSERT(!ggml_is_transposed(src1));
  5978. GGML_ASSERT(!ggml_is_permuted(src0));
  5979. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5980. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5981. const uint64_t ne00 = src0->ne[0];
  5982. const uint64_t ne01 = src0->ne[1];
  5983. const uint64_t ne02 = src0->ne[2];
  5984. const uint64_t ne03 = src0->ne[3];
  5985. const uint64_t nb01 = src0->nb[1];
  5986. const uint64_t nb02 = src0->nb[2];
  5987. const uint64_t nb12 = src1->nb[2];
  5988. // const uint64_t ne10 = src1->ne[0];
  5989. const uint64_t ne11 = src1->ne[1];
  5990. const uint64_t ne12 = src1->ne[2];
  5991. // const uint64_t ne13 = src1->ne[3];
  5992. const uint32_t nb03 = (uint32_t)(src0->nb[3] / sizeof(ggml_fp16_t));
  5993. const uint32_t nb13 = (uint32_t)(src1->nb[3] / sizeof(float));
  5994. const uint32_t nb23 = (uint32_t)(dst->nb[3] / sizeof(float));
  5995. GGML_ASSERT(ne11 == 1);
  5996. GGML_ASSERT(src0->ne[3] == src1->ne[3]); // checked in supports_op
  5997. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5998. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5999. vk_buffer d_Qy = nullptr;
  6000. size_t qy_buf_offset = 0;
  6001. bool src1_uma = false;
  6002. if (ctx->device->uma) {
  6003. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  6004. src1_uma = d_Qy != nullptr;
  6005. }
  6006. const uint64_t d_ne = ne01 * ne11 * ne12 * ne03;
  6007. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  6008. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  6009. const uint32_t channel_stride_y = nb12 / sizeof(float);
  6010. const uint64_t qx_sz = ggml_nbytes(src0);
  6011. const uint64_t qy_sz = ggml_nbytes(src1);
  6012. const uint64_t d_sz = sizeof(float) * d_ne;
  6013. {
  6014. // Request descriptor sets
  6015. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  6016. }
  6017. vk_buffer d_D;
  6018. uint64_t d_buf_offset = 0;
  6019. if (ctx->num_additional_fused_ops > 0) {
  6020. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  6021. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)add->buffer->context;
  6022. d_D = dst_buf_ctx->dev_buffer;
  6023. d_buf_offset = vk_tensor_offset(add) + add->view_offs;
  6024. } else {
  6025. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6026. d_D = dst_buf_ctx->dev_buffer;
  6027. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6028. }
  6029. GGML_ASSERT(d_D != nullptr);
  6030. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  6031. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6032. GGML_ASSERT(d_Qx != nullptr);
  6033. if (!src1_uma) {
  6034. d_Qy = src1_buf_ctx->dev_buffer;
  6035. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6036. GGML_ASSERT(d_Qx != nullptr);
  6037. }
  6038. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  6039. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  6040. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  6041. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  6042. uint32_t enable_bias = ctx->num_additional_fused_ops > 0;
  6043. vk_buffer d_B = d_D;
  6044. size_t b_buf_offset = 0;
  6045. uint64_t b_sz = 0;
  6046. if (enable_bias) {
  6047. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  6048. const ggml_tensor * bias = add->src[0] == dst ? add->src[1] : add->src[0];
  6049. bool b_uma = false;
  6050. if (ctx->device->uma) {
  6051. ggml_vk_host_get(ctx->device, bias->data, d_B, b_buf_offset);
  6052. b_uma = d_B != nullptr;
  6053. }
  6054. if(!b_uma) {
  6055. ggml_backend_vk_buffer_context * bias_buf_ctx = (ggml_backend_vk_buffer_context *)bias->buffer->context;
  6056. d_B = bias_buf_ctx->dev_buffer;
  6057. b_buf_offset = vk_tensor_offset(bias) + bias->view_offs;
  6058. GGML_ASSERT(d_B != nullptr);
  6059. b_sz = ggml_nbytes(bias);
  6060. }
  6061. }
  6062. // compute
  6063. const std::array<uint32_t, 13> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, channel_stride_y, (uint32_t)(ne12 / ne02), (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)), nb03, nb13, nb23, enable_bias };
  6064. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  6065. {
  6066. vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz },
  6067. vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset },
  6068. vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset },
  6069. vk_subbuffer{ d_B, b_buf_offset, b_sz },
  6070. }, pc, { (uint32_t)ne03, (uint32_t)ne01, (uint32_t)ne12 });
  6071. }
  6072. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
  6073. ggml_tensor * dst = cgraph->nodes[node_idx];
  6074. ggml_tensor * src0 = dst->src[0];
  6075. ggml_tensor * src1 = dst->src[1];
  6076. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  6077. // Handle huge A matrix by splitting the M dimensions. This works well for convolution use cases
  6078. // where the M dimension is very large.
  6079. // Split_k doesn't work with M splitting.
  6080. const size_t nbytes = ggml_nbytes(src0);
  6081. const bool needs_split = nbytes > ctx->device->properties.limits.maxStorageBufferRange;
  6082. if (needs_split) {
  6083. // Choose the number of rows that can fit (and divide by two, to allow for any additional offsets)
  6084. const uint32_t M_split = ctx->device->properties.limits.maxStorageBufferRange / (2 * src0->nb[1]);
  6085. uint32_t m_offset = 0;
  6086. while (m_offset < dst->ne[0]) {
  6087. const uint32_t cur_M_size = std::min(M_split, (uint32_t)(dst->ne[0] - m_offset));
  6088. ggml_tensor dst2 = *dst;
  6089. ggml_tensor src02 = *src0;
  6090. dst2.view_src = dst->view_src ? dst->view_src : dst;
  6091. src02.view_src = src0->view_src ? src0->view_src : src0;
  6092. dst2.view_offs += m_offset * dst->nb[0];
  6093. src02.view_offs += m_offset * src0->nb[1];
  6094. dst2.ne[0] = cur_M_size;
  6095. src02.ne[1] = cur_M_size;
  6096. ggml_vk_mul_mat_q_f16(ctx, subctx, &src02, src1, &dst2, true);
  6097. m_offset += cur_M_size;
  6098. }
  6099. } else if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  6100. // detect 0213 permutation, and batch size of 1
  6101. src0->nb[0] <= src0->nb[2] &&
  6102. src0->nb[2] <= src0->nb[1] &&
  6103. src0->nb[1] <= src0->nb[3] &&
  6104. src1->nb[0] <= src1->nb[2] &&
  6105. src1->nb[2] <= src1->nb[1] &&
  6106. src1->nb[1] <= src1->nb[3] &&
  6107. src0->ne[3] == 1 &&
  6108. src1->ne[3] == 1) {
  6109. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, cgraph, node_idx);
  6110. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  6111. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  6112. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, cgraph, node_idx);
  6113. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  6114. // when ne12 and ne13 are one.
  6115. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  6116. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  6117. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, cgraph, node_idx);
  6118. } else {
  6119. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, false);
  6120. }
  6121. }
  6122. 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) {
  6123. 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];
  6124. 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];
  6125. 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];
  6126. 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] << "),)");
  6127. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  6128. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  6129. const uint64_t ne00 = src0->ne[0];
  6130. const uint64_t ne01 = src0->ne[1];
  6131. const uint64_t ne02 = src0->ne[2];
  6132. const uint64_t ne03 = src0->ne[3];
  6133. const uint64_t ne10 = src1->ne[0];
  6134. const uint64_t ne11 = src1->ne[1];
  6135. const uint64_t ne12 = src1->ne[2];
  6136. const uint64_t ne13 = src1->ne[3];
  6137. const uint64_t nei0 = ids->ne[0];
  6138. const uint64_t nei1 = ids->ne[1];
  6139. const uint32_t nbi1 = ids->nb[1];
  6140. const uint32_t nbi2 = ids->nb[2];
  6141. const uint64_t ne20 = dst->ne[0];
  6142. const uint64_t ne21 = dst->ne[1];
  6143. const uint64_t ne22 = dst->ne[2];
  6144. const uint64_t ne23 = dst->ne[3];
  6145. const uint64_t n_as = ne02;
  6146. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6147. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  6148. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  6149. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  6150. vk_buffer d_Qx = nullptr;
  6151. size_t qx_buf_offset = 0;
  6152. vk_buffer d_Qy = nullptr;
  6153. size_t qy_buf_offset = 0;
  6154. vk_buffer d_ids = nullptr;
  6155. size_t ids_buf_offset = 0;
  6156. bool src0_uma = false;
  6157. bool src1_uma = false;
  6158. bool ids_uma = false;
  6159. if (ctx->device->uma) {
  6160. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  6161. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  6162. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  6163. src0_uma = d_Qx != nullptr;
  6164. src1_uma = d_Qy != nullptr;
  6165. ids_uma = d_ids != nullptr;
  6166. }
  6167. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  6168. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  6169. !ggml_vk_dim01_contiguous(src0);
  6170. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  6171. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  6172. !ggml_vk_dim01_contiguous(src1);
  6173. // If src0 is BF16, try to use a BF16 x BF16 multiply
  6174. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  6175. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  6176. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  6177. // Check for mmq first
  6178. vk_matmul_pipeline mmp = quantize_y ? ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, GGML_TYPE_Q8_1, (ggml_prec)dst->op_params[0]) : nullptr;
  6179. if (mmp == nullptr) {
  6180. // Fall back to f16 dequant mul mat
  6181. 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]);
  6182. quantize_y = false;
  6183. }
  6184. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  6185. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  6186. if (qx_needs_dequant) {
  6187. // Fall back to dequant + f16 mulmat
  6188. 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]);
  6189. }
  6190. // Not implemented
  6191. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  6192. const uint32_t kpad = quantize_y ? 0 : ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? f16_type : src0->type));
  6193. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && nei1 > 8;
  6194. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  6195. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  6196. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  6197. const uint64_t x_ne = ne01 * ne00;
  6198. const uint64_t y_ne = padded_n * ne10;
  6199. const uint64_t d_ne = ne21 * ne20;
  6200. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  6201. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  6202. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  6203. 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);
  6204. const uint64_t ids_sz = nbi2;
  6205. const uint64_t d_sz = sizeof(float) * d_ne;
  6206. vk_pipeline to_fp16_vk_0 = nullptr;
  6207. vk_pipeline to_fp16_vk_1 = nullptr;
  6208. vk_pipeline to_q8_1 = nullptr;
  6209. if (x_non_contig) {
  6210. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  6211. } else {
  6212. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  6213. }
  6214. if (y_non_contig) {
  6215. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  6216. } else {
  6217. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  6218. }
  6219. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  6220. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  6221. if (quantize_y) {
  6222. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1, true);
  6223. }
  6224. {
  6225. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  6226. uint64_t y_sz_upd = y_sz * ne12 * ne13;
  6227. if (quantize_y) {
  6228. y_sz_upd = CEIL_DIV(y_sz_upd, 144) * 144;
  6229. }
  6230. if (
  6231. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  6232. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange)) {
  6233. GGML_ABORT("Requested preallocation size is too large");
  6234. }
  6235. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  6236. ctx->prealloc_size_x = x_sz_upd;
  6237. ggml_vk_preallocate_buffers(ctx, subctx);
  6238. }
  6239. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  6240. ctx->prealloc_size_y = y_sz_upd;
  6241. ggml_vk_preallocate_buffers(ctx, subctx);
  6242. }
  6243. // Request descriptor sets
  6244. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6245. if (qx_needs_dequant) {
  6246. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  6247. }
  6248. if (qy_needs_dequant) {
  6249. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  6250. }
  6251. if (quantize_y) {
  6252. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  6253. }
  6254. }
  6255. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6256. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6257. GGML_ASSERT(d_D != nullptr);
  6258. vk_buffer d_X;
  6259. uint64_t x_buf_offset = 0;
  6260. vk_buffer d_Y;
  6261. uint64_t y_buf_offset = 0;
  6262. if (!src0_uma) {
  6263. d_Qx = src0_buf_ctx->dev_buffer;
  6264. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6265. GGML_ASSERT(d_Qx != nullptr);
  6266. }
  6267. if (!src1_uma) {
  6268. d_Qy = src1_buf_ctx->dev_buffer;
  6269. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6270. GGML_ASSERT(d_Qy != nullptr);
  6271. }
  6272. if (!ids_uma) {
  6273. d_ids = ids_buf_ctx->dev_buffer;
  6274. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  6275. GGML_ASSERT(d_ids != nullptr);
  6276. }
  6277. if (qx_needs_dequant) {
  6278. d_X = ctx->prealloc_x;
  6279. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  6280. } else {
  6281. d_X = d_Qx;
  6282. x_buf_offset = qx_buf_offset;
  6283. GGML_ASSERT(qx_sz == x_sz);
  6284. }
  6285. if (qy_needs_dequant) {
  6286. d_Y = ctx->prealloc_y;
  6287. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  6288. } else if (quantize_y) {
  6289. d_Y = ctx->prealloc_y;
  6290. GGML_ASSERT(d_Y->size >= CEIL_DIV(y_sz * ne12 * ne13, 144) * 144);
  6291. } else {
  6292. d_Y = d_Qy;
  6293. y_buf_offset = qy_buf_offset;
  6294. GGML_ASSERT(qy_sz == y_sz);
  6295. }
  6296. if (x_non_contig || qx_needs_dequant) {
  6297. if (ctx->prealloc_x_need_sync) {
  6298. ggml_vk_sync_buffers(ctx, subctx);
  6299. }
  6300. }
  6301. if (x_non_contig) {
  6302. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  6303. } else if (qx_needs_dequant) {
  6304. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  6305. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  6306. { 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});
  6307. ggml_vk_sync_buffers(ctx, subctx);
  6308. }
  6309. if (y_non_contig) {
  6310. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  6311. ctx->prealloc_y_last_tensor_used != src1) {
  6312. if (ctx->prealloc_y_need_sync) {
  6313. ggml_vk_sync_buffers(ctx, subctx);
  6314. }
  6315. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  6316. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  6317. ctx->prealloc_y_last_tensor_used = src1;
  6318. }
  6319. }
  6320. if (quantize_y) {
  6321. if (ctx->prealloc_y_last_pipeline_used != to_q8_1.get() ||
  6322. ctx->prealloc_y_last_tensor_used != src1) {
  6323. if (ctx->prealloc_y_need_sync) {
  6324. ggml_vk_sync_buffers(ctx, subctx);
  6325. }
  6326. ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0), y_ne * ne12 * ne13, true);
  6327. ctx->prealloc_y_last_pipeline_used = to_q8_1.get();
  6328. ctx->prealloc_y_last_tensor_used = src1;
  6329. }
  6330. }
  6331. uint32_t stride_batch_x = ne00*ne01;
  6332. uint32_t stride_batch_y = ne10*ne11;
  6333. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  6334. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  6335. }
  6336. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  6337. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  6338. }
  6339. uint32_t y_sz_total = y_sz * ne12 * ne13;
  6340. if (quantize_y) {
  6341. y_sz_total = CEIL_DIV(y_sz_total, 144) * 144;
  6342. }
  6343. // compute
  6344. ggml_vk_matmul_id(
  6345. ctx, subctx, pipeline,
  6346. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz_total },
  6347. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  6348. ne01, ne21, ne10, ne10, ne10, ne01,
  6349. stride_batch_x, stride_batch_y, ne20*ne21,
  6350. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  6351. ); // NOLINT
  6352. if (x_non_contig || qx_needs_dequant) {
  6353. ctx->prealloc_x_need_sync = true;
  6354. }
  6355. if (y_non_contig) {
  6356. ctx->prealloc_y_need_sync = true;
  6357. }
  6358. }
  6359. static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
  6360. ggml_tensor * dst = cgraph->nodes[node_idx];
  6361. ggml_tensor * src0 = dst->src[0];
  6362. ggml_tensor * src1 = dst->src[1];
  6363. ggml_tensor * ids = dst->src[2];
  6364. 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];
  6365. 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];
  6366. 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];
  6367. 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];
  6368. std::cerr << "))");
  6369. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  6370. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  6371. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  6372. const uint64_t ne00 = src0->ne[0];
  6373. const uint64_t ne01 = src0->ne[1];
  6374. const uint64_t ne02 = src0->ne[2];
  6375. const uint64_t ne03 = src0->ne[3];
  6376. const uint64_t ne10 = src1->ne[0];
  6377. const uint64_t ne11 = src1->ne[1];
  6378. const uint64_t ne12 = src1->ne[2];
  6379. const uint64_t ne13 = src1->ne[3];
  6380. const uint64_t nei0 = ids->ne[0];
  6381. const uint64_t nei1 = ids->ne[1];
  6382. const uint64_t nbi2 = ids->nb[2];
  6383. GGML_ASSERT(nei1 == 1);
  6384. const uint64_t ne20 = dst->ne[0];
  6385. const uint64_t ne21 = dst->ne[1];
  6386. const uint64_t ne22 = dst->ne[2];
  6387. const uint64_t ne23 = dst->ne[3];
  6388. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  6389. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  6390. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  6391. vk_buffer d_Qx = nullptr;
  6392. size_t qx_buf_offset = 0;
  6393. vk_buffer d_Qy = nullptr;
  6394. size_t qy_buf_offset = 0;
  6395. vk_buffer d_ids = nullptr;
  6396. size_t ids_buf_offset = 0;
  6397. bool src0_uma = false;
  6398. bool src1_uma = false;
  6399. bool ids_uma = false;
  6400. if (ctx->device->uma) {
  6401. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  6402. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  6403. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  6404. src0_uma = d_Qx != nullptr;
  6405. src1_uma = d_Qy != nullptr;
  6406. ids_uma = d_ids != nullptr;
  6407. }
  6408. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  6409. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  6410. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  6411. const bool qx_needs_dequant = x_non_contig;
  6412. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  6413. // Not implemented
  6414. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  6415. const uint64_t x_ne = ne01 * ne00;
  6416. const uint64_t y_ne = ne11 * ne10;
  6417. const uint64_t d_ne = ne21 * ne20;
  6418. 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);
  6419. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  6420. 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;
  6421. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  6422. const uint64_t ids_sz = nbi2;
  6423. const uint64_t d_sz = sizeof(float) * d_ne;
  6424. vk_pipeline to_fp16_vk_0 = nullptr;
  6425. vk_pipeline to_fp16_vk_1 = nullptr;
  6426. if (x_non_contig) {
  6427. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  6428. }
  6429. if (y_non_contig) {
  6430. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  6431. } else {
  6432. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  6433. }
  6434. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  6435. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  6436. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  6437. GGML_ASSERT(dmmv != nullptr);
  6438. {
  6439. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  6440. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  6441. if (
  6442. (qx_needs_dequant && x_sz_upd > ctx->device->properties.limits.maxStorageBufferRange) ||
  6443. (qy_needs_dequant && y_sz_upd > ctx->device->properties.limits.maxStorageBufferRange)) {
  6444. GGML_ABORT("Requested preallocation size is too large");
  6445. }
  6446. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  6447. ctx->prealloc_size_x = x_sz_upd;
  6448. ggml_vk_preallocate_buffers(ctx, subctx);
  6449. }
  6450. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  6451. ctx->prealloc_size_y = y_sz_upd;
  6452. ggml_vk_preallocate_buffers(ctx, subctx);
  6453. }
  6454. // Request descriptor sets
  6455. if (qx_needs_dequant) {
  6456. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  6457. }
  6458. if (qy_needs_dequant) {
  6459. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  6460. }
  6461. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  6462. }
  6463. vk_buffer d_D;
  6464. uint64_t d_buf_offset = 0;
  6465. if (ctx->num_additional_fused_ops > 0) {
  6466. const ggml_tensor * add = cgraph->nodes[node_idx + 1];
  6467. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)add->buffer->context;
  6468. d_D = dst_buf_ctx->dev_buffer;
  6469. d_buf_offset = vk_tensor_offset(add) + add->view_offs;
  6470. } else {
  6471. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6472. d_D = dst_buf_ctx->dev_buffer;
  6473. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6474. }
  6475. GGML_ASSERT(d_D != nullptr);
  6476. vk_buffer d_X;
  6477. uint64_t x_buf_offset = 0;
  6478. vk_buffer d_Y;
  6479. uint64_t y_buf_offset = 0;
  6480. if(!src0_uma) {
  6481. d_Qx = src0_buf_ctx->dev_buffer;
  6482. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6483. GGML_ASSERT(d_Qx != nullptr);
  6484. }
  6485. if(!src1_uma) {
  6486. d_Qy = src1_buf_ctx->dev_buffer;
  6487. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6488. GGML_ASSERT(d_Qy != nullptr);
  6489. }
  6490. if(!ids_uma) {
  6491. d_ids = ids_buf_ctx->dev_buffer;
  6492. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  6493. GGML_ASSERT(d_ids != nullptr);
  6494. }
  6495. if (qx_needs_dequant) {
  6496. d_X = ctx->prealloc_x;
  6497. } else {
  6498. d_X = d_Qx;
  6499. x_buf_offset = qx_buf_offset;
  6500. GGML_ASSERT(qx_sz == x_sz);
  6501. }
  6502. if (qy_needs_dequant) {
  6503. d_Y = ctx->prealloc_y;
  6504. } else {
  6505. d_Y = d_Qy;
  6506. y_buf_offset = qy_buf_offset;
  6507. GGML_ASSERT(qy_sz == y_sz);
  6508. }
  6509. if (x_non_contig) {
  6510. if (ctx->prealloc_x_need_sync) {
  6511. ggml_vk_sync_buffers(ctx, subctx);
  6512. }
  6513. }
  6514. if (x_non_contig) {
  6515. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  6516. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, ggml_vk_subbuffer(ctx, d_Qx, qx_buf_offset), ggml_vk_subbuffer(ctx, d_X, 0));
  6517. }
  6518. if (y_non_contig) {
  6519. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  6520. if (ctx->prealloc_y_last_pipeline_used != to_fp16_vk_1.get() ||
  6521. ctx->prealloc_y_last_tensor_used != src1) {
  6522. if (ctx->prealloc_y_need_sync) {
  6523. ggml_vk_sync_buffers(ctx, subctx);
  6524. }
  6525. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, ggml_vk_subbuffer(ctx, d_Qy, qy_buf_offset), ggml_vk_subbuffer(ctx, d_Y, 0));
  6526. ctx->prealloc_y_last_pipeline_used = to_fp16_vk_1.get();
  6527. ctx->prealloc_y_last_tensor_used = src1;
  6528. }
  6529. }
  6530. uint32_t stride_batch_y = ne10*ne11;
  6531. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  6532. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  6533. }
  6534. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  6535. uint32_t groups_x = ne01;
  6536. uint32_t groups_z = 1;
  6537. if (ne01 > max_groups_x) {
  6538. groups_z = 64;
  6539. groups_x = CEIL_DIV(groups_x, groups_z);
  6540. }
  6541. uint32_t enable_bias = ctx->num_additional_fused_ops > 0;
  6542. vk_buffer d_B = d_D;
  6543. size_t b_buf_offset = 0;
  6544. uint64_t b_sz = 0;
  6545. if (enable_bias) {
  6546. const ggml_tensor * bias = cgraph->nodes[node_idx + 1]->src[1];
  6547. bool b_uma = false;
  6548. if (ctx->device->uma) {
  6549. ggml_vk_host_get(ctx->device, bias->data, d_B, b_buf_offset);
  6550. b_uma = d_B != nullptr;
  6551. }
  6552. if(!b_uma) {
  6553. ggml_backend_vk_buffer_context * bias_buf_ctx = (ggml_backend_vk_buffer_context *)bias->buffer->context;
  6554. d_B = bias_buf_ctx->dev_buffer;
  6555. b_buf_offset = vk_tensor_offset(bias) + bias->view_offs;
  6556. GGML_ASSERT(d_B != nullptr);
  6557. b_sz = ggml_nbytes(bias);
  6558. }
  6559. }
  6560. // compute
  6561. const vk_mat_vec_id_push_constants pc = {
  6562. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  6563. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  6564. enable_bias,
  6565. (uint32_t)nei0, (uint32_t)ne11,
  6566. };
  6567. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  6568. {
  6569. vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  6570. vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  6571. vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23},
  6572. vk_subbuffer{ d_B, b_buf_offset, b_sz },
  6573. vk_subbuffer{ d_ids, ids_buf_offset, ids_sz },
  6574. },
  6575. pc, { groups_x, (uint32_t)nei0, groups_z });
  6576. if (x_non_contig) {
  6577. ctx->prealloc_x_need_sync = true;
  6578. }
  6579. if (y_non_contig) {
  6580. ctx->prealloc_y_need_sync = true;
  6581. }
  6582. }
  6583. static bool ggml_vk_use_mul_mat_vec_id(const struct ggml_cgraph * cgraph, int node_idx) {
  6584. ggml_tensor * dst = cgraph->nodes[node_idx];
  6585. ggml_tensor * src0 = dst->src[0];
  6586. ggml_tensor * src2 = dst->src[2];
  6587. return src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type));
  6588. }
  6589. static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
  6590. ggml_tensor * dst = cgraph->nodes[node_idx];
  6591. ggml_tensor * src0 = dst->src[0];
  6592. ggml_tensor * src1 = dst->src[1];
  6593. ggml_tensor * src2 = dst->src[2];
  6594. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  6595. if (ggml_vk_use_mul_mat_vec_id(cgraph, node_idx)) {
  6596. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, cgraph, node_idx);
  6597. } else {
  6598. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst);
  6599. }
  6600. }
  6601. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  6602. // Needs to be kept up to date on shader changes
  6603. GGML_UNUSED(hsv);
  6604. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  6605. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  6606. const uint32_t Bc = scalar_flash_attention_Bc;
  6607. const uint32_t tmpsh = wg_size * sizeof(float);
  6608. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  6609. const uint32_t masksh = Bc * Br * sizeof(float);
  6610. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  6611. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  6612. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  6613. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  6614. return supported;
  6615. }
  6616. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  6617. // Needs to be kept up to date on shader changes
  6618. GGML_UNUSED(hsv);
  6619. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  6620. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  6621. const uint32_t Bc = scalar_flash_attention_Bc;
  6622. const uint32_t hsk_pad = ROUNDUP_POW2(hsk, 16);
  6623. const uint32_t acctype = f32acc ? 4 : 2;
  6624. const uint32_t f16vec4 = 8;
  6625. const uint32_t tmpsh = wg_size * sizeof(float);
  6626. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  6627. const uint32_t qstride = hsk_pad / 4 + 2;
  6628. const uint32_t Qf = Br * qstride * f16vec4;
  6629. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  6630. const uint32_t sfsh = Bc * sfshstride * acctype;
  6631. const uint32_t kshstride = hsk_pad / 4 + 2;
  6632. const uint32_t ksh = Bc * kshstride * f16vec4;
  6633. const uint32_t slope = Br * sizeof(float);
  6634. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  6635. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  6636. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  6637. return supported;
  6638. }
  6639. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, const ggml_tensor * sinks, ggml_tensor * dst) {
  6640. 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];
  6641. 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];
  6642. 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];
  6643. 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];
  6644. if (sinks) {
  6645. std::cerr << "), (" << sinks << ", name=" << sinks->name << ", type=" << sinks->type << ", ne0=" << sinks->ne[0] << ", ne1=" << sinks->ne[1] << ", ne2=" << sinks->ne[2] << ", ne3=" << sinks->ne[3] << ", nb0=" << sinks->nb[0] << ", nb1=" << sinks->nb[1] << ", nb2=" << sinks->nb[2] << ", nb3=" << sinks->nb[3];
  6646. }
  6647. std::cerr << "))");
  6648. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  6649. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  6650. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  6651. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  6652. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  6653. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  6654. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  6655. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  6656. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  6657. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  6658. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  6659. const uint32_t HSK = nek0;
  6660. const uint32_t HSV = nev0;
  6661. uint32_t N = neq1;
  6662. const uint32_t KV = nek1;
  6663. GGML_ASSERT(ne0 == HSV);
  6664. GGML_ASSERT(ne2 == N);
  6665. // input tensor rows must be contiguous
  6666. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  6667. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  6668. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  6669. GGML_ASSERT(neq0 == HSK);
  6670. GGML_ASSERT(neq1 == N);
  6671. GGML_ASSERT(nev1 == nek1);
  6672. // dst cannot be transposed or permuted
  6673. GGML_ASSERT(nb0 == sizeof(float));
  6674. GGML_ASSERT(nb0 <= nb1);
  6675. GGML_ASSERT(nb1 <= nb2);
  6676. GGML_ASSERT(nb2 <= nb3);
  6677. assert(dst->type == GGML_TYPE_F32);
  6678. assert(q->type == GGML_TYPE_F32);
  6679. assert(k->type == v->type);
  6680. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  6681. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  6682. if (path == FA_COOPMAT1) {
  6683. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  6684. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  6685. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  6686. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  6687. path = FA_SCALAR;
  6688. }
  6689. }
  6690. uint32_t gqa_ratio = 1;
  6691. uint32_t qk_ratio = neq2 / nek2;
  6692. uint32_t workgroups_x = (uint32_t)neq1;
  6693. uint32_t workgroups_y = (uint32_t)neq2;
  6694. uint32_t workgroups_z = (uint32_t)neq3;
  6695. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  6696. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  6697. uint32_t max_gqa;
  6698. switch (path) {
  6699. case FA_SCALAR:
  6700. case FA_COOPMAT1:
  6701. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  6702. max_gqa = get_fa_scalar_num_large_rows(HSV);
  6703. break;
  6704. case FA_COOPMAT2:
  6705. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  6706. break;
  6707. default:
  6708. GGML_ASSERT(0);
  6709. }
  6710. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  6711. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  6712. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  6713. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  6714. // and change addressing calculations to index Q's dimension 2.
  6715. gqa_ratio = qk_ratio;
  6716. N = gqa_ratio;
  6717. workgroups_y /= N;
  6718. }
  6719. bool small_rows = N <= get_fa_num_small_rows(path);
  6720. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  6721. // So use scalar instead.
  6722. if (small_rows && path == FA_COOPMAT1) {
  6723. path = FA_SCALAR;
  6724. }
  6725. // scalar is faster than coopmat2 when N==1
  6726. if (N == 1 && path == FA_COOPMAT2) {
  6727. path = FA_SCALAR;
  6728. }
  6729. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  6730. if (path == FA_SCALAR &&
  6731. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  6732. small_rows = true;
  6733. }
  6734. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  6735. uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  6736. uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  6737. // For F32, the shader treats it as a block of size 4 (for vec4 loads)
  6738. if (k->type == GGML_TYPE_F32) {
  6739. k_stride /= 4;
  6740. }
  6741. if (v->type == GGML_TYPE_F32) {
  6742. v_stride /= 4;
  6743. }
  6744. uint32_t alignment = fa_align(path, HSK, HSV, k->type, small_rows);
  6745. bool aligned = (KV % alignment) == 0 &&
  6746. // the "aligned" shader variant will forcibly align strides, for performance
  6747. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  6748. // Need to use the coopmat2 variant that clamps loads when HSK/HSV aren't sufficiently aligned.
  6749. if (((HSK | HSV) % 16) != 0 && path == FA_COOPMAT2) {
  6750. aligned = false;
  6751. }
  6752. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  6753. vk_fa_pipeline_state fa_pipeline_state(HSK, HSV, small_rows, path, aligned, f32acc);
  6754. vk_pipeline pipeline = nullptr;
  6755. auto &pipelines = ctx->device->pipeline_flash_attn_f32_f16[k->type];
  6756. auto it = pipelines.find(fa_pipeline_state);
  6757. if (it != pipelines.end()) {
  6758. pipeline = it->second;
  6759. } else {
  6760. pipelines[fa_pipeline_state] = pipeline = std::make_shared<vk_pipeline_struct>();
  6761. }
  6762. assert(pipeline);
  6763. uint32_t split_kv = KV;
  6764. uint32_t split_k = 1;
  6765. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  6766. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  6767. // Try to use split_k when KV is large enough to be worth the overhead
  6768. if (workgroups_x == 1 && shader_core_count > 0) {
  6769. // Try to run two workgroups per SM.
  6770. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  6771. if (split_k > 1) {
  6772. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  6773. // of "align", so recompute split_k based on that.
  6774. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), alignment);
  6775. split_k = CEIL_DIV(KV, split_kv);
  6776. workgroups_x = split_k;
  6777. }
  6778. }
  6779. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  6780. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  6781. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  6782. if (split_k_size > ctx->device->properties.limits.maxStorageBufferRange) {
  6783. GGML_ABORT("Requested preallocation size is too large");
  6784. }
  6785. if (ctx->prealloc_size_split_k < split_k_size) {
  6786. ctx->prealloc_size_split_k = split_k_size;
  6787. ggml_vk_preallocate_buffers(ctx, subctx);
  6788. }
  6789. {
  6790. // Request descriptor sets
  6791. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6792. if (split_k > 1) {
  6793. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  6794. }
  6795. }
  6796. float scale = 1.0f;
  6797. float max_bias = 0.0f;
  6798. float logit_softcap = 0.0f;
  6799. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  6800. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  6801. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  6802. if (logit_softcap != 0) {
  6803. scale /= logit_softcap;
  6804. }
  6805. const uint32_t n_head_kv = neq2;
  6806. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6807. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6808. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6809. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr, d_S = nullptr;
  6810. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0, s_buf_offset = 0;
  6811. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false, S_uma = false;
  6812. if (ctx->device->uma) {
  6813. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  6814. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  6815. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  6816. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  6817. Q_uma = d_Q != nullptr;
  6818. K_uma = d_K != nullptr;
  6819. V_uma = d_V != nullptr;
  6820. D_uma = d_D != nullptr;
  6821. if (mask) {
  6822. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  6823. M_uma = d_M != nullptr;
  6824. }
  6825. if (sinks) {
  6826. ggml_vk_host_get(ctx->device, sinks->data, d_S, s_buf_offset);
  6827. S_uma = d_S != nullptr;
  6828. }
  6829. }
  6830. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6831. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  6832. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  6833. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  6834. if (!Q_uma) {
  6835. d_Q = q_buf_ctx->dev_buffer;
  6836. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  6837. }
  6838. if (!K_uma) {
  6839. d_K = k_buf_ctx->dev_buffer;
  6840. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  6841. }
  6842. if (!V_uma) {
  6843. d_V = v_buf_ctx->dev_buffer;
  6844. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  6845. }
  6846. if (!D_uma) {
  6847. d_D = d_buf_ctx->dev_buffer;
  6848. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6849. }
  6850. if (!M_uma) {
  6851. d_M = d_Q;
  6852. m_buf_offset = q_buf_offset;
  6853. if (mask) {
  6854. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  6855. d_M = m_buf_ctx->dev_buffer;
  6856. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  6857. }
  6858. }
  6859. if (!S_uma) {
  6860. d_S = d_Q;
  6861. s_buf_offset = q_buf_offset;
  6862. if (sinks) {
  6863. ggml_backend_vk_buffer_context * s_buf_ctx = (ggml_backend_vk_buffer_context*)sinks->buffer->context;
  6864. d_S = s_buf_ctx->dev_buffer;
  6865. s_buf_offset = vk_tensor_offset(sinks) + sinks->view_offs;
  6866. }
  6867. }
  6868. uint32_t mask_n_head_log2 = ((sinks != nullptr) << 24) | ((mask != nullptr) << 16) | n_head_log2;
  6869. const vk_flash_attn_push_constants pc = { N, KV,
  6870. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  6871. (uint32_t)neq2, (uint32_t)neq3,
  6872. (uint32_t)nek2, (uint32_t)nek3,
  6873. (uint32_t)nev2, (uint32_t)nev3,
  6874. nem1, nem2, nem3,
  6875. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  6876. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  6877. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  6878. scale, max_bias, logit_softcap,
  6879. mask_n_head_log2, m0, m1,
  6880. gqa_ratio, split_kv, split_k };
  6881. if (split_k > 1) {
  6882. if (ctx->prealloc_split_k_need_sync) {
  6883. ggml_vk_sync_buffers(ctx, subctx);
  6884. }
  6885. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6886. {
  6887. ggml_vk_subbuffer(ctx, d_Q, q_buf_offset),
  6888. ggml_vk_subbuffer(ctx, d_K, k_buf_offset),
  6889. ggml_vk_subbuffer(ctx, d_V, v_buf_offset),
  6890. ggml_vk_subbuffer(ctx, d_M, m_buf_offset),
  6891. ggml_vk_subbuffer(ctx, d_S, s_buf_offset),
  6892. ggml_vk_subbuffer(ctx, ctx->prealloc_split_k, 0),
  6893. },
  6894. // We only use split_k when group query attention is enabled, which means
  6895. // there's no more than one tile of rows (i.e. workgroups_x would have been
  6896. // one). We reuse workgroups_x to mean the number of splits, so we need to
  6897. // cancel out the divide by wg_denoms[0].
  6898. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  6899. ggml_vk_sync_buffers(ctx, subctx);
  6900. const std::array<uint32_t, 5> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k, (sinks != nullptr) };
  6901. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  6902. {
  6903. ggml_vk_subbuffer(ctx, ctx->prealloc_split_k, 0),
  6904. ggml_vk_subbuffer(ctx, d_S, s_buf_offset),
  6905. ggml_vk_subbuffer(ctx, d_D, d_buf_offset),
  6906. },
  6907. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  6908. ctx->prealloc_split_k_need_sync = true;
  6909. } else {
  6910. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  6911. {
  6912. ggml_vk_subbuffer(ctx, d_Q, q_buf_offset),
  6913. ggml_vk_subbuffer(ctx, d_K, k_buf_offset),
  6914. ggml_vk_subbuffer(ctx, d_V, v_buf_offset),
  6915. ggml_vk_subbuffer(ctx, d_M, m_buf_offset),
  6916. ggml_vk_subbuffer(ctx, d_S, s_buf_offset),
  6917. ggml_vk_subbuffer(ctx, d_D, d_buf_offset),
  6918. },
  6919. pc, { workgroups_x, workgroups_y, workgroups_z });
  6920. }
  6921. }
  6922. static std::array<uint32_t, 3> ggml_vk_get_conv_elements(const ggml_tensor *dst) {
  6923. const ggml_tensor *src0 = dst->src[0];
  6924. const ggml_tensor *src1 = dst->src[1];
  6925. // src0 - kernel: [KW, KH, Cin, Cout]
  6926. // src1 - input: [W, H, Cin, N]
  6927. // dst - result: [OW, OH, Cout, N]
  6928. // Copied from ggml.c: int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d)
  6929. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  6930. return (ins + 2 * p - d * (ks - 1) - 1) / s + 1;
  6931. };
  6932. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  6933. int64_t W = src1->ne[0];
  6934. int64_t H = src1->ne[1];
  6935. int64_t KW = src0->ne[0];
  6936. int64_t KH = src0->ne[1];
  6937. int64_t Cout = src0->ne[3];
  6938. int64_t N = src1->ne[3];
  6939. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[1], dst->op_params[3], dst->op_params[5]);
  6940. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], dst->op_params[2], dst->op_params[4]);
  6941. int64_t NPQ = N * OW * OH;
  6942. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  6943. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  6944. return elements;
  6945. }
  6946. static std::array<uint32_t, 3> ggml_vk_get_conv_transpose_2d_elements(const ggml_tensor *dst) {
  6947. const ggml_tensor *src0 = dst->src[0];
  6948. const ggml_tensor *src1 = dst->src[1];
  6949. // src0 - kernel: [KW, KH, Cout, Cin]
  6950. // src1 - input: [W, H, Cin, N]
  6951. // dst - result: [OW, OH, Cout, N]
  6952. auto calc_conv_output_size = [](int64_t ins, int64_t ks, int s, int p, int d) -> int64_t {
  6953. return (ins - 1) * s - 2 * p + (ks - 1) * d + 1;
  6954. };
  6955. // parallelize in {OW/BS_K, OH/BS_NPQ, 1}
  6956. int64_t W = src1->ne[0];
  6957. int64_t H = src1->ne[1];
  6958. int64_t KW = src0->ne[0];
  6959. int64_t KH = src0->ne[1];
  6960. int64_t Cout = src0->ne[2];
  6961. int64_t N = src1->ne[3];
  6962. int64_t OH = calc_conv_output_size(H, KH, dst->op_params[0], 0, 1);
  6963. int64_t OW = calc_conv_output_size(W, KW, dst->op_params[0], 0, 1);
  6964. int64_t NPQ = N * OW * OH;
  6965. // Tile output matrix to (K/NB_K, NPQ/NB_NPQ, 1) workgroups
  6966. std::array<uint32_t, 3> elements = { static_cast<uint32_t>(Cout), static_cast<uint32_t>(NPQ), 1 };
  6967. return elements;
  6968. }
  6969. static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, const ggml_tensor * dst, ggml_op op) {
  6970. switch (op) {
  6971. case GGML_OP_GET_ROWS:
  6972. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6973. if (dst->type == GGML_TYPE_F16) {
  6974. return ctx->device->pipeline_get_rows[src0->type];
  6975. }
  6976. if (dst->type == GGML_TYPE_F32) {
  6977. return ctx->device->pipeline_get_rows_f32[src0->type];
  6978. }
  6979. return nullptr;
  6980. case GGML_OP_ACC:
  6981. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6982. return ctx->device->pipeline_acc_f32;
  6983. }
  6984. return nullptr;
  6985. case GGML_OP_ADD:
  6986. case GGML_OP_SUB:
  6987. case GGML_OP_MUL:
  6988. case GGML_OP_DIV:
  6989. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  6990. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  6991. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  6992. return nullptr;
  6993. }
  6994. switch (op) {
  6995. case GGML_OP_ADD:
  6996. {
  6997. if (ctx->num_additional_fused_ops > 0) {
  6998. if (ctx->do_add_rms_partials) {
  6999. return ctx->device->pipeline_multi_add_rms[ctx->num_additional_fused_ops];
  7000. } else {
  7001. return ctx->device->pipeline_multi_add[ctx->num_additional_fused_ops];
  7002. }
  7003. }
  7004. if (ctx->do_add_rms_partials) {
  7005. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_rms_norepeat : ctx->device->pipeline_add_rms;
  7006. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  7007. } else {
  7008. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  7009. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  7010. }
  7011. }
  7012. case GGML_OP_SUB:
  7013. {
  7014. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  7015. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  7016. }
  7017. case GGML_OP_MUL:
  7018. {
  7019. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  7020. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  7021. }
  7022. case GGML_OP_DIV:
  7023. {
  7024. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  7025. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  7026. }
  7027. default:
  7028. break;
  7029. }
  7030. return nullptr;
  7031. case GGML_OP_ADD_ID:
  7032. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && src2->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32) {
  7033. return ctx->device->pipeline_add_id_f32;
  7034. }
  7035. return nullptr;
  7036. case GGML_OP_CONCAT:
  7037. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7038. return ctx->device->pipeline_concat_f32;
  7039. }
  7040. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7041. return ctx->device->pipeline_concat_f16;
  7042. }
  7043. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  7044. return ctx->device->pipeline_concat_i32;
  7045. }
  7046. return nullptr;
  7047. case GGML_OP_UPSCALE:
  7048. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7049. ggml_scale_mode mode = (ggml_scale_mode)(ggml_get_op_params_i32(dst, 0) & 0xFF);
  7050. switch (mode) {
  7051. case GGML_SCALE_MODE_NEAREST:
  7052. return ctx->device->pipeline_upscale_nearest_f32;
  7053. case GGML_SCALE_MODE_BILINEAR:
  7054. return ctx->device->pipeline_upscale_bilinear_f32;
  7055. default:
  7056. return nullptr;
  7057. }
  7058. }
  7059. return nullptr;
  7060. case GGML_OP_SCALE:
  7061. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7062. return ctx->device->pipeline_scale_f32;
  7063. }
  7064. return nullptr;
  7065. case GGML_OP_SQR:
  7066. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7067. return ctx->device->pipeline_sqr_f32;
  7068. }
  7069. return nullptr;
  7070. case GGML_OP_SQRT:
  7071. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7072. return ctx->device->pipeline_sqrt_f32;
  7073. }
  7074. return nullptr;
  7075. case GGML_OP_SIN:
  7076. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7077. return ctx->device->pipeline_sin_f32;
  7078. }
  7079. return nullptr;
  7080. case GGML_OP_COS:
  7081. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7082. return ctx->device->pipeline_cos_f32;
  7083. }
  7084. return nullptr;
  7085. case GGML_OP_CLAMP:
  7086. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7087. return ctx->device->pipeline_clamp_f32;
  7088. }
  7089. return nullptr;
  7090. case GGML_OP_PAD:
  7091. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7092. return ctx->device->pipeline_pad_f32;
  7093. }
  7094. return nullptr;
  7095. case GGML_OP_ROLL:
  7096. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7097. return ctx->device->pipeline_roll_f32;
  7098. }
  7099. return nullptr;
  7100. case GGML_OP_REPEAT:
  7101. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  7102. return ctx->device->pipeline_repeat_f32;
  7103. }
  7104. return nullptr;
  7105. case GGML_OP_REPEAT_BACK:
  7106. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7107. return ctx->device->pipeline_repeat_back_f32;
  7108. }
  7109. return nullptr;
  7110. case GGML_OP_CPY:
  7111. case GGML_OP_CONT:
  7112. case GGML_OP_DUP:
  7113. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  7114. case GGML_OP_SET_ROWS:
  7115. if (src1->type == GGML_TYPE_I64) {
  7116. return ctx->device->pipeline_set_rows_i64[dst->type];
  7117. } else {
  7118. return ctx->device->pipeline_set_rows_i32[dst->type];
  7119. }
  7120. case GGML_OP_SILU_BACK:
  7121. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7122. return ctx->device->pipeline_silu_back_f32;
  7123. }
  7124. return nullptr;
  7125. case GGML_OP_NORM:
  7126. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7127. return ctx->device->pipeline_norm_f32;
  7128. }
  7129. return nullptr;
  7130. case GGML_OP_GROUP_NORM:
  7131. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7132. return ctx->device->pipeline_group_norm_f32;
  7133. }
  7134. return nullptr;
  7135. case GGML_OP_RMS_NORM:
  7136. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7137. if (ctx->do_add_rms_partials) {
  7138. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_partials_f32 : ctx->device->pipeline_rms_norm_partials_f32;
  7139. } else {
  7140. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  7141. }
  7142. }
  7143. return nullptr;
  7144. case GGML_OP_RMS_NORM_BACK:
  7145. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7146. return ctx->device->pipeline_rms_norm_back_f32;
  7147. }
  7148. return nullptr;
  7149. case GGML_OP_L2_NORM:
  7150. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7151. return ctx->device->pipeline_l2_norm_f32;
  7152. }
  7153. return nullptr;
  7154. case GGML_OP_UNARY:
  7155. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  7156. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  7157. (src0->type != dst->type)) {
  7158. return nullptr;
  7159. }
  7160. switch (ggml_get_unary_op(dst)) {
  7161. case GGML_UNARY_OP_EXP:
  7162. return ctx->device->pipeline_exp[dst->type == GGML_TYPE_F16];
  7163. case GGML_UNARY_OP_SILU:
  7164. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  7165. case GGML_UNARY_OP_GELU:
  7166. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  7167. case GGML_UNARY_OP_GELU_ERF:
  7168. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  7169. case GGML_UNARY_OP_GELU_QUICK:
  7170. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  7171. case GGML_UNARY_OP_RELU:
  7172. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  7173. case GGML_UNARY_OP_TANH:
  7174. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  7175. case GGML_UNARY_OP_SIGMOID:
  7176. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  7177. case GGML_UNARY_OP_HARDSIGMOID:
  7178. return ctx->device->pipeline_hardsigmoid[dst->type == GGML_TYPE_F16];
  7179. case GGML_UNARY_OP_HARDSWISH:
  7180. return ctx->device->pipeline_hardswish[dst->type == GGML_TYPE_F16];
  7181. default:
  7182. break;
  7183. }
  7184. return nullptr;
  7185. case GGML_OP_GLU:
  7186. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  7187. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  7188. (src0->type != dst->type)) {
  7189. return nullptr;
  7190. }
  7191. switch (ggml_get_glu_op(dst)) {
  7192. case GGML_GLU_OP_GEGLU:
  7193. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  7194. case GGML_GLU_OP_REGLU:
  7195. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  7196. case GGML_GLU_OP_SWIGLU:
  7197. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  7198. case GGML_GLU_OP_SWIGLU_OAI:
  7199. return ctx->device->pipeline_swiglu_oai[dst->type == GGML_TYPE_F16];
  7200. case GGML_GLU_OP_GEGLU_ERF:
  7201. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  7202. case GGML_GLU_OP_GEGLU_QUICK:
  7203. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  7204. default:
  7205. break;
  7206. }
  7207. return nullptr;
  7208. case GGML_OP_DIAG_MASK_INF:
  7209. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7210. return ctx->device->pipeline_diag_mask_inf_f32;
  7211. }
  7212. return nullptr;
  7213. case GGML_OP_SOFT_MAX:
  7214. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  7215. GGML_ASSERT(!src2 || src2->type == GGML_TYPE_F32);
  7216. if (ctx->num_additional_fused_ops) {
  7217. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  7218. GGML_ASSERT(idx < num_topk_moe_pipelines);
  7219. topk_moe_mode mode = ggml_vk_num_additional_ops_to_topk_moe_mode(ctx->num_additional_fused_ops);
  7220. return ctx->device->pipeline_topk_moe[idx][mode];
  7221. }
  7222. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  7223. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  7224. }
  7225. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  7226. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  7227. }
  7228. return nullptr;
  7229. case GGML_OP_SOFT_MAX_BACK:
  7230. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7231. return ctx->device->pipeline_soft_max_back_f32;
  7232. }
  7233. return nullptr;
  7234. case GGML_OP_ROPE:
  7235. case GGML_OP_ROPE_BACK:
  7236. {
  7237. const ggml_tensor *rope = ctx->num_additional_fused_ops == 2 ? dst->src[0]->src[0] : dst;
  7238. const int mode = ((const int32_t *) rope->op_params)[2];
  7239. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7240. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  7241. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7242. if (is_neox) {
  7243. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7244. return ctx->device->pipeline_rope_neox_f32;
  7245. }
  7246. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  7247. return ctx->device->pipeline_rope_neox_f32_f16;
  7248. }
  7249. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7250. return ctx->device->pipeline_rope_neox_f16;
  7251. }
  7252. } else if (is_mrope && !is_vision) {
  7253. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7254. return ctx->device->pipeline_rope_multi_f32;
  7255. }
  7256. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7257. return ctx->device->pipeline_rope_multi_f16;
  7258. }
  7259. } else if (is_vision) {
  7260. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7261. return ctx->device->pipeline_rope_vision_f32;
  7262. }
  7263. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7264. return ctx->device->pipeline_rope_vision_f16;
  7265. }
  7266. } else {
  7267. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7268. return ctx->device->pipeline_rope_norm_f32;
  7269. }
  7270. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  7271. return ctx->device->pipeline_rope_norm_f32_f16;
  7272. }
  7273. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  7274. return ctx->device->pipeline_rope_norm_f16;
  7275. }
  7276. }
  7277. return nullptr;
  7278. }
  7279. case GGML_OP_ARGSORT:
  7280. if (ctx->num_additional_fused_ops) {
  7281. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  7282. GGML_ASSERT(idx < num_topk_moe_pipelines);
  7283. topk_moe_mode mode = ggml_vk_num_additional_ops_to_topk_moe_mode(ctx->num_additional_fused_ops);
  7284. return ctx->device->pipeline_topk_moe[idx][mode];
  7285. }
  7286. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  7287. uint32_t idx = (uint32_t)ceilf(log2f(float(dst->ne[0])));
  7288. return ctx->device->pipeline_argsort_f32[idx];
  7289. }
  7290. return nullptr;
  7291. case GGML_OP_SUM:
  7292. case GGML_OP_SUM_ROWS:
  7293. case GGML_OP_MEAN:
  7294. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7295. return ctx->device->pipeline_sum_rows_f32;
  7296. }
  7297. return nullptr;
  7298. case GGML_OP_ARGMAX:
  7299. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  7300. return ctx->device->pipeline_argmax_f32;
  7301. }
  7302. return nullptr;
  7303. case GGML_OP_COUNT_EQUAL:
  7304. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  7305. return ctx->device->pipeline_count_equal_i32;
  7306. }
  7307. return nullptr;
  7308. case GGML_OP_IM2COL:
  7309. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7310. return ctx->device->pipeline_im2col_f32;
  7311. }
  7312. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  7313. return ctx->device->pipeline_im2col_f32_f16;
  7314. }
  7315. return nullptr;
  7316. case GGML_OP_IM2COL_3D:
  7317. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7318. return ctx->device->pipeline_im2col_3d_f32;
  7319. }
  7320. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  7321. return ctx->device->pipeline_im2col_3d_f32_f16;
  7322. }
  7323. return nullptr;
  7324. case GGML_OP_TIMESTEP_EMBEDDING:
  7325. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7326. return ctx->device->pipeline_timestep_embedding_f32;
  7327. }
  7328. return nullptr;
  7329. case GGML_OP_CONV_TRANSPOSE_1D:
  7330. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7331. return ctx->device->pipeline_conv_transpose_1d_f32;
  7332. }
  7333. return nullptr;
  7334. case GGML_OP_POOL_2D:
  7335. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7336. return ctx->device->pipeline_pool2d_f32;
  7337. }
  7338. return nullptr;
  7339. case GGML_OP_RWKV_WKV6:
  7340. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7341. return ctx->device->pipeline_rwkv_wkv6_f32;
  7342. }
  7343. return nullptr;
  7344. case GGML_OP_RWKV_WKV7:
  7345. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7346. return ctx->device->pipeline_rwkv_wkv7_f32;
  7347. }
  7348. return nullptr;
  7349. case GGML_OP_SSM_SCAN:
  7350. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7351. const uint32_t d_state = src0->ne[0];
  7352. if (d_state == 128) {
  7353. return ctx->device->pipeline_ssm_scan_f32_d128;
  7354. } else if (d_state == 256) {
  7355. return ctx->device->pipeline_ssm_scan_f32_d256;
  7356. }
  7357. }
  7358. return nullptr;
  7359. case GGML_OP_SSM_CONV:
  7360. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7361. return ctx->device->pipeline_ssm_conv_f32;
  7362. }
  7363. return nullptr;
  7364. case GGML_OP_OPT_STEP_ADAMW:
  7365. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7366. return ctx->device->pipeline_opt_step_adamw_f32;
  7367. }
  7368. return nullptr;
  7369. case GGML_OP_OPT_STEP_SGD:
  7370. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7371. return ctx->device->pipeline_opt_step_sgd_f32;
  7372. }
  7373. return nullptr;
  7374. case GGML_OP_LEAKY_RELU:
  7375. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7376. return ctx->device->pipeline_leaky_relu_f32;
  7377. }
  7378. return nullptr;
  7379. case GGML_OP_CONV_2D:
  7380. case GGML_OP_CONV_TRANSPOSE_2D:
  7381. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
  7382. ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
  7383. std::array<uint32_t, 3> elements;
  7384. if (op == GGML_OP_CONV_2D) elements = ggml_vk_get_conv_elements(dst);
  7385. else if (op == GGML_OP_CONV_TRANSPOSE_2D) elements = ggml_vk_get_conv_transpose_2d_elements(dst);
  7386. vk_conv_shapes shape;
  7387. uint32_t tiles[CONV_SHAPE_COUNT];
  7388. for (uint32_t i = 0; i < CONV_SHAPE_COUNT; ++i) {
  7389. tiles[i] = CEIL_DIV(elements[0], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[0]) * CEIL_DIV(elements[1], ctx->device->pipeline_conv2d_f32[i]->wg_denoms[1]);
  7390. }
  7391. // We can't query number of shader cores on Intel, use 32 as a placeholder
  7392. // so small convolutions will still choose a smaller tile.
  7393. const uint32_t shader_core_count = ctx->device->shader_core_count > 0 ? ctx->device->shader_core_count : 32;
  7394. if (elements[0] > 64 && tiles[CONV_SHAPE_128x128] >= shader_core_count * 2) {
  7395. shape = CONV_SHAPE_128x128;
  7396. } else if (elements[0] <= 32 && tiles[CONV_SHAPE_32x256] >= shader_core_count * 2) {
  7397. shape = CONV_SHAPE_32x256;
  7398. } else {
  7399. shape = CONV_SHAPE_64x32;
  7400. }
  7401. if (op == GGML_OP_CONV_2D) {
  7402. if (src0->type == GGML_TYPE_F32) {
  7403. return ctx->device->pipeline_conv2d_f32[shape];
  7404. } else if (src0->type == GGML_TYPE_F16) {
  7405. return ctx->device->pipeline_conv2d_f16_f32[shape];
  7406. }
  7407. } else if (op == GGML_OP_CONV_TRANSPOSE_2D) {
  7408. if (src0->type == GGML_TYPE_F32) {
  7409. return ctx->device->pipeline_conv_transpose_2d_f32[shape];
  7410. } else if (src0->type == GGML_TYPE_F16) {
  7411. return ctx->device->pipeline_conv_transpose_2d_f16_f32[shape];
  7412. }
  7413. }
  7414. }
  7415. return nullptr;
  7416. case GGML_OP_CONV_2D_DW:
  7417. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  7418. if (ggml_is_contiguous(src1)) {
  7419. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  7420. } else if (ggml_is_contiguous_channels(src1)) {
  7421. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  7422. }
  7423. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  7424. if (ggml_is_contiguous(src1)) {
  7425. return ctx->device->pipeline_conv2d_dw_whcn_f16_f32;
  7426. } else if (ggml_is_contiguous_channels(src1)) {
  7427. return ctx->device->pipeline_conv2d_dw_cwhn_f16_f32;
  7428. }
  7429. }
  7430. return nullptr;
  7431. default:
  7432. return nullptr;
  7433. }
  7434. GGML_UNUSED(src2);
  7435. }
  7436. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  7437. switch (op) {
  7438. case GGML_OP_CPY:
  7439. case GGML_OP_GET_ROWS:
  7440. case GGML_OP_ADD:
  7441. case GGML_OP_SUB:
  7442. case GGML_OP_MUL:
  7443. case GGML_OP_DIV:
  7444. case GGML_OP_ADD_ID:
  7445. case GGML_OP_CONCAT:
  7446. case GGML_OP_UPSCALE:
  7447. case GGML_OP_SQR:
  7448. case GGML_OP_SQRT:
  7449. case GGML_OP_SIN:
  7450. case GGML_OP_COS:
  7451. case GGML_OP_CLAMP:
  7452. case GGML_OP_PAD:
  7453. case GGML_OP_REPEAT:
  7454. case GGML_OP_REPEAT_BACK:
  7455. case GGML_OP_ROPE:
  7456. case GGML_OP_RMS_NORM:
  7457. case GGML_OP_CONV_2D_DW:
  7458. case GGML_OP_IM2COL:
  7459. case GGML_OP_IM2COL_3D:
  7460. case GGML_OP_SET_ROWS:
  7461. case GGML_OP_SUM:
  7462. case GGML_OP_SUM_ROWS:
  7463. case GGML_OP_MEAN:
  7464. return true;
  7465. default:
  7466. return false;
  7467. }
  7468. }
  7469. static uint32_t get_misalign_bytes(const ggml_backend_vk_context * ctx, const ggml_tensor * t)
  7470. {
  7471. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  7472. }
  7473. 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, const ggml_tensor * src3, ggml_tensor * dst) {
  7474. GGML_UNUSED(p);
  7475. GGML_UNUSED(src0);
  7476. GGML_UNUSED(src1);
  7477. GGML_UNUSED(src2);
  7478. GGML_UNUSED(src3);
  7479. GGML_UNUSED(dst);
  7480. static_assert(!std::is_const<T>::value, "unexpected type");
  7481. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  7482. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  7483. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  7484. GGML_ASSERT(!src3 || get_misalign_bytes(ctx, src3) == 0);
  7485. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  7486. }
  7487. 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, const ggml_tensor * src3, ggml_tensor * dst) {
  7488. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7489. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7490. p.misalign_offsets = (a_offset << 16) | d_offset;
  7491. GGML_UNUSED(src1);
  7492. GGML_UNUSED(src2);
  7493. GGML_UNUSED(src3);
  7494. }
  7495. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_sum_rows_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, const ggml_tensor * src3, ggml_tensor * dst) {
  7496. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7497. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7498. p.misalign_offsets = (a_offset << 16) | d_offset;
  7499. GGML_UNUSED(src1);
  7500. GGML_UNUSED(src2);
  7501. GGML_UNUSED(src3);
  7502. }
  7503. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_pad_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, const ggml_tensor * src3, ggml_tensor * dst) {
  7504. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7505. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7506. p.misalign_offsets = (a_offset << 16) | d_offset;
  7507. GGML_UNUSED(src1);
  7508. GGML_UNUSED(src2);
  7509. GGML_UNUSED(src3);
  7510. }
  7511. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_im2col_3d_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, const ggml_tensor * src3, ggml_tensor * dst) {
  7512. const uint32_t a_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  7513. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7514. p.misalign_offsets = (a_offset << 16) | d_offset;
  7515. GGML_UNUSED(src0);
  7516. GGML_UNUSED(src2);
  7517. GGML_UNUSED(src3);
  7518. }
  7519. 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, const ggml_tensor * src3, ggml_tensor * dst) {
  7520. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7521. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  7522. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7523. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  7524. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  7525. GGML_UNUSED(src2);
  7526. GGML_UNUSED(src3);
  7527. }
  7528. 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, const ggml_tensor * src3, ggml_tensor * dst) {
  7529. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  7530. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  7531. p.a_offset = a_offset;
  7532. p.d_offset = d_offset;
  7533. GGML_UNUSED(src1);
  7534. GGML_UNUSED(src2);
  7535. GGML_UNUSED(src3);
  7536. }
  7537. template<typename PC>
  7538. 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, const ggml_tensor * src3, ggml_tensor * dst, ggml_op op, PC&& pc) {
  7539. 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];
  7540. if (src1 != nullptr) {
  7541. 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];
  7542. }
  7543. if (src2 != nullptr) {
  7544. 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];
  7545. }
  7546. if (src3 != nullptr) {
  7547. std::cerr << "), (" << src3 << ", name=" << src3->name << ", type=" << src3->type << ", ne0=" << src3->ne[0] << ", ne1=" << src3->ne[1] << ", ne2=" << src3->ne[2] << ", ne3=" << src3->ne[3] << ", nb0=" << src3->nb[0] << ", nb1=" << src3->nb[1] << ", nb2=" << src3->nb[2] << ", nb3=" << src3->nb[3];
  7548. }
  7549. 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];
  7550. std::cerr << "), " << ggml_op_name(op) << ")");
  7551. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  7552. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  7553. GGML_ASSERT(dst->buffer != nullptr);
  7554. const uint64_t ne00 = src0->ne[0];
  7555. const uint64_t ne01 = src0->ne[1];
  7556. const uint64_t ne02 = src0->ne[2];
  7557. const uint64_t ne03 = src0->ne[3];
  7558. const uint64_t ne0 = ne00 * ne01;
  7559. const bool use_src1 = src1 != nullptr;
  7560. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  7561. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  7562. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  7563. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  7564. const uint64_t ne1 = ne10 * ne11;
  7565. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  7566. const bool use_src2 = src2 != nullptr;
  7567. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  7568. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  7569. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  7570. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  7571. const uint64_t ne2 = ne20 * ne21;
  7572. const bool use_src3 = src3 != nullptr;
  7573. const uint64_t ne30 = use_src3 ? src3->ne[0] : 0;
  7574. const uint64_t ne31 = use_src3 ? src3->ne[1] : 0;
  7575. const uint64_t ne32 = use_src3 ? src3->ne[2] : 0;
  7576. const uint64_t ne33 = use_src3 ? src3->ne[3] : 0;
  7577. const uint64_t ne3 = ne30 * ne31;
  7578. const uint64_t ned0 = dst->ne[0];
  7579. const uint64_t ned1 = dst->ne[1];
  7580. const uint64_t ned2 = dst->ne[2];
  7581. const uint64_t ned3 = dst->ne[3];
  7582. const uint64_t ned = ned0 * ned1;
  7583. init_pushconst_fastdiv(pc);
  7584. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  7585. if (pipeline == nullptr) {
  7586. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  7587. if (src1 != nullptr) {
  7588. std::cerr << " and " << ggml_type_name(src1->type);
  7589. }
  7590. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  7591. GGML_ABORT("fatal error");
  7592. }
  7593. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7594. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  7595. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7596. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  7597. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  7598. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  7599. ggml_backend_vk_buffer_context * src3_buf_ctx = use_src3 ? (ggml_backend_vk_buffer_context *)src3->buffer->context : nullptr;
  7600. vk_buffer d_X = nullptr;
  7601. size_t x_buf_offset = 0;
  7602. vk_buffer d_Y = nullptr;
  7603. size_t y_buf_offset = 0;
  7604. vk_buffer d_Z = nullptr;
  7605. size_t z_buf_offset = 0;
  7606. vk_buffer d_W = nullptr;
  7607. size_t w_buf_offset = 0;
  7608. bool src0_uma = false;
  7609. bool src1_uma = false;
  7610. bool src2_uma = false;
  7611. bool src3_uma = false;
  7612. if (ctx->device->uma) {
  7613. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  7614. src0_uma = d_X != nullptr;
  7615. if (use_src1) {
  7616. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  7617. src1_uma = d_Y != nullptr;
  7618. }
  7619. if (use_src2) {
  7620. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  7621. src2_uma = d_Z != nullptr;
  7622. }
  7623. if (use_src3) {
  7624. ggml_vk_host_get(ctx->device, src3->data, d_W, w_buf_offset);
  7625. src3_uma = d_W != nullptr;
  7626. }
  7627. }
  7628. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  7629. GGML_ASSERT(d_D != nullptr);
  7630. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  7631. if(!src0_uma) {
  7632. d_X = src0_buf_ctx->dev_buffer;
  7633. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  7634. GGML_ASSERT(d_X != nullptr);
  7635. }
  7636. if (use_src1 && !src1_uma) {
  7637. d_Y = src1_buf_ctx->dev_buffer;
  7638. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  7639. GGML_ASSERT(d_Y != nullptr);
  7640. }
  7641. if (use_src2 && !src2_uma) {
  7642. d_Z = src2_buf_ctx->dev_buffer;
  7643. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  7644. GGML_ASSERT(d_Z != nullptr);
  7645. }
  7646. if (use_src3 && !src3_uma) {
  7647. d_W = src3_buf_ctx->dev_buffer;
  7648. w_buf_offset = vk_tensor_offset(src3) + src3->view_offs;
  7649. GGML_ASSERT(d_W != nullptr);
  7650. }
  7651. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  7652. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, src3, dst);
  7653. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7654. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7655. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7656. w_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7657. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  7658. std::array<uint32_t, 3> elements;
  7659. // Single call if dimension 2 is contiguous
  7660. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  7661. switch (op) {
  7662. case GGML_OP_NORM:
  7663. case GGML_OP_RMS_NORM_BACK:
  7664. case GGML_OP_L2_NORM:
  7665. case GGML_OP_SOFT_MAX:
  7666. case GGML_OP_SOFT_MAX_BACK:
  7667. case GGML_OP_SUM_ROWS:
  7668. case GGML_OP_MEAN:
  7669. case GGML_OP_ARGMAX:
  7670. {
  7671. const uint32_t nr = ggml_nrows(src0);
  7672. if (nr > 262144) {
  7673. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  7674. } else if (nr > 512) {
  7675. elements = { 512, CEIL_DIV(nr, 512), 1 };
  7676. } else {
  7677. elements = { nr, 1, 1 };
  7678. }
  7679. } break;
  7680. case GGML_OP_RMS_NORM:
  7681. if (ctx->do_add_rms_partials) {
  7682. // Run one element per thread, 128 threads per workgroup
  7683. elements = { (uint32_t)CEIL_DIV(ne00, 128), 1, 1 };
  7684. } else {
  7685. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  7686. }
  7687. break;
  7688. case GGML_OP_SUM:
  7689. // We use GGML_OP_SUM_ROWS with 1 row.
  7690. elements = { 1, 1, 1 };
  7691. break;
  7692. case GGML_OP_GROUP_NORM:
  7693. {
  7694. const uint32_t num_groups = dst->op_params[0];
  7695. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  7696. } break;
  7697. case GGML_OP_DIAG_MASK_INF:
  7698. case GGML_OP_ROPE:
  7699. case GGML_OP_ROPE_BACK:
  7700. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  7701. break;
  7702. case GGML_OP_GET_ROWS:
  7703. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  7704. elements[1] = std::min(elements[1], ctx->device->properties.limits.maxComputeWorkGroupCount[1]);
  7705. elements[2] = std::min(elements[2], ctx->device->properties.limits.maxComputeWorkGroupCount[2]);
  7706. break;
  7707. case GGML_OP_ARGSORT:
  7708. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  7709. elements[1] = std::min(elements[1], ctx->device->properties.limits.maxComputeWorkGroupCount[1]);
  7710. break;
  7711. case GGML_OP_IM2COL:
  7712. {
  7713. const bool is_2D = dst->op_params[6] == 1;
  7714. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  7715. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  7716. const uint32_t KW = src0->ne[0];
  7717. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  7718. const uint32_t OW = dst->ne[1];
  7719. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  7720. elements = { OW * KW * KH, OH, batch * IC };
  7721. } break;
  7722. case GGML_OP_IM2COL_3D:
  7723. {
  7724. const uint32_t IC = ((const uint32_t *)(dst->op_params))[9];
  7725. const uint32_t N = ne13 / IC;
  7726. const uint32_t KD = ne02;
  7727. const uint32_t KH = ne01;
  7728. const uint32_t KW = ne00;
  7729. const uint32_t OD = ned3 / N;
  7730. const uint32_t OH = ned2;
  7731. const uint32_t OW = ned1;
  7732. const uint32_t IC_KD_KH_KW = IC*KD*KH*KW;
  7733. const uint32_t N_OD_OH = N*OD*OH;
  7734. elements = { IC_KD_KH_KW, OW, N_OD_OH };
  7735. elements[2] = std::min(elements[2], ctx->device->properties.limits.maxComputeWorkGroupCount[2]);
  7736. } break;
  7737. case GGML_OP_TIMESTEP_EMBEDDING:
  7738. {
  7739. const uint32_t dim = dst->op_params[0];
  7740. uint32_t half_ceil = (dim + 1) / 2;
  7741. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  7742. } break;
  7743. case GGML_OP_CONV_TRANSPOSE_1D:
  7744. {
  7745. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  7746. } break;
  7747. case GGML_OP_POOL_2D:
  7748. {
  7749. const uint32_t N = dst->ne[3];
  7750. const uint32_t OC = dst->ne[2];
  7751. const uint32_t OH = dst->ne[1];
  7752. const uint32_t OW = dst->ne[0];
  7753. elements = { N * OC * OH * OW, 1, 1};
  7754. } break;
  7755. case GGML_OP_CONV_2D:
  7756. {
  7757. elements = ggml_vk_get_conv_elements(dst);
  7758. } break;
  7759. case GGML_OP_CONV_TRANSPOSE_2D:
  7760. {
  7761. elements = ggml_vk_get_conv_transpose_2d_elements(dst);
  7762. } break;
  7763. case GGML_OP_ADD:
  7764. case GGML_OP_SUB:
  7765. case GGML_OP_DIV:
  7766. case GGML_OP_MUL:
  7767. case GGML_OP_SCALE:
  7768. case GGML_OP_SQR:
  7769. case GGML_OP_SQRT:
  7770. case GGML_OP_SIN:
  7771. case GGML_OP_COS:
  7772. case GGML_OP_CLAMP:
  7773. case GGML_OP_PAD:
  7774. case GGML_OP_ROLL:
  7775. case GGML_OP_REPEAT:
  7776. case GGML_OP_REPEAT_BACK:
  7777. case GGML_OP_CPY:
  7778. case GGML_OP_CONCAT:
  7779. case GGML_OP_UPSCALE:
  7780. case GGML_OP_UNARY:
  7781. case GGML_OP_GLU:
  7782. case GGML_OP_CONV_2D_DW:
  7783. {
  7784. uint32_t ne = ggml_nelements(dst);
  7785. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7786. // Convert from number of logical elements to 2- or 4-byte units.
  7787. ne /= ggml_blck_size(src0->type);
  7788. if ((ggml_type_size(src0->type) % 4) == 0) {
  7789. ne *= ggml_type_size(src0->type) / 4;
  7790. } else {
  7791. ne *= ggml_type_size(src0->type) / 2;
  7792. }
  7793. }
  7794. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  7795. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  7796. // So divide by block size here before splitting into 512x512 groups.
  7797. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  7798. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  7799. }
  7800. if (ne > 262144) {
  7801. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7802. } else if (ne > 512) {
  7803. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7804. } else {
  7805. elements = { ne, 1, 1 };
  7806. }
  7807. } break;
  7808. case GGML_OP_ADD_ID:
  7809. {
  7810. elements = { (uint32_t)ne01, (uint32_t)ne02, 1 };
  7811. } break;
  7812. case GGML_OP_SET_ROWS:
  7813. {
  7814. uint32_t ne = ggml_nelements(src0);
  7815. if (ggml_is_quantized(dst->type)) {
  7816. // quants run 32 threads each doing QUANT_K elements
  7817. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  7818. } else {
  7819. // scalar types do one element per thread, running 512 threads
  7820. ne = CEIL_DIV(ne, 512);
  7821. }
  7822. if (ne > 262144) {
  7823. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  7824. } else if (ne > 512) {
  7825. elements = { 512, CEIL_DIV(ne, 512), 1 };
  7826. } else {
  7827. elements = { ne, 1, 1 };
  7828. }
  7829. }
  7830. break;
  7831. case GGML_OP_SSM_CONV:
  7832. {
  7833. const uint32_t nr = src0->ne[1];
  7834. const uint32_t n_t = dst->ne[1];
  7835. const uint32_t n_s = dst->ne[2];
  7836. elements = { nr, n_t, n_s };
  7837. }
  7838. break;
  7839. default:
  7840. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  7841. break;
  7842. }
  7843. uint64_t x_sz, y_sz, z_sz, w_sz, d_sz;
  7844. if (op_supports_incontiguous) {
  7845. x_sz = ggml_nbytes(src0) + get_misalign_bytes(ctx, src0);
  7846. y_sz = use_src1 ? ggml_nbytes(src1) + get_misalign_bytes(ctx, src1) : 0;
  7847. z_sz = use_src2 ? ggml_nbytes(src2) + get_misalign_bytes(ctx, src2) : 0;
  7848. w_sz = use_src3 ? ggml_nbytes(src3) + get_misalign_bytes(ctx, src3) : 0;
  7849. d_sz = ggml_nbytes(dst) + get_misalign_bytes(ctx, dst);
  7850. if (x_buf_offset + x_sz >= d_X->size) {
  7851. x_sz = ggml_vk_get_max_buffer_range(ctx, d_X, x_buf_offset);
  7852. }
  7853. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  7854. y_sz = ggml_vk_get_max_buffer_range(ctx, d_Y, y_buf_offset);
  7855. }
  7856. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  7857. z_sz = ggml_vk_get_max_buffer_range(ctx, d_Z, z_buf_offset);
  7858. }
  7859. if (use_src3 && w_buf_offset + w_sz >= d_W->size) {
  7860. w_sz = ggml_vk_get_max_buffer_range(ctx, d_W, w_buf_offset);
  7861. }
  7862. if (d_buf_offset + d_sz >= d_D->size) {
  7863. d_sz = ggml_vk_get_max_buffer_range(ctx, d_D, d_buf_offset);
  7864. }
  7865. } else {
  7866. x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0 * ne02 * ne03;
  7867. y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 * ne12 * ne13 : 0;
  7868. z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 * ne22 * ne23 : 0;
  7869. w_sz = use_src3 ? ggml_type_size(src3->type) * ne3 * ne32 * ne33 : 0;
  7870. d_sz = ggml_type_size(dst->type) * ned * ned2 * ned3;
  7871. }
  7872. if (op == GGML_OP_ADD || op == GGML_OP_RMS_NORM) {
  7873. vk_buffer d_A = ctx->do_add_rms_partials ? ctx->prealloc_add_rms_partials : d_X;
  7874. size_t a_buf_offset = ctx->do_add_rms_partials ? ctx->prealloc_size_add_rms_partials_offset : 0;
  7875. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  7876. { vk_subbuffer{ d_X, x_buf_offset, x_sz },
  7877. vk_subbuffer{ d_Y, y_buf_offset, y_sz },
  7878. vk_subbuffer{ d_D, d_buf_offset, d_sz },
  7879. ggml_vk_subbuffer(ctx, d_A, a_buf_offset),
  7880. }, pc, elements);
  7881. } else if (op == GGML_OP_GLU) {
  7882. // Empty src1 is possible in glu, but the shader needs a buffer
  7883. vk_subbuffer subbuf_y;
  7884. if (use_src1) {
  7885. subbuf_y = { d_Y, y_buf_offset, y_sz };
  7886. } else {
  7887. subbuf_y = { d_X, 0, x_sz };
  7888. }
  7889. 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);
  7890. } else if (op == GGML_OP_SOFT_MAX) {
  7891. // Empty src1 and src2 is possible in soft_max, but the shader needs a buffer
  7892. vk_subbuffer subbuf_y;
  7893. if (use_src1) {
  7894. subbuf_y = { d_Y, y_buf_offset, y_sz };
  7895. } else {
  7896. subbuf_y = { d_X, 0, x_sz };
  7897. }
  7898. vk_subbuffer subbuf_z;
  7899. if (use_src2) {
  7900. subbuf_z = { d_Z, z_buf_offset, z_sz };
  7901. } else {
  7902. subbuf_z = { d_X, 0, x_sz };
  7903. }
  7904. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7905. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  7906. // Empty src2 is possible in rope, but the shader needs a buffer
  7907. vk_subbuffer subbuf_z, subbuf_w;
  7908. if (use_src2) {
  7909. subbuf_z = { d_Z, z_buf_offset, z_sz };
  7910. } else {
  7911. subbuf_z = { d_X, 0, x_sz };
  7912. }
  7913. if (use_src3) {
  7914. subbuf_w = { d_W, w_buf_offset, w_sz };
  7915. } else {
  7916. subbuf_w = { d_X, 0, x_sz };
  7917. }
  7918. 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 }, subbuf_w }, pc, elements);
  7919. } else if (op == GGML_OP_IM2COL || op == GGML_OP_IM2COL_3D) {
  7920. if (ctx->device->shader_int64 && ctx->device->buffer_device_address) {
  7921. // buffer device address path doesn't use dst buffer
  7922. d_sz = 1;
  7923. }
  7924. // im2col uses only src1 and dst buffers
  7925. 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);
  7926. } else if (op == GGML_OP_COUNT_EQUAL) {
  7927. // count_equal assumes that destination buffer is initialized with zeroes
  7928. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  7929. ggml_vk_sync_buffers(ctx, subctx);
  7930. 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);
  7931. } else if (op == GGML_OP_OPT_STEP_SGD) {
  7932. // OPT_STEP_SGD works on src0, it does not need dst
  7933. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz } }, pc, elements);
  7934. } else if (use_src3) {
  7935. 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_W, w_buf_offset, w_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  7936. } else if (use_src2) {
  7937. 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);
  7938. } else if (use_src1) {
  7939. 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);
  7940. } else {
  7941. 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);
  7942. }
  7943. }
  7944. 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) {
  7945. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7946. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7947. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7948. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_GET_ROWS, {
  7949. (uint32_t)ggml_nelements(src0),
  7950. (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,
  7951. (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,
  7952. (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,
  7953. 0,
  7954. 0.0f, 0.0f, 0,
  7955. });
  7956. }
  7957. static void ggml_vk_acc(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7958. const uint32_t src0_type_size = ggml_type_size(src0->type);
  7959. const uint32_t src1_type_size = ggml_type_size(src1->type);
  7960. const uint32_t dst_type_size = ggml_type_size(dst->type);
  7961. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  7962. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  7963. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  7964. int offset = dst->op_params[3] / 4; // offset in bytes
  7965. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_ACC, {
  7966. (uint32_t)ggml_nelements(src0),
  7967. (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,
  7968. (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,
  7969. (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,
  7970. 0,
  7971. 0.0f, 0.0f, offset,
  7972. });
  7973. }
  7974. static void ggml_vk_multi_add(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cgraph * cgraph, int node_idx) {
  7975. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  7976. const ggml_tensor *dst = cgraph->nodes[node_idx + ctx->num_additional_fused_ops];
  7977. // Make a list of all the tensors used by the op.
  7978. // Last element of the list is the dest tensor.
  7979. const ggml_tensor *tensors[MAX_PARAMETER_COUNT];
  7980. uint32_t num_srcs = ctx->num_additional_fused_ops + 2;
  7981. uint32_t num_tensors = num_srcs + 1;
  7982. GGML_ASSERT(num_tensors + ctx->do_add_rms_partials <= MAX_PARAMETER_COUNT);
  7983. tensors[0] = first_node->src[0];
  7984. tensors[1] = first_node->src[1];
  7985. for (int32_t i = 0; i < ctx->num_additional_fused_ops; ++i) {
  7986. // check whether the previous result is src[0] or src[1]
  7987. if (cgraph->nodes[node_idx + i] == cgraph->nodes[node_idx + i + 1]->src[0]) {
  7988. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[1];
  7989. } else {
  7990. tensors[i+2] = cgraph->nodes[node_idx + i + 1]->src[0];
  7991. }
  7992. }
  7993. tensors[num_srcs] = dst;
  7994. vk_op_multi_add_push_constants pc;
  7995. pc.ne20 = (uint32_t)dst->ne[0];
  7996. pc.ne21 = (uint32_t)dst->ne[1];
  7997. pc.ne22 = (uint32_t)dst->ne[2];
  7998. pc.ne23 = (uint32_t)dst->ne[3];
  7999. for (uint32_t i = 0; i < num_tensors; ++i) {
  8000. const ggml_tensor *t = tensors[i];
  8001. pc.nb[i][0] = (uint32_t)t->nb[0] / sizeof(float);
  8002. pc.nb[i][1] = (uint32_t)t->nb[1] / sizeof(float);
  8003. pc.nb[i][2] = (uint32_t)t->nb[2] / sizeof(float);
  8004. pc.nb[i][3] = (uint32_t)t->nb[3] / sizeof(float);
  8005. }
  8006. pc.rms_partials = ctx->do_add_rms_partials;
  8007. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, tensors[0], tensors[1], nullptr, dst, dst->op);
  8008. if (pipeline == nullptr) {
  8009. std::cerr << "ggml_vulkan: Error: Missing multi_add";
  8010. GGML_ABORT("fatal error");
  8011. }
  8012. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8013. ggml_backend_vk_buffer_context * buf_ctx[MAX_PARAMETER_COUNT];
  8014. vk_buffer buf[MAX_PARAMETER_COUNT];
  8015. size_t offset[MAX_PARAMETER_COUNT];
  8016. bool uma[MAX_PARAMETER_COUNT];
  8017. for (uint32_t i = 0; i < num_tensors; ++i) {
  8018. buf_ctx[i] = (ggml_backend_vk_buffer_context *)tensors[i]->buffer->context;
  8019. buf[i] = nullptr;
  8020. offset[i] = 0;
  8021. uma[i] = false;
  8022. if (ctx->device->uma) {
  8023. ggml_vk_host_get(ctx->device, tensors[i]->data, buf[i], offset[i]);
  8024. uma[i] = buf[i] != nullptr;
  8025. }
  8026. if (!uma[i]) {
  8027. buf[i] = buf_ctx[i]->dev_buffer;
  8028. offset[i] = vk_tensor_offset(tensors[i]) + tensors[i]->view_offs;
  8029. }
  8030. GGML_ASSERT(buf[i] != nullptr);
  8031. }
  8032. // If any remaining descriptors are unused, just point them at src[0]
  8033. for (uint32_t i = num_tensors; i < MAX_PARAMETER_COUNT; ++i) {
  8034. buf[i] = buf[0];
  8035. offset[i] = 0;
  8036. }
  8037. if (ctx->do_add_rms_partials) {
  8038. buf[num_tensors] = ctx->prealloc_add_rms_partials;
  8039. offset[num_tensors] = ctx->prealloc_size_add_rms_partials_offset;
  8040. }
  8041. std::array<uint32_t, 3> elements;
  8042. uint32_t ne = ggml_nelements(dst);
  8043. if (ne > 262144) {
  8044. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  8045. } else if (ne > 512) {
  8046. elements = { 512, CEIL_DIV(ne, 512), 1 };
  8047. } else {
  8048. elements = { ne, 1, 1 };
  8049. }
  8050. static_assert(MAX_PARAMETER_COUNT == 12);
  8051. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  8052. {
  8053. ggml_vk_subbuffer(ctx, buf[0], offset[0]),
  8054. ggml_vk_subbuffer(ctx, buf[1], offset[1]),
  8055. ggml_vk_subbuffer(ctx, buf[2], offset[2]),
  8056. ggml_vk_subbuffer(ctx, buf[3], offset[3]),
  8057. ggml_vk_subbuffer(ctx, buf[4], offset[4]),
  8058. ggml_vk_subbuffer(ctx, buf[5], offset[5]),
  8059. ggml_vk_subbuffer(ctx, buf[6], offset[6]),
  8060. ggml_vk_subbuffer(ctx, buf[7], offset[7]),
  8061. ggml_vk_subbuffer(ctx, buf[8], offset[8]),
  8062. ggml_vk_subbuffer(ctx, buf[9], offset[9]),
  8063. ggml_vk_subbuffer(ctx, buf[10], offset[10]),
  8064. ggml_vk_subbuffer(ctx, buf[11], offset[11]),
  8065. }, pc, elements);
  8066. }
  8067. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8068. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8069. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8070. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8071. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_ADD, {
  8072. (uint32_t)ggml_nelements(src0),
  8073. (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,
  8074. (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,
  8075. (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,
  8076. 0,
  8077. 0.0f, 0.0f, ctx->do_add_rms_partials,
  8078. });
  8079. }
  8080. static void ggml_vk_sub(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8081. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8082. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8083. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8084. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_SUB, {
  8085. (uint32_t)ggml_nelements(src0),
  8086. (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,
  8087. (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,
  8088. (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,
  8089. 0,
  8090. 0.0f, 0.0f, 0,
  8091. });
  8092. }
  8093. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8094. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8095. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8096. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8097. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_MUL, {
  8098. (uint32_t)ggml_nelements(src0),
  8099. (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,
  8100. (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,
  8101. (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,
  8102. 0,
  8103. 0.0f, 0.0f, 0,
  8104. });
  8105. }
  8106. static void ggml_vk_div(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8107. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8108. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8109. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8110. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_DIV, {
  8111. (uint32_t)ggml_nelements(src0),
  8112. (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,
  8113. (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,
  8114. (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,
  8115. 0,
  8116. 0.0f, 0.0f, 0,
  8117. });
  8118. }
  8119. static void ggml_vk_add_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  8120. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8121. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8122. const uint32_t src2_type_size = ggml_type_size(src2->type);
  8123. ggml_vk_op_f32<vk_op_add_id_push_constants>(ctx, subctx, src0, src1, src2, nullptr, dst, GGML_OP_ADD_ID, {
  8124. (uint32_t)dst->ne[0],
  8125. (uint32_t)dst->ne[1],
  8126. (uint32_t)src0->nb[1] / src0_type_size,
  8127. (uint32_t)src0->nb[2] / src0_type_size,
  8128. (uint32_t)src1->nb[1] / src1_type_size,
  8129. (uint32_t)src2->nb[1] / src2_type_size,
  8130. });
  8131. }
  8132. 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) {
  8133. GGML_ASSERT(version == 6 || version == 7);
  8134. int num_srcs = version == 6 ? 6 : 7;
  8135. for (int i = 0; i < num_srcs; i++) {
  8136. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  8137. }
  8138. GGML_ASSERT(dst->buffer != nullptr);
  8139. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  8140. GGML_ASSERT(pipeline != nullptr);
  8141. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8142. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8143. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  8144. for (int i = 0; i < num_srcs; i++) {
  8145. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  8146. }
  8147. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  8148. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  8149. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  8150. if (ctx->device->uma) {
  8151. for (int i = 0; i < num_srcs; i++) {
  8152. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  8153. srcs_uma[i] = d_srcs[i] != nullptr;
  8154. }
  8155. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  8156. dst_uma = d_D != nullptr;
  8157. }
  8158. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  8159. for (int i = 0; i < num_srcs; i++) {
  8160. src_sizes[i] = ggml_nbytes(dst->src[i]);
  8161. if (!srcs_uma[i]) {
  8162. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  8163. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  8164. }
  8165. }
  8166. const uint64_t dst_size = ggml_nbytes(dst);
  8167. if (!dst_uma) {
  8168. d_D = dst_buf_ctx->dev_buffer;
  8169. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  8170. }
  8171. std::array<uint32_t, 3> elements = {
  8172. (uint32_t)(pc.B * pc.H),
  8173. 1,
  8174. 1
  8175. };
  8176. if (version == 6) {
  8177. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  8178. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  8179. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  8180. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  8181. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  8182. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  8183. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  8184. vk_subbuffer{ d_D, dst_offset, dst_size }
  8185. }, pc, elements);
  8186. } else if (version == 7) {
  8187. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  8188. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  8189. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  8190. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  8191. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  8192. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  8193. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  8194. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  8195. vk_subbuffer{ d_D, dst_offset, dst_size }
  8196. }, pc, elements);
  8197. } else {
  8198. // shouldn't happen
  8199. GGML_ASSERT(false);
  8200. }
  8201. }
  8202. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
  8203. const size_t seq_length = dst->src[0]->ne[2];
  8204. const size_t n_embed = dst->ne[0];
  8205. const size_t n_heads = dst->src[0]->ne[1];
  8206. const size_t n_seqs = dst->src[5]->ne[1];
  8207. ggml_vk_op_f32_wkv(
  8208. ctx, subctx, dst,
  8209. {
  8210. (uint32_t)n_seqs,
  8211. (uint32_t)seq_length,
  8212. (uint32_t)n_embed,
  8213. (uint32_t)n_heads,
  8214. },
  8215. 6
  8216. );
  8217. }
  8218. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
  8219. const size_t seq_length = dst->src[0]->ne[2];
  8220. const size_t n_embed = dst->ne[0];
  8221. const size_t n_heads = dst->src[0]->ne[1];
  8222. const size_t n_seqs = dst->src[6]->ne[1];
  8223. ggml_vk_op_f32_wkv(
  8224. ctx, subctx, dst,
  8225. {
  8226. (uint32_t)n_seqs,
  8227. (uint32_t)seq_length,
  8228. (uint32_t)n_embed,
  8229. (uint32_t)n_heads,
  8230. },
  8231. 7
  8232. );
  8233. }
  8234. static void ggml_vk_ssm_scan(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
  8235. const ggml_tensor * src0 = dst->src[0];
  8236. const ggml_tensor * src1 = dst->src[1];
  8237. const ggml_tensor * src2 = dst->src[2];
  8238. const ggml_tensor * src3 = dst->src[3];
  8239. const ggml_tensor * src4 = dst->src[4];
  8240. const ggml_tensor * src5 = dst->src[5];
  8241. GGML_ASSERT(dst->buffer != nullptr);
  8242. const uint32_t head_dim = src0->ne[1];
  8243. const uint32_t n_head = src1->ne[1];
  8244. const uint32_t n_group = src4->ne[1];
  8245. const uint32_t n_tok = src1->ne[2];
  8246. const uint32_t n_seq = src1->ne[3];
  8247. bool is_mamba2 = (src3->nb[1] == sizeof(float));
  8248. GGML_ASSERT(is_mamba2);
  8249. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, dst->op);
  8250. GGML_ASSERT(pipeline != nullptr);
  8251. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8252. const int64_t s_off = ggml_nelements(src1) * sizeof(float);
  8253. const vk_op_ssm_scan_push_constants pc = {
  8254. (uint32_t)src0->nb[2], (uint32_t)src0->nb[3],
  8255. (uint32_t)src1->nb[2], (uint32_t)src1->nb[3],
  8256. (uint32_t)src2->nb[1], (uint32_t)src2->nb[2],
  8257. (uint32_t)src3->nb[1],
  8258. (uint32_t)src4->nb[2], (uint32_t)src4->nb[3],
  8259. (uint32_t)src5->nb[2], (uint32_t)src5->nb[3],
  8260. (uint32_t)s_off,
  8261. n_head, head_dim, n_group, n_tok
  8262. };
  8263. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8264. ggml_backend_vk_buffer_context * src_buf_ctxs[GGML_MAX_SRC];
  8265. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  8266. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  8267. }
  8268. vk_buffer d_D = nullptr, d_srcs[GGML_MAX_SRC] = { nullptr };
  8269. size_t dst_offset = 0, src_offsets[GGML_MAX_SRC] = { 0 };
  8270. bool dst_uma = false, srcs_uma[GGML_MAX_SRC] = { false };
  8271. if (ctx->device->uma) {
  8272. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  8273. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  8274. srcs_uma[i] = d_srcs[i] != nullptr;
  8275. }
  8276. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  8277. dst_uma = d_D != nullptr;
  8278. }
  8279. if (!dst_uma) {
  8280. d_D = dst_buf_ctx->dev_buffer;
  8281. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  8282. }
  8283. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  8284. if (!srcs_uma[i]) {
  8285. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  8286. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  8287. }
  8288. }
  8289. size_t dst_size = ggml_nbytes(dst);
  8290. size_t src_sizes[GGML_MAX_SRC];
  8291. for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
  8292. src_sizes[i] = ggml_nbytes(dst->src[i]);
  8293. }
  8294. std::array<uint32_t, 3> elements;
  8295. const int splitH = 16;
  8296. const uint32_t num_workgroups_x = CEIL_DIV(n_head * head_dim, splitH);
  8297. const uint32_t num_workgroups_y = n_seq;
  8298. elements = { num_workgroups_x, num_workgroups_y, 1 };
  8299. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  8300. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  8301. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  8302. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  8303. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  8304. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  8305. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  8306. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  8307. vk_subbuffer{ d_D, dst_offset, dst_size }
  8308. }, pc, elements);
  8309. }
  8310. static void ggml_vk_ssm_conv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
  8311. const ggml_tensor * src0 = dst->src[0];
  8312. const ggml_tensor * src1 = dst->src[1];
  8313. ggml_vk_op_f32<vk_op_ssm_conv_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_SSM_CONV, {
  8314. (uint32_t)src0->nb[1], (uint32_t)src0->nb[2],
  8315. (uint32_t)src1->nb[1],
  8316. (uint32_t)dst->nb[0], (uint32_t)dst->nb[1], (uint32_t)dst->nb[2],
  8317. (uint32_t)src1->ne[0],
  8318. (uint32_t)src0->ne[0],
  8319. (uint32_t)src0->ne[1],
  8320. (uint32_t)dst->ne[1],
  8321. (uint32_t)dst->ne[2],
  8322. });
  8323. }
  8324. 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) {
  8325. const ggml_tensor * x = dst->src[0];
  8326. const ggml_tensor * g = dst->src[1];
  8327. const ggml_tensor * gm = dst->src[2];
  8328. const ggml_tensor * gv = dst->src[3];
  8329. const ggml_tensor * p = dst->src[4];
  8330. GGML_ASSERT(x->type == GGML_TYPE_F32);
  8331. GGML_ASSERT(g->type == GGML_TYPE_F32);
  8332. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  8333. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  8334. GGML_ASSERT(p->type == GGML_TYPE_F32);
  8335. GGML_ASSERT(dst->buffer != nullptr);
  8336. GGML_ASSERT(ggml_is_contiguous(x));
  8337. GGML_ASSERT(ggml_is_contiguous(g));
  8338. GGML_ASSERT(ggml_is_contiguous(gm));
  8339. GGML_ASSERT(ggml_is_contiguous(gv));
  8340. GGML_ASSERT(ggml_is_contiguous(p));
  8341. GGML_ASSERT(ggml_are_same_shape(x, g));
  8342. GGML_ASSERT(ggml_are_same_shape(x, gm));
  8343. GGML_ASSERT(ggml_are_same_shape(x, gv));
  8344. GGML_ASSERT(ggml_nelements(p) == 7);
  8345. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  8346. GGML_ASSERT(pipeline != nullptr);
  8347. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8348. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  8349. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  8350. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  8351. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  8352. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  8353. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  8354. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  8355. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  8356. if (ctx->device->uma) {
  8357. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  8358. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  8359. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  8360. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  8361. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  8362. X_uma = d_X != nullptr;
  8363. G_uma = d_G != nullptr;
  8364. GM_uma = d_GM != nullptr;
  8365. GV_uma = d_GV != nullptr;
  8366. P_uma = d_P != nullptr;
  8367. }
  8368. if (!X_uma) {
  8369. d_X = x_buf_ctx->dev_buffer;
  8370. x_offset = vk_tensor_offset(x) + x->view_offs;
  8371. }
  8372. if (!G_uma) {
  8373. d_G = g_buf_ctx->dev_buffer;
  8374. g_offset = vk_tensor_offset(g) + g->view_offs;
  8375. }
  8376. if (!GM_uma) {
  8377. d_GM = gm_buf_ctx->dev_buffer;
  8378. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  8379. }
  8380. if (!GV_uma) {
  8381. d_GV = gv_buf_ctx->dev_buffer;
  8382. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  8383. }
  8384. if (!P_uma) {
  8385. d_P = p_buf_ctx->dev_buffer;
  8386. p_offset = vk_tensor_offset(p) + p->view_offs;
  8387. }
  8388. const uint64_t x_size = ggml_nbytes(x);
  8389. const uint64_t g_size = ggml_nbytes(g);
  8390. const uint64_t gm_size = ggml_nbytes(gm);
  8391. const uint64_t gv_size = ggml_nbytes(gv);
  8392. const uint64_t p_size = ggml_nbytes(p);
  8393. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  8394. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  8395. vk_subbuffer{ d_X, x_offset, x_size },
  8396. vk_subbuffer{ d_G, g_offset, g_size },
  8397. vk_subbuffer{ d_GM, gm_offset, gm_size },
  8398. vk_subbuffer{ d_GV, gv_offset, gv_size },
  8399. vk_subbuffer{ d_P, p_offset, p_size },
  8400. }, pc, elements);
  8401. }
  8402. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
  8403. const size_t n = ggml_nelements(dst->src[0]);
  8404. ggml_vk_op_f32_opt_step_adamw(
  8405. ctx, subctx, dst,
  8406. { (uint32_t)n, 0, 0.0f, 0.0f }
  8407. );
  8408. }
  8409. static void ggml_vk_opt_step_sgd(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  8410. const size_t n = ggml_nelements(dst->src[0]);
  8411. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, src2, nullptr, dst, GGML_OP_OPT_STEP_SGD, { (uint32_t)n, 0, 0.0f, 0.0f });
  8412. }
  8413. static void ggml_vk_concat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8414. int * op_params = (int *)dst->op_params;
  8415. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8416. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8417. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8418. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_CONCAT, {
  8419. (uint32_t)ggml_nelements(dst),
  8420. (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,
  8421. (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,
  8422. (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,
  8423. 0,
  8424. 0.0f, 0.0f, op_params[0],
  8425. });
  8426. }
  8427. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8428. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8429. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  8430. GGML_TENSOR_UNARY_OP_LOCALS
  8431. float sf0 = (float)ne0 / ne00;
  8432. float sf1 = (float)ne1 / ne01;
  8433. float sf2 = (float)ne2 / ne02;
  8434. float sf3 = (float)ne3 / ne03;
  8435. float pixel_offset = 0.5f;
  8436. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  8437. sf0 = ne0 > 1 && ne00 > 1 ? (float)(ne0 - 1) / (ne00 - 1) : sf0;
  8438. sf1 = ne1 > 1 && ne01 > 1 ? (float)(ne1 - 1) / (ne01 - 1) : sf1;
  8439. pixel_offset = 0.0f;
  8440. }
  8441. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  8442. (uint32_t)ggml_nelements(dst), 0, 0,
  8443. (uint32_t)ne00, (uint32_t)ne01,
  8444. (uint32_t)nb00 / src0_type_size, (uint32_t)nb01 / src0_type_size, (uint32_t)nb02 / src0_type_size, (uint32_t)nb03 / src0_type_size,
  8445. (uint32_t)ne0, (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  8446. sf0, sf1, sf2, sf3, pixel_offset
  8447. });
  8448. }
  8449. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8450. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  8451. p.param1 = ggml_get_op_params_f32(dst, 0);
  8452. p.param2 = ggml_get_op_params_f32(dst, 1);
  8453. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p));
  8454. }
  8455. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8456. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst));
  8457. }
  8458. static void ggml_vk_sqrt(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8459. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_SQRT, vk_op_unary_push_constants_init(src0, dst));
  8460. }
  8461. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8462. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst));
  8463. }
  8464. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8465. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst));
  8466. }
  8467. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8468. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  8469. p.param1 = ggml_get_op_params_f32(dst, 0);
  8470. p.param2 = ggml_get_op_params_f32(dst, 1);
  8471. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p));
  8472. }
  8473. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8474. vk_op_pad_push_constants p = vk_op_pad_push_constants_init(src0, dst);
  8475. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p));
  8476. }
  8477. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8478. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  8479. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  8480. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  8481. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  8482. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  8483. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  8484. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  8485. memcpy(&p.param1, &s01_packed, sizeof(float));
  8486. memcpy(&p.param2, &s23_packed, sizeof(float));
  8487. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p));
  8488. }
  8489. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8490. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  8491. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p));
  8492. }
  8493. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8494. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  8495. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p));
  8496. }
  8497. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8498. uint32_t ne = (uint32_t)ggml_nelements(src0);
  8499. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  8500. // Convert from number of logical elements to 2- or 4-byte units.
  8501. ne /= ggml_blck_size(src0->type);
  8502. if ((ggml_type_size(src0->type) % 4) == 0) {
  8503. ne *= ggml_type_size(src0->type) / 4;
  8504. } else {
  8505. ne *= ggml_type_size(src0->type) / 2;
  8506. }
  8507. }
  8508. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  8509. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p));
  8510. }
  8511. 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) {
  8512. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8513. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8514. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8515. // Skip empty skip_rows operations. For most ops the empty check at the start
  8516. // of ggml_vk_build_graph is sufficient, but set_rows can have a nonempty dst
  8517. // with empty srcs.
  8518. if (ggml_is_empty(src0) || ggml_is_empty(src1)) {
  8519. return;
  8520. }
  8521. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_SET_ROWS, {
  8522. (uint32_t)ggml_nelements(src0),
  8523. (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,
  8524. (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,
  8525. (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,
  8526. 0,
  8527. 0.0f, 0.0f, 0,
  8528. });
  8529. }
  8530. 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) {
  8531. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_SILU_BACK, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f });
  8532. }
  8533. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8534. float * op_params = (float *)dst->op_params;
  8535. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f });
  8536. }
  8537. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8538. const int * int_op_params = (const int *)dst->op_params;
  8539. const float * float_op_params = (const float *)dst->op_params;
  8540. const uint32_t num_groups = int_op_params[0];
  8541. const float eps = float_op_params[1];
  8542. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  8543. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_GROUP_NORM, { group_size, 0, eps, 0.0f });
  8544. }
  8545. static uint32_t ggml_vk_rms_num_partials(ggml_backend_vk_context * ctx, const ggml_tensor *node) {
  8546. const uint32_t ne = (uint32_t)node->ne[0];
  8547. const uint32_t denom = ctx->device->pipeline_add_rms[0][0][0]->wg_denoms[0];
  8548. const uint32_t num_partials = CEIL_DIV(ne, denom);
  8549. return num_partials;
  8550. }
  8551. static uint32_t ggml_vk_rms_partials_size(ggml_backend_vk_context * ctx, const ggml_tensor *node) {
  8552. const uint32_t num_partials = ggml_vk_rms_num_partials(ctx, node);
  8553. const uint32_t num_bytes = ROUNDUP_POW2(num_partials * sizeof(uint32_t), ctx->device->partials_binding_alignment);
  8554. return num_bytes;
  8555. }
  8556. 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) {
  8557. const uint32_t src0_type_size = ggml_type_size(src0->type);
  8558. const uint32_t src1_type_size = ggml_type_size(src1->type);
  8559. const uint32_t dst_type_size = ggml_type_size(dst->type);
  8560. uint32_t param3 = ctx->do_add_rms_partials ? ggml_vk_rms_num_partials(ctx, dst) : 0;
  8561. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_RMS_NORM, {
  8562. (uint32_t)ggml_nelements(src0),
  8563. (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,
  8564. (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,
  8565. (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,
  8566. 0,
  8567. op_params[0], 0.0f, (int32_t)param3,
  8568. });
  8569. if (ctx->do_add_rms_partials_offset_calculation) {
  8570. ctx->prealloc_size_add_rms_partials_offset += ggml_vk_rms_partials_size(ctx, src0);
  8571. ctx->do_add_rms_partials = false;
  8572. ctx->do_add_rms_partials_offset_calculation = false;
  8573. }
  8574. }
  8575. 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) {
  8576. float * op_params = (float *)dst->op_params;
  8577. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_RMS_NORM_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f });
  8578. }
  8579. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8580. float * op_params = (float *)dst->op_params;
  8581. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_L2_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f });
  8582. }
  8583. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8584. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_UNARY, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f });
  8585. }
  8586. static void ggml_vk_glu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8587. const float * op_params_f = (const float *)dst->op_params;
  8588. const bool swapped = (bool)dst->op_params[1];
  8589. const bool split = src1 != nullptr;
  8590. const float alpha = op_params_f[2];
  8591. const float limit = op_params_f[3];
  8592. GGML_ASSERT(ggml_is_contiguous(src0));
  8593. if (!split) {
  8594. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  8595. } else {
  8596. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  8597. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  8598. GGML_ASSERT(src0->type == src1->type);
  8599. }
  8600. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  8601. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_GLU,
  8602. {
  8603. (uint32_t)ggml_nelements(dst),
  8604. (uint32_t)src0->ne[0],
  8605. (uint32_t)dst->ne[0],
  8606. mode,
  8607. alpha,
  8608. limit
  8609. });
  8610. }
  8611. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8612. int32_t * op_params = (int32_t *)dst->op_params;
  8613. ggml_vk_op_f32<vk_op_diag_mask_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_DIAG_MASK_INF, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0] });
  8614. }
  8615. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  8616. float * op_params = (float *)dst->op_params;
  8617. float scale = op_params[0];
  8618. float max_bias = op_params[1];
  8619. const uint32_t ncols = (uint32_t)src0->ne[0];
  8620. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  8621. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  8622. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  8623. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  8624. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  8625. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  8626. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  8627. const uint32_t n_head_kv = src0->ne[2];
  8628. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  8629. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  8630. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  8631. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, src2, nullptr, dst, GGML_OP_SOFT_MAX, {
  8632. ncols,
  8633. src1 != nullptr ? nrows_y : (uint32_t)0,
  8634. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  8635. ne12, ne13,
  8636. nb11, nb12, nb13,
  8637. scale, max_bias,
  8638. m0, m1,
  8639. n_head_log2,
  8640. nrows_x,
  8641. src2 != nullptr
  8642. });
  8643. }
  8644. 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) {
  8645. float * op_params = (float *)dst->op_params;
  8646. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)ggml_nrows(src0), op_params[0], op_params[1] });
  8647. }
  8648. static void ggml_vk_topk_moe(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_cgraph * cgraph, int node_idx) {
  8649. topk_moe_mode mode = ggml_vk_num_additional_ops_to_topk_moe_mode(ctx->num_additional_fused_ops);
  8650. ggml_tensor * logits = cgraph->nodes[node_idx + 0]->src[0];
  8651. ggml_tensor * weights = (mode == TOPK_MOE_EARLY_SOFTMAX_NORM) ? cgraph->nodes[node_idx + 9] :
  8652. (mode == TOPK_MOE_EARLY_SOFTMAX) ? cgraph->nodes[node_idx + 4] :
  8653. cgraph->nodes[node_idx + 5];
  8654. ggml_tensor * ids = (mode == TOPK_MOE_LATE_SOFTMAX) ? cgraph->nodes[node_idx + 1] : cgraph->nodes[node_idx + 3];
  8655. GGML_ASSERT(logits->type == GGML_TYPE_F32);
  8656. GGML_ASSERT(weights->type == GGML_TYPE_F32);
  8657. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  8658. const int n_experts = logits->ne[0];
  8659. const int n_rows = logits->ne[1];
  8660. const int n_expert_used = weights->ne[1];
  8661. GGML_ASSERT(ids->nb[1] / ggml_type_size(ids->type) == (size_t) n_experts);
  8662. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, nullptr, nullptr, nullptr, cgraph->nodes[node_idx], GGML_OP_SOFT_MAX);
  8663. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  8664. ggml_backend_vk_buffer_context * logits_buf_ctx = (ggml_backend_vk_buffer_context *)logits->buffer->context;
  8665. ggml_backend_vk_buffer_context * weights_buf_ctx = (ggml_backend_vk_buffer_context *)weights->buffer->context;
  8666. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  8667. vk_buffer d_logits = nullptr;
  8668. size_t logits_buf_offset = 0;
  8669. vk_buffer d_weights = nullptr;
  8670. size_t weights_buf_offset = 0;
  8671. vk_buffer d_ids = nullptr;
  8672. size_t ids_buf_offset = 0;
  8673. bool logits_uma = false;
  8674. bool weights_uma = false;
  8675. bool ids_uma = false;
  8676. if (ctx->device->uma) {
  8677. ggml_vk_host_get(ctx->device, logits->data, d_logits, logits_buf_offset);
  8678. ggml_vk_host_get(ctx->device, weights->data, d_weights, weights_buf_offset);
  8679. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  8680. logits_uma = d_logits != nullptr;
  8681. weights_uma = d_weights != nullptr;
  8682. ids_uma = d_ids != nullptr;
  8683. }
  8684. if (!logits_uma) {
  8685. d_logits = logits_buf_ctx->dev_buffer;
  8686. logits_buf_offset = vk_tensor_offset(logits) + logits->view_offs;
  8687. GGML_ASSERT(d_logits != nullptr);
  8688. }
  8689. if (!weights_uma) {
  8690. d_weights = weights_buf_ctx->dev_buffer;
  8691. weights_buf_offset = vk_tensor_offset(weights) + weights->view_offs;
  8692. GGML_ASSERT(d_weights != nullptr);
  8693. }
  8694. if (!ids_uma) {
  8695. d_ids = ids_buf_ctx->dev_buffer;
  8696. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  8697. GGML_ASSERT(d_ids != nullptr);
  8698. }
  8699. vk_op_topk_moe_push_constants pc {};
  8700. pc.n_rows = n_rows;
  8701. pc.n_expert_used = n_expert_used;
  8702. if (mode == TOPK_MOE_EARLY_SOFTMAX_NORM) {
  8703. ggml_tensor * clamp = cgraph->nodes[node_idx + 7];
  8704. pc.clamp_min = ggml_get_op_params_f32(clamp, 0);
  8705. pc.clamp_max = ggml_get_op_params_f32(clamp, 1);
  8706. }
  8707. GGML_ASSERT(n_expert_used <= n_experts);
  8708. const uint32_t rows_per_block = 4;
  8709. std::array<uint32_t, 3> elements = { CEIL_DIV(n_rows, rows_per_block), 1, 1 };
  8710. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  8711. {
  8712. ggml_vk_subbuffer(ctx, d_logits, logits_buf_offset),
  8713. ggml_vk_subbuffer(ctx, d_weights, weights_buf_offset),
  8714. ggml_vk_subbuffer(ctx, d_ids, ids_buf_offset),
  8715. }, pc, elements);
  8716. }
  8717. static void ggml_vk_rope(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_cgraph * cgraph, int node_idx, bool backprop) {
  8718. ggml_tensor * dst = cgraph->nodes[node_idx];
  8719. const ggml_tensor * src0 = dst->src[0];
  8720. const ggml_tensor * src1 = dst->src[1];
  8721. const ggml_tensor * src2 = dst->src[2];
  8722. const ggml_tensor * src3 = nullptr;
  8723. const int n_dims = ((int32_t *) dst->op_params)[1];
  8724. const int mode = ((int32_t *) dst->op_params)[2];
  8725. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  8726. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  8727. const float freq_base = ((float *) dst->op_params)[5];
  8728. const float freq_scale = ((float *) dst->op_params)[6];
  8729. const float ext_factor = ((float *) dst->op_params)[7];
  8730. const float attn_factor = ((float *) dst->op_params)[8];
  8731. const float beta_fast = ((float *) dst->op_params)[9];
  8732. const float beta_slow = ((float *) dst->op_params)[10];
  8733. int sections[4] {};
  8734. if (mode & GGML_ROPE_TYPE_MROPE) {
  8735. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  8736. }
  8737. const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE;
  8738. float corr_dims[2];
  8739. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  8740. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  8741. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  8742. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  8743. uint32_t set_rows_stride = 0;
  8744. // Fused rope + view + set_rows passes the set_rows destination stride in set_rows_stride
  8745. // and overrides the dst and sets src3=row_indices
  8746. if (ctx->num_additional_fused_ops > 0) {
  8747. set_rows_stride = cgraph->nodes[node_idx + 2]->nb[1] / ggml_type_size(cgraph->nodes[node_idx + 2]->type);
  8748. src3 = cgraph->nodes[node_idx + 2]->src[1];
  8749. dst = cgraph->nodes[node_idx + 2];
  8750. }
  8751. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, src3, dst, GGML_OP_ROPE, {
  8752. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  8753. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  8754. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  8755. { sections[0], sections[1], sections[2], sections[3] }, is_imrope, backprop, set_rows_stride,
  8756. });
  8757. }
  8758. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8759. int32_t * op_params = (int32_t *)dst->op_params;
  8760. uint32_t ncols = src0->ne[0];
  8761. uint32_t nrows = ggml_nrows(src0);
  8762. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  8763. ncols,
  8764. nrows,
  8765. op_params[0],
  8766. });
  8767. }
  8768. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8769. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, ggml_nelements(src0));
  8770. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_SUM, p);
  8771. }
  8772. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8773. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, src0->ne[0]);
  8774. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, p);
  8775. }
  8776. static void ggml_vk_mean(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8777. vk_op_sum_rows_push_constants p = vk_op_sum_rows_push_constants_init(src0, dst, src0->ne[0]);
  8778. p.weight = 1.0f / (float)src0->ne[0];
  8779. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_MEAN, p);
  8780. }
  8781. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8782. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], 0.0f, 0.0f });
  8783. }
  8784. 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) {
  8785. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_COUNT_EQUAL, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f });
  8786. }
  8787. static void ggml_vk_im2col(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8788. const int32_t s0 = dst->op_params[0];
  8789. const int32_t s1 = dst->op_params[1];
  8790. const int32_t p0 = dst->op_params[2];
  8791. const int32_t p1 = dst->op_params[3];
  8792. const int32_t d0 = dst->op_params[4];
  8793. const int32_t d1 = dst->op_params[5];
  8794. const bool is_2D = dst->op_params[6] == 1;
  8795. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  8796. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  8797. const uint32_t IW = src1->ne[0];
  8798. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  8799. const uint32_t KW = src0->ne[0];
  8800. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  8801. const uint32_t OW = dst->ne[1];
  8802. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  8803. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  8804. const uint32_t pelements = OW * KW * KH;
  8805. const ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8806. const vk_buffer d_buf = d_buf_ctx->dev_buffer;
  8807. const vk::DeviceAddress dst_addr = d_buf->bda_addr + vk_tensor_offset(dst) + dst->view_offs;
  8808. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_IM2COL, {
  8809. dst_addr,
  8810. batch_offset, offset_delta,
  8811. IC, IW, IH, OW, OH, KW, KH,
  8812. pelements,
  8813. IC * KH * KW,
  8814. s0, s1, p0, p1, d0, d1,
  8815. });
  8816. }
  8817. static void ggml_vk_im2col_3d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8818. GGML_TENSOR_BINARY_OP_LOCALS
  8819. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8820. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8821. const int32_t s2 = ((const int32_t *)(dst->op_params))[2];
  8822. const int32_t p0 = ((const int32_t *)(dst->op_params))[3];
  8823. const int32_t p1 = ((const int32_t *)(dst->op_params))[4];
  8824. const int32_t p2 = ((const int32_t *)(dst->op_params))[5];
  8825. const int32_t d0 = ((const int32_t *)(dst->op_params))[6];
  8826. const int32_t d1 = ((const int32_t *)(dst->op_params))[7];
  8827. const int32_t d2 = ((const int32_t *)(dst->op_params))[8];
  8828. const int32_t IC = ((const int32_t *)(dst->op_params))[9];
  8829. const int64_t N = ne13 / IC;
  8830. const int64_t ID = ne12;
  8831. const int64_t IH = ne11;
  8832. const int64_t IW = ne10;
  8833. const int64_t KD = ne02;
  8834. const int64_t KH = ne01;
  8835. const int64_t KW = ne00;
  8836. const int64_t OD = ne3 / N;
  8837. const int64_t OH = ne2;
  8838. const int64_t OW = ne1;
  8839. const ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8840. const vk_buffer d_buf = d_buf_ctx->dev_buffer;
  8841. const vk::DeviceAddress dst_addr = d_buf->bda_addr + vk_tensor_offset(dst) + dst->view_offs;
  8842. vk_op_im2col_3d_push_constants pc {};
  8843. pc.dst_addr = dst_addr;
  8844. pc.nb10 = nb10 / ggml_type_size(src1->type);
  8845. pc.nb11 = nb11 / ggml_type_size(src1->type);
  8846. pc.nb12 = nb12 / ggml_type_size(src1->type);
  8847. pc.nb13 = nb13 / ggml_type_size(src1->type);
  8848. pc.s0 = s0;
  8849. pc.s1 = s1;
  8850. pc.s2 = s2;
  8851. pc.p0 = p0;
  8852. pc.p1 = p1;
  8853. pc.p2 = p2;
  8854. pc.d0 = d0;
  8855. pc.d1 = d1;
  8856. pc.d2 = d2;
  8857. pc.IW = IW;
  8858. pc.IH = IH;
  8859. pc.ID = ID;
  8860. pc.IC = IC;
  8861. pc.KW = KW;
  8862. pc.OH = OH;
  8863. pc.KD_KH_KW = KD*KH*KW;
  8864. pc.KH_KW = KH*KW;
  8865. pc.IC_KD_KH_KW = IC*KD*KH*KW;
  8866. pc.N_OD_OH = N*OD*OH;
  8867. pc.OD_OH = OD*OH;
  8868. pc.OD_OH_OW_IC_KD_KH_KW = OD*OH*OW*IC*KD*KH*KW;
  8869. pc.OH_OW_IC_KD_KH_KW = OH*OW*IC*KD*KH*KW;
  8870. pc.OW_IC_KD_KH_KW = OW*IC*KD*KH*KW;
  8871. ggml_vk_op_f32<vk_op_im2col_3d_push_constants>(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_IM2COL_3D, std::move(pc));
  8872. }
  8873. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8874. const uint32_t dim = dst->op_params[0];
  8875. const uint32_t max_period = dst->op_params[1];
  8876. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  8877. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  8878. nb1, dim, max_period,
  8879. });
  8880. }
  8881. 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) {
  8882. // src0: (K, Cout, Cin, 1) -- kernel
  8883. // src1: (L, Cin, 1, 1) -- input
  8884. // dst: (*, Cout, 1, 1)
  8885. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8886. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8887. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8888. GGML_TENSOR_BINARY_OP_LOCALS
  8889. GGML_ASSERT(nb00 == sizeof(float));
  8890. GGML_ASSERT(nb10 == sizeof(float));
  8891. const int32_t s0 = dst->op_params[0];
  8892. vk_op_conv_transpose_1d_push_constants p{};
  8893. p.Cout = static_cast<uint32_t>(ne01);
  8894. p.Cin = static_cast<uint32_t>(ne02);
  8895. p.K = static_cast<uint32_t>(ne00);
  8896. p.L = static_cast<uint32_t>(ne10);
  8897. p.KL = static_cast<uint32_t>(ne0);
  8898. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  8899. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  8900. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  8901. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  8902. p.s0 = static_cast<uint32_t>(s0);
  8903. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p));
  8904. }
  8905. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  8906. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  8907. const int32_t k1 = dst->op_params[1];
  8908. const int32_t k0 = dst->op_params[2];
  8909. const int32_t s1 = dst->op_params[3];
  8910. const int32_t s0 = dst->op_params[4];
  8911. const int32_t p1 = dst->op_params[5];
  8912. const int32_t p0 = dst->op_params[6];
  8913. const uint32_t IH = src0->ne[1];
  8914. const uint32_t IW = src0->ne[0];
  8915. const uint32_t N = dst->ne[3];
  8916. const uint32_t OC = dst->ne[2];
  8917. const uint32_t OH = dst->ne[1];
  8918. const uint32_t OW = dst->ne[0];
  8919. const uint32_t parallel_elements = N * OC * OH * OW;
  8920. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  8921. IW, IH, OW, OH, OC,
  8922. parallel_elements,
  8923. op,
  8924. k0, k1, s0, s1, p0, p1,
  8925. });
  8926. }
  8927. static void ggml_vk_conv_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  8928. const ggml_tensor * src1, ggml_tensor * dst) {
  8929. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  8930. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8931. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  8932. GGML_TENSOR_BINARY_OP_LOCALS
  8933. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  8934. GGML_ASSERT(nb10 == sizeof(float));
  8935. GGML_ASSERT(nb0 == sizeof(float));
  8936. vk_op_conv2d_push_constants p{};
  8937. p.Cout = static_cast<uint32_t>(ne03);
  8938. p.Cin = static_cast<uint32_t>(ne02);
  8939. p.N = static_cast<uint32_t>(ne13);
  8940. p.KW = static_cast<uint32_t>(ne00);
  8941. p.KH = static_cast<uint32_t>(ne01);
  8942. p.W = static_cast<uint32_t>(ne10);
  8943. p.H = static_cast<uint32_t>(ne11);
  8944. p.OW = static_cast<uint32_t>(ne0);
  8945. p.OH = static_cast<uint32_t>(ne1);
  8946. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  8947. p.s1 = static_cast<uint32_t>(dst->op_params[1]);
  8948. p.p0 = static_cast<uint32_t>(dst->op_params[2]);
  8949. p.p1 = static_cast<uint32_t>(dst->op_params[3]);
  8950. p.d0 = static_cast<uint32_t>(dst->op_params[4]);
  8951. p.d1 = static_cast<uint32_t>(dst->op_params[5]);
  8952. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  8953. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  8954. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  8955. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  8956. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  8957. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  8958. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  8959. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  8960. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  8961. GGML_ASSERT(ne03 == ne2);
  8962. GGML_ASSERT(ne02 == ne12);
  8963. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_CONV_2D, std::move(p));
  8964. }
  8965. static void ggml_vk_conv_transpose_2d(ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src0,
  8966. const ggml_tensor * src1, ggml_tensor * dst) {
  8967. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  8968. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8969. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  8970. GGML_TENSOR_BINARY_OP_LOCALS
  8971. GGML_ASSERT(nb00 == sizeof(float) || nb00 == sizeof(ggml_fp16_t));
  8972. GGML_ASSERT(nb10 == sizeof(float));
  8973. GGML_ASSERT(nb0 == sizeof(float));
  8974. vk_op_conv_transpose_2d_push_constants p{};
  8975. p.Cout = static_cast<uint32_t>(ne02);
  8976. p.Cin = static_cast<uint32_t>(ne03);
  8977. p.N = static_cast<uint32_t>(ne13);
  8978. p.KW = static_cast<uint32_t>(ne00);
  8979. p.KH = static_cast<uint32_t>(ne01);
  8980. p.W = static_cast<uint32_t>(ne10);
  8981. p.H = static_cast<uint32_t>(ne11);
  8982. p.OW = static_cast<uint32_t>(ne0);
  8983. p.OH = static_cast<uint32_t>(ne1);
  8984. p.s0 = static_cast<uint32_t>(dst->op_params[0]);
  8985. p.s1 = static_cast<uint32_t>(dst->op_params[0]);
  8986. p.p0 = 0;
  8987. p.p1 = 0;
  8988. p.d0 = 1;
  8989. p.d1 = 1;
  8990. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  8991. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  8992. p.nb03 = static_cast<uint32_t>(nb03 / nb00);
  8993. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  8994. p.nb12 = static_cast<uint32_t>(nb12 / nb10);
  8995. p.nb13 = static_cast<uint32_t>(nb13 / nb10);
  8996. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  8997. p.nb2 = static_cast<uint32_t>(nb2 / nb0);
  8998. p.nb3 = static_cast<uint32_t>(nb3 / nb0);
  8999. GGML_ASSERT(ne02 == ne2);
  9000. GGML_ASSERT(ne03 == ne12);
  9001. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_CONV_TRANSPOSE_2D, std::move(p));
  9002. }
  9003. 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) {
  9004. vk_op_conv2d_dw_push_constants p{};
  9005. p.ne = ggml_nelements(dst);
  9006. p.channels = dst->ne[2];
  9007. p.batches = dst->ne[3];
  9008. p.dst_w = dst->ne[0];
  9009. p.dst_h = dst->ne[1];
  9010. p.src_w = src1->ne[0];
  9011. p.src_h = src1->ne[1];
  9012. p.knl_w = src0->ne[0];
  9013. p.knl_h = src0->ne[1];
  9014. p.stride_x = dst->op_params[0];
  9015. p.stride_y = dst->op_params[1];
  9016. p.pad_x = dst->op_params[2];
  9017. p.pad_y = dst->op_params[3];
  9018. p.dilation_x = dst->op_params[4];
  9019. p.dilation_y = dst->op_params[5];
  9020. GGML_ASSERT(src0->ne[3] == p.channels);
  9021. GGML_ASSERT(src1->ne[3] == p.batches);
  9022. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p));
  9023. }
  9024. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  9025. const float * op_params = (const float *)dst->op_params;
  9026. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, nullptr, dst, GGML_OP_LEAKY_RELU, { (uint32_t)ggml_nelements(src0), 0, op_params[0], 0.0f });
  9027. }
  9028. #ifdef GGML_VULKAN_RUN_TESTS
  9029. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  9030. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  9031. return;
  9032. }
  9033. i0 = std::max(i0, 5);
  9034. i1 = std::max(i1, 5);
  9035. i2 = std::max(i2, 0);
  9036. fprintf(stderr, " ");
  9037. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9038. fprintf(stderr, "%7d ", idx1);
  9039. }
  9040. fprintf(stderr, "\n");
  9041. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9042. fprintf(stderr, "%7d: ", idx0);
  9043. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9044. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  9045. float val;
  9046. if (type == GGML_TYPE_F32) {
  9047. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  9048. } else if (type == GGML_TYPE_F16) {
  9049. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  9050. } else {
  9051. GGML_ABORT("fatal error");
  9052. }
  9053. fprintf(stderr, "% 7.2f ", val);
  9054. } else {
  9055. fprintf(stderr, " ");
  9056. }
  9057. }
  9058. fprintf(stderr, "\n");
  9059. }
  9060. }
  9061. template <typename X_TYPE, typename Y_TYPE>
  9062. 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) {
  9063. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  9064. const size_t x_ne = m * k * batch;
  9065. const size_t y_ne = k * n * batch;
  9066. const size_t d_ne = m * n * batch;
  9067. vk_pipeline p;
  9068. std::string shname;
  9069. if (shader_size == 0) {
  9070. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9071. p = ctx->device->pipeline_matmul_f32->a_s;
  9072. shname = "F32_ALIGNED_S";
  9073. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9074. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  9075. shname = "F32_F16_ALIGNED_S";
  9076. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9077. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  9078. shname = "F16_F32_ALIGNED_S";
  9079. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9080. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  9081. shname = "F16_ALIGNED_S";
  9082. } else {
  9083. GGML_ABORT("fatal error");
  9084. }
  9085. } else if (shader_size == 1) {
  9086. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9087. p = ctx->device->pipeline_matmul_f32->a_m;
  9088. shname = "F32_ALIGNED_M";
  9089. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9090. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  9091. shname = "F32_F16_ALIGNED_M";
  9092. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9093. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  9094. shname = "F16_F32_ALIGNED_M";
  9095. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9096. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  9097. shname = "F16_ALIGNED_M";
  9098. } else {
  9099. GGML_ABORT("fatal error");
  9100. }
  9101. } else if (shader_size == 2) {
  9102. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9103. p = ctx->device->pipeline_matmul_f32->a_l;
  9104. shname = "F32_ALIGNED_L";
  9105. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9106. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  9107. shname = "F32_F16_ALIGNED_L";
  9108. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9109. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  9110. shname = "F16_F32_ALIGNED_L";
  9111. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9112. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  9113. shname = "F16_ALIGNED_L";
  9114. } else {
  9115. GGML_ABORT("fatal error");
  9116. }
  9117. } else {
  9118. GGML_ASSERT(0);
  9119. }
  9120. const size_t kpad = ggml_vk_align_size(k, p->align);
  9121. if (k != kpad) {
  9122. if (shader_size == 0) {
  9123. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9124. p = ctx->device->pipeline_matmul_f32->s;
  9125. shname = "F32_S";
  9126. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9127. p = ctx->device->pipeline_matmul_f32_f16->s;
  9128. shname = "F32_F16_S";
  9129. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9130. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  9131. shname = "F16_F32_S";
  9132. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9133. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  9134. shname = "F16_S";
  9135. }
  9136. } else if (shader_size == 1) {
  9137. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9138. p = ctx->device->pipeline_matmul_f32->m;
  9139. shname = "F32_M";
  9140. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9141. p = ctx->device->pipeline_matmul_f32_f16->m;
  9142. shname = "F32_F16_M";
  9143. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9144. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  9145. shname = "F16_F32_M";
  9146. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9147. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  9148. shname = "F16_M";
  9149. }
  9150. } else if (shader_size == 2) {
  9151. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9152. p = ctx->device->pipeline_matmul_f32->l;
  9153. shname = "F32_L";
  9154. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9155. p = ctx->device->pipeline_matmul_f32_f16->l;
  9156. shname = "F32_F16_L";
  9157. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  9158. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  9159. shname = "F16_F32_L";
  9160. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9161. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  9162. shname = "F16_L";
  9163. }
  9164. }
  9165. }
  9166. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  9167. if (split_k > 1) {
  9168. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  9169. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  9170. // Resize buffer
  9171. if (ctx->prealloc_split_k != nullptr) {
  9172. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  9173. }
  9174. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9175. }
  9176. }
  9177. ggml_pipeline_allocate_descriptor_sets(ctx);
  9178. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9179. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9180. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9181. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  9182. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  9183. float* d = (float *) malloc(sizeof(float) * d_ne);
  9184. for (size_t i = 0; i < x_ne; i++) {
  9185. if (std::is_same<float, X_TYPE>()) {
  9186. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  9187. // x[i] = 1.0f;
  9188. // x[i] = i + 1;
  9189. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  9190. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  9191. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  9192. // x[i] = ggml_fp32_to_fp16(1.0f);
  9193. // x[i] = ggml_fp32_to_fp16(i + 1);
  9194. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  9195. } else {
  9196. GGML_ABORT("fatal error");
  9197. }
  9198. }
  9199. for (size_t i = 0; i < y_ne; i++) {
  9200. if (std::is_same<float, Y_TYPE>()) {
  9201. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  9202. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  9203. // y[i] = i + 1;
  9204. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9205. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  9206. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  9207. // y[i] = ggml_fp32_to_fp16(i + 1);
  9208. } else {
  9209. GGML_ABORT("fatal error");
  9210. }
  9211. }
  9212. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  9213. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  9214. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9215. ggml_vk_ctx_begin(ctx->device, subctx);
  9216. for (size_t i = 0; i < num_it; i++) {
  9217. ggml_vk_matmul(
  9218. ctx, subctx, p, ggml_vk_subbuffer(ctx, d_X), ggml_vk_subbuffer(ctx, d_Y), ggml_vk_subbuffer(ctx, d_D), ggml_vk_subbuffer(ctx, ctx->prealloc_split_k),
  9219. m, n, k,
  9220. k, k, m, k*m, k*n, m*n,
  9221. split_k, batch, batch, batch, 1, 1, n
  9222. );
  9223. }
  9224. ggml_vk_ctx_end(subctx);
  9225. auto begin = std::chrono::high_resolution_clock::now();
  9226. ggml_vk_submit(subctx, ctx->fence);
  9227. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  9228. ctx->device->device.resetFences({ ctx->fence });
  9229. ggml_vk_queue_command_pools_cleanup(ctx->device);
  9230. auto end = std::chrono::high_resolution_clock::now();
  9231. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9232. // copy dst to host
  9233. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  9234. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  9235. ggml_init_params iparams = {
  9236. /*.mem_size =*/ 1024*1024*1024,
  9237. /*.mem_buffer =*/ NULL,
  9238. /*.no_alloc =*/ true,
  9239. };
  9240. ggml_context * ggml_ctx = ggml_init(iparams);
  9241. ggml_type src0_type;
  9242. ggml_type src1_type;
  9243. if (std::is_same<float, X_TYPE>()) {
  9244. src0_type = GGML_TYPE_F32;
  9245. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  9246. src0_type = GGML_TYPE_F16;
  9247. } else {
  9248. GGML_ABORT("fatal error");
  9249. }
  9250. if (std::is_same<float, Y_TYPE>()) {
  9251. src1_type = GGML_TYPE_F32;
  9252. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  9253. src1_type = GGML_TYPE_F16;
  9254. } else {
  9255. GGML_ABORT("fatal error");
  9256. }
  9257. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  9258. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  9259. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  9260. src0_ggml->data = x;
  9261. src1_ggml->data = y;
  9262. tensor_ggml->data = d_chk;
  9263. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  9264. ggml_build_forward_expand(cgraph, tensor_ggml);
  9265. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  9266. ggml_free(ggml_ctx);
  9267. double avg_err = 0.0;
  9268. int first_err_n = -1;
  9269. int first_err_m = -1;
  9270. int first_err_b = -1;
  9271. for (size_t i = 0; i < m*n*batch; i++) {
  9272. double err = std::fabs(d[i] - d_chk[i]);
  9273. avg_err += err;
  9274. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  9275. first_err_b = i / (m * n);
  9276. first_err_n = (i % (m * n)) / m;
  9277. first_err_m = (i % (m * n)) % m;
  9278. }
  9279. }
  9280. avg_err /= m * n;
  9281. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  9282. 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;
  9283. if (avg_err > 0.1 || std::isnan(avg_err)) {
  9284. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  9285. std::cerr << "Actual result: " << std::endl << std::endl;
  9286. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9287. std::cerr << "Expected result: " << std::endl << std::endl;
  9288. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9289. if (split_k > 1) {
  9290. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  9291. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  9292. std::cerr << "d_buf0: " << std::endl << std::endl;
  9293. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9294. std::cerr << "d_buf1: " << std::endl << std::endl;
  9295. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9296. std::cerr << "d_buf2: " << std::endl << std::endl;
  9297. 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);
  9298. std::cerr << "d_buf3: " << std::endl << std::endl;
  9299. 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);
  9300. free(split_k_buf);
  9301. }
  9302. }
  9303. free(d_chk);
  9304. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  9305. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  9306. ggml_vk_destroy_buffer(d_X);
  9307. ggml_vk_destroy_buffer(d_Y);
  9308. ggml_vk_destroy_buffer(d_D);
  9309. free(x);
  9310. free(y);
  9311. free(d);
  9312. }
  9313. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  9314. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  9315. return;
  9316. }
  9317. i0 = std::max(i0, 5);
  9318. i1 = std::max(i1, 5);
  9319. i2 = std::max(i2, 0);
  9320. i3 = std::max(i3, 0);
  9321. fprintf(stderr, " ");
  9322. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9323. fprintf(stderr, "%7d ", idx1);
  9324. }
  9325. fprintf(stderr, "\n");
  9326. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9327. fprintf(stderr, "%7d: ", idx0);
  9328. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9329. 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]) {
  9330. float val;
  9331. if (tensor->type == GGML_TYPE_F32) {
  9332. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9333. } else if (tensor->type == GGML_TYPE_F16) {
  9334. 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]));
  9335. } else {
  9336. GGML_ABORT("fatal error");
  9337. }
  9338. fprintf(stderr, "% 7.2f ", val);
  9339. } else {
  9340. fprintf(stderr, " ");
  9341. }
  9342. }
  9343. fprintf(stderr, "\n");
  9344. }
  9345. }
  9346. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  9347. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  9348. }
  9349. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  9350. if (quant == GGML_TYPE_F32) {
  9351. memcpy(to, from, sizeof(float) * ne);
  9352. return;
  9353. }
  9354. const auto * tt = ggml_get_type_traits(quant);
  9355. ggml_to_float_t dequant_fn = tt->to_float;
  9356. dequant_fn(from, to, ne);
  9357. }
  9358. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  9359. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  9360. const size_t x_sz = sizeof(float) * ne;
  9361. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  9362. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  9363. float * x = (float *) malloc(x_sz);
  9364. void * qx = malloc(qx_sz);
  9365. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9366. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9367. float * x_ref = (float *) malloc(x_sz);
  9368. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  9369. for (size_t i = 0; i < ne; i++) {
  9370. x[i] = rand() / (float)RAND_MAX;
  9371. }
  9372. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  9373. ggml_vk_quantize_data(x, qx, ne, quant);
  9374. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  9375. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  9376. ggml_pipeline_allocate_descriptor_sets(ctx);
  9377. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  9378. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9379. ggml_vk_ctx_begin(ctx->device, subctx);
  9380. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  9381. 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});
  9382. ggml_vk_ctx_end(subctx);
  9383. auto begin = std::chrono::high_resolution_clock::now();
  9384. ggml_vk_submit(subctx, ctx->fence);
  9385. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  9386. ctx->device->device.resetFences({ ctx->fence });
  9387. ggml_vk_queue_command_pools_cleanup(ctx->device);
  9388. auto end = std::chrono::high_resolution_clock::now();
  9389. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9390. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  9391. int first_err = -1;
  9392. double avg_err = 0.0;
  9393. for (size_t i = 0; i < ne; i++) {
  9394. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  9395. avg_err += error;
  9396. if (first_err < 0 && error > 0.05) {
  9397. first_err = i;
  9398. }
  9399. }
  9400. avg_err /= ne;
  9401. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  9402. if (avg_err > 0.1) {
  9403. std::cerr << "first_error = " << first_err << std::endl;
  9404. std::cerr << "Actual result: " << std::endl << std::endl;
  9405. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  9406. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  9407. }
  9408. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  9409. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  9410. std::cerr << x_ref[i] << ", ";
  9411. }
  9412. std::cerr << std::endl;
  9413. }
  9414. ggml_vk_destroy_buffer(x_buf);
  9415. ggml_vk_destroy_buffer(qx_buf);
  9416. free(x);
  9417. free(qx);
  9418. free(x_ref);
  9419. free(x_chk);
  9420. }
  9421. // This does not work without ggml q8_1 quantization support
  9422. //
  9423. // typedef uint16_t ggml_half;
  9424. // typedef uint32_t ggml_half2;
  9425. //
  9426. // #define QK8_1 32
  9427. // typedef struct {
  9428. // union {
  9429. // struct {
  9430. // ggml_half d; // delta
  9431. // ggml_half s; // d * sum(qs[i])
  9432. // } GGML_COMMON_AGGR_S;
  9433. // ggml_half2 ds;
  9434. // } GGML_COMMON_AGGR_U;
  9435. // int8_t qs[QK8_1]; // quants
  9436. // } block_q8_1;
  9437. //
  9438. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  9439. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  9440. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  9441. //
  9442. // const size_t x_sz = sizeof(float) * ne;
  9443. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  9444. // float * x = (float *) malloc(x_sz);
  9445. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  9446. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  9447. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9448. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9449. //
  9450. // for (size_t i = 0; i < ne; i++) {
  9451. // x[i] = rand() / (float)RAND_MAX;
  9452. // }
  9453. //
  9454. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  9455. //
  9456. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  9457. //
  9458. // ggml_pipeline_allocate_descriptor_sets(ctx);
  9459. //
  9460. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  9461. //
  9462. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9463. // ggml_vk_ctx_begin(ctx->device, subctx);
  9464. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(ctx, x_buf), ggml_vk_subbuffer(ctx, qx_buf), ne);
  9465. // ggml_vk_ctx_end(subctx);
  9466. //
  9467. // auto begin = std::chrono::high_resolution_clock::now();
  9468. //
  9469. // ggml_vk_submit(subctx, ctx->fence);
  9470. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  9471. // ctx->device->device.resetFences({ ctx->fence });
  9472. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  9473. //
  9474. // auto end = std::chrono::high_resolution_clock::now();
  9475. //
  9476. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9477. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  9478. //
  9479. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  9480. //
  9481. // int first_err = -1;
  9482. //
  9483. // for (size_t i = 0; i < ne / 32; i++) {
  9484. // 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));
  9485. //
  9486. // if (first_err < 0 && error > 0.1) {
  9487. // first_err = i;
  9488. // }
  9489. //
  9490. // 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));
  9491. //
  9492. // if (first_err < 0 && error > 0.1) {
  9493. // first_err = i;
  9494. // }
  9495. //
  9496. // for (size_t j = 0; j < 32; j++) {
  9497. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  9498. //
  9499. // if (first_err < 0 && error > 1) {
  9500. // first_err = i;
  9501. // }
  9502. // }
  9503. // }
  9504. //
  9505. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  9506. //
  9507. // if (first_err != -1) {
  9508. // std::cerr << "first_error = " << first_err << std::endl;
  9509. // std::cerr << "Actual result: " << std::endl << std::endl;
  9510. // 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) << " ";
  9511. // for (size_t j = 0; j < 32; j++) {
  9512. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  9513. // }
  9514. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  9515. // 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) << " ";
  9516. // for (size_t j = 0; j < 32; j++) {
  9517. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  9518. // }
  9519. // std::cerr << std::endl;
  9520. // }
  9521. //
  9522. // ggml_vk_destroy_buffer(x_buf);
  9523. // ggml_vk_destroy_buffer(qx_buf);
  9524. //
  9525. // free(x);
  9526. // free(qx);
  9527. // free(qx_res);
  9528. // }
  9529. 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) {
  9530. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  9531. const size_t x_ne = m * k * batch;
  9532. const size_t y_ne = k * n * batch;
  9533. const size_t d_ne = m * n * batch;
  9534. vk_matmul_pipeline2 * pipelines;
  9535. if (mmq) {
  9536. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  9537. } else {
  9538. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  9539. }
  9540. const bool fp16acc = ctx->device->fp16;
  9541. vk_pipeline p;
  9542. std::string shname;
  9543. if (shader_size == 0) {
  9544. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  9545. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  9546. } else if (shader_size == 1) {
  9547. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  9548. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  9549. } else if (shader_size == 2) {
  9550. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  9551. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  9552. } else {
  9553. GGML_ASSERT(0);
  9554. }
  9555. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  9556. if (mmq || k != kpad) {
  9557. if (shader_size == 0) {
  9558. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  9559. shname = std::string(ggml_type_name(quant)) + "_S";
  9560. } else if (shader_size == 1) {
  9561. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  9562. shname = std::string(ggml_type_name(quant)) + "_M";
  9563. } else if (shader_size == 2) {
  9564. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  9565. shname = std::string(ggml_type_name(quant)) + "_L";
  9566. } else {
  9567. GGML_ASSERT(0);
  9568. }
  9569. }
  9570. if (p == nullptr) {
  9571. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  9572. return;
  9573. }
  9574. const size_t x_sz = sizeof(float) * x_ne;
  9575. const size_t y_sz = sizeof(float) * y_ne;
  9576. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  9577. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  9578. const size_t d_sz = sizeof(float) * d_ne;
  9579. float * x = (float *) malloc(x_sz);
  9580. float * y = (float *) malloc(y_sz);
  9581. void * qx = malloc(qx_sz);
  9582. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9583. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9584. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9585. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9586. float * d = (float *) malloc(d_sz);
  9587. float * d_chk = (float *) malloc(d_sz);
  9588. for (size_t i = 0; i < x_ne; i++) {
  9589. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  9590. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  9591. // x[i] = i % k;
  9592. }
  9593. ggml_vk_quantize_data(x, qx, x_ne, quant);
  9594. for (size_t i = 0; i < y_ne; i++) {
  9595. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  9596. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  9597. // y[i] = i % k;
  9598. }
  9599. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  9600. if (split_k > 1) {
  9601. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  9602. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  9603. // Resize buffer
  9604. if (ctx->prealloc_split_k != nullptr) {
  9605. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  9606. }
  9607. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, {vk::MemoryPropertyFlagBits::eDeviceLocal});
  9608. }
  9609. }
  9610. if (mmq) {
  9611. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  9612. }
  9613. ggml_pipeline_allocate_descriptor_sets(ctx);
  9614. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  9615. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  9616. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9617. ggml_vk_ctx_begin(ctx->device, subctx);
  9618. if (mmq) {
  9619. for (size_t i = 0; i < num_it; i++) {
  9620. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  9621. ggml_vk_matmul(
  9622. 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 },
  9623. m, n, k,
  9624. k, k, m, k*m, k*n, m*n,
  9625. split_k, batch, batch, batch, 1, 1, n
  9626. );
  9627. }
  9628. } else {
  9629. for (size_t i = 0; i < num_it; i++) {
  9630. ggml_vk_matmul(
  9631. 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 },
  9632. m, n, k,
  9633. k, k, m, k*m, k*n, m*n,
  9634. split_k, batch, batch, batch, 1, 1, n
  9635. );
  9636. }
  9637. }
  9638. ggml_vk_ctx_end(subctx);
  9639. auto begin = std::chrono::high_resolution_clock::now();
  9640. ggml_vk_submit(subctx, ctx->fence);
  9641. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  9642. ctx->device->device.resetFences({ ctx->fence });
  9643. ggml_vk_queue_command_pools_cleanup(ctx->device);
  9644. auto end = std::chrono::high_resolution_clock::now();
  9645. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  9646. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  9647. ggml_init_params iparams = {
  9648. /*.mem_size =*/ 1024*1024*1024,
  9649. /*.mem_buffer =*/ NULL,
  9650. /*.no_alloc =*/ true,
  9651. };
  9652. ggml_context * ggml_ctx = ggml_init(iparams);
  9653. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  9654. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  9655. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  9656. src0_ggml->data = qx;
  9657. src1_ggml->data = y;
  9658. tensor_ggml->data = d_chk;
  9659. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  9660. ggml_build_forward_expand(cgraph, tensor_ggml);
  9661. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  9662. ggml_free(ggml_ctx);
  9663. double avg_err = 0.0;
  9664. int first_err_n = -1;
  9665. int first_err_m = -1;
  9666. int first_err_b = -1;
  9667. for (size_t i = 0; i < m*n*batch; i++) {
  9668. double err = std::fabs(d[i] - d_chk[i]);
  9669. avg_err += err;
  9670. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  9671. first_err_b = i / (m * n);
  9672. first_err_n = (i % (m * n)) / m;
  9673. first_err_m = (i % (m * n)) % m;
  9674. }
  9675. }
  9676. avg_err /= m * n;
  9677. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  9678. std::cerr << "TEST dequant matmul " << shname;
  9679. if (mmq) {
  9680. std::cerr << " mmq";
  9681. }
  9682. 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;
  9683. if (avg_err > 0.01 || std::isnan(avg_err)) {
  9684. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  9685. std::cerr << "Actual result: " << std::endl << std::endl;
  9686. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9687. std::cerr << std::endl;
  9688. std::cerr << "Expected result: " << std::endl << std::endl;
  9689. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9690. std::cerr << "src0: " << std::endl << std::endl;
  9691. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  9692. std::cerr << std::endl;
  9693. std::cerr << "src1: " << std::endl << std::endl;
  9694. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  9695. if (split_k > 1) {
  9696. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  9697. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  9698. std::cerr << "d_buf0: " << std::endl << std::endl;
  9699. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9700. std::cerr << "d_buf1: " << std::endl << std::endl;
  9701. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  9702. std::cerr << "d_buf2: " << std::endl << std::endl;
  9703. 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);
  9704. std::cerr << "d_buf3: " << std::endl << std::endl;
  9705. 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);
  9706. free(split_k_buf);
  9707. }
  9708. }
  9709. ggml_vk_destroy_buffer(qx_buf);
  9710. ggml_vk_destroy_buffer(y_buf);
  9711. ggml_vk_destroy_buffer(qy_buf);
  9712. ggml_vk_destroy_buffer(d_buf);
  9713. free(x);
  9714. free(qx);
  9715. free(y);
  9716. free(d);
  9717. free(d_chk);
  9718. }
  9719. #endif
  9720. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx, vk_context subctx) {
  9721. #if defined(GGML_VULKAN_RUN_TESTS)
  9722. const std::vector<size_t> vals {
  9723. 512, 512, 128,
  9724. 128, 512, 512,
  9725. 4096, 512, 4096,
  9726. 11008, 512, 4096,
  9727. 4096, 512, 11008,
  9728. 32000, 512, 4096,
  9729. 8, 8, 8,
  9730. 100, 46, 576,
  9731. 623, 111, 128,
  9732. 100, 46, 558,
  9733. 512, 1, 256,
  9734. 128, 110, 622,
  9735. 511, 511, 127,
  9736. 511, 511, 7,
  9737. 511, 511, 17,
  9738. 49, 49, 128,
  9739. 128, 49, 49,
  9740. 4096, 49, 4096,
  9741. };
  9742. const size_t num_it = 100;
  9743. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  9744. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  9745. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  9746. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  9747. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  9748. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  9749. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  9750. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  9751. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  9752. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  9753. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  9754. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  9755. abort();
  9756. for (size_t i = 0; i < vals.size(); i += 3) {
  9757. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  9758. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  9759. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  9760. std::cerr << '\n';
  9761. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  9762. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  9763. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  9764. std::cerr << '\n';
  9765. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  9766. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  9767. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  9768. std::cerr << '\n' << std::endl;
  9769. if (vals[i + 2] % 32 == 0) {
  9770. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  9771. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  9772. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  9773. std::cerr << '\n';
  9774. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  9775. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  9776. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  9777. std::cerr << '\n';
  9778. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  9779. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  9780. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  9781. std::cerr << '\n' << std::endl;
  9782. }
  9783. if (vals[i + 2] % 256 == 0) {
  9784. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  9785. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  9786. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  9787. std::cerr << '\n';
  9788. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  9789. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  9790. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  9791. std::cerr << '\n';
  9792. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  9793. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  9794. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  9795. std::cerr << '\n' << std::endl;
  9796. }
  9797. }
  9798. GGML_ABORT("fatal error");
  9799. #endif
  9800. if (subctx) {
  9801. // Submit and wait for any pending work before reallocating the buffers
  9802. ggml_vk_ctx_end(subctx);
  9803. ggml_vk_submit(subctx, ctx->fence);
  9804. ggml_vk_wait_for_fence(ctx);
  9805. ggml_vk_ctx_begin(ctx->device, subctx);
  9806. }
  9807. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  9808. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  9809. // Resize buffer
  9810. if (ctx->prealloc_x != nullptr) {
  9811. ggml_vk_destroy_buffer(ctx->prealloc_x);
  9812. }
  9813. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  9814. }
  9815. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  9816. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  9817. // Resize buffer
  9818. if (ctx->prealloc_y != nullptr) {
  9819. ggml_vk_destroy_buffer(ctx->prealloc_y);
  9820. }
  9821. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  9822. }
  9823. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  9824. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  9825. // Resize buffer
  9826. if (ctx->prealloc_split_k != nullptr) {
  9827. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  9828. }
  9829. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  9830. }
  9831. if (ctx->prealloc_add_rms_partials == nullptr || (ctx->prealloc_size_add_rms_partials > 0 && ctx->prealloc_add_rms_partials->size < ctx->prealloc_size_add_rms_partials)) {
  9832. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(add_partials_size: " << ctx->prealloc_add_rms_partials << ")");
  9833. // Resize buffer
  9834. if (ctx->prealloc_add_rms_partials != nullptr) {
  9835. ggml_vk_destroy_buffer(ctx->prealloc_add_rms_partials);
  9836. }
  9837. ctx->prealloc_add_rms_partials = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_add_rms_partials);
  9838. }
  9839. }
  9840. 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);
  9841. // Returns true if node has enqueued work into the queue, false otherwise
  9842. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  9843. 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 last_node, bool almost_ready, bool submit){
  9844. ggml_tensor * node = cgraph->nodes[node_idx];
  9845. if (ggml_is_empty(node) || !node->buffer) {
  9846. return false;
  9847. }
  9848. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  9849. ctx->semaphore_idx = 0;
  9850. ggml_tensor * src0 = node->src[0];
  9851. ggml_tensor * src1 = node->src[1];
  9852. ggml_tensor * src2 = node->src[2];
  9853. ggml_tensor * src3 = node->src[3];
  9854. switch (node->op) {
  9855. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  9856. case GGML_OP_RESHAPE:
  9857. case GGML_OP_VIEW:
  9858. case GGML_OP_PERMUTE:
  9859. case GGML_OP_TRANSPOSE:
  9860. case GGML_OP_NONE:
  9861. return false;
  9862. case GGML_OP_UNARY:
  9863. switch (ggml_get_unary_op(node)) {
  9864. case GGML_UNARY_OP_EXP:
  9865. case GGML_UNARY_OP_SILU:
  9866. case GGML_UNARY_OP_GELU:
  9867. case GGML_UNARY_OP_GELU_ERF:
  9868. case GGML_UNARY_OP_GELU_QUICK:
  9869. case GGML_UNARY_OP_RELU:
  9870. case GGML_UNARY_OP_TANH:
  9871. case GGML_UNARY_OP_SIGMOID:
  9872. case GGML_UNARY_OP_HARDSIGMOID:
  9873. case GGML_UNARY_OP_HARDSWISH:
  9874. break;
  9875. default:
  9876. return false;
  9877. }
  9878. break;
  9879. case GGML_OP_GLU:
  9880. switch (ggml_get_glu_op(node)) {
  9881. case GGML_GLU_OP_GEGLU:
  9882. case GGML_GLU_OP_REGLU:
  9883. case GGML_GLU_OP_SWIGLU:
  9884. case GGML_GLU_OP_SWIGLU_OAI:
  9885. case GGML_GLU_OP_GEGLU_ERF:
  9886. case GGML_GLU_OP_GEGLU_QUICK:
  9887. break;
  9888. default:
  9889. return false;
  9890. }
  9891. break;
  9892. case GGML_OP_ADD:
  9893. {
  9894. int next_node_idx = node_idx + 1 + ctx->num_additional_fused_ops;
  9895. if (next_node_idx < cgraph->n_nodes &&
  9896. cgraph->nodes[next_node_idx]->op == GGML_OP_RMS_NORM &&
  9897. cgraph->nodes[next_node_idx]->src[0] == cgraph->nodes[next_node_idx - 1] &&
  9898. ggml_nrows(cgraph->nodes[next_node_idx]) == 1 &&
  9899. ctx->device->add_rms_fusion) {
  9900. uint32_t size = ggml_vk_rms_partials_size(ctx, cgraph->nodes[node_idx]);
  9901. ctx->do_add_rms_partials_offset_calculation = true;
  9902. if (ctx->prealloc_size_add_rms_partials_offset + size <= ctx->prealloc_size_add_rms_partials) {
  9903. ctx->do_add_rms_partials = true;
  9904. }
  9905. }
  9906. } break;
  9907. case GGML_OP_REPEAT:
  9908. case GGML_OP_REPEAT_BACK:
  9909. case GGML_OP_GET_ROWS:
  9910. case GGML_OP_ADD_ID:
  9911. case GGML_OP_ACC:
  9912. case GGML_OP_SUB:
  9913. case GGML_OP_MUL:
  9914. case GGML_OP_DIV:
  9915. case GGML_OP_CONCAT:
  9916. case GGML_OP_UPSCALE:
  9917. case GGML_OP_SCALE:
  9918. case GGML_OP_SQR:
  9919. case GGML_OP_SQRT:
  9920. case GGML_OP_SIN:
  9921. case GGML_OP_COS:
  9922. case GGML_OP_CLAMP:
  9923. case GGML_OP_PAD:
  9924. case GGML_OP_ROLL:
  9925. case GGML_OP_CPY:
  9926. case GGML_OP_SET_ROWS:
  9927. case GGML_OP_CONT:
  9928. case GGML_OP_DUP:
  9929. case GGML_OP_SILU_BACK:
  9930. case GGML_OP_NORM:
  9931. case GGML_OP_GROUP_NORM:
  9932. case GGML_OP_RMS_NORM:
  9933. case GGML_OP_RMS_NORM_BACK:
  9934. case GGML_OP_L2_NORM:
  9935. case GGML_OP_DIAG_MASK_INF:
  9936. case GGML_OP_SOFT_MAX:
  9937. case GGML_OP_SOFT_MAX_BACK:
  9938. case GGML_OP_ROPE:
  9939. case GGML_OP_ROPE_BACK:
  9940. case GGML_OP_MUL_MAT:
  9941. case GGML_OP_MUL_MAT_ID:
  9942. case GGML_OP_ARGSORT:
  9943. case GGML_OP_SUM:
  9944. case GGML_OP_SUM_ROWS:
  9945. case GGML_OP_MEAN:
  9946. case GGML_OP_ARGMAX:
  9947. case GGML_OP_COUNT_EQUAL:
  9948. case GGML_OP_IM2COL:
  9949. case GGML_OP_IM2COL_3D:
  9950. case GGML_OP_TIMESTEP_EMBEDDING:
  9951. case GGML_OP_CONV_TRANSPOSE_1D:
  9952. case GGML_OP_POOL_2D:
  9953. case GGML_OP_CONV_2D:
  9954. case GGML_OP_CONV_TRANSPOSE_2D:
  9955. case GGML_OP_CONV_2D_DW:
  9956. case GGML_OP_RWKV_WKV6:
  9957. case GGML_OP_RWKV_WKV7:
  9958. case GGML_OP_SSM_SCAN:
  9959. case GGML_OP_SSM_CONV:
  9960. case GGML_OP_LEAKY_RELU:
  9961. case GGML_OP_FLASH_ATTN_EXT:
  9962. case GGML_OP_OPT_STEP_ADAMW:
  9963. case GGML_OP_OPT_STEP_SGD:
  9964. break;
  9965. default:
  9966. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  9967. GGML_ABORT("fatal error");
  9968. }
  9969. vk_context compute_ctx;
  9970. if (ctx->compute_ctx.expired()) {
  9971. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  9972. ctx->compute_ctx = compute_ctx;
  9973. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  9974. } else {
  9975. compute_ctx = ctx->compute_ctx.lock();
  9976. }
  9977. {
  9978. // This logic detects dependencies between modes in the graph and calls ggml_vk_sync_buffers
  9979. // to synchronize them. This handles most "normal" synchronization when computing the graph, and when
  9980. // there is no auxiliary memory use, it shouldn't be necessary to call ggml_vk_sync_buffers
  9981. // outside of this logic. When a node uses one of the prealloc buffers for something like
  9982. // dequantization or split_k, additional synchronization is needed between those passes.
  9983. bool need_sync = false;
  9984. // Check whether "node" requires synchronization. The node requires synchronization if it
  9985. // overlaps in memory with another unsynchronized node and at least one of them is a write.
  9986. // Destination nodes are checked against both the written/read lists. Source nodes are only
  9987. // checked against the written list. Two nodes overlap in memory if they come from the same
  9988. // buffer and the tensor or view ranges overlap.
  9989. auto const &overlaps_unsynced = [&](const ggml_tensor *node, const std::vector<const ggml_tensor *> &unsynced_nodes) -> bool {
  9990. if (unsynced_nodes.size() == 0) {
  9991. return false;
  9992. }
  9993. auto n_base = vk_tensor_offset(node) + node->view_offs;
  9994. auto n_size = ggml_nbytes(node);
  9995. ggml_backend_vk_buffer_context * a_buf_ctx = (ggml_backend_vk_buffer_context *)node->buffer->context;
  9996. vk_buffer a_buf = a_buf_ctx->dev_buffer;
  9997. for (auto &other : unsynced_nodes) {
  9998. ggml_backend_vk_buffer_context * o_buf_ctx = (ggml_backend_vk_buffer_context *)other->buffer->context;
  9999. vk_buffer o_buf = o_buf_ctx->dev_buffer;
  10000. if (a_buf == o_buf) {
  10001. auto o_base = vk_tensor_offset(other) + other->view_offs;
  10002. auto o_size = ggml_nbytes(other);
  10003. if ((o_base <= n_base && n_base < o_base + o_size) ||
  10004. (n_base <= o_base && o_base < n_base + n_size)) {
  10005. return true;
  10006. }
  10007. }
  10008. }
  10009. return false;
  10010. };
  10011. // For all fused ops, check if the destination node or any of the source
  10012. // nodes require synchronization.
  10013. for (int32_t i = 0; i < ctx->num_additional_fused_ops + 1 && !need_sync; ++i) {
  10014. const ggml_tensor *cur_node = cgraph->nodes[node_idx + i];
  10015. // If the node actually writes to memory, then check if it needs to sync
  10016. if (ctx->fused_ops_write_mask & (1 << i)) {
  10017. if (overlaps_unsynced(cur_node, ctx->unsynced_nodes_read) || overlaps_unsynced(cur_node, ctx->unsynced_nodes_written)) {
  10018. need_sync = true;
  10019. break;
  10020. }
  10021. }
  10022. for (uint32_t j = 0; j < GGML_MAX_SRC; ++j) {
  10023. if (!cur_node->src[j]) {
  10024. continue;
  10025. }
  10026. if (overlaps_unsynced(cur_node->src[j], ctx->unsynced_nodes_written)) {
  10027. need_sync = true;
  10028. break;
  10029. }
  10030. }
  10031. }
  10032. #define ENABLE_SYNC_LOGGING 0
  10033. if (need_sync) {
  10034. #if ENABLE_SYNC_LOGGING
  10035. std::cerr << "sync" << std::endl;
  10036. #endif
  10037. ctx->unsynced_nodes_written.clear();
  10038. ctx->unsynced_nodes_read.clear();
  10039. ggml_vk_sync_buffers(ctx, compute_ctx);
  10040. }
  10041. // Add all fused nodes to the unsynchronized lists.
  10042. for (int32_t i = 0; i < ctx->num_additional_fused_ops + 1; ++i) {
  10043. const ggml_tensor *cur_node = cgraph->nodes[node_idx + i];
  10044. // Multiple outputs could be written, e.g. in topk_moe. Add them all to the list.
  10045. if (ctx->fused_ops_write_mask & (1 << i)) {
  10046. ctx->unsynced_nodes_written.push_back(cur_node);
  10047. }
  10048. for (uint32_t j = 0; j < GGML_MAX_SRC; ++j) {
  10049. if (!cur_node->src[j]) {
  10050. continue;
  10051. }
  10052. ctx->unsynced_nodes_read.push_back(cur_node->src[j]);
  10053. }
  10054. }
  10055. }
  10056. #if ENABLE_SYNC_LOGGING
  10057. for (int i = 0; i < ctx->num_additional_fused_ops + 1; ++i) {
  10058. auto *n = cgraph->nodes[node_idx + i];
  10059. std::cerr << node_idx + i << " " << ggml_op_name(n->op) << " " << n->name;
  10060. if (n->op == GGML_OP_GLU) {
  10061. std::cerr << " " << ggml_glu_op_name(ggml_get_glu_op(n)) << " " << (n->src[1] ? "split" : "single") << " ";
  10062. }
  10063. std::cerr << std::endl;
  10064. }
  10065. #endif
  10066. switch (node->op) {
  10067. case GGML_OP_REPEAT:
  10068. ggml_vk_repeat(ctx, compute_ctx, src0, node);
  10069. break;
  10070. case GGML_OP_REPEAT_BACK:
  10071. ggml_vk_repeat_back(ctx, compute_ctx, src0, node);
  10072. break;
  10073. case GGML_OP_ACC:
  10074. ggml_vk_acc(ctx, compute_ctx, src0, src1, node);
  10075. break;
  10076. case GGML_OP_GET_ROWS:
  10077. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node);
  10078. break;
  10079. case GGML_OP_ADD:
  10080. if (ctx->num_additional_fused_ops) {
  10081. ggml_vk_multi_add(ctx, compute_ctx, cgraph, node_idx);
  10082. } else {
  10083. ggml_vk_add(ctx, compute_ctx, src0, src1, node);
  10084. }
  10085. break;
  10086. case GGML_OP_SUB:
  10087. ggml_vk_sub(ctx, compute_ctx, src0, src1, node);
  10088. break;
  10089. case GGML_OP_MUL:
  10090. ggml_vk_mul(ctx, compute_ctx, src0, src1, node);
  10091. break;
  10092. case GGML_OP_DIV:
  10093. ggml_vk_div(ctx, compute_ctx, src0, src1, node);
  10094. break;
  10095. case GGML_OP_ADD_ID:
  10096. ggml_vk_add_id(ctx, compute_ctx, src0, src1, src2, node);
  10097. break;
  10098. case GGML_OP_CONCAT:
  10099. ggml_vk_concat(ctx, compute_ctx, src0, src1, node);
  10100. break;
  10101. case GGML_OP_UPSCALE:
  10102. ggml_vk_upscale(ctx, compute_ctx, src0, node);
  10103. break;
  10104. case GGML_OP_SCALE:
  10105. ggml_vk_scale(ctx, compute_ctx, src0, node);
  10106. break;
  10107. case GGML_OP_SQR:
  10108. ggml_vk_sqr(ctx, compute_ctx, src0, node);
  10109. break;
  10110. case GGML_OP_SQRT:
  10111. ggml_vk_sqrt(ctx, compute_ctx, src0, node);
  10112. break;
  10113. case GGML_OP_SIN:
  10114. ggml_vk_sin(ctx, compute_ctx, src0, node);
  10115. break;
  10116. case GGML_OP_COS:
  10117. ggml_vk_cos(ctx, compute_ctx, src0, node);
  10118. break;
  10119. case GGML_OP_CLAMP:
  10120. ggml_vk_clamp(ctx, compute_ctx, src0, node);
  10121. break;
  10122. case GGML_OP_PAD:
  10123. ggml_vk_pad(ctx, compute_ctx, src0, node);
  10124. break;
  10125. case GGML_OP_ROLL:
  10126. ggml_vk_roll(ctx, compute_ctx, src0, node);
  10127. break;
  10128. case GGML_OP_CPY:
  10129. case GGML_OP_CONT:
  10130. case GGML_OP_DUP:
  10131. ggml_vk_cpy(ctx, compute_ctx, src0, node);
  10132. break;
  10133. case GGML_OP_SET_ROWS:
  10134. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node);
  10135. break;
  10136. case GGML_OP_SILU_BACK:
  10137. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node);
  10138. break;
  10139. case GGML_OP_NORM:
  10140. ggml_vk_norm(ctx, compute_ctx, src0, node);
  10141. break;
  10142. case GGML_OP_GROUP_NORM:
  10143. ggml_vk_group_norm(ctx, compute_ctx, src0, node);
  10144. break;
  10145. case GGML_OP_RMS_NORM:
  10146. if (ctx->num_additional_fused_ops > 0) {
  10147. // fused rms_norm + mul
  10148. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  10149. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  10150. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params);
  10151. } else {
  10152. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params);
  10153. }
  10154. break;
  10155. case GGML_OP_RMS_NORM_BACK:
  10156. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node);
  10157. break;
  10158. case GGML_OP_L2_NORM:
  10159. ggml_vk_l2_norm(ctx, compute_ctx, src0, node);
  10160. break;
  10161. case GGML_OP_UNARY:
  10162. switch (ggml_get_unary_op(node)) {
  10163. case GGML_UNARY_OP_EXP:
  10164. case GGML_UNARY_OP_SILU:
  10165. case GGML_UNARY_OP_GELU:
  10166. case GGML_UNARY_OP_GELU_ERF:
  10167. case GGML_UNARY_OP_GELU_QUICK:
  10168. case GGML_UNARY_OP_RELU:
  10169. case GGML_UNARY_OP_TANH:
  10170. case GGML_UNARY_OP_SIGMOID:
  10171. case GGML_UNARY_OP_HARDSIGMOID:
  10172. case GGML_UNARY_OP_HARDSWISH:
  10173. ggml_vk_unary(ctx, compute_ctx, src0, node);
  10174. break;
  10175. default:
  10176. return false;
  10177. }
  10178. break;
  10179. case GGML_OP_GLU:
  10180. switch (ggml_get_glu_op(node)) {
  10181. case GGML_GLU_OP_GEGLU:
  10182. case GGML_GLU_OP_REGLU:
  10183. case GGML_GLU_OP_SWIGLU:
  10184. case GGML_GLU_OP_SWIGLU_OAI:
  10185. case GGML_GLU_OP_GEGLU_ERF:
  10186. case GGML_GLU_OP_GEGLU_QUICK:
  10187. ggml_vk_glu(ctx, compute_ctx, src0, src1, node);
  10188. break;
  10189. default:
  10190. return false;
  10191. }
  10192. break;
  10193. case GGML_OP_DIAG_MASK_INF:
  10194. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node);
  10195. break;
  10196. case GGML_OP_SOFT_MAX:
  10197. if (ctx->num_additional_fused_ops) {
  10198. ggml_vk_topk_moe(ctx, compute_ctx, cgraph, node_idx);
  10199. } else {
  10200. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, src2, node);
  10201. }
  10202. break;
  10203. case GGML_OP_SOFT_MAX_BACK:
  10204. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node);
  10205. break;
  10206. case GGML_OP_ROPE:
  10207. ggml_vk_rope(ctx, compute_ctx, cgraph, node_idx, false);
  10208. break;
  10209. case GGML_OP_ROPE_BACK:
  10210. ggml_vk_rope(ctx, compute_ctx, cgraph, node_idx, true);
  10211. break;
  10212. case GGML_OP_ARGSORT:
  10213. if (ctx->num_additional_fused_ops) {
  10214. ggml_vk_topk_moe(ctx, compute_ctx, cgraph, node_idx);
  10215. } else {
  10216. ggml_vk_argsort(ctx, compute_ctx, src0, node);
  10217. }
  10218. break;
  10219. case GGML_OP_SUM:
  10220. ggml_vk_sum(ctx, compute_ctx, src0, node);
  10221. break;
  10222. case GGML_OP_SUM_ROWS:
  10223. ggml_vk_sum_rows(ctx, compute_ctx, src0, node);
  10224. break;
  10225. case GGML_OP_MEAN:
  10226. ggml_vk_mean(ctx, compute_ctx, src0, node);
  10227. break;
  10228. case GGML_OP_ARGMAX:
  10229. ggml_vk_argmax(ctx, compute_ctx, src0, node);
  10230. break;
  10231. case GGML_OP_COUNT_EQUAL:
  10232. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node);
  10233. break;
  10234. case GGML_OP_IM2COL:
  10235. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node);
  10236. break;
  10237. case GGML_OP_IM2COL_3D:
  10238. ggml_vk_im2col_3d(ctx, compute_ctx, src0, src1, node);
  10239. break;
  10240. case GGML_OP_TIMESTEP_EMBEDDING:
  10241. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node);
  10242. break;
  10243. case GGML_OP_CONV_TRANSPOSE_1D:
  10244. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node);
  10245. break;
  10246. case GGML_OP_POOL_2D:
  10247. ggml_vk_pool_2d(ctx, compute_ctx, src0, node);
  10248. break;
  10249. case GGML_OP_CONV_2D:
  10250. ggml_vk_conv_2d(ctx, compute_ctx, src0, src1, node);
  10251. break;
  10252. case GGML_OP_CONV_TRANSPOSE_2D:
  10253. ggml_vk_conv_transpose_2d(ctx, compute_ctx, src0, src1, node);
  10254. break;
  10255. case GGML_OP_CONV_2D_DW:
  10256. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node);
  10257. break;
  10258. case GGML_OP_LEAKY_RELU:
  10259. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node);
  10260. break;
  10261. case GGML_OP_MUL_MAT:
  10262. ggml_vk_mul_mat(ctx, compute_ctx, cgraph, node_idx);
  10263. break;
  10264. case GGML_OP_MUL_MAT_ID:
  10265. ggml_vk_mul_mat_id(ctx, compute_ctx, cgraph, node_idx);
  10266. break;
  10267. case GGML_OP_FLASH_ATTN_EXT:
  10268. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node->src[4], node);
  10269. break;
  10270. case GGML_OP_RWKV_WKV6:
  10271. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node);
  10272. break;
  10273. case GGML_OP_RWKV_WKV7:
  10274. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node);
  10275. break;
  10276. case GGML_OP_SSM_SCAN:
  10277. ggml_vk_ssm_scan(ctx, compute_ctx, node);
  10278. break;
  10279. case GGML_OP_SSM_CONV:
  10280. ggml_vk_ssm_conv(ctx, compute_ctx, node);
  10281. break;
  10282. case GGML_OP_OPT_STEP_ADAMW:
  10283. ggml_vk_opt_step_adamw(ctx, compute_ctx, node);
  10284. break;
  10285. case GGML_OP_OPT_STEP_SGD:
  10286. ggml_vk_opt_step_sgd(ctx, compute_ctx, src0, src1, src2, node);
  10287. break;
  10288. default:
  10289. return false;
  10290. }
  10291. ctx->tensor_ctxs[node_idx] = compute_ctx;
  10292. #if defined(GGML_VULKAN_CHECK_RESULTS)
  10293. // Force context reset on each node so that each tensor ends up in its own context
  10294. // and can be run and compared to its CPU equivalent separately
  10295. last_node = true;
  10296. #endif
  10297. if (submit || last_node) {
  10298. ggml_vk_ctx_end(compute_ctx);
  10299. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  10300. if (last_node) {
  10301. compute_ctx->exit_tensor_idx = node_idx_begin;
  10302. }
  10303. else {
  10304. compute_ctx->exit_tensor_idx = -1;
  10305. }
  10306. ctx->compute_ctx.reset();
  10307. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  10308. if (!ok) {
  10309. if (node->op == GGML_OP_UNARY) {
  10310. 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;
  10311. } else if (node->op == GGML_OP_GLU) {
  10312. 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;
  10313. } else {
  10314. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  10315. }
  10316. }
  10317. }
  10318. return true;
  10319. }
  10320. 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) {
  10321. GGML_UNUSED(cgraph);
  10322. ggml_backend_buffer * buf = nullptr;
  10323. switch (tensor->op) {
  10324. case GGML_OP_ADD:
  10325. case GGML_OP_ACC:
  10326. case GGML_OP_GET_ROWS:
  10327. case GGML_OP_SUB:
  10328. case GGML_OP_MUL:
  10329. case GGML_OP_DIV:
  10330. case GGML_OP_ADD_ID:
  10331. case GGML_OP_CONCAT:
  10332. case GGML_OP_UPSCALE:
  10333. case GGML_OP_SCALE:
  10334. case GGML_OP_SQR:
  10335. case GGML_OP_SQRT:
  10336. case GGML_OP_SIN:
  10337. case GGML_OP_COS:
  10338. case GGML_OP_CLAMP:
  10339. case GGML_OP_PAD:
  10340. case GGML_OP_ROLL:
  10341. case GGML_OP_CPY:
  10342. case GGML_OP_SET_ROWS:
  10343. case GGML_OP_CONT:
  10344. case GGML_OP_DUP:
  10345. case GGML_OP_SILU_BACK:
  10346. case GGML_OP_NORM:
  10347. case GGML_OP_GROUP_NORM:
  10348. case GGML_OP_RMS_NORM:
  10349. case GGML_OP_RMS_NORM_BACK:
  10350. case GGML_OP_L2_NORM:
  10351. case GGML_OP_DIAG_MASK_INF:
  10352. case GGML_OP_SOFT_MAX:
  10353. case GGML_OP_SOFT_MAX_BACK:
  10354. case GGML_OP_ROPE:
  10355. case GGML_OP_ROPE_BACK:
  10356. case GGML_OP_RESHAPE:
  10357. case GGML_OP_VIEW:
  10358. case GGML_OP_PERMUTE:
  10359. case GGML_OP_TRANSPOSE:
  10360. case GGML_OP_NONE:
  10361. case GGML_OP_ARGSORT:
  10362. case GGML_OP_SUM:
  10363. case GGML_OP_SUM_ROWS:
  10364. case GGML_OP_MEAN:
  10365. case GGML_OP_ARGMAX:
  10366. case GGML_OP_COUNT_EQUAL:
  10367. case GGML_OP_IM2COL:
  10368. case GGML_OP_IM2COL_3D:
  10369. case GGML_OP_TIMESTEP_EMBEDDING:
  10370. case GGML_OP_CONV_TRANSPOSE_1D:
  10371. case GGML_OP_POOL_2D:
  10372. case GGML_OP_CONV_2D:
  10373. case GGML_OP_CONV_TRANSPOSE_2D:
  10374. case GGML_OP_CONV_2D_DW:
  10375. case GGML_OP_RWKV_WKV6:
  10376. case GGML_OP_RWKV_WKV7:
  10377. case GGML_OP_SSM_SCAN:
  10378. case GGML_OP_SSM_CONV:
  10379. case GGML_OP_LEAKY_RELU:
  10380. case GGML_OP_REPEAT:
  10381. case GGML_OP_REPEAT_BACK:
  10382. case GGML_OP_OPT_STEP_ADAMW:
  10383. case GGML_OP_OPT_STEP_SGD:
  10384. buf = tensor->buffer;
  10385. break;
  10386. case GGML_OP_UNARY:
  10387. switch (ggml_get_unary_op(tensor)) {
  10388. case GGML_UNARY_OP_EXP:
  10389. case GGML_UNARY_OP_SILU:
  10390. case GGML_UNARY_OP_GELU:
  10391. case GGML_UNARY_OP_GELU_ERF:
  10392. case GGML_UNARY_OP_GELU_QUICK:
  10393. case GGML_UNARY_OP_RELU:
  10394. case GGML_UNARY_OP_TANH:
  10395. case GGML_UNARY_OP_SIGMOID:
  10396. case GGML_UNARY_OP_HARDSIGMOID:
  10397. case GGML_UNARY_OP_HARDSWISH:
  10398. buf = tensor->buffer;
  10399. break;
  10400. default:
  10401. return false;
  10402. }
  10403. break;
  10404. case GGML_OP_GLU:
  10405. switch (ggml_get_glu_op(tensor)) {
  10406. case GGML_GLU_OP_GEGLU:
  10407. case GGML_GLU_OP_REGLU:
  10408. case GGML_GLU_OP_SWIGLU:
  10409. case GGML_GLU_OP_SWIGLU_OAI:
  10410. case GGML_GLU_OP_GEGLU_ERF:
  10411. case GGML_GLU_OP_GEGLU_QUICK:
  10412. buf = tensor->buffer;
  10413. break;
  10414. default:
  10415. return false;
  10416. }
  10417. break;
  10418. case GGML_OP_MUL_MAT:
  10419. case GGML_OP_MUL_MAT_ID:
  10420. case GGML_OP_FLASH_ATTN_EXT:
  10421. buf = tensor->buffer;
  10422. break;
  10423. default:
  10424. return false;
  10425. }
  10426. if (buf == nullptr) {
  10427. return false;
  10428. }
  10429. 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 << ")");
  10430. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  10431. // always wait for the GPU work to be done for the last submit
  10432. if (tensor_idx == subctx->exit_tensor_idx) {
  10433. use_fence = true;
  10434. }
  10435. // Only run if ctx hasn't been submitted yet
  10436. if (!subctx->seqs.empty()) {
  10437. #ifdef GGML_VULKAN_CHECK_RESULTS
  10438. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  10439. use_fence = true;
  10440. #endif
  10441. // Do staging buffer copies
  10442. for (auto& cpy : subctx->in_memcpys) {
  10443. memcpy(cpy.dst, cpy.src, cpy.n);
  10444. }
  10445. for (auto& mset : subctx->memsets) {
  10446. memset(mset.dst, mset.val, mset.n);
  10447. }
  10448. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  10449. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  10450. ctx->almost_ready_fence_pending = true;
  10451. } else {
  10452. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  10453. }
  10454. if (use_fence) {
  10455. ggml_vk_wait_for_fence(ctx);
  10456. }
  10457. #ifdef GGML_VULKAN_CHECK_RESULTS
  10458. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  10459. #endif
  10460. }
  10461. if (tensor_idx == subctx->exit_tensor_idx) {
  10462. // Do staging buffer copies
  10463. for (auto& cpy : subctx->out_memcpys) {
  10464. memcpy(cpy.dst, cpy.src, cpy.n);
  10465. }
  10466. subctx->in_memcpys.clear();
  10467. subctx->out_memcpys.clear();
  10468. subctx->memsets.clear();
  10469. }
  10470. return true;
  10471. }
  10472. // Clean up after graph processing is done
  10473. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  10474. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  10475. ctx->prealloc_y_last_pipeline_used = {};
  10476. ctx->unsynced_nodes_written.clear();
  10477. ctx->unsynced_nodes_read.clear();
  10478. ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false;
  10479. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  10480. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  10481. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  10482. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  10483. }
  10484. ctx->gc.semaphores.clear();
  10485. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  10486. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  10487. }
  10488. ctx->gc.tl_semaphores.clear();
  10489. ctx->semaphore_idx = 0;
  10490. ctx->event_idx = 0;
  10491. for (auto& event : ctx->gc.events) {
  10492. ctx->device->device.resetEvent(event);
  10493. }
  10494. ctx->tensor_ctxs.clear();
  10495. ctx->gc.contexts.clear();
  10496. ctx->pipeline_descriptor_set_requirements = 0;
  10497. ctx->descriptor_set_idx = 0;
  10498. }
  10499. // Clean up on backend free
  10500. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  10501. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  10502. ggml_vk_graph_cleanup(ctx);
  10503. ggml_vk_destroy_buffer(ctx->prealloc_x);
  10504. ggml_vk_destroy_buffer(ctx->prealloc_y);
  10505. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  10506. ctx->prealloc_y_last_pipeline_used = nullptr;
  10507. ctx->prealloc_size_x = 0;
  10508. ctx->prealloc_size_y = 0;
  10509. ctx->prealloc_size_split_k = 0;
  10510. for (auto& event : ctx->gc.events) {
  10511. ctx->device->device.destroyEvent(event);
  10512. }
  10513. ctx->gc.events.clear();
  10514. ctx->device->device.destroyFence(ctx->fence);
  10515. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  10516. for (auto& pool : ctx->descriptor_pools) {
  10517. ctx->device->device.destroyDescriptorPool(pool);
  10518. }
  10519. ctx->descriptor_pools.clear();
  10520. ctx->descriptor_sets.clear();
  10521. ctx->compute_cmd_pool.destroy(ctx->device->device);
  10522. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  10523. }
  10524. static int ggml_vk_get_device_count() {
  10525. ggml_vk_instance_init();
  10526. return vk_instance.device_indices.size();
  10527. }
  10528. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  10529. ggml_vk_instance_init();
  10530. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  10531. vk::PhysicalDeviceProperties props;
  10532. devices[device].getProperties(&props);
  10533. snprintf(description, description_size, "%s", props.deviceName.data());
  10534. }
  10535. // backend interface
  10536. #define UNUSED GGML_UNUSED
  10537. // device backend
  10538. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  10539. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  10540. }
  10541. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  10542. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  10543. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10544. ggml_vk_destroy_buffer(ctx->dev_buffer);
  10545. delete ctx;
  10546. }
  10547. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  10548. return vk_ptr_base;
  10549. UNUSED(buffer);
  10550. }
  10551. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  10552. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  10553. if (tensor->view_src != nullptr) {
  10554. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  10555. }
  10556. return GGML_STATUS_SUCCESS;
  10557. }
  10558. 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) {
  10559. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  10560. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10561. vk_buffer buf = buf_ctx->dev_buffer;
  10562. uint32_t val32 = (uint32_t)value * 0x01010101;
  10563. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  10564. }
  10565. 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) {
  10566. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  10567. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10568. vk_buffer buf = buf_ctx->dev_buffer;
  10569. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10570. }
  10571. 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) {
  10572. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  10573. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10574. vk_buffer buf = buf_ctx->dev_buffer;
  10575. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10576. }
  10577. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  10578. if (ggml_backend_buffer_is_vk(src->buffer)) {
  10579. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  10580. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  10581. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  10582. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  10583. 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));
  10584. return true;
  10585. }
  10586. return false;
  10587. UNUSED(buffer);
  10588. }
  10589. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  10590. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  10591. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  10592. }
  10593. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  10594. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  10595. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  10596. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  10597. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  10598. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  10599. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  10600. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  10601. /* .clear = */ ggml_backend_vk_buffer_clear,
  10602. /* .reset = */ NULL,
  10603. };
  10604. // vk buffer type
  10605. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  10606. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  10607. return ctx->name.c_str();
  10608. }
  10609. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  10610. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  10611. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  10612. vk_buffer dev_buffer = nullptr;
  10613. try {
  10614. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  10615. } catch (const vk::SystemError& e) {
  10616. return nullptr;
  10617. }
  10618. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  10619. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  10620. }
  10621. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  10622. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  10623. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  10624. }
  10625. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  10626. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  10627. return ctx->device->suballocation_block_size;
  10628. }
  10629. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  10630. return ggml_nbytes(tensor);
  10631. UNUSED(buft);
  10632. }
  10633. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  10634. ggml_vk_instance_init();
  10635. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  10636. vk_device dev = ggml_vk_get_device(dev_num);
  10637. return &dev->buffer_type;
  10638. }
  10639. // host buffer type
  10640. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  10641. return GGML_VK_NAME "_Host";
  10642. UNUSED(buft);
  10643. }
  10644. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  10645. return GGML_VK_NAME "_Host";
  10646. UNUSED(buffer);
  10647. }
  10648. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  10649. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  10650. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  10651. }
  10652. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  10653. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  10654. size += 32; // Behave like the CPU buffer type
  10655. void * ptr = nullptr;
  10656. try {
  10657. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  10658. } catch (vk::SystemError& e) {
  10659. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  10660. // fallback to cpu buffer
  10661. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  10662. }
  10663. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  10664. buffer->buft = buft;
  10665. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  10666. return buffer;
  10667. UNUSED(buft);
  10668. }
  10669. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  10670. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  10671. UNUSED(buft);
  10672. }
  10673. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  10674. return vk_instance.devices[0]->suballocation_block_size;
  10675. UNUSED(buft);
  10676. }
  10677. // Should be changed to return device-specific host buffer type
  10678. // but that probably requires changes in llama.cpp
  10679. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  10680. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  10681. /* .iface = */ {
  10682. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  10683. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  10684. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  10685. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  10686. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  10687. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  10688. },
  10689. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  10690. /* .context = */ nullptr,
  10691. };
  10692. // Make sure device 0 is initialized
  10693. ggml_vk_instance_init();
  10694. ggml_vk_get_device(0);
  10695. return &ggml_backend_vk_buffer_type_host;
  10696. }
  10697. // backend
  10698. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  10699. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10700. return ctx->name.c_str();
  10701. }
  10702. static void ggml_backend_vk_free(ggml_backend_t backend) {
  10703. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10704. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  10705. ggml_vk_cleanup(ctx);
  10706. delete ctx;
  10707. delete backend;
  10708. }
  10709. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  10710. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10711. return &ctx->device->buffer_type;
  10712. }
  10713. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  10714. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  10715. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10716. 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");
  10717. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10718. vk_context transfer_ctx;
  10719. if (ctx->transfer_ctx.expired()) {
  10720. // Initialize new transfer context
  10721. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  10722. ctx->transfer_ctx = transfer_ctx;
  10723. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  10724. } else {
  10725. transfer_ctx = ctx->transfer_ctx.lock();
  10726. }
  10727. vk_buffer buf = buf_ctx->dev_buffer;
  10728. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10729. }
  10730. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  10731. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  10732. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10733. 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");
  10734. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  10735. vk_context transfer_ctx;
  10736. if (ctx->transfer_ctx.expired()) {
  10737. // Initialize new transfer context
  10738. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  10739. ctx->transfer_ctx = transfer_ctx;
  10740. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  10741. } else {
  10742. transfer_ctx = ctx->transfer_ctx.lock();
  10743. }
  10744. vk_buffer buf = buf_ctx->dev_buffer;
  10745. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  10746. }
  10747. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  10748. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  10749. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10750. 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)) {
  10751. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  10752. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  10753. vk_context transfer_ctx;
  10754. if (ctx->transfer_ctx.expired()) {
  10755. // Initialize new transfer context
  10756. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  10757. ctx->transfer_ctx = transfer_ctx;
  10758. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  10759. } else {
  10760. transfer_ctx = ctx->transfer_ctx.lock();
  10761. }
  10762. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  10763. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  10764. 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));
  10765. return true;
  10766. }
  10767. return false;
  10768. }
  10769. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  10770. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  10771. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10772. if(ctx->transfer_ctx.expired()) {
  10773. return;
  10774. }
  10775. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  10776. ggml_vk_ctx_end(transfer_ctx);
  10777. for (auto& cpy : transfer_ctx->in_memcpys) {
  10778. memcpy(cpy.dst, cpy.src, cpy.n);
  10779. }
  10780. ggml_vk_submit(transfer_ctx, ctx->fence);
  10781. ggml_vk_wait_for_fence(ctx);
  10782. for (auto& cpy : transfer_ctx->out_memcpys) {
  10783. memcpy(cpy.dst, cpy.src, cpy.n);
  10784. }
  10785. ctx->transfer_ctx.reset();
  10786. }
  10787. static bool ggml_vk_is_empty(ggml_tensor * node) {
  10788. 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;
  10789. }
  10790. static bool ggml_vk_can_fuse(const ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  10791. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  10792. return false;
  10793. }
  10794. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  10795. // additional constraints specific to this fusion
  10796. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  10797. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  10798. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  10799. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  10800. // rms_norm only supports f32
  10801. if (mul->src[0]->type != GGML_TYPE_F32 ||
  10802. mul->src[1]->type != GGML_TYPE_F32 ||
  10803. mul->type != GGML_TYPE_F32) {
  10804. return false;
  10805. }
  10806. // if rms_norm is the B operand, then we don't handle broadcast
  10807. if (rms_norm == mul->src[1] &&
  10808. !ggml_are_same_shape(mul->src[0], rms_norm)) {
  10809. return false;
  10810. }
  10811. // rms_norm shader assumes contiguous rows
  10812. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  10813. return false;
  10814. }
  10815. }
  10816. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_MUL_MAT && ops.begin()[1] == GGML_OP_ADD) {
  10817. // additional constraints specific to this fusion
  10818. const ggml_tensor *mul = cgraph->nodes[node_idx];
  10819. const ggml_tensor *add = cgraph->nodes[node_idx + 1];
  10820. const ggml_tensor *bias = add->src[0] == mul ? add->src[1] : add->src[0];
  10821. // mat-vec only
  10822. if (ggml_nrows(mul) != 1) {
  10823. return false;
  10824. }
  10825. // shaders assume the types match
  10826. if (mul->type != bias->type) {
  10827. return false;
  10828. }
  10829. // shaders reuse the D shape for bias
  10830. if (!ggml_are_same_shape(mul, bias) ||
  10831. !ggml_are_same_stride(mul, bias)) {
  10832. return false;
  10833. }
  10834. // unaligned bias isn't handled
  10835. if (get_misalign_bytes(ctx, bias) != 0) {
  10836. return false;
  10837. }
  10838. }
  10839. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_MUL_MAT_ID && ops.begin()[1] == GGML_OP_ADD_ID) {
  10840. // additional constraints specific to this fusion
  10841. const ggml_tensor *mul = cgraph->nodes[node_idx];
  10842. const ggml_tensor *add = cgraph->nodes[node_idx + 1];
  10843. const ggml_tensor *bias = add->src[1];
  10844. if (mul != add->src[0]) {
  10845. return false;
  10846. }
  10847. // mat-vec only
  10848. if (!ggml_vk_use_mul_mat_vec_id(cgraph, node_idx)) {
  10849. return false;
  10850. }
  10851. // shaders assume the types match
  10852. if (mul->type != bias->type) {
  10853. return false;
  10854. }
  10855. // shaders assume the bias is contiguous
  10856. if (!ggml_is_contiguous(bias)) {
  10857. return false;
  10858. }
  10859. // the ID tensor must be the same for mul_mat_id and add_id
  10860. if (mul->src[2] != add->src[2]) {
  10861. return false;
  10862. }
  10863. // unaligned bias isn't handled
  10864. if (get_misalign_bytes(ctx, bias) != 0) {
  10865. return false;
  10866. }
  10867. }
  10868. return true;
  10869. }
  10870. static bool ggml_vk_can_fuse_topk_moe(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph,
  10871. int node_idx, topk_moe_mode mode) {
  10872. const ggml_tensor * softmax;
  10873. const ggml_tensor * weights;
  10874. switch (mode) {
  10875. case TOPK_MOE_EARLY_SOFTMAX_NORM:
  10876. softmax = cgraph->nodes[node_idx + 0];
  10877. weights = cgraph->nodes[node_idx + 9];
  10878. break;
  10879. case TOPK_MOE_EARLY_SOFTMAX:
  10880. softmax = cgraph->nodes[node_idx + 0];
  10881. weights = cgraph->nodes[node_idx + 4];
  10882. break;
  10883. case TOPK_MOE_LATE_SOFTMAX:
  10884. softmax = cgraph->nodes[node_idx + 4];
  10885. weights = cgraph->nodes[node_idx + 5];
  10886. break;
  10887. default:
  10888. return false;
  10889. }
  10890. const float * op_params = (const float *)softmax->op_params;
  10891. float scale = op_params[0];
  10892. float max_bias = op_params[1];
  10893. if (!ggml_is_contiguous(softmax->src[0]) || !ggml_is_contiguous(weights)) {
  10894. return false;
  10895. }
  10896. if (scale != 1.0f || max_bias != 0.0f) {
  10897. return false;
  10898. }
  10899. // don't fuse when masks or sinks are present
  10900. if (softmax->src[1] || softmax->src[2]) {
  10901. return false;
  10902. }
  10903. const int n_expert = softmax->ne[0];
  10904. // n_expert must be a power of 2
  10905. if (!is_pow2(n_expert) || n_expert > (1 << (num_topk_moe_pipelines-1))) {
  10906. return false;
  10907. }
  10908. if (!ctx->device->subgroup_arithmetic ||
  10909. !ctx->device->subgroup_shuffle ||
  10910. !ctx->device->subgroup_require_full_support ||
  10911. ctx->device->disable_fusion) {
  10912. return false;
  10913. }
  10914. return true;
  10915. }
  10916. static bool ggml_vk_can_fuse_rope_set_rows(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph,
  10917. int node_idx) {
  10918. GGML_UNUSED(ctx);
  10919. const ggml_tensor *rope = cgraph->nodes[node_idx + 0];
  10920. const ggml_tensor *view = cgraph->nodes[node_idx + 1];
  10921. const ggml_tensor *set_rows = cgraph->nodes[node_idx + 2];
  10922. // ne3 not tested
  10923. if (rope->src[0]->ne[3] != 1) {
  10924. return false;
  10925. }
  10926. if (set_rows->type != GGML_TYPE_F32 && set_rows->type != GGML_TYPE_F16) {
  10927. return false;
  10928. }
  10929. if (set_rows->src[1]->type != GGML_TYPE_I64) {
  10930. return false;
  10931. }
  10932. // The view should flatten two dims of rope into one dim
  10933. if (!ggml_is_contiguous(view) ||
  10934. view->ne[0] != rope->ne[0] * rope->ne[1]) {
  10935. return false;
  10936. }
  10937. // Only norm/neox shaders have the fusion code
  10938. const int mode = ((const int32_t *) rope->op_params)[2];
  10939. if (mode != GGML_ROPE_TYPE_NORMAL && mode != GGML_ROPE_TYPE_NEOX) {
  10940. return false;
  10941. }
  10942. return true;
  10943. }
  10944. static uint32_t ggml_vk_fuse_multi_add(ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph, int node_idx) {
  10945. const ggml_tensor *first_node = cgraph->nodes[node_idx];
  10946. if (first_node->op != GGML_OP_ADD) {
  10947. return 0;
  10948. }
  10949. if (!ctx->device->multi_add) {
  10950. return 0;
  10951. }
  10952. int32_t num_adds = 1;
  10953. while (node_idx + num_adds < cgraph->n_nodes &&
  10954. cgraph->nodes[node_idx + num_adds]->op == GGML_OP_ADD &&
  10955. num_adds < MAX_FUSED_ADDS) {
  10956. num_adds++;
  10957. }
  10958. // The shader currently requires same shapes (but different strides are allowed),
  10959. // everything f32, and no misalignment
  10960. for (int32_t i = 0; i < num_adds; ++i) {
  10961. const ggml_tensor *next_node = cgraph->nodes[node_idx + i];
  10962. if (!ggml_are_same_shape(first_node, next_node->src[0]) ||
  10963. !ggml_are_same_shape(first_node, next_node->src[1]) ||
  10964. next_node->type != GGML_TYPE_F32 ||
  10965. next_node->src[0]->type != GGML_TYPE_F32 ||
  10966. next_node->src[1]->type != GGML_TYPE_F32 ||
  10967. get_misalign_bytes(ctx, next_node) ||
  10968. get_misalign_bytes(ctx, next_node->src[0]) ||
  10969. get_misalign_bytes(ctx, next_node->src[1])) {
  10970. num_adds = i;
  10971. }
  10972. }
  10973. // Verify we can fuse these
  10974. ggml_op adds[MAX_FUSED_ADDS];
  10975. for (int32_t i = 0; i < num_adds; ++i) {
  10976. adds[i] = GGML_OP_ADD;
  10977. }
  10978. // decrease num_adds if they can't all be fused
  10979. while (num_adds > 1 && !ggml_can_fuse(cgraph, node_idx, adds, num_adds)) {
  10980. num_adds--;
  10981. }
  10982. // a single add is not "fused", so just return zero
  10983. if (num_adds == 1) {
  10984. return 0;
  10985. }
  10986. return num_adds;
  10987. }
  10988. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  10989. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  10990. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  10991. if (vk_instance.debug_utils_support) {
  10992. vk::DebugUtilsLabelEXT dul = {};
  10993. dul.pLabelName = "ggml_backend_vk_graph_compute";
  10994. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  10995. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  10996. }
  10997. ctx->prealloc_size_add_rms_partials_offset = 0;
  10998. ctx->do_add_rms_partials = false;
  10999. ctx->do_add_rms_partials_offset_calculation = false;
  11000. int last_node = cgraph->n_nodes - 1;
  11001. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  11002. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  11003. last_node -= 1;
  11004. }
  11005. // Reserve tensor context space for all nodes
  11006. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  11007. bool first_node_in_batch = true; // true if next node will be first node in a batch
  11008. int submit_node_idx = 0; // index to first node in a batch
  11009. vk_context compute_ctx;
  11010. if (vk_perf_logger_enabled) {
  11011. // allocate/resize the query pool
  11012. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  11013. if (ctx->device->query_pool) {
  11014. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  11015. }
  11016. vk::QueryPoolCreateInfo query_create_info;
  11017. query_create_info.queryType = vk::QueryType::eTimestamp;
  11018. query_create_info.queryCount = cgraph->n_nodes + 100;
  11019. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  11020. ctx->device->num_queries = query_create_info.queryCount;
  11021. }
  11022. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  11023. GGML_ASSERT(ctx->compute_ctx.expired());
  11024. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  11025. ctx->compute_ctx = compute_ctx;
  11026. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  11027. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  11028. }
  11029. ctx->prealloc_y_last_pipeline_used = nullptr;
  11030. ctx->prealloc_y_last_tensor_used = nullptr;
  11031. if (ctx->prealloc_size_add_rms_partials) {
  11032. ggml_vk_preallocate_buffers(ctx, nullptr);
  11033. if (ctx->compute_ctx.expired()) {
  11034. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  11035. ctx->compute_ctx = compute_ctx;
  11036. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  11037. } else {
  11038. compute_ctx = ctx->compute_ctx.lock();
  11039. }
  11040. // initialize partial sums to zero.
  11041. ggml_vk_buffer_memset_async(compute_ctx, ctx->prealloc_add_rms_partials, 0, 0, ctx->prealloc_size_add_rms_partials);
  11042. ggml_vk_sync_buffers(ctx, compute_ctx);
  11043. }
  11044. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  11045. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  11046. // (and scaled down based on model size, so smaller models submit earlier).
  11047. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  11048. int nodes_per_submit = 100;
  11049. int submitted_nodes = 0;
  11050. int submit_count = 0;
  11051. uint64_t mul_mat_bytes = 0;
  11052. uint64_t total_mul_mat_bytes = 0;
  11053. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), ctx->last_total_mul_mat_bytes / 40u);
  11054. for (int i = 0; i < cgraph->n_nodes; i++) {
  11055. if (first_node_in_batch) {
  11056. submit_node_idx = i;
  11057. }
  11058. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  11059. auto bytes = ggml_nbytes(cgraph->nodes[i]->src[0]);
  11060. mul_mat_bytes += bytes;
  11061. total_mul_mat_bytes += bytes;
  11062. }
  11063. if (!ctx->device->disable_fusion) {
  11064. uint32_t num_adds = ggml_vk_fuse_multi_add(ctx, cgraph, i);
  11065. if (num_adds) {
  11066. ctx->num_additional_fused_ops = num_adds - 1;
  11067. } else if (ggml_vk_can_fuse(ctx, cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  11068. ctx->num_additional_fused_ops = 1;
  11069. } else if (ggml_vk_can_fuse(ctx, cgraph, i, { GGML_OP_MUL_MAT, GGML_OP_ADD })) {
  11070. ctx->num_additional_fused_ops = 1;
  11071. } else if (ggml_vk_can_fuse(ctx, cgraph, i, { GGML_OP_MUL_MAT_ID, GGML_OP_ADD_ID })) {
  11072. ctx->num_additional_fused_ops = 1;
  11073. } else if (ggml_can_fuse_subgraph(cgraph, i, { GGML_OP_ROPE, GGML_OP_VIEW, GGML_OP_SET_ROWS }, { i + 2 }) &&
  11074. ggml_check_edges(cgraph, i, rope_view_set_rows_edges) &&
  11075. ggml_vk_can_fuse_rope_set_rows(ctx, cgraph, i)) {
  11076. ctx->num_additional_fused_ops = 2;
  11077. } else if (ggml_can_fuse_subgraph(cgraph, i, topk_moe_early_softmax_norm, { i + 3, i + 9 }) &&
  11078. ggml_check_edges(cgraph, i, topk_moe_early_softmax_norm_edges) &&
  11079. ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, TOPK_MOE_EARLY_SOFTMAX_NORM)) {
  11080. ctx->num_additional_fused_ops = topk_moe_early_softmax_norm.size() - 1;
  11081. // view of argsort writes to memory
  11082. ctx->fused_ops_write_mask |= 1 << 3;
  11083. } else if (ggml_can_fuse_subgraph(cgraph, i, topk_moe_early_softmax, { i + 3, i + 4 }) &&
  11084. ggml_check_edges(cgraph, i, topk_moe_early_softmax_edges) &&
  11085. ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, TOPK_MOE_EARLY_SOFTMAX)) {
  11086. ctx->num_additional_fused_ops = topk_moe_early_softmax.size() - 1;
  11087. // view of argsort writes to memory
  11088. ctx->fused_ops_write_mask |= 1 << 3;
  11089. } else if (ggml_can_fuse_subgraph(cgraph, i, topk_moe_late_softmax, { i + 1, i + 5 }) &&
  11090. ggml_check_edges(cgraph, i, topk_moe_late_softmax_edges) &&
  11091. ggml_vk_can_fuse_topk_moe(ctx, cgraph, i, TOPK_MOE_LATE_SOFTMAX)) {
  11092. ctx->num_additional_fused_ops = topk_moe_late_softmax.size() - 1;
  11093. // view of argsort writes to memory
  11094. ctx->fused_ops_write_mask |= 1 << 1;
  11095. }
  11096. }
  11097. ctx->fused_ops_write_mask |= 1 << ctx->num_additional_fused_ops;
  11098. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  11099. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  11100. bool submit = (submitted_nodes >= nodes_per_submit) ||
  11101. (mul_mat_bytes_per_submit != 0 && mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  11102. (i + ctx->num_additional_fused_ops >= last_node) ||
  11103. (almost_ready && !ctx->almost_ready_fence_pending);
  11104. bool enqueued = ggml_vk_build_graph(ctx, cgraph, i, cgraph->nodes[submit_node_idx], submit_node_idx, i + ctx->num_additional_fused_ops >= last_node, almost_ready, submit);
  11105. if (vk_perf_logger_enabled) {
  11106. if (ctx->compute_ctx.expired()) {
  11107. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  11108. ctx->compute_ctx = compute_ctx;
  11109. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  11110. } else {
  11111. compute_ctx = ctx->compute_ctx.lock();
  11112. }
  11113. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  11114. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  11115. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  11116. }
  11117. }
  11118. if (enqueued) {
  11119. ++submitted_nodes;
  11120. #ifndef GGML_VULKAN_CHECK_RESULTS
  11121. if (first_node_in_batch) {
  11122. first_node_in_batch = false;
  11123. }
  11124. #endif
  11125. }
  11126. if (submit && enqueued) {
  11127. first_node_in_batch = true;
  11128. submitted_nodes = 0;
  11129. mul_mat_bytes = 0;
  11130. if (submit_count < 3) {
  11131. mul_mat_bytes_per_submit *= 2;
  11132. }
  11133. submit_count++;
  11134. }
  11135. i += ctx->num_additional_fused_ops;
  11136. ctx->num_additional_fused_ops = 0;
  11137. ctx->fused_ops_write_mask = 0;
  11138. }
  11139. ctx->prealloc_size_add_rms_partials = std::max(ctx->prealloc_size_add_rms_partials, ctx->prealloc_size_add_rms_partials_offset);
  11140. ctx->last_total_mul_mat_bytes = total_mul_mat_bytes;
  11141. if (vk_perf_logger_enabled) {
  11142. // End the command buffer and submit/wait
  11143. GGML_ASSERT(!ctx->compute_ctx.expired());
  11144. compute_ctx = ctx->compute_ctx.lock();
  11145. ggml_vk_ctx_end(compute_ctx);
  11146. ggml_vk_submit(compute_ctx, ctx->device->fence);
  11147. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  11148. ctx->device->device.resetFences({ ctx->device->fence });
  11149. // Get the results and pass them to the logger
  11150. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  11151. 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");
  11152. for (int i = 0; i < cgraph->n_nodes; i++) {
  11153. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  11154. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  11155. }
  11156. }
  11157. ctx->device->perf_logger->print_timings();
  11158. }
  11159. ggml_vk_graph_cleanup(ctx);
  11160. return GGML_STATUS_SUCCESS;
  11161. UNUSED(backend);
  11162. }
  11163. // Sort the graph for improved parallelism.
  11164. static void ggml_vk_graph_optimize(ggml_backend_t backend, struct ggml_cgraph * graph)
  11165. {
  11166. VK_LOG_DEBUG("ggml_vk_graph_optimize(" << graph->n_nodes << " nodes)");
  11167. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  11168. if (ctx->device->disable_graph_optimize) {
  11169. return;
  11170. }
  11171. auto const &is_empty = [](ggml_tensor * node) -> bool {
  11172. return node->op == GGML_OP_NONE || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE;
  11173. };
  11174. auto const &is_src_of = [](const ggml_tensor *dst, const ggml_tensor *src) -> bool {
  11175. for (uint32_t s = 0; s < GGML_MAX_SRC; ++s) {
  11176. if (dst->src[s] == src) {
  11177. return true;
  11178. }
  11179. }
  11180. // implicit dependency if they view the same tensor
  11181. const ggml_tensor *dst2 = dst->view_src ? dst->view_src : dst;
  11182. const ggml_tensor *src2 = src->view_src ? src->view_src : src;
  11183. if (dst2 == src2) {
  11184. return true;
  11185. }
  11186. return false;
  11187. };
  11188. // This function tries to reorder the graph to allow nodes to run in parallel.
  11189. // This helps with small batches, but for large batches its a slowdown, probably
  11190. // due to cache contention. So only reorder if the majority of nodes have few rows.
  11191. int num_small_nodes = 0;
  11192. int num_counted_nodes = 0;
  11193. for (int i = 0; i < graph->n_nodes; ++i) {
  11194. if (!is_empty(graph->nodes[i]) &&
  11195. graph->nodes[i]->op != GGML_OP_SET_ROWS) {
  11196. if (ggml_nrows(graph->nodes[i]) <= 8) {
  11197. num_small_nodes++;
  11198. }
  11199. num_counted_nodes++;
  11200. }
  11201. }
  11202. if (num_small_nodes < num_counted_nodes / 2) {
  11203. return;
  11204. }
  11205. std::vector<ggml_tensor *> new_order;
  11206. std::vector<bool> used(graph->n_nodes, false);
  11207. int first_unused = 0;
  11208. while (first_unused < graph->n_nodes) {
  11209. std::vector<int> current_set;
  11210. // Check for fusion patterns and avoid reordering them
  11211. auto const &match_pattern = [&](const std::initializer_list<ggml_op> &pattern, int start) -> bool {
  11212. if (start + (int)pattern.size() <= graph->n_nodes) {
  11213. bool is_pattern = true;
  11214. for (size_t j = 0; j < pattern.size(); ++j) {
  11215. if (graph->nodes[start + j]->op != pattern.begin()[j] || used[start + j]) {
  11216. is_pattern = false;
  11217. }
  11218. }
  11219. return is_pattern;
  11220. }
  11221. return false;
  11222. };
  11223. auto const &keep_pattern = [&](const std::initializer_list<ggml_op> &pattern) -> bool {
  11224. if (match_pattern(pattern, first_unused)) {
  11225. for (size_t j = 0; j < pattern.size(); ++j) {
  11226. new_order.push_back(graph->nodes[first_unused + j]);
  11227. used[first_unused + j] = true;
  11228. }
  11229. while (first_unused < graph->n_nodes && used[first_unused]) {
  11230. first_unused++;
  11231. }
  11232. return true;
  11233. }
  11234. return false;
  11235. };
  11236. if (keep_pattern(topk_moe_early_softmax_norm)) {
  11237. continue;
  11238. }
  11239. if (keep_pattern(topk_moe_early_softmax)) {
  11240. continue;
  11241. }
  11242. if (keep_pattern(topk_moe_late_softmax)) {
  11243. continue;
  11244. }
  11245. // First, grab the next unused node.
  11246. current_set.push_back(first_unused);
  11247. // Loop through the next N nodes. Grab any that don't depend on other nodes that
  11248. // haven't already been run. Nodes that have already been run have used[i] set
  11249. // to true. Allow nodes that depend on the previous node if it's a fusion pattern
  11250. // that we support (e.g. RMS_NORM + MUL).
  11251. // This first pass only grabs "real" (non-view nodes). Second pass grabs view nodes.
  11252. // The goal is to not interleave real and view nodes in a way that breaks fusion.
  11253. const int NUM_TO_CHECK = 20;
  11254. for (int j = first_unused+1; j < std::min(first_unused + NUM_TO_CHECK, graph->n_nodes); ++j) {
  11255. if (used[j]) {
  11256. continue;
  11257. }
  11258. if (is_empty(graph->nodes[j])) {
  11259. continue;
  11260. }
  11261. // Don't pull forward nodes from fusion patterns
  11262. if (match_pattern(topk_moe_early_softmax_norm, j) ||
  11263. match_pattern(topk_moe_early_softmax, j) ||
  11264. match_pattern(topk_moe_late_softmax, j)) {
  11265. continue;
  11266. }
  11267. bool ok = true;
  11268. for (int c = first_unused; c < j; ++c) {
  11269. if (!used[c] &&
  11270. is_src_of(graph->nodes[j], graph->nodes[c]) &&
  11271. !(j == c+1 && c == current_set.back() && graph->nodes[c]->op == GGML_OP_RMS_NORM && graph->nodes[j]->op == GGML_OP_MUL) &&
  11272. !(j == c+1 && c == current_set.back() && graph->nodes[c]->op == GGML_OP_MUL_MAT && graph->nodes[j]->op == GGML_OP_ADD) &&
  11273. !(j == c+1 && c == current_set.back() && graph->nodes[c]->op == GGML_OP_MUL_MAT_ID && graph->nodes[j]->op == GGML_OP_ADD_ID)) {
  11274. ok = false;
  11275. break;
  11276. }
  11277. }
  11278. if (ok) {
  11279. current_set.push_back(j);
  11280. // Look for ROPE + VIEW + SET_ROWS and make them consecutive
  11281. if (graph->nodes[j]->op == GGML_OP_ROPE) {
  11282. int view_idx = -1;
  11283. int set_rows_idx = -1;
  11284. for (int k = j+1; k < std::min(j + 10, graph->n_nodes); ++k) {
  11285. if (view_idx == -1 &&
  11286. graph->nodes[k]->op == GGML_OP_VIEW &&
  11287. graph->nodes[k]->src[0] == graph->nodes[j]) {
  11288. view_idx = k;
  11289. continue;
  11290. }
  11291. if (view_idx != -1 &&
  11292. set_rows_idx == -1 &&
  11293. graph->nodes[k]->op == GGML_OP_SET_ROWS &&
  11294. graph->nodes[k]->src[0] == graph->nodes[view_idx]) {
  11295. set_rows_idx = k;
  11296. break;
  11297. }
  11298. }
  11299. if (set_rows_idx != -1) {
  11300. current_set.push_back(view_idx);
  11301. current_set.push_back(set_rows_idx);
  11302. used[view_idx] = true;
  11303. used[set_rows_idx] = true;
  11304. }
  11305. }
  11306. }
  11307. }
  11308. // Second pass grabs view nodes.
  11309. // Skip this if it would break a fusion optimization (don't split up add->rms_norm or add->add).
  11310. if (graph->nodes[current_set.back()]->op != GGML_OP_ADD) {
  11311. for (int j = first_unused+1; j < std::min(first_unused + NUM_TO_CHECK, graph->n_nodes); ++j) {
  11312. if (used[j]) {
  11313. continue;
  11314. }
  11315. if (!is_empty(graph->nodes[j])) {
  11316. continue;
  11317. }
  11318. bool ok = true;
  11319. for (int c = first_unused; c < j; ++c) {
  11320. bool c_in_current_set = std::find(current_set.begin(), current_set.end(), c) != current_set.end();
  11321. // skip views whose srcs haven't been processed.
  11322. if (!used[c] &&
  11323. is_src_of(graph->nodes[j], graph->nodes[c]) &&
  11324. !c_in_current_set) {
  11325. ok = false;
  11326. break;
  11327. }
  11328. }
  11329. if (ok) {
  11330. current_set.push_back(j);
  11331. }
  11332. }
  11333. }
  11334. // Push the current set into new_order
  11335. for (auto c : current_set) {
  11336. new_order.push_back(graph->nodes[c]);
  11337. used[c] = true;
  11338. }
  11339. while (first_unused < graph->n_nodes && used[first_unused]) {
  11340. first_unused++;
  11341. }
  11342. }
  11343. // Replace the graph with the new order.
  11344. for (int i = 0; i < graph->n_nodes; ++i) {
  11345. graph->nodes[i] = new_order[i];
  11346. }
  11347. }
  11348. // TODO: enable async and synchronize
  11349. static ggml_backend_i ggml_backend_vk_interface = {
  11350. /* .get_name = */ ggml_backend_vk_name,
  11351. /* .free = */ ggml_backend_vk_free,
  11352. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  11353. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  11354. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  11355. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  11356. /* .graph_plan_create = */ NULL,
  11357. /* .graph_plan_free = */ NULL,
  11358. /* .graph_plan_update = */ NULL,
  11359. /* .graph_plan_compute = */ NULL,
  11360. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  11361. /* .event_record = */ NULL,
  11362. /* .event_wait = */ NULL,
  11363. /* .graph_optimize = */ ggml_vk_graph_optimize,
  11364. };
  11365. static ggml_guid_t ggml_backend_vk_guid() {
  11366. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  11367. return &guid;
  11368. }
  11369. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  11370. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  11371. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  11372. ggml_vk_init(ctx, dev_num);
  11373. ggml_backend_t vk_backend = new ggml_backend {
  11374. /* .guid = */ ggml_backend_vk_guid(),
  11375. /* .iface = */ ggml_backend_vk_interface,
  11376. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  11377. /* .context = */ ctx,
  11378. };
  11379. return vk_backend;
  11380. }
  11381. bool ggml_backend_is_vk(ggml_backend_t backend) {
  11382. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  11383. }
  11384. int ggml_backend_vk_get_device_count() {
  11385. return ggml_vk_get_device_count();
  11386. }
  11387. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  11388. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  11389. int dev_idx = vk_instance.device_indices[device];
  11390. ggml_vk_get_device_description(dev_idx, description, description_size);
  11391. }
  11392. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  11393. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  11394. GGML_ASSERT(device < (int) vk_instance.device_supports_membudget.size());
  11395. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  11396. vk::PhysicalDeviceMemoryBudgetPropertiesEXT budgetprops;
  11397. vk::PhysicalDeviceMemoryProperties2 memprops = {};
  11398. bool membudget_supported = vk_instance.device_supports_membudget[device];
  11399. if (membudget_supported) {
  11400. memprops.pNext = &budgetprops;
  11401. }
  11402. vkdev.getMemoryProperties2(&memprops);
  11403. for (uint32_t i = 0; i < memprops.memoryProperties.memoryHeapCount; ++i) {
  11404. const vk::MemoryHeap & heap = memprops.memoryProperties.memoryHeaps[i];
  11405. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  11406. *total = heap.size;
  11407. if (membudget_supported && i < budgetprops.heapUsage.size()) {
  11408. *free = budgetprops.heapBudget[i] - budgetprops.heapUsage[i];
  11409. } else {
  11410. *free = heap.size;
  11411. }
  11412. break;
  11413. }
  11414. }
  11415. }
  11416. static vk::PhysicalDeviceType ggml_backend_vk_get_device_type(int device_idx) {
  11417. GGML_ASSERT(device_idx >= 0 && device_idx < (int) vk_instance.device_indices.size());
  11418. vk::PhysicalDevice device = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device_idx]];
  11419. vk::PhysicalDeviceProperties2 props = {};
  11420. device.getProperties2(&props);
  11421. return props.properties.deviceType;
  11422. }
  11423. static std::string ggml_backend_vk_get_device_pci_id(int device_idx) {
  11424. GGML_ASSERT(device_idx >= 0 && device_idx < (int) vk_instance.device_indices.size());
  11425. vk::PhysicalDevice device = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device_idx]];
  11426. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  11427. bool ext_support = false;
  11428. for (const auto& properties : ext_props) {
  11429. if (strcmp("VK_EXT_pci_bus_info", properties.extensionName) == 0) {
  11430. ext_support = true;
  11431. break;
  11432. }
  11433. }
  11434. if (!ext_support) {
  11435. return "";
  11436. }
  11437. vk::PhysicalDeviceProperties2 props = {};
  11438. vk::PhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info = {};
  11439. props.pNext = &pci_bus_info;
  11440. device.getProperties2(&props);
  11441. const uint32_t pci_domain = pci_bus_info.pciDomain;
  11442. const uint32_t pci_bus = pci_bus_info.pciBus;
  11443. const uint32_t pci_device = pci_bus_info.pciDevice;
  11444. const uint8_t pci_function = (uint8_t) pci_bus_info.pciFunction; // pci function is between 0 and 7, prevent printf overflow warning
  11445. char pci_bus_id[16] = {};
  11446. snprintf(pci_bus_id, sizeof(pci_bus_id), "%04x:%02x:%02x.%x", pci_domain, pci_bus, pci_device, pci_function);
  11447. return std::string(pci_bus_id);
  11448. }
  11449. //////////////////////////
  11450. struct ggml_backend_vk_device_context {
  11451. size_t device;
  11452. std::string name;
  11453. std::string description;
  11454. bool is_integrated_gpu;
  11455. std::string pci_bus_id;
  11456. };
  11457. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  11458. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11459. return ctx->name.c_str();
  11460. }
  11461. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  11462. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11463. return ctx->description.c_str();
  11464. }
  11465. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  11466. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  11467. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  11468. }
  11469. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  11470. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11471. return ggml_backend_vk_buffer_type(ctx->device);
  11472. }
  11473. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  11474. UNUSED(dev);
  11475. return ggml_backend_vk_host_buffer_type();
  11476. }
  11477. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  11478. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11479. return ctx->is_integrated_gpu ? GGML_BACKEND_DEVICE_TYPE_IGPU : GGML_BACKEND_DEVICE_TYPE_GPU;
  11480. }
  11481. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  11482. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11483. props->name = ggml_backend_vk_device_get_name(dev);
  11484. props->description = ggml_backend_vk_device_get_description(dev);
  11485. props->type = ggml_backend_vk_device_get_type(dev);
  11486. props->device_id = ctx->pci_bus_id.empty() ? nullptr : ctx->pci_bus_id.c_str();
  11487. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  11488. props->caps = {
  11489. /* .async = */ false,
  11490. /* .host_buffer = */ true,
  11491. /* .buffer_from_host_ptr = */ false,
  11492. /* .events = */ false,
  11493. };
  11494. }
  11495. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  11496. UNUSED(params);
  11497. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11498. return ggml_backend_vk_init(ctx->device);
  11499. }
  11500. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  11501. switch (op->op) {
  11502. case GGML_OP_UNARY:
  11503. switch (ggml_get_unary_op(op)) {
  11504. case GGML_UNARY_OP_EXP:
  11505. case GGML_UNARY_OP_GELU:
  11506. case GGML_UNARY_OP_GELU_ERF:
  11507. case GGML_UNARY_OP_GELU_QUICK:
  11508. case GGML_UNARY_OP_SILU:
  11509. case GGML_UNARY_OP_RELU:
  11510. case GGML_UNARY_OP_TANH:
  11511. case GGML_UNARY_OP_SIGMOID:
  11512. case GGML_UNARY_OP_HARDSIGMOID:
  11513. case GGML_UNARY_OP_HARDSWISH:
  11514. return ggml_is_contiguous(op->src[0]) &&
  11515. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11516. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  11517. (op->src[0]->type == op->type);
  11518. default:
  11519. return false;
  11520. }
  11521. case GGML_OP_GLU:
  11522. switch (ggml_get_glu_op(op)) {
  11523. case GGML_GLU_OP_GEGLU:
  11524. case GGML_GLU_OP_REGLU:
  11525. case GGML_GLU_OP_SWIGLU:
  11526. case GGML_GLU_OP_SWIGLU_OAI:
  11527. case GGML_GLU_OP_GEGLU_ERF:
  11528. case GGML_GLU_OP_GEGLU_QUICK:
  11529. return ggml_is_contiguous(op->src[0]) &&
  11530. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11531. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  11532. (op->src[0]->type == op->type);
  11533. default:
  11534. return false;
  11535. }
  11536. case GGML_OP_MUL_MAT:
  11537. case GGML_OP_MUL_MAT_ID:
  11538. {
  11539. ggml_type src0_type = op->src[0]->type;
  11540. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11541. const vk_device& device = ggml_vk_get_device(ctx->device);
  11542. if (op->op == GGML_OP_MUL_MAT_ID) {
  11543. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  11544. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  11545. return false;
  11546. }
  11547. }
  11548. switch (src0_type) {
  11549. case GGML_TYPE_F32:
  11550. case GGML_TYPE_F16:
  11551. case GGML_TYPE_BF16:
  11552. case GGML_TYPE_Q4_0:
  11553. case GGML_TYPE_Q4_1:
  11554. case GGML_TYPE_Q5_0:
  11555. case GGML_TYPE_Q5_1:
  11556. case GGML_TYPE_Q8_0:
  11557. case GGML_TYPE_Q2_K:
  11558. case GGML_TYPE_Q3_K:
  11559. case GGML_TYPE_Q4_K:
  11560. case GGML_TYPE_Q5_K:
  11561. case GGML_TYPE_Q6_K:
  11562. case GGML_TYPE_IQ1_S:
  11563. case GGML_TYPE_IQ1_M:
  11564. case GGML_TYPE_IQ2_XXS:
  11565. case GGML_TYPE_IQ2_XS:
  11566. case GGML_TYPE_IQ2_S:
  11567. case GGML_TYPE_IQ3_XXS:
  11568. case GGML_TYPE_IQ3_S:
  11569. case GGML_TYPE_IQ4_XS:
  11570. case GGML_TYPE_IQ4_NL:
  11571. case GGML_TYPE_MXFP4:
  11572. break;
  11573. default:
  11574. return false;
  11575. }
  11576. struct ggml_tensor * a;
  11577. struct ggml_tensor * b;
  11578. if (op->op == GGML_OP_MUL_MAT) {
  11579. a = op->src[0];
  11580. b = op->src[1];
  11581. } else {
  11582. a = op->src[2];
  11583. b = op->src[1];
  11584. }
  11585. if (a->ne[3] != b->ne[3]) {
  11586. return false;
  11587. }
  11588. 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) ||
  11589. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  11590. return false;
  11591. }
  11592. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  11593. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  11594. // So don't support this combination for now.
  11595. return false;
  11596. }
  11597. return true;
  11598. }
  11599. case GGML_OP_FLASH_ATTN_EXT:
  11600. {
  11601. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11602. auto device = ggml_vk_get_device(ctx->device);
  11603. bool coopmat2 = device->coopmat2;
  11604. uint32_t HSK = op->src[1]->ne[0];
  11605. uint32_t HSV = op->src[2]->ne[0];
  11606. if ((HSK % 8) != 0 || (HSV % 8) != 0) {
  11607. return false;
  11608. }
  11609. if (op->src[4] && op->src[4]->type != GGML_TYPE_F32) {
  11610. return false;
  11611. }
  11612. if (op->src[0]->type != GGML_TYPE_F32) {
  11613. return false;
  11614. }
  11615. if (op->type != GGML_TYPE_F32) {
  11616. return false;
  11617. }
  11618. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  11619. return false;
  11620. }
  11621. // It's straightforward to support different K/V dequant, but would
  11622. // significantly increase the number of pipelines
  11623. if (op->src[1]->type != op->src[2]->type) {
  11624. return false;
  11625. }
  11626. switch (op->src[1]->type) {
  11627. case GGML_TYPE_F16:
  11628. case GGML_TYPE_F32:
  11629. case GGML_TYPE_Q4_0:
  11630. case GGML_TYPE_Q8_0:
  11631. // supported in scalar and coopmat2 paths
  11632. break;
  11633. case GGML_TYPE_Q4_1:
  11634. case GGML_TYPE_Q5_0:
  11635. case GGML_TYPE_Q5_1:
  11636. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  11637. //case GGML_TYPE_Q2_K:
  11638. //case GGML_TYPE_Q3_K:
  11639. //case GGML_TYPE_Q4_K:
  11640. //case GGML_TYPE_Q5_K:
  11641. //case GGML_TYPE_Q6_K:
  11642. //case GGML_TYPE_IQ1_S:
  11643. //case GGML_TYPE_IQ1_M:
  11644. //case GGML_TYPE_IQ2_XXS:
  11645. //case GGML_TYPE_IQ2_XS:
  11646. //case GGML_TYPE_IQ2_S:
  11647. //case GGML_TYPE_IQ3_XXS:
  11648. //case GGML_TYPE_IQ3_S:
  11649. //case GGML_TYPE_IQ4_XS:
  11650. case GGML_TYPE_IQ4_NL:
  11651. // currently supported only in coopmat2 path
  11652. if (!coopmat2) {
  11653. return false;
  11654. }
  11655. break;
  11656. default:
  11657. return false;
  11658. }
  11659. if (!coopmat2 && !device->subgroup_shuffle) {
  11660. // scalar FA uses subgroupShuffle
  11661. return false;
  11662. }
  11663. return true;
  11664. }
  11665. case GGML_OP_GET_ROWS:
  11666. {
  11667. switch (op->src[0]->type) {
  11668. case GGML_TYPE_F32:
  11669. case GGML_TYPE_F16:
  11670. case GGML_TYPE_BF16:
  11671. case GGML_TYPE_Q4_0:
  11672. case GGML_TYPE_Q4_1:
  11673. case GGML_TYPE_Q5_0:
  11674. case GGML_TYPE_Q5_1:
  11675. case GGML_TYPE_Q8_0:
  11676. case GGML_TYPE_Q2_K:
  11677. case GGML_TYPE_Q3_K:
  11678. case GGML_TYPE_Q4_K:
  11679. case GGML_TYPE_Q5_K:
  11680. case GGML_TYPE_Q6_K:
  11681. case GGML_TYPE_IQ1_S:
  11682. case GGML_TYPE_IQ1_M:
  11683. case GGML_TYPE_IQ2_XXS:
  11684. case GGML_TYPE_IQ2_XS:
  11685. case GGML_TYPE_IQ2_S:
  11686. case GGML_TYPE_IQ3_XXS:
  11687. case GGML_TYPE_IQ3_S:
  11688. case GGML_TYPE_IQ4_XS:
  11689. case GGML_TYPE_IQ4_NL:
  11690. case GGML_TYPE_MXFP4:
  11691. return true;
  11692. default:
  11693. return false;
  11694. }
  11695. }
  11696. case GGML_OP_SET_ROWS:
  11697. {
  11698. switch (op->type) {
  11699. case GGML_TYPE_F32:
  11700. case GGML_TYPE_F16:
  11701. case GGML_TYPE_BF16:
  11702. case GGML_TYPE_Q4_0:
  11703. case GGML_TYPE_Q4_1:
  11704. case GGML_TYPE_Q5_0:
  11705. case GGML_TYPE_Q5_1:
  11706. case GGML_TYPE_Q8_0:
  11707. case GGML_TYPE_IQ4_NL:
  11708. return true;
  11709. default:
  11710. return false;
  11711. }
  11712. }
  11713. case GGML_OP_CONT:
  11714. case GGML_OP_CPY:
  11715. case GGML_OP_DUP:
  11716. {
  11717. ggml_type src0_type = op->src[0]->type;
  11718. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  11719. if (src0_type == GGML_TYPE_F32) {
  11720. switch (src1_type) {
  11721. case GGML_TYPE_F32:
  11722. case GGML_TYPE_F16:
  11723. case GGML_TYPE_BF16:
  11724. case GGML_TYPE_Q4_0:
  11725. case GGML_TYPE_Q4_1:
  11726. case GGML_TYPE_Q5_0:
  11727. case GGML_TYPE_Q5_1:
  11728. case GGML_TYPE_Q8_0:
  11729. case GGML_TYPE_IQ4_NL:
  11730. return true;
  11731. default:
  11732. break;
  11733. }
  11734. }
  11735. if (src1_type == GGML_TYPE_F32) {
  11736. switch (src0_type) {
  11737. case GGML_TYPE_F16:
  11738. case GGML_TYPE_Q4_0:
  11739. case GGML_TYPE_Q4_1:
  11740. case GGML_TYPE_Q5_0:
  11741. case GGML_TYPE_Q5_1:
  11742. case GGML_TYPE_Q8_0:
  11743. case GGML_TYPE_IQ4_NL:
  11744. return true;
  11745. default:
  11746. break;
  11747. }
  11748. }
  11749. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  11750. return true;
  11751. }
  11752. if (
  11753. (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_I32) ||
  11754. (src0_type == GGML_TYPE_I32 && src1_type == GGML_TYPE_F32)
  11755. ) {
  11756. return true;
  11757. }
  11758. // We can handle copying from a type to the same type if it's
  11759. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  11760. // so the type/block size must be a multiple of 4.
  11761. if (src0_type == src1_type &&
  11762. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  11763. (ggml_type_size(src0_type) % 2) == 0) {
  11764. return true;
  11765. }
  11766. return false;
  11767. }
  11768. case GGML_OP_REPEAT:
  11769. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  11770. case GGML_OP_REPEAT_BACK:
  11771. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  11772. case GGML_OP_ROPE:
  11773. case GGML_OP_ROPE_BACK:
  11774. case GGML_OP_NONE:
  11775. case GGML_OP_RESHAPE:
  11776. case GGML_OP_VIEW:
  11777. case GGML_OP_PERMUTE:
  11778. case GGML_OP_TRANSPOSE:
  11779. case GGML_OP_RMS_NORM:
  11780. return true;
  11781. case GGML_OP_NORM:
  11782. case GGML_OP_GROUP_NORM:
  11783. case GGML_OP_L2_NORM:
  11784. return ggml_is_contiguous(op->src[0]);
  11785. case GGML_OP_ADD:
  11786. case GGML_OP_SUB:
  11787. case GGML_OP_MUL:
  11788. case GGML_OP_DIV:
  11789. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11790. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  11791. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  11792. case GGML_OP_ADD_ID:
  11793. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32 && op->src[2]->type == GGML_TYPE_I32 &&
  11794. op->type == GGML_TYPE_F32;
  11795. case GGML_OP_SILU_BACK:
  11796. case GGML_OP_RMS_NORM_BACK:
  11797. case GGML_OP_SQR:
  11798. case GGML_OP_SQRT:
  11799. case GGML_OP_SIN:
  11800. case GGML_OP_COS:
  11801. case GGML_OP_CLAMP:
  11802. case GGML_OP_LEAKY_RELU:
  11803. case GGML_OP_OPT_STEP_ADAMW:
  11804. case GGML_OP_OPT_STEP_SGD:
  11805. return op->src[0]->type == GGML_TYPE_F32;
  11806. case GGML_OP_ARGSORT:
  11807. return op->ne[0] <= max_argsort_cols;
  11808. case GGML_OP_UPSCALE:
  11809. case GGML_OP_ACC:
  11810. case GGML_OP_CONCAT:
  11811. case GGML_OP_SCALE:
  11812. case GGML_OP_PAD:
  11813. case GGML_OP_ROLL:
  11814. case GGML_OP_DIAG_MASK_INF:
  11815. case GGML_OP_SOFT_MAX:
  11816. case GGML_OP_SOFT_MAX_BACK:
  11817. return true;
  11818. case GGML_OP_SUM:
  11819. case GGML_OP_SUM_ROWS:
  11820. case GGML_OP_MEAN:
  11821. return op->src[0]->type == GGML_TYPE_F32 && ggml_is_contiguous_rows(op->src[0]);
  11822. case GGML_OP_ARGMAX:
  11823. case GGML_OP_COUNT_EQUAL:
  11824. case GGML_OP_IM2COL:
  11825. case GGML_OP_IM2COL_3D:
  11826. case GGML_OP_TIMESTEP_EMBEDDING:
  11827. case GGML_OP_CONV_2D_DW:
  11828. case GGML_OP_POOL_2D:
  11829. case GGML_OP_RWKV_WKV6:
  11830. case GGML_OP_RWKV_WKV7:
  11831. return true;
  11832. case GGML_OP_SSM_SCAN:
  11833. {
  11834. for (int i = 0; i < 6; i++) {
  11835. if (op->src[i] && ggml_is_quantized(op->src[i]->type)) {
  11836. return false;
  11837. }
  11838. }
  11839. if (op->src[6] && op->src[6]->type != GGML_TYPE_I32) {
  11840. return false;
  11841. }
  11842. if (op->src[0]->type != GGML_TYPE_F32 || op->type != GGML_TYPE_F32) {
  11843. return false;
  11844. }
  11845. const uint32_t d_state = op->src[0]->ne[0];
  11846. const uint32_t head_dim = op->src[0]->ne[1];
  11847. bool is_mamba2 = (op->src[3] && op->src[3]->nb[1] == sizeof(float));
  11848. if (!is_mamba2) {
  11849. return false;
  11850. }
  11851. if ((d_state != 128 && d_state != 256) || head_dim % 16 != 0) {
  11852. return false;
  11853. }
  11854. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11855. const vk_device& device = ggml_vk_get_device(ctx->device);
  11856. const uint32_t SPLIT_H = 16;
  11857. size_t stateC_size = SPLIT_H * d_state * sizeof(float);
  11858. if (stateC_size > device->properties.limits.maxComputeSharedMemorySize) {
  11859. return false;
  11860. }
  11861. return true;
  11862. }
  11863. case GGML_OP_SSM_CONV:
  11864. return true;
  11865. case GGML_OP_CONV_TRANSPOSE_1D:
  11866. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  11867. case GGML_OP_CONV_2D:
  11868. case GGML_OP_CONV_TRANSPOSE_2D:
  11869. {
  11870. // Op is disabled for Apple because it segfaults at pipeline create time on MoltenVK
  11871. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11872. const vk_device& device = ggml_vk_get_device(ctx->device);
  11873. if (op->op == GGML_OP_CONV_TRANSPOSE_2D &&
  11874. device->properties.limits.maxPushConstantsSize < sizeof(vk_op_conv_transpose_2d_push_constants)) {
  11875. return false;
  11876. }
  11877. // Channel-contiguous format is not supported yet.
  11878. return ((op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  11879. op->src[1]->type == GGML_TYPE_F32 &&
  11880. op->type == GGML_TYPE_F32 &&
  11881. ggml_is_contiguous(op->src[0]) &&
  11882. ggml_is_contiguous(op->src[1]) &&
  11883. ggml_is_contiguous(op));
  11884. }
  11885. default:
  11886. return false;
  11887. }
  11888. UNUSED(dev);
  11889. }
  11890. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  11891. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  11892. return false;
  11893. }
  11894. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  11895. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  11896. return buft_ctx->device->idx == ctx->device;
  11897. }
  11898. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  11899. const int min_batch_size = 32;
  11900. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  11901. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  11902. UNUSED(dev);
  11903. }
  11904. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  11905. /* .get_name = */ ggml_backend_vk_device_get_name,
  11906. /* .get_description = */ ggml_backend_vk_device_get_description,
  11907. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  11908. /* .get_type = */ ggml_backend_vk_device_get_type,
  11909. /* .get_props = */ ggml_backend_vk_device_get_props,
  11910. /* .init_backend = */ ggml_backend_vk_device_init,
  11911. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  11912. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  11913. /* .buffer_from_host_ptr = */ NULL,
  11914. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  11915. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  11916. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  11917. /* .event_new = */ NULL,
  11918. /* .event_free = */ NULL,
  11919. /* .event_synchronize = */ NULL,
  11920. };
  11921. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  11922. UNUSED(reg);
  11923. return GGML_VK_NAME;
  11924. }
  11925. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  11926. UNUSED(reg);
  11927. return ggml_backend_vk_get_device_count();
  11928. }
  11929. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  11930. static std::vector<ggml_backend_dev_t> devices;
  11931. static bool initialized = false;
  11932. {
  11933. static std::mutex mutex;
  11934. std::lock_guard<std::mutex> lock(mutex);
  11935. if (!initialized) {
  11936. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  11937. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  11938. char desc[256];
  11939. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  11940. ctx->device = i;
  11941. ctx->name = GGML_VK_NAME + std::to_string(i);
  11942. ctx->description = desc;
  11943. ctx->is_integrated_gpu = ggml_backend_vk_get_device_type(i) == vk::PhysicalDeviceType::eIntegratedGpu;
  11944. ctx->pci_bus_id = ggml_backend_vk_get_device_pci_id(i);
  11945. devices.push_back(new ggml_backend_device {
  11946. /* .iface = */ ggml_backend_vk_device_i,
  11947. /* .reg = */ reg,
  11948. /* .context = */ ctx,
  11949. });
  11950. }
  11951. initialized = true;
  11952. }
  11953. }
  11954. GGML_ASSERT(device < devices.size());
  11955. return devices[device];
  11956. }
  11957. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  11958. /* .get_name = */ ggml_backend_vk_reg_get_name,
  11959. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  11960. /* .get_device = */ ggml_backend_vk_reg_get_device,
  11961. /* .get_proc_address = */ NULL,
  11962. };
  11963. ggml_backend_reg_t ggml_backend_vk_reg() {
  11964. static ggml_backend_reg reg = {
  11965. /* .api_version = */ GGML_BACKEND_API_VERSION,
  11966. /* .iface = */ ggml_backend_vk_reg_i,
  11967. /* .context = */ nullptr,
  11968. };
  11969. try {
  11970. ggml_vk_instance_init();
  11971. return &reg;
  11972. } catch (const vk::SystemError& e) {
  11973. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  11974. return nullptr;
  11975. } catch (const std::exception &e) {
  11976. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: " << e.what());
  11977. return nullptr;
  11978. } catch (...) {
  11979. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: unknown exception during Vulkan init");
  11980. return nullptr;
  11981. }
  11982. }
  11983. // Extension availability
  11984. static bool ggml_vk_instance_validation_ext_available() {
  11985. #ifdef GGML_VULKAN_VALIDATE
  11986. // Check if validation layer provides the extension
  11987. const std::string layer_name = "VK_LAYER_KHRONOS_validation";
  11988. for (const auto& layer : vk::enumerateInstanceLayerProperties()) {
  11989. if (layer_name == layer.layerName.data()) {
  11990. for (const auto& ext : vk::enumerateInstanceExtensionProperties(layer_name)) {
  11991. if (strcmp("VK_EXT_validation_features", ext.extensionName.data()) == 0) {
  11992. return true;
  11993. }
  11994. }
  11995. }
  11996. }
  11997. std::cerr << "ggml_vulkan: WARNING: Validation layer or layer extension VK_EXT_validation_features not found." << std::endl;
  11998. #endif
  11999. return false;
  12000. }
  12001. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  12002. #ifdef __APPLE__
  12003. // Check for portability enumeration extension for MoltenVK support
  12004. for (const auto& properties : instance_extensions) {
  12005. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  12006. return true;
  12007. }
  12008. }
  12009. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  12010. #endif
  12011. return false;
  12012. UNUSED(instance_extensions);
  12013. }
  12014. // Extension availability
  12015. static bool ggml_vk_instance_debug_utils_ext_available(
  12016. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  12017. // Check for portability enumeration extension for MoltenVK support
  12018. for (const auto & properties : instance_extensions) {
  12019. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  12020. return true;
  12021. }
  12022. }
  12023. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  12024. return false;
  12025. UNUSED(instance_extensions);
  12026. }
  12027. static bool ggml_vk_device_is_supported(const vk::PhysicalDevice & vkdev) {
  12028. VkPhysicalDeviceFeatures2 device_features2;
  12029. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  12030. VkPhysicalDeviceVulkan11Features vk11_features;
  12031. vk11_features.pNext = nullptr;
  12032. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  12033. device_features2.pNext = &vk11_features;
  12034. vkGetPhysicalDeviceFeatures2(vkdev, &device_features2);
  12035. return vk11_features.storageBuffer16BitAccess;
  12036. }
  12037. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  12038. switch (props.vendorID) {
  12039. case VK_VENDOR_ID_INTEL:
  12040. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  12041. // while some older hardware (ex. Arc A770) has performance regressions
  12042. return arch == vk_device_architecture::INTEL_XE2;
  12043. case VK_VENDOR_ID_AMD:
  12044. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  12045. // Workaround for AMD proprietary driver reporting support on all GPUs
  12046. return arch == vk_device_architecture::AMD_RDNA3;
  12047. }
  12048. return true;
  12049. default:
  12050. return true;
  12051. }
  12052. }
  12053. // checks
  12054. #ifdef GGML_VULKAN_CHECK_RESULTS
  12055. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  12056. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  12057. return;
  12058. }
  12059. for (int j = 0; j < level; j++) {
  12060. std::cerr << " ";
  12061. }
  12062. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  12063. done.push_back(tensor);
  12064. for (int i = 0; i < GGML_MAX_SRC; i++) {
  12065. if (tensor->src[i] != nullptr) {
  12066. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  12067. }
  12068. }
  12069. }
  12070. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  12071. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  12072. return;
  12073. }
  12074. i0 = std::max(i0, 5);
  12075. i1 = std::max(i1, 5);
  12076. i2 = std::max(i2, 0);
  12077. i3 = std::max(i3, 0);
  12078. fprintf(stderr, " ");
  12079. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  12080. fprintf(stderr, "%7d ", idx1);
  12081. }
  12082. fprintf(stderr, "\n");
  12083. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  12084. fprintf(stderr, "%7d: ", idx0);
  12085. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  12086. 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]) {
  12087. float val;
  12088. if (tensor->type == GGML_TYPE_F32) {
  12089. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  12090. } else if (tensor->type == GGML_TYPE_F16) {
  12091. 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]));
  12092. } else if (tensor->type == GGML_TYPE_I32) {
  12093. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  12094. } else {
  12095. GGML_ABORT("fatal error");
  12096. }
  12097. fprintf(stderr, "% 7.2f ", val);
  12098. } else {
  12099. fprintf(stderr, " ");
  12100. }
  12101. }
  12102. fprintf(stderr, "\n");
  12103. }
  12104. }
  12105. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  12106. void * tensor_data = tensor->data;
  12107. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  12108. if (is_gpu) {
  12109. const size_t tensor_size = ggml_nbytes(tensor);
  12110. tensor_data = malloc(tensor_size);
  12111. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  12112. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  12113. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  12114. }
  12115. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  12116. 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;
  12117. if (tensor->src[0] != nullptr) {
  12118. 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;
  12119. }
  12120. if (tensor->src[1] != nullptr) {
  12121. 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;
  12122. }
  12123. std::cerr << std::endl << "Result:" << std::endl;
  12124. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  12125. std::cerr << std::endl;
  12126. std::vector<const ggml_tensor *> done;
  12127. ggml_vk_print_graph_origin(tensor, done);
  12128. if (is_gpu) {
  12129. free(tensor_data);
  12130. }
  12131. }
  12132. void * comp_result;
  12133. size_t comp_size;
  12134. size_t comp_nb[GGML_MAX_DIMS];
  12135. size_t check_counter = 0;
  12136. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  12137. ggml_tensor * tensor = cgraph->nodes[tensor_idx + ctx->num_additional_fused_ops];
  12138. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  12139. return;
  12140. }
  12141. check_counter++;
  12142. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  12143. return;
  12144. }
  12145. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  12146. struct ggml_init_params iparams = {
  12147. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  12148. /*.mem_buffer =*/ NULL,
  12149. /*.no_alloc =*/ false,
  12150. };
  12151. struct ggml_context * ggml_ctx = ggml_init(iparams);
  12152. std::array<struct ggml_tensor *, GGML_MAX_SRC> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  12153. const char * srci_name[GGML_MAX_SRC] = {"src0", "src1", "src2", "src3", "src4", "src5", "src6", "src7", "src8", "src9"};
  12154. std::map<ggml_tensor *, ggml_tensor *> cloned_tensors;
  12155. std::vector<void *> cloned_mallocs;
  12156. struct ggml_tensor * tensor_clone = nullptr;
  12157. for (int f = 0; f < ctx->num_additional_fused_ops + 1; ++f) {
  12158. tensor = cgraph->nodes[tensor_idx + f];
  12159. for (int i = 0; i < GGML_MAX_SRC; i++) {
  12160. ggml_tensor * srci = tensor->src[i];
  12161. if (srci == nullptr) {
  12162. continue;
  12163. }
  12164. // If a src tensor has been cloned, use that one
  12165. auto it = cloned_tensors.find(srci);
  12166. if (it != cloned_tensors.end()) {
  12167. src_clone[i] = it->second;
  12168. continue;
  12169. }
  12170. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  12171. size_t srci_size = ggml_nbytes(srci);
  12172. src_clone[i] = srci_clone;
  12173. void *src_buffer = malloc(srci_size);
  12174. cloned_mallocs.push_back(src_buffer);
  12175. srci_clone->data = src_buffer;
  12176. if (ggml_backend_buffer_is_host(srci->buffer)) {
  12177. memcpy(srci_clone->data, srci->data, srci_size);
  12178. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  12179. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  12180. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  12181. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  12182. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  12183. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  12184. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  12185. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  12186. const int idx = i3*srci->ne[2] + i2;
  12187. 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]);
  12188. }
  12189. }
  12190. srci_clone->nb[0] = srci->nb[0];
  12191. srci_clone->nb[1] = srci->nb[1];
  12192. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  12193. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  12194. }
  12195. } else {
  12196. if (offset + srci_size >= buffer_gpu->size) {
  12197. srci_size = buffer_gpu->size - offset;
  12198. }
  12199. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  12200. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  12201. }
  12202. } else {
  12203. GGML_ABORT("fatal error");
  12204. }
  12205. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  12206. ggml_vk_print_tensor(srci, srci_name[i]);
  12207. }
  12208. }
  12209. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  12210. const float * params = (const float *)tensor->op_params;
  12211. 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]);
  12212. if (src_clone[4]) {
  12213. ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]);
  12214. }
  12215. } else if (tensor->op == GGML_OP_MUL_MAT) {
  12216. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  12217. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  12218. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  12219. } else if (tensor->op == GGML_OP_SUB) {
  12220. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  12221. } else if (tensor->op == GGML_OP_MUL) {
  12222. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  12223. } else if (tensor->op == GGML_OP_DIV) {
  12224. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  12225. } else if (tensor->op == GGML_OP_CONCAT) {
  12226. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  12227. } else if (tensor->op == GGML_OP_UPSCALE) {
  12228. tensor_clone = ggml_interpolate(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  12229. } else if (tensor->op == GGML_OP_SCALE) {
  12230. const float * params = (const float *)tensor->op_params;
  12231. tensor_clone = ggml_scale_bias(ggml_ctx, src_clone[0], params[0], params[1]);
  12232. } else if (tensor->op == GGML_OP_SQR) {
  12233. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  12234. } else if (tensor->op == GGML_OP_SQRT) {
  12235. tensor_clone = ggml_sqrt(ggml_ctx, src_clone[0]);
  12236. } else if (tensor->op == GGML_OP_SIN) {
  12237. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  12238. } else if (tensor->op == GGML_OP_COS) {
  12239. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  12240. } else if (tensor->op == GGML_OP_CLAMP) {
  12241. const float * params = (const float *)tensor->op_params;
  12242. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  12243. } else if (tensor->op == GGML_OP_PAD) {
  12244. tensor_clone = ggml_pad_ext(ggml_ctx, src_clone[0], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3],
  12245. tensor->op_params[4], tensor->op_params[5], tensor->op_params[6], tensor->op_params[7]);
  12246. } else if (tensor->op == GGML_OP_REPEAT) {
  12247. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  12248. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  12249. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  12250. } else if (tensor->op == GGML_OP_ADD) {
  12251. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  12252. } else if (tensor->op == GGML_OP_ACC) {
  12253. 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]);
  12254. } else if (tensor->op == GGML_OP_NORM) {
  12255. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  12256. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  12257. const float * float_params = (const float *)tensor->op_params;
  12258. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  12259. } else if (tensor->op == GGML_OP_RMS_NORM) {
  12260. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  12261. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  12262. const float eps = ((float *) tensor->op_params)[0];
  12263. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  12264. } else if (tensor->op == GGML_OP_SILU_BACK) {
  12265. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  12266. } else if (tensor->op == GGML_OP_L2_NORM) {
  12267. const float eps = ((float *) tensor->op_params)[0];
  12268. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  12269. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  12270. if (tensor->src[1] != nullptr) {
  12271. const float * params = (const float *)tensor->op_params;
  12272. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  12273. } else {
  12274. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  12275. }
  12276. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  12277. 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]);
  12278. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  12279. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  12280. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  12281. const int n_dims = ((int32_t *) tensor->op_params)[1];
  12282. const int mode = ((int32_t *) tensor->op_params)[2];
  12283. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  12284. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  12285. const float freq_base = ((float *) tensor->op_params)[5];
  12286. const float freq_scale = ((float *) tensor->op_params)[6];
  12287. const float ext_factor = ((float *) tensor->op_params)[7];
  12288. const float attn_factor = ((float *) tensor->op_params)[8];
  12289. const float beta_fast = ((float *) tensor->op_params)[9];
  12290. const float beta_slow = ((float *) tensor->op_params)[10];
  12291. if (mode & GGML_ROPE_TYPE_MROPE) {
  12292. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  12293. if (tensor->op == GGML_OP_ROPE) {
  12294. 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);
  12295. } else {
  12296. 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);
  12297. }
  12298. } else {
  12299. if (tensor->op == GGML_OP_ROPE) {
  12300. 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);
  12301. } else {
  12302. 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);
  12303. }
  12304. }
  12305. } else if (tensor->op == GGML_OP_UNARY) {
  12306. switch (ggml_get_unary_op(tensor)) {
  12307. case GGML_UNARY_OP_EXP:
  12308. tensor_clone = ggml_exp(ggml_ctx, src_clone[0]);
  12309. break;
  12310. case GGML_UNARY_OP_SILU:
  12311. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  12312. break;
  12313. case GGML_UNARY_OP_GELU:
  12314. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  12315. break;
  12316. case GGML_UNARY_OP_GELU_ERF:
  12317. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  12318. break;
  12319. case GGML_UNARY_OP_GELU_QUICK:
  12320. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  12321. break;
  12322. case GGML_UNARY_OP_RELU:
  12323. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  12324. break;
  12325. case GGML_UNARY_OP_TANH:
  12326. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  12327. break;
  12328. case GGML_UNARY_OP_SIGMOID:
  12329. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  12330. break;
  12331. case GGML_UNARY_OP_HARDSIGMOID:
  12332. tensor_clone = ggml_hardsigmoid(ggml_ctx, src_clone[0]);
  12333. break;
  12334. case GGML_UNARY_OP_HARDSWISH:
  12335. tensor_clone = ggml_hardswish(ggml_ctx, src_clone[0]);
  12336. break;
  12337. default:
  12338. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  12339. GGML_ABORT("fatal error");
  12340. }
  12341. } else if (tensor->op == GGML_OP_GLU) {
  12342. if (src_clone[1] == nullptr) {
  12343. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  12344. } else {
  12345. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  12346. }
  12347. ggml_set_op_params_i32(tensor_clone, 2, ggml_get_op_params_i32(tensor, 2));
  12348. ggml_set_op_params_i32(tensor_clone, 3, ggml_get_op_params_i32(tensor, 3));
  12349. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  12350. if (tensor->src[1] == nullptr) {
  12351. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  12352. tensor_clone->type = tensor->type;
  12353. } else {
  12354. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  12355. }
  12356. } else if (tensor->op == GGML_OP_CONT) {
  12357. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  12358. } else if (tensor->op == GGML_OP_RESHAPE) {
  12359. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  12360. } else if (tensor->op == GGML_OP_VIEW) {
  12361. 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]);
  12362. } else if (tensor->op == GGML_OP_PERMUTE) {
  12363. int32_t * params = (int32_t *)tensor->op_params;
  12364. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  12365. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  12366. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  12367. } else if (tensor->op == GGML_OP_GET_ROWS) {
  12368. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  12369. } else if (tensor->op == GGML_OP_ARGSORT) {
  12370. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  12371. } else if (tensor->op == GGML_OP_SUM) {
  12372. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  12373. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  12374. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  12375. } else if (tensor->op == GGML_OP_MEAN) {
  12376. tensor_clone = ggml_mean(ggml_ctx, src_clone[0]);
  12377. } else if (tensor->op == GGML_OP_ARGMAX) {
  12378. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  12379. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  12380. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  12381. } else if (tensor->op == GGML_OP_IM2COL) {
  12382. const int32_t s0 = tensor->op_params[0];
  12383. const int32_t s1 = tensor->op_params[1];
  12384. const int32_t p0 = tensor->op_params[2];
  12385. const int32_t p1 = tensor->op_params[3];
  12386. const int32_t d0 = tensor->op_params[4];
  12387. const int32_t d1 = tensor->op_params[5];
  12388. const bool is_2D = tensor->op_params[6] == 1;
  12389. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  12390. } else if (tensor->op == GGML_OP_IM2COL_3D) {
  12391. const int32_t s0 = tensor->op_params[0];
  12392. const int32_t s1 = tensor->op_params[1];
  12393. const int32_t s2 = tensor->op_params[2];
  12394. const int32_t p0 = tensor->op_params[3];
  12395. const int32_t p1 = tensor->op_params[4];
  12396. const int32_t p2 = tensor->op_params[5];
  12397. const int32_t d0 = tensor->op_params[6];
  12398. const int32_t d1 = tensor->op_params[7];
  12399. const int32_t d2 = tensor->op_params[8];
  12400. const int32_t IC = tensor->op_params[9];
  12401. tensor_clone = ggml_im2col_3d(ggml_ctx, src_clone[0], src_clone[1], IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, tensor->type);
  12402. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  12403. const int32_t dim = tensor->op_params[0];
  12404. const int32_t max_period = tensor->op_params[1];
  12405. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  12406. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  12407. const int32_t s0 = tensor->op_params[0];
  12408. const int32_t p0 = tensor->op_params[1];
  12409. const int32_t d0 = tensor->op_params[2];
  12410. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  12411. } else if (tensor->op == GGML_OP_POOL_2D) {
  12412. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  12413. const int32_t k0 = tensor->op_params[1];
  12414. const int32_t k1 = tensor->op_params[2];
  12415. const int32_t s0 = tensor->op_params[3];
  12416. const int32_t s1 = tensor->op_params[4];
  12417. const int32_t p0 = tensor->op_params[5];
  12418. const int32_t p1 = tensor->op_params[6];
  12419. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  12420. } else if (tensor->op == GGML_OP_CONV_2D) {
  12421. const int32_t s0 = tensor->op_params[0];
  12422. const int32_t s1 = tensor->op_params[1];
  12423. const int32_t p0 = tensor->op_params[2];
  12424. const int32_t p1 = tensor->op_params[3];
  12425. const int32_t d0 = tensor->op_params[4];
  12426. const int32_t d1 = tensor->op_params[5];
  12427. tensor_clone = ggml_conv_2d(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1);
  12428. } else if (tensor->op == GGML_OP_CONV_2D_DW) {
  12429. const int32_t s0 = tensor->op_params[0];
  12430. const int32_t s1 = tensor->op_params[1];
  12431. const int32_t p0 = tensor->op_params[2];
  12432. const int32_t p1 = tensor->op_params[3];
  12433. const int32_t d0 = tensor->op_params[4];
  12434. const int32_t d1 = tensor->op_params[5];
  12435. tensor_clone = ggml_conv_2d_dw_direct(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1);
  12436. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_2D) {
  12437. const int32_t s = tensor->op_params[0];
  12438. tensor_clone = ggml_conv_transpose_2d_p0(ggml_ctx, src_clone[0], src_clone[1], s);
  12439. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  12440. const float * op_params = (const float *)tensor->op_params;
  12441. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  12442. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  12443. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  12444. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  12445. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  12446. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  12447. src_clone[4], src_clone[5], src_clone[6]);
  12448. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  12449. src_clone[0]->flags = tensor->src[0]->flags;
  12450. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  12451. src_clone[2], src_clone[3], src_clone[4]);
  12452. } else if (tensor->op == GGML_OP_OPT_STEP_SGD) {
  12453. src_clone[0]->flags = tensor->src[0]->flags;
  12454. tensor_clone = ggml_opt_step_sgd(ggml_ctx, src_clone[0], src_clone[1],
  12455. src_clone[2]);
  12456. } else if (tensor->op == GGML_OP_ADD_ID) {
  12457. tensor_clone = ggml_add_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  12458. } else if (tensor->op == GGML_OP_SSM_SCAN) {
  12459. tensor_clone = ggml_ssm_scan(ggml_ctx, src_clone[0], src_clone[1], src_clone[2],
  12460. src_clone[3], src_clone[4], src_clone[5], src_clone[6]);
  12461. } else if (tensor->op == GGML_OP_SSM_CONV) {
  12462. tensor_clone = ggml_ssm_conv(ggml_ctx, src_clone[0], src_clone[1]);
  12463. } else if (tensor->op == GGML_OP_ROLL) {
  12464. const int32_t s0 = tensor->op_params[0];
  12465. const int32_t s1 = tensor->op_params[1];
  12466. const int32_t s2 = tensor->op_params[2];
  12467. const int32_t s3 = tensor->op_params[3];
  12468. tensor_clone = ggml_roll(ggml_ctx, src_clone[0], s0, s1, s2, s3);
  12469. }
  12470. else {
  12471. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  12472. GGML_ABORT("fatal error");
  12473. }
  12474. cloned_tensors[tensor] = tensor_clone;
  12475. }
  12476. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  12477. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  12478. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  12479. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  12480. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  12481. }
  12482. comp_size = ggml_nbytes(tensor_clone);
  12483. comp_result = malloc(comp_size);
  12484. memcpy(comp_result, tensor_clone->data, comp_size);
  12485. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  12486. for (auto m : cloned_mallocs) {
  12487. free(m);
  12488. }
  12489. ggml_free(ggml_ctx);
  12490. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  12491. }
  12492. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  12493. ggml_tensor * tensor = cgraph->nodes[tensor_idx + ctx->num_additional_fused_ops];
  12494. if (tensor->op == GGML_OP_TRANSPOSE || tensor->op == GGML_OP_SET_ROWS) {
  12495. return;
  12496. }
  12497. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  12498. return;
  12499. }
  12500. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  12501. ggml_tensor * src0 = tensor->src[0];
  12502. ggml_tensor * src1 = tensor->src[1];
  12503. ggml_tensor * src2 = tensor->src[2];
  12504. ggml_tensor * src3 = tensor->src[3];
  12505. void * tensor_data = tensor->data;
  12506. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  12507. size_t tensor_size = ggml_nbytes(tensor);
  12508. tensor_data = malloc(tensor_size);
  12509. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  12510. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  12511. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  12512. if (offset + tensor_size >= buffer_gpu->size) {
  12513. tensor_size = buffer_gpu->size - offset;
  12514. }
  12515. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  12516. }
  12517. float first_error_result = -1.0f;
  12518. float first_error_correct = -1.0f;
  12519. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  12520. double avg_err = 0.0;
  12521. size_t counter = 0;
  12522. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  12523. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  12524. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  12525. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  12526. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  12527. float correct = 0.0f;
  12528. float result = 0.0f;
  12529. if (buffer_size_fit) {
  12530. if (tensor->type == GGML_TYPE_F32) {
  12531. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  12532. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  12533. } else if (tensor->type == GGML_TYPE_F16) {
  12534. 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]));
  12535. 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]));
  12536. } else if (tensor->type == GGML_TYPE_BF16) {
  12537. correct = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]));
  12538. result = ggml_bf16_to_fp32(*(ggml_bf16_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]));
  12539. } else if (tensor->type == GGML_TYPE_I32) {
  12540. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  12541. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  12542. } else if (tensor->type == GGML_TYPE_I64) {
  12543. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  12544. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  12545. } else {
  12546. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  12547. }
  12548. } else {
  12549. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  12550. GGML_ABORT("fatal error");
  12551. }
  12552. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  12553. 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;
  12554. 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;
  12555. if (src0 != nullptr) {
  12556. 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;
  12557. }
  12558. if (src1 != nullptr) {
  12559. 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;
  12560. }
  12561. if (src2 != nullptr) {
  12562. 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;
  12563. }
  12564. if (src3 != nullptr) {
  12565. 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;
  12566. }
  12567. 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;
  12568. std::cerr << std::endl << "Result:" << std::endl;
  12569. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  12570. std::cerr << std::endl << "Correct:" << std::endl;
  12571. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  12572. std::cerr << std::endl;
  12573. std::vector<const ggml_tensor *> done;
  12574. ggml_vk_print_graph_origin(tensor, done);
  12575. GGML_ABORT("fatal error");
  12576. }
  12577. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  12578. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  12579. first_error[0] = i0;
  12580. first_error[1] = i1;
  12581. first_error[2] = i2;
  12582. first_error[3] = i3;
  12583. first_error_result = result;
  12584. first_error_correct = correct;
  12585. }
  12586. // Special case, value is infinite, avoid NaN result in avg_err
  12587. // NaN also appears in results, if both are nan error is 0
  12588. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  12589. avg_err += std::fabs(correct - result) / denom;
  12590. }
  12591. counter++;
  12592. }
  12593. }
  12594. }
  12595. }
  12596. avg_err /= counter;
  12597. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  12598. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  12599. 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;
  12600. if (src0 != nullptr) {
  12601. 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;
  12602. }
  12603. if (src1 != nullptr) {
  12604. 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;
  12605. }
  12606. if (src2 != nullptr) {
  12607. 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;
  12608. }
  12609. if (src3 != nullptr) {
  12610. 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;
  12611. }
  12612. 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;
  12613. std::cerr << std::endl << "Result:" << std::endl;
  12614. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  12615. std::cerr << std::endl << "Correct:" << std::endl;
  12616. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  12617. std::cerr << std::endl;
  12618. std::vector<const ggml_tensor *> done;
  12619. ggml_vk_print_graph_origin(tensor, done);
  12620. }
  12621. if (avg_err > 0.5 || std::isnan(avg_err)) {
  12622. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  12623. 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;
  12624. if (src0 != nullptr) {
  12625. 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;
  12626. }
  12627. if (src1 != nullptr) {
  12628. 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;
  12629. }
  12630. if (src2 != nullptr) {
  12631. 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;
  12632. }
  12633. if (src3 != nullptr) {
  12634. 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;
  12635. }
  12636. 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;
  12637. std::cerr << std::endl << "Result:" << std::endl;
  12638. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  12639. std::cerr << std::endl << "Correct:" << std::endl;
  12640. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  12641. std::cerr << std::endl;
  12642. std::vector<const ggml_tensor *> done;
  12643. ggml_vk_print_graph_origin(tensor, done);
  12644. GGML_ABORT("fatal error");
  12645. } else {
  12646. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  12647. }
  12648. free(comp_result);
  12649. comp_result = nullptr;
  12650. comp_size = 0;
  12651. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  12652. free(tensor_data);
  12653. }
  12654. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  12655. }
  12656. #endif
  12657. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)