ggml-vulkan.cpp 562 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #if defined(_MSC_VER)
  25. # define NOMINMAX 1
  26. # include <windows.h>
  27. # define YIELD() YieldProcessor()
  28. #elif defined(__clang__) || defined(__GNUC__)
  29. # if defined(__x86_64__) ||defined(__i386__)
  30. # include <immintrin.h>
  31. # define YIELD() _mm_pause()
  32. # elif defined(__arm__) || defined(__aarch64__)
  33. # if defined(__clang__)
  34. # include <arm_acle.h>
  35. # define YIELD() __yield()
  36. # else
  37. # define YIELD() asm volatile("yield")
  38. # endif
  39. # endif
  40. #endif
  41. #if !defined(YIELD)
  42. #define YIELD()
  43. #endif
  44. #include "ggml-impl.h"
  45. #include "ggml-backend-impl.h"
  46. #include "ggml-vulkan-shaders.hpp"
  47. // remove this once it's more widely available in the SDK
  48. #if !defined(VK_KHR_shader_bfloat16)
  49. #define VK_KHR_shader_bfloat16 1
  50. #define VK_KHR_SHADER_BFLOAT16_SPEC_VERSION 1
  51. #define VK_KHR_SHADER_BFLOAT16_EXTENSION_NAME "VK_KHR_shader_bfloat16"
  52. #define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR ((VkStructureType)1000141000)
  53. #define VK_COMPONENT_TYPE_BFLOAT16_KHR ((VkComponentTypeKHR)1000141000)
  54. typedef struct VkPhysicalDeviceShaderBfloat16FeaturesKHR {
  55. VkStructureType sType;
  56. void* pNext;
  57. VkBool32 shaderBFloat16Type;
  58. VkBool32 shaderBFloat16DotProduct;
  59. VkBool32 shaderBFloat16CooperativeMatrix;
  60. } VkPhysicalDeviceShaderBfloat16FeaturesKHR;
  61. #endif
  62. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  63. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  64. static bool is_pow2(uint32_t x) { return x > 1 && (x & (x-1)) == 0; }
  65. #define VK_VENDOR_ID_AMD 0x1002
  66. #define VK_VENDOR_ID_APPLE 0x106b
  67. #define VK_VENDOR_ID_INTEL 0x8086
  68. #define VK_VENDOR_ID_NVIDIA 0x10de
  69. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 256
  70. #define GGML_VK_MAX_NODES 8192
  71. #define MAX_VK_BUFFERS 256
  72. #define VK_CHECK(err, msg) \
  73. do { \
  74. vk::Result err_ = (err); \
  75. if (err_ != vk::Result::eSuccess) { \
  76. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  77. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  78. exit(1); \
  79. } \
  80. } while (0)
  81. #ifdef GGML_VULKAN_DEBUG
  82. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  83. #else
  84. #define VK_LOG_DEBUG(msg) ((void) 0)
  85. #endif // GGML_VULKAN_DEBUG
  86. struct ggml_backend_vk_context;
  87. #define MAX_PARAMETER_COUNT 8
  88. struct vk_pipeline_struct {
  89. std::string name;
  90. vk::ShaderModule shader_module;
  91. vk::PipelineLayout layout;
  92. vk::Pipeline pipeline;
  93. uint32_t push_constant_size;
  94. uint32_t parameter_count;
  95. std::array<uint32_t, 3> wg_denoms;
  96. uint32_t align;
  97. // set to true to request the pipeline is compiled after the dryrun
  98. bool needed {};
  99. // set to true when the shader has been compiled
  100. bool compiled {};
  101. };
  102. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  103. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  104. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  105. struct vk_matmul_pipeline_struct {
  106. vk_pipeline l, m, s;
  107. vk_pipeline a_l, a_m, a_s;
  108. };
  109. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  110. struct vk_matmul_pipeline2 {
  111. vk_matmul_pipeline2() {
  112. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  113. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  114. }
  115. vk_matmul_pipeline f32acc;
  116. vk_matmul_pipeline f16acc;
  117. };
  118. struct vk_device_struct;
  119. typedef std::shared_ptr<vk_device_struct> vk_device;
  120. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  121. struct vk_buffer_struct;
  122. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  123. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  124. struct ggml_backend_vk_buffer_type_context {
  125. std::string name;
  126. vk_device device;
  127. };
  128. struct vk_queue;
  129. // Stores command pool/buffers. There's an instance of this
  130. // for each (context,queue) pair and for each (device,queue) pair.
  131. struct vk_command_pool {
  132. void init(vk_device& device, vk_queue *q_);
  133. void destroy(vk::Device& device);
  134. vk::CommandPool pool;
  135. uint32_t cmd_buffer_idx;
  136. std::vector<vk::CommandBuffer> cmd_buffers;
  137. vk_queue *q;
  138. };
  139. // Prevent simultaneous submissions to the same queue.
  140. // This could be per vk_queue if we stopped having two vk_queue structures
  141. // sharing the same vk::Queue.
  142. static std::mutex queue_mutex;
  143. struct vk_queue {
  144. uint32_t queue_family_index;
  145. vk::Queue queue;
  146. vk_command_pool cmd_pool;
  147. vk::PipelineStageFlags stage_flags;
  148. bool transfer_only;
  149. // copy everything except the cmd_pool
  150. void copyFrom(vk_queue &other) {
  151. queue_family_index = other.queue_family_index;
  152. queue = other.queue;
  153. stage_flags = other.stage_flags;
  154. transfer_only = other.transfer_only;
  155. }
  156. };
  157. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  158. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  159. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  160. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  161. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  162. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  163. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  164. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  165. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  166. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  167. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  168. /* .is_host = */ NULL,
  169. };
  170. #ifdef GGML_VULKAN_MEMORY_DEBUG
  171. class vk_memory_logger;
  172. #endif
  173. class vk_perf_logger;
  174. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  175. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  176. static constexpr uint32_t p021_max_gqa_ratio = 8;
  177. enum vk_device_architecture {
  178. OTHER,
  179. AMD_GCN,
  180. AMD_RDNA1,
  181. AMD_RDNA2,
  182. AMD_RDNA3,
  183. INTEL_XE2,
  184. };
  185. // HSK x HSV
  186. enum FaHeadSizes {
  187. FA_HEAD_SIZE_64,
  188. FA_HEAD_SIZE_80,
  189. FA_HEAD_SIZE_96,
  190. FA_HEAD_SIZE_112,
  191. FA_HEAD_SIZE_128,
  192. FA_HEAD_SIZE_192,
  193. FA_HEAD_SIZE_192_128,
  194. FA_HEAD_SIZE_256,
  195. FA_HEAD_SIZE_576_512,
  196. FA_HEAD_SIZE_UNSUPPORTED,
  197. FA_HEAD_SIZE_COUNT = FA_HEAD_SIZE_UNSUPPORTED,
  198. };
  199. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  200. vk::PhysicalDeviceProperties props = device.getProperties();
  201. if (props.vendorID == VK_VENDOR_ID_AMD) {
  202. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  203. bool amd_shader_core_properties = false;
  204. bool integer_dot_product = false;
  205. bool subgroup_size_control = false;
  206. for (const auto& properties : ext_props) {
  207. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  208. amd_shader_core_properties = true;
  209. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  210. integer_dot_product = true;
  211. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  212. subgroup_size_control = true;
  213. }
  214. }
  215. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  216. return vk_device_architecture::OTHER;
  217. }
  218. vk::PhysicalDeviceProperties2 props2;
  219. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  220. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  221. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  222. props2.pNext = &shader_core_props_amd;
  223. shader_core_props_amd.pNext = &integer_dot_props;
  224. integer_dot_props.pNext = &subgroup_size_control_props;
  225. device.getProperties2(&props2);
  226. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  227. return vk_device_architecture::AMD_GCN;
  228. }
  229. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  230. // RDNA
  231. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  232. return vk_device_architecture::AMD_RDNA1;
  233. }
  234. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  235. return vk_device_architecture::AMD_RDNA3;
  236. }
  237. return vk_device_architecture::AMD_RDNA2;
  238. }
  239. } else if (props.vendorID == VK_VENDOR_ID_INTEL) {
  240. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  241. bool subgroup_size_control = false;
  242. for (const auto& properties : ext_props) {
  243. if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  244. subgroup_size_control = true;
  245. }
  246. }
  247. if (!subgroup_size_control) {
  248. return vk_device_architecture::OTHER;
  249. }
  250. vk::PhysicalDeviceProperties2 props2;
  251. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  252. props2.pNext = &subgroup_size_control_props;
  253. device.getProperties2(&props2);
  254. if (subgroup_size_control_props.minSubgroupSize == 16) {
  255. // Xe2 architecture uses SIMD16 while previous Xe and Gen architecture uses SIMD8.
  256. // Minimum subgroup size matches the SIMD width so we distinguish architecture by checking this value.
  257. // https://www.intel.com/content/www/us/en/content-details/824434/2024-intel-tech-tour-xe2-and-lunar-lake-s-gpu.html
  258. // https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2025-0/intel-xe-gpu-architecture.html
  259. return vk_device_architecture::INTEL_XE2;
  260. }
  261. }
  262. return vk_device_architecture::OTHER;
  263. }
  264. struct vk_device_struct {
  265. std::recursive_mutex mutex;
  266. vk::PhysicalDevice physical_device;
  267. vk::PhysicalDeviceProperties properties;
  268. std::string name;
  269. uint64_t max_memory_allocation_size;
  270. uint64_t suballocation_block_size;
  271. bool fp16;
  272. bool pipeline_robustness;
  273. vk::Device device;
  274. uint32_t vendor_id;
  275. vk::DriverId driver_id;
  276. vk_device_architecture architecture;
  277. vk_queue compute_queue;
  278. vk_queue transfer_queue;
  279. bool single_queue;
  280. uint32_t subgroup_size;
  281. uint32_t shader_core_count;
  282. bool uma;
  283. bool prefer_host_memory;
  284. bool float_controls_rte_fp16;
  285. bool subgroup_add;
  286. bool subgroup_shuffle;
  287. bool integer_dot_product;
  288. bool subgroup_size_control;
  289. uint32_t subgroup_min_size;
  290. uint32_t subgroup_max_size;
  291. bool subgroup_require_full_support;
  292. bool coopmat_support;
  293. bool coopmat_acc_f32_support {};
  294. bool coopmat_acc_f16_support {};
  295. bool coopmat_bf16_support {};
  296. bool coopmat_support_16x16x16_f16acc {};
  297. bool coopmat_support_16x16x16_f32acc {};
  298. bool coopmat1_fa_support {};
  299. uint32_t coopmat_m;
  300. uint32_t coopmat_n;
  301. uint32_t coopmat_k;
  302. bool coopmat_int_support;
  303. uint32_t coopmat_int_m;
  304. uint32_t coopmat_int_n;
  305. uint32_t coopmat_int_k;
  306. bool coopmat2;
  307. size_t idx;
  308. bool mul_mat_l[GGML_TYPE_COUNT];
  309. bool mul_mat_m[GGML_TYPE_COUNT];
  310. bool mul_mat_s[GGML_TYPE_COUNT];
  311. bool mul_mat_id_l[GGML_TYPE_COUNT];
  312. bool mul_mat_id_m[GGML_TYPE_COUNT];
  313. bool mul_mat_id_s[GGML_TYPE_COUNT];
  314. // set to true to indicate that some shaders need to be compiled after the dryrun
  315. bool need_compiles {};
  316. vk::DescriptorSetLayout dsl;
  317. vk_matmul_pipeline pipeline_matmul_f32 {};
  318. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  319. vk_matmul_pipeline pipeline_matmul_bf16 {};
  320. vk_matmul_pipeline2 pipeline_matmul_f16;
  321. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  322. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  323. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  324. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  325. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  326. vk_matmul_pipeline pipeline_matmul_id_bf16 {};
  327. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  328. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  329. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  330. vk_pipeline pipeline_matmul_split_k_reduce;
  331. vk_pipeline pipeline_quantize_q8_1;
  332. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  333. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  334. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  335. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  336. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  337. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  338. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  339. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  340. vk_pipeline pipeline_acc_f32;
  341. // [src0 0=fp32,1=fp16][src1 0=fp32,1=fp16][dst 0=fp32,1=fp16]
  342. vk_pipeline pipeline_add[2][2][2];
  343. vk_pipeline pipeline_add_norepeat[2][2][2];
  344. vk_pipeline pipeline_sub[2][2][2];
  345. vk_pipeline pipeline_sub_norepeat[2][2][2];
  346. vk_pipeline pipeline_mul[2][2][2];
  347. vk_pipeline pipeline_mul_norepeat[2][2][2];
  348. vk_pipeline pipeline_div[2][2][2];
  349. vk_pipeline pipeline_div_norepeat[2][2][2];
  350. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  351. vk_pipeline pipeline_upscale_f32;
  352. vk_pipeline pipeline_scale_f32;
  353. vk_pipeline pipeline_sqr_f32;
  354. vk_pipeline pipeline_sin_f32;
  355. vk_pipeline pipeline_cos_f32;
  356. vk_pipeline pipeline_clamp_f32;
  357. vk_pipeline pipeline_pad_f32;
  358. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  359. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16;
  360. 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;
  361. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  362. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  363. vk_pipeline pipeline_norm_f32;
  364. vk_pipeline pipeline_group_norm_f32;
  365. vk_pipeline pipeline_rms_norm_f32;
  366. vk_pipeline pipeline_rms_norm_mul_f32;
  367. vk_pipeline pipeline_rms_norm_back_f32;
  368. vk_pipeline pipeline_l2_norm_f32;
  369. // [src/dst 0=fp32,1=fp16]
  370. vk_pipeline pipeline_gelu[2];
  371. vk_pipeline pipeline_gelu_erf[2];
  372. vk_pipeline pipeline_gelu_quick[2];
  373. vk_pipeline pipeline_silu[2];
  374. vk_pipeline pipeline_relu[2];
  375. vk_pipeline pipeline_tanh[2];
  376. vk_pipeline pipeline_sigmoid[2];
  377. vk_pipeline pipeline_geglu[2];
  378. vk_pipeline pipeline_reglu[2];
  379. vk_pipeline pipeline_swiglu[2];
  380. vk_pipeline pipeline_geglu_erf[2];
  381. vk_pipeline pipeline_geglu_quick[2];
  382. vk_pipeline pipeline_leaky_relu_f32;
  383. vk_pipeline pipeline_silu_back_f32;
  384. vk_pipeline pipeline_diag_mask_inf_f32;
  385. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  386. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  387. vk_pipeline pipeline_soft_max_back_f32;
  388. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  389. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  390. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  391. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  392. vk_pipeline pipeline_argsort_f32;
  393. vk_pipeline pipeline_sum_rows_f32;
  394. vk_pipeline pipeline_argmax_f32;
  395. vk_pipeline pipeline_count_equal_i32;
  396. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  397. vk_pipeline pipeline_timestep_embedding_f32;
  398. vk_pipeline pipeline_conv_transpose_1d_f32;
  399. vk_pipeline pipeline_pool2d_f32;
  400. vk_pipeline pipeline_rwkv_wkv6_f32;
  401. vk_pipeline pipeline_rwkv_wkv7_f32;
  402. vk_pipeline pipeline_opt_step_adamw_f32;
  403. vk_pipeline pipeline_conv2d_dw_whcn_f32;
  404. vk_pipeline pipeline_conv2d_dw_cwhn_f32;
  405. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  406. vk_pipeline pipeline_flash_attn_f32_f16_cm2[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  407. vk_pipeline pipeline_flash_attn_f32_f16_cm1[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  408. vk_pipeline pipeline_flash_attn_f32_f16[GGML_TYPE_COUNT][FA_HEAD_SIZE_COUNT][2][2][2];
  409. vk_pipeline pipeline_flash_attn_split_k_reduce;
  410. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  411. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  412. vk::Fence fence;
  413. vk_buffer sync_staging;
  414. ggml_backend_buffer_type buffer_type;
  415. #ifdef GGML_VULKAN_MEMORY_DEBUG
  416. std::unique_ptr<vk_memory_logger> memory_logger;
  417. #endif
  418. // for GGML_VK_PERF_LOGGER
  419. std::unique_ptr<vk_perf_logger> perf_logger;
  420. vk::QueryPool query_pool;
  421. int32_t num_queries;
  422. ~vk_device_struct() {
  423. VK_LOG_DEBUG("destroy device " << name);
  424. device.destroyFence(fence);
  425. ggml_vk_destroy_buffer(sync_staging);
  426. compute_queue.cmd_pool.destroy(device);
  427. transfer_queue.cmd_pool.destroy(device);
  428. for (auto& pipeline : pipelines) {
  429. if (pipeline.second.expired()) {
  430. continue;
  431. }
  432. vk_pipeline pl = pipeline.second.lock();
  433. ggml_vk_destroy_pipeline(device, pl);
  434. }
  435. pipelines.clear();
  436. device.destroyDescriptorSetLayout(dsl);
  437. device.destroy();
  438. }
  439. };
  440. void vk_command_pool::init(vk_device& device, vk_queue *q_) {
  441. cmd_buffer_idx = 0;
  442. q = q_;
  443. vk::CommandPoolCreateInfo command_pool_create_info(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), q->queue_family_index);
  444. pool = device->device.createCommandPool(command_pool_create_info);
  445. }
  446. void vk_command_pool::destroy(vk::Device& device) {
  447. device.destroyCommandPool(pool);
  448. pool = nullptr;
  449. cmd_buffers.clear();
  450. }
  451. struct vk_buffer_struct {
  452. vk::Buffer buffer = VK_NULL_HANDLE;
  453. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  454. vk::MemoryPropertyFlags memory_property_flags;
  455. void * ptr;
  456. size_t size = 0;
  457. vk_device device;
  458. ~vk_buffer_struct() {
  459. if (size == 0) {
  460. return;
  461. }
  462. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  463. device->device.freeMemory(device_memory);
  464. device->device.destroyBuffer(buffer);
  465. }
  466. };
  467. struct vk_subbuffer {
  468. vk_buffer buffer;
  469. uint64_t offset;
  470. uint64_t size;
  471. operator vk::DescriptorBufferInfo() const {
  472. return { buffer->buffer, offset, size };
  473. }
  474. };
  475. struct vk_semaphore {
  476. vk::Semaphore s;
  477. uint64_t value;
  478. };
  479. struct vk_submission {
  480. vk::CommandBuffer buffer;
  481. std::vector<vk_semaphore> wait_semaphores;
  482. std::vector<vk_semaphore> signal_semaphores;
  483. };
  484. typedef std::vector<vk_submission> vk_sequence;
  485. struct vk_mat_mat_push_constants {
  486. uint32_t M; uint32_t N; uint32_t K;
  487. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  488. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  489. uint32_t k_split;
  490. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  491. uint32_t padded_N;
  492. };
  493. struct vk_mat_vec_push_constants {
  494. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  495. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  496. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  497. };
  498. struct vk_mat_mat_id_push_constants {
  499. uint32_t M; uint32_t N; uint32_t K;
  500. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  501. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  502. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  503. uint32_t padded_N;
  504. };
  505. struct vk_mat_vec_id_push_constants {
  506. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  507. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  508. uint32_t nei0; uint32_t ne11;
  509. };
  510. struct vk_flash_attn_push_constants {
  511. uint32_t N;
  512. uint32_t KV;
  513. uint32_t ne1;
  514. uint32_t ne2;
  515. uint32_t ne3;
  516. uint32_t neq2;
  517. uint32_t neq3;
  518. uint32_t nek2;
  519. uint32_t nek3;
  520. uint32_t nev2;
  521. uint32_t nev3;
  522. uint32_t nem1;
  523. uint32_t nem2;
  524. uint32_t nb01;
  525. uint32_t nb02;
  526. uint32_t nb03;
  527. uint32_t nb11;
  528. uint32_t nb12;
  529. uint32_t nb13;
  530. uint32_t nb21;
  531. uint32_t nb22;
  532. uint32_t nb23;
  533. float scale;
  534. float max_bias;
  535. float logit_softcap;
  536. uint32_t mask;
  537. uint32_t n_head_log2;
  538. float m0;
  539. float m1;
  540. uint32_t gqa_ratio;
  541. uint32_t split_kv;
  542. uint32_t k_num;
  543. };
  544. static_assert(sizeof(vk_flash_attn_push_constants) <= 128, "sizeof(vk_flash_attn_push_constants) must be <= 128");
  545. struct vk_op_push_constants {
  546. uint32_t KX;
  547. uint32_t KY;
  548. float param1;
  549. float param2;
  550. };
  551. struct vk_op_glu_push_constants {
  552. uint32_t N;
  553. uint32_t ne00;
  554. uint32_t ne20;
  555. uint32_t mode; // 0: default, 1: swapped, 2: split
  556. };
  557. struct vk_op_unary_push_constants {
  558. uint32_t ne;
  559. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  560. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  561. uint32_t misalign_offsets;
  562. float param1; float param2;
  563. uint32_t ne0_012mp; uint32_t ne0_012L;
  564. uint32_t ne0_01mp; uint32_t ne0_01L;
  565. uint32_t ne0_0mp; uint32_t ne0_0L;
  566. uint32_t ne1_012mp; uint32_t ne1_012L;
  567. uint32_t ne1_01mp; uint32_t ne1_01L;
  568. uint32_t ne1_0mp; uint32_t ne1_0L;
  569. };
  570. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  571. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  572. // Precompute mp (m' in the paper) and L such that division
  573. // can be computed using a multiply (high 32b of 64b result)
  574. // and a shift:
  575. //
  576. // n/d = (mulhi(n, mp) + n) >> L;
  577. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  578. {
  579. // compute L = ceil(log2(d));
  580. L = 0;
  581. while (L < 32 && (uint32_t{1} << L) < d) {
  582. L++;
  583. }
  584. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  585. }
  586. template <typename T> void init_pushconst_fastdiv(T &p) {
  587. GGML_UNUSED(p);
  588. static_assert(!std::is_const<T>::value, "unexpected type");
  589. }
  590. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  591. // Compute magic values to divide by these six numbers.
  592. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  593. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  594. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  595. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  596. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  597. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  598. }
  599. struct vk_op_binary_push_constants {
  600. uint32_t ne;
  601. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  602. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  603. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  604. uint32_t misalign_offsets;
  605. float param1; float param2; int32_t param3;
  606. };
  607. struct vk_op_diag_mask_push_constants {
  608. uint32_t ncols;
  609. uint32_t rows_per_channel;
  610. int32_t n_past;
  611. };
  612. struct vk_op_rope_push_constants {
  613. uint32_t ncols;
  614. uint32_t n_dims;
  615. float freq_scale;
  616. uint32_t p_delta_rows;
  617. float freq_base;
  618. float ext_factor;
  619. float attn_factor;
  620. float corr_dims[2];
  621. float theta_scale;
  622. uint32_t has_ff;
  623. uint32_t ne02;
  624. uint32_t s1;
  625. uint32_t s2;
  626. int32_t sections[4];
  627. uint32_t is_back;
  628. };
  629. struct vk_op_soft_max_push_constants {
  630. uint32_t KX;
  631. uint32_t KY;
  632. uint32_t ne00;
  633. uint32_t ne01;
  634. uint32_t ne02;
  635. uint32_t ne12;
  636. uint32_t ne13;
  637. uint32_t nb11;
  638. uint32_t nb12;
  639. uint32_t nb13;
  640. float scale;
  641. float max_bias;
  642. float m0;
  643. float m1;
  644. uint32_t n_head_log2;
  645. uint32_t nrows_x;
  646. };
  647. struct vk_op_argsort_push_constants {
  648. uint32_t ncols;
  649. uint32_t ncols_pad;
  650. int32_t order;
  651. };
  652. struct vk_op_im2col_push_constants {
  653. uint32_t batch_offset; uint32_t offset_delta;
  654. uint32_t IC;
  655. uint32_t IW; uint32_t IH;
  656. uint32_t OW; uint32_t OH;
  657. uint32_t KW; uint32_t KH;
  658. uint32_t pelements;
  659. uint32_t CHW;
  660. int32_t s0; int32_t s1;
  661. int32_t p0; int32_t p1;
  662. int32_t d0; int32_t d1;
  663. };
  664. struct vk_op_timestep_embedding_push_constants {
  665. uint32_t nb1;
  666. uint32_t dim;
  667. uint32_t max_period;
  668. };
  669. struct vk_op_conv_transpose_1d_push_constants {
  670. uint32_t Cout;
  671. uint32_t Cin;
  672. uint32_t K;
  673. uint32_t L;
  674. uint32_t KL;
  675. uint32_t nb01;
  676. uint32_t nb02;
  677. uint32_t nb11;
  678. uint32_t nb1;
  679. int32_t s0;
  680. };
  681. struct vk_op_pool2d_push_constants {
  682. uint32_t IW; uint32_t IH;
  683. uint32_t OW; uint32_t OH;
  684. uint32_t OC;
  685. uint32_t pelements;
  686. uint32_t op;
  687. int32_t k0; int32_t k1;
  688. int32_t s0; int32_t s1;
  689. int32_t p0; int32_t p1;
  690. };
  691. struct vk_op_rwkv_wkv6_push_constants {
  692. uint32_t B;
  693. uint32_t T;
  694. uint32_t C;
  695. uint32_t H;
  696. };
  697. struct vk_op_rwkv_wkv7_push_constants {
  698. uint32_t B;
  699. uint32_t T;
  700. uint32_t C;
  701. uint32_t H;
  702. };
  703. struct vk_op_conv2d_dw_push_constants {
  704. uint32_t ne;
  705. uint32_t batches;
  706. uint32_t channels;
  707. uint32_t dst_w;
  708. uint32_t dst_h;
  709. uint32_t src_w;
  710. uint32_t src_h;
  711. uint32_t knl_w;
  712. uint32_t knl_h;
  713. int32_t stride_x;
  714. int32_t stride_y;
  715. int32_t pad_x;
  716. int32_t pad_y;
  717. int32_t dilation_x;
  718. int32_t dilation_y;
  719. };
  720. struct vk_op_upscale_push_constants {
  721. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  722. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  723. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  724. float sf0; float sf1; float sf2; float sf3;
  725. };
  726. // Allow pre-recording command buffers
  727. struct vk_staging_memcpy {
  728. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  729. void * dst;
  730. const void * src;
  731. size_t n;
  732. };
  733. struct vk_context_struct {
  734. vk_submission * s;
  735. std::vector<vk_sequence> seqs;
  736. int exit_tensor_idx;
  737. std::vector<vk_staging_memcpy> in_memcpys;
  738. std::vector<vk_staging_memcpy> out_memcpys;
  739. vk_command_pool * p {};
  740. };
  741. typedef std::shared_ptr<vk_context_struct> vk_context;
  742. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  743. struct ggml_vk_garbage_collector {
  744. std::vector<vk_semaphore> tl_semaphores;
  745. std::vector<vk_semaphore> semaphores;
  746. std::vector<vk::Event> events;
  747. std::vector<vk_buffer> temp_buffers;
  748. std::vector<vk_context> contexts;
  749. };
  750. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  751. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  752. static std::string format_size(size_t size) {
  753. const size_t kib = 1024;
  754. const size_t mib = kib * 1024;
  755. const size_t gib = mib * 1024;
  756. std::ostringstream oss;
  757. oss << std::fixed << std::setprecision(2);
  758. if (size >= gib) {
  759. oss << static_cast<double>(size) / gib << " GiB";
  760. } else if (size >= mib) {
  761. oss << static_cast<double>(size) / mib << " MiB";
  762. } else if (size >= kib) {
  763. oss << static_cast<double>(size) / kib << " KiB";
  764. } else {
  765. oss << size << " B";
  766. }
  767. return oss.str();
  768. }
  769. static std::mutex log_mutex;
  770. class vk_memory_logger {
  771. public:
  772. vk_memory_logger(): total_device(0), total_host(0) {}
  773. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  774. void log_deallocation(vk_buffer_ref buf_ref);
  775. private:
  776. std::map<vk::Buffer, size_t> allocations; // Track allocations
  777. size_t total_device;
  778. size_t total_host;
  779. };
  780. #else
  781. #define VK_LOG_MEMORY(msg) ((void) 0)
  782. #endif // GGML_VULKAN_MEMORY_DEBUG
  783. class vk_perf_logger {
  784. public:
  785. void print_timings() {
  786. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  787. for (const auto& t : timings) {
  788. uint64_t total = 0;
  789. for (const auto& time : t.second) {
  790. total += time;
  791. }
  792. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " us" << std::endl;
  793. }
  794. timings.clear();
  795. }
  796. void log_timing(const ggml_tensor * node, uint64_t time) {
  797. if (node->op == GGML_OP_UNARY) {
  798. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  799. return;
  800. }
  801. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  802. const uint64_t m = node->src[0]->ne[1];
  803. const uint64_t n = node->src[1]->ne[1];
  804. const uint64_t k = node->src[1]->ne[0];
  805. std::string name = ggml_op_name(node->op);
  806. if (n == 1) {
  807. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  808. } else {
  809. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  810. }
  811. timings[name].push_back(time);
  812. return;
  813. }
  814. timings[ggml_op_name(node->op)].push_back(time);
  815. }
  816. private:
  817. std::map<std::string, std::vector<uint64_t>> timings;
  818. };
  819. struct ggml_backend_vk_context {
  820. std::string name;
  821. vk_device device;
  822. size_t semaphore_idx, event_idx;
  823. ggml_vk_garbage_collector gc;
  824. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  825. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  826. vk::Fence fence, almost_ready_fence;
  827. bool almost_ready_fence_pending {};
  828. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  829. vk_context_ref compute_ctx;
  830. vk_context_ref transfer_ctx;
  831. std::vector<vk_context_ref> tensor_ctxs;
  832. std::vector<vk::DescriptorPool> descriptor_pools;
  833. std::vector<vk::DescriptorSet> descriptor_sets;
  834. uint32_t descriptor_set_idx {};
  835. uint32_t pipeline_descriptor_set_requirements {};
  836. vk_command_pool compute_cmd_pool;
  837. vk_command_pool transfer_cmd_pool;
  838. // number of additional consecutive nodes that are being fused with the
  839. // node currently being processed
  840. int num_additional_fused_ops {};
  841. };
  842. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  843. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  844. if (tensor->view_src) {
  845. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  846. }
  847. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  848. }
  849. struct ggml_backend_vk_buffer_context {
  850. vk_device_ref device;
  851. vk_buffer dev_buffer;
  852. std::string name;
  853. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  854. device(device),
  855. dev_buffer(dev_buffer),
  856. name(name) {
  857. }
  858. ~ggml_backend_vk_buffer_context() {
  859. ggml_vk_destroy_buffer(dev_buffer);
  860. }
  861. };
  862. #ifdef GGML_VULKAN_MEMORY_DEBUG
  863. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  864. std::lock_guard<std::mutex> guard(log_mutex);
  865. vk_buffer buf = buf_ref.lock();
  866. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  867. const std::string type = device ? "device" : "host";
  868. allocations[buf->buffer] = size;
  869. total_device += device ? size : 0;
  870. total_host += device ? 0 : size;
  871. 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));
  872. }
  873. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  874. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  875. return;
  876. }
  877. std::lock_guard<std::mutex> guard(log_mutex);
  878. vk_buffer buf = buf_ref.lock();
  879. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  880. std::string type = device ? "device" : "host";
  881. auto it = allocations.find(buf->buffer);
  882. total_device -= device ? it->second : 0;
  883. total_host -= device ? 0 : it->second;
  884. if (it != allocations.end()) {
  885. 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));
  886. allocations.erase(it);
  887. } else {
  888. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  889. }
  890. }
  891. #endif // GGML_VULKAN_MEMORY_DEBUG
  892. struct vk_instance_t {
  893. vk::Instance instance;
  894. bool debug_utils_support = false; // VK_EXT_debug_utils enabled
  895. PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
  896. PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
  897. PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
  898. PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
  899. PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
  900. PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
  901. std::vector<size_t> device_indices;
  902. vk_device devices[GGML_VK_MAX_DEVICES];
  903. };
  904. static bool vk_instance_initialized = false;
  905. static vk_instance_t vk_instance;
  906. static bool vk_perf_logger_enabled = false;
  907. #ifdef GGML_VULKAN_CHECK_RESULTS
  908. static size_t vk_skip_checks;
  909. static size_t vk_output_tensor;
  910. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  911. static void ggml_vk_check_results_0(ggml_tensor * tensor);
  912. static void ggml_vk_check_results_1(ggml_tensor * tensor);
  913. #endif
  914. 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);
  915. static void ggml_backend_vk_free(ggml_backend_t backend);
  916. // Wait for ctx->fence to be signaled.
  917. static void ggml_vk_wait_for_fence(ggml_backend_vk_context * ctx) {
  918. // Use waitForFences while most of the graph executes. Hopefully the CPU can sleep
  919. // during this wait.
  920. if (ctx->almost_ready_fence_pending) {
  921. VK_CHECK(ctx->device->device.waitForFences({ ctx->almost_ready_fence }, true, UINT64_MAX), "almost_ready_fence");
  922. ctx->device->device.resetFences({ ctx->almost_ready_fence });
  923. ctx->almost_ready_fence_pending = false;
  924. }
  925. // Spin (w/pause) waiting for the graph to finish executing.
  926. vk::Result result;
  927. while ((result = ctx->device->device.getFenceStatus(ctx->fence)) != vk::Result::eSuccess) {
  928. if (result != vk::Result::eNotReady) {
  929. fprintf(stderr, "ggml_vulkan: error %s at %s:%d\n", to_string(result).c_str(), __FILE__, __LINE__);
  930. exit(1);
  931. }
  932. for (uint32_t i = 0; i < 100; ++i) {
  933. YIELD();
  934. YIELD();
  935. YIELD();
  936. YIELD();
  937. YIELD();
  938. YIELD();
  939. YIELD();
  940. YIELD();
  941. YIELD();
  942. YIELD();
  943. }
  944. }
  945. ctx->device->device.resetFences({ ctx->fence });
  946. }
  947. // variables to track number of compiles in progress
  948. static uint32_t compile_count = 0;
  949. static std::mutex compile_count_mutex;
  950. static std::condition_variable compile_count_cond;
  951. 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,
  952. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  953. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  954. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  955. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  956. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  957. GGML_ASSERT(parameter_count > 0);
  958. GGML_ASSERT(parameter_count <= MAX_PARAMETER_COUNT);
  959. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  960. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  961. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  962. vk::PushConstantRange pcr(
  963. vk::ShaderStageFlagBits::eCompute,
  964. 0,
  965. pipeline->push_constant_size
  966. );
  967. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), device->dsl, pcr);
  968. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  969. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  970. for (size_t i = 0; i < specialization_constants.size(); i++) {
  971. specialization_entries[i].constantID = i;
  972. specialization_entries[i].offset = i * sizeof(uint32_t);
  973. specialization_entries[i].size = sizeof(uint32_t);
  974. }
  975. vk::SpecializationInfo specialization_info(
  976. specialization_entries.size(),
  977. specialization_entries.data(),
  978. specialization_constants.size() * sizeof(uint32_t),
  979. specialization_constants.data()
  980. );
  981. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  982. if (device->subgroup_require_full_support && require_full_subgroups) {
  983. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  984. }
  985. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  986. pipeline_shader_stage_create_flags,
  987. vk::ShaderStageFlagBits::eCompute,
  988. pipeline->shader_module,
  989. entrypoint.c_str(),
  990. &specialization_info);
  991. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  992. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  993. if (device->subgroup_size_control && required_subgroup_size > 0) {
  994. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  995. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  996. }
  997. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  998. vk::PipelineCreateFlags{},
  999. pipeline_shader_create_info,
  1000. pipeline->layout);
  1001. vk::PipelineRobustnessCreateInfoEXT rci;
  1002. if (device->pipeline_robustness && disable_robustness) {
  1003. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1004. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  1005. compute_pipeline_create_info.setPNext(&rci);
  1006. }
  1007. try {
  1008. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  1009. } catch (const vk::SystemError& e) {
  1010. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  1011. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1012. throw e;
  1013. }
  1014. pipeline->compiled = true;
  1015. if (vk_instance.debug_utils_support) {
  1016. vk::DebugUtilsObjectNameInfoEXT duoni;
  1017. duoni.objectType = vk::ObjectType::ePipeline;
  1018. duoni.pObjectName = pipeline->name.c_str();
  1019. duoni.objectHandle = reinterpret_cast<uint64_t>(static_cast<VkPipeline_T*>(pipeline->pipeline));
  1020. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
  1021. }
  1022. {
  1023. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1024. device->pipelines.insert({ pipeline->name, pipeline });
  1025. }
  1026. {
  1027. std::lock_guard<std::mutex> guard(compile_count_mutex);
  1028. assert(compile_count > 0);
  1029. compile_count--;
  1030. }
  1031. compile_count_cond.notify_all();
  1032. }
  1033. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  1034. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  1035. device.destroyPipelineLayout(pipeline->layout);
  1036. device.destroyShaderModule(pipeline->shader_module);
  1037. device.destroyPipeline(pipeline->pipeline);
  1038. }
  1039. static void ggml_pipeline_request_descriptor_sets(ggml_backend_vk_context *ctx, vk_pipeline& pipeline, uint32_t n) {
  1040. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  1041. ctx->pipeline_descriptor_set_requirements += n;
  1042. if (!pipeline->compiled) {
  1043. pipeline->needed = true;
  1044. ctx->device->need_compiles = true;
  1045. }
  1046. }
  1047. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx) {
  1048. if (ctx->descriptor_sets.size() >= ctx->pipeline_descriptor_set_requirements) {
  1049. // Enough descriptors are available
  1050. return;
  1051. }
  1052. vk_device& device = ctx->device;
  1053. uint32_t to_alloc = ctx->pipeline_descriptor_set_requirements - ctx->descriptor_sets.size();
  1054. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - ctx->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1055. uint32_t pool_idx = ctx->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1056. while (to_alloc > 0) {
  1057. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  1058. to_alloc -= alloc_count;
  1059. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  1060. if (pool_idx >= ctx->descriptor_pools.size()) {
  1061. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, MAX_PARAMETER_COUNT * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  1062. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  1063. ctx->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  1064. }
  1065. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  1066. for (uint32_t i = 0; i < alloc_count; i++) {
  1067. layouts[i] = device->dsl;
  1068. }
  1069. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(ctx->descriptor_pools[pool_idx], alloc_count, layouts.data());
  1070. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  1071. ctx->descriptor_sets.insert(ctx->descriptor_sets.end(), sets.begin(), sets.end());
  1072. pool_idx++;
  1073. }
  1074. }
  1075. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_command_pool& p) {
  1076. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  1077. if (p.cmd_buffers.size() > p.cmd_buffer_idx) {
  1078. // Reuse command buffer
  1079. return p.cmd_buffers[p.cmd_buffer_idx++];
  1080. }
  1081. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  1082. p.pool,
  1083. vk::CommandBufferLevel::ePrimary,
  1084. 1);
  1085. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  1086. auto buf = cmd_buffers.front();
  1087. p.cmd_buffers.push_back(buf);
  1088. p.cmd_buffer_idx++;
  1089. return buf;
  1090. }
  1091. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  1092. if (ctx->seqs.empty()) {
  1093. if (fence) {
  1094. std::lock_guard<std::mutex> guard(queue_mutex);
  1095. ctx->p->q->queue.submit({}, fence);
  1096. }
  1097. return;
  1098. }
  1099. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  1100. std::vector<std::vector<uint64_t>> tl_wait_vals;
  1101. std::vector<std::vector<uint64_t>> tl_signal_vals;
  1102. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  1103. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  1104. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  1105. std::vector<vk::SubmitInfo> submit_infos;
  1106. int idx = -1;
  1107. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  1108. size_t reserve = 0;
  1109. for (const auto& sequence : ctx->seqs) {
  1110. reserve += sequence.size();
  1111. }
  1112. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  1113. tl_wait_semaphores.reserve(reserve);
  1114. tl_wait_vals.reserve(reserve);
  1115. tl_signal_semaphores.reserve(reserve);
  1116. tl_signal_vals.reserve(reserve);
  1117. tl_submit_infos.reserve(reserve);
  1118. submit_infos.reserve(reserve);
  1119. stage_flags.reserve(reserve);
  1120. for (const auto& sequence : ctx->seqs) {
  1121. for (const auto& submission : sequence) {
  1122. stage_flags.push_back({});
  1123. idx++;
  1124. tl_wait_vals.push_back({});
  1125. tl_wait_semaphores.push_back({});
  1126. tl_signal_vals.push_back({});
  1127. tl_signal_semaphores.push_back({});
  1128. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  1129. stage_flags[idx].push_back(ctx->p->q->stage_flags);
  1130. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  1131. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  1132. }
  1133. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  1134. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  1135. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  1136. }
  1137. tl_submit_infos.push_back({
  1138. (uint32_t) submission.wait_semaphores.size(),
  1139. tl_wait_vals[idx].data(),
  1140. (uint32_t) submission.signal_semaphores.size(),
  1141. tl_signal_vals[idx].data(),
  1142. });
  1143. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  1144. tl_submit_infos[idx].pNext = nullptr;
  1145. vk::SubmitInfo si{
  1146. (uint32_t) submission.wait_semaphores.size(),
  1147. tl_wait_semaphores[idx].data(),
  1148. stage_flags[idx].data(),
  1149. 1,
  1150. &submission.buffer,
  1151. (uint32_t) submission.signal_semaphores.size(),
  1152. tl_signal_semaphores[idx].data(),
  1153. };
  1154. si.setPNext(&tl_submit_infos[idx]);
  1155. submit_infos.push_back(si);
  1156. }
  1157. }
  1158. std::lock_guard<std::mutex> guard(queue_mutex);
  1159. ctx->p->q->queue.submit(submit_infos, fence);
  1160. ctx->seqs.clear();
  1161. }
  1162. 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) {
  1163. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  1164. const uint32_t qfsize = queue_family_props.size();
  1165. // Try with avoid preferences first
  1166. for (uint32_t i = 0; i < qfsize; i++) {
  1167. 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)) {
  1168. return i;
  1169. }
  1170. }
  1171. // Fall back to only required
  1172. for (size_t i = 0; i < qfsize; i++) {
  1173. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  1174. return i;
  1175. }
  1176. }
  1177. // Fall back to reusing compute queue
  1178. for (size_t i = 0; i < qfsize; i++) {
  1179. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  1180. return i;
  1181. }
  1182. }
  1183. // Fall back to ignoring min_num_queries
  1184. for (size_t i = 0; i < qfsize; i++) {
  1185. if (queue_family_props[i].queueFlags & required) {
  1186. return i;
  1187. }
  1188. }
  1189. // 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.
  1190. // 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.
  1191. if (compute_index >= 0) {
  1192. return compute_index;
  1193. }
  1194. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1195. for(auto &q_family : queue_family_props) {
  1196. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1197. }
  1198. abort();
  1199. }
  1200. 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) {
  1201. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1202. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  1203. q.queue_family_index = queue_family_index;
  1204. q.transfer_only = transfer_only;
  1205. q.cmd_pool.init(device, &q);
  1206. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1207. q.stage_flags = stage_flags;
  1208. }
  1209. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_command_pool& p) {
  1210. vk_context result = std::make_shared<vk_context_struct>();
  1211. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1212. ctx->gc.contexts.emplace_back(result);
  1213. result->p = &p;
  1214. return result;
  1215. }
  1216. static vk_context ggml_vk_create_temporary_context(vk_command_pool& p) {
  1217. vk_context result = std::make_shared<vk_context_struct>();
  1218. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1219. result->p = &p;
  1220. return result;
  1221. }
  1222. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1223. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1224. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1225. vk::SemaphoreCreateInfo ci{};
  1226. ci.setPNext(&tci);
  1227. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1228. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1229. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1230. }
  1231. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1232. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1233. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1234. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1235. vk::SemaphoreCreateInfo ci{};
  1236. ci.setPNext(&tci);
  1237. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1238. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1239. }
  1240. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1241. }
  1242. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1243. if (ctx->event_idx >= ctx->gc.events.size()) {
  1244. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1245. }
  1246. return ctx->gc.events[ctx->event_idx++];
  1247. }
  1248. static void ggml_vk_command_pool_cleanup(vk_device& device, vk_command_pool& p) {
  1249. VK_LOG_DEBUG("ggml_vk_command_pool_cleanup()");
  1250. // Requires command buffers to be done
  1251. device->device.resetCommandPool(p.pool);
  1252. p.cmd_buffer_idx = 0;
  1253. }
  1254. static void ggml_vk_queue_command_pools_cleanup(vk_device& device) {
  1255. VK_LOG_DEBUG("ggml_vk_queue_command_pools_cleanup()");
  1256. // Arbitrary frequency to cleanup/reuse command buffers
  1257. static constexpr uint32_t cleanup_frequency = 10;
  1258. if (device->compute_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1259. ggml_vk_command_pool_cleanup(device, device->compute_queue.cmd_pool);
  1260. }
  1261. if (device->transfer_queue.cmd_pool.cmd_buffer_idx >= cleanup_frequency) {
  1262. ggml_vk_command_pool_cleanup(device, device->transfer_queue.cmd_pool);
  1263. }
  1264. }
  1265. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1266. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1267. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1268. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1269. (flags & memory_type.propertyFlags) == flags &&
  1270. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1271. return static_cast<int32_t>(i);
  1272. }
  1273. }
  1274. return UINT32_MAX;
  1275. }
  1276. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1277. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1278. if (size > device->max_memory_allocation_size) {
  1279. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1280. }
  1281. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1282. if (size == 0) {
  1283. buf->size = 0;
  1284. return buf;
  1285. }
  1286. vk::BufferCreateInfo buffer_create_info{
  1287. vk::BufferCreateFlags(),
  1288. size,
  1289. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1290. vk::SharingMode::eExclusive,
  1291. 0,
  1292. nullptr,
  1293. };
  1294. buf->buffer = device->device.createBuffer(buffer_create_info);
  1295. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1296. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1297. uint32_t memory_type_index = UINT32_MAX;
  1298. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1299. buf->memory_property_flags = req_flags;
  1300. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1301. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1302. buf->memory_property_flags = fallback_flags;
  1303. }
  1304. if (memory_type_index == UINT32_MAX) {
  1305. device->device.destroyBuffer(buf->buffer);
  1306. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1307. }
  1308. try {
  1309. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1310. } catch (const vk::SystemError& e) {
  1311. if (buf->memory_property_flags != fallback_flags) {
  1312. // Try again with fallback flags
  1313. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1314. buf->memory_property_flags = fallback_flags;
  1315. try {
  1316. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1317. }
  1318. catch (const vk::SystemError& e) {
  1319. device->device.destroyBuffer(buf->buffer);
  1320. throw e;
  1321. }
  1322. } else {
  1323. // Out of Host/Device memory, clean up buffer
  1324. device->device.destroyBuffer(buf->buffer);
  1325. throw e;
  1326. }
  1327. }
  1328. buf->ptr = nullptr;
  1329. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1330. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1331. }
  1332. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1333. buf->device = device;
  1334. buf->size = size;
  1335. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1336. device->memory_logger->log_allocation(buf, size);
  1337. #endif
  1338. return buf;
  1339. }
  1340. 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)) {
  1341. try {
  1342. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1343. } catch (const vk::SystemError& e) {
  1344. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1345. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1346. throw e;
  1347. }
  1348. }
  1349. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1350. vk_buffer buf;
  1351. try {
  1352. if (device->prefer_host_memory) {
  1353. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1354. } else if (device->uma) {
  1355. // Fall back to host memory type
  1356. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1357. } else {
  1358. // use rebar if available, otherwise fallback to device only visible memory
  1359. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1360. }
  1361. } catch (const vk::SystemError& e) {
  1362. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1363. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1364. throw e;
  1365. }
  1366. return buf;
  1367. }
  1368. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1369. if (buf == nullptr) {
  1370. return;
  1371. }
  1372. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1373. if (buf->device != nullptr) {
  1374. buf->device->memory_logger->log_deallocation(buf);
  1375. }
  1376. #endif
  1377. buf.reset();
  1378. }
  1379. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1380. return { buf, 0, VK_WHOLE_SIZE };
  1381. }
  1382. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1383. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1384. const bool transfer_queue = ctx->p->q->transfer_only;
  1385. ctx->s->buffer.pipelineBarrier(
  1386. ctx->p->q->stage_flags,
  1387. ctx->p->q->stage_flags,
  1388. {},
  1389. { {
  1390. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1391. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1392. } },
  1393. {},
  1394. {}
  1395. );
  1396. }
  1397. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1398. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1399. if (events.empty()) {
  1400. return;
  1401. }
  1402. ctx->s->buffer.waitEvents(
  1403. events,
  1404. ctx->p->q->stage_flags,
  1405. ctx->p->q->stage_flags,
  1406. {},
  1407. {},
  1408. {}
  1409. );
  1410. }
  1411. enum FaCodePath {
  1412. FA_SCALAR,
  1413. FA_COOPMAT1,
  1414. FA_COOPMAT2,
  1415. };
  1416. static FaHeadSizes fa_get_head_sizes(uint32_t hsk, uint32_t hsv) {
  1417. if (hsk != 192 && hsk != 576 && hsk != hsv) {
  1418. return FA_HEAD_SIZE_UNSUPPORTED;
  1419. }
  1420. switch (hsk) {
  1421. case 64: return FA_HEAD_SIZE_64;
  1422. case 80: return FA_HEAD_SIZE_80;
  1423. case 96: return FA_HEAD_SIZE_96;
  1424. case 112: return FA_HEAD_SIZE_112;
  1425. case 128: return FA_HEAD_SIZE_128;
  1426. case 192:
  1427. if (hsv == 192) {
  1428. return FA_HEAD_SIZE_192;
  1429. } else if (hsv == 128) {
  1430. return FA_HEAD_SIZE_192_128;
  1431. } else {
  1432. return FA_HEAD_SIZE_UNSUPPORTED;
  1433. }
  1434. case 256: return FA_HEAD_SIZE_256;
  1435. case 576:
  1436. if (hsv == 512) {
  1437. return FA_HEAD_SIZE_576_512;
  1438. } else {
  1439. return FA_HEAD_SIZE_UNSUPPORTED;
  1440. }
  1441. default: return FA_HEAD_SIZE_UNSUPPORTED;
  1442. }
  1443. }
  1444. // number of rows/cols for flash attention shader
  1445. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1446. static constexpr uint32_t scalar_flash_attention_num_small_rows = 1;
  1447. static constexpr uint32_t scalar_flash_attention_num_large_rows = 8;
  1448. // The FA coopmat1 shader assumes 16x16x16 matrix multiply support.
  1449. // 128 threads split into four subgroups, each subgroup does 1/4
  1450. // of the Bc dimension.
  1451. static constexpr uint32_t coopmat1_flash_attention_num_large_rows = 16;
  1452. static constexpr uint32_t scalar_flash_attention_Bc = 64;
  1453. static constexpr uint32_t scalar_flash_attention_workgroup_size = 128;
  1454. static uint32_t get_fa_num_small_rows(FaCodePath path) {
  1455. if (path == FA_COOPMAT2) {
  1456. return flash_attention_num_small_rows;
  1457. } else {
  1458. return scalar_flash_attention_num_small_rows;
  1459. }
  1460. }
  1461. 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) {
  1462. GGML_UNUSED(clamp);
  1463. GGML_UNUSED(hsv);
  1464. if (path == FA_SCALAR) {
  1465. if (small_rows) {
  1466. return {scalar_flash_attention_num_small_rows, 64};
  1467. } else {
  1468. return {scalar_flash_attention_num_large_rows, 32};
  1469. }
  1470. }
  1471. if (path == FA_COOPMAT1) {
  1472. if (small_rows) {
  1473. return {scalar_flash_attention_num_small_rows, scalar_flash_attention_Bc};
  1474. } else {
  1475. return {coopmat1_flash_attention_num_large_rows, scalar_flash_attention_Bc};
  1476. }
  1477. }
  1478. // small rows, large cols
  1479. if (small_rows) {
  1480. return {get_fa_num_small_rows(FA_COOPMAT2), 32};
  1481. }
  1482. // small cols to reduce register count
  1483. if (ggml_is_quantized(type) || hsk >= 256) {
  1484. return {64, 32};
  1485. }
  1486. return {64, 64};
  1487. }
  1488. 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) {
  1489. uint32_t lut_size = 0;
  1490. switch (src0_type) {
  1491. case GGML_TYPE_IQ1_S:
  1492. case GGML_TYPE_IQ1_M:
  1493. lut_size = 2*2048;
  1494. break;
  1495. case GGML_TYPE_IQ2_XXS:
  1496. lut_size = 8*256;
  1497. break;
  1498. case GGML_TYPE_IQ2_XS:
  1499. lut_size = 8*512;
  1500. break;
  1501. case GGML_TYPE_IQ2_S:
  1502. lut_size = 8*1024;
  1503. break;
  1504. case GGML_TYPE_IQ3_XXS:
  1505. lut_size = 4*256;
  1506. break;
  1507. case GGML_TYPE_IQ3_S:
  1508. lut_size = 4*512;
  1509. break;
  1510. case GGML_TYPE_IQ4_NL:
  1511. case GGML_TYPE_IQ4_XS:
  1512. lut_size = 4*16;
  1513. break;
  1514. default:
  1515. break;
  1516. }
  1517. // Needs to be kept up to date on shader changes
  1518. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1519. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1520. const uint32_t warps = warptile[0] / warptile[10];
  1521. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1522. const uint32_t mmid_row_ids = mul_mat_id ? 4096 * sizeof(uint32_t) : 0;
  1523. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1524. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1525. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1526. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1527. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1528. return supported;
  1529. }
  1530. struct GpuPipelineConfig {
  1531. // GPU architecture identifier.
  1532. // Example: vk_device_architecture::AMD_GCN
  1533. vk_device_architecture arch;
  1534. // Mapping of pipeline names to their specific subgroup sizes.
  1535. // Example: {"soft_max_f32", 64}
  1536. std::unordered_map<std::string, uint32_t> pipelines;
  1537. // Default subgroup size for this GPU.
  1538. // Defaults to 0 if not explicitly provided.
  1539. uint32_t default_subgroup_size = 0;
  1540. };
  1541. // Pipeline configuration for RDNA1 GPUs.
  1542. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1543. {"soft_max", 64}, {"im2col", 64},
  1544. {"argmax", 64}, {"mul_mat_vec", 64},
  1545. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1546. };
  1547. // Pipeline configuration for RDNA2 GPUs.
  1548. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1549. {"soft_max", 64}, {"im2col", 64},
  1550. };
  1551. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1552. // Define configurations for different GPUs.
  1553. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1554. {
  1555. vk_device_architecture::AMD_RDNA1,
  1556. {
  1557. rdna1_pipelines,
  1558. },
  1559. RDNA_DEFAULT_SUBGROUP_SIZE
  1560. },
  1561. {
  1562. vk_device_architecture::AMD_RDNA2,
  1563. {
  1564. rdna2_pipelines,
  1565. },
  1566. RDNA_DEFAULT_SUBGROUP_SIZE
  1567. },
  1568. };
  1569. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1570. for (const auto &config : gpu_pipeline_configs) {
  1571. if (config.arch == arch) {
  1572. auto pipIt = config.pipelines.find(pipeline_name);
  1573. if (pipIt != config.pipelines.end()) {
  1574. return pipIt->second;
  1575. }
  1576. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1577. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1578. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1579. for (const auto &entry : sorted_pipelines) {
  1580. if (pipeline_name.find(entry.first) != std::string::npos) {
  1581. return entry.second;
  1582. }
  1583. }
  1584. return config.default_subgroup_size;
  1585. }
  1586. }
  1587. return 0; // If no matching configuration is found
  1588. }
  1589. static void ggml_vk_load_shaders(vk_device& device) {
  1590. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1591. // some shaders have a minimum subgroup size
  1592. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1593. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1594. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1595. // mulmat
  1596. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1597. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1598. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1599. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1600. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1601. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1602. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1603. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1604. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1605. uint32_t l_align, m_align, s_align;
  1606. if (device->coopmat2) {
  1607. // spec constants and tile sizes for non-quant matmul/matmul_id
  1608. l_warptile = { 256, 128, 256, 64, 1 };
  1609. m_warptile = { 256, 128, 128, 64, 0 };
  1610. s_warptile = { 128, 64, 64, 64, 0 };
  1611. l_wg_denoms = {128, 256, 1 };
  1612. m_wg_denoms = {128, 128, 1 };
  1613. s_wg_denoms = { 64, 64, 1 };
  1614. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1615. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1616. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1617. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1618. l_mmq_wg_denoms = { 128, 256, 1 };
  1619. m_mmq_wg_denoms = { 128, 128, 1 };
  1620. s_mmq_wg_denoms = { 32, 64, 1 };
  1621. // spec constants and tile sizes for quant matmul (Qi_K)
  1622. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1623. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1624. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1625. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1626. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1627. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1628. // spec constants and tile sizes for quant matmul_id
  1629. l_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1630. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1631. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1632. l_mmqid_wg_denoms = { 128, 64, 1 };
  1633. m_mmqid_wg_denoms = { 128, 64, 1 };
  1634. s_mmqid_wg_denoms = { 128, 64, 1 };
  1635. l_align = 128;
  1636. m_align = 64;
  1637. s_align = 32;
  1638. } else {
  1639. // Matrix cores require different warp group sizes
  1640. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1641. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1642. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1643. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1644. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1645. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1646. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1647. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1648. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1649. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1650. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1651. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1652. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1653. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1654. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1655. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  1656. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, 2, 2, 1, subgroup_size_8 };
  1657. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, 2, 1, 1, subgroup_size_8 };
  1658. // chip specific tuning
  1659. if ((device->architecture == AMD_GCN) && (device->driver_id != vk::DriverId::eAmdProprietary)) {
  1660. m_warptile_mmq = m_warptile_mmq_int = { 256, 64, 64, 32, 16, 16, 2, 2, 2, 1, 16 };
  1661. }
  1662. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1663. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1664. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1665. l_align = 128;
  1666. m_align = 64;
  1667. s_align = 32;
  1668. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1669. ggml_type t = (ggml_type)i;
  1670. // Disable medium and large matrix multiplication if not enough shared memory is available
  1671. // Check mmq warptiles as the largest configuration
  1672. // Throw an error if not enough for any matrix multiplication is available
  1673. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1674. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1675. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1676. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1677. device->mul_mat_m[i] = false;
  1678. device->mul_mat_l[i] = false;
  1679. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1680. device->mul_mat_l[i] = false;
  1681. }
  1682. // Disable mul_mat_id if not enough shared memory is available
  1683. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1684. device->mul_mat_id_s[i] = false;
  1685. device->mul_mat_id_m[i] = false;
  1686. device->mul_mat_id_l[i] = false;
  1687. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1688. device->mul_mat_id_m[i] = false;
  1689. device->mul_mat_id_l[i] = false;
  1690. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1691. device->mul_mat_id_l[i] = false;
  1692. }
  1693. }
  1694. }
  1695. if (!device->pipeline_matmul_f32) {
  1696. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1697. }
  1698. if (!device->pipeline_matmul_f32_f16) {
  1699. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1700. }
  1701. if (!device->pipeline_matmul_id_f32) {
  1702. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1703. }
  1704. if (!device->pipeline_matmul_bf16) {
  1705. device->pipeline_matmul_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1706. }
  1707. if (!device->pipeline_matmul_id_bf16) {
  1708. device->pipeline_matmul_id_bf16 = std::make_shared<vk_matmul_pipeline_struct>();
  1709. }
  1710. std::vector<std::future<void>> compiles;
  1711. 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,
  1712. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1713. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1714. if (!require_full_subgroups && required_subgroup_size == 0) {
  1715. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1716. }
  1717. if (!pipeline) {
  1718. pipeline = std::make_shared<vk_pipeline_struct>();
  1719. pipeline->name = name;
  1720. pipeline->parameter_count = parameter_count;
  1721. pipeline->push_constant_size = push_constant_size;
  1722. pipeline->wg_denoms = wg_denoms;
  1723. pipeline->align = align;
  1724. }
  1725. if (!pipeline->needed || pipeline->compiled) {
  1726. return;
  1727. }
  1728. {
  1729. // wait until fewer than N compiles are in progress
  1730. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1731. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1732. while (compile_count >= N) {
  1733. compile_count_cond.wait(guard);
  1734. }
  1735. compile_count++;
  1736. }
  1737. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1738. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1739. };
  1740. 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> {
  1741. return {fa_rows_cols(path, hsk, hsv, clamp, type, small_rows)[0], 1, 1};
  1742. };
  1743. 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> {
  1744. // For large number of rows, 128 invocations seems to work best.
  1745. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1746. // can't use 256 for D==80.
  1747. // For scalar, use 128 (arbitrary)
  1748. // The same D_split value is used for both HSK and HSV, so just base it on the union of the LSBs.
  1749. const uint32_t D = (hsk|hsv);
  1750. uint32_t wg_size = (path == FA_SCALAR || path == FA_COOPMAT1)
  1751. ? scalar_flash_attention_workgroup_size
  1752. : ((small_rows && (D % 32) == 0) ? 256 : 128);
  1753. auto rows_cols = fa_rows_cols(path, hsk, hsv, clamp, type, small_rows);
  1754. // D_split can't be larger than a subgroup because we use subgroupShuffle to reduce it.
  1755. // D_split can't be larger than the LSB of D divided by 4 due to vectorization in the shader.
  1756. const uint32_t D_lsb = D ^ (D & (D-1));
  1757. uint32_t D_split = std::min(std::min(device->subgroup_size, 8u), D_lsb / 4);
  1758. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  1759. GGML_ASSERT((GGML_KQ_MASK_PAD % rows_cols[0]) == 0);
  1760. return {wg_size, rows_cols[0], rows_cols[1], hsk, hsv, clamp, D_split};
  1761. };
  1762. #define CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, HSK, HSV, HEAD_SIZES) \
  1763. 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)); \
  1764. 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)); \
  1765. 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)); \
  1766. 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)); \
  1767. 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)); \
  1768. 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)); \
  1769. 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)); \
  1770. 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)); \
  1771. #define CREATE_FA(TYPE, NAMELC, FAPATH, SUFFIX) \
  1772. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 64, 64, 64) \
  1773. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 80, 80, 80) \
  1774. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 96, 96, 96) \
  1775. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 112, 112, 112) \
  1776. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 128, 128, 128) \
  1777. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 192, 192) \
  1778. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 192, 128, 192_128) \
  1779. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 256, 256, 256) \
  1780. CREATE_FA2(TYPE, NAMELC, FAPATH, SUFFIX, 576, 512, 576_512)
  1781. CREATE_FA(GGML_TYPE_F16, f16, FA_SCALAR, )
  1782. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_SCALAR, )
  1783. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_SCALAR, )
  1784. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1785. if (device->coopmat1_fa_support) {
  1786. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT1, _cm1)
  1787. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT1, _cm1)
  1788. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT1, _cm1)
  1789. }
  1790. #endif
  1791. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1792. if (device->coopmat2) {
  1793. CREATE_FA(GGML_TYPE_F16, f16, FA_COOPMAT2, _cm2)
  1794. CREATE_FA(GGML_TYPE_Q4_0, q4_0, FA_COOPMAT2, _cm2)
  1795. CREATE_FA(GGML_TYPE_Q4_1, q4_1, FA_COOPMAT2, _cm2)
  1796. CREATE_FA(GGML_TYPE_Q5_0, q5_0, FA_COOPMAT2, _cm2)
  1797. CREATE_FA(GGML_TYPE_Q5_1, q5_1, FA_COOPMAT2, _cm2)
  1798. CREATE_FA(GGML_TYPE_Q8_0, q8_0, FA_COOPMAT2, _cm2)
  1799. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl, FA_COOPMAT2, _cm2)
  1800. }
  1801. #endif
  1802. #undef CREATE_FA2
  1803. #undef CREATE_FA
  1804. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1805. if (device->coopmat2) {
  1806. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1807. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1808. 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); \
  1809. 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); \
  1810. 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); \
  1811. 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); \
  1812. 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); \
  1813. 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); \
  1814. // Create 2 variants, {f16,f32} accumulator
  1815. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1816. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1817. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1818. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1819. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1820. if (device->coopmat_bf16_support) {
  1821. CREATE_MM(pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1822. }
  1823. #endif
  1824. 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)
  1825. 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)
  1826. 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)
  1827. 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)
  1828. 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)
  1829. 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)
  1830. 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)
  1831. 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)
  1832. 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)
  1833. 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)
  1834. 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)
  1835. 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)
  1836. 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)
  1837. 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)
  1838. 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)
  1839. 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)
  1840. 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)
  1841. 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)
  1842. 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)
  1843. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1844. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1845. if (device->coopmat_bf16_support) {
  1846. CREATE_MM(pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1847. }
  1848. #endif
  1849. 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)
  1850. 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)
  1851. 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)
  1852. 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)
  1853. 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)
  1854. 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)
  1855. 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)
  1856. 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)
  1857. 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)
  1858. 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)
  1859. 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)
  1860. 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)
  1861. 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)
  1862. 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)
  1863. 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)
  1864. 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)
  1865. 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)
  1866. 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)
  1867. 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)
  1868. #undef CREATE_MM
  1869. #undef CREATE_MM2
  1870. } else
  1871. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1872. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1873. if (device->coopmat_support) {
  1874. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1875. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1876. if (device->mul_mat ## ID ## _l[TYPE]) \
  1877. 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); \
  1878. if (device->mul_mat ## ID ## _m[TYPE]) \
  1879. 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); \
  1880. if (device->mul_mat ## ID ## _s[TYPE]) \
  1881. 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); \
  1882. if (device->mul_mat ## ID ## _l[TYPE]) \
  1883. 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); \
  1884. if (device->mul_mat ## ID ## _m[TYPE]) \
  1885. 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); \
  1886. if (device->mul_mat ## ID ## _s[TYPE]) \
  1887. 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); \
  1888. // Create 2 variants, {f16,f32} accumulator
  1889. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1890. if (device->coopmat_acc_f16_support) { \
  1891. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1892. } \
  1893. if (device->coopmat_acc_f32_support) { \
  1894. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1895. } \
  1896. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1897. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1898. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1899. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1900. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1901. if (device->coopmat_bf16_support) {
  1902. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, )
  1903. }
  1904. #endif
  1905. if (device->coopmat_acc_f16_support) {
  1906. 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, );
  1907. 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, );
  1908. 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, );
  1909. 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, );
  1910. 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, );
  1911. 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, );
  1912. 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, );
  1913. 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, );
  1914. 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, );
  1915. 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, );
  1916. 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, );
  1917. 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, );
  1918. 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, );
  1919. 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, );
  1920. 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, );
  1921. 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, );
  1922. 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, );
  1923. 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, );
  1924. 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, );
  1925. } else {
  1926. 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, );
  1927. 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, );
  1928. 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, );
  1929. 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, );
  1930. 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, );
  1931. 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, );
  1932. 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, );
  1933. 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, );
  1934. 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, );
  1935. 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, );
  1936. 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, );
  1937. 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, );
  1938. 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, );
  1939. 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, );
  1940. 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, );
  1941. 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, );
  1942. 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, );
  1943. 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, );
  1944. 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, );
  1945. }
  1946. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1947. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1948. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1949. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  1950. if (device->coopmat_bf16_support) {
  1951. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1952. }
  1953. #endif
  1954. if (device->coopmat_acc_f16_support) {
  1955. 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);
  1956. 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);
  1957. 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);
  1958. 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);
  1959. 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);
  1960. 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);
  1961. 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);
  1962. 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);
  1963. 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);
  1964. 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);
  1965. 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);
  1966. 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);
  1967. 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);
  1968. 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);
  1969. 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);
  1970. 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);
  1971. 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);
  1972. 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);
  1973. 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);
  1974. } else {
  1975. 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);
  1976. 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);
  1977. 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);
  1978. 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);
  1979. 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);
  1980. 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);
  1981. 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);
  1982. 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);
  1983. 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);
  1984. 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);
  1985. 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);
  1986. 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);
  1987. 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);
  1988. 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);
  1989. 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);
  1990. 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);
  1991. 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);
  1992. 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);
  1993. 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);
  1994. }
  1995. #undef CREATE_MM2
  1996. #undef CREATE_MM
  1997. } else
  1998. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1999. if (device->fp16) {
  2000. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2001. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2002. if (device->mul_mat ## ID ## _l[TYPE]) \
  2003. 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); \
  2004. if (device->mul_mat ## ID ## _m[TYPE]) \
  2005. 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); \
  2006. if (device->mul_mat ## ID ## _s[TYPE]) \
  2007. 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); \
  2008. if (device->mul_mat ## ID ## _l[TYPE]) \
  2009. 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); \
  2010. if (device->mul_mat ## ID ## _m[TYPE]) \
  2011. 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); \
  2012. if (device->mul_mat ## ID ## _s[TYPE]) \
  2013. 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); \
  2014. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2015. if (device->mul_mat ## ID ## _l[TYPE]) { \
  2016. 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); \
  2017. 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); \
  2018. } \
  2019. if (device->mul_mat ## ID ## _m[TYPE]) { \
  2020. 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); \
  2021. 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); \
  2022. } \
  2023. if (device->mul_mat ## ID ## _s[TYPE]) { \
  2024. 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); \
  2025. 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); \
  2026. } \
  2027. // Create 2 variants, {f16,f32} accumulator
  2028. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2029. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2030. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2031. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2032. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2033. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2034. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2035. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2036. 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, );
  2037. 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, );
  2038. 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, );
  2039. 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, );
  2040. 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, );
  2041. 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, );
  2042. 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, );
  2043. 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, );
  2044. 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, );
  2045. 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, );
  2046. 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, );
  2047. 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, );
  2048. 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, );
  2049. 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, );
  2050. 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, );
  2051. 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, );
  2052. 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, );
  2053. 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, );
  2054. 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, );
  2055. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2056. if (device->integer_dot_product) {
  2057. 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, );
  2058. 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, );
  2059. 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, );
  2060. 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, );
  2061. 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, );
  2062. }
  2063. #endif
  2064. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2065. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2066. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2067. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2068. 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);
  2069. 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);
  2070. 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);
  2071. 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);
  2072. 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);
  2073. 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);
  2074. 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);
  2075. 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);
  2076. 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);
  2077. 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);
  2078. 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);
  2079. 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);
  2080. 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);
  2081. 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);
  2082. 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);
  2083. 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);
  2084. 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);
  2085. 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);
  2086. 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);
  2087. #undef CREATE_MM2
  2088. #undef CREATE_MMQ
  2089. #undef CREATE_MM
  2090. } else {
  2091. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  2092. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2093. if (device->mul_mat ## ID ## _l[TYPE]) \
  2094. 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); \
  2095. if (device->mul_mat ## ID ## _m[TYPE]) \
  2096. 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); \
  2097. if (device->mul_mat ## ID ## _s[TYPE]) \
  2098. 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); \
  2099. if (device->mul_mat ## ID ## _l[TYPE]) \
  2100. 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); \
  2101. if (device->mul_mat ## ID ## _m[TYPE]) \
  2102. 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); \
  2103. if (device->mul_mat ## ID ## _s[TYPE]) \
  2104. 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); \
  2105. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  2106. if (device->mul_mat ## ID ## _l[TYPE]) \
  2107. 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); \
  2108. if (device->mul_mat ## ID ## _m[TYPE]) \
  2109. 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); \
  2110. if (device->mul_mat ## ID ## _s[TYPE]) \
  2111. 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); \
  2112. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2113. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2114. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2115. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2116. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2117. 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, );
  2118. 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, );
  2119. 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, );
  2120. 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, );
  2121. 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, );
  2122. 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, );
  2123. 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, );
  2124. 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, );
  2125. 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, );
  2126. 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, );
  2127. 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, );
  2128. 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, );
  2129. 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, );
  2130. 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, );
  2131. 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, );
  2132. 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, );
  2133. 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, );
  2134. 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, );
  2135. 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, );
  2136. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2137. if (device->integer_dot_product) {
  2138. 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, );
  2139. 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, );
  2140. 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, );
  2141. 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, );
  2142. 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, );
  2143. }
  2144. #endif
  2145. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2146. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  2147. 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);
  2148. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2149. 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);
  2150. 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);
  2151. 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);
  2152. 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);
  2153. 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);
  2154. 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);
  2155. 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);
  2156. 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);
  2157. 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);
  2158. 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);
  2159. 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);
  2160. 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);
  2161. 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);
  2162. 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);
  2163. 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);
  2164. 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);
  2165. 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);
  2166. 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);
  2167. 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);
  2168. }
  2169. // reusing CREATE_MM from the fp32 path
  2170. if ((device->coopmat2 || device->coopmat_support)
  2171. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2172. && !device->coopmat_bf16_support
  2173. #endif
  2174. ) {
  2175. // use scalar tile sizes
  2176. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, 4, 4, 1, subgroup_size_8 };
  2177. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, 4, 2, 1, subgroup_size_8 };
  2178. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, 2, 2, 1, subgroup_size_8 };
  2179. l_wg_denoms = {128, 128, 1 };
  2180. m_wg_denoms = { 64, 64, 1 };
  2181. s_wg_denoms = { 32, 32, 1 };
  2182. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_bf16, matmul_bf16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  2183. CREATE_MM(GGML_TYPE_BF16, pipeline_matmul_id_bf16, matmul_id_bf16, , wg_denoms, warptile, vk_mat_mat_id_push_constants, 4, _id);
  2184. }
  2185. #undef CREATE_MM
  2186. // mul mat vec
  2187. // the number of rows computed per shader depends on GPU model and quant
  2188. uint32_t rm_stdq = 1;
  2189. uint32_t rm_kq = 2;
  2190. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  2191. if (device->architecture == AMD_GCN) {
  2192. rm_stdq = 2;
  2193. rm_kq = 4;
  2194. }
  2195. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  2196. rm_stdq = 2;
  2197. uint32_t rm_iq = 2 * rm_kq;
  2198. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  2199. 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);
  2200. 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);
  2201. 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);
  2202. 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);
  2203. 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);
  2204. 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);
  2205. 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);
  2206. 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);
  2207. 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);
  2208. 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);
  2209. 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);
  2210. 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);
  2211. 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);
  2212. 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);
  2213. 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);
  2214. 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);
  2215. 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);
  2216. 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);
  2217. 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);
  2218. 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);
  2219. 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);
  2220. 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);
  2221. 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);
  2222. 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);
  2223. 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);
  2224. 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);
  2225. 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);
  2226. 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);
  2227. 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);
  2228. 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);
  2229. 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);
  2230. 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);
  2231. 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);
  2232. 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);
  2233. 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);
  2234. 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);
  2235. 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);
  2236. 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);
  2237. 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);
  2238. 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);
  2239. 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);
  2240. 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);
  2241. 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);
  2242. 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);
  2243. }
  2244. 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);
  2245. 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);
  2246. 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);
  2247. 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);
  2248. 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);
  2249. 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);
  2250. 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);
  2251. 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);
  2252. 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);
  2253. 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);
  2254. 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);
  2255. 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);
  2256. 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);
  2257. 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);
  2258. 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);
  2259. 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);
  2260. 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);
  2261. 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);
  2262. 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);
  2263. 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);
  2264. 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);
  2265. 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);
  2266. // dequant shaders
  2267. 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);
  2268. 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);
  2269. 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);
  2270. 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);
  2271. 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);
  2272. 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);
  2273. 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);
  2274. 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);
  2275. 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);
  2276. 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);
  2277. 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);
  2278. 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);
  2279. 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);
  2280. 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);
  2281. 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);
  2282. 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);
  2283. 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);
  2284. 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);
  2285. 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);
  2286. 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);
  2287. // get_rows
  2288. 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);
  2289. 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);
  2290. 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);
  2291. 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);
  2292. 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);
  2293. 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);
  2294. 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);
  2295. 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);
  2296. 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);
  2297. 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);
  2298. 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);
  2299. 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);
  2300. 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);
  2301. 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);
  2302. 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);
  2303. 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);
  2304. 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);
  2305. 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);
  2306. 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);
  2307. 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);
  2308. 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);
  2309. 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);
  2310. 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);
  2311. 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);
  2312. 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);
  2313. 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);
  2314. 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);
  2315. 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);
  2316. 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);
  2317. 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);
  2318. 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);
  2319. 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);
  2320. 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);
  2321. 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);
  2322. 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);
  2323. 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, 3 * sizeof(uint32_t), {1, 1, 1}, {}, 1, true);
  2324. 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);
  2325. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2326. if (device->subgroup_add && device->subgroup_require_full_support) {
  2327. 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);
  2328. } else {
  2329. 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);
  2330. }
  2331. }
  2332. 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);
  2333. 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);
  2334. 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);
  2335. 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);
  2336. 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);
  2337. 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);
  2338. 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);
  2339. 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);
  2340. 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);
  2341. 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);
  2342. 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);
  2343. 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);
  2344. 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);
  2345. 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);
  2346. 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);
  2347. 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);
  2348. 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);
  2349. if (device->float_controls_rte_fp16) {
  2350. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2351. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2352. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2353. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2354. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2355. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2356. } else {
  2357. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2358. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2359. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2360. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2361. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2362. 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), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2363. }
  2364. 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);
  2365. 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);
  2366. 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);
  2367. 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);
  2368. 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);
  2369. 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);
  2370. auto get_suffix = [](bool src0_f16, bool src1_f16, bool dst_f16) {
  2371. std::string s;
  2372. s += std::string(src0_f16 ? "_f16" : "_f32");
  2373. s += std::string(src1_f16 ? "_f16" : "_f32");
  2374. s += std::string(dst_f16 ? "_f16" : "_f32");
  2375. return s;
  2376. };
  2377. #define CREATE_BINARY(name, namemod, spec) \
  2378. for (int s0 : {0,1}) for (int s1 : {0,1}) for (int d : {0,1}) \
  2379. ggml_vk_create_pipeline(device, device->pipeline_ ## name ## namemod[s0][s1][d], \
  2380. #name + get_suffix(s0, s1, d) + #namemod, name ## _len[s0][s1][d], name ## _data[s0][s1][d], \
  2381. "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, spec, 1);
  2382. CREATE_BINARY(add, , {0})
  2383. CREATE_BINARY(add, _norepeat, {1})
  2384. CREATE_BINARY(sub, , {0})
  2385. CREATE_BINARY(sub, _norepeat, {1})
  2386. CREATE_BINARY(mul, , {0})
  2387. CREATE_BINARY(mul, _norepeat, {1})
  2388. CREATE_BINARY(div, , {0})
  2389. CREATE_BINARY(div, _norepeat, {1})
  2390. #undef CREATE_BINARY
  2391. 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);
  2392. 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);
  2393. 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);
  2394. 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);
  2395. ggml_vk_create_pipeline(device, device->pipeline_upscale_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {}, 1);
  2396. 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);
  2397. 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);
  2398. 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);
  2399. 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);
  2400. 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);
  2401. 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);
  2402. 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);
  2403. 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);
  2404. #define CREATE_UNARY(name) \
  2405. 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); \
  2406. 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);
  2407. CREATE_UNARY(gelu)
  2408. CREATE_UNARY(gelu_erf)
  2409. CREATE_UNARY(gelu_quick)
  2410. CREATE_UNARY(silu)
  2411. CREATE_UNARY(relu)
  2412. CREATE_UNARY(tanh)
  2413. CREATE_UNARY(sigmoid)
  2414. #undef CREATE_UNARY
  2415. #define CREATE_GLU(name) \
  2416. 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); \
  2417. 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);
  2418. CREATE_GLU(geglu)
  2419. CREATE_GLU(reglu)
  2420. CREATE_GLU(swiglu)
  2421. CREATE_GLU(geglu_erf)
  2422. CREATE_GLU(geglu_quick)
  2423. #undef CREATE_GLU
  2424. 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);
  2425. 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);
  2426. 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);
  2427. 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);
  2428. 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);
  2429. 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);
  2430. 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);
  2431. 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);
  2432. 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);
  2433. 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);
  2434. 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);
  2435. 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);
  2436. if (device->float_controls_rte_fp16) {
  2437. 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);
  2438. 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);
  2439. 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);
  2440. 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);
  2441. } else {
  2442. 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);
  2443. 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);
  2444. 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);
  2445. 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);
  2446. }
  2447. 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);
  2448. 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);
  2449. 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);
  2450. 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);
  2451. 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);
  2452. if (device->float_controls_rte_fp16) {
  2453. 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);
  2454. } else {
  2455. 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);
  2456. }
  2457. 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);
  2458. 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);
  2459. 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);
  2460. 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);
  2461. 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);
  2462. 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);
  2463. 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);
  2464. 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);
  2465. for (auto &c : compiles) {
  2466. c.wait();
  2467. }
  2468. device->need_compiles = false;
  2469. }
  2470. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2471. static vk_device ggml_vk_get_device(size_t idx) {
  2472. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2473. if (vk_instance.devices[idx] == nullptr) {
  2474. VK_LOG_DEBUG("Initializing new vk_device");
  2475. vk_device device = std::make_shared<vk_device_struct>();
  2476. vk_instance.devices[idx] = device;
  2477. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2478. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2479. #endif
  2480. if (vk_perf_logger_enabled) {
  2481. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2482. }
  2483. size_t dev_num = vk_instance.device_indices[idx];
  2484. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2485. if (dev_num >= physical_devices.size()) {
  2486. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2487. throw std::runtime_error("Device not found");
  2488. }
  2489. device->physical_device = physical_devices[dev_num];
  2490. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2491. device->architecture = get_device_architecture(device->physical_device);
  2492. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2493. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2494. bool fp16_storage = false;
  2495. bool fp16_compute = false;
  2496. bool maintenance4_support = false;
  2497. bool sm_builtins = false;
  2498. bool amd_shader_core_properties2 = false;
  2499. bool pipeline_robustness = false;
  2500. bool coopmat2_support = false;
  2501. device->coopmat_support = false;
  2502. device->integer_dot_product = false;
  2503. bool bfloat16_support = false;
  2504. for (const auto& properties : ext_props) {
  2505. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2506. maintenance4_support = true;
  2507. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2508. fp16_storage = true;
  2509. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2510. fp16_compute = true;
  2511. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2512. sm_builtins = true;
  2513. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2514. amd_shader_core_properties2 = true;
  2515. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2516. pipeline_robustness = true;
  2517. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2518. device->subgroup_size_control = true;
  2519. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2520. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2521. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2522. device->coopmat_support = true;
  2523. device->coopmat_m = 0;
  2524. device->coopmat_n = 0;
  2525. device->coopmat_k = 0;
  2526. #endif
  2527. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2528. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2529. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2530. coopmat2_support = true;
  2531. #endif
  2532. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2533. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2534. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2535. device->integer_dot_product = true;
  2536. #endif
  2537. #if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2538. } else if (strcmp("VK_KHR_shader_bfloat16", properties.extensionName) == 0 &&
  2539. !getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2540. bfloat16_support = true;
  2541. #endif
  2542. }
  2543. }
  2544. vk::PhysicalDeviceProperties2 props2;
  2545. vk::PhysicalDeviceMaintenance3Properties props3;
  2546. vk::PhysicalDeviceMaintenance4Properties props4;
  2547. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2548. vk::PhysicalDeviceDriverProperties driver_props;
  2549. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2550. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2551. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2552. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2553. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2554. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2555. props2.pNext = &props3;
  2556. props3.pNext = &subgroup_props;
  2557. subgroup_props.pNext = &driver_props;
  2558. driver_props.pNext = &vk11_props;
  2559. vk11_props.pNext = &vk12_props;
  2560. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2561. if (maintenance4_support) {
  2562. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2563. last_struct = (VkBaseOutStructure *)&props4;
  2564. }
  2565. if (sm_builtins) {
  2566. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2567. last_struct = (VkBaseOutStructure *)&sm_props;
  2568. }
  2569. if (amd_shader_core_properties2) {
  2570. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2571. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2572. }
  2573. if (device->subgroup_size_control) {
  2574. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2575. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2576. }
  2577. #if defined(VK_NV_cooperative_matrix2)
  2578. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2579. if (coopmat2_support) {
  2580. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2581. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2582. }
  2583. #endif
  2584. if (device->integer_dot_product) {
  2585. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2586. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2587. }
  2588. device->physical_device.getProperties2(&props2);
  2589. device->properties = props2.properties;
  2590. device->vendor_id = device->properties.vendorID;
  2591. device->driver_id = driver_props.driverID;
  2592. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2593. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2594. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2595. } else if (maintenance4_support) {
  2596. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2597. } else {
  2598. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2599. }
  2600. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2601. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2602. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2603. } else {
  2604. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2605. device->suballocation_block_size = 1024*1024*1024;
  2606. }
  2607. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2608. device->subgroup_size = subgroup_props.subgroupSize;
  2609. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2610. if (sm_builtins) {
  2611. device->shader_core_count = sm_props.shaderSMCount;
  2612. } else if (amd_shader_core_properties2) {
  2613. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2614. } else {
  2615. device->shader_core_count = 0;
  2616. }
  2617. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2618. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2619. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2620. device->subgroup_shuffle = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2621. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eShuffle);
  2622. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2623. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2624. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2625. device->coopmat_support = false;
  2626. }
  2627. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2628. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2629. // Try to find a non-graphics compute queue and transfer-focused queues
  2630. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2631. 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);
  2632. const float priorities[] = { 1.0f, 1.0f };
  2633. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2634. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2635. if (compute_queue_family_index != transfer_queue_family_index) {
  2636. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2637. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2638. } else if(!device->single_queue) {
  2639. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2640. } else {
  2641. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2642. }
  2643. vk::DeviceCreateInfo device_create_info;
  2644. std::vector<const char *> device_extensions;
  2645. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2646. VkPhysicalDeviceFeatures2 device_features2;
  2647. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2648. device_features2.pNext = nullptr;
  2649. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2650. VkPhysicalDeviceVulkan11Features vk11_features;
  2651. vk11_features.pNext = nullptr;
  2652. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2653. device_features2.pNext = &vk11_features;
  2654. VkPhysicalDeviceVulkan12Features vk12_features;
  2655. vk12_features.pNext = nullptr;
  2656. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2657. vk11_features.pNext = &vk12_features;
  2658. last_struct = (VkBaseOutStructure *)&vk12_features;
  2659. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2660. pl_robustness_features.pNext = nullptr;
  2661. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2662. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2663. if (pipeline_robustness) {
  2664. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2665. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2666. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2667. }
  2668. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2669. subgroup_size_control_features.pNext = nullptr;
  2670. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2671. subgroup_size_control_features.computeFullSubgroups = false;
  2672. subgroup_size_control_features.subgroupSizeControl = false;
  2673. if (device->subgroup_size_control) {
  2674. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2675. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2676. }
  2677. #if defined(VK_KHR_cooperative_matrix)
  2678. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2679. coopmat_features.pNext = nullptr;
  2680. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2681. coopmat_features.cooperativeMatrix = VK_FALSE;
  2682. if (device->coopmat_support) {
  2683. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2684. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2685. }
  2686. #endif
  2687. #if defined(VK_NV_cooperative_matrix2)
  2688. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2689. coopmat2_features.pNext = nullptr;
  2690. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2691. if (coopmat2_support) {
  2692. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2693. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2694. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2695. }
  2696. #endif
  2697. #if defined(VK_KHR_shader_bfloat16)
  2698. VkPhysicalDeviceShaderBfloat16FeaturesKHR bfloat16_features {};
  2699. bfloat16_features.pNext = nullptr;
  2700. bfloat16_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_BFLOAT16_FEATURES_KHR;
  2701. if (bfloat16_support) {
  2702. last_struct->pNext = (VkBaseOutStructure *)&bfloat16_features;
  2703. last_struct = (VkBaseOutStructure *)&bfloat16_features;
  2704. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2705. }
  2706. #endif
  2707. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2708. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2709. if (maintenance4_support) {
  2710. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2711. last_struct = (VkBaseOutStructure *)&maint4_features;
  2712. device_extensions.push_back("VK_KHR_maintenance4");
  2713. }
  2714. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2715. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2716. if (device->integer_dot_product) {
  2717. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2718. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2719. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  2720. }
  2721. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2722. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2723. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2724. if (device->subgroup_size_control) {
  2725. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2726. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2727. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2728. }
  2729. device->subgroup_size_control = device->subgroup_size_control &&
  2730. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2731. subgroup_size_control_features.subgroupSizeControl;
  2732. if (device->subgroup_size_control) {
  2733. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2734. }
  2735. #if defined(VK_KHR_cooperative_matrix)
  2736. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2737. // coopmat1 fa shader currently assumes 32 invocations per subgroup
  2738. device->coopmat1_fa_support = device->coopmat_support && device->subgroup_require_full_support &&
  2739. device->subgroup_size_control && device->subgroup_min_size <= 32 &&
  2740. device->subgroup_max_size >= 32;
  2741. #endif
  2742. if (coopmat2_support) {
  2743. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2744. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2745. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2746. coopmat2_features.cooperativeMatrixReductions &&
  2747. coopmat2_features.cooperativeMatrixConversions &&
  2748. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2749. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2750. coopmat2_features.cooperativeMatrixBlockLoads &&
  2751. vk12_features.bufferDeviceAddress) {
  2752. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2753. uint32_t count = 0;
  2754. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2755. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2756. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2757. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2758. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2759. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2760. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2761. flexible_dimensions.resize(count, empty_prop);
  2762. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2763. bool found_fp16_128 = false,
  2764. found_fp16_256 = false,
  2765. found_fp32_128 = false,
  2766. found_fp32_256 = false;
  2767. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2768. // with 32x16x16 and 256 with 32x32x16.
  2769. for (auto &prop : flexible_dimensions) {
  2770. if (prop.saturatingAccumulation == VK_FALSE &&
  2771. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2772. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2773. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2774. if (prop.workgroupInvocations == 128 &&
  2775. prop.MGranularity <= 32 &&
  2776. prop.NGranularity <= 16 &&
  2777. prop.KGranularity <= 16) {
  2778. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2779. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2780. found_fp16_128 = true;
  2781. }
  2782. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2783. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2784. found_fp32_128 = true;
  2785. }
  2786. }
  2787. if (prop.workgroupInvocations == 256 &&
  2788. prop.MGranularity <= 32 &&
  2789. prop.NGranularity <= 32 &&
  2790. prop.KGranularity <= 16) {
  2791. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2792. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2793. found_fp16_256 = true;
  2794. }
  2795. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2796. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2797. found_fp32_256 = true;
  2798. }
  2799. }
  2800. }
  2801. }
  2802. if (found_fp16_128 && found_fp16_256 &&
  2803. found_fp32_128 && found_fp32_256 &&
  2804. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2805. device->coopmat2 = true;
  2806. }
  2807. }
  2808. #endif
  2809. }
  2810. if (!vk11_features.storageBuffer16BitAccess) {
  2811. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2812. throw std::runtime_error("Unsupported device");
  2813. }
  2814. device_extensions.push_back("VK_KHR_16bit_storage");
  2815. #ifdef GGML_VULKAN_VALIDATE
  2816. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2817. #endif
  2818. if (device->fp16) {
  2819. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2820. }
  2821. #if defined(VK_KHR_cooperative_matrix)
  2822. if (device->coopmat_support) {
  2823. // Query supported shapes
  2824. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2825. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2826. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2827. uint32_t cm_props_num;
  2828. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2829. cm_props.resize(cm_props_num);
  2830. for (auto& prop : cm_props) {
  2831. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2832. }
  2833. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2834. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2835. for (auto& prop : cm_props) {
  2836. 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));
  2837. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2838. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2839. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2840. ) {
  2841. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2842. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2843. // coopmat sizes not set yet
  2844. if (device->coopmat_m == 0) {
  2845. device->coopmat_acc_f32_support = true;
  2846. device->coopmat_m = prop.MSize;
  2847. device->coopmat_n = prop.NSize;
  2848. device->coopmat_k = prop.KSize;
  2849. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2850. // Only enable if shape is identical
  2851. device->coopmat_acc_f32_support = true;
  2852. }
  2853. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2854. device->coopmat_support_16x16x16_f32acc = true;
  2855. }
  2856. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2857. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2858. // coopmat sizes not set yet
  2859. if (device->coopmat_m == 0) {
  2860. device->coopmat_acc_f16_support = true;
  2861. device->coopmat_m = prop.MSize;
  2862. device->coopmat_n = prop.NSize;
  2863. device->coopmat_k = prop.KSize;
  2864. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2865. // Only enable if shape is identical
  2866. device->coopmat_acc_f16_support = true;
  2867. }
  2868. if (prop.MSize == 16 && prop.NSize == 16 && prop.KSize == 16) {
  2869. device->coopmat_support_16x16x16_f16acc = true;
  2870. }
  2871. }
  2872. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  2873. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  2874. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  2875. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  2876. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  2877. device->coopmat_int_m == 0
  2878. ) {
  2879. device->coopmat_int_support = true;
  2880. device->coopmat_int_m = prop.MSize;
  2881. device->coopmat_int_n = prop.NSize;
  2882. device->coopmat_int_k = prop.KSize;
  2883. }
  2884. #if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
  2885. if (prop.AType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2886. prop.BType == VK_COMPONENT_TYPE_BFLOAT16_KHR &&
  2887. prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2888. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2889. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2890. ) {
  2891. // coopmat sizes not set yet
  2892. if (device->coopmat_m == 0) {
  2893. device->coopmat_bf16_support = true;
  2894. device->coopmat_m = prop.MSize;
  2895. device->coopmat_n = prop.NSize;
  2896. device->coopmat_k = prop.KSize;
  2897. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2898. // Only enable if shape is identical
  2899. device->coopmat_bf16_support = true;
  2900. }
  2901. }
  2902. #endif
  2903. }
  2904. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2905. // No suitable matmul mode found
  2906. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2907. device->coopmat_support = false;
  2908. }
  2909. if (getenv("GGML_VK_DISABLE_BFLOAT16")) {
  2910. device->coopmat_bf16_support = false;
  2911. }
  2912. }
  2913. if (device->coopmat_support) {
  2914. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2915. }
  2916. #if defined(VK_KHR_shader_bfloat16)
  2917. if (device->coopmat_bf16_support) {
  2918. device_extensions.push_back("VK_KHR_shader_bfloat16");
  2919. }
  2920. #endif
  2921. #endif
  2922. device->name = GGML_VK_NAME + std::to_string(idx);
  2923. device_create_info = {
  2924. vk::DeviceCreateFlags(),
  2925. device_queue_create_infos,
  2926. {},
  2927. device_extensions
  2928. };
  2929. device_create_info.setPNext(&device_features2);
  2930. device->device = device->physical_device.createDevice(device_create_info);
  2931. // Queues
  2932. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2933. // Shaders
  2934. // Disable matmul tile sizes early if performance low or not supported
  2935. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2936. switch (device->vendor_id) {
  2937. #ifndef GGML_VULKAN_RUN_TESTS
  2938. case VK_VENDOR_ID_AMD:
  2939. case VK_VENDOR_ID_INTEL:
  2940. device->mul_mat_l[i] = false;
  2941. device->mul_mat_m[i] = true;
  2942. device->mul_mat_s[i] = true;
  2943. device->mul_mat_id_l[i] = false;
  2944. device->mul_mat_id_m[i] = true;
  2945. device->mul_mat_id_s[i] = true;
  2946. break;
  2947. case VK_VENDOR_ID_APPLE:
  2948. device->mul_mat_l[i] = false;
  2949. device->mul_mat_m[i] = true;
  2950. device->mul_mat_s[i] = false;
  2951. device->mul_mat_id_l[i] = false;
  2952. device->mul_mat_id_m[i] = true;
  2953. device->mul_mat_id_s[i] = false;
  2954. break;
  2955. #endif
  2956. default:
  2957. device->mul_mat_l[i] = true;
  2958. device->mul_mat_m[i] = true;
  2959. device->mul_mat_s[i] = true;
  2960. device->mul_mat_id_l[i] = true;
  2961. device->mul_mat_id_m[i] = true;
  2962. device->mul_mat_id_s[i] = true;
  2963. break;
  2964. }
  2965. }
  2966. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  2967. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  2968. for (uint32_t i = 0; i < MAX_PARAMETER_COUNT; i++) {
  2969. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  2970. dsl_binding_flags.push_back({});
  2971. }
  2972. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  2973. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  2974. {},
  2975. dsl_binding);
  2976. descriptor_set_layout_create_info.setPNext(&dslbfci);
  2977. device->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  2978. ggml_vk_load_shaders(device);
  2979. if (!device->single_queue) {
  2980. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  2981. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  2982. } else {
  2983. // TODO: Use pointer or reference to avoid copy
  2984. device->transfer_queue.copyFrom(device->compute_queue);
  2985. device->transfer_queue.cmd_pool.init(device, &device->transfer_queue);
  2986. }
  2987. device->buffer_type = {
  2988. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  2989. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  2990. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  2991. };
  2992. device->fence = device->device.createFence({});
  2993. device->idx = idx;
  2994. return device;
  2995. }
  2996. return vk_instance.devices[idx];
  2997. }
  2998. static void ggml_vk_print_gpu_info(size_t idx) {
  2999. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3000. size_t dev_num = vk_instance.device_indices[idx];
  3001. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  3002. GGML_ASSERT(vk_instance_initialized);
  3003. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3004. if (dev_num >= devices.size()) {
  3005. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  3006. throw std::runtime_error("Device not found");
  3007. }
  3008. vk::PhysicalDevice physical_device = devices[dev_num];
  3009. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  3010. bool fp16_storage = false;
  3011. bool fp16_compute = false;
  3012. bool coopmat_support = false;
  3013. bool coopmat2_support = false;
  3014. bool integer_dot_product = false;
  3015. for (auto properties : ext_props) {
  3016. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  3017. fp16_storage = true;
  3018. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  3019. fp16_compute = true;
  3020. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3021. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  3022. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  3023. coopmat_support = true;
  3024. #endif
  3025. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  3026. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  3027. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  3028. coopmat2_support = true;
  3029. #endif
  3030. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  3031. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  3032. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  3033. integer_dot_product = true;
  3034. #endif
  3035. }
  3036. }
  3037. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  3038. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  3039. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  3040. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  3041. vk::PhysicalDeviceProperties2 props2;
  3042. vk::PhysicalDeviceMaintenance3Properties props3;
  3043. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  3044. vk::PhysicalDeviceDriverProperties driver_props;
  3045. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  3046. props2.pNext = &props3;
  3047. props3.pNext = &subgroup_props;
  3048. subgroup_props.pNext = &driver_props;
  3049. // Pointer to the last chain element
  3050. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  3051. if (integer_dot_product) {
  3052. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3053. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  3054. }
  3055. physical_device.getProperties2(&props2);
  3056. VkPhysicalDeviceFeatures2 device_features2;
  3057. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  3058. device_features2.pNext = nullptr;
  3059. VkPhysicalDeviceVulkan11Features vk11_features;
  3060. vk11_features.pNext = nullptr;
  3061. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  3062. device_features2.pNext = &vk11_features;
  3063. VkPhysicalDeviceVulkan12Features vk12_features;
  3064. vk12_features.pNext = nullptr;
  3065. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  3066. vk11_features.pNext = &vk12_features;
  3067. // Pointer to the last chain element
  3068. last_struct = (VkBaseOutStructure *)&vk12_features;
  3069. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3070. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  3071. coopmat_features.pNext = nullptr;
  3072. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  3073. coopmat_features.cooperativeMatrix = VK_FALSE;
  3074. if (coopmat_support) {
  3075. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  3076. last_struct = (VkBaseOutStructure *)&coopmat_features;
  3077. }
  3078. #endif
  3079. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  3080. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  3081. if (integer_dot_product) {
  3082. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3083. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  3084. }
  3085. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  3086. fp16 = fp16 && vk12_features.shaderFloat16;
  3087. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  3088. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  3089. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  3090. integer_dot_product = integer_dot_product
  3091. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  3092. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  3093. coopmat_support = coopmat_support
  3094. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  3095. && coopmat_features.cooperativeMatrix
  3096. #endif
  3097. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  3098. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  3099. std::string device_name = props2.properties.deviceName.data();
  3100. 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",
  3101. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  3102. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  3103. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  3104. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  3105. }
  3106. }
  3107. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3108. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  3109. static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
  3110. static void ggml_vk_instance_init() {
  3111. if (vk_instance_initialized) {
  3112. return;
  3113. }
  3114. VK_LOG_DEBUG("ggml_vk_instance_init()");
  3115. uint32_t api_version = vk::enumerateInstanceVersion();
  3116. if (api_version < VK_API_VERSION_1_2) {
  3117. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  3118. GGML_ABORT("fatal error");
  3119. }
  3120. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  3121. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  3122. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  3123. #ifdef __APPLE__
  3124. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  3125. #endif
  3126. const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
  3127. std::vector<const char*> layers;
  3128. if (validation_ext) {
  3129. layers.push_back("VK_LAYER_KHRONOS_validation");
  3130. }
  3131. std::vector<const char*> extensions;
  3132. if (validation_ext) {
  3133. extensions.push_back("VK_EXT_validation_features");
  3134. }
  3135. #ifdef __APPLE__
  3136. if (portability_enumeration_ext) {
  3137. extensions.push_back("VK_KHR_portability_enumeration");
  3138. }
  3139. #endif
  3140. if (debug_utils_ext) {
  3141. extensions.push_back("VK_EXT_debug_utils");
  3142. }
  3143. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  3144. #ifdef __APPLE__
  3145. if (portability_enumeration_ext) {
  3146. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  3147. }
  3148. #endif
  3149. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  3150. vk::ValidationFeaturesEXT validation_features;
  3151. if (validation_ext) {
  3152. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  3153. validation_features = {
  3154. features_enable,
  3155. {},
  3156. };
  3157. validation_features.setPNext(nullptr);
  3158. instance_create_info.setPNext(&validation_features);
  3159. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  3160. }
  3161. vk_instance.instance = vk::createInstance(instance_create_info);
  3162. vk_instance_initialized = true;
  3163. if (debug_utils_ext) {
  3164. vk_instance.debug_utils_support = true;
  3165. vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
  3166. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
  3167. vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
  3168. vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
  3169. vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
  3170. vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
  3171. }
  3172. vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
  3173. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  3174. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  3175. if (devices_env != nullptr) {
  3176. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  3177. std::string devices(devices_env);
  3178. std::replace(devices.begin(), devices.end(), ',', ' ');
  3179. std::stringstream ss(devices);
  3180. size_t tmp;
  3181. while (ss >> tmp) {
  3182. if(tmp >= num_available_devices) {
  3183. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  3184. throw std::runtime_error("Invalid Vulkan device index");
  3185. }
  3186. vk_instance.device_indices.push_back(tmp);
  3187. }
  3188. } else {
  3189. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  3190. // If no vulkan devices are found, return early
  3191. if (devices.empty()) {
  3192. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3193. return;
  3194. }
  3195. // Default to using all dedicated GPUs
  3196. for (size_t i = 0; i < devices.size(); i++) {
  3197. vk::PhysicalDeviceProperties2 new_props;
  3198. vk::PhysicalDeviceDriverProperties new_driver;
  3199. vk::PhysicalDeviceIDProperties new_id;
  3200. new_props.pNext = &new_driver;
  3201. new_driver.pNext = &new_id;
  3202. devices[i].getProperties2(&new_props);
  3203. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  3204. // Check if there are two physical devices corresponding to the same GPU
  3205. auto old_device = std::find_if(
  3206. vk_instance.device_indices.begin(),
  3207. vk_instance.device_indices.end(),
  3208. [&devices, &new_id](const size_t k){
  3209. vk::PhysicalDeviceProperties2 old_props;
  3210. vk::PhysicalDeviceIDProperties old_id;
  3211. old_props.pNext = &old_id;
  3212. devices[k].getProperties2(&old_props);
  3213. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  3214. }
  3215. );
  3216. if (old_device == vk_instance.device_indices.end()) {
  3217. vk_instance.device_indices.push_back(i);
  3218. } else {
  3219. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  3220. // This can cause error when splitting layers aross the devices, need to keep only 1
  3221. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  3222. vk::PhysicalDeviceProperties2 old_props;
  3223. vk::PhysicalDeviceDriverProperties old_driver;
  3224. old_props.pNext = &old_driver;
  3225. devices[*old_device].getProperties2(&old_props);
  3226. std::map<vk::DriverId, int> driver_priorities {};
  3227. int old_priority = std::numeric_limits<int>::max();
  3228. int new_priority = std::numeric_limits<int>::max();
  3229. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  3230. // Smaller number -> higher priority
  3231. switch (old_props.properties.vendorID) {
  3232. case VK_VENDOR_ID_AMD:
  3233. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  3234. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  3235. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  3236. break;
  3237. case VK_VENDOR_ID_INTEL:
  3238. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  3239. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  3240. break;
  3241. case VK_VENDOR_ID_NVIDIA:
  3242. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  3243. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  3244. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  3245. #endif
  3246. break;
  3247. }
  3248. if (driver_priorities.count(old_driver.driverID)) {
  3249. old_priority = driver_priorities[old_driver.driverID];
  3250. }
  3251. if (driver_priorities.count(new_driver.driverID)) {
  3252. new_priority = driver_priorities[new_driver.driverID];
  3253. }
  3254. if (new_priority < old_priority) {
  3255. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  3256. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  3257. vk_instance.device_indices.push_back(i);
  3258. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  3259. }
  3260. else {
  3261. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  3262. }
  3263. }
  3264. }
  3265. }
  3266. // If no dedicated GPUs found, fall back to the first non-CPU device.
  3267. // If only CPU devices are available, return without devices.
  3268. if (vk_instance.device_indices.empty()) {
  3269. for (size_t i = 0; i < devices.size(); i++) {
  3270. if (devices[i].getProperties().deviceType != vk::PhysicalDeviceType::eCpu) {
  3271. vk_instance.device_indices.push_back(i);
  3272. break;
  3273. }
  3274. }
  3275. }
  3276. if (vk_instance.device_indices.empty()) {
  3277. GGML_LOG_INFO("ggml_vulkan: No devices found.\n");
  3278. return;
  3279. }
  3280. }
  3281. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  3282. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  3283. ggml_vk_print_gpu_info(i);
  3284. }
  3285. }
  3286. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  3287. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  3288. ggml_vk_instance_init();
  3289. GGML_ASSERT(idx < vk_instance.device_indices.size());
  3290. ctx->name = GGML_VK_NAME + std::to_string(idx);
  3291. ctx->device = ggml_vk_get_device(idx);
  3292. ctx->semaphore_idx = 0;
  3293. ctx->event_idx = 0;
  3294. ctx->prealloc_size_x = 0;
  3295. ctx->prealloc_size_y = 0;
  3296. ctx->prealloc_size_split_k = 0;
  3297. ctx->fence = ctx->device->device.createFence({});
  3298. ctx->almost_ready_fence = ctx->device->device.createFence({});
  3299. ctx->compute_cmd_pool.init(ctx->device, &ctx->device->compute_queue);
  3300. ctx->transfer_cmd_pool.init(ctx->device, &ctx->device->transfer_queue);
  3301. #ifdef GGML_VULKAN_CHECK_RESULTS
  3302. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  3303. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  3304. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  3305. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  3306. #endif
  3307. }
  3308. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  3309. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  3310. switch (type) {
  3311. case GGML_TYPE_F32:
  3312. case GGML_TYPE_Q4_0:
  3313. case GGML_TYPE_Q4_1:
  3314. case GGML_TYPE_Q5_0:
  3315. case GGML_TYPE_Q5_1:
  3316. case GGML_TYPE_Q8_0:
  3317. case GGML_TYPE_Q2_K:
  3318. case GGML_TYPE_Q3_K:
  3319. case GGML_TYPE_Q4_K:
  3320. case GGML_TYPE_Q5_K:
  3321. case GGML_TYPE_Q6_K:
  3322. case GGML_TYPE_IQ1_S:
  3323. case GGML_TYPE_IQ1_M:
  3324. case GGML_TYPE_IQ2_XXS:
  3325. case GGML_TYPE_IQ2_XS:
  3326. case GGML_TYPE_IQ2_S:
  3327. case GGML_TYPE_IQ3_XXS:
  3328. case GGML_TYPE_IQ3_S:
  3329. case GGML_TYPE_IQ4_XS:
  3330. case GGML_TYPE_IQ4_NL:
  3331. break;
  3332. default:
  3333. return nullptr;
  3334. }
  3335. return ctx->device->pipeline_dequant[type];
  3336. }
  3337. 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) {
  3338. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ", " << prec << ")");
  3339. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3340. return ctx->device->pipeline_matmul_f32;
  3341. }
  3342. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  3343. return ctx->device->pipeline_matmul_f32_f16;
  3344. }
  3345. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3346. return ctx->device->pipeline_matmul_bf16;
  3347. }
  3348. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3349. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3350. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  3351. }
  3352. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3353. return ctx->device->pipeline_matmul_f16.f16acc;
  3354. }
  3355. } else {
  3356. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3357. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  3358. }
  3359. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3360. return ctx->device->pipeline_matmul_f16.f32acc;
  3361. }
  3362. }
  3363. // MMQ
  3364. if (src1_type == GGML_TYPE_Q8_1) {
  3365. 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;
  3366. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  3367. return nullptr;
  3368. }
  3369. return pipelines;
  3370. }
  3371. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  3372. return nullptr;
  3373. }
  3374. switch (src0_type) {
  3375. case GGML_TYPE_Q4_0:
  3376. case GGML_TYPE_Q4_1:
  3377. case GGML_TYPE_Q5_0:
  3378. case GGML_TYPE_Q5_1:
  3379. case GGML_TYPE_Q8_0:
  3380. case GGML_TYPE_Q2_K:
  3381. case GGML_TYPE_Q3_K:
  3382. case GGML_TYPE_Q4_K:
  3383. case GGML_TYPE_Q5_K:
  3384. case GGML_TYPE_Q6_K:
  3385. case GGML_TYPE_IQ1_S:
  3386. case GGML_TYPE_IQ1_M:
  3387. case GGML_TYPE_IQ2_XXS:
  3388. case GGML_TYPE_IQ2_XS:
  3389. case GGML_TYPE_IQ2_S:
  3390. case GGML_TYPE_IQ3_XXS:
  3391. case GGML_TYPE_IQ3_S:
  3392. case GGML_TYPE_IQ4_XS:
  3393. case GGML_TYPE_IQ4_NL:
  3394. break;
  3395. default:
  3396. return nullptr;
  3397. }
  3398. if (ctx->device->coopmat2) {
  3399. assert(src1_type == GGML_TYPE_F16);
  3400. 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;
  3401. }
  3402. if (ctx->device->coopmat_support) {
  3403. 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;
  3404. }
  3405. 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;
  3406. }
  3407. 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) {
  3408. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3409. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  3410. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  3411. switch (a_type) {
  3412. case GGML_TYPE_F32:
  3413. case GGML_TYPE_F16:
  3414. case GGML_TYPE_BF16:
  3415. case GGML_TYPE_Q4_0:
  3416. case GGML_TYPE_Q4_1:
  3417. case GGML_TYPE_Q5_0:
  3418. case GGML_TYPE_Q5_1:
  3419. case GGML_TYPE_Q8_0:
  3420. case GGML_TYPE_Q2_K:
  3421. case GGML_TYPE_Q3_K:
  3422. case GGML_TYPE_Q4_K:
  3423. case GGML_TYPE_Q5_K:
  3424. case GGML_TYPE_Q6_K:
  3425. case GGML_TYPE_IQ1_S:
  3426. case GGML_TYPE_IQ1_M:
  3427. case GGML_TYPE_IQ2_XXS:
  3428. case GGML_TYPE_IQ2_XS:
  3429. case GGML_TYPE_IQ2_S:
  3430. case GGML_TYPE_IQ3_XXS:
  3431. case GGML_TYPE_IQ3_S:
  3432. case GGML_TYPE_IQ4_XS:
  3433. case GGML_TYPE_IQ4_NL:
  3434. break;
  3435. default:
  3436. return nullptr;
  3437. }
  3438. 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];
  3439. }
  3440. 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) {
  3441. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  3442. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  3443. return ctx->device->pipeline_matmul_id_f32;
  3444. }
  3445. if (src0_type == GGML_TYPE_BF16 && src1_type == GGML_TYPE_BF16) {
  3446. return ctx->device->pipeline_matmul_id_bf16;
  3447. }
  3448. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  3449. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3450. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  3451. }
  3452. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3453. return ctx->device->pipeline_matmul_id_f16.f16acc;
  3454. }
  3455. } else {
  3456. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  3457. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  3458. }
  3459. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  3460. return ctx->device->pipeline_matmul_id_f16.f32acc;
  3461. }
  3462. }
  3463. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  3464. switch (src0_type) {
  3465. case GGML_TYPE_Q4_0:
  3466. case GGML_TYPE_Q4_1:
  3467. case GGML_TYPE_Q5_0:
  3468. case GGML_TYPE_Q5_1:
  3469. case GGML_TYPE_Q8_0:
  3470. case GGML_TYPE_Q2_K:
  3471. case GGML_TYPE_Q3_K:
  3472. case GGML_TYPE_Q4_K:
  3473. case GGML_TYPE_Q5_K:
  3474. case GGML_TYPE_Q6_K:
  3475. case GGML_TYPE_IQ1_S:
  3476. case GGML_TYPE_IQ1_M:
  3477. case GGML_TYPE_IQ2_XXS:
  3478. case GGML_TYPE_IQ2_XS:
  3479. case GGML_TYPE_IQ2_S:
  3480. case GGML_TYPE_IQ3_XXS:
  3481. case GGML_TYPE_IQ3_S:
  3482. case GGML_TYPE_IQ4_XS:
  3483. case GGML_TYPE_IQ4_NL:
  3484. break;
  3485. default:
  3486. return nullptr;
  3487. }
  3488. 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;
  3489. }
  3490. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3491. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3492. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3493. switch (a_type) {
  3494. case GGML_TYPE_F32:
  3495. case GGML_TYPE_F16:
  3496. case GGML_TYPE_BF16:
  3497. case GGML_TYPE_Q4_0:
  3498. case GGML_TYPE_Q4_1:
  3499. case GGML_TYPE_Q5_0:
  3500. case GGML_TYPE_Q5_1:
  3501. case GGML_TYPE_Q8_0:
  3502. case GGML_TYPE_Q2_K:
  3503. case GGML_TYPE_Q3_K:
  3504. case GGML_TYPE_Q4_K:
  3505. case GGML_TYPE_Q5_K:
  3506. case GGML_TYPE_Q6_K:
  3507. case GGML_TYPE_IQ1_S:
  3508. case GGML_TYPE_IQ1_M:
  3509. case GGML_TYPE_IQ2_XXS:
  3510. case GGML_TYPE_IQ2_XS:
  3511. case GGML_TYPE_IQ2_S:
  3512. case GGML_TYPE_IQ3_XXS:
  3513. case GGML_TYPE_IQ3_S:
  3514. case GGML_TYPE_IQ4_XS:
  3515. case GGML_TYPE_IQ4_NL:
  3516. break;
  3517. default:
  3518. return nullptr;
  3519. }
  3520. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3521. }
  3522. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3523. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3524. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3525. int best_i = -1;
  3526. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3527. int worst_i = -1;
  3528. size_t worst_size = 0; //largest unused buffer seen so far
  3529. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3530. vk_buffer &b = ctx->buffer_pool[i];
  3531. if (b != nullptr && b->size >= size && b->size < best_size) {
  3532. best_i = i;
  3533. best_size = b->size;
  3534. }
  3535. if (b != nullptr && b->size > worst_size) {
  3536. worst_i = i;
  3537. worst_size = b->size;
  3538. }
  3539. }
  3540. if(best_i != -1) {
  3541. //found the smallest buffer that fits our needs
  3542. vk_buffer b = ctx->buffer_pool[best_i];
  3543. ctx->buffer_pool[best_i].reset();
  3544. return b;
  3545. }
  3546. if(worst_i != -1) {
  3547. //no buffer that fits our needs, resize largest one to save memory
  3548. vk_buffer& b = ctx->buffer_pool[worst_i];
  3549. ggml_vk_destroy_buffer(b);
  3550. }
  3551. return ggml_vk_create_buffer_device(ctx->device, size);
  3552. }
  3553. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3554. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3555. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3556. vk_buffer& b = ctx->buffer_pool[i];
  3557. if (b == nullptr) {
  3558. b = buffer;
  3559. return;
  3560. }
  3561. }
  3562. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3563. ggml_vk_destroy_buffer(buffer);
  3564. }
  3565. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3566. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3567. // Try to find existing temp buffer with enough capacity
  3568. for (auto& buffer : ctx->gc.temp_buffers) {
  3569. if (buffer->size >= size) {
  3570. return buffer;
  3571. }
  3572. }
  3573. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3574. // Otherwise create new buffer
  3575. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3576. ctx->gc.temp_buffers.push_back(buf);
  3577. return buf;
  3578. }
  3579. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3580. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3581. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3582. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3583. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3584. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3585. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3586. size/1024.0/1024.0);
  3587. device->device.freeMemory(buf->device_memory);
  3588. device->device.destroyBuffer(buf->buffer);
  3589. return nullptr;
  3590. }
  3591. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3592. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3593. return buf->ptr;
  3594. }
  3595. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3596. if (ptr == nullptr) {
  3597. return;
  3598. }
  3599. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3600. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3601. vk_buffer buf;
  3602. size_t index;
  3603. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3604. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3605. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3606. if (ptr >= addr && ptr < endr) {
  3607. buf = std::get<2>(device->pinned_memory[i]);
  3608. index = i;
  3609. break;
  3610. }
  3611. }
  3612. if (buf == nullptr) {
  3613. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3614. return;
  3615. }
  3616. ggml_vk_destroy_buffer(buf);
  3617. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3618. }
  3619. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3620. std::lock_guard<std::recursive_mutex> guard(device->mutex);
  3621. buf = nullptr;
  3622. buf_offset = 0;
  3623. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3624. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3625. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3626. if (ptr >= addr && ptr < endr) {
  3627. buf = std::get<2>(device->pinned_memory[i]);
  3628. buf_offset = ((const uint8_t *)ptr) - addr;
  3629. break;
  3630. }
  3631. }
  3632. }
  3633. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_command_pool& p, bool one_time = true) {
  3634. vk_submission s;
  3635. s.buffer = ggml_vk_create_cmd_buffer(device, p);
  3636. if (one_time) {
  3637. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3638. } else {
  3639. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3640. }
  3641. return s;
  3642. }
  3643. template <typename T> size_t push_constant_size(const T &t) {
  3644. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3645. GGML_UNUSED(t);
  3646. return sizeof(T);
  3647. }
  3648. template <typename T> size_t push_constant_size(const std::vector<T> &t) {
  3649. GGML_UNUSED(t);
  3650. return sizeof(T) * t.size();
  3651. }
  3652. template <typename T, uint32_t N> size_t push_constant_size(const std::array<T, N> &t) {
  3653. GGML_UNUSED(t);
  3654. return sizeof(T) * N;
  3655. }
  3656. template <typename T> const T *push_constant_data(const T &t) {
  3657. static_assert(std::is_class<T>::value, "T must be a struct/class");
  3658. return &t;
  3659. }
  3660. template <typename T> const T *push_constant_data(const std::vector<T> &t) {
  3661. return t.data();
  3662. }
  3663. template <typename T, uint32_t N> const T *push_constant_data(const std::array<T, N> &t) {
  3664. return t.data();
  3665. }
  3666. template <typename T>
  3667. 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) {
  3668. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3669. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3670. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3671. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3672. for (auto& buffer : descriptor_buffer_infos) {
  3673. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3674. }
  3675. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3676. GGML_ASSERT(ctx->descriptor_set_idx < ctx->descriptor_sets.size());
  3677. GGML_ASSERT(descriptor_buffer_infos.size() <= MAX_PARAMETER_COUNT);
  3678. vk::DescriptorSet& descriptor_set = ctx->descriptor_sets[ctx->descriptor_set_idx++];
  3679. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3680. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3681. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size(push_constants), push_constant_data(push_constants));
  3682. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3683. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3684. pipeline->layout,
  3685. 0,
  3686. { descriptor_set },
  3687. {});
  3688. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3689. }
  3690. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3691. s.buffer.end();
  3692. s.wait_semaphores = std::move(wait_semaphores);
  3693. s.signal_semaphores = std::move(signal_semaphores);
  3694. }
  3695. static void ggml_vk_ctx_end(vk_context& ctx) {
  3696. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3697. if (ctx->s == nullptr) {
  3698. return;
  3699. }
  3700. ctx->s->buffer.end();
  3701. ctx->s = nullptr;
  3702. }
  3703. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3704. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3705. if (subctx->s != nullptr) {
  3706. ggml_vk_ctx_end(subctx);
  3707. }
  3708. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->p) });
  3709. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3710. }
  3711. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3712. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3713. return CEIL_DIV(width, align) * align;
  3714. }
  3715. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3716. if (memcpys == nullptr) {
  3717. memcpy(dst, src, size);
  3718. } else {
  3719. memcpys->emplace_back(dst, src, size);
  3720. }
  3721. }
  3722. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3723. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3724. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3725. ggml_vk_destroy_buffer(device->sync_staging);
  3726. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3727. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3728. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3729. }
  3730. }
  3731. 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) {
  3732. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3733. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3734. // Buffer is already mapped
  3735. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3736. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3737. GGML_ABORT("fatal error");
  3738. }
  3739. // Check if src is pinned memory
  3740. vk_buffer buf = nullptr;
  3741. size_t buf_offset = 0;
  3742. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3743. const uint64_t ne0 = tensor->ne[0];
  3744. const uint64_t ne1 = tensor->ne[1];
  3745. const uint64_t ne2 = tensor->ne[2];
  3746. const uint64_t ne3 = tensor->ne[3];
  3747. const uint64_t nb0 = tensor->nb[0];
  3748. const uint64_t nb1 = tensor->nb[1];
  3749. const uint64_t nb2 = tensor->nb[2];
  3750. const uint64_t nb3 = tensor->nb[3];
  3751. const ggml_type type = tensor->type;
  3752. const uint64_t ts = ggml_type_size(type);
  3753. const uint64_t bs = ggml_blck_size(type);
  3754. const uint64_t dstnb0 = ts;
  3755. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3756. const uint64_t dstnb2 = dstnb1*ne1;
  3757. const uint64_t dstnb3 = dstnb2*ne2;
  3758. const uint64_t ne = ggml_nelements(tensor);
  3759. if (buf != nullptr) {
  3760. // Memory is pinned, use as staging buffer
  3761. std::vector<vk::BufferCopy> slices;
  3762. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3763. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3764. // Find longest contiguous slice
  3765. if (ne1*nb1 == dstnb2) {
  3766. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3767. } else {
  3768. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3769. if (ne0*nb0/bs == dstnb1) {
  3770. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3771. } else {
  3772. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3773. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3774. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3775. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3776. }
  3777. }
  3778. }
  3779. }
  3780. }
  3781. }
  3782. ggml_vk_sync_buffers(subctx);
  3783. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3784. return;
  3785. }
  3786. if (!sync_staging) {
  3787. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3788. }
  3789. // Staging buffer required
  3790. vk_buffer& staging = ctx->device->sync_staging;
  3791. const uint64_t copy_size = ts*ne/bs;
  3792. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3793. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3794. ggml_vk_sync_buffers(subctx);
  3795. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3796. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3797. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3798. // Find longest contiguous slice
  3799. if (ne1*nb1 == dstnb2) {
  3800. 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);
  3801. } else {
  3802. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3803. if (ne0*nb0/bs == dstnb1) {
  3804. 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);
  3805. } else {
  3806. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3807. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3808. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3809. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3810. }
  3811. }
  3812. }
  3813. }
  3814. }
  3815. }
  3816. }
  3817. 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) {
  3818. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3819. // Buffer is already mapped
  3820. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3821. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3822. GGML_ABORT("fatal error");
  3823. }
  3824. // Check if src is pinned memory
  3825. vk_buffer buf = nullptr;
  3826. size_t buf_offset = 0;
  3827. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3828. if (buf != nullptr) {
  3829. // Memory is pinned, use as staging buffer
  3830. std::vector<vk::BufferCopy> slices(1);
  3831. if (width == spitch) {
  3832. // Only do single write if stride is equal
  3833. slices[0].srcOffset = buf_offset;
  3834. slices[0].dstOffset = offset;
  3835. slices[0].size = width * height;
  3836. } else {
  3837. slices.resize(height);
  3838. for (size_t i = 0; i < height; i++) {
  3839. slices[i].srcOffset = buf_offset + i * spitch;
  3840. slices[i].dstOffset = offset + i * width;
  3841. slices[i].size = width;
  3842. }
  3843. }
  3844. ggml_vk_sync_buffers(subctx);
  3845. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3846. return;
  3847. }
  3848. VK_LOG_DEBUG("STAGING");
  3849. if (!sync_staging) {
  3850. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3851. }
  3852. // Staging buffer required
  3853. const size_t copy_size = width*height;
  3854. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3855. vk_buffer& staging_buffer = dst->device->sync_staging;
  3856. VkBufferCopy buf_copy = {
  3857. 0,
  3858. offset,
  3859. copy_size};
  3860. ggml_vk_sync_buffers(subctx);
  3861. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3862. if (width == spitch) {
  3863. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3864. } else {
  3865. for (size_t i = 0; i < height; i++) {
  3866. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3867. }
  3868. }
  3869. }
  3870. 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) {
  3871. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3872. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3873. }
  3874. 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) {
  3875. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3876. // Buffer is already mapped
  3877. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3878. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3879. for (size_t i = 0; i < height; i++) {
  3880. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3881. }
  3882. } else {
  3883. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  3884. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  3885. ggml_vk_ctx_begin(dst->device, subctx);
  3886. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3887. ggml_vk_ctx_end(subctx);
  3888. for (auto& cpy : subctx->in_memcpys) {
  3889. memcpy(cpy.dst, cpy.src, cpy.n);
  3890. }
  3891. ggml_vk_submit(subctx, dst->device->fence);
  3892. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3893. dst->device->device.resetFences({ dst->device->fence });
  3894. ggml_vk_queue_command_pools_cleanup(dst->device);
  3895. }
  3896. }
  3897. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3898. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3899. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3900. }
  3901. 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) {
  3902. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3903. GGML_ASSERT(width > 0);
  3904. GGML_ASSERT(height > 0);
  3905. GGML_ASSERT(src != nullptr);
  3906. // TODO: staging_offset is not used
  3907. // Check if dst is pinned memory
  3908. vk_buffer buf = nullptr;
  3909. size_t buf_offset = 0;
  3910. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3911. std::vector<vk::BufferCopy> slices(1);
  3912. if (width == spitch && width == dpitch) {
  3913. // Only do single write if stride is equal
  3914. slices[0].srcOffset = offset;
  3915. slices[0].dstOffset = buf_offset;
  3916. slices[0].size = width * height;
  3917. } else {
  3918. slices.resize(height);
  3919. for (size_t i = 0; i < height; i++) {
  3920. slices[i].srcOffset = offset + i * spitch;
  3921. slices[i].dstOffset = buf_offset + i * dpitch;
  3922. slices[i].size = width;
  3923. }
  3924. }
  3925. if (buf != nullptr) {
  3926. // Memory is pinned, use as staging buffer
  3927. ggml_vk_sync_buffers(subctx);
  3928. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3929. return;
  3930. }
  3931. VK_LOG_DEBUG("STAGING");
  3932. if (!sync_staging) {
  3933. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3934. }
  3935. // Fall back to staging buffer
  3936. const size_t copy_size = dpitch * height;
  3937. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  3938. vk_buffer& staging_buffer = src->device->sync_staging;
  3939. ggml_vk_sync_buffers(subctx);
  3940. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  3941. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  3942. }
  3943. 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) {
  3944. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  3945. }
  3946. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  3947. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  3948. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  3949. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  3950. // the HW device to host copy path.
  3951. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  3952. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3953. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  3954. } else {
  3955. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  3956. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  3957. ggml_vk_ctx_begin(src->device, subctx);
  3958. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  3959. ggml_vk_ctx_end(subctx);
  3960. ggml_vk_submit(subctx, src->device->fence);
  3961. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  3962. src->device->device.resetFences({ src->device->fence });
  3963. ggml_vk_queue_command_pools_cleanup(src->device);
  3964. for (auto& cpy : subctx->out_memcpys) {
  3965. memcpy(cpy.dst, cpy.src, cpy.n);
  3966. }
  3967. }
  3968. }
  3969. 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) {
  3970. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  3971. // Make sure both buffers are on same device
  3972. GGML_ASSERT(src->device == dst->device);
  3973. VkBufferCopy bc{ src_offset, dst_offset, size };
  3974. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  3975. }
  3976. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  3977. if (src->device == dst->device) {
  3978. std::lock_guard<std::recursive_mutex> guard(src->device->mutex);
  3979. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  3980. // Copy within the device
  3981. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue.cmd_pool);
  3982. ggml_vk_ctx_begin(src->device, subctx);
  3983. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  3984. ggml_vk_ctx_end(subctx);
  3985. ggml_vk_submit(subctx, src->device->fence);
  3986. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  3987. src->device->device.resetFences({ src->device->fence });
  3988. ggml_vk_queue_command_pools_cleanup(src->device);
  3989. } else {
  3990. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  3991. // Copy device to device
  3992. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  3993. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  3994. // Copy to src staging buffer
  3995. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  3996. // memcpy to dst staging buffer
  3997. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  3998. // Copy to dst buffer
  3999. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  4000. }
  4001. }
  4002. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4003. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  4004. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4005. }
  4006. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  4007. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  4008. std::lock_guard<std::recursive_mutex> guard(dst->device->mutex);
  4009. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue.cmd_pool);
  4010. ggml_vk_ctx_begin(dst->device, subctx);
  4011. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  4012. ggml_vk_ctx_end(subctx);
  4013. ggml_vk_submit(subctx, dst->device->fence);
  4014. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  4015. dst->device->device.resetFences({ dst->device->fence });
  4016. ggml_vk_queue_command_pools_cleanup(dst->device);
  4017. }
  4018. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  4019. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  4020. uint32_t split_k = 1;
  4021. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  4022. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  4023. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  4024. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  4025. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  4026. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  4027. // Clamp to 2 or 4
  4028. split_k = std::min(split_k, 4u);
  4029. if (split_k == 3) {
  4030. split_k = 2;
  4031. }
  4032. if (ctx->device->coopmat2) {
  4033. // coopmat2 shader expects splits to be aligned to 256
  4034. while (split_k > 1 && ((k / split_k) % 256) != 0) {
  4035. split_k /= 2;
  4036. }
  4037. }
  4038. }
  4039. }
  4040. return split_k;
  4041. }
  4042. 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) {
  4043. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4044. if (ctx->device->coopmat2) {
  4045. // Use large shader when the N dimension is greater than the medium shader's tile size
  4046. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4047. 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])) {
  4048. return aligned ? mmp->a_l : mmp->l;
  4049. }
  4050. // Use medium shader when the N dimension is greater than the small shader's tile size
  4051. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4052. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  4053. return aligned ? mmp->a_m : mmp->m;
  4054. }
  4055. return aligned ? mmp->a_s : mmp->s;
  4056. }
  4057. 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])) {
  4058. return aligned ? mmp->a_s : mmp->s;
  4059. }
  4060. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  4061. return aligned ? mmp->a_m : mmp->m;
  4062. }
  4063. return aligned ? mmp->a_l : mmp->l;
  4064. GGML_UNUSED(src1_type);
  4065. }
  4066. 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) {
  4067. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  4068. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  4069. }
  4070. static void ggml_vk_matmul(
  4071. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4072. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  4073. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4074. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4075. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  4076. uint32_t padded_n) {
  4077. 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 << ")");
  4078. ggml_vk_sync_buffers(subctx);
  4079. if (split_k == 1) {
  4080. 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 };
  4081. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, pc, { m, n, batch });
  4082. return;
  4083. }
  4084. GGML_ASSERT(batch_stride_d == m * n);
  4085. 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 };
  4086. // Make sure enough workgroups get assigned for split k to work
  4087. 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 });
  4088. ggml_vk_sync_buffers(subctx);
  4089. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  4090. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2, { m * n * batch, 1, 1 });
  4091. }
  4092. 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) {
  4093. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  4094. if (ctx->device->coopmat2) {
  4095. // Use large shader when the N dimension is greater than the medium shader's tile size
  4096. uint32_t crossover_large = mmp->m->wg_denoms[1];
  4097. 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])) {
  4098. return aligned ? mmp->a_l : mmp->l;
  4099. }
  4100. // Use medium shader when the N dimension is greater than the small shader's tile size
  4101. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  4102. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  4103. return aligned ? mmp->a_m : mmp->m;
  4104. }
  4105. return aligned ? mmp->a_s : mmp->s;
  4106. }
  4107. 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])) {
  4108. return aligned ? mmp->a_s : mmp->s;
  4109. }
  4110. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  4111. return aligned ? mmp->a_m : mmp->m;
  4112. }
  4113. return aligned ? mmp->a_l : mmp->l;
  4114. }
  4115. 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) {
  4116. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  4117. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  4118. }
  4119. static void ggml_vk_matmul_id(
  4120. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  4121. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  4122. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  4123. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  4124. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  4125. uint32_t padded_n) {
  4126. 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 << "), " <<
  4127. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  4128. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  4129. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  4130. ggml_vk_sync_buffers(subctx);
  4131. 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,
  4132. nei0, nei1, nbi1, ne11, padded_n };
  4133. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, pc, { m, nei1, n_as });
  4134. }
  4135. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  4136. return
  4137. tensor->nb[0] == ggml_type_size(tensor->type) &&
  4138. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  4139. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  4140. }
  4141. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  4142. // Choose "contiguous copy" shader if src/dst are contiguous
  4143. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  4144. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  4145. if (contig) {
  4146. return ctx->device->pipeline_contig_cpy_f32_f32;
  4147. } else {
  4148. return ctx->device->pipeline_cpy_f32_f32;
  4149. }
  4150. }
  4151. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  4152. if (contig) {
  4153. return ctx->device->pipeline_contig_cpy_f32_f16;
  4154. } else {
  4155. return ctx->device->pipeline_cpy_f32_f16;
  4156. }
  4157. }
  4158. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  4159. if (contig) {
  4160. return ctx->device->pipeline_contig_cpy_f16_f16;
  4161. } else {
  4162. return ctx->device->pipeline_cpy_f16_f16;
  4163. }
  4164. }
  4165. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F32) {
  4166. if (contig) {
  4167. return ctx->device->pipeline_contig_cpy_f16_f32;
  4168. } else {
  4169. return ctx->device->pipeline_cpy_f16_f32;
  4170. }
  4171. }
  4172. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_BF16) {
  4173. if (contig) {
  4174. return ctx->device->pipeline_contig_cpy_f32_bf16;
  4175. } else {
  4176. return ctx->device->pipeline_cpy_f32_bf16;
  4177. }
  4178. }
  4179. if (src->type == GGML_TYPE_F32) {
  4180. switch (to) {
  4181. case GGML_TYPE_Q4_0:
  4182. case GGML_TYPE_Q4_1:
  4183. case GGML_TYPE_Q5_0:
  4184. case GGML_TYPE_Q5_1:
  4185. case GGML_TYPE_Q8_0:
  4186. case GGML_TYPE_IQ4_NL:
  4187. return ctx->device->pipeline_cpy_f32_quant[to];
  4188. default:
  4189. break;
  4190. }
  4191. }
  4192. if (to == GGML_TYPE_F32) {
  4193. switch (src->type) {
  4194. case GGML_TYPE_Q4_0:
  4195. case GGML_TYPE_Q4_1:
  4196. case GGML_TYPE_Q5_0:
  4197. case GGML_TYPE_Q5_1:
  4198. case GGML_TYPE_Q8_0:
  4199. case GGML_TYPE_IQ4_NL:
  4200. return ctx->device->pipeline_cpy_quant_f32[src->type];
  4201. default:
  4202. break;
  4203. }
  4204. }
  4205. if (src->type == to) {
  4206. // Copy two or four bytes at a time, depending on block size.
  4207. // For quantized types, we scale by block size/type size. But
  4208. // this path is also used for bf16->bf16 for example, where the
  4209. // type size must be exactly 2 or 4.
  4210. GGML_ASSERT(ggml_is_quantized(to) || ggml_type_size(src->type) == 2 || ggml_type_size(src->type) == 4);
  4211. if ((ggml_type_size(src->type) % 4) == 0) {
  4212. if (contig) {
  4213. return ctx->device->pipeline_contig_cpy_f32_f32;
  4214. } else {
  4215. return ctx->device->pipeline_cpy_f32_f32;
  4216. }
  4217. } else {
  4218. if (contig) {
  4219. return ctx->device->pipeline_contig_cpy_f16_f16;
  4220. } else {
  4221. return ctx->device->pipeline_cpy_f16_f16;
  4222. }
  4223. }
  4224. }
  4225. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  4226. GGML_ABORT("fatal error");
  4227. }
  4228. 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) {
  4229. 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] << "), ";
  4230. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  4231. const int tensor_type_size = ggml_type_size(tensor->type);
  4232. const uint32_t ne = ggml_nelements(tensor);
  4233. std::array<uint32_t, 3> elements;
  4234. if (ne > 262144) {
  4235. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  4236. } else if (ne > 512) {
  4237. elements = { 512, CEIL_DIV(ne, 512), 1 };
  4238. } else {
  4239. elements = { ne, 1, 1 };
  4240. }
  4241. vk_op_unary_push_constants pc = {
  4242. (uint32_t)ne,
  4243. (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,
  4244. (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]),
  4245. 0,
  4246. 0.0f, 0.0f,
  4247. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4248. };
  4249. init_pushconst_fastdiv(pc);
  4250. ggml_vk_sync_buffers(subctx);
  4251. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, pc, elements);
  4252. }
  4253. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  4254. switch(type) {
  4255. case GGML_TYPE_Q8_1:
  4256. return ctx->device->pipeline_quantize_q8_1;
  4257. default:
  4258. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  4259. GGML_ABORT("fatal error");
  4260. }
  4261. }
  4262. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  4263. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  4264. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4265. ggml_vk_sync_buffers(subctx);
  4266. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, std::array<uint32_t, 1>{ne}, { ne, 1, 1 });
  4267. }
  4268. 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) {
  4269. 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];
  4270. 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];
  4271. 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];
  4272. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4273. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4274. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4275. const uint64_t ne00 = src0->ne[0];
  4276. const uint64_t ne01 = src0->ne[1];
  4277. const uint64_t ne02 = src0->ne[2];
  4278. const uint64_t ne03 = src0->ne[3];
  4279. const uint64_t ne10 = src1->ne[0];
  4280. const uint64_t ne11 = src1->ne[1];
  4281. const uint64_t ne12 = src1->ne[2];
  4282. const uint64_t ne13 = src1->ne[3];
  4283. const uint64_t ne20 = dst->ne[0];
  4284. const uint64_t ne21 = dst->ne[1];
  4285. const uint64_t r2 = ne12 / ne02;
  4286. const uint64_t r3 = ne13 / ne03;
  4287. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4288. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4289. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4290. vk_buffer d_Qx = nullptr;
  4291. size_t qx_buf_offset = 0;
  4292. vk_buffer d_Qy = nullptr;
  4293. size_t qy_buf_offset = 0;
  4294. bool src0_uma = false;
  4295. bool src1_uma = false;
  4296. if (ctx->device->uma) {
  4297. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4298. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4299. src0_uma = d_Qx != nullptr;
  4300. src1_uma = d_Qy != nullptr;
  4301. }
  4302. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4303. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4304. !ggml_vk_dim01_contiguous(src0);
  4305. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4306. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4307. !ggml_vk_dim01_contiguous(src1);
  4308. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4309. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4310. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4311. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  4312. // Check for mmq first
  4313. 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;
  4314. if (mmp == nullptr) {
  4315. // Fall back to f16 dequant mul mat
  4316. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? f16_type : src1->type, (ggml_prec)dst->op_params[0]);
  4317. quantize_y = false;
  4318. }
  4319. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4320. const bool qy_needs_dequant = !quantize_y && ((src1->type != f16_type && !y_f32_kernel) || y_non_contig);
  4321. if (qx_needs_dequant) {
  4322. // Fall back to dequant + f16 mulmat
  4323. 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]);
  4324. }
  4325. // Not implemented
  4326. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4327. 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)));
  4328. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  4329. 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));
  4330. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4331. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  4332. const int x_ne = ne01 * ne00;
  4333. const int y_ne = padded_n * ne10;
  4334. const int d_ne = ne11 * ne01;
  4335. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  4336. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4337. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4338. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4339. 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);
  4340. const uint64_t d_sz = sizeof(float) * d_ne;
  4341. vk_pipeline to_fp16_vk_0 = nullptr;
  4342. vk_pipeline to_fp16_vk_1 = nullptr;
  4343. vk_pipeline to_q8_1 = nullptr;
  4344. if (x_non_contig) {
  4345. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4346. } else {
  4347. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4348. }
  4349. if (y_non_contig) {
  4350. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4351. } else {
  4352. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4353. }
  4354. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4355. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4356. if (quantize_y) {
  4357. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  4358. }
  4359. if (dryrun) {
  4360. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4361. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4362. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  4363. if (
  4364. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4365. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  4366. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  4367. GGML_ABORT("Requested preallocation size is too large");
  4368. }
  4369. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4370. ctx->prealloc_size_x = x_sz_upd;
  4371. }
  4372. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  4373. ctx->prealloc_size_y = y_sz_upd;
  4374. }
  4375. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  4376. ctx->prealloc_size_split_k = split_k_size;
  4377. }
  4378. // Request descriptor sets
  4379. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4380. if (qx_needs_dequant) {
  4381. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4382. }
  4383. if (qy_needs_dequant) {
  4384. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4385. }
  4386. if (quantize_y) {
  4387. ggml_pipeline_request_descriptor_sets(ctx, to_q8_1, 1);
  4388. }
  4389. if (split_k > 1) {
  4390. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  4391. }
  4392. return;
  4393. }
  4394. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4395. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4396. GGML_ASSERT(d_D != nullptr);
  4397. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  4398. vk_buffer d_X;
  4399. uint64_t x_buf_offset = 0;
  4400. vk_buffer d_Y;
  4401. uint64_t y_buf_offset = 0;
  4402. if (!src0_uma) {
  4403. d_Qx = src0_buf_ctx->dev_buffer;
  4404. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4405. GGML_ASSERT(d_Qx != nullptr);
  4406. }
  4407. if (!src1_uma) {
  4408. d_Qy = src1_buf_ctx->dev_buffer;
  4409. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4410. GGML_ASSERT(d_Qy != nullptr);
  4411. }
  4412. if (qx_needs_dequant) {
  4413. d_X = ctx->prealloc_x;
  4414. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4415. } else {
  4416. d_X = d_Qx;
  4417. x_buf_offset = qx_buf_offset;
  4418. GGML_ASSERT(qx_sz == x_sz);
  4419. }
  4420. if (qy_needs_dequant) {
  4421. d_Y = ctx->prealloc_y;
  4422. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4423. } else if (quantize_y) {
  4424. d_Y = ctx->prealloc_y;
  4425. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  4426. } else {
  4427. d_Y = d_Qy;
  4428. y_buf_offset = qy_buf_offset;
  4429. GGML_ASSERT(qy_sz == y_sz);
  4430. }
  4431. if (x_non_contig) {
  4432. 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 });
  4433. } else if (qx_needs_dequant) {
  4434. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4435. ggml_vk_sync_buffers(subctx);
  4436. 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});
  4437. }
  4438. if (y_non_contig) {
  4439. 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 });
  4440. }
  4441. if (quantize_y) {
  4442. 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);
  4443. }
  4444. uint32_t stride_batch_x = ne00*ne01;
  4445. uint32_t stride_batch_y = ne10*ne11;
  4446. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4447. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4448. }
  4449. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  4450. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4451. }
  4452. // compute
  4453. ggml_vk_matmul(
  4454. ctx, subctx, pipeline,
  4455. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4456. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  4457. ne01, ne11, ne10,
  4458. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  4459. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  4460. ); // NOLINT
  4461. }
  4462. 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) {
  4463. 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];
  4464. 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];
  4465. 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];
  4466. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  4467. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4468. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4469. const uint64_t ne00 = src0->ne[0];
  4470. const uint64_t ne01 = src0->ne[1];
  4471. const uint64_t ne02 = src0->ne[2];
  4472. const uint64_t ne03 = src0->ne[3];
  4473. const uint64_t ne10 = src1->ne[0];
  4474. const uint64_t ne11 = src1->ne[1];
  4475. const uint64_t ne12 = src1->ne[2];
  4476. const uint64_t ne13 = src1->ne[3];
  4477. const uint64_t ne20 = dst->ne[0];
  4478. const uint64_t ne21 = dst->ne[1];
  4479. const uint64_t ne22 = dst->ne[2];
  4480. const uint64_t ne23 = dst->ne[3];
  4481. const uint64_t r2 = ne12 / ne02;
  4482. const uint64_t r3 = ne13 / ne03;
  4483. // batch_n indicates that we need to compute a few vector results, and this assumes
  4484. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  4485. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  4486. bool batch_n = ne11 > 1;
  4487. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4488. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4489. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4490. vk_buffer d_Qx = nullptr;
  4491. size_t qx_buf_offset = 0;
  4492. vk_buffer d_Qy = nullptr;
  4493. size_t qy_buf_offset = 0;
  4494. bool src0_uma = false;
  4495. bool src1_uma = false;
  4496. if (ctx->device->uma) {
  4497. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4498. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4499. src0_uma = d_Qx != nullptr;
  4500. src1_uma = d_Qy != nullptr;
  4501. }
  4502. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4503. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4504. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4505. const bool qx_needs_dequant = x_non_contig;
  4506. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4507. // Not implemented
  4508. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4509. const uint64_t x_ne = ne01 * ne00;
  4510. const uint64_t y_ne = ne11 * ne10;
  4511. const uint64_t d_ne = ne11 * ne01;
  4512. 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);
  4513. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4514. 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;
  4515. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4516. const uint64_t d_sz = sizeof(float) * d_ne;
  4517. vk_pipeline to_fp16_vk_0 = nullptr;
  4518. vk_pipeline to_fp16_vk_1 = nullptr;
  4519. if (x_non_contig) {
  4520. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4521. }
  4522. if (y_non_contig) {
  4523. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4524. } else {
  4525. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4526. }
  4527. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  4528. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4529. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4530. GGML_ASSERT(dmmv != nullptr);
  4531. if (dryrun) {
  4532. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4533. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4534. if (
  4535. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4536. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4537. GGML_ABORT("Requested preallocation size is too large");
  4538. }
  4539. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4540. ctx->prealloc_size_x = x_sz_upd;
  4541. }
  4542. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4543. ctx->prealloc_size_y = y_sz_upd;
  4544. }
  4545. // Request descriptor sets
  4546. if (qx_needs_dequant) {
  4547. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4548. }
  4549. if (qy_needs_dequant) {
  4550. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4551. }
  4552. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  4553. return;
  4554. }
  4555. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4556. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4557. GGML_ASSERT(d_D != nullptr);
  4558. vk_buffer d_X;
  4559. uint64_t x_buf_offset = 0;
  4560. vk_buffer d_Y;
  4561. uint64_t y_buf_offset = 0;
  4562. if(!src0_uma) {
  4563. d_Qx = src0_buf_ctx->dev_buffer;
  4564. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4565. GGML_ASSERT(d_Qx != nullptr);
  4566. }
  4567. if(!src1_uma) {
  4568. d_Qy = src1_buf_ctx->dev_buffer;
  4569. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4570. GGML_ASSERT(d_Qy != nullptr);
  4571. }
  4572. if (qx_needs_dequant) {
  4573. d_X = ctx->prealloc_x;
  4574. } else {
  4575. d_X = d_Qx;
  4576. x_buf_offset = qx_buf_offset;
  4577. GGML_ASSERT(qx_sz == x_sz);
  4578. }
  4579. if (qy_needs_dequant) {
  4580. d_Y = ctx->prealloc_y;
  4581. } else {
  4582. d_Y = d_Qy;
  4583. y_buf_offset = qy_buf_offset;
  4584. GGML_ASSERT(qy_sz == y_sz);
  4585. }
  4586. if (x_non_contig) {
  4587. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4588. 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 });
  4589. }
  4590. if (y_non_contig) {
  4591. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4592. 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 });
  4593. }
  4594. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4595. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4596. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4597. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4598. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4599. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4600. }
  4601. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4602. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4603. }
  4604. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4605. uint32_t groups_x = ne01;
  4606. uint32_t groups_z = 1;
  4607. if (ne01 > max_groups_x) {
  4608. groups_z = 64;
  4609. groups_x = CEIL_DIV(groups_x, groups_z);
  4610. }
  4611. // compute
  4612. const vk_mat_vec_push_constants pc = {
  4613. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4614. stride_batch_x, stride_batch_y, stride_batch_d,
  4615. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4616. };
  4617. ggml_vk_sync_buffers(subctx);
  4618. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4619. { 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} },
  4620. pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4621. }
  4622. 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) {
  4623. 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];
  4624. 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];
  4625. 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];
  4626. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4627. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4628. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4629. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4630. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4631. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4632. const uint64_t ne00 = src0->ne[0];
  4633. const uint64_t ne01 = src0->ne[1];
  4634. const uint64_t ne02 = src0->ne[2];
  4635. // const uint64_t ne03 = src0->ne[3];
  4636. const uint64_t ne10 = src1->ne[0];
  4637. const uint64_t ne11 = src1->ne[1];
  4638. const uint64_t ne12 = src1->ne[2];
  4639. // const uint64_t ne13 = src1->ne[3];
  4640. GGML_ASSERT(ne11 == 1);
  4641. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4642. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4643. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4644. vk_buffer d_Qy = nullptr;
  4645. size_t qy_buf_offset = 0;
  4646. bool src1_uma = false;
  4647. if (ctx->device->uma) {
  4648. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4649. src1_uma = d_Qy != nullptr;
  4650. }
  4651. const uint64_t x_ne = ne00 * ne01 * ne02;
  4652. const uint64_t y_ne = ne10 * ne11 * ne12;
  4653. const uint64_t d_ne = ne01 * ne11 * ne12;
  4654. 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);
  4655. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4656. const uint64_t d_sz = sizeof(float) * d_ne;
  4657. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  4658. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  4659. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  4660. gqa_ratio = 1;
  4661. }
  4662. if (dryrun) {
  4663. // Request descriptor sets
  4664. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  4665. return;
  4666. }
  4667. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4668. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4669. GGML_ASSERT(d_D != nullptr);
  4670. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4671. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4672. GGML_ASSERT(d_Qx != nullptr);
  4673. if (!src1_uma) {
  4674. d_Qy = src1_buf_ctx->dev_buffer;
  4675. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4676. GGML_ASSERT(d_Qx != nullptr);
  4677. }
  4678. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4679. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4680. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4681. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4682. // compute
  4683. 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)) };
  4684. uint32_t workgroups_z = (uint32_t)ne12;
  4685. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  4686. if (gqa_ratio > 1) {
  4687. workgroups_z /= gqa_ratio;
  4688. }
  4689. ggml_vk_sync_buffers(subctx);
  4690. 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 });
  4691. }
  4692. 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) {
  4693. 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];
  4694. 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];
  4695. 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];
  4696. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4697. GGML_ASSERT(!ggml_is_transposed(src0));
  4698. GGML_ASSERT(!ggml_is_transposed(src1));
  4699. GGML_ASSERT(!ggml_is_permuted(src0));
  4700. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4701. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4702. const uint64_t ne00 = src0->ne[0];
  4703. const uint64_t ne01 = src0->ne[1];
  4704. const uint64_t ne02 = src0->ne[2];
  4705. // const uint64_t ne03 = src0->ne[3];
  4706. const uint64_t nb01 = src0->nb[1];
  4707. const uint64_t nb02 = src0->nb[2];
  4708. const uint64_t nb12 = src1->nb[2];
  4709. // const uint64_t ne10 = src1->ne[0];
  4710. const uint64_t ne11 = src1->ne[1];
  4711. const uint64_t ne12 = src1->ne[2];
  4712. // const uint64_t ne13 = src1->ne[3];
  4713. GGML_ASSERT(ne11 == 1);
  4714. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4715. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4716. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4717. vk_buffer d_Qy = nullptr;
  4718. size_t qy_buf_offset = 0;
  4719. bool src1_uma = false;
  4720. if (ctx->device->uma) {
  4721. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4722. src1_uma = d_Qy != nullptr;
  4723. }
  4724. const uint64_t d_ne = ne01 * ne11 * ne12;
  4725. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4726. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4727. const uint32_t channel_stride_y = nb12 / sizeof(float);
  4728. const uint64_t qx_sz = ggml_nbytes(src0);
  4729. const uint64_t qy_sz = ggml_nbytes(src1);
  4730. const uint64_t d_sz = sizeof(float) * d_ne;
  4731. if (dryrun) {
  4732. // Request descriptor sets
  4733. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4734. return;
  4735. }
  4736. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4737. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4738. GGML_ASSERT(d_D != nullptr);
  4739. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4740. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4741. GGML_ASSERT(d_Qx != nullptr);
  4742. if (!src1_uma) {
  4743. d_Qy = src1_buf_ctx->dev_buffer;
  4744. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4745. GGML_ASSERT(d_Qx != nullptr);
  4746. }
  4747. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4748. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4749. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4750. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4751. // compute
  4752. 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)) };
  4753. ggml_vk_sync_buffers(subctx);
  4754. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4755. { 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 });
  4756. }
  4757. 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) {
  4758. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4759. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4760. // detect 0213 permutation, and batch size of 1
  4761. src0->nb[0] <= src0->nb[2] &&
  4762. src0->nb[2] <= src0->nb[1] &&
  4763. src0->nb[1] <= src0->nb[3] &&
  4764. src1->nb[0] <= src1->nb[2] &&
  4765. src1->nb[2] <= src1->nb[1] &&
  4766. src1->nb[1] <= src1->nb[3] &&
  4767. src0->ne[3] == 1 &&
  4768. src1->ne[3] == 1) {
  4769. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4770. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4771. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4772. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4773. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4774. // when ne12 and ne13 are one.
  4775. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4776. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
  4777. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4778. } else {
  4779. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4780. }
  4781. }
  4782. 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) {
  4783. 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];
  4784. 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];
  4785. 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];
  4786. 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] << "),)");
  4787. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4788. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4789. const uint64_t ne00 = src0->ne[0];
  4790. const uint64_t ne01 = src0->ne[1];
  4791. const uint64_t ne02 = src0->ne[2];
  4792. const uint64_t ne03 = src0->ne[3];
  4793. const uint64_t ne10 = src1->ne[0];
  4794. const uint64_t ne11 = src1->ne[1];
  4795. const uint64_t ne12 = src1->ne[2];
  4796. const uint64_t ne13 = src1->ne[3];
  4797. const uint64_t nei0 = ids->ne[0];
  4798. const uint64_t nei1 = ids->ne[1];
  4799. GGML_ASSERT(nei0 * nei1 <= 4096);
  4800. const uint32_t nbi1 = ids->nb[1];
  4801. const uint32_t nbi2 = ids->nb[2];
  4802. const uint64_t ne20 = dst->ne[0];
  4803. const uint64_t ne21 = dst->ne[1];
  4804. const uint64_t ne22 = dst->ne[2];
  4805. const uint64_t ne23 = dst->ne[3];
  4806. const uint64_t n_as = ne02;
  4807. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4808. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4809. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4810. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4811. vk_buffer d_Qx = nullptr;
  4812. size_t qx_buf_offset = 0;
  4813. vk_buffer d_Qy = nullptr;
  4814. size_t qy_buf_offset = 0;
  4815. vk_buffer d_ids = nullptr;
  4816. size_t ids_buf_offset = 0;
  4817. bool src0_uma = false;
  4818. bool src1_uma = false;
  4819. bool ids_uma = false;
  4820. if (ctx->device->uma) {
  4821. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4822. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4823. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4824. src0_uma = d_Qx != nullptr;
  4825. src1_uma = d_Qy != nullptr;
  4826. ids_uma = d_ids != nullptr;
  4827. }
  4828. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4829. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4830. !ggml_vk_dim01_contiguous(src0);
  4831. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4832. (src0->type == GGML_TYPE_BF16 && src1->type != GGML_TYPE_BF16) ||
  4833. !ggml_vk_dim01_contiguous(src1);
  4834. // If src0 is BF16, try to use a BF16 x BF16 multiply
  4835. ggml_type f16_type = src0->type == GGML_TYPE_BF16 ? GGML_TYPE_BF16 : GGML_TYPE_F16;
  4836. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4837. 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]);
  4838. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4839. const bool qy_needs_dequant = (src1->type != f16_type && !y_f32_kernel) || y_non_contig;
  4840. if (qx_needs_dequant) {
  4841. // Fall back to dequant + f16 mulmat
  4842. 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]);
  4843. }
  4844. // Not implemented
  4845. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4846. 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));
  4847. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4848. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? f16_type : src0->type);
  4849. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4850. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4851. const uint64_t x_ne = ne01 * ne00;
  4852. const uint64_t y_ne = padded_n * ne10;
  4853. const uint64_t d_ne = ne21 * ne20;
  4854. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4855. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4856. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4857. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4858. const uint64_t ids_sz = nbi2;
  4859. const uint64_t d_sz = sizeof(float) * d_ne;
  4860. vk_pipeline to_fp16_vk_0 = nullptr;
  4861. vk_pipeline to_fp16_vk_1 = nullptr;
  4862. if (x_non_contig) {
  4863. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, f16_type);
  4864. } else {
  4865. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4866. }
  4867. if (y_non_contig) {
  4868. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, f16_type);
  4869. } else {
  4870. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4871. }
  4872. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4873. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4874. if (dryrun) {
  4875. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4876. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4877. if (
  4878. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4879. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4880. GGML_ABORT("Requested preallocation size is too large");
  4881. }
  4882. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4883. ctx->prealloc_size_x = x_sz_upd;
  4884. }
  4885. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4886. ctx->prealloc_size_y = y_sz_upd;
  4887. }
  4888. // Request descriptor sets
  4889. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  4890. if (qx_needs_dequant) {
  4891. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  4892. }
  4893. if (qy_needs_dequant) {
  4894. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  4895. }
  4896. return;
  4897. }
  4898. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4899. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4900. GGML_ASSERT(d_D != nullptr);
  4901. vk_buffer d_X;
  4902. uint64_t x_buf_offset = 0;
  4903. vk_buffer d_Y;
  4904. uint64_t y_buf_offset = 0;
  4905. if (!src0_uma) {
  4906. d_Qx = src0_buf_ctx->dev_buffer;
  4907. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4908. GGML_ASSERT(d_Qx != nullptr);
  4909. }
  4910. if (!src1_uma) {
  4911. d_Qy = src1_buf_ctx->dev_buffer;
  4912. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4913. GGML_ASSERT(d_Qy != nullptr);
  4914. }
  4915. if (!ids_uma) {
  4916. d_ids = ids_buf_ctx->dev_buffer;
  4917. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4918. GGML_ASSERT(d_ids != nullptr);
  4919. }
  4920. if (qx_needs_dequant) {
  4921. d_X = ctx->prealloc_x;
  4922. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4923. } else {
  4924. d_X = d_Qx;
  4925. x_buf_offset = qx_buf_offset;
  4926. GGML_ASSERT(qx_sz == x_sz);
  4927. }
  4928. if (qy_needs_dequant) {
  4929. d_Y = ctx->prealloc_y;
  4930. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4931. } else {
  4932. d_Y = d_Qy;
  4933. y_buf_offset = qy_buf_offset;
  4934. GGML_ASSERT(qy_sz == y_sz);
  4935. }
  4936. if (x_non_contig) {
  4937. 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 });
  4938. } else if (qx_needs_dequant) {
  4939. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4940. ggml_vk_sync_buffers(subctx);
  4941. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  4942. { 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});
  4943. }
  4944. if (y_non_contig) {
  4945. 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 });
  4946. }
  4947. uint32_t stride_batch_x = ne00*ne01;
  4948. uint32_t stride_batch_y = ne10*ne11;
  4949. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4950. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4951. }
  4952. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4953. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4954. }
  4955. // compute
  4956. ggml_vk_matmul_id(
  4957. ctx, subctx, pipeline,
  4958. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4959. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  4960. ne01, ne21, ne10, ne10, ne10, ne01,
  4961. stride_batch_x, stride_batch_y, ne20*ne21,
  4962. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  4963. ); // NOLINT
  4964. }
  4965. 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) {
  4966. 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];
  4967. 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];
  4968. 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];
  4969. 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];
  4970. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4971. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
  4972. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4973. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4974. const uint64_t ne00 = src0->ne[0];
  4975. const uint64_t ne01 = src0->ne[1];
  4976. const uint64_t ne02 = src0->ne[2];
  4977. const uint64_t ne03 = src0->ne[3];
  4978. const uint64_t ne10 = src1->ne[0];
  4979. const uint64_t ne11 = src1->ne[1];
  4980. const uint64_t ne12 = src1->ne[2];
  4981. const uint64_t ne13 = src1->ne[3];
  4982. const uint64_t nei0 = ids->ne[0];
  4983. const uint64_t nei1 = ids->ne[1];
  4984. const uint64_t nbi2 = ids->nb[2];
  4985. GGML_ASSERT(nei1 == 1);
  4986. const uint64_t ne20 = dst->ne[0];
  4987. const uint64_t ne21 = dst->ne[1];
  4988. const uint64_t ne22 = dst->ne[2];
  4989. const uint64_t ne23 = dst->ne[3];
  4990. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4991. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4992. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4993. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4994. vk_buffer d_Qx = nullptr;
  4995. size_t qx_buf_offset = 0;
  4996. vk_buffer d_Qy = nullptr;
  4997. size_t qy_buf_offset = 0;
  4998. vk_buffer d_ids = nullptr;
  4999. size_t ids_buf_offset = 0;
  5000. bool src0_uma = false;
  5001. bool src1_uma = false;
  5002. bool ids_uma = false;
  5003. if (ctx->device->uma) {
  5004. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  5005. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  5006. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  5007. src0_uma = d_Qx != nullptr;
  5008. src1_uma = d_Qy != nullptr;
  5009. ids_uma = d_ids != nullptr;
  5010. }
  5011. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  5012. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  5013. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  5014. const bool qx_needs_dequant = x_non_contig;
  5015. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  5016. // Not implemented
  5017. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  5018. const uint64_t x_ne = ne01 * ne00;
  5019. const uint64_t y_ne = ne11 * ne10;
  5020. const uint64_t d_ne = ne21 * ne20;
  5021. 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);
  5022. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  5023. 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;
  5024. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  5025. const uint64_t ids_sz = nbi2;
  5026. const uint64_t d_sz = sizeof(float) * d_ne;
  5027. vk_pipeline to_fp16_vk_0 = nullptr;
  5028. vk_pipeline to_fp16_vk_1 = nullptr;
  5029. if (x_non_contig) {
  5030. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  5031. }
  5032. if (y_non_contig) {
  5033. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  5034. } else {
  5035. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  5036. }
  5037. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  5038. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  5039. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  5040. GGML_ASSERT(dmmv != nullptr);
  5041. if (dryrun) {
  5042. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  5043. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  5044. if (
  5045. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  5046. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  5047. GGML_ABORT("Requested preallocation size is too large");
  5048. }
  5049. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  5050. ctx->prealloc_size_x = x_sz_upd;
  5051. }
  5052. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  5053. ctx->prealloc_size_y = y_sz_upd;
  5054. }
  5055. // Request descriptor sets
  5056. if (qx_needs_dequant) {
  5057. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_0, 1);
  5058. }
  5059. if (qy_needs_dequant) {
  5060. ggml_pipeline_request_descriptor_sets(ctx, to_fp16_vk_1, 1);
  5061. }
  5062. ggml_pipeline_request_descriptor_sets(ctx, dmmv, 1);
  5063. return;
  5064. }
  5065. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5066. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5067. GGML_ASSERT(d_D != nullptr);
  5068. vk_buffer d_X;
  5069. uint64_t x_buf_offset = 0;
  5070. vk_buffer d_Y;
  5071. uint64_t y_buf_offset = 0;
  5072. if(!src0_uma) {
  5073. d_Qx = src0_buf_ctx->dev_buffer;
  5074. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5075. GGML_ASSERT(d_Qx != nullptr);
  5076. }
  5077. if(!src1_uma) {
  5078. d_Qy = src1_buf_ctx->dev_buffer;
  5079. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5080. GGML_ASSERT(d_Qy != nullptr);
  5081. }
  5082. if(!ids_uma) {
  5083. d_ids = ids_buf_ctx->dev_buffer;
  5084. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  5085. GGML_ASSERT(d_ids != nullptr);
  5086. }
  5087. if (qx_needs_dequant) {
  5088. d_X = ctx->prealloc_x;
  5089. } else {
  5090. d_X = d_Qx;
  5091. x_buf_offset = qx_buf_offset;
  5092. GGML_ASSERT(qx_sz == x_sz);
  5093. }
  5094. if (qy_needs_dequant) {
  5095. d_Y = ctx->prealloc_y;
  5096. } else {
  5097. d_Y = d_Qy;
  5098. y_buf_offset = qy_buf_offset;
  5099. GGML_ASSERT(qy_sz == y_sz);
  5100. }
  5101. if (x_non_contig) {
  5102. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  5103. 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 });
  5104. }
  5105. if (y_non_contig) {
  5106. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  5107. 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 });
  5108. }
  5109. uint32_t stride_batch_y = ne10*ne11;
  5110. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  5111. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  5112. }
  5113. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  5114. uint32_t groups_x = ne01;
  5115. uint32_t groups_z = 1;
  5116. if (ne01 > max_groups_x) {
  5117. groups_z = 64;
  5118. groups_x = CEIL_DIV(groups_x, groups_z);
  5119. }
  5120. // compute
  5121. const vk_mat_vec_id_push_constants pc = {
  5122. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  5123. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  5124. (uint32_t)nei0, (uint32_t)ne11,
  5125. };
  5126. ggml_vk_sync_buffers(subctx);
  5127. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  5128. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  5129. 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 } },
  5130. pc, { groups_x, (uint32_t)nei0, groups_z });
  5131. }
  5132. 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) {
  5133. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  5134. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  5135. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  5136. } else {
  5137. // Split based on number of ids, to fit in shared memory
  5138. const uint32_t nei0 = (uint32_t)src2->ne[0];
  5139. const uint32_t nei1 = (uint32_t)src2->ne[1];
  5140. GGML_ASSERT(nei0 <= 4096);
  5141. const uint32_t split_size = std::min(nei1, 4096u / nei0);
  5142. ggml_tensor src1_copy = *src1;
  5143. ggml_tensor src2_copy = *src2;
  5144. ggml_tensor dst_copy = *dst;
  5145. for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
  5146. const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
  5147. src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
  5148. src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
  5149. dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
  5150. src1_copy.ne[2] = n_tokens;
  5151. src2_copy.ne[1] = n_tokens;
  5152. dst_copy.ne[2] = n_tokens;
  5153. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
  5154. }
  5155. }
  5156. }
  5157. static bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv) {
  5158. // Needs to be kept up to date on shader changes
  5159. GGML_UNUSED(hsv);
  5160. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5161. const uint32_t Br = scalar_flash_attention_num_large_rows;
  5162. const uint32_t Bc = scalar_flash_attention_Bc;
  5163. const uint32_t tmpsh = wg_size * sizeof(float);
  5164. const uint32_t tmpshv4 = wg_size * 4 * sizeof(float);
  5165. const uint32_t masksh = Bc * Br * sizeof(float);
  5166. const uint32_t Qf = Br * (hsk / 4 + 2) * 4 * sizeof(float);
  5167. const uint32_t total_size = tmpsh + tmpshv4 + masksh + Qf;
  5168. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5169. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", total_size=" << total_size << ", supported=" << supported);
  5170. return supported;
  5171. }
  5172. static bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const uint32_t hsk, uint32_t hsv, bool f32acc) {
  5173. // Needs to be kept up to date on shader changes
  5174. GGML_UNUSED(hsv);
  5175. const uint32_t wg_size = scalar_flash_attention_workgroup_size;
  5176. const uint32_t Br = coopmat1_flash_attention_num_large_rows;
  5177. const uint32_t Bc = scalar_flash_attention_Bc;
  5178. const uint32_t acctype = f32acc ? 4 : 2;
  5179. const uint32_t f16vec4 = 8;
  5180. const uint32_t tmpsh = wg_size * sizeof(float);
  5181. const uint32_t tmpshv4 = wg_size * 4 * acctype;
  5182. const uint32_t Qf = Br * (hsk / 4 + 2) * f16vec4;
  5183. const uint32_t sfshstride = (hsk <= 128) ? (Br + 8) : Br;
  5184. const uint32_t sfsh = Bc * sfshstride * acctype;
  5185. const uint32_t kshstride = hsk / 4 + 2;
  5186. const uint32_t ksh = Bc * kshstride * f16vec4;
  5187. const uint32_t slope = Br * sizeof(float);
  5188. const uint32_t total_size = tmpsh + tmpshv4 + Qf + sfsh + ksh + slope;
  5189. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  5190. VK_LOG_DEBUG("ggml_vk_flash_attn_coopmat_shmem_support(HSK=" << hsk << ", HSV=" << hsv << ", f32acc=" << f32acc << ", total_size=" << total_size << ", supported=" << supported);
  5191. return supported;
  5192. }
  5193. 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) {
  5194. 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];
  5195. 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];
  5196. 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];
  5197. 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];
  5198. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  5199. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  5200. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  5201. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  5202. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  5203. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  5204. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  5205. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  5206. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  5207. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  5208. const uint32_t nem2 = mask ? mask->ne[2] : 0;
  5209. const uint32_t HSK = nek0;
  5210. const uint32_t HSV = nev0;
  5211. uint32_t N = neq1;
  5212. const uint32_t KV = nek1;
  5213. GGML_ASSERT(ne0 == HSV);
  5214. GGML_ASSERT(ne2 == N);
  5215. // input tensor rows must be contiguous
  5216. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  5217. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  5218. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  5219. GGML_ASSERT(neq0 == HSK);
  5220. GGML_ASSERT(neq1 == N);
  5221. GGML_ASSERT(nev1 == nek1);
  5222. // dst cannot be transposed or permuted
  5223. GGML_ASSERT(nb0 == sizeof(float));
  5224. GGML_ASSERT(nb0 <= nb1);
  5225. GGML_ASSERT(nb1 <= nb2);
  5226. GGML_ASSERT(nb2 <= nb3);
  5227. assert(dst->type == GGML_TYPE_F32);
  5228. assert(q->type == GGML_TYPE_F32);
  5229. assert(k->type == v->type);
  5230. FaCodePath path = ctx->device->coopmat2 ? FA_COOPMAT2 :
  5231. ctx->device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
  5232. if (path == FA_COOPMAT1) {
  5233. const bool coopmat_shape_supported = (dst->op_params[3] == GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f32acc) ||
  5234. (dst->op_params[3] != GGML_PREC_F32 && ctx->device->coopmat_support_16x16x16_f16acc);
  5235. const bool coopmat_shmem_supported = ggml_vk_flash_attn_coopmat_shmem_support(ctx->device, HSK, HSV, dst->op_params[3] == GGML_PREC_F32);
  5236. if (!coopmat_shape_supported || !coopmat_shmem_supported) {
  5237. path = FA_SCALAR;
  5238. }
  5239. }
  5240. uint32_t gqa_ratio = 1;
  5241. uint32_t qk_ratio = neq2 / nek2;
  5242. uint32_t workgroups_x = (uint32_t)neq1;
  5243. uint32_t workgroups_y = (uint32_t)neq2;
  5244. uint32_t workgroups_z = (uint32_t)neq3;
  5245. // For scalar/coopmat1 FA, we can use the "large" size to accommodate qga.
  5246. // For coopmat2 FA, we always use the small size (which is still pretty large for gqa).
  5247. uint32_t max_gqa;
  5248. switch (path) {
  5249. case FA_SCALAR:
  5250. case FA_COOPMAT1:
  5251. // We may switch from coopmat1 to scalar, so use the scalar limit for both
  5252. max_gqa = scalar_flash_attention_num_large_rows;
  5253. break;
  5254. case FA_COOPMAT2:
  5255. max_gqa = get_fa_num_small_rows(FA_COOPMAT2);
  5256. break;
  5257. default:
  5258. GGML_ASSERT(0);
  5259. }
  5260. if (N == 1 && qk_ratio > 1 && qk_ratio <= max_gqa &&
  5261. qk_ratio * nek2 == neq2 && nek2 == nev2 && neq3 == 1 && nek3 == 1 && nev3 == 1) {
  5262. // grouped query attention - make the N dimension equal to gqa_ratio, reduce
  5263. // workgroups proportionally in y dimension. The shader will detect gqa_ratio > 1
  5264. // and change addressing calculations to index Q's dimension 2.
  5265. gqa_ratio = qk_ratio;
  5266. N = gqa_ratio;
  5267. workgroups_y /= N;
  5268. }
  5269. vk_pipeline *pipelines;
  5270. bool small_rows = N <= get_fa_num_small_rows(path);
  5271. // coopmat1 does not actually support "small rows" (it needs 16 rows).
  5272. // So use scalar instead.
  5273. if (small_rows && path == FA_COOPMAT1) {
  5274. path = FA_SCALAR;
  5275. }
  5276. // scalar is faster than coopmat2 when N==1
  5277. if (N == 1 && path == FA_COOPMAT2) {
  5278. path = FA_SCALAR;
  5279. }
  5280. // with large hsk/hsv, scalar path may need to use small_rows to fit in shared memory
  5281. if (path == FA_SCALAR &&
  5282. !ggml_vk_flash_attn_scalar_shmem_support(ctx->device, HSK, HSV)) {
  5283. small_rows = true;
  5284. }
  5285. bool f32acc = path == FA_SCALAR || dst->op_params[3] == GGML_PREC_F32;
  5286. FaHeadSizes head_sizes = fa_get_head_sizes(k->ne[0], v->ne[0]);
  5287. switch (path) {
  5288. case FA_SCALAR:
  5289. pipelines = &ctx->device->pipeline_flash_attn_f32_f16[k->type][head_sizes][f32acc][small_rows][0];
  5290. break;
  5291. case FA_COOPMAT1:
  5292. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm1[k->type][head_sizes][f32acc][small_rows][0];
  5293. break;
  5294. case FA_COOPMAT2:
  5295. pipelines = &ctx->device->pipeline_flash_attn_f32_f16_cm2[k->type][head_sizes][f32acc][small_rows][0];
  5296. break;
  5297. default:
  5298. GGML_ASSERT(0);
  5299. }
  5300. assert(pipelines);
  5301. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  5302. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  5303. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  5304. bool aligned = (KV % pipelines[1]->align) == 0 &&
  5305. // the "aligned" shader variant will forcibly align strides, for performance
  5306. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  5307. // mask dim1 is padded to 64, we rely on this to avoid clamping mask loads
  5308. GGML_ASSERT((nem1 % GGML_KQ_MASK_PAD) == 0);
  5309. vk_pipeline pipeline = pipelines[aligned];
  5310. assert(pipeline);
  5311. uint32_t split_kv = KV;
  5312. uint32_t split_k = 1;
  5313. // Use a placeholder core count if one isn't available. split_k is a big help for perf.
  5314. const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count : 16;
  5315. // Try to use split_k when KV is large enough to be worth the overhead
  5316. if (workgroups_x == 1 && shader_core_count > 0 && KV >= 512) {
  5317. // Try to run two workgroups per SM.
  5318. split_k = shader_core_count * 2 / (workgroups_y * workgroups_z);
  5319. if (split_k > 1) {
  5320. // Try to evenly split KV into split_k chunks, but it needs to be a multiple
  5321. // of "align", so recompute split_k based on that.
  5322. split_kv = ROUNDUP_POW2(KV / split_k, pipelines[1]->align);
  5323. split_k = CEIL_DIV(KV, split_kv);
  5324. workgroups_x = split_k;
  5325. }
  5326. }
  5327. // Reserve space for split_k temporaries. For each split x batch, we need to store the O matrix (D x ne1)
  5328. // and the per-row m and L values (ne1 rows). We store all the matrices first, followed by the rows.
  5329. const uint64_t split_k_size = split_k > 1 ? (HSV * ne1 * sizeof(float) + ne1 * sizeof(float) * 2) * split_k * ne3 : 0;
  5330. if (split_k_size > ctx->device->max_memory_allocation_size) {
  5331. GGML_ABORT("Requested preallocation size is too large");
  5332. }
  5333. if (ctx->prealloc_size_split_k < split_k_size) {
  5334. ctx->prealloc_size_split_k = split_k_size;
  5335. }
  5336. if (dryrun) {
  5337. // Request descriptor sets
  5338. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5339. if (split_k > 1) {
  5340. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_flash_attn_split_k_reduce, 1);
  5341. }
  5342. return;
  5343. }
  5344. float scale = 1.0f;
  5345. float max_bias = 0.0f;
  5346. float logit_softcap = 0.0f;
  5347. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  5348. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  5349. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  5350. if (logit_softcap != 0) {
  5351. scale /= logit_softcap;
  5352. }
  5353. const uint32_t n_head_kv = neq2;
  5354. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5355. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5356. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5357. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  5358. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  5359. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  5360. if (ctx->device->uma) {
  5361. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  5362. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  5363. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  5364. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  5365. Q_uma = d_Q != nullptr;
  5366. K_uma = d_K != nullptr;
  5367. V_uma = d_V != nullptr;
  5368. D_uma = d_D != nullptr;
  5369. if (mask) {
  5370. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  5371. M_uma = d_M != nullptr;
  5372. }
  5373. }
  5374. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5375. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  5376. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  5377. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  5378. if (!Q_uma) {
  5379. d_Q = q_buf_ctx->dev_buffer;
  5380. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  5381. }
  5382. if (!K_uma) {
  5383. d_K = k_buf_ctx->dev_buffer;
  5384. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  5385. }
  5386. if (!V_uma) {
  5387. d_V = v_buf_ctx->dev_buffer;
  5388. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  5389. }
  5390. if (!D_uma) {
  5391. d_D = d_buf_ctx->dev_buffer;
  5392. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5393. }
  5394. if (!M_uma) {
  5395. d_M = d_Q;
  5396. m_buf_offset = q_buf_offset;
  5397. if (mask) {
  5398. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  5399. d_M = m_buf_ctx->dev_buffer;
  5400. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  5401. }
  5402. }
  5403. const vk_flash_attn_push_constants pc = { N, KV,
  5404. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  5405. (uint32_t)neq2, (uint32_t)neq3,
  5406. (uint32_t)nek2, (uint32_t)nek3,
  5407. (uint32_t)nev2, (uint32_t)nev3,
  5408. nem1, nem2,
  5409. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  5410. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  5411. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  5412. scale, max_bias, logit_softcap,
  5413. mask != nullptr, n_head_log2, m0, m1,
  5414. gqa_ratio, split_kv, split_k };
  5415. ggml_vk_sync_buffers(subctx);
  5416. if (split_k > 1) {
  5417. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5418. {
  5419. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5420. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5421. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5422. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5423. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5424. },
  5425. // We only use split_k when group query attention is enabled, which means
  5426. // there's no more than one tile of rows (i.e. workgroups_x would have been
  5427. // one). We reuse workgroups_x to mean the number of splits, so we need to
  5428. // cancel out the divide by wg_denoms[0].
  5429. pc, { workgroups_x * pipeline->wg_denoms[0], workgroups_y, workgroups_z });
  5430. ggml_vk_sync_buffers(subctx);
  5431. const std::array<uint32_t, 4> pc2 = { HSV, (uint32_t)ne1, (uint32_t)ne3, split_k };
  5432. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_flash_attn_split_k_reduce,
  5433. {
  5434. vk_subbuffer{ctx->prealloc_split_k, 0, VK_WHOLE_SIZE},
  5435. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5436. },
  5437. pc2, { (uint32_t)ne1, 1, (uint32_t)ne3 });
  5438. } else {
  5439. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  5440. {
  5441. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  5442. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  5443. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  5444. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  5445. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  5446. },
  5447. pc, { workgroups_x, workgroups_y, workgroups_z });
  5448. }
  5449. }
  5450. 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) {
  5451. switch (op) {
  5452. case GGML_OP_GET_ROWS:
  5453. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5454. if (dst->type == GGML_TYPE_F16) {
  5455. return ctx->device->pipeline_get_rows[src0->type];
  5456. }
  5457. if (dst->type == GGML_TYPE_F32) {
  5458. return ctx->device->pipeline_get_rows_f32[src0->type];
  5459. }
  5460. return nullptr;
  5461. case GGML_OP_ACC:
  5462. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5463. return ctx->device->pipeline_acc_f32;
  5464. }
  5465. return nullptr;
  5466. case GGML_OP_ADD:
  5467. case GGML_OP_SUB:
  5468. case GGML_OP_MUL:
  5469. case GGML_OP_DIV:
  5470. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5471. (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
  5472. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
  5473. return nullptr;
  5474. }
  5475. switch (op) {
  5476. case GGML_OP_ADD:
  5477. {
  5478. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_norepeat : ctx->device->pipeline_add;
  5479. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5480. }
  5481. case GGML_OP_SUB:
  5482. {
  5483. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_norepeat : ctx->device->pipeline_sub;
  5484. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5485. }
  5486. case GGML_OP_MUL:
  5487. {
  5488. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_norepeat : ctx->device->pipeline_mul;
  5489. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5490. }
  5491. case GGML_OP_DIV:
  5492. {
  5493. auto pipelines = ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_norepeat : ctx->device->pipeline_div;
  5494. return pipelines[src0->type == GGML_TYPE_F16][src1->type == GGML_TYPE_F16][dst->type == GGML_TYPE_F16];
  5495. }
  5496. default:
  5497. break;
  5498. }
  5499. return nullptr;
  5500. case GGML_OP_CONCAT:
  5501. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5502. return ctx->device->pipeline_concat_f32;
  5503. }
  5504. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5505. return ctx->device->pipeline_concat_f16;
  5506. }
  5507. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  5508. return ctx->device->pipeline_concat_i32;
  5509. }
  5510. return nullptr;
  5511. case GGML_OP_UPSCALE:
  5512. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 && dst->op_params[0] == GGML_SCALE_MODE_NEAREST) {
  5513. return ctx->device->pipeline_upscale_f32;
  5514. }
  5515. return nullptr;
  5516. case GGML_OP_SCALE:
  5517. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5518. return ctx->device->pipeline_scale_f32;
  5519. }
  5520. return nullptr;
  5521. case GGML_OP_SQR:
  5522. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5523. return ctx->device->pipeline_sqr_f32;
  5524. }
  5525. return nullptr;
  5526. case GGML_OP_SIN:
  5527. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5528. return ctx->device->pipeline_sin_f32;
  5529. }
  5530. return nullptr;
  5531. case GGML_OP_COS:
  5532. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5533. return ctx->device->pipeline_cos_f32;
  5534. }
  5535. return nullptr;
  5536. case GGML_OP_CLAMP:
  5537. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5538. return ctx->device->pipeline_clamp_f32;
  5539. }
  5540. return nullptr;
  5541. case GGML_OP_PAD:
  5542. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5543. return ctx->device->pipeline_pad_f32;
  5544. }
  5545. return nullptr;
  5546. case GGML_OP_REPEAT:
  5547. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  5548. return ctx->device->pipeline_repeat_f32;
  5549. }
  5550. return nullptr;
  5551. case GGML_OP_REPEAT_BACK:
  5552. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5553. return ctx->device->pipeline_repeat_back_f32;
  5554. }
  5555. return nullptr;
  5556. case GGML_OP_CPY:
  5557. case GGML_OP_CONT:
  5558. case GGML_OP_DUP:
  5559. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  5560. case GGML_OP_SILU_BACK:
  5561. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5562. return ctx->device->pipeline_silu_back_f32;
  5563. }
  5564. return nullptr;
  5565. case GGML_OP_NORM:
  5566. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5567. return ctx->device->pipeline_norm_f32;
  5568. }
  5569. return nullptr;
  5570. case GGML_OP_GROUP_NORM:
  5571. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5572. return ctx->device->pipeline_group_norm_f32;
  5573. }
  5574. return nullptr;
  5575. case GGML_OP_RMS_NORM:
  5576. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5577. return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx->device->pipeline_rms_norm_f32;
  5578. }
  5579. return nullptr;
  5580. case GGML_OP_RMS_NORM_BACK:
  5581. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5582. return ctx->device->pipeline_rms_norm_back_f32;
  5583. }
  5584. return nullptr;
  5585. case GGML_OP_L2_NORM:
  5586. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5587. return ctx->device->pipeline_l2_norm_f32;
  5588. }
  5589. return nullptr;
  5590. case GGML_OP_UNARY:
  5591. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5592. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5593. (src0->type != dst->type)) {
  5594. return nullptr;
  5595. }
  5596. switch (ggml_get_unary_op(dst)) {
  5597. case GGML_UNARY_OP_SILU:
  5598. return ctx->device->pipeline_silu[dst->type == GGML_TYPE_F16];
  5599. case GGML_UNARY_OP_GELU:
  5600. return ctx->device->pipeline_gelu[dst->type == GGML_TYPE_F16];
  5601. case GGML_UNARY_OP_GELU_ERF:
  5602. return ctx->device->pipeline_gelu_erf[dst->type == GGML_TYPE_F16];
  5603. case GGML_UNARY_OP_GELU_QUICK:
  5604. return ctx->device->pipeline_gelu_quick[dst->type == GGML_TYPE_F16];
  5605. case GGML_UNARY_OP_RELU:
  5606. return ctx->device->pipeline_relu[dst->type == GGML_TYPE_F16];
  5607. case GGML_UNARY_OP_TANH:
  5608. return ctx->device->pipeline_tanh[dst->type == GGML_TYPE_F16];
  5609. case GGML_UNARY_OP_SIGMOID:
  5610. return ctx->device->pipeline_sigmoid[dst->type == GGML_TYPE_F16];
  5611. default:
  5612. break;
  5613. }
  5614. return nullptr;
  5615. case GGML_OP_GLU:
  5616. if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
  5617. (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) ||
  5618. (src0->type != dst->type)) {
  5619. return nullptr;
  5620. }
  5621. switch (ggml_get_glu_op(dst)) {
  5622. case GGML_GLU_OP_GEGLU:
  5623. return ctx->device->pipeline_geglu[dst->type == GGML_TYPE_F16];
  5624. case GGML_GLU_OP_REGLU:
  5625. return ctx->device->pipeline_reglu[dst->type == GGML_TYPE_F16];
  5626. case GGML_GLU_OP_SWIGLU:
  5627. return ctx->device->pipeline_swiglu[dst->type == GGML_TYPE_F16];
  5628. case GGML_GLU_OP_GEGLU_ERF:
  5629. return ctx->device->pipeline_geglu_erf[dst->type == GGML_TYPE_F16];
  5630. case GGML_GLU_OP_GEGLU_QUICK:
  5631. return ctx->device->pipeline_geglu_quick[dst->type == GGML_TYPE_F16];
  5632. default:
  5633. break;
  5634. }
  5635. return nullptr;
  5636. case GGML_OP_DIAG_MASK_INF:
  5637. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5638. return ctx->device->pipeline_diag_mask_inf_f32;
  5639. }
  5640. return nullptr;
  5641. case GGML_OP_SOFT_MAX:
  5642. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  5643. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  5644. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  5645. }
  5646. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5647. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  5648. }
  5649. return nullptr;
  5650. case GGML_OP_SOFT_MAX_BACK:
  5651. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5652. return ctx->device->pipeline_soft_max_back_f32;
  5653. }
  5654. return nullptr;
  5655. case GGML_OP_ROPE:
  5656. case GGML_OP_ROPE_BACK:
  5657. {
  5658. const int mode = ((const int32_t *) dst->op_params)[2];
  5659. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  5660. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  5661. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  5662. if (is_neox) {
  5663. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5664. return ctx->device->pipeline_rope_neox_f32;
  5665. }
  5666. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5667. return ctx->device->pipeline_rope_neox_f16;
  5668. }
  5669. } else if (is_mrope && !is_vision) {
  5670. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5671. return ctx->device->pipeline_rope_multi_f32;
  5672. }
  5673. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5674. return ctx->device->pipeline_rope_multi_f16;
  5675. }
  5676. } else if (is_vision) {
  5677. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5678. return ctx->device->pipeline_rope_vision_f32;
  5679. }
  5680. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5681. return ctx->device->pipeline_rope_vision_f16;
  5682. }
  5683. } else {
  5684. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5685. return ctx->device->pipeline_rope_norm_f32;
  5686. }
  5687. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5688. return ctx->device->pipeline_rope_norm_f16;
  5689. }
  5690. }
  5691. return nullptr;
  5692. }
  5693. case GGML_OP_ARGSORT:
  5694. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5695. return ctx->device->pipeline_argsort_f32;
  5696. }
  5697. return nullptr;
  5698. case GGML_OP_SUM:
  5699. case GGML_OP_SUM_ROWS:
  5700. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5701. return ctx->device->pipeline_sum_rows_f32;
  5702. }
  5703. return nullptr;
  5704. case GGML_OP_ARGMAX:
  5705. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  5706. return ctx->device->pipeline_argmax_f32;
  5707. }
  5708. return nullptr;
  5709. case GGML_OP_COUNT_EQUAL:
  5710. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  5711. return ctx->device->pipeline_count_equal_i32;
  5712. }
  5713. return nullptr;
  5714. case GGML_OP_IM2COL:
  5715. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5716. return ctx->device->pipeline_im2col_f32;
  5717. }
  5718. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  5719. return ctx->device->pipeline_im2col_f32_f16;
  5720. }
  5721. return nullptr;
  5722. case GGML_OP_TIMESTEP_EMBEDDING:
  5723. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5724. return ctx->device->pipeline_timestep_embedding_f32;
  5725. }
  5726. return nullptr;
  5727. case GGML_OP_CONV_TRANSPOSE_1D:
  5728. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5729. return ctx->device->pipeline_conv_transpose_1d_f32;
  5730. }
  5731. return nullptr;
  5732. case GGML_OP_POOL_2D:
  5733. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5734. return ctx->device->pipeline_pool2d_f32;
  5735. }
  5736. return nullptr;
  5737. case GGML_OP_RWKV_WKV6:
  5738. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5739. return ctx->device->pipeline_rwkv_wkv6_f32;
  5740. }
  5741. return nullptr;
  5742. case GGML_OP_RWKV_WKV7:
  5743. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5744. return ctx->device->pipeline_rwkv_wkv7_f32;
  5745. }
  5746. return nullptr;
  5747. case GGML_OP_OPT_STEP_ADAMW:
  5748. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5749. return ctx->device->pipeline_opt_step_adamw_f32;
  5750. }
  5751. return nullptr;
  5752. case GGML_OP_LEAKY_RELU:
  5753. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5754. return ctx->device->pipeline_leaky_relu_f32;
  5755. }
  5756. return nullptr;
  5757. case GGML_OP_CONV_2D_DW:
  5758. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5759. if (ggml_is_contiguous(src1)) {
  5760. return ctx->device->pipeline_conv2d_dw_whcn_f32;
  5761. } else if (ggml_is_contiguous_channels(src1)) {
  5762. return ctx->device->pipeline_conv2d_dw_cwhn_f32;
  5763. }
  5764. }
  5765. return nullptr;
  5766. default:
  5767. return nullptr;
  5768. }
  5769. GGML_UNUSED(src2);
  5770. }
  5771. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  5772. switch (op) {
  5773. case GGML_OP_CPY:
  5774. case GGML_OP_GET_ROWS:
  5775. case GGML_OP_ADD:
  5776. case GGML_OP_SUB:
  5777. case GGML_OP_MUL:
  5778. case GGML_OP_DIV:
  5779. case GGML_OP_CONCAT:
  5780. case GGML_OP_UPSCALE:
  5781. case GGML_OP_SQR:
  5782. case GGML_OP_SIN:
  5783. case GGML_OP_COS:
  5784. case GGML_OP_CLAMP:
  5785. case GGML_OP_PAD:
  5786. case GGML_OP_REPEAT:
  5787. case GGML_OP_REPEAT_BACK:
  5788. case GGML_OP_ROPE:
  5789. case GGML_OP_RMS_NORM:
  5790. case GGML_OP_CONV_2D_DW:
  5791. case GGML_OP_IM2COL:
  5792. return true;
  5793. default:
  5794. return false;
  5795. }
  5796. }
  5797. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  5798. {
  5799. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  5800. }
  5801. 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) {
  5802. GGML_UNUSED(p);
  5803. GGML_UNUSED(src0);
  5804. GGML_UNUSED(src1);
  5805. GGML_UNUSED(src2);
  5806. GGML_UNUSED(dst);
  5807. static_assert(!std::is_const<T>::value, "unexpected type");
  5808. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  5809. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  5810. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  5811. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  5812. }
  5813. 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) {
  5814. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5815. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5816. p.misalign_offsets = (a_offset << 16) | d_offset;
  5817. GGML_UNUSED(src1);
  5818. GGML_UNUSED(src2);
  5819. }
  5820. 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) {
  5821. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5822. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  5823. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5824. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  5825. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  5826. GGML_UNUSED(src2);
  5827. }
  5828. 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) {
  5829. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5830. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5831. p.a_offset = a_offset;
  5832. p.d_offset = d_offset;
  5833. GGML_UNUSED(src1);
  5834. GGML_UNUSED(src2);
  5835. }
  5836. template<typename PC>
  5837. 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) {
  5838. 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];
  5839. if (src1 != nullptr) {
  5840. 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];
  5841. }
  5842. if (src2 != nullptr) {
  5843. 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];
  5844. }
  5845. 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];
  5846. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  5847. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  5848. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  5849. GGML_ASSERT(dst->buffer != nullptr);
  5850. const uint64_t ne00 = src0->ne[0];
  5851. const uint64_t ne01 = src0->ne[1];
  5852. const uint64_t ne02 = src0->ne[2];
  5853. const uint64_t ne03 = src0->ne[3];
  5854. const uint64_t ne0 = ne00 * ne01;
  5855. const bool use_src1 = src1 != nullptr;
  5856. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  5857. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  5858. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  5859. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  5860. const uint64_t ne1 = ne10 * ne11;
  5861. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  5862. const bool use_src2 = src2 != nullptr;
  5863. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  5864. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  5865. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  5866. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  5867. const uint64_t ne2 = ne20 * ne21;
  5868. const uint64_t ned0 = dst->ne[0];
  5869. const uint64_t ned1 = dst->ne[1];
  5870. const uint64_t ned2 = dst->ne[2];
  5871. const uint64_t ned3 = dst->ne[3];
  5872. const uint64_t ned = ned0 * ned1;
  5873. init_pushconst_fastdiv(pc);
  5874. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  5875. if (pipeline == nullptr) {
  5876. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  5877. if (src1 != nullptr) {
  5878. std::cerr << " and " << ggml_type_name(src1->type);
  5879. }
  5880. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  5881. GGML_ABORT("fatal error");
  5882. }
  5883. if (dryrun) {
  5884. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  5885. return;
  5886. }
  5887. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  5888. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5889. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5890. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  5891. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  5892. vk_buffer d_X = nullptr;
  5893. size_t x_buf_offset = 0;
  5894. vk_buffer d_Y = nullptr;
  5895. size_t y_buf_offset = 0;
  5896. vk_buffer d_Z = nullptr;
  5897. size_t z_buf_offset = 0;
  5898. bool src0_uma = false;
  5899. bool src1_uma = false;
  5900. bool src2_uma = false;
  5901. if (ctx->device->uma) {
  5902. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  5903. src0_uma = d_X != nullptr;
  5904. if (use_src1) {
  5905. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  5906. src1_uma = d_Y != nullptr;
  5907. }
  5908. if (use_src2) {
  5909. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  5910. src2_uma = d_Z != nullptr;
  5911. }
  5912. }
  5913. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  5914. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  5915. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  5916. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  5917. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5918. // Workaround for tiny tensor inputs on ROPE
  5919. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  5920. y_sz = VK_WHOLE_SIZE;
  5921. }
  5922. GGML_ASSERT(d_D != nullptr);
  5923. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5924. if(!src0_uma) {
  5925. d_X = src0_buf_ctx->dev_buffer;
  5926. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5927. GGML_ASSERT(d_X != nullptr);
  5928. }
  5929. if (use_src1 && !src1_uma) {
  5930. d_Y = src1_buf_ctx->dev_buffer;
  5931. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5932. GGML_ASSERT(d_Y != nullptr);
  5933. }
  5934. if (use_src2 && !src2_uma) {
  5935. d_Z = src2_buf_ctx->dev_buffer;
  5936. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  5937. GGML_ASSERT(d_Z != nullptr);
  5938. }
  5939. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  5940. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  5941. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5942. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5943. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5944. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5945. if (op_supports_incontiguous) {
  5946. x_sz = ggml_nbytes(src0);
  5947. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  5948. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  5949. d_sz = ggml_nbytes(dst);
  5950. if (x_buf_offset + x_sz >= d_X->size) {
  5951. x_sz = VK_WHOLE_SIZE;
  5952. }
  5953. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  5954. y_sz = VK_WHOLE_SIZE;
  5955. }
  5956. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  5957. z_sz = VK_WHOLE_SIZE;
  5958. }
  5959. if (d_buf_offset + d_sz >= d_D->size) {
  5960. d_sz = VK_WHOLE_SIZE;
  5961. }
  5962. }
  5963. std::array<uint32_t, 3> elements;
  5964. // Single call if dimension 2 is contiguous
  5965. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  5966. switch (op) {
  5967. case GGML_OP_NORM:
  5968. case GGML_OP_RMS_NORM_BACK:
  5969. case GGML_OP_L2_NORM:
  5970. case GGML_OP_SOFT_MAX:
  5971. case GGML_OP_SOFT_MAX_BACK:
  5972. case GGML_OP_SUM_ROWS:
  5973. case GGML_OP_ARGMAX:
  5974. {
  5975. const uint32_t nr = ggml_nrows(src0);
  5976. if (nr > 262144) {
  5977. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  5978. } else if (nr > 512) {
  5979. elements = { 512, CEIL_DIV(nr, 512), 1 };
  5980. } else {
  5981. elements = { nr, 1, 1 };
  5982. }
  5983. } break;
  5984. case GGML_OP_RMS_NORM:
  5985. elements = { (uint32_t)ne01, (uint32_t)ne02, (uint32_t)ne03 };
  5986. break;
  5987. case GGML_OP_SUM:
  5988. // We use GGML_OP_SUM_ROWS with 1 row.
  5989. elements = { 1, 1, 1 };
  5990. break;
  5991. case GGML_OP_GROUP_NORM:
  5992. {
  5993. const uint32_t num_groups = dst->op_params[0];
  5994. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  5995. } break;
  5996. case GGML_OP_DIAG_MASK_INF:
  5997. case GGML_OP_ROPE:
  5998. case GGML_OP_ROPE_BACK:
  5999. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  6000. break;
  6001. case GGML_OP_GET_ROWS:
  6002. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  6003. break;
  6004. case GGML_OP_ARGSORT:
  6005. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  6006. break;
  6007. case GGML_OP_IM2COL:
  6008. {
  6009. const bool is_2D = dst->op_params[6] == 1;
  6010. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6011. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6012. const uint32_t KW = src0->ne[0];
  6013. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6014. const uint32_t OW = dst->ne[1];
  6015. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  6016. elements = { OW * KW * KH, OH, batch * IC };
  6017. } break;
  6018. case GGML_OP_TIMESTEP_EMBEDDING:
  6019. {
  6020. const uint32_t dim = dst->op_params[0];
  6021. uint32_t half_ceil = (dim + 1) / 2;
  6022. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  6023. } break;
  6024. case GGML_OP_CONV_TRANSPOSE_1D:
  6025. {
  6026. elements = {uint32_t(src0->ne[1]), 1, 1}; // parallelize in {Cout, 1, 1}
  6027. } break;
  6028. case GGML_OP_POOL_2D:
  6029. {
  6030. const uint32_t N = dst->ne[3];
  6031. const uint32_t OC = dst->ne[2];
  6032. const uint32_t OH = dst->ne[1];
  6033. const uint32_t OW = dst->ne[0];
  6034. elements = { N * OC * OH * OW, 1, 1};
  6035. } break;
  6036. case GGML_OP_ADD:
  6037. case GGML_OP_SUB:
  6038. case GGML_OP_DIV:
  6039. case GGML_OP_MUL:
  6040. case GGML_OP_SCALE:
  6041. case GGML_OP_SQR:
  6042. case GGML_OP_SIN:
  6043. case GGML_OP_COS:
  6044. case GGML_OP_CLAMP:
  6045. case GGML_OP_PAD:
  6046. case GGML_OP_REPEAT:
  6047. case GGML_OP_REPEAT_BACK:
  6048. case GGML_OP_CPY:
  6049. case GGML_OP_CONCAT:
  6050. case GGML_OP_UPSCALE:
  6051. case GGML_OP_UNARY:
  6052. case GGML_OP_GLU:
  6053. case GGML_OP_CONV_2D_DW:
  6054. {
  6055. uint32_t ne = ggml_nelements(dst);
  6056. if (op == GGML_OP_CPY && ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6057. // Convert from number of logical elements to 2- or 4-byte units.
  6058. ne /= ggml_blck_size(src0->type);
  6059. if ((ggml_type_size(src0->type) % 4) == 0) {
  6060. ne *= ggml_type_size(src0->type) / 4;
  6061. } else {
  6062. ne *= ggml_type_size(src0->type) / 2;
  6063. }
  6064. }
  6065. if (ne > 262144) {
  6066. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  6067. } else if (ne > 512) {
  6068. elements = { 512, CEIL_DIV(ne, 512), 1 };
  6069. } else {
  6070. elements = { ne, 1, 1 };
  6071. }
  6072. } break;
  6073. default:
  6074. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  6075. break;
  6076. }
  6077. if (!op_supports_incontiguous) {
  6078. if (x_sz != VK_WHOLE_SIZE) {
  6079. x_sz *= ne02 * ne03;
  6080. }
  6081. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  6082. y_sz *= ne12 * ne13;
  6083. }
  6084. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  6085. z_sz *= ne22 * ne23;
  6086. }
  6087. if (d_sz != VK_WHOLE_SIZE) {
  6088. d_sz *= ned2 * ned3;
  6089. }
  6090. }
  6091. if (op == GGML_OP_SOFT_MAX || op == GGML_OP_GLU) {
  6092. // Empty src1 is possible in soft_max, but the shader needs a buffer
  6093. vk_subbuffer subbuf_y;
  6094. if (use_src1) {
  6095. subbuf_y = { d_Y, y_buf_offset, y_sz };
  6096. } else {
  6097. subbuf_y = { d_X, 0, x_sz };
  6098. }
  6099. ggml_vk_sync_buffers(subctx);
  6100. 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);
  6101. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  6102. // Empty src2 is possible in rope, but the shader needs a buffer
  6103. vk_subbuffer subbuf_z;
  6104. if (use_src2) {
  6105. subbuf_z = { d_Z, z_buf_offset, z_sz };
  6106. } else {
  6107. subbuf_z = { d_X, 0, x_sz };
  6108. }
  6109. ggml_vk_sync_buffers(subctx);
  6110. 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);
  6111. } else if (op == GGML_OP_IM2COL) {
  6112. // im2col uses only src1 and dst buffers
  6113. ggml_vk_sync_buffers(subctx);
  6114. 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);
  6115. } else if (op == GGML_OP_COUNT_EQUAL) {
  6116. ggml_vk_sync_buffers(subctx);
  6117. // count_equal assumes that destination buffer is initialized with zeroes
  6118. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  6119. ggml_vk_sync_buffers(subctx);
  6120. 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);
  6121. } else if (use_src2) {
  6122. ggml_vk_sync_buffers(subctx);
  6123. 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);
  6124. } else if (use_src1) {
  6125. ggml_vk_sync_buffers(subctx);
  6126. 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);
  6127. } else {
  6128. ggml_vk_sync_buffers(subctx);
  6129. 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);
  6130. }
  6131. }
  6132. 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) {
  6133. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6134. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6135. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6136. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  6137. (uint32_t)ggml_nelements(src0),
  6138. (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,
  6139. (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,
  6140. (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,
  6141. 0,
  6142. 0.0f, 0.0f, 0,
  6143. }, dryrun);
  6144. }
  6145. 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) {
  6146. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6147. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6148. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6149. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6150. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6151. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6152. int offset = dst->op_params[3] / 4; // offset in bytes
  6153. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  6154. (uint32_t)ggml_nelements(src0),
  6155. (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,
  6156. (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,
  6157. (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,
  6158. 0,
  6159. 0.0f, 0.0f, offset,
  6160. }, dryrun);
  6161. }
  6162. 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) {
  6163. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6164. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6165. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6166. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  6167. (uint32_t)ggml_nelements(src0),
  6168. (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,
  6169. (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,
  6170. (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,
  6171. 0,
  6172. 0.0f, 0.0f, 0,
  6173. }, dryrun);
  6174. }
  6175. 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) {
  6176. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6177. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6178. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6179. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  6180. (uint32_t)ggml_nelements(src0),
  6181. (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,
  6182. (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,
  6183. (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,
  6184. 0,
  6185. 0.0f, 0.0f, 0,
  6186. }, dryrun);
  6187. }
  6188. 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) {
  6189. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6190. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6191. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6192. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  6193. (uint32_t)ggml_nelements(src0),
  6194. (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,
  6195. (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,
  6196. (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,
  6197. 0,
  6198. 0.0f, 0.0f, 0,
  6199. }, dryrun);
  6200. }
  6201. 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) {
  6202. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6203. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6204. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6205. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  6206. (uint32_t)ggml_nelements(src0),
  6207. (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,
  6208. (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,
  6209. (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,
  6210. 0,
  6211. 0.0f, 0.0f, 0,
  6212. }, dryrun);
  6213. }
  6214. 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) {
  6215. GGML_ASSERT(version == 6 || version == 7);
  6216. int num_srcs = version == 6 ? 6 : 7;
  6217. for (int i = 0; i < num_srcs; i++) {
  6218. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  6219. }
  6220. GGML_ASSERT(dst->buffer != nullptr);
  6221. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  6222. GGML_ASSERT(pipeline != nullptr);
  6223. if (dryrun) {
  6224. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6225. return;
  6226. }
  6227. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6228. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6229. for (int i = 0; i < num_srcs; i++) {
  6230. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  6231. }
  6232. ggml_vk_sync_buffers(subctx);
  6233. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  6234. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6235. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  6236. if (ctx->device->uma) {
  6237. for (int i = 0; i < num_srcs; i++) {
  6238. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  6239. srcs_uma[i] = d_srcs[i] != nullptr;
  6240. }
  6241. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  6242. dst_uma = d_D != nullptr;
  6243. }
  6244. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  6245. for (int i = 0; i < num_srcs; i++) {
  6246. src_sizes[i] = ggml_nbytes(dst->src[i]);
  6247. if (!srcs_uma[i]) {
  6248. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  6249. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  6250. }
  6251. }
  6252. const uint64_t dst_size = ggml_nbytes(dst);
  6253. if (!dst_uma) {
  6254. d_D = dst_buf_ctx->dev_buffer;
  6255. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  6256. }
  6257. std::array<uint32_t, 3> elements = {
  6258. (uint32_t)(pc.B * pc.H),
  6259. 1,
  6260. 1
  6261. };
  6262. if (version == 6) {
  6263. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6264. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6265. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6266. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6267. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6268. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6269. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6270. vk_subbuffer{ d_D, dst_offset, dst_size }
  6271. }, pc, elements);
  6272. } else if (version == 7) {
  6273. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6274. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  6275. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  6276. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  6277. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  6278. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  6279. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  6280. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  6281. vk_subbuffer{ d_D, dst_offset, dst_size }
  6282. }, pc, elements);
  6283. } else {
  6284. // shouldn't happen
  6285. GGML_ASSERT(false);
  6286. }
  6287. }
  6288. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6289. const size_t seq_length = dst->src[0]->ne[2];
  6290. const size_t n_embed = dst->ne[0];
  6291. const size_t n_heads = dst->src[0]->ne[1];
  6292. const size_t n_seqs = dst->src[5]->ne[1];
  6293. ggml_vk_op_f32_wkv(
  6294. ctx, subctx, dst,
  6295. {
  6296. (uint32_t)n_seqs,
  6297. (uint32_t)seq_length,
  6298. (uint32_t)n_embed,
  6299. (uint32_t)n_heads,
  6300. },
  6301. 6,
  6302. dryrun
  6303. );
  6304. }
  6305. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6306. const size_t seq_length = dst->src[0]->ne[2];
  6307. const size_t n_embed = dst->ne[0];
  6308. const size_t n_heads = dst->src[0]->ne[1];
  6309. const size_t n_seqs = dst->src[6]->ne[1];
  6310. ggml_vk_op_f32_wkv(
  6311. ctx, subctx, dst,
  6312. {
  6313. (uint32_t)n_seqs,
  6314. (uint32_t)seq_length,
  6315. (uint32_t)n_embed,
  6316. (uint32_t)n_heads,
  6317. },
  6318. 7,
  6319. dryrun
  6320. );
  6321. }
  6322. 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) {
  6323. const ggml_tensor * x = dst->src[0];
  6324. const ggml_tensor * g = dst->src[1];
  6325. const ggml_tensor * gm = dst->src[2];
  6326. const ggml_tensor * gv = dst->src[3];
  6327. const ggml_tensor * p = dst->src[4];
  6328. GGML_ASSERT(x->type == GGML_TYPE_F32);
  6329. GGML_ASSERT(g->type == GGML_TYPE_F32);
  6330. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  6331. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  6332. GGML_ASSERT(p->type == GGML_TYPE_F32);
  6333. GGML_ASSERT(dst->buffer != nullptr);
  6334. GGML_ASSERT(ggml_is_contiguous(x));
  6335. GGML_ASSERT(ggml_is_contiguous(g));
  6336. GGML_ASSERT(ggml_is_contiguous(gm));
  6337. GGML_ASSERT(ggml_is_contiguous(gv));
  6338. GGML_ASSERT(ggml_is_contiguous(p));
  6339. GGML_ASSERT(ggml_are_same_shape(x, g));
  6340. GGML_ASSERT(ggml_are_same_shape(x, gm));
  6341. GGML_ASSERT(ggml_are_same_shape(x, gv));
  6342. GGML_ASSERT(ggml_nelements(p) == 7);
  6343. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  6344. GGML_ASSERT(pipeline != nullptr);
  6345. if (dryrun) {
  6346. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  6347. return;
  6348. }
  6349. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  6350. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  6351. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  6352. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  6353. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  6354. ggml_vk_sync_buffers(subctx);
  6355. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  6356. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  6357. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  6358. if (ctx->device->uma) {
  6359. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  6360. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  6361. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  6362. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  6363. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  6364. X_uma = d_X != nullptr;
  6365. G_uma = d_G != nullptr;
  6366. GM_uma = d_GM != nullptr;
  6367. GV_uma = d_GV != nullptr;
  6368. P_uma = d_P != nullptr;
  6369. }
  6370. if (!X_uma) {
  6371. d_X = x_buf_ctx->dev_buffer;
  6372. x_offset = vk_tensor_offset(x) + x->view_offs;
  6373. }
  6374. if (!G_uma) {
  6375. d_G = g_buf_ctx->dev_buffer;
  6376. g_offset = vk_tensor_offset(g) + g->view_offs;
  6377. }
  6378. if (!GM_uma) {
  6379. d_GM = gm_buf_ctx->dev_buffer;
  6380. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  6381. }
  6382. if (!GV_uma) {
  6383. d_GV = gv_buf_ctx->dev_buffer;
  6384. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  6385. }
  6386. if (!P_uma) {
  6387. d_P = p_buf_ctx->dev_buffer;
  6388. p_offset = vk_tensor_offset(p) + p->view_offs;
  6389. }
  6390. const uint64_t x_size = ggml_nbytes(x);
  6391. const uint64_t g_size = ggml_nbytes(g);
  6392. const uint64_t gm_size = ggml_nbytes(gm);
  6393. const uint64_t gv_size = ggml_nbytes(gv);
  6394. const uint64_t p_size = ggml_nbytes(p);
  6395. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  6396. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  6397. vk_subbuffer{ d_X, x_offset, x_size },
  6398. vk_subbuffer{ d_G, g_offset, g_size },
  6399. vk_subbuffer{ d_GM, gm_offset, gm_size },
  6400. vk_subbuffer{ d_GV, gv_offset, gv_size },
  6401. vk_subbuffer{ d_P, p_offset, p_size },
  6402. }, pc, elements);
  6403. }
  6404. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  6405. const size_t n = ggml_nelements(dst->src[0]);
  6406. ggml_vk_op_f32_opt_step_adamw(
  6407. ctx, subctx, dst,
  6408. { (uint32_t)n, 0, 0.0f, 0.0f },
  6409. dryrun
  6410. );
  6411. }
  6412. 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) {
  6413. int * op_params = (int *)dst->op_params;
  6414. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6415. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6416. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6417. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  6418. (uint32_t)ggml_nelements(dst),
  6419. (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,
  6420. (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,
  6421. (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,
  6422. 0,
  6423. 0.0f, 0.0f, op_params[0],
  6424. }, dryrun);
  6425. }
  6426. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6427. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6428. const float sf0 = (float)dst->ne[0] / src0->ne[0];
  6429. const float sf1 = (float)dst->ne[1] / src0->ne[1];
  6430. const float sf2 = (float)dst->ne[2] / src0->ne[2];
  6431. const float sf3 = (float)dst->ne[3] / src0->ne[3];
  6432. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  6433. (uint32_t)ggml_nelements(dst), 0, 0,
  6434. (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,
  6435. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  6436. sf0, sf1, sf2, sf3,
  6437. }, dryrun);
  6438. }
  6439. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6440. float * op_params = (float *)dst->op_params;
  6441. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6442. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6443. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  6444. (uint32_t)ggml_nelements(src0),
  6445. (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,
  6446. (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,
  6447. 0,
  6448. op_params[0], 0.0f,
  6449. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6450. }, dryrun);
  6451. }
  6452. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6453. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6454. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6455. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  6456. (uint32_t)ggml_nelements(src0),
  6457. (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,
  6458. (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,
  6459. 0,
  6460. 0.0f, 0.0f,
  6461. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6462. }, dryrun);
  6463. }
  6464. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6465. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6466. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6467. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, {
  6468. (uint32_t)ggml_nelements(src0),
  6469. (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,
  6470. (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,
  6471. 0,
  6472. 0.0f, 0.0f,
  6473. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6474. }, dryrun);
  6475. }
  6476. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6477. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6478. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6479. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, {
  6480. (uint32_t)ggml_nelements(src0),
  6481. (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,
  6482. (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,
  6483. 0,
  6484. 0.0f, 0.0f,
  6485. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6486. }, dryrun);
  6487. }
  6488. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6489. float * op_params = (float *)dst->op_params;
  6490. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6491. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6492. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  6493. (uint32_t)ggml_nelements(src0),
  6494. (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,
  6495. (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,
  6496. 0,
  6497. op_params[0], op_params[1],
  6498. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6499. }, dryrun);
  6500. }
  6501. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6502. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6503. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6504. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, {
  6505. (uint32_t)ggml_nelements(dst),
  6506. (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,
  6507. (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,
  6508. 0,
  6509. 0.0f, 0.0f,
  6510. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6511. }, dryrun);
  6512. }
  6513. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6514. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6515. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6516. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, {
  6517. (uint32_t)ggml_nelements(dst),
  6518. (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,
  6519. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2], (uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t) dst->nb[1] / dst_type_size, (uint32_t) dst->nb[2] / dst_type_size, (uint32_t) dst->nb[3] / dst_type_size,
  6520. 0,
  6521. 0.0f, 0.0f,
  6522. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6523. }, dryrun);
  6524. }
  6525. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6526. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6527. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6528. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, {
  6529. (uint32_t)ggml_nelements(dst),
  6530. (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,
  6531. (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,
  6532. 0,
  6533. 0.0f, 0.0f,
  6534. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6535. }, dryrun);
  6536. }
  6537. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6538. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6539. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6540. uint32_t ne = (uint32_t)ggml_nelements(src0);
  6541. if (ggml_is_quantized(src0->type) && ggml_is_quantized(dst->type)) {
  6542. // Convert from number of logical elements to 2- or 4-byte units.
  6543. ne /= ggml_blck_size(src0->type);
  6544. if ((ggml_type_size(src0->type) % 4) == 0) {
  6545. ne *= ggml_type_size(src0->type) / 4;
  6546. } else {
  6547. ne *= ggml_type_size(src0->type) / 2;
  6548. }
  6549. }
  6550. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  6551. ne,
  6552. (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,
  6553. (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,
  6554. 0,
  6555. 0.0f, 0.0f,
  6556. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6557. }, dryrun);
  6558. }
  6559. 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) {
  6560. 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);
  6561. }
  6562. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6563. float * op_params = (float *)dst->op_params;
  6564. 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);
  6565. }
  6566. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6567. const int * int_op_params = (const int *)dst->op_params;
  6568. const float * float_op_params = (const float *)dst->op_params;
  6569. const uint32_t num_groups = int_op_params[0];
  6570. const float eps = float_op_params[1];
  6571. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6572. 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);
  6573. }
  6574. 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, bool dryrun = false) {
  6575. float * op_params = (float *)dst->op_params;
  6576. const uint32_t src0_type_size = ggml_type_size(src0->type);
  6577. const uint32_t src1_type_size = ggml_type_size(src1->type);
  6578. const uint32_t dst_type_size = ggml_type_size(dst->type);
  6579. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM, {
  6580. (uint32_t)ggml_nelements(src0),
  6581. (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,
  6582. (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,
  6583. (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,
  6584. 0,
  6585. op_params[0], 0.0f, 0,
  6586. }, dryrun);
  6587. }
  6588. 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) {
  6589. float * op_params = (float *)dst->op_params;
  6590. 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);
  6591. }
  6592. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6593. float * op_params = (float *)dst->op_params;
  6594. 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);
  6595. }
  6596. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6597. 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);
  6598. }
  6599. 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) {
  6600. const bool swapped = (bool)dst->op_params[1];
  6601. const bool split = src1 != nullptr;
  6602. GGML_ASSERT(ggml_is_contiguous(src0));
  6603. if (!split) {
  6604. GGML_ASSERT(src0->ne[0] / 2 == dst->ne[0]);
  6605. } else {
  6606. GGML_ASSERT(src0->ne[0] == src1->ne[0]);
  6607. GGML_ASSERT(src0->ne[0] == dst->ne[0]);
  6608. GGML_ASSERT(src0->type == src1->type);
  6609. }
  6610. const uint32_t mode = split ? 2 : (swapped ? 1 : 0);
  6611. 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);
  6612. }
  6613. 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) {
  6614. int32_t * op_params = (int32_t *)dst->op_params;
  6615. 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);
  6616. }
  6617. 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) {
  6618. float * op_params = (float *)dst->op_params;
  6619. float scale = op_params[0];
  6620. float max_bias = op_params[1];
  6621. const uint32_t ncols = (uint32_t)src0->ne[0];
  6622. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  6623. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  6624. const uint32_t ne12 = src1 ? (uint32_t)(src1->ne[2]) : 0u;
  6625. const uint32_t ne13 = src1 ? (uint32_t)(src1->ne[3]) : 0u;
  6626. const uint32_t nb11 = src1 ? (uint32_t)(src1->nb[1] / src1->nb[0]) : 0u;
  6627. const uint32_t nb12 = src1 ? (uint32_t)(src1->nb[2] / src1->nb[0]) : 0u;
  6628. const uint32_t nb13 = src1 ? (uint32_t)(src1->nb[3] / src1->nb[0]) : 0u;
  6629. const uint32_t n_head_kv = src0->ne[2];
  6630. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6631. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6632. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6633. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  6634. ncols,
  6635. src1 != nullptr ? nrows_y : (uint32_t)0,
  6636. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],
  6637. ne12, ne13,
  6638. nb11, nb12, nb13,
  6639. scale, max_bias,
  6640. m0, m1,
  6641. n_head_log2,
  6642. nrows_x,
  6643. }, dryrun);
  6644. }
  6645. 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) {
  6646. float * op_params = (float *)dst->op_params;
  6647. 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);
  6648. }
  6649. 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) {
  6650. const int n_dims = ((int32_t *) dst->op_params)[1];
  6651. const int mode = ((int32_t *) dst->op_params)[2];
  6652. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  6653. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  6654. const float freq_base = ((float *) dst->op_params)[5];
  6655. const float freq_scale = ((float *) dst->op_params)[6];
  6656. const float ext_factor = ((float *) dst->op_params)[7];
  6657. const float attn_factor = ((float *) dst->op_params)[8];
  6658. const float beta_fast = ((float *) dst->op_params)[9];
  6659. const float beta_slow = ((float *) dst->op_params)[10];
  6660. int sections[4] {};
  6661. if (mode & GGML_ROPE_TYPE_MROPE) {
  6662. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  6663. }
  6664. float corr_dims[2];
  6665. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  6666. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6667. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  6668. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  6669. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  6670. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  6671. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  6672. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  6673. sections[0], sections[1], sections[2], sections[3], backprop
  6674. }, dryrun);
  6675. }
  6676. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6677. int32_t * op_params = (int32_t *)dst->op_params;
  6678. uint32_t ncols = src0->ne[0];
  6679. uint32_t ncols_pad = 1;
  6680. while (ncols_pad < ncols) {
  6681. ncols_pad *= 2;
  6682. }
  6683. GGML_ASSERT(ncols_pad <= 1024);
  6684. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  6685. ncols,
  6686. ncols_pad,
  6687. op_params[0],
  6688. }, dryrun);
  6689. }
  6690. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6691. 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);
  6692. }
  6693. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6694. 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);
  6695. }
  6696. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6697. 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);
  6698. }
  6699. 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) {
  6700. 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);
  6701. }
  6702. 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) {
  6703. const int32_t s0 = dst->op_params[0];
  6704. const int32_t s1 = dst->op_params[1];
  6705. const int32_t p0 = dst->op_params[2];
  6706. const int32_t p1 = dst->op_params[3];
  6707. const int32_t d0 = dst->op_params[4];
  6708. const int32_t d1 = dst->op_params[5];
  6709. const bool is_2D = dst->op_params[6] == 1;
  6710. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  6711. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  6712. const uint32_t IW = src1->ne[0];
  6713. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  6714. const uint32_t KW = src0->ne[0];
  6715. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  6716. const uint32_t OW = dst->ne[1];
  6717. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6718. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  6719. const uint32_t pelements = OW * KW * KH;
  6720. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  6721. batch_offset, offset_delta,
  6722. IC, IW, IH, OW, OH, KW, KH,
  6723. pelements,
  6724. IC * KH * KW,
  6725. s0, s1, p0, p1, d0, d1,
  6726. }, dryrun);
  6727. }
  6728. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6729. const uint32_t dim = dst->op_params[0];
  6730. const uint32_t max_period = dst->op_params[1];
  6731. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  6732. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  6733. nb1, dim, max_period,
  6734. }, dryrun);
  6735. }
  6736. 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) {
  6737. // src0: (K, Cout, Cin, 1) -- kernel
  6738. // src1: (L, Cin, 1, 1) -- input
  6739. // dst: (*, Cout, 1, 1)
  6740. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6741. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6742. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6743. GGML_TENSOR_BINARY_OP_LOCALS
  6744. GGML_ASSERT(nb00 == sizeof(float));
  6745. GGML_ASSERT(nb10 == sizeof(float));
  6746. const int32_t s0 = dst->op_params[0];
  6747. vk_op_conv_transpose_1d_push_constants p{};
  6748. p.Cout = static_cast<uint32_t>(ne01);
  6749. p.Cin = static_cast<uint32_t>(ne02);
  6750. p.K = static_cast<uint32_t>(ne00);
  6751. p.L = static_cast<uint32_t>(ne10);
  6752. p.KL = static_cast<uint32_t>(ne0);
  6753. p.nb01 = static_cast<uint32_t>(nb01 / nb00);
  6754. p.nb02 = static_cast<uint32_t>(nb02 / nb00);
  6755. p.nb11 = static_cast<uint32_t>(nb11 / nb10);
  6756. p.nb1 = static_cast<uint32_t>(nb1 / nb0);
  6757. p.s0 = static_cast<uint32_t>(s0);
  6758. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_TRANSPOSE_1D, std::move(p), dryrun);
  6759. }
  6760. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6761. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  6762. const int32_t k1 = dst->op_params[1];
  6763. const int32_t k0 = dst->op_params[2];
  6764. const int32_t s1 = dst->op_params[3];
  6765. const int32_t s0 = dst->op_params[4];
  6766. const int32_t p1 = dst->op_params[5];
  6767. const int32_t p0 = dst->op_params[6];
  6768. const uint32_t IH = src0->ne[1];
  6769. const uint32_t IW = src0->ne[0];
  6770. const uint32_t N = dst->ne[3];
  6771. const uint32_t OC = dst->ne[2];
  6772. const uint32_t OH = dst->ne[1];
  6773. const uint32_t OW = dst->ne[0];
  6774. const uint32_t parallel_elements = N * OC * OH * OW;
  6775. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  6776. IW, IH, OW, OH, OC,
  6777. parallel_elements,
  6778. op,
  6779. k0, k1, s0, s1, p0, p1,
  6780. }, dryrun);
  6781. }
  6782. 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) {
  6783. vk_op_conv2d_dw_push_constants p{};
  6784. p.ne = ggml_nelements(dst);
  6785. p.channels = dst->ne[2];
  6786. p.batches = dst->ne[3];
  6787. p.dst_w = dst->ne[0];
  6788. p.dst_h = dst->ne[1];
  6789. p.src_w = src1->ne[0];
  6790. p.src_h = src1->ne[1];
  6791. p.knl_w = src0->ne[0];
  6792. p.knl_h = src0->ne[1];
  6793. p.stride_x = dst->op_params[0];
  6794. p.stride_y = dst->op_params[1];
  6795. p.pad_x = dst->op_params[2];
  6796. p.pad_y = dst->op_params[3];
  6797. p.dilation_x = dst->op_params[4];
  6798. p.dilation_y = dst->op_params[5];
  6799. GGML_ASSERT(src0->ne[3] == p.channels);
  6800. GGML_ASSERT(src1->ne[3] == p.batches);
  6801. ggml_vk_op_f32(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONV_2D_DW, std::move(p), dryrun);
  6802. }
  6803. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  6804. const float * op_params = (const float *)dst->op_params;
  6805. 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);
  6806. }
  6807. #ifdef GGML_VULKAN_RUN_TESTS
  6808. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  6809. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  6810. return;
  6811. }
  6812. i0 = std::max(i0, 5);
  6813. i1 = std::max(i1, 5);
  6814. i2 = std::max(i2, 0);
  6815. fprintf(stderr, " ");
  6816. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6817. fprintf(stderr, "%7d ", idx1);
  6818. }
  6819. fprintf(stderr, "\n");
  6820. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6821. fprintf(stderr, "%7d: ", idx0);
  6822. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6823. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  6824. float val;
  6825. if (type == GGML_TYPE_F32) {
  6826. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  6827. } else if (type == GGML_TYPE_F16) {
  6828. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  6829. } else {
  6830. GGML_ABORT("fatal error");
  6831. }
  6832. fprintf(stderr, "% 7.2f ", val);
  6833. } else {
  6834. fprintf(stderr, " ");
  6835. }
  6836. }
  6837. fprintf(stderr, "\n");
  6838. }
  6839. }
  6840. template <typename X_TYPE, typename Y_TYPE>
  6841. 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) {
  6842. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  6843. const size_t x_ne = m * k * batch;
  6844. const size_t y_ne = k * n * batch;
  6845. const size_t d_ne = m * n * batch;
  6846. vk_pipeline p;
  6847. std::string shname;
  6848. if (shader_size == 0) {
  6849. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6850. p = ctx->device->pipeline_matmul_f32->a_s;
  6851. shname = "F32_ALIGNED_S";
  6852. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6853. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  6854. shname = "F32_F16_ALIGNED_S";
  6855. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6856. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  6857. shname = "F16_F32_ALIGNED_S";
  6858. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6859. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  6860. shname = "F16_ALIGNED_S";
  6861. } else {
  6862. GGML_ABORT("fatal error");
  6863. }
  6864. } else if (shader_size == 1) {
  6865. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6866. p = ctx->device->pipeline_matmul_f32->a_m;
  6867. shname = "F32_ALIGNED_M";
  6868. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6869. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  6870. shname = "F32_F16_ALIGNED_M";
  6871. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6872. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  6873. shname = "F16_F32_ALIGNED_M";
  6874. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6875. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  6876. shname = "F16_ALIGNED_M";
  6877. } else {
  6878. GGML_ABORT("fatal error");
  6879. }
  6880. } else if (shader_size == 2) {
  6881. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6882. p = ctx->device->pipeline_matmul_f32->a_l;
  6883. shname = "F32_ALIGNED_L";
  6884. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6885. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  6886. shname = "F32_F16_ALIGNED_L";
  6887. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6888. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  6889. shname = "F16_F32_ALIGNED_L";
  6890. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6891. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  6892. shname = "F16_ALIGNED_L";
  6893. } else {
  6894. GGML_ABORT("fatal error");
  6895. }
  6896. } else {
  6897. GGML_ASSERT(0);
  6898. }
  6899. const size_t kpad = ggml_vk_align_size(k, p->align);
  6900. if (k != kpad) {
  6901. if (shader_size == 0) {
  6902. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6903. p = ctx->device->pipeline_matmul_f32->s;
  6904. shname = "F32_S";
  6905. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6906. p = ctx->device->pipeline_matmul_f32_f16->s;
  6907. shname = "F32_F16_S";
  6908. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6909. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  6910. shname = "F16_F32_S";
  6911. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6912. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  6913. shname = "F16_S";
  6914. }
  6915. } else if (shader_size == 1) {
  6916. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6917. p = ctx->device->pipeline_matmul_f32->m;
  6918. shname = "F32_M";
  6919. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6920. p = ctx->device->pipeline_matmul_f32_f16->m;
  6921. shname = "F32_F16_M";
  6922. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6923. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  6924. shname = "F16_F32_M";
  6925. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6926. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  6927. shname = "F16_M";
  6928. }
  6929. } else if (shader_size == 2) {
  6930. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6931. p = ctx->device->pipeline_matmul_f32->l;
  6932. shname = "F32_L";
  6933. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6934. p = ctx->device->pipeline_matmul_f32_f16->l;
  6935. shname = "F32_F16_L";
  6936. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6937. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  6938. shname = "F16_F32_L";
  6939. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6940. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  6941. shname = "F16_L";
  6942. }
  6943. }
  6944. }
  6945. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  6946. if (split_k > 1) {
  6947. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6948. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6949. // Resize buffer
  6950. if (ctx->prealloc_split_k != nullptr) {
  6951. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6952. }
  6953. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6954. }
  6955. }
  6956. if (ctx->device->need_compiles) {
  6957. ggml_vk_load_shaders(ctx->device);
  6958. }
  6959. ggml_pipeline_allocate_descriptor_sets(ctx);
  6960. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6961. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6962. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6963. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  6964. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  6965. float* d = (float *) malloc(sizeof(float) * d_ne);
  6966. for (size_t i = 0; i < x_ne; i++) {
  6967. if (std::is_same<float, X_TYPE>()) {
  6968. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6969. // x[i] = 1.0f;
  6970. // x[i] = i + 1;
  6971. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6972. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  6973. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  6974. // x[i] = ggml_fp32_to_fp16(1.0f);
  6975. // x[i] = ggml_fp32_to_fp16(i + 1);
  6976. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  6977. } else {
  6978. GGML_ABORT("fatal error");
  6979. }
  6980. }
  6981. for (size_t i = 0; i < y_ne; i++) {
  6982. if (std::is_same<float, Y_TYPE>()) {
  6983. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6984. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6985. // y[i] = i + 1;
  6986. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6987. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  6988. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  6989. // y[i] = ggml_fp32_to_fp16(i + 1);
  6990. } else {
  6991. GGML_ABORT("fatal error");
  6992. }
  6993. }
  6994. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  6995. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  6996. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  6997. ggml_vk_ctx_begin(ctx->device, subctx);
  6998. for (size_t i = 0; i < num_it; i++) {
  6999. ggml_vk_matmul(
  7000. 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),
  7001. m, n, k,
  7002. k, k, m, k*m, k*n, m*n,
  7003. split_k, batch, batch, batch, 1, 1, n
  7004. );
  7005. }
  7006. ggml_vk_ctx_end(subctx);
  7007. auto begin = std::chrono::high_resolution_clock::now();
  7008. ggml_vk_submit(subctx, ctx->fence);
  7009. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  7010. ctx->device->device.resetFences({ ctx->fence });
  7011. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7012. auto end = std::chrono::high_resolution_clock::now();
  7013. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7014. // copy dst to host
  7015. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  7016. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  7017. ggml_init_params iparams = {
  7018. /*.mem_size =*/ 1024*1024*1024,
  7019. /*.mem_buffer =*/ NULL,
  7020. /*.no_alloc =*/ true,
  7021. };
  7022. ggml_context * ggml_ctx = ggml_init(iparams);
  7023. ggml_type src0_type;
  7024. ggml_type src1_type;
  7025. if (std::is_same<float, X_TYPE>()) {
  7026. src0_type = GGML_TYPE_F32;
  7027. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  7028. src0_type = GGML_TYPE_F16;
  7029. } else {
  7030. GGML_ABORT("fatal error");
  7031. }
  7032. if (std::is_same<float, Y_TYPE>()) {
  7033. src1_type = GGML_TYPE_F32;
  7034. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  7035. src1_type = GGML_TYPE_F16;
  7036. } else {
  7037. GGML_ABORT("fatal error");
  7038. }
  7039. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  7040. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  7041. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7042. src0_ggml->data = x;
  7043. src1_ggml->data = y;
  7044. tensor_ggml->data = d_chk;
  7045. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7046. ggml_build_forward_expand(cgraph, tensor_ggml);
  7047. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7048. ggml_free(ggml_ctx);
  7049. double avg_err = 0.0;
  7050. int first_err_n = -1;
  7051. int first_err_m = -1;
  7052. int first_err_b = -1;
  7053. for (size_t i = 0; i < m*n*batch; i++) {
  7054. double err = std::fabs(d[i] - d_chk[i]);
  7055. avg_err += err;
  7056. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7057. first_err_b = i / (m * n);
  7058. first_err_n = (i % (m * n)) / m;
  7059. first_err_m = (i % (m * n)) % m;
  7060. }
  7061. }
  7062. avg_err /= m * n;
  7063. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7064. 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;
  7065. if (avg_err > 0.1 || std::isnan(avg_err)) {
  7066. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7067. std::cerr << "Actual result: " << std::endl << std::endl;
  7068. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7069. std::cerr << "Expected result: " << std::endl << std::endl;
  7070. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7071. if (split_k > 1) {
  7072. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7073. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7074. std::cerr << "d_buf0: " << std::endl << std::endl;
  7075. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7076. std::cerr << "d_buf1: " << std::endl << std::endl;
  7077. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7078. std::cerr << "d_buf2: " << std::endl << std::endl;
  7079. 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);
  7080. std::cerr << "d_buf3: " << std::endl << std::endl;
  7081. 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);
  7082. free(split_k_buf);
  7083. }
  7084. }
  7085. free(d_chk);
  7086. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  7087. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  7088. ggml_vk_destroy_buffer(d_X);
  7089. ggml_vk_destroy_buffer(d_Y);
  7090. ggml_vk_destroy_buffer(d_D);
  7091. free(x);
  7092. free(y);
  7093. free(d);
  7094. }
  7095. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  7096. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  7097. return;
  7098. }
  7099. i0 = std::max(i0, 5);
  7100. i1 = std::max(i1, 5);
  7101. i2 = std::max(i2, 0);
  7102. i3 = std::max(i3, 0);
  7103. fprintf(stderr, " ");
  7104. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7105. fprintf(stderr, "%7d ", idx1);
  7106. }
  7107. fprintf(stderr, "\n");
  7108. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7109. fprintf(stderr, "%7d: ", idx0);
  7110. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7111. 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]) {
  7112. float val;
  7113. if (tensor->type == GGML_TYPE_F32) {
  7114. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7115. } else if (tensor->type == GGML_TYPE_F16) {
  7116. 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]));
  7117. } else {
  7118. GGML_ABORT("fatal error");
  7119. }
  7120. fprintf(stderr, "% 7.2f ", val);
  7121. } else {
  7122. fprintf(stderr, " ");
  7123. }
  7124. }
  7125. fprintf(stderr, "\n");
  7126. }
  7127. }
  7128. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  7129. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  7130. }
  7131. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  7132. if (quant == GGML_TYPE_F32) {
  7133. memcpy(to, from, sizeof(float) * ne);
  7134. return;
  7135. }
  7136. const auto * tt = ggml_get_type_traits(quant);
  7137. ggml_to_float_t dequant_fn = tt->to_float;
  7138. dequant_fn(from, to, ne);
  7139. }
  7140. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7141. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  7142. const size_t x_sz = sizeof(float) * ne;
  7143. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  7144. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7145. float * x = (float *) malloc(x_sz);
  7146. void * qx = malloc(qx_sz);
  7147. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7148. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7149. float * x_ref = (float *) malloc(x_sz);
  7150. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  7151. for (size_t i = 0; i < ne; i++) {
  7152. x[i] = rand() / (float)RAND_MAX;
  7153. }
  7154. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  7155. ggml_vk_quantize_data(x, qx, ne, quant);
  7156. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  7157. ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7158. if (ctx->device->need_compiles) {
  7159. ggml_vk_load_shaders(ctx->device);
  7160. }
  7161. ggml_pipeline_allocate_descriptor_sets(ctx);
  7162. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7163. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7164. ggml_vk_ctx_begin(ctx->device, subctx);
  7165. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  7166. 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});
  7167. ggml_vk_ctx_end(subctx);
  7168. auto begin = std::chrono::high_resolution_clock::now();
  7169. ggml_vk_submit(subctx, ctx->fence);
  7170. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7171. ctx->device->device.resetFences({ ctx->fence });
  7172. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7173. auto end = std::chrono::high_resolution_clock::now();
  7174. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7175. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  7176. int first_err = -1;
  7177. double avg_err = 0.0;
  7178. for (size_t i = 0; i < ne; i++) {
  7179. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  7180. avg_err += error;
  7181. if (first_err < 0 && error > 0.05) {
  7182. first_err = i;
  7183. }
  7184. }
  7185. avg_err /= ne;
  7186. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  7187. if (avg_err > 0.1) {
  7188. std::cerr << "first_error = " << first_err << std::endl;
  7189. std::cerr << "Actual result: " << std::endl << std::endl;
  7190. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7191. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  7192. }
  7193. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  7194. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  7195. std::cerr << x_ref[i] << ", ";
  7196. }
  7197. std::cerr << std::endl;
  7198. }
  7199. ggml_vk_destroy_buffer(x_buf);
  7200. ggml_vk_destroy_buffer(qx_buf);
  7201. free(x);
  7202. free(qx);
  7203. free(x_ref);
  7204. free(x_chk);
  7205. }
  7206. // This does not work without ggml q8_1 quantization support
  7207. //
  7208. // typedef uint16_t ggml_half;
  7209. // typedef uint32_t ggml_half2;
  7210. //
  7211. // #define QK8_1 32
  7212. // typedef struct {
  7213. // union {
  7214. // struct {
  7215. // ggml_half d; // delta
  7216. // ggml_half s; // d * sum(qs[i])
  7217. // } GGML_COMMON_AGGR_S;
  7218. // ggml_half2 ds;
  7219. // } GGML_COMMON_AGGR_U;
  7220. // int8_t qs[QK8_1]; // quants
  7221. // } block_q8_1;
  7222. //
  7223. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  7224. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  7225. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  7226. //
  7227. // const size_t x_sz = sizeof(float) * ne;
  7228. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7229. // float * x = (float *) malloc(x_sz);
  7230. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  7231. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  7232. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7233. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7234. //
  7235. // for (size_t i = 0; i < ne; i++) {
  7236. // x[i] = rand() / (float)RAND_MAX;
  7237. // }
  7238. //
  7239. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  7240. //
  7241. // ggml_pipeline_request_descriptor_sets(ctx, p, 1);
  7242. //
  7243. // if (ctx->device->need_compiles) {
  7244. // ggml_vk_load_shaders(ctx->device);
  7245. // }
  7246. //
  7247. // ggml_pipeline_allocate_descriptor_sets(ctx);
  7248. //
  7249. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  7250. //
  7251. // vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7252. // ggml_vk_ctx_begin(ctx->device, subctx);
  7253. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  7254. // ggml_vk_ctx_end(subctx);
  7255. //
  7256. // auto begin = std::chrono::high_resolution_clock::now();
  7257. //
  7258. // ggml_vk_submit(subctx, ctx->fence);
  7259. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  7260. // ctx->device->device.resetFences({ ctx->fence });
  7261. // ggml_vk_queue_command_pools_cleanup(ctx->device);
  7262. //
  7263. // auto end = std::chrono::high_resolution_clock::now();
  7264. //
  7265. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7266. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  7267. //
  7268. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  7269. //
  7270. // int first_err = -1;
  7271. //
  7272. // for (size_t i = 0; i < ne / 32; i++) {
  7273. // 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));
  7274. //
  7275. // if (first_err < 0 && error > 0.1) {
  7276. // first_err = i;
  7277. // }
  7278. //
  7279. // 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));
  7280. //
  7281. // if (first_err < 0 && error > 0.1) {
  7282. // first_err = i;
  7283. // }
  7284. //
  7285. // for (size_t j = 0; j < 32; j++) {
  7286. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  7287. //
  7288. // if (first_err < 0 && error > 1) {
  7289. // first_err = i;
  7290. // }
  7291. // }
  7292. // }
  7293. //
  7294. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  7295. //
  7296. // if (first_err != -1) {
  7297. // std::cerr << "first_error = " << first_err << std::endl;
  7298. // std::cerr << "Actual result: " << std::endl << std::endl;
  7299. // 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) << " ";
  7300. // for (size_t j = 0; j < 32; j++) {
  7301. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  7302. // }
  7303. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  7304. // 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) << " ";
  7305. // for (size_t j = 0; j < 32; j++) {
  7306. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  7307. // }
  7308. // std::cerr << std::endl;
  7309. // }
  7310. //
  7311. // ggml_vk_destroy_buffer(x_buf);
  7312. // ggml_vk_destroy_buffer(qx_buf);
  7313. //
  7314. // free(x);
  7315. // free(qx);
  7316. // free(qx_res);
  7317. // }
  7318. 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) {
  7319. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  7320. const size_t x_ne = m * k * batch;
  7321. const size_t y_ne = k * n * batch;
  7322. const size_t d_ne = m * n * batch;
  7323. vk_matmul_pipeline2 * pipelines;
  7324. if (mmq) {
  7325. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  7326. } else {
  7327. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  7328. }
  7329. const bool fp16acc = ctx->device->fp16;
  7330. vk_pipeline p;
  7331. std::string shname;
  7332. if (shader_size == 0) {
  7333. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  7334. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  7335. } else if (shader_size == 1) {
  7336. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  7337. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  7338. } else if (shader_size == 2) {
  7339. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  7340. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  7341. } else {
  7342. GGML_ASSERT(0);
  7343. }
  7344. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  7345. if (mmq || k != kpad) {
  7346. if (shader_size == 0) {
  7347. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  7348. shname = std::string(ggml_type_name(quant)) + "_S";
  7349. } else if (shader_size == 1) {
  7350. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  7351. shname = std::string(ggml_type_name(quant)) + "_M";
  7352. } else if (shader_size == 2) {
  7353. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  7354. shname = std::string(ggml_type_name(quant)) + "_L";
  7355. } else {
  7356. GGML_ASSERT(0);
  7357. }
  7358. }
  7359. if (p == nullptr) {
  7360. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  7361. return;
  7362. }
  7363. const size_t x_sz = sizeof(float) * x_ne;
  7364. const size_t y_sz = sizeof(float) * y_ne;
  7365. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  7366. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  7367. const size_t d_sz = sizeof(float) * d_ne;
  7368. float * x = (float *) malloc(x_sz);
  7369. float * y = (float *) malloc(y_sz);
  7370. void * qx = malloc(qx_sz);
  7371. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7372. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7373. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7374. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7375. float * d = (float *) malloc(d_sz);
  7376. float * d_chk = (float *) malloc(d_sz);
  7377. for (size_t i = 0; i < x_ne; i++) {
  7378. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7379. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7380. // x[i] = i % k;
  7381. }
  7382. ggml_vk_quantize_data(x, qx, x_ne, quant);
  7383. for (size_t i = 0; i < y_ne; i++) {
  7384. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  7385. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  7386. // y[i] = i % k;
  7387. }
  7388. ggml_pipeline_request_descriptor_sets(ctx, p, num_it);
  7389. if (split_k > 1) {
  7390. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  7391. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  7392. // Resize buffer
  7393. if (ctx->prealloc_split_k != nullptr) {
  7394. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7395. }
  7396. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  7397. }
  7398. }
  7399. if (mmq) {
  7400. ggml_pipeline_request_descriptor_sets(ctx, ctx->device->pipeline_quantize_q8_1, num_it);
  7401. }
  7402. if (ctx->device->need_compiles) {
  7403. ggml_vk_load_shaders(ctx->device);
  7404. }
  7405. ggml_pipeline_allocate_descriptor_sets(ctx);
  7406. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  7407. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  7408. vk_context subctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7409. ggml_vk_ctx_begin(ctx->device, subctx);
  7410. if (mmq) {
  7411. for (size_t i = 0; i < num_it; i++) {
  7412. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  7413. ggml_vk_matmul(
  7414. 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 },
  7415. m, n, k,
  7416. k, k, m, k*m, k*n, m*n,
  7417. split_k, batch, batch, batch, 1, 1, n
  7418. );
  7419. }
  7420. } else {
  7421. for (size_t i = 0; i < num_it; i++) {
  7422. ggml_vk_matmul(
  7423. 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 },
  7424. m, n, k,
  7425. k, k, m, k*m, k*n, m*n,
  7426. split_k, batch, batch, batch, 1, 1, n
  7427. );
  7428. }
  7429. }
  7430. ggml_vk_ctx_end(subctx);
  7431. auto begin = std::chrono::high_resolution_clock::now();
  7432. ggml_vk_submit(subctx, ctx->fence);
  7433. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  7434. ctx->device->device.resetFences({ ctx->fence });
  7435. ggml_vk_queue_command_pools_cleanup(ctx->device);
  7436. auto end = std::chrono::high_resolution_clock::now();
  7437. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  7438. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  7439. ggml_init_params iparams = {
  7440. /*.mem_size =*/ 1024*1024*1024,
  7441. /*.mem_buffer =*/ NULL,
  7442. /*.no_alloc =*/ true,
  7443. };
  7444. ggml_context * ggml_ctx = ggml_init(iparams);
  7445. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  7446. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  7447. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  7448. src0_ggml->data = qx;
  7449. src1_ggml->data = y;
  7450. tensor_ggml->data = d_chk;
  7451. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  7452. ggml_build_forward_expand(cgraph, tensor_ggml);
  7453. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  7454. ggml_free(ggml_ctx);
  7455. double avg_err = 0.0;
  7456. int first_err_n = -1;
  7457. int first_err_m = -1;
  7458. int first_err_b = -1;
  7459. for (size_t i = 0; i < m*n*batch; i++) {
  7460. double err = std::fabs(d[i] - d_chk[i]);
  7461. avg_err += err;
  7462. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  7463. first_err_b = i / (m * n);
  7464. first_err_n = (i % (m * n)) / m;
  7465. first_err_m = (i % (m * n)) % m;
  7466. }
  7467. }
  7468. avg_err /= m * n;
  7469. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  7470. std::cerr << "TEST dequant matmul " << shname;
  7471. if (mmq) {
  7472. std::cerr << " mmq";
  7473. }
  7474. 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;
  7475. if (avg_err > 0.01 || std::isnan(avg_err)) {
  7476. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  7477. std::cerr << "Actual result: " << std::endl << std::endl;
  7478. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7479. std::cerr << std::endl;
  7480. std::cerr << "Expected result: " << std::endl << std::endl;
  7481. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7482. std::cerr << "src0: " << std::endl << std::endl;
  7483. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  7484. std::cerr << std::endl;
  7485. std::cerr << "src1: " << std::endl << std::endl;
  7486. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  7487. if (split_k > 1) {
  7488. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  7489. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  7490. std::cerr << "d_buf0: " << std::endl << std::endl;
  7491. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7492. std::cerr << "d_buf1: " << std::endl << std::endl;
  7493. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  7494. std::cerr << "d_buf2: " << std::endl << std::endl;
  7495. 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);
  7496. std::cerr << "d_buf3: " << std::endl << std::endl;
  7497. 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);
  7498. free(split_k_buf);
  7499. }
  7500. }
  7501. ggml_vk_destroy_buffer(qx_buf);
  7502. ggml_vk_destroy_buffer(y_buf);
  7503. ggml_vk_destroy_buffer(qy_buf);
  7504. ggml_vk_destroy_buffer(d_buf);
  7505. free(x);
  7506. free(qx);
  7507. free(y);
  7508. free(d);
  7509. free(d_chk);
  7510. }
  7511. #endif
  7512. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  7513. #if defined(GGML_VULKAN_RUN_TESTS)
  7514. const std::vector<size_t> vals {
  7515. 512, 512, 128,
  7516. 128, 512, 512,
  7517. 4096, 512, 4096,
  7518. 11008, 512, 4096,
  7519. 4096, 512, 11008,
  7520. 32000, 512, 4096,
  7521. 8, 8, 8,
  7522. 100, 46, 576,
  7523. 623, 111, 128,
  7524. 100, 46, 558,
  7525. 512, 1, 256,
  7526. 128, 110, 622,
  7527. 511, 511, 127,
  7528. 511, 511, 7,
  7529. 511, 511, 17,
  7530. 49, 49, 128,
  7531. 128, 49, 49,
  7532. 4096, 49, 4096,
  7533. };
  7534. const size_t num_it = 100;
  7535. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7536. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7537. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7538. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  7539. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  7540. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  7541. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  7542. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  7543. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  7544. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  7545. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  7546. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  7547. abort();
  7548. for (size_t i = 0; i < vals.size(); i += 3) {
  7549. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  7550. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  7551. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  7552. std::cerr << '\n';
  7553. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  7554. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  7555. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  7556. std::cerr << '\n';
  7557. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  7558. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  7559. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  7560. std::cerr << '\n' << std::endl;
  7561. if (vals[i + 2] % 32 == 0) {
  7562. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  7563. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  7564. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  7565. std::cerr << '\n';
  7566. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  7567. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  7568. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  7569. std::cerr << '\n';
  7570. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  7571. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  7572. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  7573. std::cerr << '\n' << std::endl;
  7574. }
  7575. if (vals[i + 2] % 256 == 0) {
  7576. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  7577. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  7578. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  7579. std::cerr << '\n';
  7580. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  7581. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  7582. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  7583. std::cerr << '\n';
  7584. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  7585. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  7586. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  7587. std::cerr << '\n' << std::endl;
  7588. }
  7589. }
  7590. GGML_ABORT("fatal error");
  7591. #endif
  7592. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  7593. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  7594. // Resize buffer
  7595. if (ctx->prealloc_x != nullptr) {
  7596. ggml_vk_destroy_buffer(ctx->prealloc_x);
  7597. }
  7598. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  7599. }
  7600. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  7601. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  7602. // Resize buffer
  7603. if (ctx->prealloc_y != nullptr) {
  7604. ggml_vk_destroy_buffer(ctx->prealloc_y);
  7605. }
  7606. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  7607. }
  7608. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  7609. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  7610. // Resize buffer
  7611. if (ctx->prealloc_split_k != nullptr) {
  7612. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7613. }
  7614. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  7615. }
  7616. }
  7617. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* tensor, int tensor_idx, bool use_fence, bool almost_ready);
  7618. // Returns true if node has enqueued work into the queue, false otherwise
  7619. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  7620. 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){
  7621. ggml_tensor * node = cgraph->nodes[node_idx];
  7622. if (ggml_is_empty(node) || !node->buffer) {
  7623. return false;
  7624. }
  7625. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  7626. ctx->semaphore_idx = 0;
  7627. const ggml_tensor * src0 = node->src[0];
  7628. const ggml_tensor * src1 = node->src[1];
  7629. const ggml_tensor * src2 = node->src[2];
  7630. const ggml_tensor * src3 = node->src[3];
  7631. switch (node->op) {
  7632. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  7633. case GGML_OP_RESHAPE:
  7634. case GGML_OP_VIEW:
  7635. case GGML_OP_PERMUTE:
  7636. case GGML_OP_TRANSPOSE:
  7637. case GGML_OP_NONE:
  7638. return false;
  7639. case GGML_OP_UNARY:
  7640. switch (ggml_get_unary_op(node)) {
  7641. case GGML_UNARY_OP_SILU:
  7642. case GGML_UNARY_OP_GELU:
  7643. case GGML_UNARY_OP_GELU_ERF:
  7644. case GGML_UNARY_OP_GELU_QUICK:
  7645. case GGML_UNARY_OP_RELU:
  7646. case GGML_UNARY_OP_TANH:
  7647. case GGML_UNARY_OP_SIGMOID:
  7648. break;
  7649. default:
  7650. return false;
  7651. }
  7652. break;
  7653. case GGML_OP_GLU:
  7654. switch (ggml_get_glu_op(node)) {
  7655. case GGML_GLU_OP_GEGLU:
  7656. case GGML_GLU_OP_REGLU:
  7657. case GGML_GLU_OP_SWIGLU:
  7658. case GGML_GLU_OP_GEGLU_ERF:
  7659. case GGML_GLU_OP_GEGLU_QUICK:
  7660. break;
  7661. default:
  7662. return false;
  7663. }
  7664. break;
  7665. case GGML_OP_REPEAT:
  7666. case GGML_OP_REPEAT_BACK:
  7667. case GGML_OP_GET_ROWS:
  7668. case GGML_OP_ADD:
  7669. case GGML_OP_ACC:
  7670. case GGML_OP_SUB:
  7671. case GGML_OP_MUL:
  7672. case GGML_OP_DIV:
  7673. case GGML_OP_CONCAT:
  7674. case GGML_OP_UPSCALE:
  7675. case GGML_OP_SCALE:
  7676. case GGML_OP_SQR:
  7677. case GGML_OP_SIN:
  7678. case GGML_OP_COS:
  7679. case GGML_OP_CLAMP:
  7680. case GGML_OP_PAD:
  7681. case GGML_OP_CPY:
  7682. case GGML_OP_CONT:
  7683. case GGML_OP_DUP:
  7684. case GGML_OP_SILU_BACK:
  7685. case GGML_OP_NORM:
  7686. case GGML_OP_GROUP_NORM:
  7687. case GGML_OP_RMS_NORM:
  7688. case GGML_OP_RMS_NORM_BACK:
  7689. case GGML_OP_L2_NORM:
  7690. case GGML_OP_DIAG_MASK_INF:
  7691. case GGML_OP_SOFT_MAX:
  7692. case GGML_OP_SOFT_MAX_BACK:
  7693. case GGML_OP_ROPE:
  7694. case GGML_OP_ROPE_BACK:
  7695. case GGML_OP_MUL_MAT:
  7696. case GGML_OP_MUL_MAT_ID:
  7697. case GGML_OP_ARGSORT:
  7698. case GGML_OP_SUM:
  7699. case GGML_OP_SUM_ROWS:
  7700. case GGML_OP_ARGMAX:
  7701. case GGML_OP_COUNT_EQUAL:
  7702. case GGML_OP_IM2COL:
  7703. case GGML_OP_TIMESTEP_EMBEDDING:
  7704. case GGML_OP_CONV_TRANSPOSE_1D:
  7705. case GGML_OP_POOL_2D:
  7706. case GGML_OP_CONV_2D_DW:
  7707. case GGML_OP_RWKV_WKV6:
  7708. case GGML_OP_RWKV_WKV7:
  7709. case GGML_OP_LEAKY_RELU:
  7710. case GGML_OP_FLASH_ATTN_EXT:
  7711. case GGML_OP_OPT_STEP_ADAMW:
  7712. break;
  7713. default:
  7714. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  7715. GGML_ABORT("fatal error");
  7716. return false;
  7717. }
  7718. vk_context compute_ctx;
  7719. if (!dryrun) {
  7720. if (ctx->compute_ctx.expired()) {
  7721. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  7722. ctx->compute_ctx = compute_ctx;
  7723. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  7724. } else {
  7725. compute_ctx = ctx->compute_ctx.lock();
  7726. }
  7727. } else {
  7728. switch (node->op) {
  7729. case GGML_OP_REPEAT:
  7730. case GGML_OP_REPEAT_BACK:
  7731. case GGML_OP_ACC:
  7732. case GGML_OP_GET_ROWS:
  7733. case GGML_OP_ADD:
  7734. case GGML_OP_SUB:
  7735. case GGML_OP_MUL:
  7736. case GGML_OP_DIV:
  7737. case GGML_OP_CONCAT:
  7738. case GGML_OP_UPSCALE:
  7739. case GGML_OP_SCALE:
  7740. case GGML_OP_SQR:
  7741. case GGML_OP_SIN:
  7742. case GGML_OP_COS:
  7743. case GGML_OP_CLAMP:
  7744. case GGML_OP_PAD:
  7745. case GGML_OP_CPY:
  7746. case GGML_OP_CONT:
  7747. case GGML_OP_DUP:
  7748. case GGML_OP_SILU_BACK:
  7749. case GGML_OP_NORM:
  7750. case GGML_OP_GROUP_NORM:
  7751. case GGML_OP_RMS_NORM:
  7752. case GGML_OP_RMS_NORM_BACK:
  7753. case GGML_OP_L2_NORM:
  7754. case GGML_OP_UNARY:
  7755. case GGML_OP_GLU:
  7756. case GGML_OP_DIAG_MASK_INF:
  7757. case GGML_OP_SOFT_MAX:
  7758. case GGML_OP_SOFT_MAX_BACK:
  7759. case GGML_OP_ROPE:
  7760. case GGML_OP_ROPE_BACK:
  7761. case GGML_OP_ARGSORT:
  7762. case GGML_OP_SUM:
  7763. case GGML_OP_SUM_ROWS:
  7764. case GGML_OP_ARGMAX:
  7765. case GGML_OP_COUNT_EQUAL:
  7766. case GGML_OP_IM2COL:
  7767. case GGML_OP_TIMESTEP_EMBEDDING:
  7768. case GGML_OP_CONV_TRANSPOSE_1D:
  7769. case GGML_OP_POOL_2D:
  7770. case GGML_OP_CONV_2D_DW:
  7771. case GGML_OP_LEAKY_RELU:
  7772. {
  7773. // These operations all go through ggml_vk_op_f32, so short-circuit and
  7774. // do the only thing needed for the dryrun.
  7775. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  7776. ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
  7777. return false;
  7778. }
  7779. default:
  7780. break;
  7781. }
  7782. }
  7783. switch (node->op) {
  7784. case GGML_OP_REPEAT:
  7785. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  7786. break;
  7787. case GGML_OP_REPEAT_BACK:
  7788. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  7789. break;
  7790. case GGML_OP_ACC:
  7791. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  7792. break;
  7793. case GGML_OP_GET_ROWS:
  7794. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  7795. break;
  7796. case GGML_OP_ADD:
  7797. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  7798. break;
  7799. case GGML_OP_SUB:
  7800. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  7801. break;
  7802. case GGML_OP_MUL:
  7803. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  7804. break;
  7805. case GGML_OP_DIV:
  7806. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  7807. break;
  7808. case GGML_OP_CONCAT:
  7809. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  7810. break;
  7811. case GGML_OP_UPSCALE:
  7812. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  7813. break;
  7814. case GGML_OP_SCALE:
  7815. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  7816. break;
  7817. case GGML_OP_SQR:
  7818. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  7819. break;
  7820. case GGML_OP_SIN:
  7821. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  7822. break;
  7823. case GGML_OP_COS:
  7824. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  7825. break;
  7826. case GGML_OP_CLAMP:
  7827. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  7828. break;
  7829. case GGML_OP_PAD:
  7830. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  7831. break;
  7832. case GGML_OP_CPY:
  7833. case GGML_OP_CONT:
  7834. case GGML_OP_DUP:
  7835. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  7836. break;
  7837. case GGML_OP_SILU_BACK:
  7838. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7839. break;
  7840. case GGML_OP_NORM:
  7841. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  7842. break;
  7843. case GGML_OP_GROUP_NORM:
  7844. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  7845. break;
  7846. case GGML_OP_RMS_NORM:
  7847. if (ctx->num_additional_fused_ops > 0) {
  7848. // fused rms_norm + mul
  7849. ggml_tensor *mul = cgraph->nodes[node_idx + 1];
  7850. ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
  7851. ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, dryrun);
  7852. } else {
  7853. ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, dryrun);
  7854. }
  7855. break;
  7856. case GGML_OP_RMS_NORM_BACK:
  7857. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7858. break;
  7859. case GGML_OP_L2_NORM:
  7860. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  7861. break;
  7862. case GGML_OP_UNARY:
  7863. switch (ggml_get_unary_op(node)) {
  7864. case GGML_UNARY_OP_SILU:
  7865. case GGML_UNARY_OP_GELU:
  7866. case GGML_UNARY_OP_GELU_ERF:
  7867. case GGML_UNARY_OP_GELU_QUICK:
  7868. case GGML_UNARY_OP_RELU:
  7869. case GGML_UNARY_OP_TANH:
  7870. case GGML_UNARY_OP_SIGMOID:
  7871. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  7872. break;
  7873. default:
  7874. return false;
  7875. }
  7876. break;
  7877. case GGML_OP_GLU:
  7878. switch (ggml_get_glu_op(node)) {
  7879. case GGML_GLU_OP_GEGLU:
  7880. case GGML_GLU_OP_REGLU:
  7881. case GGML_GLU_OP_SWIGLU:
  7882. case GGML_GLU_OP_GEGLU_ERF:
  7883. case GGML_GLU_OP_GEGLU_QUICK:
  7884. ggml_vk_glu(ctx, compute_ctx, src0, src1, node, dryrun);
  7885. break;
  7886. default:
  7887. return false;
  7888. }
  7889. break;
  7890. case GGML_OP_DIAG_MASK_INF:
  7891. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  7892. break;
  7893. case GGML_OP_SOFT_MAX:
  7894. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  7895. break;
  7896. case GGML_OP_SOFT_MAX_BACK:
  7897. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7898. break;
  7899. case GGML_OP_ROPE:
  7900. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  7901. break;
  7902. case GGML_OP_ROPE_BACK:
  7903. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  7904. break;
  7905. case GGML_OP_ARGSORT:
  7906. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  7907. break;
  7908. case GGML_OP_SUM:
  7909. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  7910. break;
  7911. case GGML_OP_SUM_ROWS:
  7912. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  7913. break;
  7914. case GGML_OP_ARGMAX:
  7915. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  7916. break;
  7917. case GGML_OP_COUNT_EQUAL:
  7918. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  7919. break;
  7920. case GGML_OP_IM2COL:
  7921. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  7922. break;
  7923. case GGML_OP_TIMESTEP_EMBEDDING:
  7924. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  7925. break;
  7926. case GGML_OP_CONV_TRANSPOSE_1D:
  7927. ggml_vk_conv_transpose_1d(ctx, compute_ctx, src0, src1, node, dryrun);
  7928. break;
  7929. case GGML_OP_POOL_2D:
  7930. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  7931. break;
  7932. case GGML_OP_CONV_2D_DW:
  7933. ggml_vk_conv_2d_dw(ctx, compute_ctx, src0, src1, node, dryrun);
  7934. break;
  7935. case GGML_OP_LEAKY_RELU:
  7936. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  7937. break;
  7938. case GGML_OP_MUL_MAT:
  7939. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  7940. break;
  7941. case GGML_OP_MUL_MAT_ID:
  7942. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  7943. break;
  7944. case GGML_OP_FLASH_ATTN_EXT:
  7945. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  7946. break;
  7947. case GGML_OP_RWKV_WKV6:
  7948. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  7949. break;
  7950. case GGML_OP_RWKV_WKV7:
  7951. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  7952. break;
  7953. case GGML_OP_OPT_STEP_ADAMW:
  7954. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  7955. break;
  7956. default:
  7957. return false;
  7958. }
  7959. if (dryrun) {
  7960. return false;
  7961. }
  7962. ctx->tensor_ctxs[node_idx] = compute_ctx;
  7963. #if defined(GGML_VULKAN_CHECK_RESULTS)
  7964. // Force context reset on each node so that each tensor ends up in its own context
  7965. // and can be run and compared to its CPU equivalent separately
  7966. last_node = true;
  7967. #endif
  7968. if (submit || last_node) {
  7969. ggml_vk_ctx_end(compute_ctx);
  7970. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  7971. if (last_node) {
  7972. compute_ctx->exit_tensor_idx = node_idx_begin;
  7973. }
  7974. else {
  7975. compute_ctx->exit_tensor_idx = -1;
  7976. }
  7977. ctx->compute_ctx.reset();
  7978. bool ok = ggml_vk_compute_forward(ctx, node_begin, node_idx_begin, false, almost_ready);
  7979. if (!ok) {
  7980. if (node->op == GGML_OP_UNARY) {
  7981. 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;
  7982. } else if (node->op == GGML_OP_GLU) {
  7983. 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;
  7984. } else {
  7985. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  7986. }
  7987. }
  7988. }
  7989. return true;
  7990. }
  7991. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_tensor * tensor, int tensor_idx, bool use_fence = true, bool almost_ready = false) {
  7992. ggml_backend_buffer * buf = nullptr;
  7993. switch (tensor->op) {
  7994. case GGML_OP_ADD:
  7995. case GGML_OP_ACC:
  7996. case GGML_OP_GET_ROWS:
  7997. case GGML_OP_SUB:
  7998. case GGML_OP_MUL:
  7999. case GGML_OP_DIV:
  8000. case GGML_OP_CONCAT:
  8001. case GGML_OP_UPSCALE:
  8002. case GGML_OP_SCALE:
  8003. case GGML_OP_SQR:
  8004. case GGML_OP_SIN:
  8005. case GGML_OP_COS:
  8006. case GGML_OP_CLAMP:
  8007. case GGML_OP_PAD:
  8008. case GGML_OP_CPY:
  8009. case GGML_OP_CONT:
  8010. case GGML_OP_DUP:
  8011. case GGML_OP_SILU_BACK:
  8012. case GGML_OP_NORM:
  8013. case GGML_OP_GROUP_NORM:
  8014. case GGML_OP_RMS_NORM:
  8015. case GGML_OP_RMS_NORM_BACK:
  8016. case GGML_OP_L2_NORM:
  8017. case GGML_OP_DIAG_MASK_INF:
  8018. case GGML_OP_SOFT_MAX:
  8019. case GGML_OP_SOFT_MAX_BACK:
  8020. case GGML_OP_ROPE:
  8021. case GGML_OP_ROPE_BACK:
  8022. case GGML_OP_RESHAPE:
  8023. case GGML_OP_VIEW:
  8024. case GGML_OP_PERMUTE:
  8025. case GGML_OP_TRANSPOSE:
  8026. case GGML_OP_NONE:
  8027. case GGML_OP_ARGSORT:
  8028. case GGML_OP_SUM:
  8029. case GGML_OP_SUM_ROWS:
  8030. case GGML_OP_ARGMAX:
  8031. case GGML_OP_COUNT_EQUAL:
  8032. case GGML_OP_IM2COL:
  8033. case GGML_OP_TIMESTEP_EMBEDDING:
  8034. case GGML_OP_CONV_TRANSPOSE_1D:
  8035. case GGML_OP_POOL_2D:
  8036. case GGML_OP_CONV_2D_DW:
  8037. case GGML_OP_RWKV_WKV6:
  8038. case GGML_OP_RWKV_WKV7:
  8039. case GGML_OP_LEAKY_RELU:
  8040. case GGML_OP_REPEAT:
  8041. case GGML_OP_REPEAT_BACK:
  8042. case GGML_OP_OPT_STEP_ADAMW:
  8043. buf = tensor->buffer;
  8044. break;
  8045. case GGML_OP_UNARY:
  8046. switch (ggml_get_unary_op(tensor)) {
  8047. case GGML_UNARY_OP_SILU:
  8048. case GGML_UNARY_OP_GELU:
  8049. case GGML_UNARY_OP_GELU_ERF:
  8050. case GGML_UNARY_OP_GELU_QUICK:
  8051. case GGML_UNARY_OP_RELU:
  8052. case GGML_UNARY_OP_TANH:
  8053. case GGML_UNARY_OP_SIGMOID:
  8054. buf = tensor->buffer;
  8055. break;
  8056. default:
  8057. return false;
  8058. }
  8059. break;
  8060. case GGML_OP_GLU:
  8061. switch (ggml_get_glu_op(tensor)) {
  8062. case GGML_GLU_OP_GEGLU:
  8063. case GGML_GLU_OP_REGLU:
  8064. case GGML_GLU_OP_SWIGLU:
  8065. case GGML_GLU_OP_GEGLU_ERF:
  8066. case GGML_GLU_OP_GEGLU_QUICK:
  8067. buf = tensor->buffer;
  8068. break;
  8069. default:
  8070. return false;
  8071. }
  8072. break;
  8073. case GGML_OP_MUL_MAT:
  8074. case GGML_OP_MUL_MAT_ID:
  8075. case GGML_OP_FLASH_ATTN_EXT:
  8076. buf = tensor->buffer;
  8077. break;
  8078. default:
  8079. return false;
  8080. }
  8081. if (buf == nullptr) {
  8082. return false;
  8083. }
  8084. 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 << ")");
  8085. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  8086. // always wait for the GPU work to be done for the last submit
  8087. if (tensor_idx == subctx->exit_tensor_idx) {
  8088. use_fence = true;
  8089. }
  8090. // Only run if ctx hasn't been submitted yet
  8091. if (!subctx->seqs.empty()) {
  8092. #ifdef GGML_VULKAN_CHECK_RESULTS
  8093. ggml_vk_check_results_0(tensor);
  8094. use_fence = true;
  8095. #endif
  8096. // Do staging buffer copies
  8097. for (auto& cpy : subctx->in_memcpys) {
  8098. memcpy(cpy.dst, cpy.src, cpy.n);
  8099. }
  8100. if (almost_ready && !ctx->almost_ready_fence_pending && !use_fence) {
  8101. ggml_vk_submit(subctx, ctx->almost_ready_fence);
  8102. ctx->almost_ready_fence_pending = true;
  8103. } else {
  8104. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  8105. }
  8106. if (use_fence) {
  8107. ggml_vk_wait_for_fence(ctx);
  8108. }
  8109. #ifdef GGML_VULKAN_CHECK_RESULTS
  8110. ggml_vk_check_results_1(tensor);
  8111. #endif
  8112. }
  8113. if (tensor_idx == subctx->exit_tensor_idx) {
  8114. // Do staging buffer copies
  8115. for (auto& cpy : subctx->out_memcpys) {
  8116. memcpy(cpy.dst, cpy.src, cpy.n);
  8117. }
  8118. subctx->in_memcpys.clear();
  8119. subctx->out_memcpys.clear();
  8120. }
  8121. return true;
  8122. }
  8123. // Clean up after graph processing is done
  8124. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  8125. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  8126. for (auto& buffer : ctx->gc.temp_buffers) {
  8127. ggml_vk_pool_free(ctx, buffer);
  8128. }
  8129. ctx->gc.temp_buffers.clear();
  8130. ggml_vk_command_pool_cleanup(ctx->device, ctx->compute_cmd_pool);
  8131. ggml_vk_command_pool_cleanup(ctx->device, ctx->transfer_cmd_pool);
  8132. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  8133. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  8134. }
  8135. ctx->gc.semaphores.clear();
  8136. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  8137. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  8138. }
  8139. ctx->gc.tl_semaphores.clear();
  8140. ctx->semaphore_idx = 0;
  8141. ctx->event_idx = 0;
  8142. for (auto& event : ctx->gc.events) {
  8143. ctx->device->device.resetEvent(event);
  8144. }
  8145. ctx->tensor_ctxs.clear();
  8146. ctx->gc.contexts.clear();
  8147. ctx->pipeline_descriptor_set_requirements = 0;
  8148. ctx->descriptor_set_idx = 0;
  8149. }
  8150. // Clean up on backend free
  8151. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  8152. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  8153. ggml_vk_graph_cleanup(ctx);
  8154. ggml_vk_destroy_buffer(ctx->prealloc_x);
  8155. ggml_vk_destroy_buffer(ctx->prealloc_y);
  8156. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  8157. for (auto& buffer : ctx->buffer_pool) {
  8158. ggml_vk_destroy_buffer(buffer);
  8159. }
  8160. ctx->prealloc_size_x = 0;
  8161. ctx->prealloc_size_y = 0;
  8162. ctx->prealloc_size_split_k = 0;
  8163. for (auto& event : ctx->gc.events) {
  8164. ctx->device->device.destroyEvent(event);
  8165. }
  8166. ctx->gc.events.clear();
  8167. ctx->device->device.destroyFence(ctx->fence);
  8168. ctx->device->device.destroyFence(ctx->almost_ready_fence);
  8169. for (auto& pool : ctx->descriptor_pools) {
  8170. ctx->device->device.destroyDescriptorPool(pool);
  8171. }
  8172. ctx->descriptor_pools.clear();
  8173. ctx->descriptor_sets.clear();
  8174. ctx->compute_cmd_pool.destroy(ctx->device->device);
  8175. ctx->transfer_cmd_pool.destroy(ctx->device->device);
  8176. }
  8177. static int ggml_vk_get_device_count() {
  8178. ggml_vk_instance_init();
  8179. return vk_instance.device_indices.size();
  8180. }
  8181. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  8182. ggml_vk_instance_init();
  8183. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  8184. vk::PhysicalDeviceProperties props;
  8185. devices[device].getProperties(&props);
  8186. snprintf(description, description_size, "%s", props.deviceName.data());
  8187. }
  8188. // backend interface
  8189. #define UNUSED GGML_UNUSED
  8190. // device backend
  8191. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  8192. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  8193. }
  8194. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8195. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  8196. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8197. ggml_vk_destroy_buffer(ctx->dev_buffer);
  8198. delete ctx;
  8199. }
  8200. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  8201. return vk_ptr_base;
  8202. UNUSED(buffer);
  8203. }
  8204. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8205. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  8206. if (tensor->view_src != nullptr) {
  8207. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  8208. }
  8209. return GGML_STATUS_SUCCESS;
  8210. }
  8211. 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) {
  8212. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  8213. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8214. vk_buffer buf = buf_ctx->dev_buffer;
  8215. uint32_t val32 = (uint32_t)value * 0x01010101;
  8216. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  8217. }
  8218. 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) {
  8219. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8220. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8221. vk_buffer buf = buf_ctx->dev_buffer;
  8222. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8223. }
  8224. 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) {
  8225. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  8226. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8227. vk_buffer buf = buf_ctx->dev_buffer;
  8228. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8229. }
  8230. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8231. if (ggml_backend_buffer_is_vk(src->buffer)) {
  8232. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8233. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8234. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8235. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8236. 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));
  8237. return true;
  8238. }
  8239. return false;
  8240. UNUSED(buffer);
  8241. }
  8242. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8243. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  8244. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  8245. }
  8246. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  8247. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  8248. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  8249. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  8250. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  8251. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  8252. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  8253. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  8254. /* .clear = */ ggml_backend_vk_buffer_clear,
  8255. /* .reset = */ NULL,
  8256. };
  8257. // vk buffer type
  8258. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8259. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8260. return ctx->name.c_str();
  8261. }
  8262. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8263. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  8264. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8265. vk_buffer dev_buffer = nullptr;
  8266. try {
  8267. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  8268. } catch (const vk::SystemError& e) {
  8269. return nullptr;
  8270. }
  8271. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  8272. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  8273. }
  8274. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8275. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8276. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  8277. }
  8278. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8279. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  8280. return ctx->device->suballocation_block_size;
  8281. }
  8282. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8283. return ggml_nbytes(tensor);
  8284. UNUSED(buft);
  8285. }
  8286. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  8287. ggml_vk_instance_init();
  8288. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  8289. vk_device dev = ggml_vk_get_device(dev_num);
  8290. return &dev->buffer_type;
  8291. }
  8292. // host buffer type
  8293. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8294. return GGML_VK_NAME "_Host";
  8295. UNUSED(buft);
  8296. }
  8297. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  8298. return GGML_VK_NAME "_Host";
  8299. UNUSED(buffer);
  8300. }
  8301. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8302. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  8303. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  8304. }
  8305. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8306. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  8307. size += 32; // Behave like the CPU buffer type
  8308. void * ptr = nullptr;
  8309. try {
  8310. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  8311. } catch (vk::SystemError& e) {
  8312. GGML_LOG_WARN("ggml_vulkan: Failed to allocate pinned memory (%s)\n", e.what());
  8313. // fallback to cpu buffer
  8314. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  8315. }
  8316. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  8317. buffer->buft = buft;
  8318. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  8319. return buffer;
  8320. UNUSED(buft);
  8321. }
  8322. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8323. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  8324. UNUSED(buft);
  8325. }
  8326. static size_t ggml_backend_vk_host_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  8327. return vk_instance.devices[0]->suballocation_block_size;
  8328. UNUSED(buft);
  8329. }
  8330. // Should be changed to return device-specific host buffer type
  8331. // but that probably requires changes in llama.cpp
  8332. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  8333. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  8334. /* .iface = */ {
  8335. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  8336. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  8337. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  8338. /* .get_max_size = */ ggml_backend_vk_host_buffer_type_get_max_size,
  8339. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  8340. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  8341. },
  8342. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  8343. /* .context = */ nullptr,
  8344. };
  8345. // Make sure device 0 is initialized
  8346. ggml_vk_instance_init();
  8347. ggml_vk_get_device(0);
  8348. return &ggml_backend_vk_buffer_type_host;
  8349. }
  8350. // backend
  8351. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  8352. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8353. return ctx->name.c_str();
  8354. }
  8355. static void ggml_backend_vk_free(ggml_backend_t backend) {
  8356. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8357. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  8358. ggml_vk_cleanup(ctx);
  8359. delete ctx;
  8360. delete backend;
  8361. }
  8362. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  8363. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8364. return &ctx->device->buffer_type;
  8365. }
  8366. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  8367. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  8368. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8369. 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");
  8370. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8371. vk_context transfer_ctx;
  8372. if (ctx->transfer_ctx.expired()) {
  8373. // Initialize new transfer context
  8374. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8375. ctx->transfer_ctx = transfer_ctx;
  8376. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8377. } else {
  8378. transfer_ctx = ctx->transfer_ctx.lock();
  8379. }
  8380. vk_buffer buf = buf_ctx->dev_buffer;
  8381. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8382. }
  8383. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  8384. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  8385. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8386. 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");
  8387. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8388. vk_context transfer_ctx;
  8389. if (ctx->transfer_ctx.expired()) {
  8390. // Initialize new transfer context
  8391. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8392. ctx->transfer_ctx = transfer_ctx;
  8393. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8394. } else {
  8395. transfer_ctx = ctx->transfer_ctx.lock();
  8396. }
  8397. vk_buffer buf = buf_ctx->dev_buffer;
  8398. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  8399. }
  8400. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  8401. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  8402. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8403. 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)) {
  8404. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  8405. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  8406. vk_context transfer_ctx;
  8407. if (ctx->transfer_ctx.expired()) {
  8408. // Initialize new transfer context
  8409. transfer_ctx = ggml_vk_create_context(ctx, ctx->transfer_cmd_pool);
  8410. ctx->transfer_ctx = transfer_ctx;
  8411. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  8412. } else {
  8413. transfer_ctx = ctx->transfer_ctx.lock();
  8414. }
  8415. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  8416. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  8417. 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));
  8418. return true;
  8419. }
  8420. return false;
  8421. }
  8422. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  8423. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  8424. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8425. if(ctx->transfer_ctx.expired()) {
  8426. return;
  8427. }
  8428. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  8429. ggml_vk_ctx_end(transfer_ctx);
  8430. for (auto& cpy : transfer_ctx->in_memcpys) {
  8431. memcpy(cpy.dst, cpy.src, cpy.n);
  8432. }
  8433. ggml_vk_submit(transfer_ctx, ctx->fence);
  8434. ggml_vk_wait_for_fence(ctx);
  8435. for (auto& cpy : transfer_ctx->out_memcpys) {
  8436. memcpy(cpy.dst, cpy.src, cpy.n);
  8437. }
  8438. ctx->transfer_ctx.reset();
  8439. }
  8440. static bool ggml_vk_is_empty(ggml_tensor * node) {
  8441. 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;
  8442. }
  8443. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  8444. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  8445. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  8446. if (vk_instance.debug_utils_support) {
  8447. vk::DebugUtilsLabelEXT dul = {};
  8448. dul.pLabelName = "ggml_backend_vk_graph_compute";
  8449. dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
  8450. vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
  8451. }
  8452. uint64_t total_mat_mul_bytes = 0;
  8453. for (int i = 0; i < cgraph->n_nodes; i++) {
  8454. if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8455. ctx->num_additional_fused_ops = 1;
  8456. }
  8457. ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
  8458. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8459. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8460. }
  8461. i += ctx->num_additional_fused_ops;
  8462. ctx->num_additional_fused_ops = 0;
  8463. }
  8464. if (ctx->device->need_compiles) {
  8465. ggml_vk_load_shaders(ctx->device);
  8466. }
  8467. ggml_vk_preallocate_buffers(ctx);
  8468. ggml_pipeline_allocate_descriptor_sets(ctx);
  8469. int last_node = cgraph->n_nodes - 1;
  8470. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  8471. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  8472. last_node -= 1;
  8473. }
  8474. // Reserve tensor context space for all nodes
  8475. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  8476. bool first_node_in_batch = true; // true if next node will be first node in a batch
  8477. int submit_node_idx = 0; // index to first node in a batch
  8478. vk_context compute_ctx;
  8479. if (vk_perf_logger_enabled) {
  8480. // allocate/resize the query pool
  8481. if (ctx->device->num_queries < cgraph->n_nodes + 1) {
  8482. if (ctx->device->query_pool) {
  8483. ctx->device->device.destroyQueryPool(ctx->device->query_pool);
  8484. }
  8485. vk::QueryPoolCreateInfo query_create_info;
  8486. query_create_info.queryType = vk::QueryType::eTimestamp;
  8487. query_create_info.queryCount = cgraph->n_nodes + 100;
  8488. ctx->device->query_pool = ctx->device->device.createQueryPool(query_create_info);
  8489. ctx->device->num_queries = query_create_info.queryCount;
  8490. }
  8491. ctx->device->device.resetQueryPool(ctx->device->query_pool, 0, cgraph->n_nodes+1);
  8492. GGML_ASSERT(ctx->compute_ctx.expired());
  8493. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8494. ctx->compute_ctx = compute_ctx;
  8495. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8496. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, 0);
  8497. }
  8498. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  8499. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  8500. // (and scaled down based on model size, so smaller models submit earlier).
  8501. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  8502. int nodes_per_submit = 100;
  8503. int submitted_nodes = 0;
  8504. int submit_count = 0;
  8505. uint64_t mul_mat_bytes = 0;
  8506. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  8507. for (int i = 0; i < cgraph->n_nodes; i++) {
  8508. if (first_node_in_batch) {
  8509. submit_node_idx = i;
  8510. }
  8511. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  8512. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  8513. }
  8514. if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
  8515. ctx->num_additional_fused_ops = 1;
  8516. }
  8517. // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
  8518. bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
  8519. bool submit = (submitted_nodes >= nodes_per_submit) ||
  8520. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  8521. (i + ctx->num_additional_fused_ops == last_node) ||
  8522. (almost_ready && !ctx->almost_ready_fence_pending);
  8523. 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);
  8524. if (vk_perf_logger_enabled) {
  8525. if (ctx->compute_ctx.expired()) {
  8526. compute_ctx = ggml_vk_create_context(ctx, ctx->compute_cmd_pool);
  8527. ctx->compute_ctx = compute_ctx;
  8528. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  8529. } else {
  8530. compute_ctx = ctx->compute_ctx.lock();
  8531. }
  8532. // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
  8533. for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
  8534. compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
  8535. }
  8536. }
  8537. if (enqueued) {
  8538. ++submitted_nodes;
  8539. #ifndef GGML_VULKAN_CHECK_RESULTS
  8540. if (first_node_in_batch) {
  8541. first_node_in_batch = false;
  8542. }
  8543. #endif
  8544. }
  8545. if (submit && enqueued) {
  8546. first_node_in_batch = true;
  8547. submitted_nodes = 0;
  8548. mul_mat_bytes = 0;
  8549. if (submit_count < 3) {
  8550. mul_mat_bytes_per_submit *= 2;
  8551. }
  8552. submit_count++;
  8553. }
  8554. i += ctx->num_additional_fused_ops;
  8555. ctx->num_additional_fused_ops = 0;
  8556. }
  8557. if (vk_perf_logger_enabled) {
  8558. // End the command buffer and submit/wait
  8559. GGML_ASSERT(!ctx->compute_ctx.expired());
  8560. compute_ctx = ctx->compute_ctx.lock();
  8561. ggml_vk_ctx_end(compute_ctx);
  8562. ggml_vk_submit(compute_ctx, ctx->device->fence);
  8563. VK_CHECK(ctx->device->device.waitForFences({ ctx->device->fence }, true, UINT64_MAX), "GGML_VULKAN_PERF waitForFences");
  8564. ctx->device->device.resetFences({ ctx->device->fence });
  8565. // Get the results and pass them to the logger
  8566. std::vector<uint64_t> timestamps(cgraph->n_nodes + 1);
  8567. 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");
  8568. for (int i = 0; i < cgraph->n_nodes; i++) {
  8569. if (!ggml_vk_is_empty(cgraph->nodes[i])) {
  8570. ctx->device->perf_logger->log_timing(cgraph->nodes[i], uint64_t((timestamps[i+1] - timestamps[i]) * ctx->device->properties.limits.timestampPeriod));
  8571. }
  8572. }
  8573. ctx->device->perf_logger->print_timings();
  8574. }
  8575. ggml_vk_graph_cleanup(ctx);
  8576. return GGML_STATUS_SUCCESS;
  8577. UNUSED(backend);
  8578. }
  8579. // TODO: enable async and synchronize
  8580. static ggml_backend_i ggml_backend_vk_interface = {
  8581. /* .get_name = */ ggml_backend_vk_name,
  8582. /* .free = */ ggml_backend_vk_free,
  8583. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  8584. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  8585. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  8586. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  8587. /* .graph_plan_create = */ NULL,
  8588. /* .graph_plan_free = */ NULL,
  8589. /* .graph_plan_update = */ NULL,
  8590. /* .graph_plan_compute = */ NULL,
  8591. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  8592. /* .event_record = */ NULL,
  8593. /* .event_wait = */ NULL,
  8594. };
  8595. static ggml_guid_t ggml_backend_vk_guid() {
  8596. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  8597. return &guid;
  8598. }
  8599. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  8600. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  8601. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  8602. ggml_vk_init(ctx, dev_num);
  8603. ggml_backend_t vk_backend = new ggml_backend {
  8604. /* .guid = */ ggml_backend_vk_guid(),
  8605. /* .interface = */ ggml_backend_vk_interface,
  8606. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  8607. /* .context = */ ctx,
  8608. };
  8609. return vk_backend;
  8610. }
  8611. bool ggml_backend_is_vk(ggml_backend_t backend) {
  8612. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  8613. }
  8614. int ggml_backend_vk_get_device_count() {
  8615. return ggml_vk_get_device_count();
  8616. }
  8617. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  8618. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8619. int dev_idx = vk_instance.device_indices[device];
  8620. ggml_vk_get_device_description(dev_idx, description, description_size);
  8621. }
  8622. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  8623. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  8624. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  8625. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  8626. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  8627. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  8628. *total = heap.size;
  8629. *free = heap.size;
  8630. break;
  8631. }
  8632. }
  8633. }
  8634. //////////////////////////
  8635. struct ggml_backend_vk_device_context {
  8636. size_t device;
  8637. std::string name;
  8638. std::string description;
  8639. };
  8640. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  8641. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8642. return ctx->name.c_str();
  8643. }
  8644. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  8645. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8646. return ctx->description.c_str();
  8647. }
  8648. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  8649. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  8650. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  8651. }
  8652. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  8653. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8654. return ggml_backend_vk_buffer_type(ctx->device);
  8655. }
  8656. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  8657. UNUSED(dev);
  8658. return ggml_backend_vk_host_buffer_type();
  8659. }
  8660. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  8661. UNUSED(dev);
  8662. return GGML_BACKEND_DEVICE_TYPE_GPU;
  8663. }
  8664. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  8665. props->name = ggml_backend_vk_device_get_name(dev);
  8666. props->description = ggml_backend_vk_device_get_description(dev);
  8667. props->type = ggml_backend_vk_device_get_type(dev);
  8668. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  8669. props->caps = {
  8670. /* .async = */ false,
  8671. /* .host_buffer = */ true,
  8672. /* .buffer_from_host_ptr = */ false,
  8673. /* .events = */ false,
  8674. };
  8675. }
  8676. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  8677. UNUSED(params);
  8678. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8679. return ggml_backend_vk_init(ctx->device);
  8680. }
  8681. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  8682. switch (op->op) {
  8683. case GGML_OP_UNARY:
  8684. switch (ggml_get_unary_op(op)) {
  8685. case GGML_UNARY_OP_GELU:
  8686. case GGML_UNARY_OP_GELU_ERF:
  8687. case GGML_UNARY_OP_GELU_QUICK:
  8688. case GGML_UNARY_OP_SILU:
  8689. case GGML_UNARY_OP_RELU:
  8690. case GGML_UNARY_OP_TANH:
  8691. case GGML_UNARY_OP_SIGMOID:
  8692. return ggml_is_contiguous(op->src[0]) &&
  8693. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8694. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8695. (op->src[0]->type == op->type);
  8696. default:
  8697. return false;
  8698. }
  8699. break;
  8700. case GGML_OP_GLU:
  8701. switch (ggml_get_glu_op(op)) {
  8702. case GGML_GLU_OP_GEGLU:
  8703. case GGML_GLU_OP_REGLU:
  8704. case GGML_GLU_OP_SWIGLU:
  8705. case GGML_GLU_OP_GEGLU_ERF:
  8706. case GGML_GLU_OP_GEGLU_QUICK:
  8707. return ggml_is_contiguous(op->src[0]) &&
  8708. (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8709. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) &&
  8710. (op->src[0]->type == op->type);
  8711. default:
  8712. return false;
  8713. }
  8714. break;
  8715. case GGML_OP_MUL_MAT:
  8716. case GGML_OP_MUL_MAT_ID:
  8717. {
  8718. ggml_type src0_type = op->src[0]->type;
  8719. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8720. const vk_device& device = ggml_vk_get_device(ctx->device);
  8721. if (op->op == GGML_OP_MUL_MAT_ID) {
  8722. if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  8723. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  8724. return false;
  8725. }
  8726. // Check against size of shared memory variable
  8727. if (op->src[2]->ne[0] > 4096) {
  8728. return false;
  8729. }
  8730. }
  8731. switch (src0_type) {
  8732. case GGML_TYPE_F32:
  8733. case GGML_TYPE_F16:
  8734. case GGML_TYPE_BF16:
  8735. case GGML_TYPE_Q4_0:
  8736. case GGML_TYPE_Q4_1:
  8737. case GGML_TYPE_Q5_0:
  8738. case GGML_TYPE_Q5_1:
  8739. case GGML_TYPE_Q8_0:
  8740. case GGML_TYPE_Q2_K:
  8741. case GGML_TYPE_Q3_K:
  8742. case GGML_TYPE_Q4_K:
  8743. case GGML_TYPE_Q5_K:
  8744. case GGML_TYPE_Q6_K:
  8745. case GGML_TYPE_IQ1_S:
  8746. case GGML_TYPE_IQ1_M:
  8747. case GGML_TYPE_IQ2_XXS:
  8748. case GGML_TYPE_IQ2_XS:
  8749. case GGML_TYPE_IQ2_S:
  8750. case GGML_TYPE_IQ3_XXS:
  8751. case GGML_TYPE_IQ3_S:
  8752. case GGML_TYPE_IQ4_XS:
  8753. case GGML_TYPE_IQ4_NL:
  8754. break;
  8755. default:
  8756. return false;
  8757. }
  8758. struct ggml_tensor * a;
  8759. struct ggml_tensor * b;
  8760. if (op->op == GGML_OP_MUL_MAT) {
  8761. a = op->src[0];
  8762. b = op->src[1];
  8763. } else {
  8764. a = op->src[2];
  8765. b = op->src[1];
  8766. }
  8767. if (a->ne[3] != b->ne[3]) {
  8768. return false;
  8769. }
  8770. 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) ||
  8771. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  8772. return false;
  8773. }
  8774. if (op->src[0]->type == GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_F16) {
  8775. // We currently don't have a bf16 x f16 shader, or an fp16->bf16 copy shader.
  8776. // So don't support this combination for now.
  8777. return false;
  8778. }
  8779. return true;
  8780. } break;
  8781. case GGML_OP_FLASH_ATTN_EXT:
  8782. {
  8783. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8784. auto device = ggml_vk_get_device(ctx->device);
  8785. bool coopmat2 = device->coopmat2;
  8786. FaHeadSizes head_sizes = fa_get_head_sizes(op->src[1]->ne[0], op->src[2]->ne[0]);
  8787. if (head_sizes == FA_HEAD_SIZE_UNSUPPORTED) {
  8788. return false;
  8789. }
  8790. if (op->src[0]->type != GGML_TYPE_F32) {
  8791. return false;
  8792. }
  8793. if (op->type != GGML_TYPE_F32) {
  8794. return false;
  8795. }
  8796. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  8797. return false;
  8798. }
  8799. // TODO: support broadcast
  8800. // note: this was initially implemented in https://github.com/ggml-org/llama.cpp/pull/14449, but
  8801. // the interface of ggml_flash_attn_ext() changed in https://github.com/ggml-org/llama.cpp/pull/14505
  8802. if (op->src[0]->ne[3] != 1 || (op->src[3] && op->src[3]->ne[2] != 1)) {
  8803. return false;
  8804. }
  8805. // It's straightforward to support different K/V dequant, but would
  8806. // significantly increase the number of pipelines
  8807. if (op->src[1]->type != op->src[2]->type) {
  8808. return false;
  8809. }
  8810. switch (op->src[1]->type) {
  8811. case GGML_TYPE_F16:
  8812. case GGML_TYPE_Q4_0:
  8813. case GGML_TYPE_Q8_0:
  8814. // supported in scalar and coopmat2 paths
  8815. break;
  8816. case GGML_TYPE_Q4_1:
  8817. case GGML_TYPE_Q5_0:
  8818. case GGML_TYPE_Q5_1:
  8819. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  8820. //case GGML_TYPE_Q2_K:
  8821. //case GGML_TYPE_Q3_K:
  8822. //case GGML_TYPE_Q4_K:
  8823. //case GGML_TYPE_Q5_K:
  8824. //case GGML_TYPE_Q6_K:
  8825. //case GGML_TYPE_IQ1_S:
  8826. //case GGML_TYPE_IQ1_M:
  8827. //case GGML_TYPE_IQ2_XXS:
  8828. //case GGML_TYPE_IQ2_XS:
  8829. //case GGML_TYPE_IQ2_S:
  8830. //case GGML_TYPE_IQ3_XXS:
  8831. //case GGML_TYPE_IQ3_S:
  8832. //case GGML_TYPE_IQ4_XS:
  8833. case GGML_TYPE_IQ4_NL:
  8834. // currently supported only in coopmat2 path
  8835. if (!coopmat2) {
  8836. return false;
  8837. }
  8838. break;
  8839. default:
  8840. return false;
  8841. }
  8842. if (!coopmat2 && !device->subgroup_shuffle) {
  8843. // scalar FA uses subgroupShuffle
  8844. return false;
  8845. }
  8846. return true;
  8847. }
  8848. case GGML_OP_GET_ROWS:
  8849. {
  8850. switch (op->src[0]->type) {
  8851. case GGML_TYPE_F32:
  8852. case GGML_TYPE_F16:
  8853. case GGML_TYPE_BF16:
  8854. case GGML_TYPE_Q4_0:
  8855. case GGML_TYPE_Q4_1:
  8856. case GGML_TYPE_Q5_0:
  8857. case GGML_TYPE_Q5_1:
  8858. case GGML_TYPE_Q8_0:
  8859. case GGML_TYPE_IQ1_S:
  8860. case GGML_TYPE_IQ1_M:
  8861. case GGML_TYPE_IQ2_XXS:
  8862. case GGML_TYPE_IQ2_XS:
  8863. case GGML_TYPE_IQ2_S:
  8864. case GGML_TYPE_IQ3_XXS:
  8865. case GGML_TYPE_IQ3_S:
  8866. case GGML_TYPE_IQ4_XS:
  8867. case GGML_TYPE_IQ4_NL:
  8868. return true;
  8869. default:
  8870. return false;
  8871. }
  8872. } break;
  8873. case GGML_OP_SET_ROWS:
  8874. {
  8875. // TODO: add support
  8876. // ref: https://github.com/ggml-org/llama.cpp/pull/14274
  8877. return false;
  8878. } break;
  8879. case GGML_OP_CONT:
  8880. case GGML_OP_CPY:
  8881. case GGML_OP_DUP:
  8882. {
  8883. ggml_type src0_type = op->src[0]->type;
  8884. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  8885. if (src0_type == GGML_TYPE_F32) {
  8886. switch (src1_type) {
  8887. case GGML_TYPE_F32:
  8888. case GGML_TYPE_F16:
  8889. case GGML_TYPE_BF16:
  8890. case GGML_TYPE_Q4_0:
  8891. case GGML_TYPE_Q4_1:
  8892. case GGML_TYPE_Q5_0:
  8893. case GGML_TYPE_Q5_1:
  8894. case GGML_TYPE_Q8_0:
  8895. case GGML_TYPE_IQ4_NL:
  8896. return true;
  8897. default:
  8898. break;
  8899. }
  8900. }
  8901. if (src1_type == GGML_TYPE_F32) {
  8902. switch (src0_type) {
  8903. case GGML_TYPE_F16:
  8904. case GGML_TYPE_Q4_0:
  8905. case GGML_TYPE_Q4_1:
  8906. case GGML_TYPE_Q5_0:
  8907. case GGML_TYPE_Q5_1:
  8908. case GGML_TYPE_Q8_0:
  8909. case GGML_TYPE_IQ4_NL:
  8910. return true;
  8911. default:
  8912. break;
  8913. }
  8914. }
  8915. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8916. return true;
  8917. }
  8918. // We can handle copying from a type to the same type if it's
  8919. // contiguous (memcpy). We use f16 or f32 shaders to do the copy,
  8920. // so the type/block size must be a multiple of 4.
  8921. if (src0_type == src1_type &&
  8922. ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op) &&
  8923. (ggml_type_size(src0_type) % 2) == 0) {
  8924. return true;
  8925. }
  8926. return false;
  8927. } break;
  8928. case GGML_OP_REPEAT:
  8929. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  8930. case GGML_OP_REPEAT_BACK:
  8931. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  8932. case GGML_OP_ROPE:
  8933. case GGML_OP_ROPE_BACK:
  8934. case GGML_OP_NONE:
  8935. case GGML_OP_RESHAPE:
  8936. case GGML_OP_VIEW:
  8937. case GGML_OP_PERMUTE:
  8938. case GGML_OP_TRANSPOSE:
  8939. case GGML_OP_RMS_NORM:
  8940. return true;
  8941. case GGML_OP_NORM:
  8942. case GGML_OP_GROUP_NORM:
  8943. case GGML_OP_L2_NORM:
  8944. return ggml_is_contiguous(op->src[0]);
  8945. case GGML_OP_ADD:
  8946. case GGML_OP_SUB:
  8947. case GGML_OP_MUL:
  8948. case GGML_OP_DIV:
  8949. return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) &&
  8950. (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) &&
  8951. (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16);
  8952. case GGML_OP_SILU_BACK:
  8953. case GGML_OP_RMS_NORM_BACK:
  8954. case GGML_OP_SQR:
  8955. case GGML_OP_SIN:
  8956. case GGML_OP_COS:
  8957. case GGML_OP_CLAMP:
  8958. return op->src[0]->type == GGML_TYPE_F32;
  8959. case GGML_OP_UPSCALE:
  8960. return op->op_params[0] == GGML_SCALE_MODE_NEAREST;
  8961. case GGML_OP_ACC:
  8962. case GGML_OP_CONCAT:
  8963. case GGML_OP_SCALE:
  8964. case GGML_OP_PAD:
  8965. case GGML_OP_DIAG_MASK_INF:
  8966. return true;
  8967. case GGML_OP_SOFT_MAX:
  8968. case GGML_OP_SOFT_MAX_BACK:
  8969. case GGML_OP_ARGSORT:
  8970. case GGML_OP_SUM:
  8971. case GGML_OP_SUM_ROWS:
  8972. case GGML_OP_ARGMAX:
  8973. case GGML_OP_COUNT_EQUAL:
  8974. case GGML_OP_IM2COL:
  8975. case GGML_OP_TIMESTEP_EMBEDDING:
  8976. case GGML_OP_CONV_2D_DW:
  8977. case GGML_OP_POOL_2D:
  8978. case GGML_OP_RWKV_WKV6:
  8979. case GGML_OP_RWKV_WKV7:
  8980. case GGML_OP_LEAKY_RELU:
  8981. case GGML_OP_OPT_STEP_ADAMW:
  8982. return true;
  8983. case GGML_OP_CONV_TRANSPOSE_1D:
  8984. return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
  8985. default:
  8986. return false;
  8987. }
  8988. UNUSED(dev);
  8989. }
  8990. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  8991. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  8992. return false;
  8993. }
  8994. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  8995. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  8996. return buft_ctx->device->idx == ctx->device;
  8997. }
  8998. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  8999. const int min_batch_size = 32;
  9000. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  9001. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  9002. UNUSED(dev);
  9003. }
  9004. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  9005. /* .get_name = */ ggml_backend_vk_device_get_name,
  9006. /* .get_description = */ ggml_backend_vk_device_get_description,
  9007. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  9008. /* .get_type = */ ggml_backend_vk_device_get_type,
  9009. /* .get_props = */ ggml_backend_vk_device_get_props,
  9010. /* .init_backend = */ ggml_backend_vk_device_init,
  9011. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  9012. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  9013. /* .buffer_from_host_ptr = */ NULL,
  9014. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  9015. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  9016. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  9017. /* .event_new = */ NULL,
  9018. /* .event_free = */ NULL,
  9019. /* .event_synchronize = */ NULL,
  9020. };
  9021. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  9022. UNUSED(reg);
  9023. return GGML_VK_NAME;
  9024. }
  9025. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  9026. UNUSED(reg);
  9027. return ggml_backend_vk_get_device_count();
  9028. }
  9029. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  9030. static std::vector<ggml_backend_dev_t> devices;
  9031. static bool initialized = false;
  9032. {
  9033. static std::mutex mutex;
  9034. std::lock_guard<std::mutex> lock(mutex);
  9035. if (!initialized) {
  9036. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  9037. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  9038. char desc[256];
  9039. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  9040. ctx->device = i;
  9041. ctx->name = GGML_VK_NAME + std::to_string(i);
  9042. ctx->description = desc;
  9043. devices.push_back(new ggml_backend_device {
  9044. /* .iface = */ ggml_backend_vk_device_i,
  9045. /* .reg = */ reg,
  9046. /* .context = */ ctx,
  9047. });
  9048. }
  9049. initialized = true;
  9050. }
  9051. }
  9052. GGML_ASSERT(device < devices.size());
  9053. return devices[device];
  9054. }
  9055. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  9056. /* .get_name = */ ggml_backend_vk_reg_get_name,
  9057. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  9058. /* .get_device = */ ggml_backend_vk_reg_get_device,
  9059. /* .get_proc_address = */ NULL,
  9060. };
  9061. ggml_backend_reg_t ggml_backend_vk_reg() {
  9062. static ggml_backend_reg reg = {
  9063. /* .api_version = */ GGML_BACKEND_API_VERSION,
  9064. /* .iface = */ ggml_backend_vk_reg_i,
  9065. /* .context = */ nullptr,
  9066. };
  9067. try {
  9068. ggml_vk_instance_init();
  9069. return &reg;
  9070. } catch (const vk::SystemError& e) {
  9071. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  9072. return nullptr;
  9073. }
  9074. }
  9075. // Extension availability
  9076. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9077. #ifdef GGML_VULKAN_VALIDATE
  9078. bool portability_enumeration_ext = false;
  9079. // Check for portability enumeration extension for MoltenVK support
  9080. for (const auto& properties : instance_extensions) {
  9081. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9082. return true;
  9083. }
  9084. }
  9085. if (!portability_enumeration_ext) {
  9086. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9087. }
  9088. #endif
  9089. return false;
  9090. UNUSED(instance_extensions);
  9091. }
  9092. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  9093. #ifdef __APPLE__
  9094. bool portability_enumeration_ext = false;
  9095. // Check for portability enumeration extension for MoltenVK support
  9096. for (const auto& properties : instance_extensions) {
  9097. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  9098. return true;
  9099. }
  9100. }
  9101. if (!portability_enumeration_ext) {
  9102. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  9103. }
  9104. #endif
  9105. return false;
  9106. UNUSED(instance_extensions);
  9107. }
  9108. // Extension availability
  9109. static bool ggml_vk_instance_debug_utils_ext_available(
  9110. const std::vector<vk::ExtensionProperties> & instance_extensions) {
  9111. // Check for portability enumeration extension for MoltenVK support
  9112. for (const auto & properties : instance_extensions) {
  9113. if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
  9114. return true;
  9115. }
  9116. }
  9117. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
  9118. return false;
  9119. UNUSED(instance_extensions);
  9120. }
  9121. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  9122. switch (props.vendorID) {
  9123. case VK_VENDOR_ID_INTEL:
  9124. // Only allowing Xe2 GPU at the moment since Xe2 GPU can gain significant performance boost,
  9125. // while some older hardware (ex. Arc A770) has performance regressions
  9126. return arch == vk_device_architecture::INTEL_XE2;
  9127. case VK_VENDOR_ID_AMD:
  9128. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  9129. // Workaround for AMD proprietary driver reporting support on all GPUs
  9130. return arch == vk_device_architecture::AMD_RDNA3;
  9131. }
  9132. return true;
  9133. default:
  9134. return true;
  9135. }
  9136. }
  9137. // checks
  9138. #ifdef GGML_VULKAN_CHECK_RESULTS
  9139. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  9140. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  9141. return;
  9142. }
  9143. for (int j = 0; j < level; j++) {
  9144. std::cerr << " ";
  9145. }
  9146. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  9147. done.push_back(tensor);
  9148. for (int i = 0; i < GGML_MAX_SRC; i++) {
  9149. if (tensor->src[i] != nullptr) {
  9150. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  9151. }
  9152. }
  9153. }
  9154. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  9155. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  9156. return;
  9157. }
  9158. i0 = std::max(i0, 5);
  9159. i1 = std::max(i1, 5);
  9160. i2 = std::max(i2, 0);
  9161. i3 = std::max(i3, 0);
  9162. fprintf(stderr, " ");
  9163. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9164. fprintf(stderr, "%7d ", idx1);
  9165. }
  9166. fprintf(stderr, "\n");
  9167. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  9168. fprintf(stderr, "%7d: ", idx0);
  9169. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  9170. 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]) {
  9171. float val;
  9172. if (tensor->type == GGML_TYPE_F32) {
  9173. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9174. } else if (tensor->type == GGML_TYPE_F16) {
  9175. 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]));
  9176. } else if (tensor->type == GGML_TYPE_I32) {
  9177. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  9178. } else {
  9179. GGML_ABORT("fatal error");
  9180. }
  9181. fprintf(stderr, "% 7.2f ", val);
  9182. } else {
  9183. fprintf(stderr, " ");
  9184. }
  9185. }
  9186. fprintf(stderr, "\n");
  9187. }
  9188. }
  9189. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  9190. void * tensor_data = tensor->data;
  9191. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  9192. if (is_gpu) {
  9193. const size_t tensor_size = ggml_nbytes(tensor);
  9194. tensor_data = malloc(tensor_size);
  9195. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9196. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  9197. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  9198. }
  9199. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  9200. 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;
  9201. if (tensor->src[0] != nullptr) {
  9202. 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;
  9203. }
  9204. if (tensor->src[1] != nullptr) {
  9205. 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;
  9206. }
  9207. std::cerr << std::endl << "Result:" << std::endl;
  9208. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9209. std::cerr << std::endl;
  9210. std::vector<const ggml_tensor *> done;
  9211. ggml_vk_print_graph_origin(tensor, done);
  9212. if (is_gpu) {
  9213. free(tensor_data);
  9214. }
  9215. }
  9216. void * comp_result;
  9217. size_t comp_size;
  9218. size_t comp_nb[GGML_MAX_DIMS];
  9219. size_t check_counter = 0;
  9220. static void ggml_vk_check_results_0(ggml_tensor * tensor) {
  9221. if (tensor->op == GGML_OP_TRANSPOSE) {
  9222. return;
  9223. }
  9224. check_counter++;
  9225. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9226. return;
  9227. }
  9228. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  9229. ggml_tensor * src0 = tensor->src[0];
  9230. ggml_tensor * src1 = tensor->src[1];
  9231. struct ggml_init_params iparams = {
  9232. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  9233. /*.mem_buffer =*/ NULL,
  9234. /*.no_alloc =*/ false,
  9235. };
  9236. struct ggml_context * ggml_ctx = ggml_init(iparams);
  9237. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9238. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  9239. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  9240. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  9241. struct ggml_tensor * tensor_clone = nullptr;
  9242. for (int i = 0; i < 6; i++) {
  9243. ggml_tensor * srci = tensor->src[i];
  9244. if (srci == nullptr) {
  9245. continue;
  9246. }
  9247. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  9248. size_t srci_size = ggml_nbytes(srci);
  9249. src_clone[i] = srci_clone;
  9250. src_size[i] = ggml_nbytes(srci);
  9251. src_buffer[i] = malloc(srci_size);
  9252. srci_clone->data = src_buffer[i];
  9253. if (ggml_backend_buffer_is_host(srci->buffer)) {
  9254. memcpy(srci_clone->data, srci->data, srci_size);
  9255. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9256. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  9257. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  9258. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9259. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  9260. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  9261. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  9262. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  9263. const int idx = i3*srci->ne[2] + i2;
  9264. 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]);
  9265. }
  9266. }
  9267. srci_clone->nb[0] = srci->nb[0];
  9268. srci_clone->nb[1] = srci->nb[1];
  9269. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  9270. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  9271. }
  9272. } else {
  9273. if (offset + srci_size >= buffer_gpu->size) {
  9274. srci_size = buffer_gpu->size - offset;
  9275. }
  9276. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  9277. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9278. }
  9279. } else {
  9280. GGML_ABORT("fatal error");
  9281. }
  9282. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9283. ggml_vk_print_tensor(srci, srci_name[i]);
  9284. }
  9285. }
  9286. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  9287. const float * params = (const float *)tensor->op_params;
  9288. 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]);
  9289. } else if (tensor->op == GGML_OP_MUL_MAT) {
  9290. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  9291. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  9292. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  9293. } else if (tensor->op == GGML_OP_SUB) {
  9294. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  9295. } else if (tensor->op == GGML_OP_MUL) {
  9296. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  9297. } else if (tensor->op == GGML_OP_DIV) {
  9298. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  9299. } else if (tensor->op == GGML_OP_CONCAT) {
  9300. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  9301. } else if (tensor->op == GGML_OP_UPSCALE) {
  9302. 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]);
  9303. } else if (tensor->op == GGML_OP_SCALE) {
  9304. const float * params = (const float *)tensor->op_params;
  9305. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], params[0]);
  9306. } else if (tensor->op == GGML_OP_SQR) {
  9307. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  9308. } else if (tensor->op == GGML_OP_SIN) {
  9309. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  9310. } else if (tensor->op == GGML_OP_COS) {
  9311. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  9312. } else if (tensor->op == GGML_OP_CLAMP) {
  9313. const float * params = (const float *)tensor->op_params;
  9314. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  9315. } else if (tensor->op == GGML_OP_PAD) {
  9316. 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]);
  9317. } else if (tensor->op == GGML_OP_REPEAT) {
  9318. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  9319. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  9320. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  9321. } else if (tensor->op == GGML_OP_ADD) {
  9322. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  9323. } else if (tensor->op == GGML_OP_ACC) {
  9324. 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]);
  9325. } else if (tensor->op == GGML_OP_NORM) {
  9326. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9327. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  9328. const float * float_params = (const float *)tensor->op_params;
  9329. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  9330. } else if (tensor->op == GGML_OP_RMS_NORM) {
  9331. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  9332. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  9333. const float eps = ((float *) tensor->op_params)[0];
  9334. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  9335. } else if (tensor->op == GGML_OP_SILU_BACK) {
  9336. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  9337. } else if (tensor->op == GGML_OP_L2_NORM) {
  9338. const float eps = ((float *) tensor->op_params)[0];
  9339. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  9340. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  9341. if (src1 != nullptr) {
  9342. const float * params = (const float *)tensor->op_params;
  9343. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  9344. } else {
  9345. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  9346. }
  9347. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  9348. 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]);
  9349. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  9350. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  9351. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  9352. const int n_dims = ((int32_t *) tensor->op_params)[1];
  9353. const int mode = ((int32_t *) tensor->op_params)[2];
  9354. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  9355. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  9356. const float freq_base = ((float *) tensor->op_params)[5];
  9357. const float freq_scale = ((float *) tensor->op_params)[6];
  9358. const float ext_factor = ((float *) tensor->op_params)[7];
  9359. const float attn_factor = ((float *) tensor->op_params)[8];
  9360. const float beta_fast = ((float *) tensor->op_params)[9];
  9361. const float beta_slow = ((float *) tensor->op_params)[10];
  9362. if (mode & GGML_ROPE_TYPE_MROPE) {
  9363. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  9364. if (tensor->op == GGML_OP_ROPE) {
  9365. 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);
  9366. } else {
  9367. 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);
  9368. }
  9369. } else {
  9370. if (tensor->op == GGML_OP_ROPE) {
  9371. 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);
  9372. } else {
  9373. 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);
  9374. }
  9375. }
  9376. } else if (tensor->op == GGML_OP_UNARY) {
  9377. switch (ggml_get_unary_op(tensor)) {
  9378. case GGML_UNARY_OP_SILU:
  9379. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  9380. break;
  9381. case GGML_UNARY_OP_GELU:
  9382. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  9383. break;
  9384. case GGML_UNARY_OP_GELU_ERF:
  9385. tensor_clone = ggml_gelu_erf(ggml_ctx, src_clone[0]);
  9386. break;
  9387. case GGML_UNARY_OP_GELU_QUICK:
  9388. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  9389. break;
  9390. case GGML_UNARY_OP_RELU:
  9391. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  9392. break;
  9393. case GGML_UNARY_OP_TANH:
  9394. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  9395. break;
  9396. case GGML_UNARY_OP_SIGMOID:
  9397. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  9398. break;
  9399. default:
  9400. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9401. GGML_ABORT("fatal error");
  9402. }
  9403. } else if (tensor->op == GGML_OP_GLU) {
  9404. if (src_clone[1] == nullptr) {
  9405. tensor_clone = ggml_glu(ggml_ctx, src_clone[0], (ggml_glu_op) tensor->op_params[0], tensor->op_params[1]);
  9406. } else {
  9407. tensor_clone = ggml_glu_split(ggml_ctx, src_clone[0], src_clone[1], (ggml_glu_op) tensor->op_params[0]);
  9408. }
  9409. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  9410. if (src1 == nullptr) {
  9411. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  9412. tensor_clone->type = tensor->type;
  9413. } else {
  9414. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  9415. }
  9416. } else if (tensor->op == GGML_OP_CONT) {
  9417. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9418. } else if (tensor->op == GGML_OP_RESHAPE) {
  9419. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  9420. } else if (tensor->op == GGML_OP_VIEW) {
  9421. 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]);
  9422. } else if (tensor->op == GGML_OP_PERMUTE) {
  9423. int32_t * params = (int32_t *)tensor->op_params;
  9424. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  9425. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  9426. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  9427. } else if (tensor->op == GGML_OP_GET_ROWS) {
  9428. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  9429. } else if (tensor->op == GGML_OP_ARGSORT) {
  9430. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  9431. } else if (tensor->op == GGML_OP_SUM) {
  9432. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  9433. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  9434. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  9435. } else if (tensor->op == GGML_OP_ARGMAX) {
  9436. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  9437. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  9438. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  9439. } else if (tensor->op == GGML_OP_IM2COL) {
  9440. const int32_t s0 = tensor->op_params[0];
  9441. const int32_t s1 = tensor->op_params[1];
  9442. const int32_t p0 = tensor->op_params[2];
  9443. const int32_t p1 = tensor->op_params[3];
  9444. const int32_t d0 = tensor->op_params[4];
  9445. const int32_t d1 = tensor->op_params[5];
  9446. const bool is_2D = tensor->op_params[6] == 1;
  9447. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  9448. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  9449. const int32_t dim = tensor->op_params[0];
  9450. const int32_t max_period = tensor->op_params[1];
  9451. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  9452. } else if (tensor->op == GGML_OP_CONV_TRANSPOSE_1D){
  9453. const int32_t s0 = tensor->op_params[0];
  9454. const int32_t p0 = tensor->op_params[1];
  9455. const int32_t d0 = tensor->op_params[2];
  9456. tensor_clone = ggml_conv_transpose_1d(ggml_ctx, src_clone[0], src_clone[1], s0, p0, d0);
  9457. } else if (tensor->op == GGML_OP_POOL_2D) {
  9458. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  9459. const int32_t k0 = tensor->op_params[1];
  9460. const int32_t k1 = tensor->op_params[2];
  9461. const int32_t s0 = tensor->op_params[3];
  9462. const int32_t s1 = tensor->op_params[4];
  9463. const int32_t p0 = tensor->op_params[5];
  9464. const int32_t p1 = tensor->op_params[6];
  9465. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  9466. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  9467. const float * op_params = (const float *)tensor->op_params;
  9468. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  9469. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  9470. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  9471. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  9472. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  9473. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  9474. src_clone[4], src_clone[5], src_clone[6]);
  9475. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  9476. src_clone[0]->flags = src0->flags;
  9477. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  9478. src_clone[2], src_clone[3], src_clone[4]);
  9479. }
  9480. else {
  9481. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  9482. GGML_ABORT("fatal error");
  9483. }
  9484. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  9485. ggml_build_forward_expand(cgraph, tensor_clone);
  9486. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  9487. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9488. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  9489. }
  9490. comp_size = ggml_nbytes(tensor_clone);
  9491. comp_result = malloc(comp_size);
  9492. memcpy(comp_result, tensor_clone->data, comp_size);
  9493. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  9494. for (int i = 0; i < 6; i++) {
  9495. if (src_buffer[i] != nullptr) {
  9496. free(src_buffer[i]);
  9497. }
  9498. }
  9499. ggml_free(ggml_ctx);
  9500. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  9501. }
  9502. static void ggml_vk_check_results_1(ggml_tensor * tensor) {
  9503. if (tensor->op == GGML_OP_TRANSPOSE) {
  9504. return;
  9505. }
  9506. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  9507. return;
  9508. }
  9509. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  9510. ggml_tensor * src0 = tensor->src[0];
  9511. ggml_tensor * src1 = tensor->src[1];
  9512. ggml_tensor * src2 = tensor->src[2];
  9513. ggml_tensor * src3 = tensor->src[3];
  9514. void * tensor_data = tensor->data;
  9515. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9516. size_t tensor_size = ggml_nbytes(tensor);
  9517. tensor_data = malloc(tensor_size);
  9518. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  9519. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  9520. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  9521. if (offset + tensor_size >= buffer_gpu->size) {
  9522. tensor_size = buffer_gpu->size - offset;
  9523. }
  9524. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  9525. }
  9526. float first_error_result = -1.0f;
  9527. float first_error_correct = -1.0f;
  9528. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  9529. double avg_err = 0.0;
  9530. size_t counter = 0;
  9531. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  9532. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  9533. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  9534. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  9535. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  9536. float correct = 0.0f;
  9537. float result = 0.0f;
  9538. if (buffer_size_fit) {
  9539. if (tensor->type == GGML_TYPE_F32) {
  9540. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9541. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9542. } else if (tensor->type == GGML_TYPE_F16) {
  9543. 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]));
  9544. 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]));
  9545. } else if (tensor->type == GGML_TYPE_I32) {
  9546. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9547. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9548. } else if (tensor->type == GGML_TYPE_I64) {
  9549. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  9550. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  9551. } else {
  9552. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  9553. }
  9554. } else {
  9555. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  9556. GGML_ABORT("fatal error");
  9557. }
  9558. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  9559. 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;
  9560. 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;
  9561. if (src0 != nullptr) {
  9562. 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;
  9563. }
  9564. if (src1 != nullptr) {
  9565. 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;
  9566. }
  9567. if (src2 != nullptr) {
  9568. 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;
  9569. }
  9570. if (src3 != nullptr) {
  9571. 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;
  9572. }
  9573. 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;
  9574. std::cerr << std::endl << "Result:" << std::endl;
  9575. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  9576. std::cerr << std::endl << "Correct:" << std::endl;
  9577. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  9578. std::cerr << std::endl;
  9579. std::vector<const ggml_tensor *> done;
  9580. ggml_vk_print_graph_origin(tensor, done);
  9581. GGML_ABORT("fatal error");
  9582. }
  9583. const double denom = std::fabs(correct) > 1.0f ? (std::fabs(correct) > 1e-8 ? std::fabs(correct) : 1e-8) : 1.0f;
  9584. if (first_error[0] == -1 && std::fabs(correct - result) / denom > 0.5) {
  9585. first_error[0] = i0;
  9586. first_error[1] = i1;
  9587. first_error[2] = i2;
  9588. first_error[3] = i3;
  9589. first_error_result = result;
  9590. first_error_correct = correct;
  9591. }
  9592. // Special case, value is infinite, avoid NaN result in avg_err
  9593. // NaN also appears in results, if both are nan error is 0
  9594. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  9595. avg_err += std::fabs(correct - result) / denom;
  9596. }
  9597. counter++;
  9598. }
  9599. }
  9600. }
  9601. }
  9602. avg_err /= counter;
  9603. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  9604. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9605. 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;
  9606. if (src0 != nullptr) {
  9607. 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;
  9608. }
  9609. if (src1 != nullptr) {
  9610. 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;
  9611. }
  9612. if (src2 != nullptr) {
  9613. 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;
  9614. }
  9615. if (src3 != nullptr) {
  9616. 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;
  9617. }
  9618. 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;
  9619. std::cerr << std::endl << "Result:" << std::endl;
  9620. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  9621. std::cerr << std::endl << "Correct:" << std::endl;
  9622. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  9623. std::cerr << std::endl;
  9624. std::vector<const ggml_tensor *> done;
  9625. ggml_vk_print_graph_origin(tensor, done);
  9626. }
  9627. if (avg_err > 0.5 || std::isnan(avg_err)) {
  9628. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  9629. 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;
  9630. if (src0 != nullptr) {
  9631. 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;
  9632. }
  9633. if (src1 != nullptr) {
  9634. 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;
  9635. }
  9636. if (src2 != nullptr) {
  9637. 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;
  9638. }
  9639. if (src3 != nullptr) {
  9640. 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;
  9641. }
  9642. 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;
  9643. std::cerr << std::endl << "Result:" << std::endl;
  9644. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  9645. std::cerr << std::endl << "Correct:" << std::endl;
  9646. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  9647. std::cerr << std::endl;
  9648. std::vector<const ggml_tensor *> done;
  9649. ggml_vk_print_graph_origin(tensor, done);
  9650. GGML_ABORT("fatal error");
  9651. } else {
  9652. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  9653. }
  9654. free(comp_result);
  9655. comp_result = nullptr;
  9656. comp_size = 0;
  9657. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  9658. free(tensor_data);
  9659. }
  9660. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  9661. }
  9662. #endif
  9663. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)