ggml-vulkan.cpp 570 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #if defined(_MSC_VER)
  25. # define NOMINMAX 1
  26. # include <windows.h>
  27. # define YIELD() YieldProcessor()
  28. #elif defined(__clang__) || defined(__GNUC__)
  29. # if defined(__x86_64__) ||defined(__i386__)
  30. # include <immintrin.h>
  31. # define YIELD() _mm_pause()
  32. # elif defined(__arm__) || defined(__aarch64__)
  33. # if defined(__clang__)
  34. # include <arm_acle.h>
  35. # define YIELD() __yield()
  36. # else
  37. # define YIELD() asm volatile("yield")
  38. # endif
  39. # endif
  40. #endif
  41. #if !defined(YIELD)
  42. #define YIELD()
  43. #endif
  44. #include "ggml-impl.h"
  45. #include "ggml-backend-impl.h"
  46. #include "ggml-vulkan-shaders.hpp"
  47. // remove this once it's more widely available in the SDK
  48. #if !defined(VK_KHR_shader_bfloat16)
  49. #define VK_KHR_shader_bfloat16 1
  50. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  51. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  52. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  53. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  54. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  55. VkStructureType sType;
  56. void* pNext;
  57. VkBool32 shaderBFloat16Type;
  58. VkBool32 shaderBFloat16DotProduct;
  59. VkBool32 shaderBFloat16CooperativeMatrix;
  60. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  61. #endif
  62. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  63. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  64. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  65. #define VK_VENDOR_ID_AMD 0x1002
  66. #define VK_VENDOR_ID_APPLE 0x106b
  67. #define VK_VENDOR_ID_INTEL 0x8086
  68. #define VK_VENDOR_ID_NVIDIA 0x10de
  69. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  70. #define GGML_VK_MAX_NODES 8192
  71. #define MAX_VK_BUFFERS 256
  72. #define VK_CHECK(err, msg) \
  73. do { \
  74. vk::Result err_ = (err); \
  75. if (err_ != vk::Result::eSuccess) { \
  76. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  77. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  78. exit(1); \
  79. } \
  80. } while (0)
  81. #ifdef GGML_VULKAN_DEBUG
  82. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  83. #else
  84. #define VK_LOG_DEBUG(msg) ((void) 0)
  85. #endif // GGML_VULKAN_DEBUG
  86. struct ggml_backend_vk_context;
  87. #define MAX_PARAMETER_COUNT 8
  88. struct vk_pipeline_struct {
  89. std::string name;
  90. vk::ShaderModule shader_module;
  91. vk::PipelineLayout layout;
  92. vk::Pipeline pipeline;
  93. uint32_t push_constant_size;
  94. uint32_t parameter_count;
  95. std::array<uint32_t, 3> wg_denoms;
  96. uint32_t align;
  97. // set to true to request the pipeline is compiled after the dryrun
  98. bool needed {};
  99. // set to true when the shader has been compiled
  100. bool compiled {};
  101. };
  102. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  103. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  104. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  105. struct vk_matmul_pipeline_struct {
  106. vk_pipeline l, m, s;
  107. vk_pipeline a_l, a_m, a_s;
  108. };
  109. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  110. struct vk_matmul_pipeline2 {
  111. vk_matmul_pipeline2() {
  112. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  113. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  114. }
  115. vk_matmul_pipeline f32acc;
  116. vk_matmul_pipeline f16acc;
  117. };
  118. struct vk_device_struct;
  119. typedef std::shared_ptr<vk_device_struct> vk_device;
  120. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  121. struct vk_buffer_struct;
  122. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  123. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  124. struct ggml_backend_vk_buffer_type_context {
  125. std::string name;
  126. vk_device device;
  127. };
  128. struct vk_queue;
  129. // Stores command pool/buffers. There's an instance of this
  130. // for each (context,queue) pair and for each (device,queue) pair.
  131. struct vk_command_pool {
  132. void init(vk_device& device, vk_queue *q_);
  133. void destroy(vk::Device& device);
  134. vk::CommandPool pool;
  135. uint32_t cmd_buffer_idx;
  136. std::vector<vk::CommandBuffer> cmd_buffers;
  137. vk_queue *q;
  138. };
  139. // Prevent simultaneous submissions to the same queue.
  140. // This could be per vk_queue if we stopped having two vk_queue structures
  141. // sharing the same vk::Queue.
  142. static std::mutex queue_mutex;
  143. struct vk_queue {
  144. uint32_t queue_family_index;
  145. vk::Queue queue;
  146. vk_command_pool cmd_pool;
  147. vk::PipelineStageFlags stage_flags;
  148. bool transfer_only;
  149. // copy everything except the cmd_pool
  150. void copyFrom(vk_queue &other) {
  151. queue_family_index = other.queue_family_index;
  152. queue = other.queue;
  153. stage_flags = other.stage_flags;
  154. transfer_only = other.transfer_only;
  155. }
  156. };
  157. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  158. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  159. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  160. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  161. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  162. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  163. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  164. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  165. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  166. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  167. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  168. /* .is_host = */ NULL,
  169. };
  170. #ifdef GGML_VULKAN_MEMORY_DEBUG
  171. class vk_memory_logger;
  172. #endif
  173. class vk_perf_logger;
  174. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  175. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  176. static constexpr uint32_t p021_max_gqa_ratio = 8;
  177. enum vk_device_architecture {
  178. OTHER,
  179. AMD_GCN,
  180. AMD_RDNA1,
  181. AMD_RDNA2,
  182. AMD_RDNA3,
  183. INTEL_XE2,
  184. };
  185. // HSK x HSV
  186. enum FaHeadSizes {
  187. FA_HEAD_SIZE_64,
  188. FA_HEAD_SIZE_80,
  189. FA_HEAD_SIZE_96,
  190. FA_HEAD_SIZE_112,
  191. FA_HEAD_SIZE_128,
  192. FA_HEAD_SIZE_192,
  193. FA_HEAD_SIZE_192_128,
  194. FA_HEAD_SIZE_256,
  195. FA_HEAD_SIZE_576_512,
  196. FA_HEAD_SIZE_UNSUPPORTED,
  197. FA_HEAD_SIZE_COUNT = FA_HEAD_SIZE_UNSUPPORTED,
  198. };
  199. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  200. vk::PhysicalDeviceProperties props = device.getProperties();
  201. if (props.vendorID == VK_VENDOR_ID_AMD) {
  202. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  203. bool amd_shader_core_properties = false;
  204. bool integer_dot_product = false;
  205. bool subgroup_size_control = false;
  206. for (const auto& properties : ext_props) {
  207. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  208. amd_shader_core_properties = true;
  209. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  210. integer_dot_product = true;
  211. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  212. subgroup_size_control = true;
  213. }
  214. }
  215. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  216. return vk_device_architecture::OTHER;
  217. }
  218. vk::PhysicalDeviceProperties2 props2;
  219. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  220. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  221. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  222. props2.pNext = &shader_core_props_amd;
  223. shader_core_props_amd.pNext = &integer_dot_props;
  224. integer_dot_props.pNext = &subgroup_size_control_props;
  225. device.getProperties2(&props2);
  226. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  227. return vk_device_architecture::AMD_GCN;
  228. }
  229. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  230. // RDNA
  231. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  232. return vk_device_architecture::AMD_RDNA1;
  233. }
  234. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  235. return vk_device_architecture::AMD_RDNA3;
  236. }
  237. return vk_device_architecture::AMD_RDNA2;
  238. }
  239. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  240. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  241. bool subgroup_size_control = false;
  242. for (const auto& properties : ext_props) {
  243. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  244. subgroup_size_control = true;
  245. }
  246. }
  247. if (!subgroup_size_control) {
  248. return vk_device_architecture::OTHER;
  249. }
  250. vk::PhysicalDeviceProperties2 props2;
  251. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  252. props2.pNext = &subgroup_size_control_props;
  253. device.getProperties2(&props2);
  254. if (subgroup_size_control_props.minSubgroupSize == 16) {
  255. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  256. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  257. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  258. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  259. return vk_device_architecture::INTEL_XE2;
  260. }
  261. }
  262. return vk_device_architecture::OTHER;
  263. }
  264. struct vk_device_struct {
  265. std::recursive_mutex mutex;
  266. vk::PhysicalDevice physical_device;
  267. vk::PhysicalDeviceProperties properties;
  268. std::string name;
  269. uint64_t max_memory_allocation_size;
  270. uint64_t suballocation_block_size;
  271. bool fp16;
  272. bool pipeline_robustness;
  273. vk::Device device;
  274. uint32_t vendor_id;
  275. vk::DriverId driver_id;
  276. vk_device_architecture architecture;
  277. vk_queue compute_queue;
  278. vk_queue transfer_queue;
  279. bool single_queue;
  280. uint32_t subgroup_size;
  281. uint32_t shader_core_count;
  282. bool uma;
  283. bool prefer_host_memory;
  284. bool float_controls_rte_fp16;
  285. bool subgroup_add;
  286. bool subgroup_shuffle;
  287. bool integer_dot_product;
  288. bool subgroup_size_control;
  289. uint32_t subgroup_min_size;
  290. uint32_t subgroup_max_size;
  291. bool subgroup_require_full_support;
  292. bool coopmat_support;
  293. bool coopmat_acc_f32_support {};
  294. bool coopmat_acc_f16_support {};
  295. bool coopmat_bf16_support {};
  296. bool coopmat_support_16x16x16_f16acc {};
  297. bool coopmat_support_16x16x16_f32acc {};
  298. bool coopmat1_fa_support {};
  299. uint32_t coopmat_m;
  300. uint32_t coopmat_n;
  301. uint32_t coopmat_k;
  302. bool coopmat_int_support;
  303. uint32_t coopmat_int_m;
  304. uint32_t coopmat_int_n;
  305. uint32_t coopmat_int_k;
  306. bool coopmat2;
  307. size_t idx;
  308. bool mul_mat_l[GGML_TYPE_COUNT];
  309. bool mul_mat_m[GGML_TYPE_COUNT];
  310. bool mul_mat_s[GGML_TYPE_COUNT];
  311. bool mul_mat_id_l[GGML_TYPE_COUNT];
  312. bool mul_mat_id_m[GGML_TYPE_COUNT];
  313. bool mul_mat_id_s[GGML_TYPE_COUNT];
  314. // set to true to indicate that some shaders need to be compiled after the dryrun
  315. bool need_compiles {};
  316. vk::DescriptorSetLayout dsl;
  317. vk_matmul_pipeline pipeline_matmul_f32 {};
  318. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  319. vk_matmul_pipeline pipeline_matmul_bf16 {};
  320. vk_matmul_pipeline2 pipeline_matmul_f16;
  321. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  322. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  323. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  324. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  325. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  326. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  327. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  328. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  329. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  330. vk_pipeline pipeline_matmul_split_k_reduce;
  331. vk_pipeline pipeline_quantize_q8_1;
  332. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  333. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  334. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  335. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  336. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  337. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  338. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  339. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  340. vk_pipeline pipeline_acc_f32;
  341. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  342. vk_pipeline pipeline_add[2][2][2];
  343. vk_pipeline pipeline_add_norepeat[2][2][2];
  344. vk_pipeline pipeline_sub[2][2][2];
  345. vk_pipeline pipeline_sub_norepeat[2][2][2];
  346. vk_pipeline pipeline_mul[2][2][2];
  347. vk_pipeline pipeline_mul_norepeat[2][2][2];
  348. vk_pipeline pipeline_div[2][2][2];
  349. vk_pipeline pipeline_div_norepeat[2][2][2];
  350. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  351. vk_pipeline pipeline_upscale_nearest_f32, pipeline_upscale_bilinear_f32, pipeline_upscale_bilinear_ac_f32;
  352. vk_pipeline pipeline_scale_f32;
  353. vk_pipeline pipeline_sqr_f32;
  354. vk_pipeline pipeline_sin_f32;
  355. vk_pipeline pipeline_cos_f32;
  356. vk_pipeline pipeline_clamp_f32;
  357. vk_pipeline pipeline_pad_f32;
  358. vk_pipeline pipeline_roll_f32;
  359. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  360. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  361. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16;
  362. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  363. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  364. vk_pipeline pipeline_set_rows[GGML_TYPE_COUNT];
  365. vk_pipeline pipeline_norm_f32;
  366. vk_pipeline pipeline_group_norm_f32;
  367. vk_pipeline pipeline_rms_norm_f32;
  368. vk_pipeline pipeline_rms_norm_mul_f32;
  369. vk_pipeline pipeline_rms_norm_back_f32;
  370. vk_pipeline pipeline_l2_norm_f32;
  371. // [src/dst 0=fp32,1=fp16]
  372. vk_pipeline pipeline_gelu[2];
  373. vk_pipeline pipeline_gelu_erf[2];
  374. vk_pipeline pipeline_gelu_quick[2];
  375. vk_pipeline pipeline_silu[2];
  376. vk_pipeline pipeline_relu[2];
  377. vk_pipeline pipeline_tanh[2];
  378. vk_pipeline pipeline_sigmoid[2];
  379. vk_pipeline pipeline_geglu[2];
  380. vk_pipeline pipeline_reglu[2];
  381. vk_pipeline pipeline_swiglu[2];
  382. vk_pipeline pipeline_geglu_erf[2];
  383. vk_pipeline pipeline_geglu_quick[2];
  384. vk_pipeline pipeline_leaky_relu_f32;
  385. vk_pipeline pipeline_silu_back_f32;
  386. vk_pipeline pipeline_diag_mask_inf_f32;
  387. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  388. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  389. vk_pipeline pipeline_soft_max_back_f32;
  390. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  391. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  392. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  393. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  394. vk_pipeline pipeline_argsort_f32;
  395. vk_pipeline pipeline_sum_rows_f32;
  396. vk_pipeline pipeline_argmax_f32;
  397. vk_pipeline pipeline_count_equal_i32;
  398. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  399. vk_pipeline pipeline_timestep_embedding_f32;
  400. vk_pipeline pipeline_conv_transpose_1d_f32;
  401. vk_pipeline pipeline_pool2d_f32;
  402. vk_pipeline pipeline_rwkv_wkv6_f32;
  403. vk_pipeline pipeline_rwkv_wkv7_f32;
  404. vk_pipeline pipeline_opt_step_adamw_f32;
  405. vk_pipeline pipeline_conv2d_dw_whcn_f32;
  406. vk_pipeline pipeline_conv2d_dw_cwhn_f32;
  407. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  408. vk_pipeline pipeline_flash_attn_f32_f16_cm2[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  409. vk_pipeline pipeline_flash_attn_f32_f16_cm1[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  410. vk_pipeline pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  411. vk_pipeline pipeline_flash_attn_split_k_reduce;
  412. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  413. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  414. vk::Fence fence;
  415. vk_buffer sync_staging;
  416. ggml_backend_buffer_type buffer_type;
  417. bool disable_fusion;
  418. #ifdef GGML_VULKAN_MEMORY_DEBUG
  419. std::unique_ptr<vk_memory_logger> memory_logger;
  420. #endif
  421. // for GGML_VK_PERF_LOGGER
  422. std::unique_ptr<vk_perf_logger> perf_logger;
  423. vk::QueryPool query_pool;
  424. int32_t num_queries;
  425. ~vk_device_struct() {
  426. VK_LOG_DEBUG("destroy device " << name);
  427. device.destroyFence(fence);
  428. ggml_vk_destroy_buffer(sync_staging);
  429. compute_queue.cmd_pool.destroy(device);
  430. transfer_queue.cmd_pool.destroy(device);
  431. for (auto& pipeline : pipelines) {
  432. if (pipeline.second.expired()) {
  433. continue;
  434. }
  435. vk_pipeline pl = pipeline.second.lock();
  436. ggml_vk_destroy_pipeline(device, pl);
  437. }
  438. pipelines.clear();
  439. device.destroyDescriptorSetLayout(dsl);
  440. device.destroy();
  441. }
  442. };
  443. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  444. cmd_buffer_idx = 0;
  445. q = q_;
  446. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  447. pool = device->device.createCommandPool(command_pool_create_info);
  448. }
  449. void vk_command_pool::destroy(vk::Device& device) {
  450. device.destroyCommandPool(pool);
  451. pool = nullptr;
  452. cmd_buffers.clear();
  453. }
  454. struct vk_buffer_struct {
  455. vk::Buffer buffer = VK_NULL_HANDLE;
  456. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  457. vk::MemoryPropertyFlags memory_property_flags;
  458. void * ptr;
  459. size_t size = 0;
  460. vk_device device;
  461. ~vk_buffer_struct() {
  462. if (size == 0) {
  463. return;
  464. }
  465. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  466. device->device.freeMemory(device_memory);
  467. device->device.destroyBuffer(buffer);
  468. }
  469. };
  470. struct vk_subbuffer {
  471. vk_buffer buffer;
  472. uint64_t offset;
  473. uint64_t size;
  474. operator vk::DescriptorBufferInfo() const {
  475. return { buffer->buffer, offset, size };
  476. }
  477. };
  478. struct vk_semaphore {
  479. vk::Semaphore s;
  480. uint64_t value;
  481. };
  482. struct vk_submission {
  483. vk::CommandBuffer buffer;
  484. std::vector<vk_semaphore> wait_semaphores;
  485. std::vector<vk_semaphore> signal_semaphores;
  486. };
  487. typedef std::vector<vk_submission> vk_sequence;
  488. struct vk_mat_mat_push_constants {
  489. uint32_t M; uint32_t N; uint32_t K;
  490. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  491. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  492. uint32_t k_split;
  493. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  494. uint32_t padded_N;
  495. };
  496. struct vk_mat_vec_push_constants {
  497. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  498. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  499. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  500. };
  501. struct vk_mat_mat_id_push_constants {
  502. uint32_t M; uint32_t N; uint32_t K;
  503. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  504. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  505. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  506. uint32_t padded_N;
  507. };
  508. struct vk_mat_vec_id_push_constants {
  509. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  510. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  511. uint32_t nei0; uint32_t ne11;
  512. };
  513. struct vk_flash_attn_push_constants {
  514. uint32_t N;
  515. uint32_t KV;
  516. uint32_t ne1;
  517. uint32_t ne2;
  518. uint32_t ne3;
  519. uint32_t neq2;
  520. uint32_t neq3;
  521. uint32_t nek2;
  522. uint32_t nek3;
  523. uint32_t nev2;
  524. uint32_t nev3;
  525. uint32_t nem1;
  526. uint32_t nem2;
  527. uint32_t nem3;
  528. uint32_t nb01;
  529. uint32_t nb02;
  530. uint32_t nb03;
  531. uint32_t nb11;
  532. uint32_t nb12;
  533. uint32_t nb13;
  534. uint32_t nb21;
  535. uint32_t nb22;
  536. uint32_t nb23;
  537. float scale;
  538. float max_bias;
  539. float logit_softcap;
  540. uint32_t mask_n_head_log2;
  541. float m0;
  542. float m1;
  543. uint32_t gqa_ratio;
  544. uint32_t split_kv;
  545. uint32_t k_num;
  546. };
  547. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  548. struct vk_op_push_constants {
  549. uint32_t KX;
  550. uint32_t KY;
  551. float param1;
  552. float param2;
  553. };
  554. struct vk_op_glu_push_constants {
  555. uint32_t N;
  556. uint32_t ne00;
  557. uint32_t ne20;
  558. uint32_t mode; // 0: default, 1: swapped, 2: split
  559. };
  560. struct vk_op_unary_push_constants {
  561. uint32_t ne;
  562. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  563. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  564. uint32_t misalign_offsets;
  565. float param1; float param2;
  566. uint32_t ne0_012mp; uint32_t ne0_012L;
  567. uint32_t ne0_01mp; uint32_t ne0_01L;
  568. uint32_t ne0_0mp; uint32_t ne0_0L;
  569. uint32_t ne1_012mp; uint32_t ne1_012L;
  570. uint32_t ne1_01mp; uint32_t ne1_01L;
  571. uint32_t ne1_0mp; uint32_t ne1_0L;
  572. };
  573. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  574. static vk_op_unary_push_constants vk_op_unary_push_constants_init(const ggml_tensor * src0, const ggml_tensor * dst, int64_t ne = 0) {
  575. GGML_ASSERT(ne != 0 || (ggml_nelements(src0) == ggml_nelements(dst)));
  576. ne = ne != 0 ? ne : ggml_nelements(dst);
  577. GGML_ASSERT(ne <= (int64_t)std::numeric_limits<uint32_t>::max());
  578. vk_op_unary_push_constants p{};
  579. p.ne = (uint32_t)ne;
  580. size_t src0_tsize = ggml_type_size(src0->type);
  581. p.ne00 = (uint32_t)src0->ne[0];
  582. p.ne01 = (uint32_t)src0->ne[1];
  583. p.ne02 = (uint32_t)src0->ne[2];
  584. p.ne03 = (uint32_t)src0->ne[3];
  585. p.nb00 = (uint32_t)(src0->nb[0] / src0_tsize);
  586. p.nb01 = (uint32_t)(src0->nb[1] / src0_tsize);
  587. p.nb02 = (uint32_t)(src0->nb[2] / src0_tsize);
  588. p.nb03 = (uint32_t)(src0->nb[3] / src0_tsize);
  589. size_t dst_tsize = ggml_type_size(dst->type);
  590. p.ne10 = (uint32_t)dst->ne[0];
  591. p.ne11 = (uint32_t)dst->ne[1];
  592. p.ne12 = (uint32_t)dst->ne[2];
  593. p.ne13 = (uint32_t)dst->ne[3];
  594. p.nb10 = (uint32_t)(dst->nb[0] / dst_tsize);
  595. p.nb11 = (uint32_t)(dst->nb[1] / dst_tsize);
  596. p.nb12 = (uint32_t)(dst->nb[2] / dst_tsize);
  597. p.nb13 = (uint32_t)(dst->nb[3] / dst_tsize);
  598. return p; // fastdiv values and offsets are initialized later in ggml_vk_op
  599. }
  600. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  601. // Precompute mp (m' in the paper) and L such that division
  602. // can be computed using a multiply (high 32b of 64b result)
  603. // and a shift:
  604. //
  605. // n/d = (mulhi(n, mp) + n) >> L;
  606. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  607. {
  608. // compute L = ceil(log2(d));
  609. L = 0;
  610. while (L < 32 && (uint32_t{1} << L) < d) {
  611. L++;
  612. }
  613. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  614. }
  615. template <typename T> void init_pushconst_fastdiv(T &p) {
  616. GGML_UNUSED(p);
  617. static_assert(!std::is_const<T>::value, "unexpected type");
  618. }
  619. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  620. // Compute magic values to divide by these six numbers.
  621. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  622. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  623. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  624. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  625. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  626. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  627. }
  628. struct vk_op_binary_push_constants {
  629. uint32_t ne;
  630. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  631. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  632. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  633. uint32_t misalign_offsets;
  634. float param1; float param2; int32_t param3;
  635. };
  636. struct vk_op_diag_mask_push_constants {
  637. uint32_t ncols;
  638. uint32_t rows_per_channel;
  639. int32_t n_past;
  640. };
  641. struct vk_op_rope_push_constants {
  642. uint32_t ncols;
  643. uint32_t n_dims;
  644. float freq_scale;
  645. uint32_t p_delta_rows;
  646. float freq_base;
  647. float ext_factor;
  648. float attn_factor;
  649. float corr_dims[2];
  650. float theta_scale;
  651. uint32_t has_ff;
  652. uint32_t ne02;
  653. uint32_t s1;
  654. uint32_t s2;
  655. int32_t sections[4];
  656. uint32_t is_back;
  657. };
  658. struct vk_op_soft_max_push_constants {
  659. uint32_t KX;
  660. uint32_t KY;
  661. uint32_t ne00;
  662. uint32_t ne01;
  663. uint32_t ne02;
  664. uint32_t ne12;
  665. uint32_t ne13;
  666. uint32_t nb11;
  667. uint32_t nb12;
  668. uint32_t nb13;
  669. float scale;
  670. float max_bias;
  671. float m0;
  672. float m1;
  673. uint32_t n_head_log2;
  674. uint32_t nrows_x;
  675. };
  676. struct vk_op_argsort_push_constants {
  677. uint32_t ncols;
  678. uint32_t ncols_pad;
  679. int32_t order;
  680. };
  681. struct vk_op_im2col_push_constants {
  682. uint32_t batch_offset; uint32_t offset_delta;
  683. uint32_t IC;
  684. uint32_t IW; uint32_t IH;
  685. uint32_t OW; uint32_t OH;
  686. uint32_t KW; uint32_t KH;
  687. uint32_t pelements;
  688. uint32_t CHW;
  689. int32_t s0; int32_t s1;
  690. int32_t p0; int32_t p1;
  691. int32_t d0; int32_t d1;
  692. };
  693. struct vk_op_timestep_embedding_push_constants {
  694. uint32_t nb1;
  695. uint32_t dim;
  696. uint32_t max_period;
  697. };
  698. struct vk_op_conv_transpose_1d_push_constants {
  699. uint32_t Cout;
  700. uint32_t Cin;
  701. uint32_t K;
  702. uint32_t L;
  703. uint32_t KL;
  704. uint32_t nb01;
  705. uint32_t nb02;
  706. uint32_t nb11;
  707. uint32_t nb1;
  708. int32_t s0;
  709. };
  710. struct vk_op_pool2d_push_constants {
  711. uint32_t IW; uint32_t IH;
  712. uint32_t OW; uint32_t OH;
  713. uint32_t OC;
  714. uint32_t pelements;
  715. uint32_t op;
  716. int32_t k0; int32_t k1;
  717. int32_t s0; int32_t s1;
  718. int32_t p0; int32_t p1;
  719. };
  720. struct vk_op_rwkv_wkv6_push_constants {
  721. uint32_t B;
  722. uint32_t T;
  723. uint32_t C;
  724. uint32_t H;
  725. };
  726. struct vk_op_rwkv_wkv7_push_constants {
  727. uint32_t B;
  728. uint32_t T;
  729. uint32_t C;
  730. uint32_t H;
  731. };
  732. struct vk_op_conv2d_dw_push_constants {
  733. uint32_t ne;
  734. uint32_t batches;
  735. uint32_t channels;
  736. uint32_t dst_w;
  737. uint32_t dst_h;
  738. uint32_t src_w;
  739. uint32_t src_h;
  740. uint32_t knl_w;
  741. uint32_t knl_h;
  742. int32_t stride_x;
  743. int32_t stride_y;
  744. int32_t pad_x;
  745. int32_t pad_y;
  746. int32_t dilation_x;
  747. int32_t dilation_y;
  748. };
  749. struct vk_op_upscale_push_constants {
  750. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  751. uint32_t ne00; uint32_t ne01;
  752. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  753. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  754. float sf0; float sf1; float sf2; float sf3;
  755. };
  756. // Allow pre-recording command buffers
  757. struct vk_staging_memcpy {
  758. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  759. void * dst;
  760. const void * src;
  761. size_t n;
  762. };
  763. struct vk_context_struct {
  764. vk_submission * s;
  765. std::vector<vk_sequence> seqs;
  766. int exit_tensor_idx;
  767. std::vector<vk_staging_memcpy> in_memcpys;
  768. std::vector<vk_staging_memcpy> out_memcpys;
  769. vk_command_pool * p {};
  770. };
  771. typedef std::shared_ptr<vk_context_struct> vk_context;
  772. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  773. struct ggml_vk_garbage_collector {
  774. std::vector<vk_semaphore> tl_semaphores;
  775. std::vector<vk_semaphore> semaphores;
  776. std::vector<vk::Event> events;
  777. std::vector<vk_buffer> temp_buffers;
  778. std::vector<vk_context> contexts;
  779. };
  780. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  781. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  782. static std::string format_size(size_t size) {
  783. const size_t kib = 1024;
  784. const size_t mib = kib * 1024;
  785. const size_t gib = mib * 1024;
  786. std::ostringstream oss;
  787. oss << std::fixed << std::setprecision(2);
  788. if (size >= gib) {
  789. oss << static_cast<double>(size) / gib << " GiB";
  790. } else if (size >= mib) {
  791. oss << static_cast<double>(size) / mib << " MiB";
  792. } else if (size >= kib) {
  793. oss << static_cast<double>(size) / kib << " KiB";
  794. } else {
  795. oss << size << " B";
  796. }
  797. return oss.str();
  798. }
  799. static std::mutex log_mutex;
  800. class vk_memory_logger {
  801. public:
  802. vk_memory_logger(): total_device(0), total_host(0) {}
  803. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  804. void log_deallocation(vk_buffer_ref buf_ref);
  805. private:
  806. std::map<vk::Buffer, size_t> allocations; // Track allocations
  807. size_t total_device;
  808. size_t total_host;
  809. };
  810. #else
  811. #define VK_LOG_MEMORY(msg) ((void) 0)
  812. #endif // GGML_VULKAN_MEMORY_DEBUG
  813. class vk_perf_logger {
  814. public:
  815. void print_timings() {
  816. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  817. for (const auto& t : timings) {
  818. uint64_t total = 0;
  819. for (const auto& time : t.second) {
  820. total += time;
  821. }
  822. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " us" << std::endl;
  823. }
  824. timings.clear();
  825. }
  826. void log_timing(const ggml_tensor * node, uint64_t time) {
  827. if (node->op == GGML_OP_UNARY) {
  828. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  829. return;
  830. }
  831. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  832. const uint64_t m = node->src[0]->ne[1];
  833. const uint64_t n = node->src[1]->ne[1];
  834. const uint64_t k = node->src[1]->ne[0];
  835. std::string name = ggml_op_name(node->op);
  836. if (n == 1) {
  837. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  838. } else {
  839. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  840. }
  841. timings[name].push_back(time);
  842. return;
  843. }
  844. timings[ggml_op_name(node->op)].push_back(time);
  845. }
  846. private:
  847. std::map<std::string, std::vector<uint64_t>> timings;
  848. };
  849. struct ggml_backend_vk_context {
  850. std::string name;
  851. vk_device device;
  852. size_t semaphore_idx, event_idx;
  853. ggml_vk_garbage_collector gc;
  854. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  855. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  856. vk::Fence fence, almost_ready_fence;
  857. bool almost_ready_fence_pending {};
  858. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  859. vk_context_ref compute_ctx;
  860. vk_context_ref transfer_ctx;
  861. std::vector<vk_context_ref> tensor_ctxs;
  862. std::vector<vk::DescriptorPool> descriptor_pools;
  863. std::vector<vk::DescriptorSet> descriptor_sets;
  864. uint32_t descriptor_set_idx {};
  865. uint32_t pipeline_descriptor_set_requirements {};
  866. vk_command_pool compute_cmd_pool;
  867. vk_command_pool transfer_cmd_pool;
  868. // number of additional consecutive nodes that are being fused with the
  869. // node currently being processed
  870. int num_additional_fused_ops {};
  871. };
  872. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  873. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  874. if (tensor->view_src) {
  875. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  876. }
  877. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  878. }
  879. struct ggml_backend_vk_buffer_context {
  880. vk_device_ref device;
  881. vk_buffer dev_buffer;
  882. std::string name;
  883. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  884. device(device),
  885. dev_buffer(dev_buffer),
  886. name(name) {
  887. }
  888. ~ggml_backend_vk_buffer_context() {
  889. ggml_vk_destroy_buffer(dev_buffer);
  890. }
  891. };
  892. #ifdef GGML_VULKAN_MEMORY_DEBUG
  893. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  894. std::lock_guard<std::mutex> guard(log_mutex);
  895. vk_buffer buf = buf_ref.lock();
  896. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  897. const std::string type = device ? "device" : "host";
  898. allocations[buf->buffer] = size;
  899. total_device += device ? size : 0;
  900. total_host += device ? 0 : size;
  901. 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));
  902. }
  903. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  904. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  905. return;
  906. }
  907. std::lock_guard<std::mutex> guard(log_mutex);
  908. vk_buffer buf = buf_ref.lock();
  909. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  910. std::string type = device ? "device" : "host";
  911. auto it = allocations.find(buf->buffer);
  912. total_device -= device ? it->second : 0;
  913. total_host -= device ? 0 : it->second;
  914. if (it != allocations.end()) {
  915. 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));
  916. allocations.erase(it);
  917. } else {
  918. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  919. }
  920. }
  921. #endif // GGML_VULKAN_MEMORY_DEBUG
  922. struct vk_instance_t {
  923. vk::Instance instance;
  924. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  925. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  926. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  927. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  928. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  929. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  930. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  931. std::vector<size_t> device_indices;
  932. vk_device devices[GGML_VK_MAX_DEVICES];
  933. };
  934. static bool vk_instance_initialized = false;
  935. static vk_instance_t vk_instance;
  936. static bool vk_perf_logger_enabled = false;
  937. #ifdef GGML_VULKAN_CHECK_RESULTS
  938. static size_t vk_skip_checks;
  939. static size_t vk_output_tensor;
  940. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  941. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  942. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx);
  943. #endif
  944. 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);
  945. static void ggml_backend_vk_free(ggml_backend_t backend);
  946. // Wait for ctx->fence to be signaled.
  947. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  948. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  949. // during this wait.
  950. if (ctx->almost_ready_fence_pending) {
  951. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  952. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  953. ctx->almost_ready_fence_pending = false;
  954. }
  955. // Spin (w/pause) waiting for the graph to finish executing.
  956. vk::Result result;
  957. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  958. if (result != vk::Result::eNotReady) {
  959. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  960. exit(1);
  961. }
  962. for (uint32_t i = 0; i < 100; ++i) {
  963. YIELD();
  964. YIELD();
  965. YIELD();
  966. YIELD();
  967. YIELD();
  968. YIELD();
  969. YIELD();
  970. YIELD();
  971. YIELD();
  972. YIELD();
  973. }
  974. }
  975. ctx->device->device.resetFences({ ctx->fence });
  976. }
  977. // variables to track number of compiles in progress
  978. static uint32_t compile_count = 0;
  979. static std::mutex compile_count_mutex;
  980. static std::condition_variable compile_count_cond;
  981. 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,
  982. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  983. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  984. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  985. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  986. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  987. GGML_ASSERT(parameter_count > 0);
  988. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  989. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  990. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  991. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  992. vk::PushConstantRange pcr(
  993. vk::ShaderStageFlagBits::eCompute,
  994. 0,
  995. pipeline->push_constant_size
  996. );
  997. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  998. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  999. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  1000. for (size_t i = 0; i < specialization_constants.size(); i++) {
  1001. specialization_entries[i].constantID = i;
  1002. specialization_entries[i].offset = i * sizeof(uint32_t);
  1003. specialization_entries[i].size = sizeof(uint32_t);
  1004. }
  1005. vk::SpecializationInfo specialization_info(
  1006. specialization_entries.size(),
  1007. specialization_entries.data(),
  1008. specialization_constants.size() * sizeof(uint32_t),
  1009. specialization_constants.data()
  1010. );
  1011. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  1012. if (device->subgroup_require_full_support && require_full_subgroups) {
  1013. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  1014. }
  1015. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  1016. pipeline_shader_stage_create_flags,
  1017. vk::ShaderStageFlagBits::eCompute,
  1018. pipeline->shader_module,
  1019. entrypoint.c_str(),
  1020. &specialization_info);
  1021. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  1022. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  1023. if (device->subgroup_size_control && required_subgroup_size > 0) {
  1024. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  1025. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  1026. }
  1027. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  1028. vk::PipelineCreateFlags{},
  1029. pipeline_shader_create_info,
  1030. pipeline->layout);
  1031. vk::PipelineRobustnessCreateInfoEXT rci;
  1032. if (device->pipeline_robustness && disable_robustness) {
  1033. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1034. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1035. compute_pipeline_create_info.setPNext(&rci);
  1036. }
  1037. try {
  1038. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1039. } catch (const vk::SystemError& e) {
  1040. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1041. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1042. throw e;
  1043. }
  1044. pipeline->compiled = true;
  1045. if (vk_instance.debug_utils_support) {
  1046. vk::DebugUtilsObjectNameInfoEXT duoni;
  1047. duoni.objectType = vk::ObjectType::ePipeline;
  1048. duoni.pObjectName = pipeline->name.c_str();
  1049. duoni.objectHandle = reinterpret_cast<uint64_t>(static_cast<VkPipeline_T*>(pipeline->pipeline));
  1050. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1051. }
  1052. {
  1053. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1054. device->pipelines.insert({ pipeline->name, pipeline });
  1055. }
  1056. {
  1057. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1058. assert(compile_count > 0);
  1059. compile_count--;
  1060. }
  1061. compile_count_cond.notify_all();
  1062. }
  1063. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1064. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1065. device.destroyPipelineLayout(pipeline->layout);
  1066. device.destroyShaderModule(pipeline->shader_module);
  1067. device.destroyPipeline(pipeline->pipeline);
  1068. }
  1069. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1070. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1071. ctx->pipeline_descriptor_set_requirements += n;
  1072. if (!pipeline->compiled) {
  1073. pipeline->needed = true;
  1074. ctx->device->need_compiles = true;
  1075. }
  1076. }
  1077. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1078. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1079. // Enough descriptors are available
  1080. return;
  1081. }
  1082. vk_device& device = ctx->device;
  1083. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1084. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1085. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1086. while (to_alloc > 0) {
  1087. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1088. to_alloc -= alloc_count;
  1089. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1090. if (pool_idx >= ctx->descriptor_pools.size()) {
  1091. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1092. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1093. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1094. }
  1095. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1096. for (uint32_t i = 0; i < alloc_count; i++) {
  1097. layouts[i] = device->dsl;
  1098. }
  1099. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1100. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1101. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1102. pool_idx++;
  1103. }
  1104. }
  1105. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1106. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1107. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1108. // Reuse command buffer
  1109. return p.cmd_buffers[p.cmd_buffer_idx++];
  1110. }
  1111. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1112. p.pool,
  1113. vk::CommandBufferLevel::ePrimary,
  1114. 1);
  1115. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1116. auto buf = cmd_buffers.front();
  1117. p.cmd_buffers.push_back(buf);
  1118. p.cmd_buffer_idx++;
  1119. return buf;
  1120. }
  1121. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1122. if (ctx->seqs.empty()) {
  1123. if (fence) {
  1124. std::lock_guard<std::mutex> guard(queue_mutex);
  1125. ctx->p->q->queue.submit({}, fence);
  1126. }
  1127. return;
  1128. }
  1129. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1130. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1131. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1132. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1133. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1134. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1135. std::vector<vk::SubmitInfo> submit_infos;
  1136. int idx = -1;
  1137. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1138. size_t reserve = 0;
  1139. for (const auto& sequence : ctx->seqs) {
  1140. reserve += sequence.size();
  1141. }
  1142. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1143. tl_wait_semaphores.reserve(reserve);
  1144. tl_wait_vals.reserve(reserve);
  1145. tl_signal_semaphores.reserve(reserve);
  1146. tl_signal_vals.reserve(reserve);
  1147. tl_submit_infos.reserve(reserve);
  1148. submit_infos.reserve(reserve);
  1149. stage_flags.reserve(reserve);
  1150. for (const auto& sequence : ctx->seqs) {
  1151. for (const auto& submission : sequence) {
  1152. stage_flags.push_back({});
  1153. idx++;
  1154. tl_wait_vals.push_back({});
  1155. tl_wait_semaphores.push_back({});
  1156. tl_signal_vals.push_back({});
  1157. tl_signal_semaphores.push_back({});
  1158. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1159. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1160. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1161. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1162. }
  1163. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1164. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1165. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1166. }
  1167. tl_submit_infos.push_back({
  1168. (uint32_t) submission.wait_semaphores.size(),
  1169. tl_wait_vals[idx].data(),
  1170. (uint32_t) submission.signal_semaphores.size(),
  1171. tl_signal_vals[idx].data(),
  1172. });
  1173. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1174. tl_submit_infos[idx].pNext = nullptr;
  1175. vk::SubmitInfo si{
  1176. (uint32_t) submission.wait_semaphores.size(),
  1177. tl_wait_semaphores[idx].data(),
  1178. stage_flags[idx].data(),
  1179. 1,
  1180. &submission.buffer,
  1181. (uint32_t) submission.signal_semaphores.size(),
  1182. tl_signal_semaphores[idx].data(),
  1183. };
  1184. si.setPNext(&tl_submit_infos[idx]);
  1185. submit_infos.push_back(si);
  1186. }
  1187. }
  1188. std::lock_guard<std::mutex> guard(queue_mutex);
  1189. ctx->p->q->queue.submit(submit_infos, fence);
  1190. ctx->seqs.clear();
  1191. }
  1192. 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) {
  1193. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1194. const uint32_t qfsize = queue_family_props.size();
  1195. // Try with avoid preferences first
  1196. for (uint32_t i = 0; i < qfsize; i++) {
  1197. 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)) {
  1198. return i;
  1199. }
  1200. }
  1201. // Fall back to only required
  1202. for (size_t i = 0; i < qfsize; i++) {
  1203. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1204. return i;
  1205. }
  1206. }
  1207. // Fall back to reusing compute queue
  1208. for (size_t i = 0; i < qfsize; i++) {
  1209. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1210. return i;
  1211. }
  1212. }
  1213. // Fall back to ignoring min_num_queries
  1214. for (size_t i = 0; i < qfsize; i++) {
  1215. if (queue_family_props[i].queueFlags & required) {
  1216. return i;
  1217. }
  1218. }
  1219. // 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.
  1220. // 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.
  1221. if (compute_index >= 0) {
  1222. return compute_index;
  1223. }
  1224. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1225. for(auto &q_family : queue_family_props) {
  1226. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1227. }
  1228. abort();
  1229. }
  1230. 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) {
  1231. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1232. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1233. q.queue_family_index = queue_family_index;
  1234. q.transfer_only = transfer_only;
  1235. q.cmd_pool.init(device, &q);
  1236. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1237. q.stage_flags = stage_flags;
  1238. }
  1239. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1240. vk_context result = std::make_shared<vk_context_struct>();
  1241. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1242. ctx->gc.contexts.emplace_back(result);
  1243. result->p = &p;
  1244. return result;
  1245. }
  1246. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1247. vk_context result = std::make_shared<vk_context_struct>();
  1248. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1249. result->p = &p;
  1250. return result;
  1251. }
  1252. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1253. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1254. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1255. vk::SemaphoreCreateInfo ci{};
  1256. ci.setPNext(&tci);
  1257. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1258. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1259. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1260. }
  1261. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1262. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1263. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1264. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1265. vk::SemaphoreCreateInfo ci{};
  1266. ci.setPNext(&tci);
  1267. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1268. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1269. }
  1270. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1271. }
  1272. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1273. if (ctx->event_idx >= ctx->gc.events.size()) {
  1274. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1275. }
  1276. return ctx->gc.events[ctx->event_idx++];
  1277. }
  1278. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1279. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1280. // Requires command buffers to be done
  1281. device->device.resetCommandPool(p.pool);
  1282. p.cmd_buffer_idx = 0;
  1283. }
  1284. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1285. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1286. // Arbitrary frequency to cleanup/reuse command buffers
  1287. static constexpr uint32_t cleanup_frequency = 10;
  1288. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1289. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1290. }
  1291. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1292. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1293. }
  1294. }
  1295. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1296. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1297. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1298. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1299. (flags & memory_type.propertyFlags) == flags &&
  1300. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1301. return static_cast<int32_t>(i);
  1302. }
  1303. }
  1304. return UINT32_MAX;
  1305. }
  1306. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1307. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1308. if (size > device->max_memory_allocation_size) {
  1309. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1310. }
  1311. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1312. if (size == 0) {
  1313. buf->size = 0;
  1314. return buf;
  1315. }
  1316. vk::BufferCreateInfo buffer_create_info{
  1317. vk::BufferCreateFlags(),
  1318. size,
  1319. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1320. vk::SharingMode::eExclusive,
  1321. 0,
  1322. nullptr,
  1323. };
  1324. buf->buffer = device->device.createBuffer(buffer_create_info);
  1325. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1326. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1327. uint32_t memory_type_index = UINT32_MAX;
  1328. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1329. buf->memory_property_flags = req_flags;
  1330. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1331. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1332. buf->memory_property_flags = fallback_flags;
  1333. }
  1334. if (memory_type_index == UINT32_MAX) {
  1335. device->device.destroyBuffer(buf->buffer);
  1336. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1337. }
  1338. try {
  1339. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1340. } catch (const vk::SystemError& e) {
  1341. if (buf->memory_property_flags != fallback_flags) {
  1342. // Try again with fallback flags
  1343. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1344. buf->memory_property_flags = fallback_flags;
  1345. try {
  1346. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1347. }
  1348. catch (const vk::SystemError& e) {
  1349. device->device.destroyBuffer(buf->buffer);
  1350. throw e;
  1351. }
  1352. } else {
  1353. // Out of Host/Device memory, clean up buffer
  1354. device->device.destroyBuffer(buf->buffer);
  1355. throw e;
  1356. }
  1357. }
  1358. buf->ptr = nullptr;
  1359. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1360. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1361. }
  1362. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1363. buf->device = device;
  1364. buf->size = size;
  1365. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1366. device->memory_logger->log_allocation(buf, size);
  1367. #endif
  1368. return buf;
  1369. }
  1370. 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)) {
  1371. try {
  1372. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1373. } catch (const vk::SystemError& e) {
  1374. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1375. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1376. throw e;
  1377. }
  1378. }
  1379. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1380. vk_buffer buf;
  1381. try {
  1382. if (device->prefer_host_memory) {
  1383. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1384. } else if (device->uma) {
  1385. // Fall back to host memory type
  1386. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1387. } else {
  1388. // use rebar if available, otherwise fallback to device only visible memory
  1389. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1390. }
  1391. } catch (const vk::SystemError& e) {
  1392. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1393. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1394. throw e;
  1395. }
  1396. return buf;
  1397. }
  1398. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1399. if (buf == nullptr) {
  1400. return;
  1401. }
  1402. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1403. if (buf->device != nullptr) {
  1404. buf->device->memory_logger->log_deallocation(buf);
  1405. }
  1406. #endif
  1407. buf.reset();
  1408. }
  1409. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1410. return { buf, 0, VK_WHOLE_SIZE };
  1411. }
  1412. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1413. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1414. const bool transfer_queue = ctx->p->q->transfer_only;
  1415. ctx->s->buffer.pipelineBarrier(
  1416. ctx->p->q->stage_flags,
  1417. ctx->p->q->stage_flags,
  1418. {},
  1419. { {
  1420. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1421. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1422. } },
  1423. {},
  1424. {}
  1425. );
  1426. }
  1427. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1428. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1429. if (events.empty()) {
  1430. return;
  1431. }
  1432. ctx->s->buffer.waitEvents(
  1433. events,
  1434. ctx->p->q->stage_flags,
  1435. ctx->p->q->stage_flags,
  1436. {},
  1437. {},
  1438. {}
  1439. );
  1440. }
  1441. enum FaCodePath {
  1442. FA_SCALAR,
  1443. FA_COOPMAT1,
  1444. FA_COOPMAT2,
  1445. };
  1446. static FaHeadSizes fa_get_head_sizes(uint32_t hsk, uint32_t hsv) {
  1447. if (hsk != 192 && hsk != 576 && hsk != hsv) {
  1448. return FA_HEAD_SIZE_UNSUPPORTED;
  1449. }
  1450. switch (hsk) {
  1451. case 64: return FA_HEAD_SIZE_64;
  1452. case 80: return FA_HEAD_SIZE_80;
  1453. case 96: return FA_HEAD_SIZE_96;
  1454. case 112: return FA_HEAD_SIZE_112;
  1455. case 128: return FA_HEAD_SIZE_128;
  1456. case 192:
  1457. if (hsv == 192) {
  1458. return FA_HEAD_SIZE_192;
  1459. } else if (hsv == 128) {
  1460. return FA_HEAD_SIZE_192_128;
  1461. } else {
  1462. return FA_HEAD_SIZE_UNSUPPORTED;
  1463. }
  1464. case 256: return FA_HEAD_SIZE_256;
  1465. case 576:
  1466. if (hsv == 512) {
  1467. return FA_HEAD_SIZE_576_512;
  1468. } else {
  1469. return FA_HEAD_SIZE_UNSUPPORTED;
  1470. }
  1471. default: return FA_HEAD_SIZE_UNSUPPORTED;
  1472. }
  1473. }
  1474. // number of rows/cols for flash attention shader
  1475. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1476. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1477. static uint32_t get_fa_scalar_num_large_rows(uint32_t hsv) {
  1478. if (hsv >= 512) {
  1479. return 2;
  1480. } else {
  1481. return 8;
  1482. }
  1483. }
  1484. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1485. // 128 threads split into four subgroups, each subgroup does 1/4
  1486. // of the Bc dimension.
  1487. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1488. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1489. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1490. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1491. if (path == FA_COOPMAT2) {
  1492. return flash_attention_num_small_rows;
  1493. } else {
  1494. return scalar_flash_attention_num_small_rows;
  1495. }
  1496. }
  1497. 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) {
  1498. GGML_UNUSED(clamp);
  1499. GGML_UNUSED(hsv);
  1500. if (path == FA_SCALAR) {
  1501. if (small_rows) {
  1502. return {scalar_flash_attention_num_small_rows, 64};
  1503. } else {
  1504. return {get_fa_scalar_num_large_rows(hsv), 32};
  1505. }
  1506. }
  1507. if (path == FA_COOPMAT1) {
  1508. if (small_rows) {
  1509. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1510. } else {
  1511. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1512. }
  1513. }
  1514. // small rows, large cols
  1515. if (small_rows) {
  1516. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1517. }
  1518. // small cols to reduce register count
  1519. if (ggml_is_quantized(type) || hsk >= 256) {
  1520. if (hsk >= 512) {
  1521. return {32, 32};
  1522. } else {
  1523. return {64, 32};
  1524. }
  1525. }
  1526. return {64, 64};
  1527. }
  1528. 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) {
  1529. uint32_t lut_size = 0;
  1530. switch (src0_type) {
  1531. case GGML_TYPE_IQ1_S:
  1532. case GGML_TYPE_IQ1_M:
  1533. lut_size = 2*2048;
  1534. break;
  1535. case GGML_TYPE_IQ2_XXS:
  1536. lut_size = 8*256;
  1537. break;
  1538. case GGML_TYPE_IQ2_XS:
  1539. lut_size = 8*512;
  1540. break;
  1541. case GGML_TYPE_IQ2_S:
  1542. lut_size = 8*1024;
  1543. break;
  1544. case GGML_TYPE_IQ3_XXS:
  1545. lut_size = 4*256;
  1546. break;
  1547. case GGML_TYPE_IQ3_S:
  1548. lut_size = 4*512;
  1549. break;
  1550. case GGML_TYPE_IQ4_NL:
  1551. case GGML_TYPE_IQ4_XS:
  1552. lut_size = 4*16;
  1553. break;
  1554. default:
  1555. break;
  1556. }
  1557. // Needs to be kept up to date on shader changes
  1558. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1559. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1560. const uint32_t warps = warptile[0] / warptile[10];
  1561. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1562. const uint32_t mmid_row_ids = mul_mat_id ? (4096 * sizeof(uint32_t) + 4/*_ne1*/) : 0;
  1563. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1564. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1565. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1566. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1567. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1568. return supported;
  1569. }
  1570. struct GpuPipelineConfig {
  1571. // GPU architecture identifier.
  1572. // Example: vk_device_architecture::AMD_GCN
  1573. vk_device_architecture arch;
  1574. // Mapping of pipeline names to their specific subgroup sizes.
  1575. // Example: {"soft_max_f32", 64}
  1576. std::unordered_map<std::string, uint32_t> pipelines;
  1577. // Default subgroup size for this GPU.
  1578. // Defaults to 0 if not explicitly provided.
  1579. uint32_t default_subgroup_size = 0;
  1580. };
  1581. // Pipeline configuration for RDNA1 GPUs.
  1582. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1583. {"soft_max", 64}, {"im2col", 64},
  1584. {"argmax", 64}, {"mul_mat_vec", 64},
  1585. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1586. };
  1587. // Pipeline configuration for RDNA2 GPUs.
  1588. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1589. {"soft_max", 64}, {"im2col", 64},
  1590. };
  1591. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1592. // Define configurations for different GPUs.
  1593. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1594. {
  1595. vk_device_architecture::AMD_RDNA1,
  1596. {
  1597. rdna1_pipelines,
  1598. },
  1599. RDNA_DEFAULT_SUBGROUP_SIZE
  1600. },
  1601. {
  1602. vk_device_architecture::AMD_RDNA2,
  1603. {
  1604. rdna2_pipelines,
  1605. },
  1606. RDNA_DEFAULT_SUBGROUP_SIZE
  1607. },
  1608. };
  1609. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1610. for (const auto &config : gpu_pipeline_configs) {
  1611. if (config.arch == arch) {
  1612. auto pipIt = config.pipelines.find(pipeline_name);
  1613. if (pipIt != config.pipelines.end()) {
  1614. return pipIt->second;
  1615. }
  1616. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1617. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1618. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1619. for (const auto &entry : sorted_pipelines) {
  1620. if (pipeline_name.find(entry.first) != std::string::npos) {
  1621. return entry.second;
  1622. }
  1623. }
  1624. return config.default_subgroup_size;
  1625. }
  1626. }
  1627. return 0; // If no matching configuration is found
  1628. }
  1629. static void ggml_vk_load_shaders(vk_device& device) {
  1630. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1631. // some shaders have a minimum subgroup size
  1632. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1633. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1634. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1635. // mulmat
  1636. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1637. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1638. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1639. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1640. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1641. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1642. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1643. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1644. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1645. uint32_t l_align, m_align, s_align;
  1646. if (device->coopmat2) {
  1647. // spec constants and tile sizes for non-quant matmul/matmul_id
  1648. l_warptile = { 256, 128, 256, 64, 1 };
  1649. m_warptile = { 256, 128, 128, 64, 0 };
  1650. s_warptile = { 128, 64, 64, 64, 0 };
  1651. l_wg_denoms = {128, 256, 1 };
  1652. m_wg_denoms = {128, 128, 1 };
  1653. s_wg_denoms = { 64, 64, 1 };
  1654. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1655. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1656. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1657. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1658. l_mmq_wg_denoms = { 128, 256, 1 };
  1659. m_mmq_wg_denoms = { 128, 128, 1 };
  1660. s_mmq_wg_denoms = { 32, 64, 1 };
  1661. // spec constants and tile sizes for quant matmul (Qi_K)
  1662. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1663. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1664. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1665. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1666. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1667. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1668. // spec constants and tile sizes for quant matmul_id
  1669. l_warptile_mmqid = { 256, 128, 128, 16, 0 };
  1670. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1671. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1672. l_mmqid_wg_denoms = { 128, 128, 1 };
  1673. m_mmqid_wg_denoms = { 128, 64, 1 };
  1674. s_mmqid_wg_denoms = { 128, 64, 1 };
  1675. l_align = 128;
  1676. m_align = 64;
  1677. s_align = 32;
  1678. } else {
  1679. // Matrix cores require different warp group sizes
  1680. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1681. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1682. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1683. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1684. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1685. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1686. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1687. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1688. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1689. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1690. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1691. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1692. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1693. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1694. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1695. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1696. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1697. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1698. // chip specific tuning
  1699. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1700. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1701. }
  1702. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1703. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1704. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1705. l_align = 128;
  1706. m_align = 64;
  1707. s_align = 32;
  1708. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1709. ggml_type t = (ggml_type)i;
  1710. // Disable medium and large matrix multiplication if not enough shared memory is available
  1711. // Check mmq warptiles as the largest configuration
  1712. // Throw an error if not enough for any matrix multiplication is available
  1713. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1714. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1715. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1716. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1717. device->mul_mat_m[i] = false;
  1718. device->mul_mat_l[i] = false;
  1719. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1720. device->mul_mat_l[i] = false;
  1721. }
  1722. // Disable mul_mat_id if not enough shared memory is available
  1723. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1724. device->mul_mat_id_s[i] = false;
  1725. device->mul_mat_id_m[i] = false;
  1726. device->mul_mat_id_l[i] = false;
  1727. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1728. device->mul_mat_id_m[i] = false;
  1729. device->mul_mat_id_l[i] = false;
  1730. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1731. device->mul_mat_id_l[i] = false;
  1732. }
  1733. }
  1734. }
  1735. if (!device->pipeline_matmul_f32) {
  1736. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1737. }
  1738. if (!device->pipeline_matmul_f32_f16) {
  1739. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1740. }
  1741. if (!device->pipeline_matmul_id_f32) {
  1742. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1743. }
  1744. if (!device->pipeline_matmul_bf16) {
  1745. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1746. }
  1747. if (!device->pipeline_matmul_id_bf16) {
  1748. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1749. }
  1750. std::vector<std::future<void>> compiles;
  1751. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const std::string &entrypoint,
  1752. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1753. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1754. if (!require_full_subgroups && required_subgroup_size == 0) {
  1755. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1756. }
  1757. if (!pipeline) {
  1758. pipeline = std::make_shared<vk_pipeline_struct>();
  1759. pipeline->name = name;
  1760. pipeline->parameter_count = parameter_count;
  1761. pipeline->push_constant_size = push_constant_size;
  1762. pipeline->wg_denoms = wg_denoms;
  1763. pipeline->align = align;
  1764. }
  1765. if (!pipeline->needed || pipeline->compiled) {
  1766. return;
  1767. }
  1768. {
  1769. // wait until fewer than N compiles are in progress
  1770. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1771. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1772. while (compile_count >= N) {
  1773. compile_count_cond.wait(guard);
  1774. }
  1775. compile_count++;
  1776. }
  1777. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1778. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1779. };
  1780. 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> {
  1781. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  1782. };
  1783. 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> {
  1784. // For large number of rows, 128 invocations seems to work best.
  1785. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1786. // can't use 256 for D==80.
  1787. // For scalar, use 128 (arbitrary)
  1788. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  1789. const uint32_t D = (hsk|hsv);
  1790. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  1791. ? scalar_flash_attention_workgroup_size
  1792. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  1793. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  1794. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  1795. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  1796. const uint32_t D_lsb = D ^ (D & (D-1));
  1797. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  1798. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1799. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1800. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  1801. };
  1802. #define CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, HSK, HSV, HEAD_SIZES) \
  1803. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1804. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1805. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,false), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1806. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][0][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,false), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,false), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,false)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1807. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1808. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][0][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f16acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## _f16acc ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1809. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][0], "flash_attn_f32_f16_" #HEAD_SIZES "_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,1,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,1,TYPE,true), 1, true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1810. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16 ## SUFFIX[TYPE][FA_HEAD_SIZE_##HEAD_SIZES][1][1][1], "flash_attn_f32_f16_" #HEAD_SIZES "_aligned_f32acc_smallrows" #NAMELC #SUFFIX, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _len, flash_attn_f32_f16_ ## NAMELC ## SUFFIX ## _data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(FAPATH, HSK,HSV,0,TYPE,true), fa_spec_constants(FAPATH, HSK,HSV,0,TYPE,true), fa_rows_cols(FAPATH,HSK,HSV,0,TYPE,true)[1], true, FAPATH==FA_COOPMAT1, (FAPATH==FA_COOPMAT1 ? 32 : 0)); \
  1811. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  1812. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 64, 64, 64) \
  1813. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 80, 80, 80) \
  1814. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 96, 96, 96) \
  1815. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 112, 112, 112) \
  1816. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 128, 128, 128) \
  1817. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 192, 192) \
  1818. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 128, 192_128) \
  1819. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 256, 256, 256) \
  1820. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 576, 512, 576_512)
  1821. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  1822. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  1823. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  1824. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1825. if (device->coopmat1_fa_support) {
  1826. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  1827. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  1828. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  1829. }
  1830. #endif
  1831. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1832. if (device->coopmat2) {
  1833. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  1834. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  1835. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  1836. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  1837. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  1838. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  1839. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  1840. }
  1841. #endif
  1842. #undef CREATE_FA2
  1843. #undef CREATE_FA
  1844. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1845. if (device->coopmat2) {
  1846. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1847. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1848. 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); \
  1849. 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); \
  1850. 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); \
  1851. 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); \
  1852. 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); \
  1853. 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); \
  1854. // Create 2 variants, {f16,f32} accumulator
  1855. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1856. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1857. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1858. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1859. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1860. if (device->coopmat_bf16_support) {
  1861. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1862. }
  1863. #endif
  1864. 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)
  1865. 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)
  1866. 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)
  1867. 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)
  1868. 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)
  1869. 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)
  1870. 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)
  1871. 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)
  1872. 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)
  1873. 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)
  1874. 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)
  1875. 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)
  1876. 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)
  1877. 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)
  1878. 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)
  1879. 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)
  1880. 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)
  1881. 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)
  1882. 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)
  1883. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1884. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1885. if (device->coopmat_bf16_support) {
  1886. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1887. }
  1888. #endif
  1889. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1890. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1891. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1892. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1893. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1894. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1895. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1896. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1897. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1898. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1899. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1900. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1901. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1902. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1903. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1904. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1905. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1906. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1907. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1908. #undef CREATE_MM
  1909. #undef CREATE_MM2
  1910. } else
  1911. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1912. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1913. if (device->coopmat_support) {
  1914. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1915. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1916. if (device->mul_mat ## ID ## _l[TYPE]) \
  1917. 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); \
  1918. if (device->mul_mat ## ID ## _m[TYPE]) \
  1919. 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); \
  1920. if (device->mul_mat ## ID ## _s[TYPE]) \
  1921. 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); \
  1922. if (device->mul_mat ## ID ## _l[TYPE]) \
  1923. 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); \
  1924. if (device->mul_mat ## ID ## _m[TYPE]) \
  1925. 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); \
  1926. if (device->mul_mat ## ID ## _s[TYPE]) \
  1927. 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); \
  1928. // Create 2 variants, {f16,f32} accumulator
  1929. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1930. if (device->coopmat_acc_f16_support) { \
  1931. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1932. } \
  1933. if (device->coopmat_acc_f32_support) { \
  1934. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1935. } \
  1936. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1937. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1938. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1939. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1940. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1941. if (device->coopmat_bf16_support) {
  1942. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  1943. }
  1944. #endif
  1945. if (device->coopmat_acc_f16_support) {
  1946. 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, );
  1947. 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, );
  1948. 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, );
  1949. 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, );
  1950. 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, );
  1951. 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, );
  1952. 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, );
  1953. 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, );
  1954. 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, );
  1955. 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, );
  1956. 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, );
  1957. 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, );
  1958. 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, );
  1959. 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, );
  1960. 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, );
  1961. 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, );
  1962. 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, );
  1963. 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, );
  1964. 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, );
  1965. } else {
  1966. 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, );
  1967. 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, );
  1968. 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, );
  1969. 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, );
  1970. 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, );
  1971. 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, );
  1972. 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, );
  1973. 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, );
  1974. 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, );
  1975. 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, );
  1976. 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, );
  1977. 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, );
  1978. 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, );
  1979. 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, );
  1980. 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, );
  1981. 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, );
  1982. 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, );
  1983. 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, );
  1984. 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, );
  1985. }
  1986. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1987. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1988. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1989. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1990. if (device->coopmat_bf16_support) {
  1991. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1992. }
  1993. #endif
  1994. if (device->coopmat_acc_f16_support) {
  1995. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1996. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1997. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1998. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1999. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2000. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2001. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2002. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2003. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2004. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2005. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2006. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2007. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2008. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2009. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2010. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2011. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2012. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2013. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2014. } else {
  2015. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2016. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2017. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2018. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2019. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2020. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2021. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2022. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2023. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2024. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2025. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2026. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2027. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2028. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2029. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2030. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2031. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2032. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2033. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2034. }
  2035. #undef CREATE_MM2
  2036. #undef CREATE_MM
  2037. } else
  2038. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2039. if (device->fp16) {
  2040. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2041. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2042. if (device->mul_mat ## ID ## _l[TYPE]) \
  2043. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2044. if (device->mul_mat ## ID ## _m[TYPE]) \
  2045. 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); \
  2046. if (device->mul_mat ## ID ## _s[TYPE]) \
  2047. 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); \
  2048. if (device->mul_mat ## ID ## _l[TYPE]) \
  2049. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  2050. if (device->mul_mat ## ID ## _m[TYPE]) \
  2051. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  2052. if (device->mul_mat ## ID ## _s[TYPE]) \
  2053. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  2054. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2055. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2056. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->l, #NAMELC "_f16acc_l", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  2057. 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); \
  2058. } \
  2059. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2060. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->m, #NAMELC "_f16acc_m", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  2061. 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); \
  2062. } \
  2063. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2064. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME .f16acc->s, #NAMELC "_f16acc_s", NAMELC ## _f16acc_len, NAMELC ## _f16acc_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  2065. 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); \
  2066. } \
  2067. // Create 2 variants, {f16,f32} accumulator
  2068. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2069. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2070. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2071. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2072. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2073. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2074. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2075. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2076. 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, );
  2077. 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, );
  2078. 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, );
  2079. 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, );
  2080. 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, );
  2081. 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, );
  2082. 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, );
  2083. 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, );
  2084. 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, );
  2085. 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, );
  2086. 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, );
  2087. 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, );
  2088. 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, );
  2089. 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, );
  2090. 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, );
  2091. 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, );
  2092. 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, );
  2093. 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, );
  2094. 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, );
  2095. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2096. if (device->integer_dot_product) {
  2097. 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, );
  2098. 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, );
  2099. 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, );
  2100. 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, );
  2101. 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, );
  2102. }
  2103. #endif
  2104. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2105. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2106. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2107. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2108. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2109. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2110. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2111. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2112. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2113. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2114. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2115. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2116. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2117. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2118. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2119. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2120. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2121. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2122. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2123. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2124. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2125. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2126. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2127. #undef CREATE_MM2
  2128. #undef CREATE_MMQ
  2129. #undef CREATE_MM
  2130. } else {
  2131. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2132. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2133. if (device->mul_mat ## ID ## _l[TYPE]) \
  2134. 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); \
  2135. if (device->mul_mat ## ID ## _m[TYPE]) \
  2136. 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); \
  2137. if (device->mul_mat ## ID ## _s[TYPE]) \
  2138. 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); \
  2139. if (device->mul_mat ## ID ## _l[TYPE]) \
  2140. 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); \
  2141. if (device->mul_mat ## ID ## _m[TYPE]) \
  2142. 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); \
  2143. if (device->mul_mat ## ID ## _s[TYPE]) \
  2144. 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); \
  2145. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2146. if (device->mul_mat ## ID ## _l[TYPE]) \
  2147. 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); \
  2148. if (device->mul_mat ## ID ## _m[TYPE]) \
  2149. 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); \
  2150. if (device->mul_mat ## ID ## _s[TYPE]) \
  2151. 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); \
  2152. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2153. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2154. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2155. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2156. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2157. 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, );
  2158. 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, );
  2159. 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, );
  2160. 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, );
  2161. 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, );
  2162. 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, );
  2163. 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, );
  2164. 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, );
  2165. 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, );
  2166. 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, );
  2167. 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, );
  2168. 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, );
  2169. 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, );
  2170. 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, );
  2171. 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, );
  2172. 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, );
  2173. 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, );
  2174. 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, );
  2175. 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, );
  2176. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2177. if (device->integer_dot_product) {
  2178. 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, );
  2179. 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, );
  2180. 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, );
  2181. 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, );
  2182. 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, );
  2183. }
  2184. #endif
  2185. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2186. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2187. 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);
  2188. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2189. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2190. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2191. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2192. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2193. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2194. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2195. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2196. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2197. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2198. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2199. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2200. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2201. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2202. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2203. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2204. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2205. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2206. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2207. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  2208. }
  2209. // reusing CREATE_MM from the fp32 path
  2210. if ((device->coopmat2 || device->coopmat_support)
  2211. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2212. && !device->coopmat_bf16_support
  2213. #endif
  2214. ) {
  2215. // use scalar tile sizes
  2216. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2217. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2218. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2219. l_wg_denoms = {128, 128, 1 };
  2220. m_wg_denoms = { 64, 64, 1 };
  2221. s_wg_denoms = { 32, 32, 1 };
  2222. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2223. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2224. }
  2225. #undef CREATE_MM
  2226. // mul mat vec
  2227. // the number of rows computed per shader depends on GPU model and quant
  2228. uint32_t rm_stdq = 1;
  2229. uint32_t rm_kq = 2;
  2230. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2231. if (device->architecture == AMD_GCN) {
  2232. rm_stdq = 2;
  2233. rm_kq = 4;
  2234. }
  2235. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2236. rm_stdq = 2;
  2237. uint32_t rm_iq = 2 * rm_kq;
  2238. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2239. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2240. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2241. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f32_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f32_f32_len, mul_mat_vec_bf16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2242. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2243. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2244. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2245. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2246. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f32_f32_len, mul_mat_vec_q8_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2247. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2248. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2249. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2250. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2251. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2252. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2253. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2254. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2255. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2256. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2257. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2258. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2259. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2260. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2261. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2262. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2263. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_BF16][i], "mul_mat_vec_bf16_f16_f32_"+std::to_string(i+1), mul_mat_vec_bf16_f16_f32_len, mul_mat_vec_bf16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  2264. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2265. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2266. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2267. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  2268. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f16_f32_len, mul_mat_vec_q8_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  2269. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2270. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2271. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2272. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2273. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  2274. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2275. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2276. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2277. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2278. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2279. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2280. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2281. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2282. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  2283. }
  2284. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F32 ], "mul_mat_vec_id_f32_f32", mul_mat_vec_id_f32_f32_len, mul_mat_vec_id_f32_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2285. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F16 ], "mul_mat_vec_id_f16_f32", mul_mat_vec_id_f16_f32_len, mul_mat_vec_id_f16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2286. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_BF16], "mul_mat_vec_id_bf16_f32", mul_mat_vec_id_bf16_f32_len, mul_mat_vec_id_bf16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  2287. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_0], "mul_mat_vec_id_q4_0_f32", mul_mat_vec_id_q4_0_f32_len, mul_mat_vec_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2288. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_1], "mul_mat_vec_id_q4_1_f32", mul_mat_vec_id_q4_1_f32_len, mul_mat_vec_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2289. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_0], "mul_mat_vec_id_q5_0_f32", mul_mat_vec_id_q5_0_f32_len, mul_mat_vec_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2290. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_1], "mul_mat_vec_id_q5_1_f32", mul_mat_vec_id_q5_1_f32_len, mul_mat_vec_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  2291. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q8_0], "mul_mat_vec_id_q8_0_f32", mul_mat_vec_id_q8_0_f32_len, mul_mat_vec_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq}, 1, true);
  2292. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q2_K], "mul_mat_vec_id_q2_k_f32", mul_mat_vec_id_q2_k_f32_len, mul_mat_vec_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2293. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q3_K], "mul_mat_vec_id_q3_k_f32", mul_mat_vec_id_q3_k_f32_len, mul_mat_vec_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2294. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_K], "mul_mat_vec_id_q4_k_f32", mul_mat_vec_id_q4_k_f32_len, mul_mat_vec_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2295. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_K], "mul_mat_vec_id_q5_k_f32", mul_mat_vec_id_q5_k_f32_len, mul_mat_vec_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2296. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q6_K], "mul_mat_vec_id_q6_k_f32", mul_mat_vec_id_q6_k_f32_len, mul_mat_vec_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  2297. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_S], "mul_mat_vec_id_iq1_s_f32", mul_mat_vec_id_iq1_s_f32_len, mul_mat_vec_id_iq1_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2298. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_M], "mul_mat_vec_id_iq1_m_f32", mul_mat_vec_id_iq1_m_f32_len, mul_mat_vec_id_iq1_m_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2299. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XXS], "mul_mat_vec_id_iq2_xxs_f32", mul_mat_vec_id_iq2_xxs_f32_len, mul_mat_vec_id_iq2_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2300. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XS], "mul_mat_vec_id_iq2_xs_f32", mul_mat_vec_id_iq2_xs_f32_len, mul_mat_vec_id_iq2_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2301. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_S], "mul_mat_vec_id_iq2_s_f32", mul_mat_vec_id_iq2_s_f32_len, mul_mat_vec_id_iq2_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2302. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_XXS], "mul_mat_vec_id_iq3_xxs_f32", mul_mat_vec_id_iq3_xxs_f32_len, mul_mat_vec_id_iq3_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2303. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_S], "mul_mat_vec_id_iq3_s_f32", mul_mat_vec_id_iq3_s_f32_len, mul_mat_vec_id_iq3_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2304. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_XS], "mul_mat_vec_id_iq4_xs_f32", mul_mat_vec_id_iq4_xs_f32_len, mul_mat_vec_id_iq4_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2305. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_NL], "mul_mat_vec_id_iq4_nl_f32", mul_mat_vec_id_iq4_nl_f32_len, mul_mat_vec_id_iq4_nl_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  2306. // dequant shaders
  2307. 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);
  2308. 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);
  2309. 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);
  2310. 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);
  2311. 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);
  2312. 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);
  2313. 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);
  2314. 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);
  2315. 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);
  2316. 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);
  2317. 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);
  2318. 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);
  2319. 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);
  2320. 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);
  2321. 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);
  2322. 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);
  2323. 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);
  2324. 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);
  2325. 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);
  2326. 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);
  2327. // get_rows
  2328. 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);
  2329. 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);
  2330. 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);
  2331. 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);
  2332. 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);
  2333. 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);
  2334. 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);
  2335. 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);
  2336. 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);
  2337. 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);
  2338. 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);
  2339. 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);
  2340. 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);
  2341. 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);
  2342. 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);
  2343. 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);
  2344. 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);
  2345. 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);
  2346. 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);
  2347. 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);
  2348. 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);
  2349. 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);
  2350. 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);
  2351. 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);
  2352. 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);
  2353. 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);
  2354. 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);
  2355. 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);
  2356. 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);
  2357. 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);
  2358. 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);
  2359. 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);
  2360. 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);
  2361. 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);
  2362. 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);
  2363. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_split_k_reduce, "fa_split_k_reduce", fa_split_k_reduce_len, fa_split_k_reduce_data, "main", 2, 4 * sizeof(uint32_t), {1, device->subgroup_size, 1}, {device->subgroup_size}, 1, true);
  2364. 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);
  2365. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2366. if (device->subgroup_add && device->subgroup_require_full_support) {
  2367. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  2368. } else {
  2369. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  2370. }
  2371. }
  2372. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_nc_f16_f32, "mul_mat_vec_nc_f16_f32", mul_mat_vec_nc_f16_f32_len, mul_mat_vec_nc_f16_f32_data, "main", 3, 9 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2373. 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);
  2374. 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);
  2375. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1);
  2376. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1);
  2377. 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);
  2378. 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);
  2379. 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);
  2380. 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);
  2381. 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);
  2382. 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);
  2383. 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);
  2384. 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);
  2385. 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);
  2386. 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);
  2387. 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);
  2388. 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);
  2389. if (device->float_controls_rte_fp16) {
  2390. 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);
  2391. 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);
  2392. 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);
  2393. 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);
  2394. 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);
  2395. 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);
  2396. } else {
  2397. 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);
  2398. 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);
  2399. 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);
  2400. 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);
  2401. 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);
  2402. 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);
  2403. }
  2404. if (device->float_controls_rte_fp16) {
  2405. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_rte_len, set_rows_f32_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2406. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_rte_len, set_rows_f16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2407. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_rte_len, set_rows_bf16_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2408. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_rte_len, set_rows_q4_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2409. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_rte_len, set_rows_q4_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2410. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_rte_len, set_rows_q5_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2411. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_rte_len, set_rows_q5_1_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2412. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_rte_len, set_rows_q8_0_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2413. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_rte_len, set_rows_iq4_nl_rte_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2414. } else {
  2415. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F32], "set_rows_f32", set_rows_f32_len, set_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2416. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_F16], "set_rows_f16", set_rows_f16_len, set_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2417. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_BF16], "set_rows_bf16", set_rows_bf16_len, set_rows_bf16_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2418. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_0], "set_rows_q4_0", set_rows_q4_0_len, set_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2419. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q4_1], "set_rows_q4_1", set_rows_q4_1_len, set_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2420. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_0], "set_rows_q5_0", set_rows_q5_0_len, set_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2421. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q5_1], "set_rows_q5_1", set_rows_q5_1_len, set_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2422. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_Q8_0], "set_rows_q8_0", set_rows_q8_0_len, set_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2423. ggml_vk_create_pipeline(device, device->pipeline_set_rows[GGML_TYPE_IQ4_NL], "set_rows_iq4_nl", set_rows_iq4_nl_len, set_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {1}, 1, true);
  2424. }
  2425. 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);
  2426. 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);
  2427. 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);
  2428. 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);
  2429. 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);
  2430. 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);
  2431. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2432. std::string s;
  2433. s += std::string(src0_f16 ? "_f16" : "_f32");
  2434. s += std::string(src1_f16 ? "_f16" : "_f32");
  2435. s += std::string(dst_f16 ? "_f16" : "_f32");
  2436. return s;
  2437. };
  2438. bool rte = device->float_controls_rte_fp16;
  2439. #define CREATE_BINARY(name, namemod, spec) \
  2440. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2441. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2442. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d][rte], name ## _data[s0][s1][d][rte], \
  2443. "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2444. CREATE_BINARY(add, , {0})
  2445. CREATE_BINARY(add, _norepeat, {1})
  2446. CREATE_BINARY(sub, , {0})
  2447. CREATE_BINARY(sub, _norepeat, {1})
  2448. CREATE_BINARY(mul, , {0})
  2449. CREATE_BINARY(mul, _norepeat, {1})
  2450. CREATE_BINARY(div, , {0})
  2451. CREATE_BINARY(div, _norepeat, {1})
  2452. #undef CREATE_BINARY
  2453. 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);
  2454. 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);
  2455. 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);
  2456. 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);
  2457. 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);
  2458. 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);
  2459. ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_ac_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS}, 1);
  2460. 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);
  2461. 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);
  2462. 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);
  2463. 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);
  2464. 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);
  2465. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2466. 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);
  2467. 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);
  2468. 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);
  2469. #define CREATE_UNARY(name) \
  2470. 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); \
  2471. 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);
  2472. CREATE_UNARY(gelu)
  2473. CREATE_UNARY(gelu_erf)
  2474. CREATE_UNARY(gelu_quick)
  2475. CREATE_UNARY(silu)
  2476. CREATE_UNARY(relu)
  2477. CREATE_UNARY(tanh)
  2478. CREATE_UNARY(sigmoid)
  2479. #undef CREATE_UNARY
  2480. #define CREATE_GLU(name) \
  2481. if (device->float_controls_rte_fp16) { \
  2482. 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); \
  2483. 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); \
  2484. } else { \
  2485. 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); \
  2486. 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); \
  2487. }
  2488. CREATE_GLU(geglu)
  2489. CREATE_GLU(reglu)
  2490. CREATE_GLU(swiglu)
  2491. CREATE_GLU(geglu_erf)
  2492. CREATE_GLU(geglu_quick)
  2493. #undef CREATE_GLU
  2494. 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);
  2495. 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);
  2496. 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);
  2497. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2498. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2499. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2500. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2501. 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);
  2502. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2503. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32, "rope_neox_f32", rope_neox_f32_len, rope_neox_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2504. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f32, "rope_multi_f32", rope_multi_f32_len, rope_multi_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2505. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f32, "rope_vision_f32", rope_vision_f32_len, rope_vision_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2506. if (device->float_controls_rte_fp16) {
  2507. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_rte_len, rope_norm_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2508. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_rte_len, rope_neox_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2509. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_rte_len, rope_multi_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2510. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_rte_len, rope_vision_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2511. } else {
  2512. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2513. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_len, rope_neox_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2514. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_len, rope_multi_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2515. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_len, rope_vision_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2516. }
  2517. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  2518. 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);
  2519. ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2520. 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);
  2521. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2522. if (device->float_controls_rte_fp16) {
  2523. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2524. } else {
  2525. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2526. }
  2527. 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);
  2528. 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);
  2529. 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);
  2530. 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);
  2531. 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);
  2532. 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);
  2533. 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);
  2534. 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);
  2535. for (auto &c : compiles) {
  2536. c.wait();
  2537. }
  2538. device->need_compiles = false;
  2539. }
  2540. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2541. static vk_device ggml_vk_get_device(size_t idx) {
  2542. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2543. if (vk_instance.devices[idx] == nullptr) {
  2544. VK_LOG_DEBUG("Initializing new vk_device");
  2545. vk_device device = std::make_shared<vk_device_struct>();
  2546. vk_instance.devices[idx] = device;
  2547. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2548. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2549. #endif
  2550. if (vk_perf_logger_enabled) {
  2551. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2552. }
  2553. size_t dev_num = vk_instance.device_indices[idx];
  2554. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2555. if (dev_num >= physical_devices.size()) {
  2556. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2557. throw std::runtime_error("Device not found");
  2558. }
  2559. device->physical_device = physical_devices[dev_num];
  2560. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2561. device->architecture = get_device_architecture(device->physical_device);
  2562. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2563. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2564. bool fp16_storage = false;
  2565. bool fp16_compute = false;
  2566. bool maintenance4_support = false;
  2567. bool sm_builtins = false;
  2568. bool amd_shader_core_properties2 = false;
  2569. bool pipeline_robustness = false;
  2570. bool coopmat2_support = false;
  2571. device->coopmat_support = false;
  2572. device->integer_dot_product = false;
  2573. bool bfloat16_support = false;
  2574. for (const auto& properties : ext_props) {
  2575. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2576. maintenance4_support = true;
  2577. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2578. fp16_storage = true;
  2579. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2580. fp16_compute = true;
  2581. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2582. sm_builtins = true;
  2583. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2584. amd_shader_core_properties2 = true;
  2585. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2586. pipeline_robustness = true;
  2587. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2588. device->subgroup_size_control = true;
  2589. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2590. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2591. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2592. device->coopmat_support = true;
  2593. device->coopmat_m = 0;
  2594. device->coopmat_n = 0;
  2595. device->coopmat_k = 0;
  2596. #endif
  2597. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2598. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2599. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2600. coopmat2_support = true;
  2601. #endif
  2602. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2603. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2604. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2605. device->integer_dot_product = true;
  2606. #endif
  2607. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2608. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  2609. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2610. bfloat16_support = true;
  2611. #endif
  2612. }
  2613. }
  2614. vk::PhysicalDeviceProperties2 props2;
  2615. vk::PhysicalDeviceMaintenance3Properties props3;
  2616. vk::PhysicalDeviceMaintenance4Properties props4;
  2617. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2618. vk::PhysicalDeviceDriverProperties driver_props;
  2619. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2620. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2621. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2622. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2623. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2624. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2625. props2.pNext = &props3;
  2626. props3.pNext = &subgroup_props;
  2627. subgroup_props.pNext = &driver_props;
  2628. driver_props.pNext = &vk11_props;
  2629. vk11_props.pNext = &vk12_props;
  2630. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2631. if (maintenance4_support) {
  2632. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2633. last_struct = (VkBaseOutStructure *)&props4;
  2634. }
  2635. if (sm_builtins) {
  2636. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2637. last_struct = (VkBaseOutStructure *)&sm_props;
  2638. }
  2639. if (amd_shader_core_properties2) {
  2640. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2641. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2642. }
  2643. if (device->subgroup_size_control) {
  2644. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2645. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2646. }
  2647. #if defined(VK_NV_cooperative_matrix2)
  2648. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2649. if (coopmat2_support) {
  2650. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2651. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2652. }
  2653. #endif
  2654. if (device->integer_dot_product) {
  2655. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2656. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2657. }
  2658. device->physical_device.getProperties2(&props2);
  2659. device->properties = props2.properties;
  2660. device->vendor_id = device->properties.vendorID;
  2661. device->driver_id = driver_props.driverID;
  2662. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2663. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2664. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2665. } else if (maintenance4_support) {
  2666. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2667. } else {
  2668. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2669. }
  2670. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2671. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2672. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2673. } else {
  2674. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2675. device->suballocation_block_size = 1024*1024*1024;
  2676. }
  2677. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2678. device->subgroup_size = subgroup_props.subgroupSize;
  2679. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2680. if (sm_builtins) {
  2681. device->shader_core_count = sm_props.shaderSMCount;
  2682. } else if (amd_shader_core_properties2) {
  2683. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2684. } else {
  2685. device->shader_core_count = 0;
  2686. }
  2687. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2688. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2689. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2690. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2691. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  2692. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2693. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2694. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2695. device->coopmat_support = false;
  2696. }
  2697. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2698. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2699. // Try to find a non-graphics compute queue and transfer-focused queues
  2700. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2701. 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);
  2702. const float priorities[] = { 1.0f, 1.0f };
  2703. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2704. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2705. if (compute_queue_family_index != transfer_queue_family_index) {
  2706. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2707. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2708. } else if(!device->single_queue) {
  2709. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2710. } else {
  2711. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2712. }
  2713. vk::DeviceCreateInfo device_create_info;
  2714. std::vector<const char *> device_extensions;
  2715. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2716. VkPhysicalDeviceFeatures2 device_features2;
  2717. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2718. device_features2.pNext = nullptr;
  2719. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2720. VkPhysicalDeviceVulkan11Features vk11_features;
  2721. vk11_features.pNext = nullptr;
  2722. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2723. device_features2.pNext = &vk11_features;
  2724. VkPhysicalDeviceVulkan12Features vk12_features;
  2725. vk12_features.pNext = nullptr;
  2726. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2727. vk11_features.pNext = &vk12_features;
  2728. last_struct = (VkBaseOutStructure *)&vk12_features;
  2729. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2730. pl_robustness_features.pNext = nullptr;
  2731. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2732. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2733. if (pipeline_robustness) {
  2734. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2735. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2736. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2737. }
  2738. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2739. subgroup_size_control_features.pNext = nullptr;
  2740. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2741. subgroup_size_control_features.computeFullSubgroups = false;
  2742. subgroup_size_control_features.subgroupSizeControl = false;
  2743. if (device->subgroup_size_control) {
  2744. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2745. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2746. }
  2747. #if defined(VK_KHR_cooperative_matrix)
  2748. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2749. coopmat_features.pNext = nullptr;
  2750. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2751. coopmat_features.cooperativeMatrix = VK_FALSE;
  2752. if (device->coopmat_support) {
  2753. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2754. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2755. }
  2756. #endif
  2757. #if defined(VK_NV_cooperative_matrix2)
  2758. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2759. coopmat2_features.pNext = nullptr;
  2760. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2761. if (coopmat2_support) {
  2762. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2763. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2764. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2765. }
  2766. #endif
  2767. #if defined(VK_KHR_shader_bfloat16)
  2768. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  2769. bfloat16_features.pNext = nullptr;
  2770. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  2771. if (bfloat16_support) {
  2772. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  2773. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  2774. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2775. }
  2776. #endif
  2777. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2778. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2779. if (maintenance4_support) {
  2780. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2781. last_struct = (VkBaseOutStructure *)&maint4_features;
  2782. device_extensions.push_back("VK_KHR_maintenance4");
  2783. }
  2784. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2785. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2786. if (device->integer_dot_product) {
  2787. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2788. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2789. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  2790. }
  2791. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2792. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2793. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2794. if (device->subgroup_size_control) {
  2795. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2796. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2797. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2798. }
  2799. device->subgroup_size_control = device->subgroup_size_control &&
  2800. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2801. subgroup_size_control_features.subgroupSizeControl;
  2802. if (device->subgroup_size_control) {
  2803. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2804. }
  2805. #if defined(VK_KHR_cooperative_matrix)
  2806. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2807. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  2808. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  2809. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  2810. device->subgroup_max_size >= 32;
  2811. #endif
  2812. if (coopmat2_support) {
  2813. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2814. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2815. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2816. coopmat2_features.cooperativeMatrixReductions &&
  2817. coopmat2_features.cooperativeMatrixConversions &&
  2818. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2819. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2820. coopmat2_features.cooperativeMatrixBlockLoads &&
  2821. vk12_features.bufferDeviceAddress) {
  2822. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2823. uint32_t count = 0;
  2824. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2825. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2826. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2827. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2828. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2829. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2830. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2831. flexible_dimensions.resize(count, empty_prop);
  2832. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2833. bool found_fp16_128 = false,
  2834. found_fp16_256 = false,
  2835. found_fp32_128 = false,
  2836. found_fp32_256 = false;
  2837. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2838. // with 32x16x16 and 256 with 32x32x16.
  2839. for (auto &prop : flexible_dimensions) {
  2840. if (prop.saturatingAccumulation == VK_FALSE &&
  2841. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2842. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2843. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2844. if (prop.workgroupInvocations == 128 &&
  2845. prop.MGranularity <= 32 &&
  2846. prop.NGranularity <= 16 &&
  2847. prop.KGranularity <= 16) {
  2848. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2849. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2850. found_fp16_128 = true;
  2851. }
  2852. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2853. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2854. found_fp32_128 = true;
  2855. }
  2856. }
  2857. if (prop.workgroupInvocations == 256 &&
  2858. prop.MGranularity <= 32 &&
  2859. prop.NGranularity <= 32 &&
  2860. prop.KGranularity <= 16) {
  2861. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2862. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2863. found_fp16_256 = true;
  2864. }
  2865. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2866. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2867. found_fp32_256 = true;
  2868. }
  2869. }
  2870. }
  2871. }
  2872. if (found_fp16_128 && found_fp16_256 &&
  2873. found_fp32_128 && found_fp32_256 &&
  2874. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2875. device->coopmat2 = true;
  2876. }
  2877. }
  2878. #endif
  2879. }
  2880. if (!vk11_features.storageBuffer16BitAccess) {
  2881. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2882. throw std::runtime_error("Unsupported device");
  2883. }
  2884. device_extensions.push_back("VK_KHR_16bit_storage");
  2885. #ifdef GGML_VULKAN_VALIDATE
  2886. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2887. #endif
  2888. if (device->fp16) {
  2889. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2890. }
  2891. #if defined(VK_KHR_cooperative_matrix)
  2892. if (device->coopmat_support) {
  2893. // Query supported shapes
  2894. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2895. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2896. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2897. uint32_t cm_props_num;
  2898. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2899. cm_props.resize(cm_props_num);
  2900. for (auto& prop : cm_props) {
  2901. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2902. }
  2903. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2904. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2905. for (auto& prop : cm_props) {
  2906. 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));
  2907. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2908. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2909. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2910. ) {
  2911. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2912. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2913. // coopmat sizes not set yet
  2914. if (device->coopmat_m == 0) {
  2915. device->coopmat_acc_f32_support = true;
  2916. device->coopmat_m = prop.MSize;
  2917. device->coopmat_n = prop.NSize;
  2918. device->coopmat_k = prop.KSize;
  2919. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2920. // Only enable if shape is identical
  2921. device->coopmat_acc_f32_support = true;
  2922. }
  2923. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2924. device->coopmat_support_16x16x16_f32acc = true;
  2925. }
  2926. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2927. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2928. // coopmat sizes not set yet
  2929. if (device->coopmat_m == 0) {
  2930. device->coopmat_acc_f16_support = true;
  2931. device->coopmat_m = prop.MSize;
  2932. device->coopmat_n = prop.NSize;
  2933. device->coopmat_k = prop.KSize;
  2934. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2935. // Only enable if shape is identical
  2936. device->coopmat_acc_f16_support = true;
  2937. }
  2938. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2939. device->coopmat_support_16x16x16_f16acc = true;
  2940. }
  2941. }
  2942. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  2943. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  2944. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  2945. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  2946. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  2947. device->coopmat_int_m == 0
  2948. ) {
  2949. device->coopmat_int_support = true;
  2950. device->coopmat_int_m = prop.MSize;
  2951. device->coopmat_int_n = prop.NSize;
  2952. device->coopmat_int_k = prop.KSize;
  2953. }
  2954. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2955. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2956. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2957. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2958. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2959. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2960. ) {
  2961. // coopmat sizes not set yet
  2962. if (device->coopmat_m == 0) {
  2963. device->coopmat_bf16_support = true;
  2964. device->coopmat_m = prop.MSize;
  2965. device->coopmat_n = prop.NSize;
  2966. device->coopmat_k = prop.KSize;
  2967. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2968. // Only enable if shape is identical
  2969. device->coopmat_bf16_support = true;
  2970. }
  2971. }
  2972. #endif
  2973. }
  2974. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2975. // No suitable matmul mode found
  2976. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2977. device->coopmat_support = false;
  2978. }
  2979. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2980. device->coopmat_bf16_support = false;
  2981. }
  2982. }
  2983. if (device->coopmat_support) {
  2984. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2985. }
  2986. #if defined(VK_KHR_shader_bfloat16)
  2987. if (device->coopmat_bf16_support) {
  2988. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2989. }
  2990. #endif
  2991. #endif
  2992. device->name = GGML_VK_NAME + std::to_string(idx);
  2993. device_create_info = {
  2994. vk::DeviceCreateFlags(),
  2995. device_queue_create_infos,
  2996. {},
  2997. device_extensions
  2998. };
  2999. device_create_info.setPNext(&device_features2);
  3000. device->device = device->physical_device.createDevice(device_create_info);
  3001. // Queues
  3002. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  3003. // Shaders
  3004. // Disable matmul tile sizes early if performance low or not supported
  3005. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  3006. switch (device->vendor_id) {
  3007. #ifndef GGML_VULKAN_RUN_TESTS
  3008. case VK_VENDOR_ID_AMD:
  3009. case VK_VENDOR_ID_INTEL:
  3010. device->mul_mat_l[i] = false;
  3011. device->mul_mat_m[i] = true;
  3012. device->mul_mat_s[i] = true;
  3013. device->mul_mat_id_l[i] = false;
  3014. device->mul_mat_id_m[i] = true;
  3015. device->mul_mat_id_s[i] = true;
  3016. break;
  3017. case VK_VENDOR_ID_APPLE:
  3018. device->mul_mat_l[i] = false;
  3019. device->mul_mat_m[i] = true;
  3020. device->mul_mat_s[i] = false;
  3021. device->mul_mat_id_l[i] = false;
  3022. device->mul_mat_id_m[i] = true;
  3023. device->mul_mat_id_s[i] = false;
  3024. break;
  3025. #endif
  3026. default:
  3027. device->mul_mat_l[i] = true;
  3028. device->mul_mat_m[i] = true;
  3029. device->mul_mat_s[i] = true;
  3030. device->mul_mat_id_l[i] = true;
  3031. device->mul_mat_id_m[i] = true;
  3032. device->mul_mat_id_s[i] = true;
  3033. break;
  3034. }
  3035. }
  3036. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  3037. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  3038. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  3039. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  3040. dsl_binding_flags.push_back({});
  3041. }
  3042. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  3043. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  3044. {},
  3045. dsl_binding);
  3046. descriptor_set_layout_create_info.setPNext(&dslbfci);
  3047. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  3048. ggml_vk_load_shaders(device);
  3049. if (!device->single_queue) {
  3050. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  3051. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  3052. } else {
  3053. // TODO: Use pointer or reference to avoid copy
  3054. device->transfer_queue.copyFrom(device->compute_queue);
  3055. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  3056. }
  3057. device->buffer_type = {
  3058. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  3059. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  3060. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  3061. };
  3062. device->fence = device->device.createFence({});
  3063. device->idx = idx;
  3064. device->disable_fusion = getenv("GGML_VK_DISABLE_FUSION") != nullptr;
  3065. return device;
  3066. }
  3067. return vk_instance.devices[idx];
  3068. }
  3069. static void ggml_vk_print_gpu_info(size_t idx) {
  3070. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3071. size_t dev_num = vk_instance.device_indices[idx];
  3072. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3073. GGML_ASSERT(vk_instance_initialized);
  3074. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3075. if (dev_num >= devices.size()) {
  3076. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3077. throw std::runtime_error("Device not found");
  3078. }
  3079. vk::PhysicalDevice physical_device = devices[dev_num];
  3080. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3081. bool fp16_storage = false;
  3082. bool fp16_compute = false;
  3083. bool coopmat_support = false;
  3084. bool coopmat2_support = false;
  3085. bool integer_dot_product = false;
  3086. for (auto properties : ext_props) {
  3087. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3088. fp16_storage = true;
  3089. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3090. fp16_compute = true;
  3091. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3092. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3093. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3094. coopmat_support = true;
  3095. #endif
  3096. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3097. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3098. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3099. coopmat2_support = true;
  3100. #endif
  3101. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3102. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3103. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3104. integer_dot_product = true;
  3105. #endif
  3106. }
  3107. }
  3108. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3109. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3110. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3111. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3112. vk::PhysicalDeviceProperties2 props2;
  3113. vk::PhysicalDeviceMaintenance3Properties props3;
  3114. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3115. vk::PhysicalDeviceDriverProperties driver_props;
  3116. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3117. props2.pNext = &props3;
  3118. props3.pNext = &subgroup_props;
  3119. subgroup_props.pNext = &driver_props;
  3120. // Pointer to the last chain element
  3121. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3122. if (integer_dot_product) {
  3123. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3124. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3125. }
  3126. physical_device.getProperties2(&props2);
  3127. VkPhysicalDeviceFeatures2 device_features2;
  3128. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3129. device_features2.pNext = nullptr;
  3130. VkPhysicalDeviceVulkan11Features vk11_features;
  3131. vk11_features.pNext = nullptr;
  3132. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3133. device_features2.pNext = &vk11_features;
  3134. VkPhysicalDeviceVulkan12Features vk12_features;
  3135. vk12_features.pNext = nullptr;
  3136. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3137. vk11_features.pNext = &vk12_features;
  3138. // Pointer to the last chain element
  3139. last_struct = (VkBaseOutStructure *)&vk12_features;
  3140. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3141. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3142. coopmat_features.pNext = nullptr;
  3143. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3144. coopmat_features.cooperativeMatrix = VK_FALSE;
  3145. if (coopmat_support) {
  3146. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3147. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3148. }
  3149. #endif
  3150. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3151. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3152. if (integer_dot_product) {
  3153. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3154. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3155. }
  3156. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3157. fp16 = fp16 && vk12_features.shaderFloat16;
  3158. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3159. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3160. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3161. integer_dot_product = integer_dot_product
  3162. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3163. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3164. coopmat_support = coopmat_support
  3165. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3166. && coopmat_features.cooperativeMatrix
  3167. #endif
  3168. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3169. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3170. std::string device_name = props2.properties.deviceName.data();
  3171. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  3172. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  3173. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3174. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3175. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3176. }
  3177. }
  3178. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3179. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3180. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3181. static void ggml_vk_instance_init() {
  3182. if (vk_instance_initialized) {
  3183. return;
  3184. }
  3185. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3186. uint32_t api_version = vk::enumerateInstanceVersion();
  3187. if (api_version < VK_API_VERSION_1_2) {
  3188. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3189. GGML_ABORT("fatal error");
  3190. }
  3191. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3192. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3193. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3194. #ifdef __APPLE__
  3195. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3196. #endif
  3197. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3198. std::vector<const char*> layers;
  3199. if (validation_ext) {
  3200. layers.push_back("VK_LAYER_KHRONOS_validation");
  3201. }
  3202. std::vector<const char*> extensions;
  3203. if (validation_ext) {
  3204. extensions.push_back("VK_EXT_validation_features");
  3205. }
  3206. #ifdef __APPLE__
  3207. if (portability_enumeration_ext) {
  3208. extensions.push_back("VK_KHR_portability_enumeration");
  3209. }
  3210. #endif
  3211. if (debug_utils_ext) {
  3212. extensions.push_back("VK_EXT_debug_utils");
  3213. }
  3214. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3215. #ifdef __APPLE__
  3216. if (portability_enumeration_ext) {
  3217. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3218. }
  3219. #endif
  3220. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3221. vk::ValidationFeaturesEXT validation_features;
  3222. if (validation_ext) {
  3223. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3224. validation_features = {
  3225. features_enable,
  3226. {},
  3227. };
  3228. validation_features.setPNext(nullptr);
  3229. instance_create_info.setPNext(&validation_features);
  3230. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3231. }
  3232. vk_instance.instance = vk::createInstance(instance_create_info);
  3233. vk_instance_initialized = true;
  3234. if (debug_utils_ext) {
  3235. vk_instance.debug_utils_support = true;
  3236. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3237. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3238. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3239. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3240. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3241. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3242. }
  3243. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3244. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3245. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3246. if (devices_env != nullptr) {
  3247. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  3248. std::string devices(devices_env);
  3249. std::replace(devices.begin(), devices.end(), ',', ' ');
  3250. std::stringstream ss(devices);
  3251. size_t tmp;
  3252. while (ss >> tmp) {
  3253. if(tmp >= num_available_devices) {
  3254. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3255. throw std::runtime_error("Invalid Vulkan device index");
  3256. }
  3257. vk_instance.device_indices.push_back(tmp);
  3258. }
  3259. } else {
  3260. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3261. // If no vulkan devices are found, return early
  3262. if (devices.empty()) {
  3263. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3264. return;
  3265. }
  3266. // Default to using all dedicated GPUs
  3267. for (size_t i = 0; i < devices.size(); i++) {
  3268. vk::PhysicalDeviceProperties2 new_props;
  3269. vk::PhysicalDeviceDriverProperties new_driver;
  3270. vk::PhysicalDeviceIDProperties new_id;
  3271. new_props.pNext = &new_driver;
  3272. new_driver.pNext = &new_id;
  3273. devices[i].getProperties2(&new_props);
  3274. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3275. // Check if there are two physical devices corresponding to the same GPU
  3276. auto old_device = std::find_if(
  3277. vk_instance.device_indices.begin(),
  3278. vk_instance.device_indices.end(),
  3279. [&devices, &new_id](const size_t k){
  3280. vk::PhysicalDeviceProperties2 old_props;
  3281. vk::PhysicalDeviceIDProperties old_id;
  3282. old_props.pNext = &old_id;
  3283. devices[k].getProperties2(&old_props);
  3284. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3285. }
  3286. );
  3287. if (old_device == vk_instance.device_indices.end()) {
  3288. vk_instance.device_indices.push_back(i);
  3289. } else {
  3290. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3291. // This can cause error when splitting layers aross the devices, need to keep only 1
  3292. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3293. vk::PhysicalDeviceProperties2 old_props;
  3294. vk::PhysicalDeviceDriverProperties old_driver;
  3295. old_props.pNext = &old_driver;
  3296. devices[*old_device].getProperties2(&old_props);
  3297. std::map<vk::DriverId, int> driver_priorities {};
  3298. int old_priority = std::numeric_limits<int>::max();
  3299. int new_priority = std::numeric_limits<int>::max();
  3300. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3301. // Smaller number -> higher priority
  3302. switch (old_props.properties.vendorID) {
  3303. case VK_VENDOR_ID_AMD:
  3304. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3305. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3306. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3307. break;
  3308. case VK_VENDOR_ID_INTEL:
  3309. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3310. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3311. break;
  3312. case VK_VENDOR_ID_NVIDIA:
  3313. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3314. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3315. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3316. #endif
  3317. break;
  3318. }
  3319. if (driver_priorities.count(old_driver.driverID)) {
  3320. old_priority = driver_priorities[old_driver.driverID];
  3321. }
  3322. if (driver_priorities.count(new_driver.driverID)) {
  3323. new_priority = driver_priorities[new_driver.driverID];
  3324. }
  3325. if (new_priority < old_priority) {
  3326. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3327. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3328. vk_instance.device_indices.push_back(i);
  3329. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3330. }
  3331. else {
  3332. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3333. }
  3334. }
  3335. }
  3336. }
  3337. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3338. // If only CPU devices are available, return without devices.
  3339. if (vk_instance.device_indices.empty()) {
  3340. for (size_t i = 0; i < devices.size(); i++) {
  3341. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3342. vk_instance.device_indices.push_back(i);
  3343. break;
  3344. }
  3345. }
  3346. }
  3347. if (vk_instance.device_indices.empty()) {
  3348. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3349. return;
  3350. }
  3351. }
  3352. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3353. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3354. ggml_vk_print_gpu_info(i);
  3355. }
  3356. }
  3357. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3358. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3359. ggml_vk_instance_init();
  3360. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3361. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3362. ctx->device = ggml_vk_get_device(idx);
  3363. ctx->semaphore_idx = 0;
  3364. ctx->event_idx = 0;
  3365. ctx->prealloc_size_x = 0;
  3366. ctx->prealloc_size_y = 0;
  3367. ctx->prealloc_size_split_k = 0;
  3368. ctx->fence = ctx->device->device.createFence({});
  3369. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3370. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3371. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3372. #ifdef GGML_VULKAN_CHECK_RESULTS
  3373. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3374. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3375. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3376. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3377. #endif
  3378. }
  3379. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3380. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3381. switch (type) {
  3382. case GGML_TYPE_F32:
  3383. case GGML_TYPE_Q4_0:
  3384. case GGML_TYPE_Q4_1:
  3385. case GGML_TYPE_Q5_0:
  3386. case GGML_TYPE_Q5_1:
  3387. case GGML_TYPE_Q8_0:
  3388. case GGML_TYPE_Q2_K:
  3389. case GGML_TYPE_Q3_K:
  3390. case GGML_TYPE_Q4_K:
  3391. case GGML_TYPE_Q5_K:
  3392. case GGML_TYPE_Q6_K:
  3393. case GGML_TYPE_IQ1_S:
  3394. case GGML_TYPE_IQ1_M:
  3395. case GGML_TYPE_IQ2_XXS:
  3396. case GGML_TYPE_IQ2_XS:
  3397. case GGML_TYPE_IQ2_S:
  3398. case GGML_TYPE_IQ3_XXS:
  3399. case GGML_TYPE_IQ3_S:
  3400. case GGML_TYPE_IQ4_XS:
  3401. case GGML_TYPE_IQ4_NL:
  3402. break;
  3403. default:
  3404. return nullptr;
  3405. }
  3406. return ctx->device->pipeline_dequant[type];
  3407. }
  3408. 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) {
  3409. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3410. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3411. return ctx->device->pipeline_matmul_f32;
  3412. }
  3413. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3414. return ctx->device->pipeline_matmul_f32_f16;
  3415. }
  3416. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3417. return ctx->device->pipeline_matmul_bf16;
  3418. }
  3419. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3420. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3421. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3422. }
  3423. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3424. return ctx->device->pipeline_matmul_f16.f16acc;
  3425. }
  3426. } else {
  3427. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3428. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3429. }
  3430. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3431. return ctx->device->pipeline_matmul_f16.f32acc;
  3432. }
  3433. }
  3434. // MMQ
  3435. if (src1_type == GGML_TYPE_Q8_1) {
  3436. vk_matmul_pipeline pipelines = (ctx->device->fp16 && prec == GGML_PREC_DEFAULT) ? ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f32acc;
  3437. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3438. return nullptr;
  3439. }
  3440. return pipelines;
  3441. }
  3442. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3443. return nullptr;
  3444. }
  3445. switch (src0_type) {
  3446. case GGML_TYPE_Q4_0:
  3447. case GGML_TYPE_Q4_1:
  3448. case GGML_TYPE_Q5_0:
  3449. case GGML_TYPE_Q5_1:
  3450. case GGML_TYPE_Q8_0:
  3451. case GGML_TYPE_Q2_K:
  3452. case GGML_TYPE_Q3_K:
  3453. case GGML_TYPE_Q4_K:
  3454. case GGML_TYPE_Q5_K:
  3455. case GGML_TYPE_Q6_K:
  3456. case GGML_TYPE_IQ1_S:
  3457. case GGML_TYPE_IQ1_M:
  3458. case GGML_TYPE_IQ2_XXS:
  3459. case GGML_TYPE_IQ2_XS:
  3460. case GGML_TYPE_IQ2_S:
  3461. case GGML_TYPE_IQ3_XXS:
  3462. case GGML_TYPE_IQ3_S:
  3463. case GGML_TYPE_IQ4_XS:
  3464. case GGML_TYPE_IQ4_NL:
  3465. break;
  3466. default:
  3467. return nullptr;
  3468. }
  3469. if (ctx->device->coopmat2) {
  3470. assert(src1_type == GGML_TYPE_F16);
  3471. 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;
  3472. }
  3473. if (ctx->device->coopmat_support) {
  3474. 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;
  3475. }
  3476. 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;
  3477. }
  3478. 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) {
  3479. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3480. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3481. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3482. switch (a_type) {
  3483. case GGML_TYPE_F32:
  3484. case GGML_TYPE_F16:
  3485. case GGML_TYPE_BF16:
  3486. case GGML_TYPE_Q4_0:
  3487. case GGML_TYPE_Q4_1:
  3488. case GGML_TYPE_Q5_0:
  3489. case GGML_TYPE_Q5_1:
  3490. case GGML_TYPE_Q8_0:
  3491. case GGML_TYPE_Q2_K:
  3492. case GGML_TYPE_Q3_K:
  3493. case GGML_TYPE_Q4_K:
  3494. case GGML_TYPE_Q5_K:
  3495. case GGML_TYPE_Q6_K:
  3496. case GGML_TYPE_IQ1_S:
  3497. case GGML_TYPE_IQ1_M:
  3498. case GGML_TYPE_IQ2_XXS:
  3499. case GGML_TYPE_IQ2_XS:
  3500. case GGML_TYPE_IQ2_S:
  3501. case GGML_TYPE_IQ3_XXS:
  3502. case GGML_TYPE_IQ3_S:
  3503. case GGML_TYPE_IQ4_XS:
  3504. case GGML_TYPE_IQ4_NL:
  3505. break;
  3506. default:
  3507. return nullptr;
  3508. }
  3509. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  3510. }
  3511. 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) {
  3512. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3513. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3514. return ctx->device->pipeline_matmul_id_f32;
  3515. }
  3516. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3517. return ctx->device->pipeline_matmul_id_bf16;
  3518. }
  3519. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3520. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3521. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3522. }
  3523. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3524. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3525. }
  3526. } else {
  3527. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3528. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3529. }
  3530. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3531. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3532. }
  3533. }
  3534. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3535. switch (src0_type) {
  3536. case GGML_TYPE_Q4_0:
  3537. case GGML_TYPE_Q4_1:
  3538. case GGML_TYPE_Q5_0:
  3539. case GGML_TYPE_Q5_1:
  3540. case GGML_TYPE_Q8_0:
  3541. case GGML_TYPE_Q2_K:
  3542. case GGML_TYPE_Q3_K:
  3543. case GGML_TYPE_Q4_K:
  3544. case GGML_TYPE_Q5_K:
  3545. case GGML_TYPE_Q6_K:
  3546. case GGML_TYPE_IQ1_S:
  3547. case GGML_TYPE_IQ1_M:
  3548. case GGML_TYPE_IQ2_XXS:
  3549. case GGML_TYPE_IQ2_XS:
  3550. case GGML_TYPE_IQ2_S:
  3551. case GGML_TYPE_IQ3_XXS:
  3552. case GGML_TYPE_IQ3_S:
  3553. case GGML_TYPE_IQ4_XS:
  3554. case GGML_TYPE_IQ4_NL:
  3555. break;
  3556. default:
  3557. return nullptr;
  3558. }
  3559. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3560. }
  3561. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3562. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3563. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3564. switch (a_type) {
  3565. case GGML_TYPE_F32:
  3566. case GGML_TYPE_F16:
  3567. case GGML_TYPE_BF16:
  3568. case GGML_TYPE_Q4_0:
  3569. case GGML_TYPE_Q4_1:
  3570. case GGML_TYPE_Q5_0:
  3571. case GGML_TYPE_Q5_1:
  3572. case GGML_TYPE_Q8_0:
  3573. case GGML_TYPE_Q2_K:
  3574. case GGML_TYPE_Q3_K:
  3575. case GGML_TYPE_Q4_K:
  3576. case GGML_TYPE_Q5_K:
  3577. case GGML_TYPE_Q6_K:
  3578. case GGML_TYPE_IQ1_S:
  3579. case GGML_TYPE_IQ1_M:
  3580. case GGML_TYPE_IQ2_XXS:
  3581. case GGML_TYPE_IQ2_XS:
  3582. case GGML_TYPE_IQ2_S:
  3583. case GGML_TYPE_IQ3_XXS:
  3584. case GGML_TYPE_IQ3_S:
  3585. case GGML_TYPE_IQ4_XS:
  3586. case GGML_TYPE_IQ4_NL:
  3587. break;
  3588. default:
  3589. return nullptr;
  3590. }
  3591. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3592. }
  3593. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3594. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3595. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3596. int best_i = -1;
  3597. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3598. int worst_i = -1;
  3599. size_t worst_size = 0; //largest unused buffer seen so far
  3600. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3601. vk_buffer &b = ctx->buffer_pool[i];
  3602. if (b != nullptr && b->size >= size && b->size < best_size) {
  3603. best_i = i;
  3604. best_size = b->size;
  3605. }
  3606. if (b != nullptr && b->size > worst_size) {
  3607. worst_i = i;
  3608. worst_size = b->size;
  3609. }
  3610. }
  3611. if(best_i != -1) {
  3612. //found the smallest buffer that fits our needs
  3613. vk_buffer b = ctx->buffer_pool[best_i];
  3614. ctx->buffer_pool[best_i].reset();
  3615. return b;
  3616. }
  3617. if(worst_i != -1) {
  3618. //no buffer that fits our needs, resize largest one to save memory
  3619. vk_buffer& b = ctx->buffer_pool[worst_i];
  3620. ggml_vk_destroy_buffer(b);
  3621. }
  3622. return ggml_vk_create_buffer_device(ctx->device, size);
  3623. }
  3624. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3625. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3626. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3627. vk_buffer& b = ctx->buffer_pool[i];
  3628. if (b == nullptr) {
  3629. b = buffer;
  3630. return;
  3631. }
  3632. }
  3633. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3634. ggml_vk_destroy_buffer(buffer);
  3635. }
  3636. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3637. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3638. // Try to find existing temp buffer with enough capacity
  3639. for (auto& buffer : ctx->gc.temp_buffers) {
  3640. if (buffer->size >= size) {
  3641. return buffer;
  3642. }
  3643. }
  3644. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3645. // Otherwise create new buffer
  3646. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3647. ctx->gc.temp_buffers.push_back(buf);
  3648. return buf;
  3649. }
  3650. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3651. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3652. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3653. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3654. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3655. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3656. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3657. size/1024.0/1024.0);
  3658. device->device.freeMemory(buf->device_memory);
  3659. device->device.destroyBuffer(buf->buffer);
  3660. return nullptr;
  3661. }
  3662. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3663. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3664. return buf->ptr;
  3665. }
  3666. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3667. if (ptr == nullptr) {
  3668. return;
  3669. }
  3670. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3671. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3672. vk_buffer buf;
  3673. size_t index;
  3674. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3675. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3676. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3677. if (ptr >= addr && ptr < endr) {
  3678. buf = std::get<2>(device->pinned_memory[i]);
  3679. index = i;
  3680. break;
  3681. }
  3682. }
  3683. if (buf == nullptr) {
  3684. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3685. return;
  3686. }
  3687. ggml_vk_destroy_buffer(buf);
  3688. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3689. }
  3690. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3691. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3692. buf = nullptr;
  3693. buf_offset = 0;
  3694. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3695. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3696. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3697. if (ptr >= addr && ptr < endr) {
  3698. buf = std::get<2>(device->pinned_memory[i]);
  3699. buf_offset = ((const uint8_t *)ptr) - addr;
  3700. break;
  3701. }
  3702. }
  3703. }
  3704. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  3705. vk_submission s;
  3706. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  3707. if (one_time) {
  3708. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3709. } else {
  3710. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3711. }
  3712. return s;
  3713. }
  3714. template <typename T> size_t push_constant_size(const T &t) {
  3715. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3716. GGML_UNUSED(t);
  3717. return sizeof(T);
  3718. }
  3719. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  3720. GGML_UNUSED(t);
  3721. return sizeof(T) * t.size();
  3722. }
  3723. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  3724. GGML_UNUSED(t);
  3725. return sizeof(T) * N;
  3726. }
  3727. template <typename T> const T *push_constant_data(const T &t) {
  3728. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3729. return &t;
  3730. }
  3731. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  3732. return t.data();
  3733. }
  3734. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  3735. return t.data();
  3736. }
  3737. template <typename T>
  3738. 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) {
  3739. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3740. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3741. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3742. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3743. for (auto& buffer : descriptor_buffer_infos) {
  3744. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3745. }
  3746. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3747. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  3748. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  3749. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  3750. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3751. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3752. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  3753. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3754. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3755. pipeline->layout,
  3756. 0,
  3757. { descriptor_set },
  3758. {});
  3759. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3760. }
  3761. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3762. s.buffer.end();
  3763. s.wait_semaphores = std::move(wait_semaphores);
  3764. s.signal_semaphores = std::move(signal_semaphores);
  3765. }
  3766. static void ggml_vk_ctx_end(vk_context& ctx) {
  3767. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3768. if (ctx->s == nullptr) {
  3769. return;
  3770. }
  3771. ctx->s->buffer.end();
  3772. ctx->s = nullptr;
  3773. }
  3774. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3775. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3776. if (subctx->s != nullptr) {
  3777. ggml_vk_ctx_end(subctx);
  3778. }
  3779. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  3780. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3781. }
  3782. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3783. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3784. return CEIL_DIV(width, align) * align;
  3785. }
  3786. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3787. if (memcpys == nullptr) {
  3788. memcpy(dst, src, size);
  3789. } else {
  3790. memcpys->emplace_back(dst, src, size);
  3791. }
  3792. }
  3793. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3794. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3795. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3796. ggml_vk_destroy_buffer(device->sync_staging);
  3797. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3798. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3799. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3800. }
  3801. }
  3802. 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) {
  3803. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3804. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3805. // Buffer is already mapped
  3806. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3807. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3808. GGML_ABORT("fatal error");
  3809. }
  3810. // Check if src is pinned memory
  3811. vk_buffer buf = nullptr;
  3812. size_t buf_offset = 0;
  3813. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3814. const uint64_t ne0 = tensor->ne[0];
  3815. const uint64_t ne1 = tensor->ne[1];
  3816. const uint64_t ne2 = tensor->ne[2];
  3817. const uint64_t ne3 = tensor->ne[3];
  3818. const uint64_t nb0 = tensor->nb[0];
  3819. const uint64_t nb1 = tensor->nb[1];
  3820. const uint64_t nb2 = tensor->nb[2];
  3821. const uint64_t nb3 = tensor->nb[3];
  3822. const ggml_type type = tensor->type;
  3823. const uint64_t ts = ggml_type_size(type);
  3824. const uint64_t bs = ggml_blck_size(type);
  3825. const uint64_t dstnb0 = ts;
  3826. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3827. const uint64_t dstnb2 = dstnb1*ne1;
  3828. const uint64_t dstnb3 = dstnb2*ne2;
  3829. const uint64_t ne = ggml_nelements(tensor);
  3830. if (buf != nullptr) {
  3831. // Memory is pinned, use as staging buffer
  3832. std::vector<vk::BufferCopy> slices;
  3833. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3834. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3835. // Find longest contiguous slice
  3836. if (ne1*nb1 == dstnb2) {
  3837. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3838. } else {
  3839. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3840. if (ne0*nb0/bs == dstnb1) {
  3841. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3842. } else {
  3843. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3844. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3845. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3846. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3847. }
  3848. }
  3849. }
  3850. }
  3851. }
  3852. }
  3853. ggml_vk_sync_buffers(subctx);
  3854. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3855. return;
  3856. }
  3857. if (!sync_staging) {
  3858. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3859. }
  3860. // Staging buffer required
  3861. vk_buffer& staging = ctx->device->sync_staging;
  3862. const uint64_t copy_size = ts*ne/bs;
  3863. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3864. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3865. ggml_vk_sync_buffers(subctx);
  3866. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3867. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3868. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3869. // Find longest contiguous slice
  3870. if (ne1*nb1 == dstnb2) {
  3871. 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);
  3872. } else {
  3873. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3874. if (ne0*nb0/bs == dstnb1) {
  3875. 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);
  3876. } else {
  3877. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3878. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3879. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3880. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3881. }
  3882. }
  3883. }
  3884. }
  3885. }
  3886. }
  3887. }
  3888. 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) {
  3889. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3890. // Buffer is already mapped
  3891. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3892. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3893. GGML_ABORT("fatal error");
  3894. }
  3895. // Check if src is pinned memory
  3896. vk_buffer buf = nullptr;
  3897. size_t buf_offset = 0;
  3898. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3899. if (buf != nullptr) {
  3900. // Memory is pinned, use as staging buffer
  3901. std::vector<vk::BufferCopy> slices(1);
  3902. if (width == spitch) {
  3903. // Only do single write if stride is equal
  3904. slices[0].srcOffset = buf_offset;
  3905. slices[0].dstOffset = offset;
  3906. slices[0].size = width * height;
  3907. } else {
  3908. slices.resize(height);
  3909. for (size_t i = 0; i < height; i++) {
  3910. slices[i].srcOffset = buf_offset + i * spitch;
  3911. slices[i].dstOffset = offset + i * width;
  3912. slices[i].size = width;
  3913. }
  3914. }
  3915. ggml_vk_sync_buffers(subctx);
  3916. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3917. return;
  3918. }
  3919. VK_LOG_DEBUG("STAGING");
  3920. if (!sync_staging) {
  3921. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3922. }
  3923. // Staging buffer required
  3924. const size_t copy_size = width*height;
  3925. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3926. vk_buffer& staging_buffer = dst->device->sync_staging;
  3927. VkBufferCopy buf_copy = {
  3928. 0,
  3929. offset,
  3930. copy_size};
  3931. ggml_vk_sync_buffers(subctx);
  3932. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3933. if (width == spitch) {
  3934. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3935. } else {
  3936. for (size_t i = 0; i < height; i++) {
  3937. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3938. }
  3939. }
  3940. }
  3941. 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) {
  3942. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3943. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3944. }
  3945. 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) {
  3946. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3947. // Buffer is already mapped
  3948. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3949. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3950. for (size_t i = 0; i < height; i++) {
  3951. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3952. }
  3953. } else {
  3954. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  3955. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  3956. ggml_vk_ctx_begin(dst->device, subctx);
  3957. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3958. ggml_vk_ctx_end(subctx);
  3959. for (auto& cpy : subctx->in_memcpys) {
  3960. memcpy(cpy.dst, cpy.src, cpy.n);
  3961. }
  3962. ggml_vk_submit(subctx, dst->device->fence);
  3963. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3964. dst->device->device.resetFences({ dst->device->fence });
  3965. ggml_vk_queue_command_pools_cleanup(dst->device);
  3966. }
  3967. }
  3968. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3969. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3970. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3971. }
  3972. 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) {
  3973. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3974. GGML_ASSERT(width > 0);
  3975. GGML_ASSERT(height > 0);
  3976. GGML_ASSERT(src != nullptr);
  3977. // TODO: staging_offset is not used
  3978. // Check if dst is pinned memory
  3979. vk_buffer buf = nullptr;
  3980. size_t buf_offset = 0;
  3981. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3982. std::vector<vk::BufferCopy> slices(1);
  3983. if (width == spitch && width == dpitch) {
  3984. // Only do single write if stride is equal
  3985. slices[0].srcOffset = offset;
  3986. slices[0].dstOffset = buf_offset;
  3987. slices[0].size = width * height;
  3988. } else {
  3989. slices.resize(height);
  3990. for (size_t i = 0; i < height; i++) {
  3991. slices[i].srcOffset = offset + i * spitch;
  3992. slices[i].dstOffset = buf_offset + i * dpitch;
  3993. slices[i].size = width;
  3994. }
  3995. }
  3996. if (buf != nullptr) {
  3997. // Memory is pinned, use as staging buffer
  3998. ggml_vk_sync_buffers(subctx);
  3999. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  4000. return;
  4001. }
  4002. VK_LOG_DEBUG("STAGING");
  4003. if (!sync_staging) {
  4004. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  4005. }
  4006. // Fall back to staging buffer
  4007. const size_t copy_size = dpitch * height;
  4008. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  4009. vk_buffer& staging_buffer = src->device->sync_staging;
  4010. ggml_vk_sync_buffers(subctx);
  4011. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  4012. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  4013. }
  4014. 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) {
  4015. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  4016. }
  4017. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  4018. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  4019. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  4020. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  4021. // the HW device to host copy path.
  4022. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  4023. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  4024. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  4025. } else {
  4026. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4027. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4028. ggml_vk_ctx_begin(src->device, subctx);
  4029. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  4030. ggml_vk_ctx_end(subctx);
  4031. ggml_vk_submit(subctx, src->device->fence);
  4032. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  4033. src->device->device.resetFences({ src->device->fence });
  4034. ggml_vk_queue_command_pools_cleanup(src->device);
  4035. for (auto& cpy : subctx->out_memcpys) {
  4036. memcpy(cpy.dst, cpy.src, cpy.n);
  4037. }
  4038. }
  4039. }
  4040. 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) {
  4041. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  4042. // Make sure both buffers are on same device
  4043. GGML_ASSERT(src->device == dst->device);
  4044. VkBufferCopy bc{ src_offset, dst_offset, size };
  4045. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  4046. }
  4047. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  4048. if (src->device == dst->device) {
  4049. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  4050. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  4051. // Copy within the device
  4052. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  4053. ggml_vk_ctx_begin(src->device, subctx);
  4054. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  4055. ggml_vk_ctx_end(subctx);
  4056. ggml_vk_submit(subctx, src->device->fence);
  4057. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  4058. src->device->device.resetFences({ src->device->fence });
  4059. ggml_vk_queue_command_pools_cleanup(src->device);
  4060. } else {
  4061. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  4062. // Copy device to device
  4063. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  4064. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  4065. // Copy to src staging buffer
  4066. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  4067. // memcpy to dst staging buffer
  4068. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  4069. // Copy to dst buffer
  4070. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4071. }
  4072. }
  4073. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4074. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4075. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4076. }
  4077. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4078. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4079. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4080. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4081. ggml_vk_ctx_begin(dst->device, subctx);
  4082. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4083. ggml_vk_ctx_end(subctx);
  4084. ggml_vk_submit(subctx, dst->device->fence);
  4085. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4086. dst->device->device.resetFences({ dst->device->fence });
  4087. ggml_vk_queue_command_pools_cleanup(dst->device);
  4088. }
  4089. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  4090. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4091. uint32_t split_k = 1;
  4092. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  4093. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4094. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4095. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4096. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  4097. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4098. // Clamp to 2 or 4
  4099. split_k = std::min(split_k, 4u);
  4100. if (split_k == 3) {
  4101. split_k = 2;
  4102. }
  4103. if (ctx->device->coopmat2) {
  4104. // coopmat2 shader expects splits to be aligned to 256
  4105. while (split_k > 1 && ((k / split_k) % 256) != 0) {
  4106. split_k /= 2;
  4107. }
  4108. }
  4109. }
  4110. }
  4111. return split_k;
  4112. }
  4113. 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) {
  4114. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4115. if (ctx->device->coopmat2) {
  4116. // Use large shader when the N dimension is greater than the medium shader's tile size
  4117. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4118. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  4119. return aligned ? mmp->a_l : mmp->l;
  4120. }
  4121. // Use medium shader when the N dimension is greater than the small shader's tile size
  4122. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4123. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4124. return aligned ? mmp->a_m : mmp->m;
  4125. }
  4126. return aligned ? mmp->a_s : mmp->s;
  4127. }
  4128. 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])) {
  4129. return aligned ? mmp->a_s : mmp->s;
  4130. }
  4131. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4132. return aligned ? mmp->a_m : mmp->m;
  4133. }
  4134. return aligned ? mmp->a_l : mmp->l;
  4135. GGML_UNUSED(src1_type);
  4136. }
  4137. 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) {
  4138. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4139. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4140. }
  4141. static void ggml_vk_matmul(
  4142. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4143. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4144. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4145. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4146. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4147. uint32_t padded_n) {
  4148. 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 << ")");
  4149. ggml_vk_sync_buffers(subctx);
  4150. if (split_k == 1) {
  4151. 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 };
  4152. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4153. return;
  4154. }
  4155. GGML_ASSERT(batch_stride_d == m * n);
  4156. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, padded_n };
  4157. // Make sure enough workgroups get assigned for split k to work
  4158. 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 });
  4159. ggml_vk_sync_buffers(subctx);
  4160. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4161. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4162. }
  4163. 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) {
  4164. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4165. if (ctx->device->coopmat2) {
  4166. // Use large shader when the N dimension is greater than the medium shader's tile size
  4167. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4168. 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])) {
  4169. return aligned ? mmp->a_l : mmp->l;
  4170. }
  4171. // Use medium shader when the N dimension is greater than the small shader's tile size
  4172. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4173. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4174. return aligned ? mmp->a_m : mmp->m;
  4175. }
  4176. return aligned ? mmp->a_s : mmp->s;
  4177. }
  4178. 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])) {
  4179. return aligned ? mmp->a_s : mmp->s;
  4180. }
  4181. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4182. return aligned ? mmp->a_m : mmp->m;
  4183. }
  4184. return aligned ? mmp->a_l : mmp->l;
  4185. }
  4186. 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) {
  4187. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4188. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4189. }
  4190. static void ggml_vk_matmul_id(
  4191. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4192. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4193. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4194. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4195. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4196. uint32_t padded_n) {
  4197. 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 << "), " <<
  4198. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4199. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4200. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4201. ggml_vk_sync_buffers(subctx);
  4202. 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,
  4203. nei0, nei1, nbi1, ne11, padded_n };
  4204. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4205. }
  4206. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4207. return
  4208. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4209. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4210. (tensor->ne[3] == 1 || tensor->nb[3] == tensor->nb[2]*tensor->ne[2]);
  4211. }
  4212. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4213. // Choose "contiguous copy" shader if src/dst are contiguous
  4214. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4215. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4216. if (contig) {
  4217. return ctx->device->pipeline_contig_cpy_f32_f32;
  4218. } else {
  4219. return ctx->device->pipeline_cpy_f32_f32;
  4220. }
  4221. }
  4222. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4223. if (contig) {
  4224. return ctx->device->pipeline_contig_cpy_f32_f16;
  4225. } else {
  4226. return ctx->device->pipeline_cpy_f32_f16;
  4227. }
  4228. }
  4229. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4230. if (contig) {
  4231. return ctx->device->pipeline_contig_cpy_f16_f16;
  4232. } else {
  4233. return ctx->device->pipeline_cpy_f16_f16;
  4234. }
  4235. }
  4236. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4237. if (contig) {
  4238. return ctx->device->pipeline_contig_cpy_f16_f32;
  4239. } else {
  4240. return ctx->device->pipeline_cpy_f16_f32;
  4241. }
  4242. }
  4243. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4244. if (contig) {
  4245. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4246. } else {
  4247. return ctx->device->pipeline_cpy_f32_bf16;
  4248. }
  4249. }
  4250. if (src->type == GGML_TYPE_F32) {
  4251. switch (to) {
  4252. case GGML_TYPE_Q4_0:
  4253. case GGML_TYPE_Q4_1:
  4254. case GGML_TYPE_Q5_0:
  4255. case GGML_TYPE_Q5_1:
  4256. case GGML_TYPE_Q8_0:
  4257. case GGML_TYPE_IQ4_NL:
  4258. return ctx->device->pipeline_cpy_f32_quant[to];
  4259. default:
  4260. break;
  4261. }
  4262. }
  4263. if (to == GGML_TYPE_F32) {
  4264. switch (src->type) {
  4265. case GGML_TYPE_Q4_0:
  4266. case GGML_TYPE_Q4_1:
  4267. case GGML_TYPE_Q5_0:
  4268. case GGML_TYPE_Q5_1:
  4269. case GGML_TYPE_Q8_0:
  4270. case GGML_TYPE_IQ4_NL:
  4271. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4272. default:
  4273. break;
  4274. }
  4275. }
  4276. if (src->type == to) {
  4277. // Copy two or four bytes at a time, depending on block size.
  4278. // For quantized types, we scale by block size/type size. But
  4279. // this path is also used for bf16->bf16 for example, where the
  4280. // type size must be exactly 2 or 4.
  4281. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4282. if ((ggml_type_size(src->type) % 4) == 0) {
  4283. if (contig) {
  4284. return ctx->device->pipeline_contig_cpy_f32_f32;
  4285. } else {
  4286. return ctx->device->pipeline_cpy_f32_f32;
  4287. }
  4288. } else {
  4289. if (contig) {
  4290. return ctx->device->pipeline_contig_cpy_f16_f16;
  4291. } else {
  4292. return ctx->device->pipeline_cpy_f16_f16;
  4293. }
  4294. }
  4295. }
  4296. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4297. GGML_ABORT("fatal error");
  4298. }
  4299. 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) {
  4300. 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] << "), ";
  4301. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4302. const int tensor_type_size = ggml_type_size(tensor->type);
  4303. const uint32_t ne = ggml_nelements(tensor);
  4304. std::array<uint32_t, 3> elements;
  4305. if (ne > 262144) {
  4306. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4307. } else if (ne > 512) {
  4308. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4309. } else {
  4310. elements = { ne, 1, 1 };
  4311. }
  4312. vk_op_unary_push_constants pc = {
  4313. (uint32_t)ne,
  4314. (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,
  4315. (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]),
  4316. 0,
  4317. 0.0f, 0.0f,
  4318. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4319. };
  4320. init_pushconst_fastdiv(pc);
  4321. ggml_vk_sync_buffers(subctx);
  4322. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4323. }
  4324. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  4325. switch(type) {
  4326. case GGML_TYPE_Q8_1:
  4327. return ctx->device->pipeline_quantize_q8_1;
  4328. default:
  4329. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4330. GGML_ABORT("fatal error");
  4331. }
  4332. }
  4333. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  4334. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4335. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4336. ggml_vk_sync_buffers(subctx);
  4337. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4338. }
  4339. static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4340. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4341. 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];
  4342. 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];
  4343. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4344. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4345. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4346. const uint64_t ne00 = src0->ne[0];
  4347. const uint64_t ne01 = src0->ne[1];
  4348. const uint64_t ne02 = src0->ne[2];
  4349. const uint64_t ne03 = src0->ne[3];
  4350. const uint64_t ne10 = src1->ne[0];
  4351. const uint64_t ne11 = src1->ne[1];
  4352. const uint64_t ne12 = src1->ne[2];
  4353. const uint64_t ne13 = src1->ne[3];
  4354. const uint64_t ne20 = dst->ne[0];
  4355. const uint64_t ne21 = dst->ne[1];
  4356. const uint64_t r2 = ne12 / ne02;
  4357. const uint64_t r3 = ne13 / ne03;
  4358. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4359. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4360. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4361. vk_buffer d_Qx = nullptr;
  4362. size_t qx_buf_offset = 0;
  4363. vk_buffer d_Qy = nullptr;
  4364. size_t qy_buf_offset = 0;
  4365. bool src0_uma = false;
  4366. bool src1_uma = false;
  4367. if (ctx->device->uma) {
  4368. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4369. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4370. src0_uma = d_Qx != nullptr;
  4371. src1_uma = d_Qy != nullptr;
  4372. }
  4373. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4374. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4375. !ggml_vk_dim01_contiguous(src0);
  4376. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4377. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4378. !ggml_vk_dim01_contiguous(src1);
  4379. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4380. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4381. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4382. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4383. // Check for mmq first
  4384. 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;
  4385. if (mmp == nullptr) {
  4386. // Fall back to f16 dequant mul mat
  4387. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4388. quantize_y = false;
  4389. }
  4390. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4391. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4392. if (qx_needs_dequant) {
  4393. // Fall back to dequant + f16 mulmat
  4394. 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]);
  4395. }
  4396. // Not implemented
  4397. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4398. 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)));
  4399. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4400. 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));
  4401. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4402. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4403. const int x_ne = ne01 * ne00;
  4404. const int y_ne = padded_n * ne10;
  4405. const int d_ne = ne11 * ne01;
  4406. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4407. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4408. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4409. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4410. 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);
  4411. const uint64_t d_sz = sizeof(float) * d_ne;
  4412. vk_pipeline to_fp16_vk_0 = nullptr;
  4413. vk_pipeline to_fp16_vk_1 = nullptr;
  4414. vk_pipeline to_q8_1 = nullptr;
  4415. if (x_non_contig) {
  4416. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4417. } else {
  4418. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4419. }
  4420. if (y_non_contig) {
  4421. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4422. } else {
  4423. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4424. }
  4425. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4426. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4427. if (quantize_y) {
  4428. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4429. }
  4430. if (dryrun) {
  4431. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4432. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4433. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4434. if (
  4435. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4436. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4437. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4438. GGML_ABORT("Requested preallocation size is too large");
  4439. }
  4440. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4441. ctx->prealloc_size_x = x_sz_upd;
  4442. }
  4443. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  4444. ctx->prealloc_size_y = y_sz_upd;
  4445. }
  4446. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  4447. ctx->prealloc_size_split_k = split_k_size;
  4448. }
  4449. // Request descriptor sets
  4450. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4451. if (qx_needs_dequant) {
  4452. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4453. }
  4454. if (qy_needs_dequant) {
  4455. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4456. }
  4457. if (quantize_y) {
  4458. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  4459. }
  4460. if (split_k > 1) {
  4461. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  4462. }
  4463. return;
  4464. }
  4465. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4466. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4467. GGML_ASSERT(d_D != nullptr);
  4468. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  4469. vk_buffer d_X;
  4470. uint64_t x_buf_offset = 0;
  4471. vk_buffer d_Y;
  4472. uint64_t y_buf_offset = 0;
  4473. if (!src0_uma) {
  4474. d_Qx = src0_buf_ctx->dev_buffer;
  4475. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4476. GGML_ASSERT(d_Qx != nullptr);
  4477. }
  4478. if (!src1_uma) {
  4479. d_Qy = src1_buf_ctx->dev_buffer;
  4480. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4481. GGML_ASSERT(d_Qy != nullptr);
  4482. }
  4483. if (qx_needs_dequant) {
  4484. d_X = ctx->prealloc_x;
  4485. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4486. } else {
  4487. d_X = d_Qx;
  4488. x_buf_offset = qx_buf_offset;
  4489. GGML_ASSERT(qx_sz == x_sz);
  4490. }
  4491. if (qy_needs_dequant) {
  4492. d_Y = ctx->prealloc_y;
  4493. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4494. } else if (quantize_y) {
  4495. d_Y = ctx->prealloc_y;
  4496. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  4497. } else {
  4498. d_Y = d_Qy;
  4499. y_buf_offset = qy_buf_offset;
  4500. GGML_ASSERT(qy_sz == y_sz);
  4501. }
  4502. if (x_non_contig) {
  4503. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  4504. } else if (qx_needs_dequant) {
  4505. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4506. ggml_vk_sync_buffers(subctx);
  4507. 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});
  4508. }
  4509. if (y_non_contig) {
  4510. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  4511. }
  4512. if (quantize_y) {
  4513. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13);
  4514. }
  4515. uint32_t stride_batch_x = ne00*ne01;
  4516. uint32_t stride_batch_y = ne10*ne11;
  4517. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4518. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4519. }
  4520. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  4521. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4522. }
  4523. // compute
  4524. ggml_vk_matmul(
  4525. ctx, subctx, pipeline,
  4526. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4527. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  4528. ne01, ne11, ne10,
  4529. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  4530. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  4531. ); // NOLINT
  4532. }
  4533. static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4534. 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];
  4535. 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];
  4536. 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];
  4537. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  4538. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4539. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4540. const uint64_t ne00 = src0->ne[0];
  4541. const uint64_t ne01 = src0->ne[1];
  4542. const uint64_t ne02 = src0->ne[2];
  4543. const uint64_t ne03 = src0->ne[3];
  4544. const uint64_t ne10 = src1->ne[0];
  4545. const uint64_t ne11 = src1->ne[1];
  4546. const uint64_t ne12 = src1->ne[2];
  4547. const uint64_t ne13 = src1->ne[3];
  4548. const uint64_t ne20 = dst->ne[0];
  4549. const uint64_t ne21 = dst->ne[1];
  4550. const uint64_t ne22 = dst->ne[2];
  4551. const uint64_t ne23 = dst->ne[3];
  4552. const uint64_t r2 = ne12 / ne02;
  4553. const uint64_t r3 = ne13 / ne03;
  4554. // batch_n indicates that we need to compute a few vector results, and this assumes
  4555. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4556. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4557. bool batch_n = ne11 > 1;
  4558. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4559. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4560. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4561. vk_buffer d_Qx = nullptr;
  4562. size_t qx_buf_offset = 0;
  4563. vk_buffer d_Qy = nullptr;
  4564. size_t qy_buf_offset = 0;
  4565. bool src0_uma = false;
  4566. bool src1_uma = false;
  4567. if (ctx->device->uma) {
  4568. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4569. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4570. src0_uma = d_Qx != nullptr;
  4571. src1_uma = d_Qy != nullptr;
  4572. }
  4573. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4574. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4575. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4576. const bool qx_needs_dequant = x_non_contig;
  4577. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4578. // Not implemented
  4579. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4580. const uint64_t x_ne = ne01 * ne00;
  4581. const uint64_t y_ne = ne11 * ne10;
  4582. const uint64_t d_ne = ne11 * ne01;
  4583. 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);
  4584. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4585. 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;
  4586. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4587. const uint64_t d_sz = sizeof(float) * d_ne;
  4588. vk_pipeline to_fp16_vk_0 = nullptr;
  4589. vk_pipeline to_fp16_vk_1 = nullptr;
  4590. if (x_non_contig) {
  4591. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4592. }
  4593. if (y_non_contig) {
  4594. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4595. } else {
  4596. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4597. }
  4598. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  4599. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4600. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4601. GGML_ASSERT(dmmv != nullptr);
  4602. if (dryrun) {
  4603. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4604. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4605. if (
  4606. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4607. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4608. GGML_ABORT("Requested preallocation size is too large");
  4609. }
  4610. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4611. ctx->prealloc_size_x = x_sz_upd;
  4612. }
  4613. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4614. ctx->prealloc_size_y = y_sz_upd;
  4615. }
  4616. // Request descriptor sets
  4617. if (qx_needs_dequant) {
  4618. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4619. }
  4620. if (qy_needs_dequant) {
  4621. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4622. }
  4623. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  4624. return;
  4625. }
  4626. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4627. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4628. GGML_ASSERT(d_D != nullptr);
  4629. vk_buffer d_X;
  4630. uint64_t x_buf_offset = 0;
  4631. vk_buffer d_Y;
  4632. uint64_t y_buf_offset = 0;
  4633. if(!src0_uma) {
  4634. d_Qx = src0_buf_ctx->dev_buffer;
  4635. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4636. GGML_ASSERT(d_Qx != nullptr);
  4637. }
  4638. if(!src1_uma) {
  4639. d_Qy = src1_buf_ctx->dev_buffer;
  4640. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4641. GGML_ASSERT(d_Qy != nullptr);
  4642. }
  4643. if (qx_needs_dequant) {
  4644. d_X = ctx->prealloc_x;
  4645. } else {
  4646. d_X = d_Qx;
  4647. x_buf_offset = qx_buf_offset;
  4648. GGML_ASSERT(qx_sz == x_sz);
  4649. }
  4650. if (qy_needs_dequant) {
  4651. d_Y = ctx->prealloc_y;
  4652. } else {
  4653. d_Y = d_Qy;
  4654. y_buf_offset = qy_buf_offset;
  4655. GGML_ASSERT(qy_sz == y_sz);
  4656. }
  4657. if (x_non_contig) {
  4658. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4659. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  4660. }
  4661. if (y_non_contig) {
  4662. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4663. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  4664. }
  4665. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4666. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4667. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4668. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4669. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4670. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4671. }
  4672. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4673. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4674. }
  4675. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4676. uint32_t groups_x = ne01;
  4677. uint32_t groups_z = 1;
  4678. if (ne01 > max_groups_x) {
  4679. groups_z = 64;
  4680. groups_x = CEIL_DIV(groups_x, groups_z);
  4681. }
  4682. // compute
  4683. const vk_mat_vec_push_constants pc = {
  4684. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4685. stride_batch_x, stride_batch_y, stride_batch_d,
  4686. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4687. };
  4688. ggml_vk_sync_buffers(subctx);
  4689. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4690. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  4691. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4692. }
  4693. static void ggml_vk_mul_mat_vec_p021_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4694. 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];
  4695. 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];
  4696. 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];
  4697. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4698. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4699. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4700. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4701. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4702. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4703. const uint64_t ne00 = src0->ne[0];
  4704. const uint64_t ne01 = src0->ne[1];
  4705. const uint64_t ne02 = src0->ne[2];
  4706. // const uint64_t ne03 = src0->ne[3];
  4707. const uint64_t ne10 = src1->ne[0];
  4708. const uint64_t ne11 = src1->ne[1];
  4709. const uint64_t ne12 = src1->ne[2];
  4710. // const uint64_t ne13 = src1->ne[3];
  4711. GGML_ASSERT(ne11 == 1);
  4712. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4713. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4714. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4715. vk_buffer d_Qy = nullptr;
  4716. size_t qy_buf_offset = 0;
  4717. bool src1_uma = false;
  4718. if (ctx->device->uma) {
  4719. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4720. src1_uma = d_Qy != nullptr;
  4721. }
  4722. const uint64_t x_ne = ne00 * ne01 * ne02;
  4723. const uint64_t y_ne = ne10 * ne11 * ne12;
  4724. const uint64_t d_ne = ne01 * ne11 * ne12;
  4725. 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);
  4726. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4727. const uint64_t d_sz = sizeof(float) * d_ne;
  4728. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  4729. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  4730. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  4731. gqa_ratio = 1;
  4732. }
  4733. if (dryrun) {
  4734. // Request descriptor sets
  4735. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  4736. return;
  4737. }
  4738. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4739. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4740. GGML_ASSERT(d_D != nullptr);
  4741. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4742. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4743. GGML_ASSERT(d_Qx != nullptr);
  4744. if (!src1_uma) {
  4745. d_Qy = src1_buf_ctx->dev_buffer;
  4746. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4747. GGML_ASSERT(d_Qx != nullptr);
  4748. }
  4749. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4750. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4751. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4752. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4753. // compute
  4754. const std::array<uint32_t, 6> pc = { (uint32_t)ne00, (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)) };
  4755. uint32_t workgroups_z = (uint32_t)ne12;
  4756. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  4757. if (gqa_ratio > 1) {
  4758. workgroups_z /= gqa_ratio;
  4759. }
  4760. ggml_vk_sync_buffers(subctx);
  4761. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, pc, { 1, (uint32_t)ne01, workgroups_z });
  4762. }
  4763. static void ggml_vk_mul_mat_vec_nc_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4764. 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];
  4765. 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];
  4766. 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];
  4767. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4768. GGML_ASSERT(!ggml_is_transposed(src0));
  4769. GGML_ASSERT(!ggml_is_transposed(src1));
  4770. GGML_ASSERT(!ggml_is_permuted(src0));
  4771. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4772. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4773. const uint64_t ne00 = src0->ne[0];
  4774. const uint64_t ne01 = src0->ne[1];
  4775. const uint64_t ne02 = src0->ne[2];
  4776. // const uint64_t ne03 = src0->ne[3];
  4777. const uint64_t nb01 = src0->nb[1];
  4778. const uint64_t nb02 = src0->nb[2];
  4779. const uint64_t nb12 = src1->nb[2];
  4780. // const uint64_t ne10 = src1->ne[0];
  4781. const uint64_t ne11 = src1->ne[1];
  4782. const uint64_t ne12 = src1->ne[2];
  4783. // const uint64_t ne13 = src1->ne[3];
  4784. GGML_ASSERT(ne11 == 1);
  4785. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4786. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4787. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4788. vk_buffer d_Qy = nullptr;
  4789. size_t qy_buf_offset = 0;
  4790. bool src1_uma = false;
  4791. if (ctx->device->uma) {
  4792. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4793. src1_uma = d_Qy != nullptr;
  4794. }
  4795. const uint64_t d_ne = ne01 * ne11 * ne12;
  4796. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4797. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4798. const uint32_t channel_stride_y = nb12 / sizeof(float);
  4799. const uint64_t qx_sz = ggml_nbytes(src0);
  4800. const uint64_t qy_sz = ggml_nbytes(src1);
  4801. const uint64_t d_sz = sizeof(float) * d_ne;
  4802. if (dryrun) {
  4803. // Request descriptor sets
  4804. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4805. return;
  4806. }
  4807. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4808. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4809. GGML_ASSERT(d_D != nullptr);
  4810. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4811. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4812. GGML_ASSERT(d_Qx != nullptr);
  4813. if (!src1_uma) {
  4814. d_Qy = src1_buf_ctx->dev_buffer;
  4815. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4816. GGML_ASSERT(d_Qx != nullptr);
  4817. }
  4818. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4819. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4820. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4821. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4822. // compute
  4823. const std::array<uint32_t, 9> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, channel_stride_y, (uint32_t)(ne12 / ne02), (uint32_t)ne12, (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)) };
  4824. ggml_vk_sync_buffers(subctx);
  4825. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4826. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  4827. }
  4828. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4829. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4830. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4831. // detect 0213 permutation, and batch size of 1
  4832. src0->nb[0] <= src0->nb[2] &&
  4833. src0->nb[2] <= src0->nb[1] &&
  4834. src0->nb[1] <= src0->nb[3] &&
  4835. src1->nb[0] <= src1->nb[2] &&
  4836. src1->nb[2] <= src1->nb[1] &&
  4837. src1->nb[1] <= src1->nb[3] &&
  4838. src0->ne[3] == 1 &&
  4839. src1->ne[3] == 1) {
  4840. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4841. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4842. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4843. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4844. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4845. // when ne12 and ne13 are one.
  4846. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4847. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  4848. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4849. } else {
  4850. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4851. }
  4852. }
  4853. static void ggml_vk_mul_mat_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  4854. 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];
  4855. 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];
  4856. 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];
  4857. 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] << "),)");
  4858. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4859. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4860. const uint64_t ne00 = src0->ne[0];
  4861. const uint64_t ne01 = src0->ne[1];
  4862. const uint64_t ne02 = src0->ne[2];
  4863. const uint64_t ne03 = src0->ne[3];
  4864. const uint64_t ne10 = src1->ne[0];
  4865. const uint64_t ne11 = src1->ne[1];
  4866. const uint64_t ne12 = src1->ne[2];
  4867. const uint64_t ne13 = src1->ne[3];
  4868. const uint64_t nei0 = ids->ne[0];
  4869. const uint64_t nei1 = ids->ne[1];
  4870. GGML_ASSERT(nei0 * nei1 <= 4096);
  4871. const uint32_t nbi1 = ids->nb[1];
  4872. const uint32_t nbi2 = ids->nb[2];
  4873. const uint64_t ne20 = dst->ne[0];
  4874. const uint64_t ne21 = dst->ne[1];
  4875. const uint64_t ne22 = dst->ne[2];
  4876. const uint64_t ne23 = dst->ne[3];
  4877. const uint64_t n_as = ne02;
  4878. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4879. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4880. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4881. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4882. vk_buffer d_Qx = nullptr;
  4883. size_t qx_buf_offset = 0;
  4884. vk_buffer d_Qy = nullptr;
  4885. size_t qy_buf_offset = 0;
  4886. vk_buffer d_ids = nullptr;
  4887. size_t ids_buf_offset = 0;
  4888. bool src0_uma = false;
  4889. bool src1_uma = false;
  4890. bool ids_uma = false;
  4891. if (ctx->device->uma) {
  4892. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4893. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4894. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4895. src0_uma = d_Qx != nullptr;
  4896. src1_uma = d_Qy != nullptr;
  4897. ids_uma = d_ids != nullptr;
  4898. }
  4899. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4900. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4901. !ggml_vk_dim01_contiguous(src0);
  4902. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4903. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4904. !ggml_vk_dim01_contiguous(src1);
  4905. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4906. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4907. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4908. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4909. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4910. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  4911. if (qx_needs_dequant) {
  4912. // Fall back to dequant + f16 mulmat
  4913. 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]);
  4914. }
  4915. // Not implemented
  4916. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4917. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? f16_type : src0->type));
  4918. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4919. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  4920. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4921. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4922. const uint64_t x_ne = ne01 * ne00;
  4923. const uint64_t y_ne = padded_n * ne10;
  4924. const uint64_t d_ne = ne21 * ne20;
  4925. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4926. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4927. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4928. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4929. const uint64_t ids_sz = nbi2;
  4930. const uint64_t d_sz = sizeof(float) * d_ne;
  4931. vk_pipeline to_fp16_vk_0 = nullptr;
  4932. vk_pipeline to_fp16_vk_1 = nullptr;
  4933. if (x_non_contig) {
  4934. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4935. } else {
  4936. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4937. }
  4938. if (y_non_contig) {
  4939. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4940. } else {
  4941. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4942. }
  4943. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4944. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4945. if (dryrun) {
  4946. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4947. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4948. if (
  4949. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4950. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4951. GGML_ABORT("Requested preallocation size is too large");
  4952. }
  4953. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4954. ctx->prealloc_size_x = x_sz_upd;
  4955. }
  4956. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4957. ctx->prealloc_size_y = y_sz_upd;
  4958. }
  4959. // Request descriptor sets
  4960. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4961. if (qx_needs_dequant) {
  4962. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4963. }
  4964. if (qy_needs_dequant) {
  4965. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4966. }
  4967. return;
  4968. }
  4969. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4970. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4971. GGML_ASSERT(d_D != nullptr);
  4972. vk_buffer d_X;
  4973. uint64_t x_buf_offset = 0;
  4974. vk_buffer d_Y;
  4975. uint64_t y_buf_offset = 0;
  4976. if (!src0_uma) {
  4977. d_Qx = src0_buf_ctx->dev_buffer;
  4978. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4979. GGML_ASSERT(d_Qx != nullptr);
  4980. }
  4981. if (!src1_uma) {
  4982. d_Qy = src1_buf_ctx->dev_buffer;
  4983. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4984. GGML_ASSERT(d_Qy != nullptr);
  4985. }
  4986. if (!ids_uma) {
  4987. d_ids = ids_buf_ctx->dev_buffer;
  4988. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4989. GGML_ASSERT(d_ids != nullptr);
  4990. }
  4991. if (qx_needs_dequant) {
  4992. d_X = ctx->prealloc_x;
  4993. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4994. } else {
  4995. d_X = d_Qx;
  4996. x_buf_offset = qx_buf_offset;
  4997. GGML_ASSERT(qx_sz == x_sz);
  4998. }
  4999. if (qy_needs_dequant) {
  5000. d_Y = ctx->prealloc_y;
  5001. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  5002. } else {
  5003. d_Y = d_Qy;
  5004. y_buf_offset = qy_buf_offset;
  5005. GGML_ASSERT(qy_sz == y_sz);
  5006. }
  5007. if (x_non_contig) {
  5008. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5009. } else if (qx_needs_dequant) {
  5010. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  5011. ggml_vk_sync_buffers(subctx);
  5012. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  5013. { 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});
  5014. }
  5015. if (y_non_contig) {
  5016. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5017. }
  5018. uint32_t stride_batch_x = ne00*ne01;
  5019. uint32_t stride_batch_y = ne10*ne11;
  5020. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  5021. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  5022. }
  5023. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5024. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5025. }
  5026. // compute
  5027. ggml_vk_matmul_id(
  5028. ctx, subctx, pipeline,
  5029. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  5030. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  5031. ne01, ne21, ne10, ne10, ne10, ne01,
  5032. stride_batch_x, stride_batch_y, ne20*ne21,
  5033. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  5034. ); // NOLINT
  5035. }
  5036. static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  5037. 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];
  5038. 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];
  5039. 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];
  5040. 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];
  5041. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5042. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  5043. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  5044. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  5045. const uint64_t ne00 = src0->ne[0];
  5046. const uint64_t ne01 = src0->ne[1];
  5047. const uint64_t ne02 = src0->ne[2];
  5048. const uint64_t ne03 = src0->ne[3];
  5049. const uint64_t ne10 = src1->ne[0];
  5050. const uint64_t ne11 = src1->ne[1];
  5051. const uint64_t ne12 = src1->ne[2];
  5052. const uint64_t ne13 = src1->ne[3];
  5053. const uint64_t nei0 = ids->ne[0];
  5054. const uint64_t nei1 = ids->ne[1];
  5055. const uint64_t nbi2 = ids->nb[2];
  5056. GGML_ASSERT(nei1 == 1);
  5057. const uint64_t ne20 = dst->ne[0];
  5058. const uint64_t ne21 = dst->ne[1];
  5059. const uint64_t ne22 = dst->ne[2];
  5060. const uint64_t ne23 = dst->ne[3];
  5061. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5062. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5063. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  5064. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  5065. vk_buffer d_Qx = nullptr;
  5066. size_t qx_buf_offset = 0;
  5067. vk_buffer d_Qy = nullptr;
  5068. size_t qy_buf_offset = 0;
  5069. vk_buffer d_ids = nullptr;
  5070. size_t ids_buf_offset = 0;
  5071. bool src0_uma = false;
  5072. bool src1_uma = false;
  5073. bool ids_uma = false;
  5074. if (ctx->device->uma) {
  5075. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5076. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5077. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5078. src0_uma = d_Qx != nullptr;
  5079. src1_uma = d_Qy != nullptr;
  5080. ids_uma = d_ids != nullptr;
  5081. }
  5082. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5083. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5084. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5085. const bool qx_needs_dequant = x_non_contig;
  5086. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5087. // Not implemented
  5088. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5089. const uint64_t x_ne = ne01 * ne00;
  5090. const uint64_t y_ne = ne11 * ne10;
  5091. const uint64_t d_ne = ne21 * ne20;
  5092. 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);
  5093. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5094. 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;
  5095. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5096. const uint64_t ids_sz = nbi2;
  5097. const uint64_t d_sz = sizeof(float) * d_ne;
  5098. vk_pipeline to_fp16_vk_0 = nullptr;
  5099. vk_pipeline to_fp16_vk_1 = nullptr;
  5100. if (x_non_contig) {
  5101. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5102. }
  5103. if (y_non_contig) {
  5104. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5105. } else {
  5106. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5107. }
  5108. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5109. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5110. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5111. GGML_ASSERT(dmmv != nullptr);
  5112. if (dryrun) {
  5113. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5114. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5115. if (
  5116. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5117. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5118. GGML_ABORT("Requested preallocation size is too large");
  5119. }
  5120. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5121. ctx->prealloc_size_x = x_sz_upd;
  5122. }
  5123. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5124. ctx->prealloc_size_y = y_sz_upd;
  5125. }
  5126. // Request descriptor sets
  5127. if (qx_needs_dequant) {
  5128. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5129. }
  5130. if (qy_needs_dequant) {
  5131. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5132. }
  5133. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5134. return;
  5135. }
  5136. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5137. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5138. GGML_ASSERT(d_D != nullptr);
  5139. vk_buffer d_X;
  5140. uint64_t x_buf_offset = 0;
  5141. vk_buffer d_Y;
  5142. uint64_t y_buf_offset = 0;
  5143. if(!src0_uma) {
  5144. d_Qx = src0_buf_ctx->dev_buffer;
  5145. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5146. GGML_ASSERT(d_Qx != nullptr);
  5147. }
  5148. if(!src1_uma) {
  5149. d_Qy = src1_buf_ctx->dev_buffer;
  5150. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5151. GGML_ASSERT(d_Qy != nullptr);
  5152. }
  5153. if(!ids_uma) {
  5154. d_ids = ids_buf_ctx->dev_buffer;
  5155. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5156. GGML_ASSERT(d_ids != nullptr);
  5157. }
  5158. if (qx_needs_dequant) {
  5159. d_X = ctx->prealloc_x;
  5160. } else {
  5161. d_X = d_Qx;
  5162. x_buf_offset = qx_buf_offset;
  5163. GGML_ASSERT(qx_sz == x_sz);
  5164. }
  5165. if (qy_needs_dequant) {
  5166. d_Y = ctx->prealloc_y;
  5167. } else {
  5168. d_Y = d_Qy;
  5169. y_buf_offset = qy_buf_offset;
  5170. GGML_ASSERT(qy_sz == y_sz);
  5171. }
  5172. if (x_non_contig) {
  5173. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5174. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_0, src0, { d_Qx, qx_buf_offset, VK_WHOLE_SIZE }, { d_X, 0, VK_WHOLE_SIZE });
  5175. }
  5176. if (y_non_contig) {
  5177. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5178. ggml_vk_cpy_to_contiguous(ctx, subctx, to_fp16_vk_1, src1, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE });
  5179. }
  5180. uint32_t stride_batch_y = ne10*ne11;
  5181. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5182. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5183. }
  5184. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5185. uint32_t groups_x = ne01;
  5186. uint32_t groups_z = 1;
  5187. if (ne01 > max_groups_x) {
  5188. groups_z = 64;
  5189. groups_x = CEIL_DIV(groups_x, groups_z);
  5190. }
  5191. // compute
  5192. const vk_mat_vec_id_push_constants pc = {
  5193. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5194. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5195. (uint32_t)nei0, (uint32_t)ne11,
  5196. };
  5197. ggml_vk_sync_buffers(subctx);
  5198. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5199. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5200. vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23}, vk_subbuffer{ d_ids, ids_buf_offset, ids_sz } },
  5201. pc, { groups_x, (uint32_t)nei0, groups_z });
  5202. }
  5203. static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  5204. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5205. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5206. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5207. } else {
  5208. // Split based on number of ids, to fit in shared memory
  5209. const uint32_t nei0 = (uint32_t)src2->ne[0];
  5210. const uint32_t nei1 = (uint32_t)src2->ne[1];
  5211. GGML_ASSERT(nei0 <= 4096);
  5212. const uint32_t split_size = std::min(nei1, 4096u / nei0);
  5213. ggml_tensor src1_copy = *src1;
  5214. ggml_tensor src2_copy = *src2;
  5215. ggml_tensor dst_copy = *dst;
  5216. for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
  5217. const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
  5218. src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
  5219. src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
  5220. dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
  5221. src1_copy.ne[2] = n_tokens;
  5222. src2_copy.ne[1] = n_tokens;
  5223. dst_copy.ne[2] = n_tokens;
  5224. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
  5225. }
  5226. }
  5227. }
  5228. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5229. // Needs to be kept up to date on shader changes
  5230. GGML_UNUSED(hsv);
  5231. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5232. const uint32_t Br = get_fa_scalar_num_large_rows(hsv);
  5233. const uint32_t Bc = scalar_flash_attention_Bc;
  5234. const uint32_t tmpsh = wg_size * sizeof(float);
  5235. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5236. const uint32_t masksh = Bc * Br * sizeof(float);
  5237. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5238. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5239. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5240. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5241. return supported;
  5242. }
  5243. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5244. // Needs to be kept up to date on shader changes
  5245. GGML_UNUSED(hsv);
  5246. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5247. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5248. const uint32_t Bc = scalar_flash_attention_Bc;
  5249. const uint32_t acctype = f32acc ? 4 : 2;
  5250. const uint32_t f16vec4 = 8;
  5251. const uint32_t tmpsh = wg_size * sizeof(float);
  5252. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5253. const uint32_t Qf = Br * (hsk / 4 + 2) * f16vec4;
  5254. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5255. const uint32_t sfsh = Bc * sfshstride * acctype;
  5256. const uint32_t kshstride = hsk / 4 + 2;
  5257. const uint32_t ksh = Bc * kshstride * f16vec4;
  5258. const uint32_t slope = Br * sizeof(float);
  5259. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5260. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5261. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5262. return supported;
  5263. }
  5264. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, ggml_tensor * dst, bool dryrun = false) {
  5265. 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];
  5266. 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];
  5267. 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];
  5268. 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];
  5269. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5270. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5271. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5272. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5273. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5274. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5275. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5276. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5277. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5278. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5279. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5280. const uint32_t nem3 = mask ? mask->ne[3] : 0;
  5281. const uint32_t HSK = nek0;
  5282. const uint32_t HSV = nev0;
  5283. uint32_t N = neq1;
  5284. const uint32_t KV = nek1;
  5285. GGML_ASSERT(ne0 == HSV);
  5286. GGML_ASSERT(ne2 == N);
  5287. // input tensor rows must be contiguous
  5288. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  5289. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  5290. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  5291. GGML_ASSERT(neq0 == HSK);
  5292. GGML_ASSERT(neq1 == N);
  5293. GGML_ASSERT(nev1 == nek1);
  5294. // dst cannot be transposed or permuted
  5295. GGML_ASSERT(nb0 == sizeof(float));
  5296. GGML_ASSERT(nb0 <= nb1);
  5297. GGML_ASSERT(nb1 <= nb2);
  5298. GGML_ASSERT(nb2 <= nb3);
  5299. assert(dst->type == GGML_TYPE_F32);
  5300. assert(q->type == GGML_TYPE_F32);
  5301. assert(k->type == v->type);
  5302. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  5303. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  5304. if (path == FA_COOPMAT1) {
  5305. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  5306. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  5307. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  5308. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  5309. path = FA_SCALAR;
  5310. }
  5311. }
  5312. uint32_t gqa_ratio = 1;
  5313. uint32_t qk_ratio = neq2 / nek2;
  5314. uint32_t workgroups_x = (uint32_t)neq1;
  5315. uint32_t workgroups_y = (uint32_t)neq2;
  5316. uint32_t workgroups_z = (uint32_t)neq3;
  5317. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  5318. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  5319. uint32_t max_gqa;
  5320. switch (path) {
  5321. case FA_SCALAR:
  5322. case FA_COOPMAT1:
  5323. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  5324. max_gqa = get_fa_scalar_num_large_rows(HSV);
  5325. break;
  5326. case FA_COOPMAT2:
  5327. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  5328. break;
  5329. default:
  5330. GGML_ASSERT(0);
  5331. }
  5332. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  5333. qk_ratio * nek2 == neq2 && nek2 == nev2 && nem2 <= 1) {
  5334. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  5335. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  5336. // and change addressing calculations to index Q's dimension 2.
  5337. gqa_ratio = qk_ratio;
  5338. N = gqa_ratio;
  5339. workgroups_y /= N;
  5340. }
  5341. vk_pipeline *pipelines;
  5342. bool small_rows = N <= get_fa_num_small_rows(path);
  5343. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  5344. // So use scalar instead.
  5345. if (small_rows && path == FA_COOPMAT1) {
  5346. path = FA_SCALAR;
  5347. }
  5348. // scalar is faster than coopmat2 when N==1
  5349. if (N == 1 && path == FA_COOPMAT2) {
  5350. path = FA_SCALAR;
  5351. }
  5352. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  5353. if (path == FA_SCALAR &&
  5354. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  5355. small_rows = true;
  5356. }
  5357. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  5358. FaHeadSizes head_sizes = fa_get_head_sizes(k->ne[0], v->ne[0]);
  5359. switch (path) {
  5360. case FA_SCALAR:
  5361. pipelines = &ctx->device->pipeline_flash_attn_f32_f16[k->type][head_sizes][f32acc][small_rows][0];
  5362. break;
  5363. case FA_COOPMAT1:
  5364. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm1[k->type][head_sizes][f32acc][small_rows][0];
  5365. break;
  5366. case FA_COOPMAT2:
  5367. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm2[k->type][head_sizes][f32acc][small_rows][0];
  5368. break;
  5369. default:
  5370. GGML_ASSERT(0);
  5371. }
  5372. assert(pipelines);
  5373. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  5374. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  5375. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  5376. bool aligned = (KV % pipelines[1]->align) == 0 &&
  5377. // the "aligned" shader variant will forcibly align strides, for performance
  5378. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  5379. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  5380. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  5381. vk_pipeline pipeline = pipelines[aligned];
  5382. assert(pipeline);
  5383. uint32_t split_kv = KV;
  5384. uint32_t split_k = 1;
  5385. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  5386. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  5387. // Try to use split_k when KV is large enough to be worth the overhead
  5388. if (workgroups_x == 1 && shader_core_count > 0) {
  5389. // Try to run two workgroups per SM.
  5390. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  5391. if (split_k > 1) {
  5392. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  5393. // of "align", so recompute split_k based on that.
  5394. split_kv = ROUNDUP_POW2(std::max(1u, KV / split_k), pipelines[1]->align);
  5395. split_k = CEIL_DIV(KV, split_kv);
  5396. workgroups_x = split_k;
  5397. }
  5398. }
  5399. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  5400. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  5401. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  5402. if (split_k_size > ctx->device->max_memory_allocation_size) {
  5403. GGML_ABORT("Requested preallocation size is too large");
  5404. }
  5405. if (ctx->prealloc_size_split_k < split_k_size) {
  5406. ctx->prealloc_size_split_k = split_k_size;
  5407. }
  5408. if (dryrun) {
  5409. // Request descriptor sets
  5410. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5411. if (split_k > 1) {
  5412. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  5413. }
  5414. return;
  5415. }
  5416. float scale = 1.0f;
  5417. float max_bias = 0.0f;
  5418. float logit_softcap = 0.0f;
  5419. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  5420. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  5421. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  5422. if (logit_softcap != 0) {
  5423. scale /= logit_softcap;
  5424. }
  5425. const uint32_t n_head_kv = neq2;
  5426. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5427. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5428. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5429. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  5430. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  5431. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  5432. if (ctx->device->uma) {
  5433. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  5434. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  5435. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  5436. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  5437. Q_uma = d_Q != nullptr;
  5438. K_uma = d_K != nullptr;
  5439. V_uma = d_V != nullptr;
  5440. D_uma = d_D != nullptr;
  5441. if (mask) {
  5442. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  5443. M_uma = d_M != nullptr;
  5444. }
  5445. }
  5446. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5447. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  5448. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  5449. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  5450. if (!Q_uma) {
  5451. d_Q = q_buf_ctx->dev_buffer;
  5452. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  5453. }
  5454. if (!K_uma) {
  5455. d_K = k_buf_ctx->dev_buffer;
  5456. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  5457. }
  5458. if (!V_uma) {
  5459. d_V = v_buf_ctx->dev_buffer;
  5460. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  5461. }
  5462. if (!D_uma) {
  5463. d_D = d_buf_ctx->dev_buffer;
  5464. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5465. }
  5466. if (!M_uma) {
  5467. d_M = d_Q;
  5468. m_buf_offset = q_buf_offset;
  5469. if (mask) {
  5470. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  5471. d_M = m_buf_ctx->dev_buffer;
  5472. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  5473. }
  5474. }
  5475. uint32_t mask_n_head_log2 = ((mask != nullptr) << 16) | n_head_log2;
  5476. const vk_flash_attn_push_constants pc = { N, KV,
  5477. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  5478. (uint32_t)neq2, (uint32_t)neq3,
  5479. (uint32_t)nek2, (uint32_t)nek3,
  5480. (uint32_t)nev2, (uint32_t)nev3,
  5481. nem1, nem2, nem3,
  5482. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  5483. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  5484. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  5485. scale, max_bias, logit_softcap,
  5486. mask_n_head_log2, m0, m1,
  5487. gqa_ratio, split_kv, split_k };
  5488. ggml_vk_sync_buffers(subctx);
  5489. if (split_k > 1) {
  5490. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5491. {
  5492. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5493. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5494. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5495. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5496. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5497. },
  5498. // We only use split_k when group query attention is enabled, which means
  5499. // there's no more than one tile of rows (i.e. workgroups_x would have been
  5500. // one). We reuse workgroups_x to mean the number of splits, so we need to
  5501. // cancel out the divide by wg_denoms[0].
  5502. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  5503. ggml_vk_sync_buffers(subctx);
  5504. const std::array<uint32_t, 4> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k };
  5505. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  5506. {
  5507. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5508. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5509. },
  5510. pc2, { (uint32_t)ne1, HSV, (uint32_t)ne3 });
  5511. } else {
  5512. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5513. {
  5514. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5515. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5516. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5517. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5518. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5519. },
  5520. pc, { workgroups_x, workgroups_y, workgroups_z });
  5521. }
  5522. }
  5523. static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op) {
  5524. switch (op) {
  5525. case GGML_OP_GET_ROWS:
  5526. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5527. if (dst->type == GGML_TYPE_F16) {
  5528. return ctx->device->pipeline_get_rows[src0->type];
  5529. }
  5530. if (dst->type == GGML_TYPE_F32) {
  5531. return ctx->device->pipeline_get_rows_f32[src0->type];
  5532. }
  5533. return nullptr;
  5534. case GGML_OP_ACC:
  5535. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5536. return ctx->device->pipeline_acc_f32;
  5537. }
  5538. return nullptr;
  5539. case GGML_OP_ADD:
  5540. case GGML_OP_SUB:
  5541. case GGML_OP_MUL:
  5542. case GGML_OP_DIV:
  5543. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5544. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  5545. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  5546. return nullptr;
  5547. }
  5548. switch (op) {
  5549. case GGML_OP_ADD:
  5550. {
  5551. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  5552. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5553. }
  5554. case GGML_OP_SUB:
  5555. {
  5556. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  5557. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5558. }
  5559. case GGML_OP_MUL:
  5560. {
  5561. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  5562. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5563. }
  5564. case GGML_OP_DIV:
  5565. {
  5566. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  5567. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5568. }
  5569. default:
  5570. break;
  5571. }
  5572. return nullptr;
  5573. case GGML_OP_CONCAT:
  5574. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5575. return ctx->device->pipeline_concat_f32;
  5576. }
  5577. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5578. return ctx->device->pipeline_concat_f16;
  5579. }
  5580. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  5581. return ctx->device->pipeline_concat_i32;
  5582. }
  5583. return nullptr;
  5584. case GGML_OP_UPSCALE:
  5585. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5586. int mode = ggml_get_op_params_i32(dst, 0);
  5587. switch (mode) {
  5588. case GGML_SCALE_MODE_NEAREST:
  5589. return ctx->device->pipeline_upscale_nearest_f32;
  5590. case GGML_SCALE_MODE_BILINEAR:
  5591. return ctx->device->pipeline_upscale_bilinear_f32;
  5592. case GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ALIGN_CORNERS:
  5593. return ctx->device->pipeline_upscale_bilinear_ac_f32;
  5594. }
  5595. }
  5596. return nullptr;
  5597. case GGML_OP_SCALE:
  5598. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5599. return ctx->device->pipeline_scale_f32;
  5600. }
  5601. return nullptr;
  5602. case GGML_OP_SQR:
  5603. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5604. return ctx->device->pipeline_sqr_f32;
  5605. }
  5606. return nullptr;
  5607. case GGML_OP_SIN:
  5608. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5609. return ctx->device->pipeline_sin_f32;
  5610. }
  5611. return nullptr;
  5612. case GGML_OP_COS:
  5613. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5614. return ctx->device->pipeline_cos_f32;
  5615. }
  5616. return nullptr;
  5617. case GGML_OP_CLAMP:
  5618. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5619. return ctx->device->pipeline_clamp_f32;
  5620. }
  5621. return nullptr;
  5622. case GGML_OP_PAD:
  5623. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5624. return ctx->device->pipeline_pad_f32;
  5625. }
  5626. return nullptr;
  5627. case GGML_OP_ROLL:
  5628. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5629. return ctx->device->pipeline_roll_f32;
  5630. }
  5631. return nullptr;
  5632. case GGML_OP_REPEAT:
  5633. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  5634. return ctx->device->pipeline_repeat_f32;
  5635. }
  5636. return nullptr;
  5637. case GGML_OP_REPEAT_BACK:
  5638. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5639. return ctx->device->pipeline_repeat_back_f32;
  5640. }
  5641. return nullptr;
  5642. case GGML_OP_CPY:
  5643. case GGML_OP_CONT:
  5644. case GGML_OP_DUP:
  5645. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  5646. case GGML_OP_SET_ROWS:
  5647. return ctx->device->pipeline_set_rows[dst->type];
  5648. case GGML_OP_SILU_BACK:
  5649. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5650. return ctx->device->pipeline_silu_back_f32;
  5651. }
  5652. return nullptr;
  5653. case GGML_OP_NORM:
  5654. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5655. return ctx->device->pipeline_norm_f32;
  5656. }
  5657. return nullptr;
  5658. case GGML_OP_GROUP_NORM:
  5659. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5660. return ctx->device->pipeline_group_norm_f32;
  5661. }
  5662. return nullptr;
  5663. case GGML_OP_RMS_NORM:
  5664. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5665. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  5666. }
  5667. return nullptr;
  5668. case GGML_OP_RMS_NORM_BACK:
  5669. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5670. return ctx->device->pipeline_rms_norm_back_f32;
  5671. }
  5672. return nullptr;
  5673. case GGML_OP_L2_NORM:
  5674. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5675. return ctx->device->pipeline_l2_norm_f32;
  5676. }
  5677. return nullptr;
  5678. case GGML_OP_UNARY:
  5679. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5680. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5681. (src0->type != dst->type)) {
  5682. return nullptr;
  5683. }
  5684. switch (ggml_get_unary_op(dst)) {
  5685. case GGML_UNARY_OP_SILU:
  5686. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  5687. case GGML_UNARY_OP_GELU:
  5688. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  5689. case GGML_UNARY_OP_GELU_ERF:
  5690. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  5691. case GGML_UNARY_OP_GELU_QUICK:
  5692. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  5693. case GGML_UNARY_OP_RELU:
  5694. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  5695. case GGML_UNARY_OP_TANH:
  5696. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  5697. case GGML_UNARY_OP_SIGMOID:
  5698. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  5699. default:
  5700. break;
  5701. }
  5702. return nullptr;
  5703. case GGML_OP_GLU:
  5704. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5705. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5706. (src0->type != dst->type)) {
  5707. return nullptr;
  5708. }
  5709. switch (ggml_get_glu_op(dst)) {
  5710. case GGML_GLU_OP_GEGLU:
  5711. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  5712. case GGML_GLU_OP_REGLU:
  5713. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  5714. case GGML_GLU_OP_SWIGLU:
  5715. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  5716. case GGML_GLU_OP_GEGLU_ERF:
  5717. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  5718. case GGML_GLU_OP_GEGLU_QUICK:
  5719. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  5720. default:
  5721. break;
  5722. }
  5723. return nullptr;
  5724. case GGML_OP_DIAG_MASK_INF:
  5725. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5726. return ctx->device->pipeline_diag_mask_inf_f32;
  5727. }
  5728. return nullptr;
  5729. case GGML_OP_SOFT_MAX:
  5730. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  5731. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  5732. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  5733. }
  5734. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5735. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  5736. }
  5737. return nullptr;
  5738. case GGML_OP_SOFT_MAX_BACK:
  5739. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5740. return ctx->device->pipeline_soft_max_back_f32;
  5741. }
  5742. return nullptr;
  5743. case GGML_OP_ROPE:
  5744. case GGML_OP_ROPE_BACK:
  5745. {
  5746. const int mode = ((const int32_t *) dst->op_params)[2];
  5747. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  5748. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  5749. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  5750. if (is_neox) {
  5751. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5752. return ctx->device->pipeline_rope_neox_f32;
  5753. }
  5754. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5755. return ctx->device->pipeline_rope_neox_f16;
  5756. }
  5757. } else if (is_mrope && !is_vision) {
  5758. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5759. return ctx->device->pipeline_rope_multi_f32;
  5760. }
  5761. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5762. return ctx->device->pipeline_rope_multi_f16;
  5763. }
  5764. } else if (is_vision) {
  5765. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5766. return ctx->device->pipeline_rope_vision_f32;
  5767. }
  5768. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5769. return ctx->device->pipeline_rope_vision_f16;
  5770. }
  5771. } else {
  5772. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5773. return ctx->device->pipeline_rope_norm_f32;
  5774. }
  5775. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5776. return ctx->device->pipeline_rope_norm_f16;
  5777. }
  5778. }
  5779. return nullptr;
  5780. }
  5781. case GGML_OP_ARGSORT:
  5782. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5783. return ctx->device->pipeline_argsort_f32;
  5784. }
  5785. return nullptr;
  5786. case GGML_OP_SUM:
  5787. case GGML_OP_SUM_ROWS:
  5788. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5789. return ctx->device->pipeline_sum_rows_f32;
  5790. }
  5791. return nullptr;
  5792. case GGML_OP_ARGMAX:
  5793. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5794. return ctx->device->pipeline_argmax_f32;
  5795. }
  5796. return nullptr;
  5797. case GGML_OP_COUNT_EQUAL:
  5798. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  5799. return ctx->device->pipeline_count_equal_i32;
  5800. }
  5801. return nullptr;
  5802. case GGML_OP_IM2COL:
  5803. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5804. return ctx->device->pipeline_im2col_f32;
  5805. }
  5806. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  5807. return ctx->device->pipeline_im2col_f32_f16;
  5808. }
  5809. return nullptr;
  5810. case GGML_OP_TIMESTEP_EMBEDDING:
  5811. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5812. return ctx->device->pipeline_timestep_embedding_f32;
  5813. }
  5814. return nullptr;
  5815. case GGML_OP_CONV_TRANSPOSE_1D:
  5816. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5817. return ctx->device->pipeline_conv_transpose_1d_f32;
  5818. }
  5819. return nullptr;
  5820. case GGML_OP_POOL_2D:
  5821. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5822. return ctx->device->pipeline_pool2d_f32;
  5823. }
  5824. return nullptr;
  5825. case GGML_OP_RWKV_WKV6:
  5826. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5827. return ctx->device->pipeline_rwkv_wkv6_f32;
  5828. }
  5829. return nullptr;
  5830. case GGML_OP_RWKV_WKV7:
  5831. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5832. return ctx->device->pipeline_rwkv_wkv7_f32;
  5833. }
  5834. return nullptr;
  5835. case GGML_OP_OPT_STEP_ADAMW:
  5836. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5837. return ctx->device->pipeline_opt_step_adamw_f32;
  5838. }
  5839. return nullptr;
  5840. case GGML_OP_LEAKY_RELU:
  5841. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5842. return ctx->device->pipeline_leaky_relu_f32;
  5843. }
  5844. return nullptr;
  5845. case GGML_OP_CONV_2D_DW:
  5846. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5847. if (ggml_is_contiguous(src1)) {
  5848. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  5849. } else if (ggml_is_contiguous_channels(src1)) {
  5850. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  5851. }
  5852. }
  5853. return nullptr;
  5854. default:
  5855. return nullptr;
  5856. }
  5857. GGML_UNUSED(src2);
  5858. }
  5859. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  5860. switch (op) {
  5861. case GGML_OP_CPY:
  5862. case GGML_OP_GET_ROWS:
  5863. case GGML_OP_ADD:
  5864. case GGML_OP_SUB:
  5865. case GGML_OP_MUL:
  5866. case GGML_OP_DIV:
  5867. case GGML_OP_CONCAT:
  5868. case GGML_OP_UPSCALE:
  5869. case GGML_OP_SQR:
  5870. case GGML_OP_SIN:
  5871. case GGML_OP_COS:
  5872. case GGML_OP_CLAMP:
  5873. case GGML_OP_PAD:
  5874. case GGML_OP_REPEAT:
  5875. case GGML_OP_REPEAT_BACK:
  5876. case GGML_OP_ROPE:
  5877. case GGML_OP_RMS_NORM:
  5878. case GGML_OP_CONV_2D_DW:
  5879. case GGML_OP_IM2COL:
  5880. case GGML_OP_SET_ROWS:
  5881. return true;
  5882. default:
  5883. return false;
  5884. }
  5885. }
  5886. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  5887. {
  5888. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  5889. }
  5890. template <typename T> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, T &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5891. GGML_UNUSED(p);
  5892. GGML_UNUSED(src0);
  5893. GGML_UNUSED(src1);
  5894. GGML_UNUSED(src2);
  5895. GGML_UNUSED(dst);
  5896. static_assert(!std::is_const<T>::value, "unexpected type");
  5897. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  5898. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  5899. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  5900. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  5901. }
  5902. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_unary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5903. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5904. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5905. p.misalign_offsets = (a_offset << 16) | d_offset;
  5906. GGML_UNUSED(src1);
  5907. GGML_UNUSED(src2);
  5908. }
  5909. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_binary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5910. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5911. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  5912. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5913. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  5914. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  5915. GGML_UNUSED(src2);
  5916. }
  5917. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_upscale_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5918. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5919. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5920. p.a_offset = a_offset;
  5921. p.d_offset = d_offset;
  5922. GGML_UNUSED(src1);
  5923. GGML_UNUSED(src2);
  5924. }
  5925. template<typename PC>
  5926. static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op, PC&& pc, bool dryrun = false) {
  5927. 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];
  5928. if (src1 != nullptr) {
  5929. 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];
  5930. }
  5931. if (src2 != nullptr) {
  5932. 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];
  5933. }
  5934. 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];
  5935. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  5936. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  5937. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  5938. GGML_ASSERT(dst->buffer != nullptr);
  5939. const uint64_t ne00 = src0->ne[0];
  5940. const uint64_t ne01 = src0->ne[1];
  5941. const uint64_t ne02 = src0->ne[2];
  5942. const uint64_t ne03 = src0->ne[3];
  5943. const uint64_t ne0 = ne00 * ne01;
  5944. const bool use_src1 = src1 != nullptr;
  5945. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  5946. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  5947. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  5948. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  5949. const uint64_t ne1 = ne10 * ne11;
  5950. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  5951. const bool use_src2 = src2 != nullptr;
  5952. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  5953. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  5954. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  5955. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  5956. const uint64_t ne2 = ne20 * ne21;
  5957. const uint64_t ned0 = dst->ne[0];
  5958. const uint64_t ned1 = dst->ne[1];
  5959. const uint64_t ned2 = dst->ne[2];
  5960. const uint64_t ned3 = dst->ne[3];
  5961. const uint64_t ned = ned0 * ned1;
  5962. init_pushconst_fastdiv(pc);
  5963. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  5964. if (pipeline == nullptr) {
  5965. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  5966. if (src1 != nullptr) {
  5967. std::cerr << " and " << ggml_type_name(src1->type);
  5968. }
  5969. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  5970. GGML_ABORT("fatal error");
  5971. }
  5972. if (dryrun) {
  5973. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5974. return;
  5975. }
  5976. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  5977. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5978. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5979. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  5980. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  5981. vk_buffer d_X = nullptr;
  5982. size_t x_buf_offset = 0;
  5983. vk_buffer d_Y = nullptr;
  5984. size_t y_buf_offset = 0;
  5985. vk_buffer d_Z = nullptr;
  5986. size_t z_buf_offset = 0;
  5987. bool src0_uma = false;
  5988. bool src1_uma = false;
  5989. bool src2_uma = false;
  5990. if (ctx->device->uma) {
  5991. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  5992. src0_uma = d_X != nullptr;
  5993. if (use_src1) {
  5994. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  5995. src1_uma = d_Y != nullptr;
  5996. }
  5997. if (use_src2) {
  5998. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  5999. src2_uma = d_Z != nullptr;
  6000. }
  6001. }
  6002. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  6003. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  6004. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  6005. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  6006. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  6007. // Workaround for tiny tensor inputs on ROPE
  6008. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  6009. y_sz = VK_WHOLE_SIZE;
  6010. }
  6011. GGML_ASSERT(d_D != nullptr);
  6012. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  6013. if(!src0_uma) {
  6014. d_X = src0_buf_ctx->dev_buffer;
  6015. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  6016. GGML_ASSERT(d_X != nullptr);
  6017. }
  6018. if (use_src1 && !src1_uma) {
  6019. d_Y = src1_buf_ctx->dev_buffer;
  6020. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  6021. GGML_ASSERT(d_Y != nullptr);
  6022. }
  6023. if (use_src2 && !src2_uma) {
  6024. d_Z = src2_buf_ctx->dev_buffer;
  6025. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  6026. GGML_ASSERT(d_Z != nullptr);
  6027. }
  6028. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  6029. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  6030. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6031. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6032. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6033. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  6034. if (op_supports_incontiguous) {
  6035. x_sz = ggml_nbytes(src0);
  6036. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  6037. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  6038. d_sz = ggml_nbytes(dst);
  6039. if (x_buf_offset + x_sz >= d_X->size) {
  6040. x_sz = VK_WHOLE_SIZE;
  6041. }
  6042. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  6043. y_sz = VK_WHOLE_SIZE;
  6044. }
  6045. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  6046. z_sz = VK_WHOLE_SIZE;
  6047. }
  6048. if (d_buf_offset + d_sz >= d_D->size) {
  6049. d_sz = VK_WHOLE_SIZE;
  6050. }
  6051. }
  6052. std::array<uint32_t, 3> elements;
  6053. // Single call if dimension 2 is contiguous
  6054. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  6055. switch (op) {
  6056. case GGML_OP_NORM:
  6057. case GGML_OP_RMS_NORM_BACK:
  6058. case GGML_OP_L2_NORM:
  6059. case GGML_OP_SOFT_MAX:
  6060. case GGML_OP_SOFT_MAX_BACK:
  6061. case GGML_OP_SUM_ROWS:
  6062. case GGML_OP_ARGMAX:
  6063. {
  6064. const uint32_t nr = ggml_nrows(src0);
  6065. if (nr > 262144) {
  6066. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  6067. } else if (nr > 512) {
  6068. elements = { 512, CEIL_DIV(nr, 512), 1 };
  6069. } else {
  6070. elements = { nr, 1, 1 };
  6071. }
  6072. } break;
  6073. case GGML_OP_RMS_NORM:
  6074. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  6075. break;
  6076. case GGML_OP_SUM:
  6077. // We use GGML_OP_SUM_ROWS with 1 row.
  6078. elements = { 1, 1, 1 };
  6079. break;
  6080. case GGML_OP_GROUP_NORM:
  6081. {
  6082. const uint32_t num_groups = dst->op_params[0];
  6083. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  6084. } break;
  6085. case GGML_OP_DIAG_MASK_INF:
  6086. case GGML_OP_ROPE:
  6087. case GGML_OP_ROPE_BACK:
  6088. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6089. break;
  6090. case GGML_OP_GET_ROWS:
  6091. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6092. break;
  6093. case GGML_OP_ARGSORT:
  6094. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6095. break;
  6096. case GGML_OP_IM2COL:
  6097. {
  6098. const bool is_2D = dst->op_params[6] == 1;
  6099. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6100. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6101. const uint32_t KW = src0->ne[0];
  6102. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6103. const uint32_t OW = dst->ne[1];
  6104. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6105. elements = { OW * KW * KH, OH, batch * IC };
  6106. } break;
  6107. case GGML_OP_TIMESTEP_EMBEDDING:
  6108. {
  6109. const uint32_t dim = dst->op_params[0];
  6110. uint32_t half_ceil = (dim + 1) / 2;
  6111. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6112. } break;
  6113. case GGML_OP_CONV_TRANSPOSE_1D:
  6114. {
  6115. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6116. } break;
  6117. case GGML_OP_POOL_2D:
  6118. {
  6119. const uint32_t N = dst->ne[3];
  6120. const uint32_t OC = dst->ne[2];
  6121. const uint32_t OH = dst->ne[1];
  6122. const uint32_t OW = dst->ne[0];
  6123. elements = { N * OC * OH * OW, 1, 1};
  6124. } break;
  6125. case GGML_OP_ADD:
  6126. case GGML_OP_SUB:
  6127. case GGML_OP_DIV:
  6128. case GGML_OP_MUL:
  6129. case GGML_OP_SCALE:
  6130. case GGML_OP_SQR:
  6131. case GGML_OP_SIN:
  6132. case GGML_OP_COS:
  6133. case GGML_OP_CLAMP:
  6134. case GGML_OP_PAD:
  6135. case GGML_OP_ROLL:
  6136. case GGML_OP_REPEAT:
  6137. case GGML_OP_REPEAT_BACK:
  6138. case GGML_OP_CPY:
  6139. case GGML_OP_CONCAT:
  6140. case GGML_OP_UPSCALE:
  6141. case GGML_OP_UNARY:
  6142. case GGML_OP_GLU:
  6143. case GGML_OP_CONV_2D_DW:
  6144. {
  6145. uint32_t ne = ggml_nelements(dst);
  6146. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6147. // Convert from number of logical elements to 2- or 4-byte units.
  6148. ne /= ggml_blck_size(src0->type);
  6149. if ((ggml_type_size(src0->type) % 4) == 0) {
  6150. ne *= ggml_type_size(src0->type) / 4;
  6151. } else {
  6152. ne *= ggml_type_size(src0->type) / 2;
  6153. }
  6154. }
  6155. // copy_to_quant has block size of 32, and each thread does QUANT_K elements.
  6156. // Splitting into 512x512xZ wouldn't work well since each workgroup does 1024 elements.
  6157. // So divide by block size here before splitting into 512x512 groups.
  6158. if (op == GGML_OP_CPY && !ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6159. ne = CEIL_DIV(ne, ggml_blck_size(dst->type));
  6160. }
  6161. if (ne > 262144) {
  6162. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6163. } else if (ne > 512) {
  6164. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6165. } else {
  6166. elements = { ne, 1, 1 };
  6167. }
  6168. } break;
  6169. case GGML_OP_SET_ROWS:
  6170. {
  6171. uint32_t ne = ggml_nelements(src0);
  6172. if (ggml_is_quantized(dst->type)) {
  6173. // quants run 32 threads each doing QUANT_K elements
  6174. ne = CEIL_DIV(ne, 32 * ggml_blck_size(dst->type));
  6175. } else {
  6176. // scalar types do one element per thread, running 512 threads
  6177. ne = CEIL_DIV(ne, 512);
  6178. }
  6179. if (ne > 262144) {
  6180. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6181. } else if (ne > 512) {
  6182. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6183. } else {
  6184. elements = { ne, 1, 1 };
  6185. }
  6186. }
  6187. break;
  6188. default:
  6189. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  6190. break;
  6191. }
  6192. if (!op_supports_incontiguous) {
  6193. if (x_sz != VK_WHOLE_SIZE) {
  6194. x_sz *= ne02 * ne03;
  6195. }
  6196. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  6197. y_sz *= ne12 * ne13;
  6198. }
  6199. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  6200. z_sz *= ne22 * ne23;
  6201. }
  6202. if (d_sz != VK_WHOLE_SIZE) {
  6203. d_sz *= ned2 * ned3;
  6204. }
  6205. }
  6206. if (op == GGML_OP_SOFT_MAX || op == GGML_OP_GLU) {
  6207. // Empty src1 is possible in soft_max, but the shader needs a buffer
  6208. vk_subbuffer subbuf_y;
  6209. if (use_src1) {
  6210. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6211. } else {
  6212. subbuf_y = { d_X, 0, x_sz };
  6213. }
  6214. ggml_vk_sync_buffers(subctx);
  6215. 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);
  6216. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  6217. // Empty src2 is possible in rope, but the shader needs a buffer
  6218. vk_subbuffer subbuf_z;
  6219. if (use_src2) {
  6220. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6221. } else {
  6222. subbuf_z = { d_X, 0, x_sz };
  6223. }
  6224. ggml_vk_sync_buffers(subctx);
  6225. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, pc, elements);
  6226. } else if (op == GGML_OP_IM2COL) {
  6227. // im2col uses only src1 and dst buffers
  6228. ggml_vk_sync_buffers(subctx);
  6229. 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);
  6230. } else if (op == GGML_OP_COUNT_EQUAL) {
  6231. ggml_vk_sync_buffers(subctx);
  6232. // count_equal assumes that destination buffer is initialized with zeroes
  6233. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  6234. ggml_vk_sync_buffers(subctx);
  6235. 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);
  6236. } else if (use_src2) {
  6237. ggml_vk_sync_buffers(subctx);
  6238. 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);
  6239. } else if (use_src1) {
  6240. ggml_vk_sync_buffers(subctx);
  6241. 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);
  6242. } else {
  6243. ggml_vk_sync_buffers(subctx);
  6244. 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);
  6245. }
  6246. }
  6247. static void ggml_vk_get_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6248. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6249. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6250. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6251. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  6252. (uint32_t)ggml_nelements(src0),
  6253. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
  6254. (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
  6255. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  6256. 0,
  6257. 0.0f, 0.0f, 0,
  6258. }, dryrun);
  6259. }
  6260. static void ggml_vk_acc(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6261. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6262. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6263. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6264. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6265. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6266. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6267. int offset = dst->op_params[3] / 4; // offset in bytes
  6268. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  6269. (uint32_t)ggml_nelements(src0),
  6270. (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,
  6271. (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,
  6272. (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,
  6273. 0,
  6274. 0.0f, 0.0f, offset,
  6275. }, dryrun);
  6276. }
  6277. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6278. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6279. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6280. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6281. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  6282. (uint32_t)ggml_nelements(src0),
  6283. (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,
  6284. (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,
  6285. (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,
  6286. 0,
  6287. 0.0f, 0.0f, 0,
  6288. }, dryrun);
  6289. }
  6290. static void ggml_vk_sub(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6291. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6292. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6293. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6294. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  6295. (uint32_t)ggml_nelements(src0),
  6296. (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,
  6297. (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,
  6298. (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,
  6299. 0,
  6300. 0.0f, 0.0f, 0,
  6301. }, dryrun);
  6302. }
  6303. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6304. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6305. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6306. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6307. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  6308. (uint32_t)ggml_nelements(src0),
  6309. (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,
  6310. (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,
  6311. (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,
  6312. 0,
  6313. 0.0f, 0.0f, 0,
  6314. }, dryrun);
  6315. }
  6316. static void ggml_vk_div(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6317. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6318. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6319. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6320. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  6321. (uint32_t)ggml_nelements(src0),
  6322. (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,
  6323. (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,
  6324. (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,
  6325. 0,
  6326. 0.0f, 0.0f, 0,
  6327. }, dryrun);
  6328. }
  6329. static void ggml_vk_op_f32_wkv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_rwkv_wkv6_push_constants&& pc, int version, bool dryrun = false) {
  6330. GGML_ASSERT(version == 6 || version == 7);
  6331. int num_srcs = version == 6 ? 6 : 7;
  6332. for (int i = 0; i < num_srcs; i++) {
  6333. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  6334. }
  6335. GGML_ASSERT(dst->buffer != nullptr);
  6336. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  6337. GGML_ASSERT(pipeline != nullptr);
  6338. if (dryrun) {
  6339. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6340. return;
  6341. }
  6342. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6343. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6344. for (int i = 0; i < num_srcs; i++) {
  6345. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  6346. }
  6347. ggml_vk_sync_buffers(subctx);
  6348. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6349. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6350. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  6351. if (ctx->device->uma) {
  6352. for (int i = 0; i < num_srcs; i++) {
  6353. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  6354. srcs_uma[i] = d_srcs[i] != nullptr;
  6355. }
  6356. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  6357. dst_uma = d_D != nullptr;
  6358. }
  6359. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6360. for (int i = 0; i < num_srcs; i++) {
  6361. src_sizes[i] = ggml_nbytes(dst->src[i]);
  6362. if (!srcs_uma[i]) {
  6363. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  6364. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  6365. }
  6366. }
  6367. const uint64_t dst_size = ggml_nbytes(dst);
  6368. if (!dst_uma) {
  6369. d_D = dst_buf_ctx->dev_buffer;
  6370. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  6371. }
  6372. std::array<uint32_t, 3> elements = {
  6373. (uint32_t)(pc.B * pc.H),
  6374. 1,
  6375. 1
  6376. };
  6377. if (version == 6) {
  6378. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6379. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6380. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6381. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6382. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6383. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6384. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6385. vk_subbuffer{ d_D, dst_offset, dst_size }
  6386. }, pc, elements);
  6387. } else if (version == 7) {
  6388. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6389. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6390. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6391. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6392. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6393. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6394. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6395. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  6396. vk_subbuffer{ d_D, dst_offset, dst_size }
  6397. }, pc, elements);
  6398. } else {
  6399. // shouldn't happen
  6400. GGML_ASSERT(false);
  6401. }
  6402. }
  6403. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6404. const size_t seq_length = dst->src[0]->ne[2];
  6405. const size_t n_embed = dst->ne[0];
  6406. const size_t n_heads = dst->src[0]->ne[1];
  6407. const size_t n_seqs = dst->src[5]->ne[1];
  6408. ggml_vk_op_f32_wkv(
  6409. ctx, subctx, dst,
  6410. {
  6411. (uint32_t)n_seqs,
  6412. (uint32_t)seq_length,
  6413. (uint32_t)n_embed,
  6414. (uint32_t)n_heads,
  6415. },
  6416. 6,
  6417. dryrun
  6418. );
  6419. }
  6420. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6421. const size_t seq_length = dst->src[0]->ne[2];
  6422. const size_t n_embed = dst->ne[0];
  6423. const size_t n_heads = dst->src[0]->ne[1];
  6424. const size_t n_seqs = dst->src[6]->ne[1];
  6425. ggml_vk_op_f32_wkv(
  6426. ctx, subctx, dst,
  6427. {
  6428. (uint32_t)n_seqs,
  6429. (uint32_t)seq_length,
  6430. (uint32_t)n_embed,
  6431. (uint32_t)n_heads,
  6432. },
  6433. 7,
  6434. dryrun
  6435. );
  6436. }
  6437. static void ggml_vk_op_f32_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_push_constants&& pc, bool dryrun = false) {
  6438. const ggml_tensor * x = dst->src[0];
  6439. const ggml_tensor * g = dst->src[1];
  6440. const ggml_tensor * gm = dst->src[2];
  6441. const ggml_tensor * gv = dst->src[3];
  6442. const ggml_tensor * p = dst->src[4];
  6443. GGML_ASSERT(x->type == GGML_TYPE_F32);
  6444. GGML_ASSERT(g->type == GGML_TYPE_F32);
  6445. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  6446. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  6447. GGML_ASSERT(p->type == GGML_TYPE_F32);
  6448. GGML_ASSERT(dst->buffer != nullptr);
  6449. GGML_ASSERT(ggml_is_contiguous(x));
  6450. GGML_ASSERT(ggml_is_contiguous(g));
  6451. GGML_ASSERT(ggml_is_contiguous(gm));
  6452. GGML_ASSERT(ggml_is_contiguous(gv));
  6453. GGML_ASSERT(ggml_is_contiguous(p));
  6454. GGML_ASSERT(ggml_are_same_shape(x, g));
  6455. GGML_ASSERT(ggml_are_same_shape(x, gm));
  6456. GGML_ASSERT(ggml_are_same_shape(x, gv));
  6457. GGML_ASSERT(ggml_nelements(p) == 7);
  6458. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  6459. GGML_ASSERT(pipeline != nullptr);
  6460. if (dryrun) {
  6461. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6462. return;
  6463. }
  6464. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  6465. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  6466. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  6467. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  6468. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  6469. ggml_vk_sync_buffers(subctx);
  6470. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  6471. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  6472. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  6473. if (ctx->device->uma) {
  6474. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  6475. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  6476. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  6477. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  6478. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  6479. X_uma = d_X != nullptr;
  6480. G_uma = d_G != nullptr;
  6481. GM_uma = d_GM != nullptr;
  6482. GV_uma = d_GV != nullptr;
  6483. P_uma = d_P != nullptr;
  6484. }
  6485. if (!X_uma) {
  6486. d_X = x_buf_ctx->dev_buffer;
  6487. x_offset = vk_tensor_offset(x) + x->view_offs;
  6488. }
  6489. if (!G_uma) {
  6490. d_G = g_buf_ctx->dev_buffer;
  6491. g_offset = vk_tensor_offset(g) + g->view_offs;
  6492. }
  6493. if (!GM_uma) {
  6494. d_GM = gm_buf_ctx->dev_buffer;
  6495. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  6496. }
  6497. if (!GV_uma) {
  6498. d_GV = gv_buf_ctx->dev_buffer;
  6499. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  6500. }
  6501. if (!P_uma) {
  6502. d_P = p_buf_ctx->dev_buffer;
  6503. p_offset = vk_tensor_offset(p) + p->view_offs;
  6504. }
  6505. const uint64_t x_size = ggml_nbytes(x);
  6506. const uint64_t g_size = ggml_nbytes(g);
  6507. const uint64_t gm_size = ggml_nbytes(gm);
  6508. const uint64_t gv_size = ggml_nbytes(gv);
  6509. const uint64_t p_size = ggml_nbytes(p);
  6510. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  6511. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6512. vk_subbuffer{ d_X, x_offset, x_size },
  6513. vk_subbuffer{ d_G, g_offset, g_size },
  6514. vk_subbuffer{ d_GM, gm_offset, gm_size },
  6515. vk_subbuffer{ d_GV, gv_offset, gv_size },
  6516. vk_subbuffer{ d_P, p_offset, p_size },
  6517. }, pc, elements);
  6518. }
  6519. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6520. const size_t n = ggml_nelements(dst->src[0]);
  6521. ggml_vk_op_f32_opt_step_adamw(
  6522. ctx, subctx, dst,
  6523. { (uint32_t)n, 0, 0.0f, 0.0f },
  6524. dryrun
  6525. );
  6526. }
  6527. static void ggml_vk_concat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6528. int * op_params = (int *)dst->op_params;
  6529. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6530. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6531. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6532. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  6533. (uint32_t)ggml_nelements(dst),
  6534. (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,
  6535. (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,
  6536. (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,
  6537. 0,
  6538. 0.0f, 0.0f, op_params[0],
  6539. }, dryrun);
  6540. }
  6541. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6542. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6543. const uint32_t mode = (uint32_t)ggml_get_op_params_i32(dst, 0);
  6544. float sf0 = (float)dst->ne[0] / src0->ne[0];
  6545. float sf1 = (float)dst->ne[1] / src0->ne[1];
  6546. float sf2 = (float)dst->ne[2] / src0->ne[2];
  6547. float sf3 = (float)dst->ne[3] / src0->ne[3];
  6548. if (mode & GGML_SCALE_FLAG_ALIGN_CORNERS) {
  6549. sf0 = (float)(dst->ne[0] - 1) / (src0->ne[0] - 1);
  6550. sf1 = (float)(dst->ne[1] - 1) / (src0->ne[1] - 1);
  6551. }
  6552. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  6553. (uint32_t)ggml_nelements(dst), 0, 0,
  6554. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1],
  6555. (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,
  6556. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  6557. sf0, sf1, sf2, sf3,
  6558. }, dryrun);
  6559. }
  6560. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6561. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6562. p.param1 = ggml_get_op_params_f32(dst, 0);
  6563. p.param2 = ggml_get_op_params_f32(dst, 1);
  6564. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, std::move(p), dryrun);
  6565. }
  6566. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6567. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6568. }
  6569. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6570. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6571. }
  6572. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6573. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, vk_op_unary_push_constants_init(src0, dst), dryrun);
  6574. }
  6575. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6576. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6577. p.param1 = ggml_get_op_params_f32(dst, 0);
  6578. p.param2 = ggml_get_op_params_f32(dst, 1);
  6579. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, std::move(p), dryrun);
  6580. }
  6581. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6582. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6583. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, std::move(p), dryrun);
  6584. }
  6585. static void ggml_vk_roll(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6586. const int32_t s0 = ggml_get_op_params_i32(dst, 0);
  6587. const int32_t s1 = ggml_get_op_params_i32(dst, 1);
  6588. const int32_t s2 = ggml_get_op_params_i32(dst, 2);
  6589. const int32_t s3 = ggml_get_op_params_i32(dst, 3);
  6590. const uint32_t s01_packed = ((s0 + 0x8000) << 16) | (s1 + 0x8000);
  6591. const uint32_t s23_packed = ((s2 + 0x8000) << 16) | (s3 + 0x8000);
  6592. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst);
  6593. memcpy(&p.param1, &s01_packed, sizeof(float));
  6594. memcpy(&p.param2, &s23_packed, sizeof(float));
  6595. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ROLL, std::move(p), dryrun);
  6596. }
  6597. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6598. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6599. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, std::move(p), dryrun);
  6600. }
  6601. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6602. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ggml_nelements(dst));
  6603. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, std::move(p), dryrun);
  6604. }
  6605. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6606. uint32_t ne = (uint32_t)ggml_nelements(src0);
  6607. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6608. // Convert from number of logical elements to 2- or 4-byte units.
  6609. ne /= ggml_blck_size(src0->type);
  6610. if ((ggml_type_size(src0->type) % 4) == 0) {
  6611. ne *= ggml_type_size(src0->type) / 4;
  6612. } else {
  6613. ne *= ggml_type_size(src0->type) / 2;
  6614. }
  6615. }
  6616. vk_op_unary_push_constants p = vk_op_unary_push_constants_init(src0, dst, ne);
  6617. ggml_vk_op_f32(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, std::move(p), dryrun);
  6618. }
  6619. static void ggml_vk_set_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6620. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6621. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6622. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6623. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SET_ROWS, {
  6624. (uint32_t)ggml_nelements(src0),
  6625. (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,
  6626. (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,
  6627. (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,
  6628. 0,
  6629. 0.0f, 0.0f, 0,
  6630. }, dryrun);
  6631. }
  6632. static void ggml_vk_silu_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6633. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SILU_BACK, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  6634. }
  6635. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6636. float * op_params = (float *)dst->op_params;
  6637. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  6638. }
  6639. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6640. const int * int_op_params = (const int *)dst->op_params;
  6641. const float * float_op_params = (const float *)dst->op_params;
  6642. const uint32_t num_groups = int_op_params[0];
  6643. const float eps = float_op_params[1];
  6644. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6645. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_GROUP_NORM, { group_size, 0, eps, 0.0f }, dryrun);
  6646. }
  6647. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, float * op_params, bool dryrun = false) {
  6648. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6649. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6650. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6651. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  6652. (uint32_t)ggml_nelements(src0),
  6653. (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,
  6654. (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,
  6655. (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,
  6656. 0,
  6657. op_params[0], 0.0f, 0,
  6658. }, dryrun);
  6659. }
  6660. static void ggml_vk_rms_norm_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6661. float * op_params = (float *)dst->op_params;
  6662. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  6663. }
  6664. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6665. float * op_params = (float *)dst->op_params;
  6666. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_L2_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  6667. }
  6668. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6669. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UNARY, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  6670. }
  6671. static void ggml_vk_glu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6672. const bool swapped = (bool)dst->op_params[1];
  6673. const bool split = src1 != nullptr;
  6674. GGML_ASSERT(ggml_is_contiguous(src0));
  6675. if (!split) {
  6676. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  6677. } else {
  6678. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  6679. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  6680. GGML_ASSERT(src0->type == src1->type);
  6681. }
  6682. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  6683. ggml_vk_op_f32<vk_op_glu_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GLU, { (uint32_t)ggml_nelements(dst), (uint32_t)src0->ne[0], (uint32_t)dst->ne[0], mode }, dryrun);
  6684. }
  6685. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6686. int32_t * op_params = (int32_t *)dst->op_params;
  6687. ggml_vk_op_f32<vk_op_diag_mask_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_DIAG_MASK_INF, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0] }, dryrun);
  6688. }
  6689. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6690. float * op_params = (float *)dst->op_params;
  6691. float scale = op_params[0];
  6692. float max_bias = op_params[1];
  6693. const uint32_t ncols = (uint32_t)src0->ne[0];
  6694. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  6695. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  6696. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  6697. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  6698. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  6699. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  6700. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  6701. const uint32_t n_head_kv = src0->ne[2];
  6702. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6703. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6704. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6705. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  6706. ncols,
  6707. src1 != nullptr ? nrows_y : (uint32_t)0,
  6708. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  6709. ne12, ne13,
  6710. nb11, nb12, nb13,
  6711. scale, max_bias,
  6712. m0, m1,
  6713. n_head_log2,
  6714. nrows_x,
  6715. }, dryrun);
  6716. }
  6717. static void ggml_vk_soft_max_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6718. float * op_params = (float *)dst->op_params;
  6719. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], op_params[1] }, dryrun);
  6720. }
  6721. static void ggml_vk_rope(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool backprop, bool dryrun = false) {
  6722. const int n_dims = ((int32_t *) dst->op_params)[1];
  6723. const int mode = ((int32_t *) dst->op_params)[2];
  6724. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  6725. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  6726. const float freq_base = ((float *) dst->op_params)[5];
  6727. const float freq_scale = ((float *) dst->op_params)[6];
  6728. const float ext_factor = ((float *) dst->op_params)[7];
  6729. const float attn_factor = ((float *) dst->op_params)[8];
  6730. const float beta_fast = ((float *) dst->op_params)[9];
  6731. const float beta_slow = ((float *) dst->op_params)[10];
  6732. int sections[4] {};
  6733. if (mode & GGML_ROPE_TYPE_MROPE) {
  6734. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  6735. }
  6736. float corr_dims[2];
  6737. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  6738. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6739. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  6740. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  6741. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  6742. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  6743. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  6744. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  6745. sections[0], sections[1], sections[2], sections[3], backprop
  6746. }, dryrun);
  6747. }
  6748. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6749. int32_t * op_params = (int32_t *)dst->op_params;
  6750. uint32_t ncols = src0->ne[0];
  6751. uint32_t ncols_pad = 1;
  6752. while (ncols_pad < ncols) {
  6753. ncols_pad *= 2;
  6754. }
  6755. GGML_ASSERT(ncols_pad <= 1024);
  6756. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  6757. ncols,
  6758. ncols_pad,
  6759. op_params[0],
  6760. }, dryrun);
  6761. }
  6762. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6763. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  6764. }
  6765. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6766. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6767. }
  6768. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6769. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  6770. }
  6771. static void ggml_vk_count_equal(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6772. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_COUNT_EQUAL, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  6773. }
  6774. static void ggml_vk_im2col(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6775. const int32_t s0 = dst->op_params[0];
  6776. const int32_t s1 = dst->op_params[1];
  6777. const int32_t p0 = dst->op_params[2];
  6778. const int32_t p1 = dst->op_params[3];
  6779. const int32_t d0 = dst->op_params[4];
  6780. const int32_t d1 = dst->op_params[5];
  6781. const bool is_2D = dst->op_params[6] == 1;
  6782. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6783. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  6784. const uint32_t IW = src1->ne[0];
  6785. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6786. const uint32_t KW = src0->ne[0];
  6787. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6788. const uint32_t OW = dst->ne[1];
  6789. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6790. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  6791. const uint32_t pelements = OW * KW * KH;
  6792. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  6793. batch_offset, offset_delta,
  6794. IC, IW, IH, OW, OH, KW, KH,
  6795. pelements,
  6796. IC * KH * KW,
  6797. s0, s1, p0, p1, d0, d1,
  6798. }, dryrun);
  6799. }
  6800. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6801. const uint32_t dim = dst->op_params[0];
  6802. const uint32_t max_period = dst->op_params[1];
  6803. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  6804. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  6805. nb1, dim, max_period,
  6806. }, dryrun);
  6807. }
  6808. static void ggml_vk_conv_transpose_1d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6809. // src0: (K, Cout, Cin, 1) -- kernel
  6810. // src1: (L, Cin, 1, 1) -- input
  6811. // dst: (*, Cout, 1, 1)
  6812. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6813. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6814. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6815. GGML_TENSOR_BINARY_OP_LOCALS
  6816. GGML_ASSERT(nb00 == sizeof(float));
  6817. GGML_ASSERT(nb10 == sizeof(float));
  6818. const int32_t s0 = dst->op_params[0];
  6819. vk_op_conv_transpose_1d_push_constants p{};
  6820. p.Cout = static_cast<uint32_t>(ne01);
  6821. p.Cin = static_cast<uint32_t>(ne02);
  6822. p.K = static_cast<uint32_t>(ne00);
  6823. p.L = static_cast<uint32_t>(ne10);
  6824. p.KL = static_cast<uint32_t>(ne0);
  6825. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  6826. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  6827. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  6828. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  6829. p.s0 = static_cast<uint32_t>(s0);
  6830. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  6831. }
  6832. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6833. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  6834. const int32_t k1 = dst->op_params[1];
  6835. const int32_t k0 = dst->op_params[2];
  6836. const int32_t s1 = dst->op_params[3];
  6837. const int32_t s0 = dst->op_params[4];
  6838. const int32_t p1 = dst->op_params[5];
  6839. const int32_t p0 = dst->op_params[6];
  6840. const uint32_t IH = src0->ne[1];
  6841. const uint32_t IW = src0->ne[0];
  6842. const uint32_t N = dst->ne[3];
  6843. const uint32_t OC = dst->ne[2];
  6844. const uint32_t OH = dst->ne[1];
  6845. const uint32_t OW = dst->ne[0];
  6846. const uint32_t parallel_elements = N * OC * OH * OW;
  6847. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  6848. IW, IH, OW, OH, OC,
  6849. parallel_elements,
  6850. op,
  6851. k0, k1, s0, s1, p0, p1,
  6852. }, dryrun);
  6853. }
  6854. static void ggml_vk_conv_2d_dw(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  6855. vk_op_conv2d_dw_push_constants p{};
  6856. p.ne = ggml_nelements(dst);
  6857. p.channels = dst->ne[2];
  6858. p.batches = dst->ne[3];
  6859. p.dst_w = dst->ne[0];
  6860. p.dst_h = dst->ne[1];
  6861. p.src_w = src1->ne[0];
  6862. p.src_h = src1->ne[1];
  6863. p.knl_w = src0->ne[0];
  6864. p.knl_h = src0->ne[1];
  6865. p.stride_x = dst->op_params[0];
  6866. p.stride_y = dst->op_params[1];
  6867. p.pad_x = dst->op_params[2];
  6868. p.pad_y = dst->op_params[3];
  6869. p.dilation_x = dst->op_params[4];
  6870. p.dilation_y = dst->op_params[5];
  6871. GGML_ASSERT(src0->ne[3] == p.channels);
  6872. GGML_ASSERT(src1->ne[3] == p.batches);
  6873. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  6874. }
  6875. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6876. const float * op_params = (const float *)dst->op_params;
  6877. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_LEAKY_RELU, { (uint32_t)ggml_nelements(src0), 0, op_params[0], 0.0f }, dryrun);
  6878. }
  6879. #ifdef GGML_VULKAN_RUN_TESTS
  6880. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  6881. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  6882. return;
  6883. }
  6884. i0 = std::max(i0, 5);
  6885. i1 = std::max(i1, 5);
  6886. i2 = std::max(i2, 0);
  6887. fprintf(stderr, " ");
  6888. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6889. fprintf(stderr, "%7d ", idx1);
  6890. }
  6891. fprintf(stderr, "\n");
  6892. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6893. fprintf(stderr, "%7d: ", idx0);
  6894. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6895. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  6896. float val;
  6897. if (type == GGML_TYPE_F32) {
  6898. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  6899. } else if (type == GGML_TYPE_F16) {
  6900. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  6901. } else {
  6902. GGML_ABORT("fatal error");
  6903. }
  6904. fprintf(stderr, "% 7.2f ", val);
  6905. } else {
  6906. fprintf(stderr, " ");
  6907. }
  6908. }
  6909. fprintf(stderr, "\n");
  6910. }
  6911. }
  6912. template <typename X_TYPE, typename Y_TYPE>
  6913. 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) {
  6914. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  6915. const size_t x_ne = m * k * batch;
  6916. const size_t y_ne = k * n * batch;
  6917. const size_t d_ne = m * n * batch;
  6918. vk_pipeline p;
  6919. std::string shname;
  6920. if (shader_size == 0) {
  6921. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6922. p = ctx->device->pipeline_matmul_f32->a_s;
  6923. shname = "F32_ALIGNED_S";
  6924. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6925. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  6926. shname = "F32_F16_ALIGNED_S";
  6927. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6928. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  6929. shname = "F16_F32_ALIGNED_S";
  6930. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6931. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  6932. shname = "F16_ALIGNED_S";
  6933. } else {
  6934. GGML_ABORT("fatal error");
  6935. }
  6936. } else if (shader_size == 1) {
  6937. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6938. p = ctx->device->pipeline_matmul_f32->a_m;
  6939. shname = "F32_ALIGNED_M";
  6940. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6941. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  6942. shname = "F32_F16_ALIGNED_M";
  6943. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6944. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  6945. shname = "F16_F32_ALIGNED_M";
  6946. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6947. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  6948. shname = "F16_ALIGNED_M";
  6949. } else {
  6950. GGML_ABORT("fatal error");
  6951. }
  6952. } else if (shader_size == 2) {
  6953. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6954. p = ctx->device->pipeline_matmul_f32->a_l;
  6955. shname = "F32_ALIGNED_L";
  6956. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6957. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  6958. shname = "F32_F16_ALIGNED_L";
  6959. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6960. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  6961. shname = "F16_F32_ALIGNED_L";
  6962. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6963. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  6964. shname = "F16_ALIGNED_L";
  6965. } else {
  6966. GGML_ABORT("fatal error");
  6967. }
  6968. } else {
  6969. GGML_ASSERT(0);
  6970. }
  6971. const size_t kpad = ggml_vk_align_size(k, p->align);
  6972. if (k != kpad) {
  6973. if (shader_size == 0) {
  6974. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6975. p = ctx->device->pipeline_matmul_f32->s;
  6976. shname = "F32_S";
  6977. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6978. p = ctx->device->pipeline_matmul_f32_f16->s;
  6979. shname = "F32_F16_S";
  6980. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6981. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  6982. shname = "F16_F32_S";
  6983. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6984. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  6985. shname = "F16_S";
  6986. }
  6987. } else if (shader_size == 1) {
  6988. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6989. p = ctx->device->pipeline_matmul_f32->m;
  6990. shname = "F32_M";
  6991. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6992. p = ctx->device->pipeline_matmul_f32_f16->m;
  6993. shname = "F32_F16_M";
  6994. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6995. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  6996. shname = "F16_F32_M";
  6997. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6998. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  6999. shname = "F16_M";
  7000. }
  7001. } else if (shader_size == 2) {
  7002. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7003. p = ctx->device->pipeline_matmul_f32->l;
  7004. shname = "F32_L";
  7005. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7006. p = ctx->device->pipeline_matmul_f32_f16->l;
  7007. shname = "F32_F16_L";
  7008. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  7009. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  7010. shname = "F16_F32_L";
  7011. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7012. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  7013. shname = "F16_L";
  7014. }
  7015. }
  7016. }
  7017. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7018. if (split_k > 1) {
  7019. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7020. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7021. // Resize buffer
  7022. if (ctx->prealloc_split_k != nullptr) {
  7023. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7024. }
  7025. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7026. }
  7027. }
  7028. if (ctx->device->need_compiles) {
  7029. ggml_vk_load_shaders(ctx->device);
  7030. }
  7031. ggml_pipeline_allocate_descriptor_sets(ctx);
  7032. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7033. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7034. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7035. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  7036. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  7037. float* d = (float *) malloc(sizeof(float) * d_ne);
  7038. for (size_t i = 0; i < x_ne; i++) {
  7039. if (std::is_same<float, X_TYPE>()) {
  7040. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7041. // x[i] = 1.0f;
  7042. // x[i] = i + 1;
  7043. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7044. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7045. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7046. // x[i] = ggml_fp32_to_fp16(1.0f);
  7047. // x[i] = ggml_fp32_to_fp16(i + 1);
  7048. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7049. } else {
  7050. GGML_ABORT("fatal error");
  7051. }
  7052. }
  7053. for (size_t i = 0; i < y_ne; i++) {
  7054. if (std::is_same<float, Y_TYPE>()) {
  7055. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7056. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7057. // y[i] = i + 1;
  7058. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7059. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  7060. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  7061. // y[i] = ggml_fp32_to_fp16(i + 1);
  7062. } else {
  7063. GGML_ABORT("fatal error");
  7064. }
  7065. }
  7066. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  7067. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  7068. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7069. ggml_vk_ctx_begin(ctx->device, subctx);
  7070. for (size_t i = 0; i < num_it; i++) {
  7071. ggml_vk_matmul(
  7072. ctx, subctx, p, ggml_vk_subbuffer(d_X), ggml_vk_subbuffer(d_Y), ggml_vk_subbuffer(d_D), ggml_vk_subbuffer(ctx->prealloc_split_k),
  7073. m, n, k,
  7074. k, k, m, k*m, k*n, m*n,
  7075. split_k, batch, batch, batch, 1, 1, n
  7076. );
  7077. }
  7078. ggml_vk_ctx_end(subctx);
  7079. auto begin = std::chrono::high_resolution_clock::now();
  7080. ggml_vk_submit(subctx, ctx->fence);
  7081. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  7082. ctx->device->device.resetFences({ ctx->fence });
  7083. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7084. auto end = std::chrono::high_resolution_clock::now();
  7085. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7086. // copy dst to host
  7087. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  7088. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  7089. ggml_init_params iparams = {
  7090. /*.mem_size =*/ 1024*1024*1024,
  7091. /*.mem_buffer =*/ NULL,
  7092. /*.no_alloc =*/ true,
  7093. };
  7094. ggml_context * ggml_ctx = ggml_init(iparams);
  7095. ggml_type src0_type;
  7096. ggml_type src1_type;
  7097. if (std::is_same<float, X_TYPE>()) {
  7098. src0_type = GGML_TYPE_F32;
  7099. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7100. src0_type = GGML_TYPE_F16;
  7101. } else {
  7102. GGML_ABORT("fatal error");
  7103. }
  7104. if (std::is_same<float, Y_TYPE>()) {
  7105. src1_type = GGML_TYPE_F32;
  7106. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7107. src1_type = GGML_TYPE_F16;
  7108. } else {
  7109. GGML_ABORT("fatal error");
  7110. }
  7111. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  7112. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  7113. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7114. src0_ggml->data = x;
  7115. src1_ggml->data = y;
  7116. tensor_ggml->data = d_chk;
  7117. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7118. ggml_build_forward_expand(cgraph, tensor_ggml);
  7119. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7120. ggml_free(ggml_ctx);
  7121. double avg_err = 0.0;
  7122. int first_err_n = -1;
  7123. int first_err_m = -1;
  7124. int first_err_b = -1;
  7125. for (size_t i = 0; i < m*n*batch; i++) {
  7126. double err = std::fabs(d[i] - d_chk[i]);
  7127. avg_err += err;
  7128. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7129. first_err_b = i / (m * n);
  7130. first_err_n = (i % (m * n)) / m;
  7131. first_err_m = (i % (m * n)) % m;
  7132. }
  7133. }
  7134. avg_err /= m * n;
  7135. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7136. 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;
  7137. if (avg_err > 0.1 || std::isnan(avg_err)) {
  7138. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7139. std::cerr << "Actual result: " << std::endl << std::endl;
  7140. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7141. std::cerr << "Expected result: " << std::endl << std::endl;
  7142. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7143. if (split_k > 1) {
  7144. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7145. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7146. std::cerr << "d_buf0: " << std::endl << std::endl;
  7147. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7148. std::cerr << "d_buf1: " << std::endl << std::endl;
  7149. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7150. std::cerr << "d_buf2: " << std::endl << std::endl;
  7151. 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);
  7152. std::cerr << "d_buf3: " << std::endl << std::endl;
  7153. 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);
  7154. free(split_k_buf);
  7155. }
  7156. }
  7157. free(d_chk);
  7158. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  7159. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  7160. ggml_vk_destroy_buffer(d_X);
  7161. ggml_vk_destroy_buffer(d_Y);
  7162. ggml_vk_destroy_buffer(d_D);
  7163. free(x);
  7164. free(y);
  7165. free(d);
  7166. }
  7167. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  7168. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  7169. return;
  7170. }
  7171. i0 = std::max(i0, 5);
  7172. i1 = std::max(i1, 5);
  7173. i2 = std::max(i2, 0);
  7174. i3 = std::max(i3, 0);
  7175. fprintf(stderr, " ");
  7176. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7177. fprintf(stderr, "%7d ", idx1);
  7178. }
  7179. fprintf(stderr, "\n");
  7180. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7181. fprintf(stderr, "%7d: ", idx0);
  7182. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7183. 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]) {
  7184. float val;
  7185. if (tensor->type == GGML_TYPE_F32) {
  7186. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7187. } else if (tensor->type == GGML_TYPE_F16) {
  7188. 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]));
  7189. } else {
  7190. GGML_ABORT("fatal error");
  7191. }
  7192. fprintf(stderr, "% 7.2f ", val);
  7193. } else {
  7194. fprintf(stderr, " ");
  7195. }
  7196. }
  7197. fprintf(stderr, "\n");
  7198. }
  7199. }
  7200. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  7201. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  7202. }
  7203. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  7204. if (quant == GGML_TYPE_F32) {
  7205. memcpy(to, from, sizeof(float) * ne);
  7206. return;
  7207. }
  7208. const auto * tt = ggml_get_type_traits(quant);
  7209. ggml_to_float_t dequant_fn = tt->to_float;
  7210. dequant_fn(from, to, ne);
  7211. }
  7212. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7213. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  7214. const size_t x_sz = sizeof(float) * ne;
  7215. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  7216. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7217. float * x = (float *) malloc(x_sz);
  7218. void * qx = malloc(qx_sz);
  7219. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7220. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7221. float * x_ref = (float *) malloc(x_sz);
  7222. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  7223. for (size_t i = 0; i < ne; i++) {
  7224. x[i] = rand() / (float)RAND_MAX;
  7225. }
  7226. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  7227. ggml_vk_quantize_data(x, qx, ne, quant);
  7228. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  7229. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7230. if (ctx->device->need_compiles) {
  7231. ggml_vk_load_shaders(ctx->device);
  7232. }
  7233. ggml_pipeline_allocate_descriptor_sets(ctx);
  7234. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7235. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7236. ggml_vk_ctx_begin(ctx->device, subctx);
  7237. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  7238. 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});
  7239. ggml_vk_ctx_end(subctx);
  7240. auto begin = std::chrono::high_resolution_clock::now();
  7241. ggml_vk_submit(subctx, ctx->fence);
  7242. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7243. ctx->device->device.resetFences({ ctx->fence });
  7244. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7245. auto end = std::chrono::high_resolution_clock::now();
  7246. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7247. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  7248. int first_err = -1;
  7249. double avg_err = 0.0;
  7250. for (size_t i = 0; i < ne; i++) {
  7251. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  7252. avg_err += error;
  7253. if (first_err < 0 && error > 0.05) {
  7254. first_err = i;
  7255. }
  7256. }
  7257. avg_err /= ne;
  7258. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  7259. if (avg_err > 0.1) {
  7260. std::cerr << "first_error = " << first_err << std::endl;
  7261. std::cerr << "Actual result: " << std::endl << std::endl;
  7262. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7263. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  7264. }
  7265. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  7266. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7267. std::cerr << x_ref[i] << ", ";
  7268. }
  7269. std::cerr << std::endl;
  7270. }
  7271. ggml_vk_destroy_buffer(x_buf);
  7272. ggml_vk_destroy_buffer(qx_buf);
  7273. free(x);
  7274. free(qx);
  7275. free(x_ref);
  7276. free(x_chk);
  7277. }
  7278. // This does not work without ggml q8_1 quantization support
  7279. //
  7280. // typedef uint16_t ggml_half;
  7281. // typedef uint32_t ggml_half2;
  7282. //
  7283. // #define QK8_1 32
  7284. // typedef struct {
  7285. // union {
  7286. // struct {
  7287. // ggml_half d; // delta
  7288. // ggml_half s; // d * sum(qs[i])
  7289. // } GGML_COMMON_AGGR_S;
  7290. // ggml_half2 ds;
  7291. // } GGML_COMMON_AGGR_U;
  7292. // int8_t qs[QK8_1]; // quants
  7293. // } block_q8_1;
  7294. //
  7295. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7296. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  7297. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  7298. //
  7299. // const size_t x_sz = sizeof(float) * ne;
  7300. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7301. // float * x = (float *) malloc(x_sz);
  7302. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  7303. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  7304. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7305. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7306. //
  7307. // for (size_t i = 0; i < ne; i++) {
  7308. // x[i] = rand() / (float)RAND_MAX;
  7309. // }
  7310. //
  7311. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  7312. //
  7313. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7314. //
  7315. // if (ctx->device->need_compiles) {
  7316. // ggml_vk_load_shaders(ctx->device);
  7317. // }
  7318. //
  7319. // ggml_pipeline_allocate_descriptor_sets(ctx);
  7320. //
  7321. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  7322. //
  7323. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7324. // ggml_vk_ctx_begin(ctx->device, subctx);
  7325. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  7326. // ggml_vk_ctx_end(subctx);
  7327. //
  7328. // auto begin = std::chrono::high_resolution_clock::now();
  7329. //
  7330. // ggml_vk_submit(subctx, ctx->fence);
  7331. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  7332. // ctx->device->device.resetFences({ ctx->fence });
  7333. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  7334. //
  7335. // auto end = std::chrono::high_resolution_clock::now();
  7336. //
  7337. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7338. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  7339. //
  7340. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  7341. //
  7342. // int first_err = -1;
  7343. //
  7344. // for (size_t i = 0; i < ne / 32; i++) {
  7345. // 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));
  7346. //
  7347. // if (first_err < 0 && error > 0.1) {
  7348. // first_err = i;
  7349. // }
  7350. //
  7351. // 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));
  7352. //
  7353. // if (first_err < 0 && error > 0.1) {
  7354. // first_err = i;
  7355. // }
  7356. //
  7357. // for (size_t j = 0; j < 32; j++) {
  7358. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  7359. //
  7360. // if (first_err < 0 && error > 1) {
  7361. // first_err = i;
  7362. // }
  7363. // }
  7364. // }
  7365. //
  7366. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  7367. //
  7368. // if (first_err != -1) {
  7369. // std::cerr << "first_error = " << first_err << std::endl;
  7370. // std::cerr << "Actual result: " << std::endl << std::endl;
  7371. // 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) << " ";
  7372. // for (size_t j = 0; j < 32; j++) {
  7373. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  7374. // }
  7375. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  7376. // 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) << " ";
  7377. // for (size_t j = 0; j < 32; j++) {
  7378. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  7379. // }
  7380. // std::cerr << std::endl;
  7381. // }
  7382. //
  7383. // ggml_vk_destroy_buffer(x_buf);
  7384. // ggml_vk_destroy_buffer(qx_buf);
  7385. //
  7386. // free(x);
  7387. // free(qx);
  7388. // free(qx_res);
  7389. // }
  7390. 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) {
  7391. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  7392. const size_t x_ne = m * k * batch;
  7393. const size_t y_ne = k * n * batch;
  7394. const size_t d_ne = m * n * batch;
  7395. vk_matmul_pipeline2 * pipelines;
  7396. if (mmq) {
  7397. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  7398. } else {
  7399. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  7400. }
  7401. const bool fp16acc = ctx->device->fp16;
  7402. vk_pipeline p;
  7403. std::string shname;
  7404. if (shader_size == 0) {
  7405. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  7406. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  7407. } else if (shader_size == 1) {
  7408. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  7409. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  7410. } else if (shader_size == 2) {
  7411. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  7412. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  7413. } else {
  7414. GGML_ASSERT(0);
  7415. }
  7416. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  7417. if (mmq || k != kpad) {
  7418. if (shader_size == 0) {
  7419. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  7420. shname = std::string(ggml_type_name(quant)) + "_S";
  7421. } else if (shader_size == 1) {
  7422. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  7423. shname = std::string(ggml_type_name(quant)) + "_M";
  7424. } else if (shader_size == 2) {
  7425. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  7426. shname = std::string(ggml_type_name(quant)) + "_L";
  7427. } else {
  7428. GGML_ASSERT(0);
  7429. }
  7430. }
  7431. if (p == nullptr) {
  7432. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  7433. return;
  7434. }
  7435. const size_t x_sz = sizeof(float) * x_ne;
  7436. const size_t y_sz = sizeof(float) * y_ne;
  7437. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7438. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  7439. const size_t d_sz = sizeof(float) * d_ne;
  7440. float * x = (float *) malloc(x_sz);
  7441. float * y = (float *) malloc(y_sz);
  7442. void * qx = malloc(qx_sz);
  7443. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7444. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7445. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7446. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7447. float * d = (float *) malloc(d_sz);
  7448. float * d_chk = (float *) malloc(d_sz);
  7449. for (size_t i = 0; i < x_ne; i++) {
  7450. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7451. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7452. // x[i] = i % k;
  7453. }
  7454. ggml_vk_quantize_data(x, qx, x_ne, quant);
  7455. for (size_t i = 0; i < y_ne; i++) {
  7456. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7457. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7458. // y[i] = i % k;
  7459. }
  7460. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7461. if (split_k > 1) {
  7462. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7463. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7464. // Resize buffer
  7465. if (ctx->prealloc_split_k != nullptr) {
  7466. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7467. }
  7468. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7469. }
  7470. }
  7471. if (mmq) {
  7472. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  7473. }
  7474. if (ctx->device->need_compiles) {
  7475. ggml_vk_load_shaders(ctx->device);
  7476. }
  7477. ggml_pipeline_allocate_descriptor_sets(ctx);
  7478. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7479. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  7480. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7481. ggml_vk_ctx_begin(ctx->device, subctx);
  7482. if (mmq) {
  7483. for (size_t i = 0; i < num_it; i++) {
  7484. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  7485. ggml_vk_matmul(
  7486. 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 },
  7487. m, n, k,
  7488. k, k, m, k*m, k*n, m*n,
  7489. split_k, batch, batch, batch, 1, 1, n
  7490. );
  7491. }
  7492. } else {
  7493. for (size_t i = 0; i < num_it; i++) {
  7494. ggml_vk_matmul(
  7495. 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 },
  7496. m, n, k,
  7497. k, k, m, k*m, k*n, m*n,
  7498. split_k, batch, batch, batch, 1, 1, n
  7499. );
  7500. }
  7501. }
  7502. ggml_vk_ctx_end(subctx);
  7503. auto begin = std::chrono::high_resolution_clock::now();
  7504. ggml_vk_submit(subctx, ctx->fence);
  7505. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7506. ctx->device->device.resetFences({ ctx->fence });
  7507. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7508. auto end = std::chrono::high_resolution_clock::now();
  7509. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7510. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  7511. ggml_init_params iparams = {
  7512. /*.mem_size =*/ 1024*1024*1024,
  7513. /*.mem_buffer =*/ NULL,
  7514. /*.no_alloc =*/ true,
  7515. };
  7516. ggml_context * ggml_ctx = ggml_init(iparams);
  7517. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  7518. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  7519. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7520. src0_ggml->data = qx;
  7521. src1_ggml->data = y;
  7522. tensor_ggml->data = d_chk;
  7523. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7524. ggml_build_forward_expand(cgraph, tensor_ggml);
  7525. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7526. ggml_free(ggml_ctx);
  7527. double avg_err = 0.0;
  7528. int first_err_n = -1;
  7529. int first_err_m = -1;
  7530. int first_err_b = -1;
  7531. for (size_t i = 0; i < m*n*batch; i++) {
  7532. double err = std::fabs(d[i] - d_chk[i]);
  7533. avg_err += err;
  7534. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7535. first_err_b = i / (m * n);
  7536. first_err_n = (i % (m * n)) / m;
  7537. first_err_m = (i % (m * n)) % m;
  7538. }
  7539. }
  7540. avg_err /= m * n;
  7541. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7542. std::cerr << "TEST dequant matmul " << shname;
  7543. if (mmq) {
  7544. std::cerr << " mmq";
  7545. }
  7546. 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;
  7547. if (avg_err > 0.01 || std::isnan(avg_err)) {
  7548. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7549. std::cerr << "Actual result: " << std::endl << std::endl;
  7550. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7551. std::cerr << std::endl;
  7552. std::cerr << "Expected result: " << std::endl << std::endl;
  7553. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7554. std::cerr << "src0: " << std::endl << std::endl;
  7555. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  7556. std::cerr << std::endl;
  7557. std::cerr << "src1: " << std::endl << std::endl;
  7558. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  7559. if (split_k > 1) {
  7560. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7561. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7562. std::cerr << "d_buf0: " << std::endl << std::endl;
  7563. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7564. std::cerr << "d_buf1: " << std::endl << std::endl;
  7565. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7566. std::cerr << "d_buf2: " << std::endl << std::endl;
  7567. 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);
  7568. std::cerr << "d_buf3: " << std::endl << std::endl;
  7569. 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);
  7570. free(split_k_buf);
  7571. }
  7572. }
  7573. ggml_vk_destroy_buffer(qx_buf);
  7574. ggml_vk_destroy_buffer(y_buf);
  7575. ggml_vk_destroy_buffer(qy_buf);
  7576. ggml_vk_destroy_buffer(d_buf);
  7577. free(x);
  7578. free(qx);
  7579. free(y);
  7580. free(d);
  7581. free(d_chk);
  7582. }
  7583. #endif
  7584. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  7585. #if defined(GGML_VULKAN_RUN_TESTS)
  7586. const std::vector<size_t> vals {
  7587. 512, 512, 128,
  7588. 128, 512, 512,
  7589. 4096, 512, 4096,
  7590. 11008, 512, 4096,
  7591. 4096, 512, 11008,
  7592. 32000, 512, 4096,
  7593. 8, 8, 8,
  7594. 100, 46, 576,
  7595. 623, 111, 128,
  7596. 100, 46, 558,
  7597. 512, 1, 256,
  7598. 128, 110, 622,
  7599. 511, 511, 127,
  7600. 511, 511, 7,
  7601. 511, 511, 17,
  7602. 49, 49, 128,
  7603. 128, 49, 49,
  7604. 4096, 49, 4096,
  7605. };
  7606. const size_t num_it = 100;
  7607. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7608. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7609. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7610. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  7611. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  7612. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  7613. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  7614. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  7615. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  7616. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  7617. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  7618. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  7619. abort();
  7620. for (size_t i = 0; i < vals.size(); i += 3) {
  7621. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  7622. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  7623. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  7624. std::cerr << '\n';
  7625. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  7626. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  7627. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  7628. std::cerr << '\n';
  7629. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  7630. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  7631. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  7632. std::cerr << '\n' << std::endl;
  7633. if (vals[i + 2] % 32 == 0) {
  7634. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7635. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7636. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7637. std::cerr << '\n';
  7638. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  7639. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  7640. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  7641. std::cerr << '\n';
  7642. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  7643. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  7644. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  7645. std::cerr << '\n' << std::endl;
  7646. }
  7647. if (vals[i + 2] % 256 == 0) {
  7648. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  7649. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  7650. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  7651. std::cerr << '\n';
  7652. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  7653. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  7654. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  7655. std::cerr << '\n';
  7656. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  7657. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  7658. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  7659. std::cerr << '\n' << std::endl;
  7660. }
  7661. }
  7662. GGML_ABORT("fatal error");
  7663. #endif
  7664. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  7665. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  7666. // Resize buffer
  7667. if (ctx->prealloc_x != nullptr) {
  7668. ggml_vk_destroy_buffer(ctx->prealloc_x);
  7669. }
  7670. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  7671. }
  7672. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  7673. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  7674. // Resize buffer
  7675. if (ctx->prealloc_y != nullptr) {
  7676. ggml_vk_destroy_buffer(ctx->prealloc_y);
  7677. }
  7678. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  7679. }
  7680. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  7681. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  7682. // Resize buffer
  7683. if (ctx->prealloc_split_k != nullptr) {
  7684. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7685. }
  7686. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  7687. }
  7688. }
  7689. 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);
  7690. // Returns true if node has enqueued work into the queue, false otherwise
  7691. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  7692. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
  7693. ggml_tensor * node = cgraph->nodes[node_idx];
  7694. if (ggml_is_empty(node) || !node->buffer) {
  7695. return false;
  7696. }
  7697. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  7698. ctx->semaphore_idx = 0;
  7699. const ggml_tensor * src0 = node->src[0];
  7700. const ggml_tensor * src1 = node->src[1];
  7701. const ggml_tensor * src2 = node->src[2];
  7702. const ggml_tensor * src3 = node->src[3];
  7703. switch (node->op) {
  7704. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  7705. case GGML_OP_RESHAPE:
  7706. case GGML_OP_VIEW:
  7707. case GGML_OP_PERMUTE:
  7708. case GGML_OP_TRANSPOSE:
  7709. case GGML_OP_NONE:
  7710. return false;
  7711. case GGML_OP_UNARY:
  7712. switch (ggml_get_unary_op(node)) {
  7713. case GGML_UNARY_OP_SILU:
  7714. case GGML_UNARY_OP_GELU:
  7715. case GGML_UNARY_OP_GELU_ERF:
  7716. case GGML_UNARY_OP_GELU_QUICK:
  7717. case GGML_UNARY_OP_RELU:
  7718. case GGML_UNARY_OP_TANH:
  7719. case GGML_UNARY_OP_SIGMOID:
  7720. break;
  7721. default:
  7722. return false;
  7723. }
  7724. break;
  7725. case GGML_OP_GLU:
  7726. switch (ggml_get_glu_op(node)) {
  7727. case GGML_GLU_OP_GEGLU:
  7728. case GGML_GLU_OP_REGLU:
  7729. case GGML_GLU_OP_SWIGLU:
  7730. case GGML_GLU_OP_GEGLU_ERF:
  7731. case GGML_GLU_OP_GEGLU_QUICK:
  7732. break;
  7733. default:
  7734. return false;
  7735. }
  7736. break;
  7737. case GGML_OP_REPEAT:
  7738. case GGML_OP_REPEAT_BACK:
  7739. case GGML_OP_GET_ROWS:
  7740. case GGML_OP_ADD:
  7741. case GGML_OP_ACC:
  7742. case GGML_OP_SUB:
  7743. case GGML_OP_MUL:
  7744. case GGML_OP_DIV:
  7745. case GGML_OP_CONCAT:
  7746. case GGML_OP_UPSCALE:
  7747. case GGML_OP_SCALE:
  7748. case GGML_OP_SQR:
  7749. case GGML_OP_SIN:
  7750. case GGML_OP_COS:
  7751. case GGML_OP_CLAMP:
  7752. case GGML_OP_PAD:
  7753. case GGML_OP_ROLL:
  7754. case GGML_OP_CPY:
  7755. case GGML_OP_SET_ROWS:
  7756. case GGML_OP_CONT:
  7757. case GGML_OP_DUP:
  7758. case GGML_OP_SILU_BACK:
  7759. case GGML_OP_NORM:
  7760. case GGML_OP_GROUP_NORM:
  7761. case GGML_OP_RMS_NORM:
  7762. case GGML_OP_RMS_NORM_BACK:
  7763. case GGML_OP_L2_NORM:
  7764. case GGML_OP_DIAG_MASK_INF:
  7765. case GGML_OP_SOFT_MAX:
  7766. case GGML_OP_SOFT_MAX_BACK:
  7767. case GGML_OP_ROPE:
  7768. case GGML_OP_ROPE_BACK:
  7769. case GGML_OP_MUL_MAT:
  7770. case GGML_OP_MUL_MAT_ID:
  7771. case GGML_OP_ARGSORT:
  7772. case GGML_OP_SUM:
  7773. case GGML_OP_SUM_ROWS:
  7774. case GGML_OP_ARGMAX:
  7775. case GGML_OP_COUNT_EQUAL:
  7776. case GGML_OP_IM2COL:
  7777. case GGML_OP_TIMESTEP_EMBEDDING:
  7778. case GGML_OP_CONV_TRANSPOSE_1D:
  7779. case GGML_OP_POOL_2D:
  7780. case GGML_OP_CONV_2D_DW:
  7781. case GGML_OP_RWKV_WKV6:
  7782. case GGML_OP_RWKV_WKV7:
  7783. case GGML_OP_LEAKY_RELU:
  7784. case GGML_OP_FLASH_ATTN_EXT:
  7785. case GGML_OP_OPT_STEP_ADAMW:
  7786. break;
  7787. default:
  7788. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  7789. GGML_ABORT("fatal error");
  7790. return false;
  7791. }
  7792. vk_context compute_ctx;
  7793. if (!dryrun) {
  7794. if (ctx->compute_ctx.expired()) {
  7795. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7796. ctx->compute_ctx = compute_ctx;
  7797. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  7798. } else {
  7799. compute_ctx = ctx->compute_ctx.lock();
  7800. }
  7801. } else {
  7802. switch (node->op) {
  7803. case GGML_OP_REPEAT:
  7804. case GGML_OP_REPEAT_BACK:
  7805. case GGML_OP_ACC:
  7806. case GGML_OP_GET_ROWS:
  7807. case GGML_OP_ADD:
  7808. case GGML_OP_SUB:
  7809. case GGML_OP_MUL:
  7810. case GGML_OP_DIV:
  7811. case GGML_OP_CONCAT:
  7812. case GGML_OP_UPSCALE:
  7813. case GGML_OP_SCALE:
  7814. case GGML_OP_SQR:
  7815. case GGML_OP_SIN:
  7816. case GGML_OP_COS:
  7817. case GGML_OP_CLAMP:
  7818. case GGML_OP_PAD:
  7819. case GGML_OP_CPY:
  7820. case GGML_OP_SET_ROWS:
  7821. case GGML_OP_CONT:
  7822. case GGML_OP_DUP:
  7823. case GGML_OP_SILU_BACK:
  7824. case GGML_OP_NORM:
  7825. case GGML_OP_GROUP_NORM:
  7826. case GGML_OP_RMS_NORM:
  7827. case GGML_OP_RMS_NORM_BACK:
  7828. case GGML_OP_L2_NORM:
  7829. case GGML_OP_UNARY:
  7830. case GGML_OP_GLU:
  7831. case GGML_OP_DIAG_MASK_INF:
  7832. case GGML_OP_SOFT_MAX:
  7833. case GGML_OP_SOFT_MAX_BACK:
  7834. case GGML_OP_ROPE:
  7835. case GGML_OP_ROPE_BACK:
  7836. case GGML_OP_ARGSORT:
  7837. case GGML_OP_SUM:
  7838. case GGML_OP_SUM_ROWS:
  7839. case GGML_OP_ARGMAX:
  7840. case GGML_OP_COUNT_EQUAL:
  7841. case GGML_OP_IM2COL:
  7842. case GGML_OP_TIMESTEP_EMBEDDING:
  7843. case GGML_OP_CONV_TRANSPOSE_1D:
  7844. case GGML_OP_POOL_2D:
  7845. case GGML_OP_CONV_2D_DW:
  7846. case GGML_OP_LEAKY_RELU:
  7847. {
  7848. // These operations all go through ggml_vk_op_f32, so short-circuit and
  7849. // do the only thing needed for the dryrun.
  7850. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  7851. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7852. return false;
  7853. }
  7854. default:
  7855. break;
  7856. }
  7857. }
  7858. switch (node->op) {
  7859. case GGML_OP_REPEAT:
  7860. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  7861. break;
  7862. case GGML_OP_REPEAT_BACK:
  7863. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  7864. break;
  7865. case GGML_OP_ACC:
  7866. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  7867. break;
  7868. case GGML_OP_GET_ROWS:
  7869. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7870. break;
  7871. case GGML_OP_ADD:
  7872. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  7873. break;
  7874. case GGML_OP_SUB:
  7875. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  7876. break;
  7877. case GGML_OP_MUL:
  7878. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  7879. break;
  7880. case GGML_OP_DIV:
  7881. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  7882. break;
  7883. case GGML_OP_CONCAT:
  7884. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  7885. break;
  7886. case GGML_OP_UPSCALE:
  7887. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  7888. break;
  7889. case GGML_OP_SCALE:
  7890. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  7891. break;
  7892. case GGML_OP_SQR:
  7893. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  7894. break;
  7895. case GGML_OP_SIN:
  7896. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  7897. break;
  7898. case GGML_OP_COS:
  7899. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  7900. break;
  7901. case GGML_OP_CLAMP:
  7902. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  7903. break;
  7904. case GGML_OP_PAD:
  7905. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  7906. break;
  7907. case GGML_OP_ROLL:
  7908. ggml_vk_roll(ctx, compute_ctx, src0, node, dryrun);
  7909. break;
  7910. case GGML_OP_CPY:
  7911. case GGML_OP_CONT:
  7912. case GGML_OP_DUP:
  7913. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  7914. break;
  7915. case GGML_OP_SET_ROWS:
  7916. ggml_vk_set_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7917. break;
  7918. case GGML_OP_SILU_BACK:
  7919. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7920. break;
  7921. case GGML_OP_NORM:
  7922. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  7923. break;
  7924. case GGML_OP_GROUP_NORM:
  7925. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  7926. break;
  7927. case GGML_OP_RMS_NORM:
  7928. if (ctx->num_additional_fused_ops > 0) {
  7929. // fused rms_norm + mul
  7930. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  7931. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  7932. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, (float *)node->op_params, dryrun);
  7933. } else {
  7934. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, (float *)node->op_params, dryrun);
  7935. }
  7936. break;
  7937. case GGML_OP_RMS_NORM_BACK:
  7938. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7939. break;
  7940. case GGML_OP_L2_NORM:
  7941. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  7942. break;
  7943. case GGML_OP_UNARY:
  7944. switch (ggml_get_unary_op(node)) {
  7945. case GGML_UNARY_OP_SILU:
  7946. case GGML_UNARY_OP_GELU:
  7947. case GGML_UNARY_OP_GELU_ERF:
  7948. case GGML_UNARY_OP_GELU_QUICK:
  7949. case GGML_UNARY_OP_RELU:
  7950. case GGML_UNARY_OP_TANH:
  7951. case GGML_UNARY_OP_SIGMOID:
  7952. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  7953. break;
  7954. default:
  7955. return false;
  7956. }
  7957. break;
  7958. case GGML_OP_GLU:
  7959. switch (ggml_get_glu_op(node)) {
  7960. case GGML_GLU_OP_GEGLU:
  7961. case GGML_GLU_OP_REGLU:
  7962. case GGML_GLU_OP_SWIGLU:
  7963. case GGML_GLU_OP_GEGLU_ERF:
  7964. case GGML_GLU_OP_GEGLU_QUICK:
  7965. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  7966. break;
  7967. default:
  7968. return false;
  7969. }
  7970. break;
  7971. case GGML_OP_DIAG_MASK_INF:
  7972. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  7973. break;
  7974. case GGML_OP_SOFT_MAX:
  7975. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  7976. break;
  7977. case GGML_OP_SOFT_MAX_BACK:
  7978. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7979. break;
  7980. case GGML_OP_ROPE:
  7981. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  7982. break;
  7983. case GGML_OP_ROPE_BACK:
  7984. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  7985. break;
  7986. case GGML_OP_ARGSORT:
  7987. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  7988. break;
  7989. case GGML_OP_SUM:
  7990. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  7991. break;
  7992. case GGML_OP_SUM_ROWS:
  7993. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  7994. break;
  7995. case GGML_OP_ARGMAX:
  7996. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  7997. break;
  7998. case GGML_OP_COUNT_EQUAL:
  7999. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  8000. break;
  8001. case GGML_OP_IM2COL:
  8002. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  8003. break;
  8004. case GGML_OP_TIMESTEP_EMBEDDING:
  8005. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  8006. break;
  8007. case GGML_OP_CONV_TRANSPOSE_1D:
  8008. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  8009. break;
  8010. case GGML_OP_POOL_2D:
  8011. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  8012. break;
  8013. case GGML_OP_CONV_2D_DW:
  8014. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  8015. break;
  8016. case GGML_OP_LEAKY_RELU:
  8017. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  8018. break;
  8019. case GGML_OP_MUL_MAT:
  8020. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  8021. break;
  8022. case GGML_OP_MUL_MAT_ID:
  8023. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  8024. break;
  8025. case GGML_OP_FLASH_ATTN_EXT:
  8026. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  8027. break;
  8028. case GGML_OP_RWKV_WKV6:
  8029. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  8030. break;
  8031. case GGML_OP_RWKV_WKV7:
  8032. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  8033. break;
  8034. case GGML_OP_OPT_STEP_ADAMW:
  8035. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  8036. break;
  8037. default:
  8038. return false;
  8039. }
  8040. if (dryrun) {
  8041. return false;
  8042. }
  8043. ctx->tensor_ctxs[node_idx] = compute_ctx;
  8044. #if defined(GGML_VULKAN_CHECK_RESULTS)
  8045. // Force context reset on each node so that each tensor ends up in its own context
  8046. // and can be run and compared to its CPU equivalent separately
  8047. last_node = true;
  8048. #endif
  8049. if (submit || last_node) {
  8050. ggml_vk_ctx_end(compute_ctx);
  8051. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  8052. if (last_node) {
  8053. compute_ctx->exit_tensor_idx = node_idx_begin;
  8054. }
  8055. else {
  8056. compute_ctx->exit_tensor_idx = -1;
  8057. }
  8058. ctx->compute_ctx.reset();
  8059. bool ok = ggml_vk_compute_forward(ctx, cgraph, node_begin, node_idx_begin, false, almost_ready);
  8060. if (!ok) {
  8061. if (node->op == GGML_OP_UNARY) {
  8062. 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;
  8063. } else if (node->op == GGML_OP_GLU) {
  8064. 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;
  8065. } else {
  8066. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  8067. }
  8068. }
  8069. }
  8070. return true;
  8071. }
  8072. 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) {
  8073. GGML_UNUSED(cgraph);
  8074. ggml_backend_buffer * buf = nullptr;
  8075. switch (tensor->op) {
  8076. case GGML_OP_ADD:
  8077. case GGML_OP_ACC:
  8078. case GGML_OP_GET_ROWS:
  8079. case GGML_OP_SUB:
  8080. case GGML_OP_MUL:
  8081. case GGML_OP_DIV:
  8082. case GGML_OP_CONCAT:
  8083. case GGML_OP_UPSCALE:
  8084. case GGML_OP_SCALE:
  8085. case GGML_OP_SQR:
  8086. case GGML_OP_SIN:
  8087. case GGML_OP_COS:
  8088. case GGML_OP_CLAMP:
  8089. case GGML_OP_PAD:
  8090. case GGML_OP_ROLL:
  8091. case GGML_OP_CPY:
  8092. case GGML_OP_SET_ROWS:
  8093. case GGML_OP_CONT:
  8094. case GGML_OP_DUP:
  8095. case GGML_OP_SILU_BACK:
  8096. case GGML_OP_NORM:
  8097. case GGML_OP_GROUP_NORM:
  8098. case GGML_OP_RMS_NORM:
  8099. case GGML_OP_RMS_NORM_BACK:
  8100. case GGML_OP_L2_NORM:
  8101. case GGML_OP_DIAG_MASK_INF:
  8102. case GGML_OP_SOFT_MAX:
  8103. case GGML_OP_SOFT_MAX_BACK:
  8104. case GGML_OP_ROPE:
  8105. case GGML_OP_ROPE_BACK:
  8106. case GGML_OP_RESHAPE:
  8107. case GGML_OP_VIEW:
  8108. case GGML_OP_PERMUTE:
  8109. case GGML_OP_TRANSPOSE:
  8110. case GGML_OP_NONE:
  8111. case GGML_OP_ARGSORT:
  8112. case GGML_OP_SUM:
  8113. case GGML_OP_SUM_ROWS:
  8114. case GGML_OP_ARGMAX:
  8115. case GGML_OP_COUNT_EQUAL:
  8116. case GGML_OP_IM2COL:
  8117. case GGML_OP_TIMESTEP_EMBEDDING:
  8118. case GGML_OP_CONV_TRANSPOSE_1D:
  8119. case GGML_OP_POOL_2D:
  8120. case GGML_OP_CONV_2D_DW:
  8121. case GGML_OP_RWKV_WKV6:
  8122. case GGML_OP_RWKV_WKV7:
  8123. case GGML_OP_LEAKY_RELU:
  8124. case GGML_OP_REPEAT:
  8125. case GGML_OP_REPEAT_BACK:
  8126. case GGML_OP_OPT_STEP_ADAMW:
  8127. buf = tensor->buffer;
  8128. break;
  8129. case GGML_OP_UNARY:
  8130. switch (ggml_get_unary_op(tensor)) {
  8131. case GGML_UNARY_OP_SILU:
  8132. case GGML_UNARY_OP_GELU:
  8133. case GGML_UNARY_OP_GELU_ERF:
  8134. case GGML_UNARY_OP_GELU_QUICK:
  8135. case GGML_UNARY_OP_RELU:
  8136. case GGML_UNARY_OP_TANH:
  8137. case GGML_UNARY_OP_SIGMOID:
  8138. buf = tensor->buffer;
  8139. break;
  8140. default:
  8141. return false;
  8142. }
  8143. break;
  8144. case GGML_OP_GLU:
  8145. switch (ggml_get_glu_op(tensor)) {
  8146. case GGML_GLU_OP_GEGLU:
  8147. case GGML_GLU_OP_REGLU:
  8148. case GGML_GLU_OP_SWIGLU:
  8149. case GGML_GLU_OP_GEGLU_ERF:
  8150. case GGML_GLU_OP_GEGLU_QUICK:
  8151. buf = tensor->buffer;
  8152. break;
  8153. default:
  8154. return false;
  8155. }
  8156. break;
  8157. case GGML_OP_MUL_MAT:
  8158. case GGML_OP_MUL_MAT_ID:
  8159. case GGML_OP_FLASH_ATTN_EXT:
  8160. buf = tensor->buffer;
  8161. break;
  8162. default:
  8163. return false;
  8164. }
  8165. if (buf == nullptr) {
  8166. return false;
  8167. }
  8168. 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 << ")");
  8169. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  8170. // always wait for the GPU work to be done for the last submit
  8171. if (tensor_idx == subctx->exit_tensor_idx) {
  8172. use_fence = true;
  8173. }
  8174. // Only run if ctx hasn't been submitted yet
  8175. if (!subctx->seqs.empty()) {
  8176. #ifdef GGML_VULKAN_CHECK_RESULTS
  8177. ggml_vk_check_results_0(ctx, cgraph, tensor_idx);
  8178. use_fence = true;
  8179. #endif
  8180. // Do staging buffer copies
  8181. for (auto& cpy : subctx->in_memcpys) {
  8182. memcpy(cpy.dst, cpy.src, cpy.n);
  8183. }
  8184. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  8185. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  8186. ctx->almost_ready_fence_pending = true;
  8187. } else {
  8188. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  8189. }
  8190. if (use_fence) {
  8191. ggml_vk_wait_for_fence(ctx);
  8192. }
  8193. #ifdef GGML_VULKAN_CHECK_RESULTS
  8194. ggml_vk_check_results_1(ctx, cgraph, tensor_idx);
  8195. #endif
  8196. }
  8197. if (tensor_idx == subctx->exit_tensor_idx) {
  8198. // Do staging buffer copies
  8199. for (auto& cpy : subctx->out_memcpys) {
  8200. memcpy(cpy.dst, cpy.src, cpy.n);
  8201. }
  8202. subctx->in_memcpys.clear();
  8203. subctx->out_memcpys.clear();
  8204. }
  8205. return true;
  8206. }
  8207. // Clean up after graph processing is done
  8208. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  8209. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  8210. for (auto& buffer : ctx->gc.temp_buffers) {
  8211. ggml_vk_pool_free(ctx, buffer);
  8212. }
  8213. ctx->gc.temp_buffers.clear();
  8214. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8215. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8216. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  8217. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  8218. }
  8219. ctx->gc.semaphores.clear();
  8220. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  8221. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  8222. }
  8223. ctx->gc.tl_semaphores.clear();
  8224. ctx->semaphore_idx = 0;
  8225. ctx->event_idx = 0;
  8226. for (auto& event : ctx->gc.events) {
  8227. ctx->device->device.resetEvent(event);
  8228. }
  8229. ctx->tensor_ctxs.clear();
  8230. ctx->gc.contexts.clear();
  8231. ctx->pipeline_descriptor_set_requirements = 0;
  8232. ctx->descriptor_set_idx = 0;
  8233. }
  8234. // Clean up on backend free
  8235. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  8236. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  8237. ggml_vk_graph_cleanup(ctx);
  8238. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8239. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8240. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8241. for (auto& buffer : ctx->buffer_pool) {
  8242. ggml_vk_destroy_buffer(buffer);
  8243. }
  8244. ctx->prealloc_size_x = 0;
  8245. ctx->prealloc_size_y = 0;
  8246. ctx->prealloc_size_split_k = 0;
  8247. for (auto& event : ctx->gc.events) {
  8248. ctx->device->device.destroyEvent(event);
  8249. }
  8250. ctx->gc.events.clear();
  8251. ctx->device->device.destroyFence(ctx->fence);
  8252. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  8253. for (auto& pool : ctx->descriptor_pools) {
  8254. ctx->device->device.destroyDescriptorPool(pool);
  8255. }
  8256. ctx->descriptor_pools.clear();
  8257. ctx->descriptor_sets.clear();
  8258. ctx->compute_cmd_pool.destroy(ctx->device->device);
  8259. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  8260. }
  8261. static int ggml_vk_get_device_count() {
  8262. ggml_vk_instance_init();
  8263. return vk_instance.device_indices.size();
  8264. }
  8265. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  8266. ggml_vk_instance_init();
  8267. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  8268. vk::PhysicalDeviceProperties props;
  8269. devices[device].getProperties(&props);
  8270. snprintf(description, description_size, "%s", props.deviceName.data());
  8271. }
  8272. // backend interface
  8273. #define UNUSED GGML_UNUSED
  8274. // device backend
  8275. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  8276. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  8277. }
  8278. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8279. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  8280. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8281. ggml_vk_destroy_buffer(ctx->dev_buffer);
  8282. delete ctx;
  8283. }
  8284. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  8285. return vk_ptr_base;
  8286. UNUSED(buffer);
  8287. }
  8288. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8289. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  8290. if (tensor->view_src != nullptr) {
  8291. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  8292. }
  8293. return GGML_STATUS_SUCCESS;
  8294. }
  8295. 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) {
  8296. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  8297. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8298. vk_buffer buf = buf_ctx->dev_buffer;
  8299. uint32_t val32 = (uint32_t)value * 0x01010101;
  8300. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  8301. }
  8302. 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) {
  8303. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8304. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8305. vk_buffer buf = buf_ctx->dev_buffer;
  8306. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8307. }
  8308. 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) {
  8309. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8310. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8311. vk_buffer buf = buf_ctx->dev_buffer;
  8312. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8313. }
  8314. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8315. if (ggml_backend_buffer_is_vk(src->buffer)) {
  8316. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8317. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8318. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8319. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8320. 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));
  8321. return true;
  8322. }
  8323. return false;
  8324. UNUSED(buffer);
  8325. }
  8326. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8327. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8328. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  8329. }
  8330. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  8331. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  8332. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  8333. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  8334. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  8335. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  8336. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  8337. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  8338. /* .clear = */ ggml_backend_vk_buffer_clear,
  8339. /* .reset = */ NULL,
  8340. };
  8341. // vk buffer type
  8342. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8343. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8344. return ctx->name.c_str();
  8345. }
  8346. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8347. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  8348. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8349. vk_buffer dev_buffer = nullptr;
  8350. try {
  8351. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  8352. } catch (const vk::SystemError& e) {
  8353. return nullptr;
  8354. }
  8355. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  8356. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  8357. }
  8358. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8359. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8360. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  8361. }
  8362. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8363. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8364. return ctx->device->suballocation_block_size;
  8365. }
  8366. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8367. return ggml_nbytes(tensor);
  8368. UNUSED(buft);
  8369. }
  8370. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  8371. ggml_vk_instance_init();
  8372. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  8373. vk_device dev = ggml_vk_get_device(dev_num);
  8374. return &dev->buffer_type;
  8375. }
  8376. // host buffer type
  8377. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8378. return GGML_VK_NAME "_Host";
  8379. UNUSED(buft);
  8380. }
  8381. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  8382. return GGML_VK_NAME "_Host";
  8383. UNUSED(buffer);
  8384. }
  8385. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8386. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  8387. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  8388. }
  8389. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8390. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  8391. size += 32; // Behave like the CPU buffer type
  8392. void * ptr = nullptr;
  8393. try {
  8394. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  8395. } catch (vk::SystemError& e) {
  8396. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  8397. // fallback to cpu buffer
  8398. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8399. }
  8400. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8401. buffer->buft = buft;
  8402. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  8403. return buffer;
  8404. UNUSED(buft);
  8405. }
  8406. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8407. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  8408. UNUSED(buft);
  8409. }
  8410. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8411. return vk_instance.devices[0]->suballocation_block_size;
  8412. UNUSED(buft);
  8413. }
  8414. // Should be changed to return device-specific host buffer type
  8415. // but that probably requires changes in llama.cpp
  8416. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  8417. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  8418. /* .iface = */ {
  8419. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  8420. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  8421. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  8422. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  8423. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8424. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8425. },
  8426. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  8427. /* .context = */ nullptr,
  8428. };
  8429. // Make sure device 0 is initialized
  8430. ggml_vk_instance_init();
  8431. ggml_vk_get_device(0);
  8432. return &ggml_backend_vk_buffer_type_host;
  8433. }
  8434. // backend
  8435. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  8436. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8437. return ctx->name.c_str();
  8438. }
  8439. static void ggml_backend_vk_free(ggml_backend_t backend) {
  8440. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8441. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  8442. ggml_vk_cleanup(ctx);
  8443. delete ctx;
  8444. delete backend;
  8445. }
  8446. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  8447. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8448. return &ctx->device->buffer_type;
  8449. }
  8450. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8451. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  8452. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8453. 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");
  8454. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8455. vk_context transfer_ctx;
  8456. if (ctx->transfer_ctx.expired()) {
  8457. // Initialize new transfer context
  8458. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8459. ctx->transfer_ctx = transfer_ctx;
  8460. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8461. } else {
  8462. transfer_ctx = ctx->transfer_ctx.lock();
  8463. }
  8464. vk_buffer buf = buf_ctx->dev_buffer;
  8465. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8466. }
  8467. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8468. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  8469. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8470. 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");
  8471. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8472. vk_context transfer_ctx;
  8473. if (ctx->transfer_ctx.expired()) {
  8474. // Initialize new transfer context
  8475. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8476. ctx->transfer_ctx = transfer_ctx;
  8477. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8478. } else {
  8479. transfer_ctx = ctx->transfer_ctx.lock();
  8480. }
  8481. vk_buffer buf = buf_ctx->dev_buffer;
  8482. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8483. }
  8484. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  8485. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  8486. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8487. 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)) {
  8488. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8489. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8490. vk_context transfer_ctx;
  8491. if (ctx->transfer_ctx.expired()) {
  8492. // Initialize new transfer context
  8493. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8494. ctx->transfer_ctx = transfer_ctx;
  8495. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8496. } else {
  8497. transfer_ctx = ctx->transfer_ctx.lock();
  8498. }
  8499. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8500. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8501. 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));
  8502. return true;
  8503. }
  8504. return false;
  8505. }
  8506. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  8507. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  8508. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8509. if(ctx->transfer_ctx.expired()) {
  8510. return;
  8511. }
  8512. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  8513. ggml_vk_ctx_end(transfer_ctx);
  8514. for (auto& cpy : transfer_ctx->in_memcpys) {
  8515. memcpy(cpy.dst, cpy.src, cpy.n);
  8516. }
  8517. ggml_vk_submit(transfer_ctx, ctx->fence);
  8518. ggml_vk_wait_for_fence(ctx);
  8519. for (auto& cpy : transfer_ctx->out_memcpys) {
  8520. memcpy(cpy.dst, cpy.src, cpy.n);
  8521. }
  8522. ctx->transfer_ctx.reset();
  8523. }
  8524. static bool ggml_vk_is_empty(ggml_tensor * node) {
  8525. 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;
  8526. }
  8527. static bool ggml_vk_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {
  8528. if (!ggml_can_fuse(cgraph, node_idx, ops)) {
  8529. return false;
  8530. }
  8531. if (ops.size() == 2 && ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
  8532. // additional constraints specific to this fusion
  8533. const ggml_tensor *rms_norm = cgraph->nodes[node_idx];
  8534. const ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  8535. GGML_ASSERT(rms_norm->src[0]->type == GGML_TYPE_F32);
  8536. GGML_ASSERT(rms_norm->type == GGML_TYPE_F32);
  8537. // rms_norm only supports f32
  8538. if (mul->src[0]->type != GGML_TYPE_F32 ||
  8539. mul->src[1]->type != GGML_TYPE_F32 ||
  8540. mul->type != GGML_TYPE_F32) {
  8541. return false;
  8542. }
  8543. // if rms_norm is the B operand, then we don't handle broadcast
  8544. if (rms_norm == mul->src[1] &&
  8545. mul->src[0]->ne[1] != rms_norm->ne[1]) {
  8546. return false;
  8547. }
  8548. // rms_norm shader assumes contiguous rows
  8549. if (!ggml_is_contiguous_rows(mul->src[0]) || !ggml_is_contiguous_rows(mul->src[1])) {
  8550. return false;
  8551. }
  8552. }
  8553. return true;
  8554. }
  8555. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8556. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  8557. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8558. if (vk_instance.debug_utils_support) {
  8559. vk::DebugUtilsLabelEXT dul = {};
  8560. dul.pLabelName = "ggml_backend_vk_graph_compute";
  8561. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  8562. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  8563. }
  8564. uint64_t total_mat_mul_bytes = 0;
  8565. for (int i = 0; i < cgraph->n_nodes; i++) {
  8566. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8567. ctx->num_additional_fused_ops = 1;
  8568. }
  8569. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  8570. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8571. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8572. }
  8573. i += ctx->num_additional_fused_ops;
  8574. ctx->num_additional_fused_ops = 0;
  8575. }
  8576. if (ctx->device->need_compiles) {
  8577. ggml_vk_load_shaders(ctx->device);
  8578. }
  8579. ggml_vk_preallocate_buffers(ctx);
  8580. ggml_pipeline_allocate_descriptor_sets(ctx);
  8581. int last_node = cgraph->n_nodes - 1;
  8582. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  8583. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  8584. last_node -= 1;
  8585. }
  8586. // Reserve tensor context space for all nodes
  8587. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  8588. bool first_node_in_batch = true; // true if next node will be first node in a batch
  8589. int submit_node_idx = 0; // index to first node in a batch
  8590. vk_context compute_ctx;
  8591. if (vk_perf_logger_enabled) {
  8592. // allocate/resize the query pool
  8593. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  8594. if (ctx->device->query_pool) {
  8595. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  8596. }
  8597. vk::QueryPoolCreateInfo query_create_info;
  8598. query_create_info.queryType = vk::QueryType::eTimestamp;
  8599. query_create_info.queryCount = cgraph->n_nodes + 100;
  8600. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  8601. ctx->device->num_queries = query_create_info.queryCount;
  8602. }
  8603. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  8604. GGML_ASSERT(ctx->compute_ctx.expired());
  8605. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8606. ctx->compute_ctx = compute_ctx;
  8607. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8608. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  8609. }
  8610. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  8611. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  8612. // (and scaled down based on model size, so smaller models submit earlier).
  8613. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  8614. int nodes_per_submit = 100;
  8615. int submitted_nodes = 0;
  8616. int submit_count = 0;
  8617. uint64_t mul_mat_bytes = 0;
  8618. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  8619. for (int i = 0; i < cgraph->n_nodes; i++) {
  8620. if (first_node_in_batch) {
  8621. submit_node_idx = i;
  8622. }
  8623. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8624. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8625. }
  8626. if (!ctx->device->disable_fusion && ggml_vk_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8627. ctx->num_additional_fused_ops = 1;
  8628. }
  8629. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  8630. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  8631. bool submit = (submitted_nodes >= nodes_per_submit) ||
  8632. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  8633. (i + ctx->num_additional_fused_ops == last_node) ||
  8634. (almost_ready && !ctx->almost_ready_fence_pending);
  8635. bool enqueued = ggml_vk_build_graph(ctx, cgraph, i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i + ctx->num_additional_fused_ops == last_node, almost_ready, submit);
  8636. if (vk_perf_logger_enabled) {
  8637. if (ctx->compute_ctx.expired()) {
  8638. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8639. ctx->compute_ctx = compute_ctx;
  8640. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8641. } else {
  8642. compute_ctx = ctx->compute_ctx.lock();
  8643. }
  8644. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  8645. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  8646. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  8647. }
  8648. }
  8649. if (enqueued) {
  8650. ++submitted_nodes;
  8651. #ifndef GGML_VULKAN_CHECK_RESULTS
  8652. if (first_node_in_batch) {
  8653. first_node_in_batch = false;
  8654. }
  8655. #endif
  8656. }
  8657. if (submit && enqueued) {
  8658. first_node_in_batch = true;
  8659. submitted_nodes = 0;
  8660. mul_mat_bytes = 0;
  8661. if (submit_count < 3) {
  8662. mul_mat_bytes_per_submit *= 2;
  8663. }
  8664. submit_count++;
  8665. }
  8666. i += ctx->num_additional_fused_ops;
  8667. ctx->num_additional_fused_ops = 0;
  8668. }
  8669. if (vk_perf_logger_enabled) {
  8670. // End the command buffer and submit/wait
  8671. GGML_ASSERT(!ctx->compute_ctx.expired());
  8672. compute_ctx = ctx->compute_ctx.lock();
  8673. ggml_vk_ctx_end(compute_ctx);
  8674. ggml_vk_submit(compute_ctx, ctx->device->fence);
  8675. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  8676. ctx->device->device.resetFences({ ctx->device->fence });
  8677. // Get the results and pass them to the logger
  8678. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  8679. 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");
  8680. for (int i = 0; i < cgraph->n_nodes; i++) {
  8681. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  8682. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  8683. }
  8684. }
  8685. ctx->device->perf_logger->print_timings();
  8686. }
  8687. ggml_vk_graph_cleanup(ctx);
  8688. return GGML_STATUS_SUCCESS;
  8689. UNUSED(backend);
  8690. }
  8691. // TODO: enable async and synchronize
  8692. static ggml_backend_i ggml_backend_vk_interface = {
  8693. /* .get_name = */ ggml_backend_vk_name,
  8694. /* .free = */ ggml_backend_vk_free,
  8695. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  8696. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  8697. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  8698. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  8699. /* .graph_plan_create = */ NULL,
  8700. /* .graph_plan_free = */ NULL,
  8701. /* .graph_plan_update = */ NULL,
  8702. /* .graph_plan_compute = */ NULL,
  8703. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  8704. /* .event_record = */ NULL,
  8705. /* .event_wait = */ NULL,
  8706. };
  8707. static ggml_guid_t ggml_backend_vk_guid() {
  8708. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  8709. return &guid;
  8710. }
  8711. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  8712. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  8713. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  8714. ggml_vk_init(ctx, dev_num);
  8715. ggml_backend_t vk_backend = new ggml_backend {
  8716. /* .guid = */ ggml_backend_vk_guid(),
  8717. /* .interface = */ ggml_backend_vk_interface,
  8718. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  8719. /* .context = */ ctx,
  8720. };
  8721. return vk_backend;
  8722. }
  8723. bool ggml_backend_is_vk(ggml_backend_t backend) {
  8724. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  8725. }
  8726. int ggml_backend_vk_get_device_count() {
  8727. return ggml_vk_get_device_count();
  8728. }
  8729. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  8730. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8731. int dev_idx = vk_instance.device_indices[device];
  8732. ggml_vk_get_device_description(dev_idx, description, description_size);
  8733. }
  8734. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  8735. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8736. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  8737. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  8738. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  8739. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  8740. *total = heap.size;
  8741. *free = heap.size;
  8742. break;
  8743. }
  8744. }
  8745. }
  8746. //////////////////////////
  8747. struct ggml_backend_vk_device_context {
  8748. size_t device;
  8749. std::string name;
  8750. std::string description;
  8751. };
  8752. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  8753. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8754. return ctx->name.c_str();
  8755. }
  8756. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  8757. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8758. return ctx->description.c_str();
  8759. }
  8760. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  8761. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  8762. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  8763. }
  8764. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  8765. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8766. return ggml_backend_vk_buffer_type(ctx->device);
  8767. }
  8768. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  8769. UNUSED(dev);
  8770. return ggml_backend_vk_host_buffer_type();
  8771. }
  8772. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  8773. UNUSED(dev);
  8774. return GGML_BACKEND_DEVICE_TYPE_GPU;
  8775. }
  8776. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  8777. props->name = ggml_backend_vk_device_get_name(dev);
  8778. props->description = ggml_backend_vk_device_get_description(dev);
  8779. props->type = ggml_backend_vk_device_get_type(dev);
  8780. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  8781. props->caps = {
  8782. /* .async = */ false,
  8783. /* .host_buffer = */ true,
  8784. /* .buffer_from_host_ptr = */ false,
  8785. /* .events = */ false,
  8786. };
  8787. }
  8788. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  8789. UNUSED(params);
  8790. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8791. return ggml_backend_vk_init(ctx->device);
  8792. }
  8793. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  8794. switch (op->op) {
  8795. case GGML_OP_UNARY:
  8796. switch (ggml_get_unary_op(op)) {
  8797. case GGML_UNARY_OP_GELU:
  8798. case GGML_UNARY_OP_GELU_ERF:
  8799. case GGML_UNARY_OP_GELU_QUICK:
  8800. case GGML_UNARY_OP_SILU:
  8801. case GGML_UNARY_OP_RELU:
  8802. case GGML_UNARY_OP_TANH:
  8803. case GGML_UNARY_OP_SIGMOID:
  8804. return ggml_is_contiguous(op->src[0]) &&
  8805. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8806. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8807. (op->src[0]->type == op->type);
  8808. default:
  8809. return false;
  8810. }
  8811. break;
  8812. case GGML_OP_GLU:
  8813. switch (ggml_get_glu_op(op)) {
  8814. case GGML_GLU_OP_GEGLU:
  8815. case GGML_GLU_OP_REGLU:
  8816. case GGML_GLU_OP_SWIGLU:
  8817. case GGML_GLU_OP_GEGLU_ERF:
  8818. case GGML_GLU_OP_GEGLU_QUICK:
  8819. return ggml_is_contiguous(op->src[0]) &&
  8820. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8821. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8822. (op->src[0]->type == op->type);
  8823. default:
  8824. return false;
  8825. }
  8826. break;
  8827. case GGML_OP_MUL_MAT:
  8828. case GGML_OP_MUL_MAT_ID:
  8829. {
  8830. ggml_type src0_type = op->src[0]->type;
  8831. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8832. const vk_device& device = ggml_vk_get_device(ctx->device);
  8833. if (op->op == GGML_OP_MUL_MAT_ID) {
  8834. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  8835. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  8836. return false;
  8837. }
  8838. }
  8839. switch (src0_type) {
  8840. case GGML_TYPE_F32:
  8841. case GGML_TYPE_F16:
  8842. case GGML_TYPE_BF16:
  8843. case GGML_TYPE_Q4_0:
  8844. case GGML_TYPE_Q4_1:
  8845. case GGML_TYPE_Q5_0:
  8846. case GGML_TYPE_Q5_1:
  8847. case GGML_TYPE_Q8_0:
  8848. case GGML_TYPE_Q2_K:
  8849. case GGML_TYPE_Q3_K:
  8850. case GGML_TYPE_Q4_K:
  8851. case GGML_TYPE_Q5_K:
  8852. case GGML_TYPE_Q6_K:
  8853. case GGML_TYPE_IQ1_S:
  8854. case GGML_TYPE_IQ1_M:
  8855. case GGML_TYPE_IQ2_XXS:
  8856. case GGML_TYPE_IQ2_XS:
  8857. case GGML_TYPE_IQ2_S:
  8858. case GGML_TYPE_IQ3_XXS:
  8859. case GGML_TYPE_IQ3_S:
  8860. case GGML_TYPE_IQ4_XS:
  8861. case GGML_TYPE_IQ4_NL:
  8862. break;
  8863. default:
  8864. return false;
  8865. }
  8866. struct ggml_tensor * a;
  8867. struct ggml_tensor * b;
  8868. if (op->op == GGML_OP_MUL_MAT) {
  8869. a = op->src[0];
  8870. b = op->src[1];
  8871. } else {
  8872. a = op->src[2];
  8873. b = op->src[1];
  8874. }
  8875. if (a->ne[3] != b->ne[3]) {
  8876. return false;
  8877. }
  8878. 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) ||
  8879. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  8880. return false;
  8881. }
  8882. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  8883. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  8884. // So don't support this combination for now.
  8885. return false;
  8886. }
  8887. return true;
  8888. } break;
  8889. case GGML_OP_FLASH_ATTN_EXT:
  8890. {
  8891. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8892. auto device = ggml_vk_get_device(ctx->device);
  8893. bool coopmat2 = device->coopmat2;
  8894. FaHeadSizes head_sizes = fa_get_head_sizes(op->src[1]->ne[0], op->src[2]->ne[0]);
  8895. if (head_sizes == FA_HEAD_SIZE_UNSUPPORTED) {
  8896. return false;
  8897. }
  8898. if (op->src[0]->type != GGML_TYPE_F32) {
  8899. return false;
  8900. }
  8901. if (op->type != GGML_TYPE_F32) {
  8902. return false;
  8903. }
  8904. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  8905. return false;
  8906. }
  8907. // It's straightforward to support different K/V dequant, but would
  8908. // significantly increase the number of pipelines
  8909. if (op->src[1]->type != op->src[2]->type) {
  8910. return false;
  8911. }
  8912. switch (op->src[1]->type) {
  8913. case GGML_TYPE_F16:
  8914. case GGML_TYPE_Q4_0:
  8915. case GGML_TYPE_Q8_0:
  8916. // supported in scalar and coopmat2 paths
  8917. break;
  8918. case GGML_TYPE_Q4_1:
  8919. case GGML_TYPE_Q5_0:
  8920. case GGML_TYPE_Q5_1:
  8921. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  8922. //case GGML_TYPE_Q2_K:
  8923. //case GGML_TYPE_Q3_K:
  8924. //case GGML_TYPE_Q4_K:
  8925. //case GGML_TYPE_Q5_K:
  8926. //case GGML_TYPE_Q6_K:
  8927. //case GGML_TYPE_IQ1_S:
  8928. //case GGML_TYPE_IQ1_M:
  8929. //case GGML_TYPE_IQ2_XXS:
  8930. //case GGML_TYPE_IQ2_XS:
  8931. //case GGML_TYPE_IQ2_S:
  8932. //case GGML_TYPE_IQ3_XXS:
  8933. //case GGML_TYPE_IQ3_S:
  8934. //case GGML_TYPE_IQ4_XS:
  8935. case GGML_TYPE_IQ4_NL:
  8936. // currently supported only in coopmat2 path
  8937. if (!coopmat2) {
  8938. return false;
  8939. }
  8940. break;
  8941. default:
  8942. return false;
  8943. }
  8944. if (!coopmat2 && !device->subgroup_shuffle) {
  8945. // scalar FA uses subgroupShuffle
  8946. return false;
  8947. }
  8948. return true;
  8949. }
  8950. case GGML_OP_GET_ROWS:
  8951. {
  8952. switch (op->src[0]->type) {
  8953. case GGML_TYPE_F32:
  8954. case GGML_TYPE_F16:
  8955. case GGML_TYPE_BF16:
  8956. case GGML_TYPE_Q4_0:
  8957. case GGML_TYPE_Q4_1:
  8958. case GGML_TYPE_Q5_0:
  8959. case GGML_TYPE_Q5_1:
  8960. case GGML_TYPE_Q8_0:
  8961. case GGML_TYPE_IQ1_S:
  8962. case GGML_TYPE_IQ1_M:
  8963. case GGML_TYPE_IQ2_XXS:
  8964. case GGML_TYPE_IQ2_XS:
  8965. case GGML_TYPE_IQ2_S:
  8966. case GGML_TYPE_IQ3_XXS:
  8967. case GGML_TYPE_IQ3_S:
  8968. case GGML_TYPE_IQ4_XS:
  8969. case GGML_TYPE_IQ4_NL:
  8970. return true;
  8971. default:
  8972. return false;
  8973. }
  8974. } break;
  8975. case GGML_OP_SET_ROWS:
  8976. {
  8977. switch (op->type) {
  8978. case GGML_TYPE_F32:
  8979. case GGML_TYPE_F16:
  8980. case GGML_TYPE_BF16:
  8981. case GGML_TYPE_Q4_0:
  8982. case GGML_TYPE_Q4_1:
  8983. case GGML_TYPE_Q5_0:
  8984. case GGML_TYPE_Q5_1:
  8985. case GGML_TYPE_Q8_0:
  8986. case GGML_TYPE_IQ4_NL:
  8987. return true;
  8988. default:
  8989. return false;
  8990. }
  8991. } break;
  8992. case GGML_OP_CONT:
  8993. case GGML_OP_CPY:
  8994. case GGML_OP_DUP:
  8995. {
  8996. ggml_type src0_type = op->src[0]->type;
  8997. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  8998. if (src0_type == GGML_TYPE_F32) {
  8999. switch (src1_type) {
  9000. case GGML_TYPE_F32:
  9001. case GGML_TYPE_F16:
  9002. case GGML_TYPE_BF16:
  9003. case GGML_TYPE_Q4_0:
  9004. case GGML_TYPE_Q4_1:
  9005. case GGML_TYPE_Q5_0:
  9006. case GGML_TYPE_Q5_1:
  9007. case GGML_TYPE_Q8_0:
  9008. case GGML_TYPE_IQ4_NL:
  9009. return true;
  9010. default:
  9011. break;
  9012. }
  9013. }
  9014. if (src1_type == GGML_TYPE_F32) {
  9015. switch (src0_type) {
  9016. case GGML_TYPE_F16:
  9017. case GGML_TYPE_Q4_0:
  9018. case GGML_TYPE_Q4_1:
  9019. case GGML_TYPE_Q5_0:
  9020. case GGML_TYPE_Q5_1:
  9021. case GGML_TYPE_Q8_0:
  9022. case GGML_TYPE_IQ4_NL:
  9023. return true;
  9024. default:
  9025. break;
  9026. }
  9027. }
  9028. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9029. return true;
  9030. }
  9031. // We can handle copying from a type to the same type if it's
  9032. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  9033. // so the type/block size must be a multiple of 4.
  9034. if (src0_type == src1_type &&
  9035. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  9036. (ggml_type_size(src0_type) % 2) == 0) {
  9037. return true;
  9038. }
  9039. return false;
  9040. } break;
  9041. case GGML_OP_REPEAT:
  9042. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  9043. case GGML_OP_REPEAT_BACK:
  9044. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  9045. case GGML_OP_ROPE:
  9046. case GGML_OP_ROPE_BACK:
  9047. case GGML_OP_NONE:
  9048. case GGML_OP_RESHAPE:
  9049. case GGML_OP_VIEW:
  9050. case GGML_OP_PERMUTE:
  9051. case GGML_OP_TRANSPOSE:
  9052. case GGML_OP_RMS_NORM:
  9053. return true;
  9054. case GGML_OP_NORM:
  9055. case GGML_OP_GROUP_NORM:
  9056. case GGML_OP_L2_NORM:
  9057. return ggml_is_contiguous(op->src[0]);
  9058. case GGML_OP_ADD:
  9059. case GGML_OP_SUB:
  9060. case GGML_OP_MUL:
  9061. case GGML_OP_DIV:
  9062. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  9063. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  9064. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  9065. case GGML_OP_SILU_BACK:
  9066. case GGML_OP_RMS_NORM_BACK:
  9067. case GGML_OP_SQR:
  9068. case GGML_OP_SIN:
  9069. case GGML_OP_COS:
  9070. case GGML_OP_CLAMP:
  9071. return op->src[0]->type == GGML_TYPE_F32;
  9072. case GGML_OP_UPSCALE:
  9073. case GGML_OP_ACC:
  9074. case GGML_OP_CONCAT:
  9075. case GGML_OP_SCALE:
  9076. case GGML_OP_PAD:
  9077. case GGML_OP_ROLL:
  9078. case GGML_OP_DIAG_MASK_INF:
  9079. case GGML_OP_SOFT_MAX:
  9080. case GGML_OP_SOFT_MAX_BACK:
  9081. case GGML_OP_ARGSORT:
  9082. case GGML_OP_SUM:
  9083. case GGML_OP_SUM_ROWS:
  9084. case GGML_OP_ARGMAX:
  9085. case GGML_OP_COUNT_EQUAL:
  9086. case GGML_OP_IM2COL:
  9087. case GGML_OP_TIMESTEP_EMBEDDING:
  9088. case GGML_OP_CONV_2D_DW:
  9089. case GGML_OP_POOL_2D:
  9090. case GGML_OP_RWKV_WKV6:
  9091. case GGML_OP_RWKV_WKV7:
  9092. case GGML_OP_LEAKY_RELU:
  9093. case GGML_OP_OPT_STEP_ADAMW:
  9094. return true;
  9095. case GGML_OP_CONV_TRANSPOSE_1D:
  9096. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  9097. default:
  9098. return false;
  9099. }
  9100. UNUSED(dev);
  9101. }
  9102. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  9103. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  9104. return false;
  9105. }
  9106. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  9107. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  9108. return buft_ctx->device->idx == ctx->device;
  9109. }
  9110. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  9111. const int min_batch_size = 32;
  9112. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  9113. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  9114. UNUSED(dev);
  9115. }
  9116. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  9117. /* .get_name = */ ggml_backend_vk_device_get_name,
  9118. /* .get_description = */ ggml_backend_vk_device_get_description,
  9119. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  9120. /* .get_type = */ ggml_backend_vk_device_get_type,
  9121. /* .get_props = */ ggml_backend_vk_device_get_props,
  9122. /* .init_backend = */ ggml_backend_vk_device_init,
  9123. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  9124. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  9125. /* .buffer_from_host_ptr = */ NULL,
  9126. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  9127. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  9128. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  9129. /* .event_new = */ NULL,
  9130. /* .event_free = */ NULL,
  9131. /* .event_synchronize = */ NULL,
  9132. };
  9133. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  9134. UNUSED(reg);
  9135. return GGML_VK_NAME;
  9136. }
  9137. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  9138. UNUSED(reg);
  9139. return ggml_backend_vk_get_device_count();
  9140. }
  9141. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  9142. static std::vector<ggml_backend_dev_t> devices;
  9143. static bool initialized = false;
  9144. {
  9145. static std::mutex mutex;
  9146. std::lock_guard<std::mutex> lock(mutex);
  9147. if (!initialized) {
  9148. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  9149. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  9150. char desc[256];
  9151. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  9152. ctx->device = i;
  9153. ctx->name = GGML_VK_NAME + std::to_string(i);
  9154. ctx->description = desc;
  9155. devices.push_back(new ggml_backend_device {
  9156. /* .iface = */ ggml_backend_vk_device_i,
  9157. /* .reg = */ reg,
  9158. /* .context = */ ctx,
  9159. });
  9160. }
  9161. initialized = true;
  9162. }
  9163. }
  9164. GGML_ASSERT(device < devices.size());
  9165. return devices[device];
  9166. }
  9167. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  9168. /* .get_name = */ ggml_backend_vk_reg_get_name,
  9169. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  9170. /* .get_device = */ ggml_backend_vk_reg_get_device,
  9171. /* .get_proc_address = */ NULL,
  9172. };
  9173. ggml_backend_reg_t ggml_backend_vk_reg() {
  9174. static ggml_backend_reg reg = {
  9175. /* .api_version = */ GGML_BACKEND_API_VERSION,
  9176. /* .iface = */ ggml_backend_vk_reg_i,
  9177. /* .context = */ nullptr,
  9178. };
  9179. try {
  9180. ggml_vk_instance_init();
  9181. return &reg;
  9182. } catch (const vk::SystemError& e) {
  9183. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  9184. return nullptr;
  9185. }
  9186. }
  9187. // Extension availability
  9188. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9189. #ifdef GGML_VULKAN_VALIDATE
  9190. bool portability_enumeration_ext = false;
  9191. // Check for portability enumeration extension for MoltenVK support
  9192. for (const auto& properties : instance_extensions) {
  9193. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9194. return true;
  9195. }
  9196. }
  9197. if (!portability_enumeration_ext) {
  9198. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9199. }
  9200. #endif
  9201. return false;
  9202. UNUSED(instance_extensions);
  9203. }
  9204. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9205. #ifdef __APPLE__
  9206. bool portability_enumeration_ext = false;
  9207. // Check for portability enumeration extension for MoltenVK support
  9208. for (const auto& properties : instance_extensions) {
  9209. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9210. return true;
  9211. }
  9212. }
  9213. if (!portability_enumeration_ext) {
  9214. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9215. }
  9216. #endif
  9217. return false;
  9218. UNUSED(instance_extensions);
  9219. }
  9220. // Extension availability
  9221. static bool ggml_vk_instance_debug_utils_ext_available(
  9222. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  9223. // Check for portability enumeration extension for MoltenVK support
  9224. for (const auto & properties : instance_extensions) {
  9225. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  9226. return true;
  9227. }
  9228. }
  9229. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  9230. return false;
  9231. UNUSED(instance_extensions);
  9232. }
  9233. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  9234. switch (props.vendorID) {
  9235. case VK_VENDOR_ID_INTEL:
  9236. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  9237. // while some older hardware (ex. Arc A770) has performance regressions
  9238. return arch == vk_device_architecture::INTEL_XE2;
  9239. case VK_VENDOR_ID_AMD:
  9240. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  9241. // Workaround for AMD proprietary driver reporting support on all GPUs
  9242. return arch == vk_device_architecture::AMD_RDNA3;
  9243. }
  9244. return true;
  9245. default:
  9246. return true;
  9247. }
  9248. }
  9249. // checks
  9250. #ifdef GGML_VULKAN_CHECK_RESULTS
  9251. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  9252. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  9253. return;
  9254. }
  9255. for (int j = 0; j < level; j++) {
  9256. std::cerr << " ";
  9257. }
  9258. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  9259. done.push_back(tensor);
  9260. for (int i = 0; i < GGML_MAX_SRC; i++) {
  9261. if (tensor->src[i] != nullptr) {
  9262. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  9263. }
  9264. }
  9265. }
  9266. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  9267. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  9268. return;
  9269. }
  9270. i0 = std::max(i0, 5);
  9271. i1 = std::max(i1, 5);
  9272. i2 = std::max(i2, 0);
  9273. i3 = std::max(i3, 0);
  9274. fprintf(stderr, " ");
  9275. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9276. fprintf(stderr, "%7d ", idx1);
  9277. }
  9278. fprintf(stderr, "\n");
  9279. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9280. fprintf(stderr, "%7d: ", idx0);
  9281. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9282. 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]) {
  9283. float val;
  9284. if (tensor->type == GGML_TYPE_F32) {
  9285. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9286. } else if (tensor->type == GGML_TYPE_F16) {
  9287. 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]));
  9288. } else if (tensor->type == GGML_TYPE_I32) {
  9289. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9290. } else {
  9291. GGML_ABORT("fatal error");
  9292. }
  9293. fprintf(stderr, "% 7.2f ", val);
  9294. } else {
  9295. fprintf(stderr, " ");
  9296. }
  9297. }
  9298. fprintf(stderr, "\n");
  9299. }
  9300. }
  9301. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  9302. void * tensor_data = tensor->data;
  9303. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  9304. if (is_gpu) {
  9305. const size_t tensor_size = ggml_nbytes(tensor);
  9306. tensor_data = malloc(tensor_size);
  9307. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9308. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  9309. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  9310. }
  9311. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  9312. 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;
  9313. if (tensor->src[0] != nullptr) {
  9314. 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;
  9315. }
  9316. if (tensor->src[1] != nullptr) {
  9317. 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;
  9318. }
  9319. std::cerr << std::endl << "Result:" << std::endl;
  9320. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9321. std::cerr << std::endl;
  9322. std::vector<const ggml_tensor *> done;
  9323. ggml_vk_print_graph_origin(tensor, done);
  9324. if (is_gpu) {
  9325. free(tensor_data);
  9326. }
  9327. }
  9328. void * comp_result;
  9329. size_t comp_size;
  9330. size_t comp_nb[GGML_MAX_DIMS];
  9331. size_t check_counter = 0;
  9332. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9333. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9334. if (tensor->op == GGML_OP_TRANSPOSE) {
  9335. return;
  9336. }
  9337. bool fused_rms_norm_mul = false;
  9338. int rms_norm_idx = -1;
  9339. if (ctx->num_additional_fused_ops == 1 &&
  9340. tensor->op == GGML_OP_RMS_NORM &&
  9341. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9342. fused_rms_norm_mul = true;
  9343. tensor = cgraph->nodes[tensor_idx + 1];
  9344. }
  9345. check_counter++;
  9346. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9347. return;
  9348. }
  9349. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  9350. ggml_tensor * src0 = tensor->src[0];
  9351. ggml_tensor * src1 = tensor->src[1];
  9352. struct ggml_init_params iparams = {
  9353. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  9354. /*.mem_buffer =*/ NULL,
  9355. /*.no_alloc =*/ false,
  9356. };
  9357. struct ggml_context * ggml_ctx = ggml_init(iparams);
  9358. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9359. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  9360. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9361. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  9362. struct ggml_tensor * tensor_clone = nullptr;
  9363. for (int i = 0; i < 6; i++) {
  9364. ggml_tensor * srci = tensor->src[i];
  9365. if (fused_rms_norm_mul) {
  9366. rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
  9367. ggml_tensor *rms_norm = tensor->src[rms_norm_idx];
  9368. switch (i) {
  9369. case 0: srci = rms_norm->src[0]; break;
  9370. case 1: srci = tensor->src[1 - rms_norm_idx]; break;
  9371. default: continue;
  9372. }
  9373. }
  9374. if (srci == nullptr) {
  9375. continue;
  9376. }
  9377. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  9378. size_t srci_size = ggml_nbytes(srci);
  9379. src_clone[i] = srci_clone;
  9380. src_size[i] = ggml_nbytes(srci);
  9381. src_buffer[i] = malloc(srci_size);
  9382. srci_clone->data = src_buffer[i];
  9383. if (ggml_backend_buffer_is_host(srci->buffer)) {
  9384. memcpy(srci_clone->data, srci->data, srci_size);
  9385. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9386. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  9387. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  9388. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9389. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  9390. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  9391. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  9392. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  9393. const int idx = i3*srci->ne[2] + i2;
  9394. 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]);
  9395. }
  9396. }
  9397. srci_clone->nb[0] = srci->nb[0];
  9398. srci_clone->nb[1] = srci->nb[1];
  9399. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  9400. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  9401. }
  9402. } else {
  9403. if (offset + srci_size >= buffer_gpu->size) {
  9404. srci_size = buffer_gpu->size - offset;
  9405. }
  9406. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  9407. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9408. }
  9409. } else {
  9410. GGML_ABORT("fatal error");
  9411. }
  9412. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9413. ggml_vk_print_tensor(srci, srci_name[i]);
  9414. }
  9415. }
  9416. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  9417. const float * params = (const float *)tensor->op_params;
  9418. 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]);
  9419. } else if (tensor->op == GGML_OP_MUL_MAT) {
  9420. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  9421. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  9422. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  9423. } else if (tensor->op == GGML_OP_SUB) {
  9424. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  9425. } else if (tensor->op == GGML_OP_MUL) {
  9426. if (fused_rms_norm_mul) {
  9427. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->src[rms_norm_idx]->op_params);
  9428. tensor_clone = ggml_mul(ggml_ctx, tensor_clone, src_clone[1 - rms_norm_idx]);
  9429. } else {
  9430. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  9431. }
  9432. } else if (tensor->op == GGML_OP_DIV) {
  9433. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  9434. } else if (tensor->op == GGML_OP_CONCAT) {
  9435. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  9436. } else if (tensor->op == GGML_OP_UPSCALE) {
  9437. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], (ggml_scale_mode) tensor->op_params[0]);
  9438. } else if (tensor->op == GGML_OP_SCALE) {
  9439. const float * params = (const float *)tensor->op_params;
  9440. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], params[0]);
  9441. } else if (tensor->op == GGML_OP_SQR) {
  9442. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  9443. } else if (tensor->op == GGML_OP_SIN) {
  9444. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  9445. } else if (tensor->op == GGML_OP_COS) {
  9446. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  9447. } else if (tensor->op == GGML_OP_CLAMP) {
  9448. const float * params = (const float *)tensor->op_params;
  9449. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  9450. } else if (tensor->op == GGML_OP_PAD) {
  9451. tensor_clone = ggml_pad(ggml_ctx, src_clone[0], tensor->ne[0] - src_clone[0]->ne[0], tensor->ne[1] - src_clone[0]->ne[1], tensor->ne[2] - src_clone[0]->ne[2], tensor->ne[3] - src_clone[0]->ne[3]);
  9452. } else if (tensor->op == GGML_OP_REPEAT) {
  9453. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  9454. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  9455. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  9456. } else if (tensor->op == GGML_OP_ADD) {
  9457. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  9458. } else if (tensor->op == GGML_OP_ACC) {
  9459. 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]);
  9460. } else if (tensor->op == GGML_OP_NORM) {
  9461. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9462. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  9463. const float * float_params = (const float *)tensor->op_params;
  9464. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  9465. } else if (tensor->op == GGML_OP_RMS_NORM) {
  9466. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9467. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  9468. const float eps = ((float *) tensor->op_params)[0];
  9469. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  9470. } else if (tensor->op == GGML_OP_SILU_BACK) {
  9471. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  9472. } else if (tensor->op == GGML_OP_L2_NORM) {
  9473. const float eps = ((float *) tensor->op_params)[0];
  9474. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  9475. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  9476. if (src1 != nullptr) {
  9477. const float * params = (const float *)tensor->op_params;
  9478. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  9479. } else {
  9480. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  9481. }
  9482. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  9483. 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]);
  9484. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  9485. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  9486. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  9487. const int n_dims = ((int32_t *) tensor->op_params)[1];
  9488. const int mode = ((int32_t *) tensor->op_params)[2];
  9489. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  9490. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  9491. const float freq_base = ((float *) tensor->op_params)[5];
  9492. const float freq_scale = ((float *) tensor->op_params)[6];
  9493. const float ext_factor = ((float *) tensor->op_params)[7];
  9494. const float attn_factor = ((float *) tensor->op_params)[8];
  9495. const float beta_fast = ((float *) tensor->op_params)[9];
  9496. const float beta_slow = ((float *) tensor->op_params)[10];
  9497. if (mode & GGML_ROPE_TYPE_MROPE) {
  9498. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  9499. if (tensor->op == GGML_OP_ROPE) {
  9500. 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);
  9501. } else {
  9502. 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);
  9503. }
  9504. } else {
  9505. if (tensor->op == GGML_OP_ROPE) {
  9506. 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);
  9507. } else {
  9508. 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);
  9509. }
  9510. }
  9511. } else if (tensor->op == GGML_OP_UNARY) {
  9512. switch (ggml_get_unary_op(tensor)) {
  9513. case GGML_UNARY_OP_SILU:
  9514. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  9515. break;
  9516. case GGML_UNARY_OP_GELU:
  9517. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  9518. break;
  9519. case GGML_UNARY_OP_GELU_ERF:
  9520. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  9521. break;
  9522. case GGML_UNARY_OP_GELU_QUICK:
  9523. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  9524. break;
  9525. case GGML_UNARY_OP_RELU:
  9526. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  9527. break;
  9528. case GGML_UNARY_OP_TANH:
  9529. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  9530. break;
  9531. case GGML_UNARY_OP_SIGMOID:
  9532. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  9533. break;
  9534. default:
  9535. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9536. GGML_ABORT("fatal error");
  9537. }
  9538. } else if (tensor->op == GGML_OP_GLU) {
  9539. if (src_clone[1] == nullptr) {
  9540. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  9541. } else {
  9542. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  9543. }
  9544. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  9545. if (src1 == nullptr) {
  9546. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  9547. tensor_clone->type = tensor->type;
  9548. } else {
  9549. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  9550. }
  9551. } else if (tensor->op == GGML_OP_SET_ROWS) {
  9552. tensor_clone = ggml_set_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9553. } else if (tensor->op == GGML_OP_CONT) {
  9554. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9555. } else if (tensor->op == GGML_OP_RESHAPE) {
  9556. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9557. } else if (tensor->op == GGML_OP_VIEW) {
  9558. 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]);
  9559. } else if (tensor->op == GGML_OP_PERMUTE) {
  9560. int32_t * params = (int32_t *)tensor->op_params;
  9561. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  9562. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  9563. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  9564. } else if (tensor->op == GGML_OP_GET_ROWS) {
  9565. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9566. } else if (tensor->op == GGML_OP_ARGSORT) {
  9567. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  9568. } else if (tensor->op == GGML_OP_SUM) {
  9569. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  9570. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  9571. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  9572. } else if (tensor->op == GGML_OP_ARGMAX) {
  9573. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  9574. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  9575. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  9576. } else if (tensor->op == GGML_OP_IM2COL) {
  9577. const int32_t s0 = tensor->op_params[0];
  9578. const int32_t s1 = tensor->op_params[1];
  9579. const int32_t p0 = tensor->op_params[2];
  9580. const int32_t p1 = tensor->op_params[3];
  9581. const int32_t d0 = tensor->op_params[4];
  9582. const int32_t d1 = tensor->op_params[5];
  9583. const bool is_2D = tensor->op_params[6] == 1;
  9584. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  9585. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  9586. const int32_t dim = tensor->op_params[0];
  9587. const int32_t max_period = tensor->op_params[1];
  9588. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  9589. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  9590. const int32_t s0 = tensor->op_params[0];
  9591. const int32_t p0 = tensor->op_params[1];
  9592. const int32_t d0 = tensor->op_params[2];
  9593. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  9594. } else if (tensor->op == GGML_OP_POOL_2D) {
  9595. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  9596. const int32_t k0 = tensor->op_params[1];
  9597. const int32_t k1 = tensor->op_params[2];
  9598. const int32_t s0 = tensor->op_params[3];
  9599. const int32_t s1 = tensor->op_params[4];
  9600. const int32_t p0 = tensor->op_params[5];
  9601. const int32_t p1 = tensor->op_params[6];
  9602. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  9603. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  9604. const float * op_params = (const float *)tensor->op_params;
  9605. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  9606. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  9607. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  9608. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  9609. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  9610. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  9611. src_clone[4], src_clone[5], src_clone[6]);
  9612. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  9613. src_clone[0]->flags = src0->flags;
  9614. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  9615. src_clone[2], src_clone[3], src_clone[4]);
  9616. }
  9617. else {
  9618. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9619. GGML_ABORT("fatal error");
  9620. }
  9621. ggml_cgraph * cgraph_cpu = ggml_new_graph(ggml_ctx);
  9622. ggml_build_forward_expand(cgraph_cpu, tensor_clone);
  9623. ggml_graph_compute_with_ctx(ggml_ctx, cgraph_cpu, 8);
  9624. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9625. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  9626. }
  9627. comp_size = ggml_nbytes(tensor_clone);
  9628. comp_result = malloc(comp_size);
  9629. memcpy(comp_result, tensor_clone->data, comp_size);
  9630. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9631. for (int i = 0; i < 6; i++) {
  9632. if (src_buffer[i] != nullptr) {
  9633. free(src_buffer[i]);
  9634. }
  9635. }
  9636. ggml_free(ggml_ctx);
  9637. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  9638. }
  9639. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int tensor_idx) {
  9640. ggml_tensor * tensor = cgraph->nodes[tensor_idx];
  9641. if (tensor->op == GGML_OP_TRANSPOSE) {
  9642. return;
  9643. }
  9644. bool fused_rms_norm_mul = false;
  9645. if (ctx->num_additional_fused_ops == 1 &&
  9646. tensor->op == GGML_OP_RMS_NORM &&
  9647. cgraph->nodes[tensor_idx + 1]->op == GGML_OP_MUL) {
  9648. fused_rms_norm_mul = true;
  9649. tensor = cgraph->nodes[tensor_idx + 1];
  9650. }
  9651. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9652. return;
  9653. }
  9654. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  9655. ggml_tensor * src0 = tensor->src[0];
  9656. ggml_tensor * src1 = tensor->src[1];
  9657. ggml_tensor * src2 = tensor->src[2];
  9658. ggml_tensor * src3 = tensor->src[3];
  9659. void * tensor_data = tensor->data;
  9660. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9661. size_t tensor_size = ggml_nbytes(tensor);
  9662. tensor_data = malloc(tensor_size);
  9663. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9664. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9665. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  9666. if (offset + tensor_size >= buffer_gpu->size) {
  9667. tensor_size = buffer_gpu->size - offset;
  9668. }
  9669. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  9670. }
  9671. float first_error_result = -1.0f;
  9672. float first_error_correct = -1.0f;
  9673. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  9674. double avg_err = 0.0;
  9675. size_t counter = 0;
  9676. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  9677. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  9678. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  9679. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  9680. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  9681. float correct = 0.0f;
  9682. float result = 0.0f;
  9683. if (buffer_size_fit) {
  9684. if (tensor->type == GGML_TYPE_F32) {
  9685. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9686. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9687. } else if (tensor->type == GGML_TYPE_F16) {
  9688. 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]));
  9689. 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]));
  9690. } else if (tensor->type == GGML_TYPE_I32) {
  9691. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9692. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9693. } else if (tensor->type == GGML_TYPE_I64) {
  9694. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9695. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9696. } else {
  9697. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  9698. }
  9699. } else {
  9700. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  9701. GGML_ABORT("fatal error");
  9702. }
  9703. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  9704. 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;
  9705. 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;
  9706. if (src0 != nullptr) {
  9707. 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;
  9708. }
  9709. if (src1 != nullptr) {
  9710. 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;
  9711. }
  9712. if (src2 != nullptr) {
  9713. 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;
  9714. }
  9715. if (src3 != nullptr) {
  9716. 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;
  9717. }
  9718. 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;
  9719. std::cerr << std::endl << "Result:" << std::endl;
  9720. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  9721. std::cerr << std::endl << "Correct:" << std::endl;
  9722. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  9723. std::cerr << std::endl;
  9724. std::vector<const ggml_tensor *> done;
  9725. ggml_vk_print_graph_origin(tensor, done);
  9726. GGML_ABORT("fatal error");
  9727. }
  9728. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  9729. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  9730. first_error[0] = i0;
  9731. first_error[1] = i1;
  9732. first_error[2] = i2;
  9733. first_error[3] = i3;
  9734. first_error_result = result;
  9735. first_error_correct = correct;
  9736. }
  9737. // Special case, value is infinite, avoid NaN result in avg_err
  9738. // NaN also appears in results, if both are nan error is 0
  9739. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  9740. avg_err += std::fabs(correct - result) / denom;
  9741. }
  9742. counter++;
  9743. }
  9744. }
  9745. }
  9746. }
  9747. avg_err /= counter;
  9748. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9749. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9750. 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;
  9751. if (src0 != nullptr) {
  9752. 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;
  9753. }
  9754. if (src1 != nullptr) {
  9755. 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;
  9756. }
  9757. if (src2 != nullptr) {
  9758. 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;
  9759. }
  9760. if (src3 != nullptr) {
  9761. 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;
  9762. }
  9763. 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;
  9764. std::cerr << std::endl << "Result:" << std::endl;
  9765. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9766. std::cerr << std::endl << "Correct:" << std::endl;
  9767. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  9768. std::cerr << std::endl;
  9769. std::vector<const ggml_tensor *> done;
  9770. ggml_vk_print_graph_origin(tensor, done);
  9771. }
  9772. if (avg_err > 0.5 || std::isnan(avg_err)) {
  9773. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9774. 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;
  9775. if (src0 != nullptr) {
  9776. 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;
  9777. }
  9778. if (src1 != nullptr) {
  9779. 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;
  9780. }
  9781. if (src2 != nullptr) {
  9782. 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;
  9783. }
  9784. if (src3 != nullptr) {
  9785. 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;
  9786. }
  9787. 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;
  9788. std::cerr << std::endl << "Result:" << std::endl;
  9789. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  9790. std::cerr << std::endl << "Correct:" << std::endl;
  9791. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  9792. std::cerr << std::endl;
  9793. std::vector<const ggml_tensor *> done;
  9794. ggml_vk_print_graph_origin(tensor, done);
  9795. GGML_ABORT("fatal error");
  9796. } else {
  9797. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  9798. }
  9799. free(comp_result);
  9800. comp_result = nullptr;
  9801. comp_size = 0;
  9802. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9803. free(tensor_data);
  9804. }
  9805. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  9806. }
  9807. #endif
  9808. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)