ggml-vulkan.cpp 487 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_PERF) || 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. #include "ggml-impl.h"
  25. #include "ggml-backend-impl.h"
  26. #include "ggml-vulkan-shaders.hpp"
  27. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  28. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  29. #define VK_VENDOR_ID_AMD 0x1002
  30. #define VK_VENDOR_ID_APPLE 0x106b
  31. #define VK_VENDOR_ID_INTEL 0x8086
  32. #define VK_VENDOR_ID_NVIDIA 0x10de
  33. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 32
  34. #define GGML_VK_MAX_NODES 8192
  35. #define MAX_VK_BUFFERS 256
  36. #define VK_CHECK(err, msg) \
  37. do { \
  38. vk::Result err_ = (err); \
  39. if (err_ != vk::Result::eSuccess) { \
  40. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  41. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  42. exit(1); \
  43. } \
  44. } while (0)
  45. #ifdef GGML_VULKAN_DEBUG
  46. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  47. #else
  48. #define VK_LOG_DEBUG(msg) ((void) 0)
  49. #endif // GGML_VULKAN_DEBUG
  50. struct ggml_backend_vk_context;
  51. struct vk_queue {
  52. uint32_t queue_family_index;
  53. vk::Queue queue;
  54. vk::CommandPool pool;
  55. uint32_t cmd_buffer_idx;
  56. std::vector<vk::CommandBuffer> cmd_buffers;
  57. vk::PipelineStageFlags stage_flags;
  58. bool transfer_only;
  59. };
  60. struct vk_pipeline_struct {
  61. std::string name;
  62. vk::ShaderModule shader_module;
  63. vk::DescriptorSetLayout dsl;
  64. std::vector<vk::DescriptorPool> descriptor_pools;
  65. std::vector<vk::DescriptorSet> descriptor_sets;
  66. uint32_t descriptor_set_idx;
  67. vk::PipelineLayout layout;
  68. vk::Pipeline pipeline;
  69. uint32_t push_constant_size;
  70. uint32_t parameter_count;
  71. std::array<uint32_t, 3> wg_denoms;
  72. uint32_t align;
  73. // set to true to request the pipeline is compiled after the dryrun
  74. bool needed {};
  75. // set to true when the shader has been compiled
  76. bool compiled {};
  77. };
  78. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  79. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  80. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  81. struct vk_matmul_pipeline_struct {
  82. vk_pipeline l, m, s;
  83. vk_pipeline a_l, a_m, a_s;
  84. };
  85. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  86. struct vk_matmul_pipeline2 {
  87. vk_matmul_pipeline2() {
  88. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  89. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  90. }
  91. vk_matmul_pipeline f32acc;
  92. vk_matmul_pipeline f16acc;
  93. };
  94. struct vk_device_struct;
  95. typedef std::shared_ptr<vk_device_struct> vk_device;
  96. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  97. struct vk_buffer_struct;
  98. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  99. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  100. struct ggml_backend_vk_buffer_type_context {
  101. std::string name;
  102. vk_device device;
  103. };
  104. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  105. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  106. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  107. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  108. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  109. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  110. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  111. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  112. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  113. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  114. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  115. /* .is_host = */ NULL,
  116. };
  117. #ifdef GGML_VULKAN_MEMORY_DEBUG
  118. class vk_memory_logger;
  119. #endif
  120. #ifdef GGML_VULKAN_PERF
  121. class vk_perf_logger;
  122. #endif
  123. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  124. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  125. enum vk_device_architecture {
  126. OTHER,
  127. AMD_GCN,
  128. AMD_RDNA1,
  129. AMD_RDNA2,
  130. AMD_RDNA3,
  131. };
  132. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  133. vk::PhysicalDeviceProperties props = device.getProperties();
  134. if (props.vendorID == VK_VENDOR_ID_AMD) {
  135. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  136. bool amd_shader_core_properties = false;
  137. bool integer_dot_product = false;
  138. bool subgroup_size_control = false;
  139. for (const auto& properties : ext_props) {
  140. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  141. amd_shader_core_properties = true;
  142. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  143. integer_dot_product = true;
  144. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  145. subgroup_size_control = true;
  146. }
  147. }
  148. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  149. return vk_device_architecture::OTHER;
  150. }
  151. vk::PhysicalDeviceProperties2 props2;
  152. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  153. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  154. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  155. props2.pNext = &shader_core_props_amd;
  156. shader_core_props_amd.pNext = &integer_dot_props;
  157. integer_dot_props.pNext = &subgroup_size_control_props;
  158. device.getProperties2(&props2);
  159. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  160. return vk_device_architecture::AMD_GCN;
  161. }
  162. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  163. // RDNA
  164. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  165. return vk_device_architecture::AMD_RDNA1;
  166. }
  167. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  168. return vk_device_architecture::AMD_RDNA3;
  169. }
  170. return vk_device_architecture::AMD_RDNA2;
  171. }
  172. }
  173. return vk_device_architecture::OTHER;
  174. }
  175. struct vk_device_struct {
  176. std::mutex mutex;
  177. vk::PhysicalDevice physical_device;
  178. vk::PhysicalDeviceProperties properties;
  179. std::string name;
  180. uint64_t max_memory_allocation_size;
  181. uint64_t suballocation_block_size;
  182. bool fp16;
  183. bool pipeline_robustness;
  184. vk::Device device;
  185. uint32_t vendor_id;
  186. vk_device_architecture architecture;
  187. vk_queue compute_queue;
  188. vk_queue transfer_queue;
  189. bool single_queue;
  190. uint32_t subgroup_size;
  191. uint32_t shader_core_count;
  192. bool uma;
  193. bool prefer_host_memory;
  194. bool float_controls_rte_fp16;
  195. bool subgroup_size_control;
  196. uint32_t subgroup_min_size;
  197. uint32_t subgroup_max_size;
  198. bool subgroup_require_full_support;
  199. bool coopmat_support;
  200. bool coopmat_acc_f32_support;
  201. bool coopmat_acc_f16_support;
  202. uint32_t coopmat_m;
  203. uint32_t coopmat_n;
  204. uint32_t coopmat_k;
  205. bool coopmat2;
  206. size_t idx;
  207. bool mul_mat_l[GGML_TYPE_COUNT];
  208. bool mul_mat_m[GGML_TYPE_COUNT];
  209. bool mul_mat_s[GGML_TYPE_COUNT];
  210. bool mul_mat_id_l[GGML_TYPE_COUNT];
  211. bool mul_mat_id_m[GGML_TYPE_COUNT];
  212. bool mul_mat_id_s[GGML_TYPE_COUNT];
  213. // set to true to indicate that some shaders need to be compiled after the dryrun
  214. bool need_compiles {};
  215. vk_matmul_pipeline pipeline_matmul_f32 {};
  216. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  217. vk_matmul_pipeline2 pipeline_matmul_f16;
  218. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  219. vk_pipeline pipeline_matmul_split_k_reduce;
  220. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  221. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  222. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  223. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  224. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  225. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  226. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  227. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  228. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  229. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  230. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32;
  231. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  232. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  233. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  234. vk_pipeline pipeline_acc_f32;
  235. vk_pipeline pipeline_add_f32, pipeline_add_f32_norepeat;
  236. vk_pipeline pipeline_add_f16_f32_f16, pipeline_add_f16_f32_f16_norepeat;
  237. vk_pipeline pipeline_sub_f32, pipeline_sub_f32_norepeat;
  238. vk_pipeline pipeline_mul_f32, pipeline_mul_f32_norepeat;
  239. vk_pipeline pipeline_div_f32, pipeline_div_f32_norepeat;
  240. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  241. vk_pipeline pipeline_upscale_f32;
  242. vk_pipeline pipeline_scale_f32;
  243. vk_pipeline pipeline_sqr_f32;
  244. vk_pipeline pipeline_sin_f32;
  245. vk_pipeline pipeline_cos_f32;
  246. vk_pipeline pipeline_clamp_f32;
  247. vk_pipeline pipeline_pad_f32;
  248. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  249. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16;
  250. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16;
  251. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  252. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  253. vk_pipeline pipeline_norm_f32;
  254. vk_pipeline pipeline_group_norm_f32;
  255. vk_pipeline pipeline_rms_norm_f32;
  256. vk_pipeline pipeline_rms_norm_back_f32;
  257. vk_pipeline pipeline_l2_norm_f32;
  258. vk_pipeline pipeline_gelu_f32;
  259. vk_pipeline pipeline_gelu_quick_f32;
  260. vk_pipeline pipeline_silu_f32;
  261. vk_pipeline pipeline_silu_back_f32;
  262. vk_pipeline pipeline_relu_f32;
  263. vk_pipeline pipeline_leaky_relu_f32;
  264. vk_pipeline pipeline_tanh_f32;
  265. vk_pipeline pipeline_sigmoid_f32;
  266. vk_pipeline pipeline_diag_mask_inf_f32;
  267. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  268. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  269. vk_pipeline pipeline_soft_max_back_f32;
  270. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  271. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  272. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  273. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  274. vk_pipeline pipeline_argsort_f32;
  275. vk_pipeline pipeline_sum_rows_f32;
  276. vk_pipeline pipeline_argmax_f32;
  277. vk_pipeline pipeline_count_equal_i32;
  278. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  279. vk_pipeline pipeline_timestep_embedding_f32;
  280. vk_pipeline pipeline_pool2d_f32;
  281. vk_pipeline pipeline_rwkv_wkv6_f32;
  282. vk_pipeline pipeline_rwkv_wkv7_f32;
  283. vk_pipeline pipeline_opt_step_adamw_f32;
  284. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  285. vk_pipeline pipeline_flash_attn_f32_f16_D64[GGML_TYPE_COUNT][2][2][2];
  286. vk_pipeline pipeline_flash_attn_f32_f16_D80[GGML_TYPE_COUNT][2][2][2];
  287. vk_pipeline pipeline_flash_attn_f32_f16_D96[GGML_TYPE_COUNT][2][2][2];
  288. vk_pipeline pipeline_flash_attn_f32_f16_D112[GGML_TYPE_COUNT][2][2][2];
  289. vk_pipeline pipeline_flash_attn_f32_f16_D128[GGML_TYPE_COUNT][2][2][2];
  290. vk_pipeline pipeline_flash_attn_f32_f16_D256[GGML_TYPE_COUNT][2][2][2];
  291. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  292. std::unordered_map<std::string, uint64_t> pipeline_descriptor_set_requirements;
  293. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  294. vk::Fence fence;
  295. vk_buffer sync_staging;
  296. ggml_backend_buffer_type buffer_type;
  297. #ifdef GGML_VULKAN_MEMORY_DEBUG
  298. std::unique_ptr<vk_memory_logger> memory_logger;
  299. #endif
  300. #ifdef GGML_VULKAN_PERF
  301. std::unique_ptr<vk_perf_logger> perf_logger;
  302. #endif
  303. ~vk_device_struct() {
  304. VK_LOG_DEBUG("destroy device " << name);
  305. device.destroyFence(fence);
  306. ggml_vk_destroy_buffer(sync_staging);
  307. device.destroyCommandPool(compute_queue.pool);
  308. if (!single_queue) {
  309. device.destroyCommandPool(transfer_queue.pool);
  310. }
  311. for (auto& pipeline : pipelines) {
  312. if (pipeline.second.expired()) {
  313. continue;
  314. }
  315. vk_pipeline pl = pipeline.second.lock();
  316. ggml_vk_destroy_pipeline(device, pl);
  317. }
  318. pipelines.clear();
  319. device.destroy();
  320. }
  321. };
  322. struct vk_buffer_struct {
  323. vk::Buffer buffer = VK_NULL_HANDLE;
  324. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  325. vk::MemoryPropertyFlags memory_property_flags;
  326. void * ptr;
  327. size_t size = 0;
  328. vk_device device;
  329. ~vk_buffer_struct() {
  330. if (size == 0) {
  331. return;
  332. }
  333. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  334. device->device.freeMemory(device_memory);
  335. device->device.destroyBuffer(buffer);
  336. }
  337. };
  338. struct vk_subbuffer {
  339. vk_buffer buffer;
  340. uint64_t offset;
  341. uint64_t size;
  342. operator vk::DescriptorBufferInfo() const {
  343. return { buffer->buffer, offset, size };
  344. }
  345. };
  346. struct vk_semaphore {
  347. vk::Semaphore s;
  348. uint64_t value;
  349. };
  350. struct vk_submission {
  351. vk::CommandBuffer buffer;
  352. std::vector<vk_semaphore> wait_semaphores;
  353. std::vector<vk_semaphore> signal_semaphores;
  354. };
  355. typedef std::vector<vk_submission> vk_sequence;
  356. struct vk_mat_mat_push_constants {
  357. uint32_t M; uint32_t N; uint32_t K;
  358. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  359. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  360. uint32_t k_split;
  361. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  362. uint32_t padded_N;
  363. };
  364. struct vk_mat_vec_push_constants {
  365. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  366. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  367. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  368. };
  369. struct vk_mat_mat_id_push_constants {
  370. uint32_t M; uint32_t N; uint32_t K;
  371. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  372. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  373. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  374. uint32_t padded_N;
  375. };
  376. struct vk_mat_vec_id_push_constants {
  377. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  378. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  379. uint32_t nei0; uint32_t ne11;
  380. };
  381. struct vk_flash_attn_push_constants {
  382. uint32_t N;
  383. uint32_t KV;
  384. uint32_t ne1;
  385. uint32_t ne2;
  386. uint32_t ne3;
  387. uint32_t neq2;
  388. uint32_t neq3;
  389. uint32_t nek2;
  390. uint32_t nek3;
  391. uint32_t nev2;
  392. uint32_t nev3;
  393. uint32_t nem1;
  394. uint32_t nb01;
  395. uint32_t nb02;
  396. uint32_t nb03;
  397. uint32_t nb11;
  398. uint32_t nb12;
  399. uint32_t nb13;
  400. uint32_t nb21;
  401. uint32_t nb22;
  402. uint32_t nb23;
  403. uint32_t nb31;
  404. float scale;
  405. float max_bias;
  406. float logit_softcap;
  407. uint32_t mask;
  408. uint32_t n_head_log2;
  409. float m0;
  410. float m1;
  411. };
  412. struct vk_op_push_constants {
  413. uint32_t KX;
  414. uint32_t KY;
  415. float param1;
  416. float param2;
  417. };
  418. struct vk_op_unary_push_constants {
  419. uint32_t ne;
  420. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  421. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  422. uint32_t misalign_offsets;
  423. float param1; float param2;
  424. uint32_t ne0_012mp; uint32_t ne0_012L;
  425. uint32_t ne0_01mp; uint32_t ne0_01L;
  426. uint32_t ne0_0mp; uint32_t ne0_0L;
  427. uint32_t ne1_012mp; uint32_t ne1_012L;
  428. uint32_t ne1_01mp; uint32_t ne1_01L;
  429. uint32_t ne1_0mp; uint32_t ne1_0L;
  430. };
  431. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  432. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  433. // Precompute mp (m' in the paper) and L such that division
  434. // can be computed using a multiply (high 32b of 64b result)
  435. // and a shift:
  436. //
  437. // n/d = (mulhi(n, mp) + n) >> L;
  438. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  439. {
  440. // compute L = ceil(log2(d));
  441. L = 0;
  442. while (L < 32 && (uint32_t{1} << L) < d) {
  443. L++;
  444. }
  445. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  446. }
  447. template <typename T> void init_pushconst_fastdiv(T &p) {
  448. GGML_UNUSED(p);
  449. static_assert(!std::is_const<T>::value, "unexpected type");
  450. }
  451. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  452. // Compute magic values to divide by these six numbers.
  453. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  454. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  455. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  456. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  457. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  458. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  459. }
  460. struct vk_op_binary_push_constants {
  461. uint32_t ne;
  462. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  463. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  464. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  465. uint32_t misalign_offsets;
  466. float param1; float param2; int32_t param3;
  467. };
  468. struct vk_op_diag_mask_push_constants {
  469. uint32_t ncols;
  470. uint32_t rows_per_channel;
  471. int32_t n_past;
  472. };
  473. struct vk_op_rope_push_constants {
  474. uint32_t ncols;
  475. uint32_t n_dims;
  476. float freq_scale;
  477. uint32_t p_delta_rows;
  478. float freq_base;
  479. float ext_factor;
  480. float attn_factor;
  481. float corr_dims[2];
  482. float theta_scale;
  483. uint32_t has_ff;
  484. uint32_t ne02;
  485. uint32_t s1;
  486. uint32_t s2;
  487. int32_t sections[4];
  488. uint32_t is_back;
  489. };
  490. struct vk_op_soft_max_push_constants {
  491. uint32_t KX;
  492. uint32_t KY;
  493. float scale;
  494. float max_bias;
  495. float m0;
  496. float m1;
  497. uint32_t n_head_log2;
  498. uint32_t nrows_x;
  499. };
  500. struct vk_op_argsort_push_constants {
  501. uint32_t ncols;
  502. uint32_t ncols_pad;
  503. int32_t order;
  504. };
  505. struct vk_op_im2col_push_constants {
  506. uint32_t batch_offset; uint32_t offset_delta;
  507. uint32_t IC;
  508. uint32_t IW; uint32_t IH;
  509. uint32_t OW; uint32_t OH;
  510. uint32_t KW; uint32_t KH;
  511. uint32_t pelements;
  512. uint32_t CHW;
  513. int32_t s0; int32_t s1;
  514. int32_t p0; int32_t p1;
  515. int32_t d0; int32_t d1;
  516. };
  517. struct vk_op_timestep_embedding_push_constants {
  518. uint32_t nb1;
  519. uint32_t dim;
  520. uint32_t max_period;
  521. };
  522. struct vk_op_pool2d_push_constants {
  523. uint32_t IW; uint32_t IH;
  524. uint32_t OW; uint32_t OH;
  525. uint32_t OC;
  526. uint32_t pelements;
  527. uint32_t op;
  528. int32_t k0; int32_t k1;
  529. int32_t s0; int32_t s1;
  530. int32_t p0; int32_t p1;
  531. };
  532. struct vk_op_rwkv_wkv6_push_constants {
  533. uint32_t B;
  534. uint32_t T;
  535. uint32_t C;
  536. uint32_t H;
  537. };
  538. struct vk_op_rwkv_wkv7_push_constants {
  539. uint32_t B;
  540. uint32_t T;
  541. uint32_t C;
  542. uint32_t H;
  543. };
  544. // Allow pre-recording command buffers
  545. struct vk_staging_memcpy {
  546. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  547. void * dst;
  548. const void * src;
  549. size_t n;
  550. };
  551. struct vk_op_upscale_push_constants {
  552. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  553. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  554. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  555. float sf0; float sf1; float sf2; float sf3;
  556. };
  557. struct vk_context_struct {
  558. vk_submission * s;
  559. std::vector<vk_sequence> seqs;
  560. int exit_tensor_idx;
  561. std::vector<vk_staging_memcpy> in_memcpys;
  562. std::vector<vk_staging_memcpy> out_memcpys;
  563. vk_queue * q;
  564. };
  565. typedef std::shared_ptr<vk_context_struct> vk_context;
  566. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  567. struct ggml_vk_garbage_collector {
  568. std::vector<vk_semaphore> tl_semaphores;
  569. std::vector<vk_semaphore> semaphores;
  570. std::vector<vk::Event> events;
  571. std::vector<vk_buffer> temp_buffers;
  572. std::vector<vk_context> contexts;
  573. };
  574. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  575. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  576. static std::string format_size(size_t size) {
  577. const size_t kib = 1024;
  578. const size_t mib = kib * 1024;
  579. const size_t gib = mib * 1024;
  580. std::ostringstream oss;
  581. oss << std::fixed << std::setprecision(2);
  582. if (size >= gib) {
  583. oss << static_cast<double>(size) / gib << " GiB";
  584. } else if (size >= mib) {
  585. oss << static_cast<double>(size) / mib << " MiB";
  586. } else if (size >= kib) {
  587. oss << static_cast<double>(size) / kib << " KiB";
  588. } else {
  589. oss << size << " B";
  590. }
  591. return oss.str();
  592. }
  593. static std::mutex log_mutex;
  594. class vk_memory_logger {
  595. public:
  596. vk_memory_logger(): total_device(0), total_host(0) {}
  597. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  598. void log_deallocation(vk_buffer_ref buf_ref);
  599. private:
  600. std::map<vk::Buffer, size_t> allocations; // Track allocations
  601. size_t total_device;
  602. size_t total_host;
  603. };
  604. #else
  605. #define VK_LOG_MEMORY(msg) ((void) 0)
  606. #endif // GGML_VULKAN_MEMORY_DEBUG
  607. #if defined(GGML_VULKAN_PERF)
  608. class vk_perf_logger {
  609. public:
  610. void print_timings() {
  611. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  612. for (const auto& t : timings) {
  613. uint64_t total = 0;
  614. for (const auto& time : t.second) {
  615. total += time;
  616. }
  617. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " ms" << std::endl;
  618. }
  619. timings.clear();
  620. }
  621. void log_timing(const ggml_tensor * node, uint64_t time) {
  622. if (node->op == GGML_OP_UNARY) {
  623. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  624. return;
  625. }
  626. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  627. const uint64_t m = node->src[0]->ne[1];
  628. const uint64_t n = node->src[1]->ne[1];
  629. const uint64_t k = node->src[1]->ne[0];
  630. std::string name = ggml_op_name(node->op);
  631. if (n == 1) {
  632. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  633. } else {
  634. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  635. }
  636. timings[name].push_back(time);
  637. return;
  638. }
  639. timings[ggml_op_name(node->op)].push_back(time);
  640. }
  641. private:
  642. std::map<std::string, std::vector<uint64_t>> timings;
  643. };
  644. #endif // GGML_VULKAN_PERF
  645. struct ggml_backend_vk_context {
  646. std::string name;
  647. vk_device device;
  648. size_t semaphore_idx, event_idx;
  649. ggml_vk_garbage_collector gc;
  650. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  651. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  652. vk::Fence fence;
  653. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  654. vk_context_ref compute_ctx;
  655. vk_context_ref transfer_ctx;
  656. std::vector<vk_context_ref> tensor_ctxs;
  657. };
  658. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  659. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  660. if (tensor->view_src) {
  661. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  662. }
  663. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  664. }
  665. struct ggml_backend_vk_buffer_context {
  666. vk_device_ref device;
  667. vk_buffer dev_buffer;
  668. std::string name;
  669. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  670. device(device),
  671. dev_buffer(dev_buffer),
  672. name(name) {
  673. }
  674. ~ggml_backend_vk_buffer_context() {
  675. ggml_vk_destroy_buffer(dev_buffer);
  676. }
  677. };
  678. #ifdef GGML_VULKAN_MEMORY_DEBUG
  679. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  680. std::lock_guard<std::mutex> guard(log_mutex);
  681. vk_buffer buf = buf_ref.lock();
  682. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  683. const std::string type = device ? "device" : "host";
  684. allocations[buf->buffer] = size;
  685. total_device += device ? size : 0;
  686. total_host += device ? 0 : size;
  687. 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));
  688. }
  689. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  690. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  691. return;
  692. }
  693. std::lock_guard<std::mutex> guard(log_mutex);
  694. vk_buffer buf = buf_ref.lock();
  695. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  696. std::string type = device ? "device" : "host";
  697. auto it = allocations.find(buf->buffer);
  698. total_device -= device ? it->second : 0;
  699. total_host -= device ? 0 : it->second;
  700. if (it != allocations.end()) {
  701. 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));
  702. allocations.erase(it);
  703. } else {
  704. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  705. }
  706. }
  707. #endif // GGML_VULKAN_MEMORY_DEBUG
  708. struct vk_instance_t {
  709. vk::Instance instance;
  710. std::vector<size_t> device_indices;
  711. vk_device devices[GGML_VK_MAX_DEVICES];
  712. };
  713. static bool vk_instance_initialized = false;
  714. static vk_instance_t vk_instance;
  715. #ifdef GGML_VULKAN_CHECK_RESULTS
  716. static size_t vk_skip_checks;
  717. static size_t vk_output_tensor;
  718. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  719. static void ggml_vk_check_results_0(ggml_tensor * tensor);
  720. static void ggml_vk_check_results_1(ggml_tensor * tensor);
  721. #endif
  722. 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);
  723. static void ggml_backend_vk_free(ggml_backend_t backend);
  724. // variables to track number of compiles in progress
  725. static uint32_t compile_count = 0;
  726. static std::mutex compile_count_mutex;
  727. static std::condition_variable compile_count_cond;
  728. 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,
  729. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  730. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  731. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  732. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  733. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  734. GGML_ASSERT(parameter_count > 0);
  735. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  736. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  737. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  738. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  739. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  740. for (uint32_t i = 0; i < parameter_count; i++) {
  741. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  742. dsl_binding_flags.push_back({});
  743. }
  744. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  745. vk::PushConstantRange pcr(
  746. vk::ShaderStageFlagBits::eCompute,
  747. 0,
  748. pipeline->push_constant_size
  749. );
  750. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  751. {},
  752. dsl_binding);
  753. descriptor_set_layout_create_info.setPNext(&dslbfci);
  754. pipeline->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  755. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  756. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  757. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  758. pipeline->descriptor_set_idx = 0;
  759. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), pipeline->dsl, pcr);
  760. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  761. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  762. for (size_t i = 0; i < specialization_constants.size(); i++) {
  763. specialization_entries[i].constantID = i;
  764. specialization_entries[i].offset = i * sizeof(uint32_t);
  765. specialization_entries[i].size = sizeof(uint32_t);
  766. }
  767. vk::SpecializationInfo specialization_info(
  768. specialization_entries.size(),
  769. specialization_entries.data(),
  770. specialization_constants.size() * sizeof(uint32_t),
  771. specialization_constants.data()
  772. );
  773. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  774. if (device->subgroup_require_full_support && require_full_subgroups) {
  775. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  776. }
  777. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  778. pipeline_shader_stage_create_flags,
  779. vk::ShaderStageFlagBits::eCompute,
  780. pipeline->shader_module,
  781. entrypoint.c_str(),
  782. &specialization_info);
  783. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  784. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  785. if (device->subgroup_size_control && required_subgroup_size > 0) {
  786. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  787. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  788. }
  789. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  790. vk::PipelineCreateFlags{},
  791. pipeline_shader_create_info,
  792. pipeline->layout);
  793. vk::PipelineRobustnessCreateInfoEXT rci;
  794. if (device->pipeline_robustness && disable_robustness) {
  795. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  796. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  797. compute_pipeline_create_info.setPNext(&rci);
  798. }
  799. try {
  800. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  801. } catch (const vk::SystemError& e) {
  802. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  803. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  804. throw e;
  805. }
  806. pipeline->compiled = true;
  807. {
  808. std::lock_guard<std::mutex> guard(device->mutex);
  809. device->pipelines.insert({ pipeline->name, pipeline });
  810. }
  811. {
  812. std::lock_guard<std::mutex> guard(compile_count_mutex);
  813. assert(compile_count > 0);
  814. compile_count--;
  815. }
  816. compile_count_cond.notify_all();
  817. }
  818. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  819. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  820. for (auto& pool : pipeline->descriptor_pools) {
  821. device.destroyDescriptorPool(pool);
  822. }
  823. pipeline->descriptor_pools.clear();
  824. pipeline->descriptor_sets.clear();
  825. pipeline->descriptor_set_idx = 0;
  826. device.destroyDescriptorSetLayout(pipeline->dsl);
  827. device.destroyPipelineLayout(pipeline->layout);
  828. device.destroyShaderModule(pipeline->shader_module);
  829. device.destroyPipeline(pipeline->pipeline);
  830. }
  831. static void ggml_pipeline_request_descriptor_sets(vk_device& device, vk_pipeline& pipeline, uint32_t n) {
  832. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  833. device->pipeline_descriptor_set_requirements[pipeline->name] += n;
  834. if (!pipeline->compiled) {
  835. pipeline->needed = true;
  836. device->need_compiles = true;
  837. }
  838. }
  839. static void ggml_pipeline_allocate_descriptor_sets(vk_device& device) {
  840. std::lock_guard<std::mutex> guard(device->mutex);
  841. for (auto& pair : device->pipeline_descriptor_set_requirements) {
  842. vk_pipeline pipeline = device->pipelines.at(pair.first).lock();
  843. const uint64_t n = pair.second;
  844. VK_LOG_DEBUG("ggml_pipeline_allocate_descriptor_sets(" << pipeline->name << ", " << n << ")");
  845. if (pipeline->descriptor_sets.size() >= pipeline->descriptor_set_idx + n) {
  846. // Enough descriptors are available
  847. continue;
  848. }
  849. uint32_t to_alloc = pipeline->descriptor_set_idx + n - pipeline->descriptor_sets.size();
  850. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - pipeline->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  851. uint32_t pool_idx = pipeline->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  852. while (to_alloc > 0) {
  853. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  854. to_alloc -= alloc_count;
  855. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  856. if (pool_idx >= pipeline->descriptor_pools.size()) {
  857. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  858. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  859. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  860. }
  861. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  862. for (uint32_t i = 0; i < alloc_count; i++) {
  863. layouts[i] = pipeline->dsl;
  864. }
  865. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[pool_idx], alloc_count, layouts.data());
  866. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  867. pipeline->descriptor_sets.insert(pipeline->descriptor_sets.end(), sets.begin(), sets.end());
  868. pool_idx++;
  869. }
  870. }
  871. }
  872. static void ggml_pipeline_cleanup(vk_pipeline& pipeline) {
  873. VK_LOG_DEBUG("ggml_pipeline_cleanup(" << pipeline->name << ")");
  874. pipeline->descriptor_set_idx = 0;
  875. }
  876. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_queue& q) {
  877. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  878. std::lock_guard<std::mutex> guard(device->mutex);
  879. if (q.cmd_buffers.size() > q.cmd_buffer_idx) {
  880. // Reuse command buffer
  881. return q.cmd_buffers[q.cmd_buffer_idx++];
  882. }
  883. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  884. q.pool,
  885. vk::CommandBufferLevel::ePrimary,
  886. 1);
  887. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  888. auto buf = cmd_buffers.front();
  889. q.cmd_buffers.push_back(buf);
  890. q.cmd_buffer_idx++;
  891. return buf;
  892. }
  893. static vk_submission ggml_vk_create_submission(vk_device& device, vk_queue& q, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  894. VK_LOG_DEBUG("ggml_vk_create_submission()");
  895. vk_submission s;
  896. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  897. s.wait_semaphores = std::move(wait_semaphores);
  898. s.signal_semaphores = std::move(signal_semaphores);
  899. return s;
  900. }
  901. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  902. if (ctx->seqs.empty()) {
  903. if (fence) {
  904. ctx->q->queue.submit({}, fence);
  905. }
  906. return;
  907. }
  908. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  909. std::vector<std::vector<uint64_t>> tl_wait_vals;
  910. std::vector<std::vector<uint64_t>> tl_signal_vals;
  911. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  912. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  913. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  914. std::vector<vk::SubmitInfo> submit_infos;
  915. int idx = -1;
  916. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  917. size_t reserve = 0;
  918. for (const auto& sequence : ctx->seqs) {
  919. reserve += sequence.size();
  920. }
  921. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  922. tl_wait_semaphores.reserve(reserve);
  923. tl_wait_vals.reserve(reserve);
  924. tl_signal_semaphores.reserve(reserve);
  925. tl_signal_vals.reserve(reserve);
  926. tl_submit_infos.reserve(reserve);
  927. submit_infos.reserve(reserve);
  928. stage_flags.reserve(reserve);
  929. for (const auto& sequence : ctx->seqs) {
  930. for (const auto& submission : sequence) {
  931. stage_flags.push_back({});
  932. idx++;
  933. tl_wait_vals.push_back({});
  934. tl_wait_semaphores.push_back({});
  935. tl_signal_vals.push_back({});
  936. tl_signal_semaphores.push_back({});
  937. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  938. stage_flags[idx].push_back(ctx->q->stage_flags);
  939. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  940. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  941. }
  942. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  943. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  944. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  945. }
  946. tl_submit_infos.push_back({
  947. (uint32_t) submission.wait_semaphores.size(),
  948. tl_wait_vals[idx].data(),
  949. (uint32_t) submission.signal_semaphores.size(),
  950. tl_signal_vals[idx].data(),
  951. });
  952. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  953. tl_submit_infos[idx].pNext = nullptr;
  954. vk::SubmitInfo si{
  955. (uint32_t) submission.wait_semaphores.size(),
  956. tl_wait_semaphores[idx].data(),
  957. stage_flags[idx].data(),
  958. 1,
  959. &submission.buffer,
  960. (uint32_t) submission.signal_semaphores.size(),
  961. tl_signal_semaphores[idx].data(),
  962. };
  963. si.setPNext(&tl_submit_infos[idx]);
  964. submit_infos.push_back(si);
  965. }
  966. }
  967. ctx->q->queue.submit(submit_infos, fence);
  968. ctx->seqs.clear();
  969. }
  970. 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) {
  971. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  972. const uint32_t qfsize = queue_family_props.size();
  973. // Try with avoid preferences first
  974. for (uint32_t i = 0; i < qfsize; i++) {
  975. 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)) {
  976. return i;
  977. }
  978. }
  979. // Fall back to only required
  980. for (size_t i = 0; i < qfsize; i++) {
  981. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  982. return i;
  983. }
  984. }
  985. // Fall back to reusing compute queue
  986. for (size_t i = 0; i < qfsize; i++) {
  987. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  988. return i;
  989. }
  990. }
  991. // Fall back to ignoring min_num_queries
  992. for (size_t i = 0; i < qfsize; i++) {
  993. if (queue_family_props[i].queueFlags & required) {
  994. return i;
  995. }
  996. }
  997. // 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.
  998. // 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.
  999. if (compute_index >= 0) {
  1000. return compute_index;
  1001. }
  1002. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1003. for(auto &q_family : queue_family_props) {
  1004. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1005. }
  1006. abort();
  1007. }
  1008. 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) {
  1009. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1010. std::lock_guard<std::mutex> guard(device->mutex);
  1011. q.queue_family_index = queue_family_index;
  1012. q.transfer_only = transfer_only;
  1013. vk::CommandPoolCreateInfo command_pool_create_info_compute(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), queue_family_index);
  1014. q.pool = device->device.createCommandPool(command_pool_create_info_compute);
  1015. q.cmd_buffer_idx = 0;
  1016. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1017. q.stage_flags = stage_flags;
  1018. }
  1019. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_queue& q) {
  1020. vk_context result = std::make_shared<vk_context_struct>();
  1021. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1022. ctx->gc.contexts.emplace_back(result);
  1023. result->q = &q;
  1024. return result;
  1025. }
  1026. static vk_context ggml_vk_create_temporary_context(vk_queue& q) {
  1027. vk_context result = std::make_shared<vk_context_struct>();
  1028. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1029. result->q = &q;
  1030. return result;
  1031. }
  1032. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1033. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1034. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1035. vk::SemaphoreCreateInfo ci{};
  1036. ci.setPNext(&tci);
  1037. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1038. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1039. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1040. }
  1041. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1042. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1043. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1044. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1045. vk::SemaphoreCreateInfo ci{};
  1046. ci.setPNext(&tci);
  1047. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1048. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1049. }
  1050. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1051. }
  1052. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1053. if (ctx->event_idx >= ctx->gc.events.size()) {
  1054. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1055. }
  1056. return ctx->gc.events[ctx->event_idx++];
  1057. }
  1058. static void ggml_vk_queue_cleanup(vk_device& device, vk_queue& q) {
  1059. VK_LOG_DEBUG("ggml_vk_queue_cleanup()");
  1060. std::lock_guard<std::mutex> guard(device->mutex);
  1061. // Requires command buffers to be done
  1062. device->device.resetCommandPool(q.pool);
  1063. q.cmd_buffer_idx = 0;
  1064. }
  1065. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1066. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1067. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1068. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1069. (flags & memory_type.propertyFlags) == flags &&
  1070. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1071. return static_cast<int32_t>(i);
  1072. }
  1073. }
  1074. return UINT32_MAX;
  1075. }
  1076. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1077. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1078. if (size > device->max_memory_allocation_size) {
  1079. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1080. }
  1081. std::lock_guard<std::mutex> guard(device->mutex);
  1082. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1083. if (size == 0) {
  1084. buf->size = 0;
  1085. return buf;
  1086. }
  1087. vk::BufferCreateInfo buffer_create_info{
  1088. vk::BufferCreateFlags(),
  1089. size,
  1090. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1091. vk::SharingMode::eExclusive,
  1092. 0,
  1093. nullptr,
  1094. };
  1095. buf->buffer = device->device.createBuffer(buffer_create_info);
  1096. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1097. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1098. uint32_t memory_type_index = UINT32_MAX;
  1099. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1100. buf->memory_property_flags = req_flags;
  1101. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1102. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1103. buf->memory_property_flags = fallback_flags;
  1104. }
  1105. if (memory_type_index == UINT32_MAX) {
  1106. device->device.destroyBuffer(buf->buffer);
  1107. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1108. }
  1109. try {
  1110. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1111. } catch (const vk::SystemError& e) {
  1112. if (buf->memory_property_flags != fallback_flags) {
  1113. // Try again with fallback flags
  1114. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1115. buf->memory_property_flags = fallback_flags;
  1116. try {
  1117. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1118. }
  1119. catch (const vk::SystemError& e) {
  1120. device->device.destroyBuffer(buf->buffer);
  1121. throw e;
  1122. }
  1123. } else {
  1124. // Out of Host/Device memory, clean up buffer
  1125. device->device.destroyBuffer(buf->buffer);
  1126. throw e;
  1127. }
  1128. }
  1129. buf->ptr = nullptr;
  1130. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1131. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1132. }
  1133. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1134. buf->device = device;
  1135. buf->size = size;
  1136. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1137. device->memory_logger->log_allocation(buf, size);
  1138. #endif
  1139. return buf;
  1140. }
  1141. 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)) {
  1142. try {
  1143. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1144. } catch (const vk::SystemError& e) {
  1145. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1146. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1147. throw e;
  1148. }
  1149. }
  1150. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1151. vk_buffer buf;
  1152. try {
  1153. if (device->prefer_host_memory) {
  1154. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1155. } else if (device->uma) {
  1156. // Fall back to host memory type
  1157. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1158. } else {
  1159. // use rebar if available, otherwise fallback to device only visible memory
  1160. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1161. }
  1162. } catch (const vk::SystemError& e) {
  1163. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1164. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1165. throw e;
  1166. }
  1167. return buf;
  1168. }
  1169. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1170. if (buf == nullptr) {
  1171. return;
  1172. }
  1173. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1174. if (buf->device != nullptr) {
  1175. buf->device->memory_logger->log_deallocation(buf);
  1176. }
  1177. #endif
  1178. buf.reset();
  1179. }
  1180. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1181. return { buf, 0, VK_WHOLE_SIZE };
  1182. }
  1183. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1184. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1185. const bool transfer_queue = ctx->q->transfer_only;
  1186. ctx->s->buffer.pipelineBarrier(
  1187. ctx->q->stage_flags,
  1188. ctx->q->stage_flags,
  1189. {},
  1190. { {
  1191. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1192. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1193. } },
  1194. {},
  1195. {}
  1196. );
  1197. }
  1198. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1199. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1200. if (events.empty()) {
  1201. return;
  1202. }
  1203. ctx->s->buffer.waitEvents(
  1204. events,
  1205. ctx->q->stage_flags,
  1206. ctx->q->stage_flags,
  1207. {},
  1208. {},
  1209. {}
  1210. );
  1211. }
  1212. // number of rows/cols for flash attention shader
  1213. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1214. static std::array<uint32_t, 2> fa_rows_cols(uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) {
  1215. GGML_UNUSED(clamp);
  1216. // small rows, large cols
  1217. if (small_rows) {
  1218. return {flash_attention_num_small_rows, 128};
  1219. }
  1220. // small cols to reduce register count
  1221. if (ggml_is_quantized(type) || D == 256) {
  1222. return {64, 32};
  1223. }
  1224. return {64, 64};
  1225. };
  1226. 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) {
  1227. uint32_t lut_size = 0;
  1228. switch (src0_type) {
  1229. case GGML_TYPE_IQ1_S:
  1230. case GGML_TYPE_IQ1_M:
  1231. lut_size = 2*2048;
  1232. break;
  1233. case GGML_TYPE_IQ2_XXS:
  1234. lut_size = 8*256;
  1235. break;
  1236. case GGML_TYPE_IQ2_XS:
  1237. lut_size = 8*512;
  1238. break;
  1239. case GGML_TYPE_IQ2_S:
  1240. lut_size = 8*1024;
  1241. break;
  1242. case GGML_TYPE_IQ3_XXS:
  1243. lut_size = 4*256;
  1244. break;
  1245. case GGML_TYPE_IQ3_S:
  1246. lut_size = 4*512;
  1247. break;
  1248. case GGML_TYPE_IQ4_NL:
  1249. case GGML_TYPE_IQ4_XS:
  1250. lut_size = 4*16;
  1251. break;
  1252. default:
  1253. break;
  1254. }
  1255. // Needs to be kept up to date on shader changes
  1256. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1257. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1258. const uint32_t warps = warptile[0] / warptile[10];
  1259. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1260. const uint32_t mmid_row_ids = mul_mat_id ? 3072 * sizeof(uint32_t) : 0;
  1261. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1262. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1263. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1264. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1265. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1266. return supported;
  1267. }
  1268. struct GpuPipelineConfig {
  1269. // GPU architecture identifier.
  1270. // Example: vk_device_architecture::AMD_GCN
  1271. vk_device_architecture arch;
  1272. // Mapping of pipeline names to their specific subgroup sizes.
  1273. // Example: {"soft_max_f32", 64}
  1274. std::unordered_map<std::string, uint32_t> pipelines;
  1275. // Default subgroup size for this GPU.
  1276. // Defaults to 0 if not explicitly provided.
  1277. uint32_t default_subgroup_size = 0;
  1278. };
  1279. // Pipeline configuration for RDNA1 GPUs.
  1280. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1281. {"soft_max", 64}, {"im2col", 64},
  1282. {"argmax", 64}, {"mul_mat_vec", 64},
  1283. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1284. };
  1285. // Pipeline configuration for RDNA2 GPUs.
  1286. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1287. {"soft_max", 64}, {"im2col", 64},
  1288. };
  1289. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1290. // Define configurations for different GPUs.
  1291. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1292. {
  1293. vk_device_architecture::AMD_RDNA1,
  1294. {
  1295. rdna1_pipelines,
  1296. },
  1297. RDNA_DEFAULT_SUBGROUP_SIZE
  1298. },
  1299. {
  1300. vk_device_architecture::AMD_RDNA2,
  1301. {
  1302. rdna2_pipelines,
  1303. },
  1304. RDNA_DEFAULT_SUBGROUP_SIZE
  1305. },
  1306. };
  1307. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1308. for (const auto &config : gpu_pipeline_configs) {
  1309. if (config.arch == arch) {
  1310. auto pipIt = config.pipelines.find(pipeline_name);
  1311. if (pipIt != config.pipelines.end()) {
  1312. return pipIt->second;
  1313. }
  1314. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1315. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1316. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1317. for (const auto &entry : sorted_pipelines) {
  1318. if (pipeline_name.find(entry.first) != std::string::npos) {
  1319. return entry.second;
  1320. }
  1321. }
  1322. return config.default_subgroup_size;
  1323. }
  1324. }
  1325. return 0; // If no matching configuration is found
  1326. }
  1327. static void ggml_vk_load_shaders(vk_device& device) {
  1328. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1329. // some shaders have a minimum subgroup size
  1330. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1331. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1332. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1333. // mulmat
  1334. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1335. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1336. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1337. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1338. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1339. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1340. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1341. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1342. uint32_t l_align, m_align, s_align;
  1343. if (device->coopmat2) {
  1344. // spec constants and tile sizes for non-quant matmul/matmul_id
  1345. l_warptile = { 256, 128, 256, 64, 1 };
  1346. m_warptile = { 256, 128, 128, 64, 0 };
  1347. s_warptile = { 128, 64, 64, 64, 0 };
  1348. l_wg_denoms = {128, 256, 1 };
  1349. m_wg_denoms = {128, 128, 1 };
  1350. s_wg_denoms = { 64, 64, 1 };
  1351. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1352. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1353. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1354. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1355. l_mmq_wg_denoms = { 128, 256, 1 };
  1356. m_mmq_wg_denoms = { 128, 128, 1 };
  1357. s_mmq_wg_denoms = { 32, 64, 1 };
  1358. // spec constants and tile sizes for quant matmul (Qi_K)
  1359. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1360. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1361. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1362. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1363. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1364. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1365. // spec constants and tile sizes for quant matmul_id
  1366. l_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1367. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1368. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1369. l_mmqid_wg_denoms = { 128, 64, 1 };
  1370. m_mmqid_wg_denoms = { 128, 64, 1 };
  1371. s_mmqid_wg_denoms = { 128, 64, 1 };
  1372. l_align = 128;
  1373. m_align = 64;
  1374. s_align = 32;
  1375. } else {
  1376. // Matrix cores require different warp group sizes
  1377. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1378. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1379. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1380. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1381. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1382. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1383. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1384. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1385. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1386. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1387. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1388. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1389. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1390. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1391. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1392. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1393. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1394. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1395. l_align = 128;
  1396. m_align = 64;
  1397. s_align = 32;
  1398. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1399. ggml_type t = (ggml_type)i;
  1400. // Disable medium and large matrix multiplication if not enough shared memory is available
  1401. // Check mmq warptiles as the largest configuration
  1402. // Throw an error if not enough for any matrix multiplication is available
  1403. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1404. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1405. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1406. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1407. device->mul_mat_m[i] = false;
  1408. device->mul_mat_l[i] = false;
  1409. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1410. device->mul_mat_l[i] = false;
  1411. }
  1412. // Disable mul_mat_id if not enough shared memory is available
  1413. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1414. device->mul_mat_id_s[i] = false;
  1415. device->mul_mat_id_m[i] = false;
  1416. device->mul_mat_id_l[i] = false;
  1417. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1418. device->mul_mat_id_m[i] = false;
  1419. device->mul_mat_id_l[i] = false;
  1420. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1421. device->mul_mat_id_l[i] = false;
  1422. }
  1423. }
  1424. }
  1425. if (!device->pipeline_matmul_f32) {
  1426. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1427. }
  1428. if (!device->pipeline_matmul_f32_f16) {
  1429. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1430. }
  1431. if (!device->pipeline_matmul_id_f32) {
  1432. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1433. }
  1434. std::vector<std::future<void>> compiles;
  1435. 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,
  1436. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1437. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1438. if (!require_full_subgroups && required_subgroup_size == 0) {
  1439. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1440. }
  1441. if (!pipeline) {
  1442. pipeline = std::make_shared<vk_pipeline_struct>();
  1443. pipeline->name = name;
  1444. pipeline->parameter_count = parameter_count;
  1445. pipeline->push_constant_size = push_constant_size;
  1446. pipeline->wg_denoms = wg_denoms;
  1447. pipeline->align = align;
  1448. }
  1449. if (!pipeline->needed || pipeline->compiled) {
  1450. return;
  1451. }
  1452. {
  1453. // wait until fewer than N compiles are in progress
  1454. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1455. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1456. while (compile_count >= N) {
  1457. compile_count_cond.wait(guard);
  1458. }
  1459. compile_count++;
  1460. }
  1461. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1462. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1463. };
  1464. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1465. if (device->coopmat2) {
  1466. auto const &fa_wg_denoms = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  1467. return {fa_rows_cols(D, clamp, type, small_rows)[0], 1, 1};
  1468. };
  1469. auto const &fa_spec_constants = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  1470. // For large number of rows, 128 invocations seems to work best.
  1471. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1472. // can't use 256 for D==80.
  1473. uint32_t wg_size = (small_rows && (D % 32) == 0) ? 256 : 128;
  1474. auto rows_cols = fa_rows_cols(D, clamp, type, small_rows);
  1475. return {wg_size, rows_cols[0], rows_cols[1], (D), clamp};
  1476. };
  1477. #define CREATE_FA2(TYPE, NAMELC, D) \
  1478. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][0], "flash_attn_f32_f16_D" #D "_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1479. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1480. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][0], "flash_attn_f32_f16_D" #D "_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1481. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1482. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][0], "flash_attn_f32_f16_D" #D "_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1483. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1484. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][0], "flash_attn_f32_f16_D" #D "_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1485. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1486. #define CREATE_FA(TYPE, NAMELC) \
  1487. CREATE_FA2(TYPE, NAMELC, 64) \
  1488. CREATE_FA2(TYPE, NAMELC, 80) \
  1489. CREATE_FA2(TYPE, NAMELC, 96) \
  1490. CREATE_FA2(TYPE, NAMELC, 112) \
  1491. CREATE_FA2(TYPE, NAMELC, 128) \
  1492. CREATE_FA2(TYPE, NAMELC, 256)
  1493. CREATE_FA(GGML_TYPE_F16, f16)
  1494. CREATE_FA(GGML_TYPE_Q4_0, q4_0)
  1495. CREATE_FA(GGML_TYPE_Q4_1, q4_1)
  1496. CREATE_FA(GGML_TYPE_Q5_0, q5_0)
  1497. CREATE_FA(GGML_TYPE_Q5_1, q5_1)
  1498. CREATE_FA(GGML_TYPE_Q8_0, q8_0)
  1499. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  1500. //CREATE_FA(GGML_TYPE_Q2_K, q2_k)
  1501. //CREATE_FA(GGML_TYPE_Q3_K, q3_k)
  1502. //CREATE_FA(GGML_TYPE_Q4_K, q4_k)
  1503. //CREATE_FA(GGML_TYPE_Q5_K, q5_k)
  1504. //CREATE_FA(GGML_TYPE_Q6_K, q6_k)
  1505. //CREATE_FA(GGML_TYPE_IQ1_S, iq1_s)
  1506. //CREATE_FA(GGML_TYPE_IQ1_M, iq1_m)
  1507. //CREATE_FA(GGML_TYPE_IQ2_XXS, iq2_xxs)
  1508. //CREATE_FA(GGML_TYPE_IQ2_XS, iq2_xs)
  1509. //CREATE_FA(GGML_TYPE_IQ2_S, iq2_s)
  1510. //CREATE_FA(GGML_TYPE_IQ3_XXS, iq3_xxs)
  1511. //CREATE_FA(GGML_TYPE_IQ3_S, iq3_s)
  1512. //CREATE_FA(GGML_TYPE_IQ4_XS, iq4_xs)
  1513. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl)
  1514. #undef CREATE_FA
  1515. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1516. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1517. 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); \
  1518. 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); \
  1519. 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); \
  1520. 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); \
  1521. 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); \
  1522. 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); \
  1523. // Create 2 variants, {f16,f32} accumulator
  1524. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1525. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1526. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1527. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1528. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1529. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1530. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1531. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1532. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1533. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1534. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1535. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1536. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1537. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1538. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1539. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1540. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1541. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1542. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1543. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1544. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1545. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1546. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1547. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1548. 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)
  1549. 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)
  1550. 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)
  1551. 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)
  1552. 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)
  1553. 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)
  1554. 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)
  1555. 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)
  1556. 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)
  1557. 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)
  1558. 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)
  1559. 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)
  1560. 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)
  1561. 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)
  1562. 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)
  1563. 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)
  1564. 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)
  1565. 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)
  1566. 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)
  1567. #undef CREATE_MM
  1568. #undef CREATE_MM2
  1569. } else
  1570. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1571. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1572. if (device->coopmat_support) {
  1573. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1574. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1575. if (device->mul_mat ## ID ## _l[TYPE]) \
  1576. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  1577. if (device->mul_mat ## ID ## _m[TYPE]) \
  1578. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  1579. if (device->mul_mat ## ID ## _s[TYPE]) \
  1580. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  1581. if (device->mul_mat ## ID ## _l[TYPE]) \
  1582. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  1583. if (device->mul_mat ## ID ## _m[TYPE]) \
  1584. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  1585. if (device->mul_mat ## ID ## _s[TYPE]) \
  1586. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  1587. // Create 2 variants, {f16,f32} accumulator
  1588. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1589. if (device->coopmat_acc_f16_support) { \
  1590. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1591. } \
  1592. if (device->coopmat_acc_f32_support) { \
  1593. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1594. } \
  1595. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1596. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1597. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1598. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1599. if (device->coopmat_acc_f16_support) {
  1600. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1601. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1602. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1603. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1604. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1605. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1606. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1607. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1608. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1609. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1610. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1611. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1612. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1613. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1614. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1615. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1616. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1617. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1618. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1619. } else {
  1620. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1621. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1622. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1623. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1624. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1625. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1626. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1627. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1628. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1629. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1630. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1631. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1632. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1633. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1634. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1635. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1636. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1637. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1638. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1639. }
  1640. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1641. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1642. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1643. if (device->coopmat_acc_f16_support) {
  1644. 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);
  1645. 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);
  1646. 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);
  1647. 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);
  1648. 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);
  1649. 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);
  1650. 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);
  1651. 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);
  1652. 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);
  1653. 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);
  1654. 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);
  1655. 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);
  1656. 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);
  1657. 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);
  1658. 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);
  1659. 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);
  1660. 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);
  1661. 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);
  1662. 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);
  1663. } else {
  1664. 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);
  1665. 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);
  1666. 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);
  1667. 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);
  1668. 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);
  1669. 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);
  1670. 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);
  1671. 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);
  1672. 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);
  1673. 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);
  1674. 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);
  1675. 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);
  1676. 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);
  1677. 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);
  1678. 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);
  1679. 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);
  1680. 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);
  1681. 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);
  1682. 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);
  1683. }
  1684. #undef CREATE_MM2
  1685. #undef CREATE_MM
  1686. } else
  1687. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1688. if (device->fp16) {
  1689. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1690. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1691. if (device->mul_mat ## ID ## _l[TYPE]) \
  1692. 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); \
  1693. if (device->mul_mat ## ID ## _m[TYPE]) \
  1694. 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); \
  1695. if (device->mul_mat ## ID ## _s[TYPE]) \
  1696. 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); \
  1697. if (device->mul_mat ## ID ## _l[TYPE]) \
  1698. 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); \
  1699. if (device->mul_mat ## ID ## _m[TYPE]) \
  1700. 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); \
  1701. if (device->mul_mat ## ID ## _s[TYPE]) \
  1702. 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); \
  1703. // Create 2 variants, {f16,f32} accumulator
  1704. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1705. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1706. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1707. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1708. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1709. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1710. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1711. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1712. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1713. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1714. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1715. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1716. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1717. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1718. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1719. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1720. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1721. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1722. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1723. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1724. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1725. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1726. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1727. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1728. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1729. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1730. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1731. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1732. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1733. 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);
  1734. 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);
  1735. 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);
  1736. 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);
  1737. 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);
  1738. 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);
  1739. 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);
  1740. 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);
  1741. 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);
  1742. 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);
  1743. 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);
  1744. 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);
  1745. 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);
  1746. 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);
  1747. 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);
  1748. 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);
  1749. 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);
  1750. 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);
  1751. 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);
  1752. #undef CREATE_MM2
  1753. #undef CREATE_MM
  1754. } else {
  1755. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1756. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1757. if (device->mul_mat ## ID ## _l[TYPE]) \
  1758. 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); \
  1759. if (device->mul_mat ## ID ## _m[TYPE]) \
  1760. 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); \
  1761. if (device->mul_mat ## ID ## _s[TYPE]) \
  1762. 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); \
  1763. if (device->mul_mat ## ID ## _l[TYPE]) \
  1764. 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); \
  1765. if (device->mul_mat ## ID ## _m[TYPE]) \
  1766. 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); \
  1767. if (device->mul_mat ## ID ## _s[TYPE]) \
  1768. 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); \
  1769. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1770. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1771. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1772. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1773. 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, );
  1774. 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, );
  1775. 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, );
  1776. 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, );
  1777. 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, );
  1778. 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, );
  1779. 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, );
  1780. 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, );
  1781. 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, );
  1782. 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, );
  1783. 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, );
  1784. 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, );
  1785. 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, );
  1786. 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, );
  1787. 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, );
  1788. 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, );
  1789. 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, );
  1790. 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, );
  1791. 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, );
  1792. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1793. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1794. 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);
  1795. 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);
  1796. 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);
  1797. 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);
  1798. 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);
  1799. 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);
  1800. 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);
  1801. 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);
  1802. 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);
  1803. 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);
  1804. 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);
  1805. 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);
  1806. 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);
  1807. 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);
  1808. 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);
  1809. 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);
  1810. 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);
  1811. 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);
  1812. 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);
  1813. 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);
  1814. #undef CREATE_MM
  1815. }
  1816. // mul mat vec
  1817. // the number of rows computed per shader depends on GPU model and quant
  1818. uint32_t rm_stdq = 1;
  1819. uint32_t rm_kq = 2;
  1820. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  1821. if (device->subgroup_min_size == 64 && device->subgroup_max_size == 64) { // GCN
  1822. rm_stdq = 2;
  1823. rm_kq = 4;
  1824. }
  1825. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  1826. rm_stdq = 2;
  1827. uint32_t rm_iq = 2 * rm_kq;
  1828. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  1829. 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);
  1830. 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);
  1831. 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);
  1832. 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);
  1833. 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);
  1834. 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);
  1835. 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);
  1836. 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);
  1837. 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);
  1838. 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);
  1839. 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);
  1840. 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);
  1841. 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);
  1842. 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);
  1843. 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);
  1844. 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);
  1845. 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);
  1846. 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);
  1847. 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);
  1848. 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);
  1849. 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);
  1850. 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);
  1851. 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);
  1852. 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);
  1853. 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);
  1854. 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);
  1855. 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);
  1856. 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);
  1857. 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);
  1858. 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);
  1859. 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);
  1860. 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);
  1861. 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);
  1862. 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);
  1863. 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);
  1864. 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);
  1865. 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);
  1866. 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);
  1867. 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);
  1868. 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);
  1869. 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);
  1870. 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);
  1871. }
  1872. 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);
  1873. 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);
  1874. 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);
  1875. 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);
  1876. 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);
  1877. 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);
  1878. 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);
  1879. 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);
  1880. 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);
  1881. 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);
  1882. 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);
  1883. 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);
  1884. 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);
  1885. 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);
  1886. 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);
  1887. 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);
  1888. 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);
  1889. 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);
  1890. 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);
  1891. 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);
  1892. 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);
  1893. // dequant shaders
  1894. 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);
  1895. 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);
  1896. 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);
  1897. 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);
  1898. 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);
  1899. 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);
  1900. 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);
  1901. 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);
  1902. 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);
  1903. 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);
  1904. 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);
  1905. 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);
  1906. 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);
  1907. 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);
  1908. 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);
  1909. 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);
  1910. 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);
  1911. 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);
  1912. 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);
  1913. 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);
  1914. // get_rows
  1915. 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);
  1916. 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);
  1917. 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);
  1918. 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);
  1919. 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);
  1920. 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);
  1921. 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);
  1922. 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);
  1923. 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);
  1924. 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);
  1925. 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);
  1926. 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);
  1927. 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);
  1928. 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);
  1929. 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);
  1930. 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);
  1931. 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);
  1932. 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);
  1933. 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);
  1934. 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);
  1935. 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);
  1936. 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);
  1937. 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);
  1938. 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);
  1939. 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);
  1940. 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);
  1941. 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);
  1942. 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);
  1943. 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);
  1944. 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);
  1945. 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);
  1946. 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);
  1947. 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);
  1948. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32, "mul_mat_vec_p021_f16_f32", mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  1949. 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, 7 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  1950. 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);
  1951. 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);
  1952. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1953. 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);
  1954. 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);
  1955. 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);
  1956. 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);
  1957. 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);
  1958. 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);
  1959. 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);
  1960. 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);
  1961. 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);
  1962. 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);
  1963. 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);
  1964. 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);
  1965. 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);
  1966. 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);
  1967. 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);
  1968. 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);
  1969. 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);
  1970. 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);
  1971. 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);
  1972. 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);
  1973. ggml_vk_create_pipeline(device, device->pipeline_add_f32, "add_f32", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1974. ggml_vk_create_pipeline(device, device->pipeline_add_f32_norepeat, "add_f32_norepeat", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1975. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16, "add_f16_f32_f16", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1976. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16_norepeat, "add_f16_f32_f16_norepeat", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1977. 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);
  1978. ggml_vk_create_pipeline(device, device->pipeline_sub_f32, "sub_f32", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1979. ggml_vk_create_pipeline(device, device->pipeline_sub_f32_norepeat, "sub_f32_norepeat", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1980. ggml_vk_create_pipeline(device, device->pipeline_mul_f32, "mul_f32", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1981. ggml_vk_create_pipeline(device, device->pipeline_mul_f32_norepeat, "mul_f32_norepeat", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1982. ggml_vk_create_pipeline(device, device->pipeline_div_f32, "div_f32", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  1983. ggml_vk_create_pipeline(device, device->pipeline_div_f32_norepeat, "div_f32_norepeat", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  1984. 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);
  1985. 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);
  1986. 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);
  1987. 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);
  1988. 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);
  1989. 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);
  1990. 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);
  1991. 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);
  1992. 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);
  1993. 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);
  1994. 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);
  1995. 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);
  1996. ggml_vk_create_pipeline(device, device->pipeline_gelu_f32, "gelu_f32", gelu_f32_len, gelu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1997. ggml_vk_create_pipeline(device, device->pipeline_gelu_quick_f32, "gelu_quick_f32", gelu_quick_f32_len, gelu_quick_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1998. ggml_vk_create_pipeline(device, device->pipeline_silu_f32, "silu_f32", silu_f32_len, silu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1999. 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);
  2000. ggml_vk_create_pipeline(device, device->pipeline_relu_f32, "relu_f32", relu_f32_len, relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2001. 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);
  2002. ggml_vk_create_pipeline(device, device->pipeline_tanh_f32, "tanh_f32", tanh_f32_len, tanh_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2003. ggml_vk_create_pipeline(device, device->pipeline_sigmoid_f32, "sigmoid_f32", sigmoid_f32_len, sigmoid_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2004. 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);
  2005. 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);
  2006. 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);
  2007. 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);
  2008. 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);
  2009. 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);
  2010. 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);
  2011. 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);
  2012. 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);
  2013. 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);
  2014. if (device->float_controls_rte_fp16) {
  2015. 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);
  2016. 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);
  2017. 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);
  2018. 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);
  2019. } else {
  2020. 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);
  2021. 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);
  2022. 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);
  2023. 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);
  2024. }
  2025. 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);
  2026. 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);
  2027. 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);
  2028. 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);
  2029. 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);
  2030. if (device->float_controls_rte_fp16) {
  2031. 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);
  2032. } else {
  2033. 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);
  2034. }
  2035. 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);
  2036. 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);
  2037. 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);
  2038. 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);
  2039. 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);
  2040. for (auto &c : compiles) {
  2041. c.wait();
  2042. }
  2043. device->need_compiles = false;
  2044. }
  2045. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2046. static vk_device ggml_vk_get_device(size_t idx) {
  2047. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2048. if (vk_instance.devices[idx] == nullptr) {
  2049. VK_LOG_DEBUG("Initializing new vk_device");
  2050. vk_device device = std::make_shared<vk_device_struct>();
  2051. vk_instance.devices[idx] = device;
  2052. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2053. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2054. #endif
  2055. #ifdef GGML_VULKAN_PERF
  2056. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2057. #endif
  2058. size_t dev_num = vk_instance.device_indices[idx];
  2059. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2060. if (dev_num >= physical_devices.size()) {
  2061. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2062. throw std::runtime_error("Device not found");
  2063. }
  2064. device->physical_device = physical_devices[dev_num];
  2065. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2066. device->architecture = get_device_architecture(device->physical_device);
  2067. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2068. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2069. bool fp16_storage = false;
  2070. bool fp16_compute = false;
  2071. bool maintenance4_support = false;
  2072. bool sm_builtins = false;
  2073. bool amd_shader_core_properties2 = false;
  2074. bool pipeline_robustness = false;
  2075. bool coopmat2_support = false;
  2076. device->coopmat_support = false;
  2077. for (const auto& properties : ext_props) {
  2078. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2079. maintenance4_support = true;
  2080. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2081. fp16_storage = true;
  2082. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2083. fp16_compute = true;
  2084. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2085. sm_builtins = true;
  2086. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2087. amd_shader_core_properties2 = true;
  2088. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2089. pipeline_robustness = true;
  2090. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2091. device->subgroup_size_control = true;
  2092. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2093. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2094. device->coopmat_support = true;
  2095. device->coopmat_m = 0;
  2096. device->coopmat_n = 0;
  2097. device->coopmat_k = 0;
  2098. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2099. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2100. coopmat2_support = true;
  2101. }
  2102. }
  2103. vk::PhysicalDeviceProperties2 props2;
  2104. vk::PhysicalDeviceMaintenance3Properties props3;
  2105. vk::PhysicalDeviceMaintenance4Properties props4;
  2106. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2107. vk::PhysicalDeviceDriverProperties driver_props;
  2108. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2109. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2110. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2111. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2112. props2.pNext = &props3;
  2113. props3.pNext = &subgroup_props;
  2114. subgroup_props.pNext = &driver_props;
  2115. driver_props.pNext = &vk12_props;
  2116. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2117. if (maintenance4_support) {
  2118. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2119. last_struct = (VkBaseOutStructure *)&props4;
  2120. }
  2121. if (sm_builtins) {
  2122. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2123. last_struct = (VkBaseOutStructure *)&sm_props;
  2124. }
  2125. if (amd_shader_core_properties2) {
  2126. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2127. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2128. }
  2129. if (device->subgroup_size_control) {
  2130. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2131. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2132. }
  2133. #if defined(VK_NV_cooperative_matrix2)
  2134. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2135. if (coopmat2_support) {
  2136. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2137. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2138. }
  2139. #endif
  2140. device->physical_device.getProperties2(&props2);
  2141. device->properties = props2.properties;
  2142. device->vendor_id = device->properties.vendorID;
  2143. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2144. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2145. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2146. } else if (maintenance4_support) {
  2147. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2148. } else {
  2149. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2150. }
  2151. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2152. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2153. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2154. #if defined(_WIN32)
  2155. } else if (device->vendor_id == VK_VENDOR_ID_NVIDIA) {
  2156. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2157. device->suballocation_block_size = 1024*1024*1024;
  2158. #endif
  2159. } else {
  2160. device->suballocation_block_size = device->max_memory_allocation_size;
  2161. }
  2162. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2163. device->subgroup_size = subgroup_props.subgroupSize;
  2164. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2165. if (sm_builtins) {
  2166. device->shader_core_count = sm_props.shaderSMCount;
  2167. } else if (amd_shader_core_properties2) {
  2168. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2169. } else {
  2170. device->shader_core_count = 0;
  2171. }
  2172. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2173. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2174. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2175. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2176. device->coopmat_support = false;
  2177. }
  2178. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2179. // Try to find a non-graphics compute queue and transfer-focused queues
  2180. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2181. 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);
  2182. const float priorities[] = { 1.0f, 1.0f };
  2183. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2184. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2185. if (compute_queue_family_index != transfer_queue_family_index) {
  2186. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2187. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2188. } else if(!device->single_queue) {
  2189. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2190. } else {
  2191. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2192. }
  2193. vk::DeviceCreateInfo device_create_info;
  2194. std::vector<const char *> device_extensions;
  2195. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2196. VkPhysicalDeviceFeatures2 device_features2;
  2197. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2198. device_features2.pNext = nullptr;
  2199. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2200. VkPhysicalDeviceVulkan11Features vk11_features;
  2201. vk11_features.pNext = nullptr;
  2202. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2203. device_features2.pNext = &vk11_features;
  2204. VkPhysicalDeviceVulkan12Features vk12_features;
  2205. vk12_features.pNext = nullptr;
  2206. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2207. vk11_features.pNext = &vk12_features;
  2208. last_struct = (VkBaseOutStructure *)&vk12_features;
  2209. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2210. pl_robustness_features.pNext = nullptr;
  2211. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2212. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2213. if (pipeline_robustness) {
  2214. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2215. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2216. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2217. }
  2218. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2219. subgroup_size_control_features.pNext = nullptr;
  2220. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2221. subgroup_size_control_features.computeFullSubgroups = false;
  2222. subgroup_size_control_features.subgroupSizeControl = false;
  2223. if (device->subgroup_size_control) {
  2224. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2225. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2226. }
  2227. #if defined(VK_KHR_cooperative_matrix)
  2228. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2229. coopmat_features.pNext = nullptr;
  2230. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2231. coopmat_features.cooperativeMatrix = VK_FALSE;
  2232. if (device->coopmat_support) {
  2233. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2234. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2235. }
  2236. #endif
  2237. #if defined(VK_NV_cooperative_matrix2)
  2238. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2239. coopmat2_features.pNext = nullptr;
  2240. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2241. if (coopmat2_support) {
  2242. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2243. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2244. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2245. }
  2246. #endif
  2247. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2248. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2249. if (maintenance4_support) {
  2250. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2251. last_struct = (VkBaseOutStructure *)&maint4_features;
  2252. device_extensions.push_back("VK_KHR_maintenance4");
  2253. }
  2254. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2255. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2256. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2257. if (device->subgroup_size_control) {
  2258. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2259. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2260. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2261. }
  2262. device->subgroup_size_control = device->subgroup_size_control &&
  2263. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2264. subgroup_size_control_features.subgroupSizeControl;
  2265. if (device->subgroup_size_control) {
  2266. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2267. }
  2268. #if defined(VK_KHR_cooperative_matrix)
  2269. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2270. #endif
  2271. if (coopmat2_support) {
  2272. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2273. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2274. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2275. coopmat2_features.cooperativeMatrixReductions &&
  2276. coopmat2_features.cooperativeMatrixConversions &&
  2277. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2278. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2279. coopmat2_features.cooperativeMatrixBlockLoads &&
  2280. vk12_features.bufferDeviceAddress) {
  2281. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2282. uint32_t count = 0;
  2283. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2284. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2285. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2286. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2287. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2288. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2289. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2290. flexible_dimensions.resize(count, empty_prop);
  2291. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2292. bool found_fp16_128 = false,
  2293. found_fp16_256 = false,
  2294. found_fp32_128 = false,
  2295. found_fp32_256 = false;
  2296. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2297. // with 32x16x16 and 256 with 32x32x16.
  2298. for (auto &prop : flexible_dimensions) {
  2299. if (prop.saturatingAccumulation == VK_FALSE &&
  2300. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2301. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2302. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2303. if (prop.workgroupInvocations == 128 &&
  2304. prop.MGranularity <= 32 &&
  2305. prop.NGranularity <= 16 &&
  2306. prop.KGranularity <= 16) {
  2307. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2308. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2309. found_fp16_128 = true;
  2310. }
  2311. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2312. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2313. found_fp32_128 = true;
  2314. }
  2315. }
  2316. if (prop.workgroupInvocations == 256 &&
  2317. prop.MGranularity <= 32 &&
  2318. prop.NGranularity <= 32 &&
  2319. prop.KGranularity <= 16) {
  2320. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2321. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2322. found_fp16_256 = true;
  2323. }
  2324. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2325. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2326. found_fp32_256 = true;
  2327. }
  2328. }
  2329. }
  2330. }
  2331. if (found_fp16_128 && found_fp16_256 &&
  2332. found_fp32_128 && found_fp32_256 &&
  2333. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2334. device->coopmat2 = true;
  2335. }
  2336. }
  2337. #endif
  2338. }
  2339. if (!vk11_features.storageBuffer16BitAccess) {
  2340. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2341. throw std::runtime_error("Unsupported device");
  2342. }
  2343. device_extensions.push_back("VK_KHR_16bit_storage");
  2344. #ifdef GGML_VULKAN_VALIDATE
  2345. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2346. #endif
  2347. if (device->fp16) {
  2348. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2349. }
  2350. #if defined(VK_KHR_cooperative_matrix)
  2351. if (device->coopmat_support) {
  2352. // Query supported shapes
  2353. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2354. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2355. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2356. uint32_t cm_props_num;
  2357. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2358. cm_props.resize(cm_props_num);
  2359. for (auto& prop : cm_props) {
  2360. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2361. }
  2362. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2363. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2364. for (auto& prop : cm_props) {
  2365. 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));
  2366. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2367. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2368. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2369. ) {
  2370. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2371. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2372. // coopmat sizes not set yet
  2373. if (device->coopmat_m == 0) {
  2374. device->coopmat_acc_f32_support = true;
  2375. device->coopmat_m = prop.MSize;
  2376. device->coopmat_n = prop.NSize;
  2377. device->coopmat_k = prop.KSize;
  2378. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2379. // Only enable if shape is identical
  2380. device->coopmat_acc_f32_support = true;
  2381. }
  2382. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2383. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2384. // coopmat sizes not set yet
  2385. if (device->coopmat_m == 0) {
  2386. device->coopmat_acc_f16_support = true;
  2387. device->coopmat_m = prop.MSize;
  2388. device->coopmat_n = prop.NSize;
  2389. device->coopmat_k = prop.KSize;
  2390. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2391. // Only enable if shape is identical
  2392. device->coopmat_acc_f16_support = true;
  2393. }
  2394. }
  2395. }
  2396. }
  2397. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2398. // No suitable matmul mode found
  2399. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2400. device->coopmat_support = false;
  2401. }
  2402. }
  2403. if (device->coopmat_support) {
  2404. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2405. }
  2406. #endif
  2407. device->name = GGML_VK_NAME + std::to_string(idx);
  2408. device_create_info = {
  2409. vk::DeviceCreateFlags(),
  2410. device_queue_create_infos,
  2411. {},
  2412. device_extensions
  2413. };
  2414. device_create_info.setPNext(&device_features2);
  2415. device->device = device->physical_device.createDevice(device_create_info);
  2416. // Queues
  2417. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2418. // Shaders
  2419. // Disable matmul tile sizes early if performance low or not supported
  2420. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2421. switch (device->vendor_id) {
  2422. #ifndef GGML_VULKAN_RUN_TESTS
  2423. case VK_VENDOR_ID_AMD:
  2424. case VK_VENDOR_ID_INTEL:
  2425. device->mul_mat_l[i] = false;
  2426. device->mul_mat_m[i] = true;
  2427. device->mul_mat_s[i] = true;
  2428. device->mul_mat_id_l[i] = false;
  2429. device->mul_mat_id_m[i] = true;
  2430. device->mul_mat_id_s[i] = true;
  2431. break;
  2432. case VK_VENDOR_ID_APPLE:
  2433. device->mul_mat_l[i] = false;
  2434. device->mul_mat_m[i] = true;
  2435. device->mul_mat_s[i] = false;
  2436. device->mul_mat_id_l[i] = false;
  2437. device->mul_mat_id_m[i] = true;
  2438. device->mul_mat_id_s[i] = false;
  2439. break;
  2440. #endif
  2441. default:
  2442. device->mul_mat_l[i] = true;
  2443. device->mul_mat_m[i] = true;
  2444. device->mul_mat_s[i] = true;
  2445. device->mul_mat_id_l[i] = true;
  2446. device->mul_mat_id_m[i] = true;
  2447. device->mul_mat_id_s[i] = true;
  2448. break;
  2449. }
  2450. }
  2451. ggml_vk_load_shaders(device);
  2452. if (!device->single_queue) {
  2453. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  2454. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  2455. } else {
  2456. // TODO: Use pointer or reference to avoid copy
  2457. device->transfer_queue = device->compute_queue;
  2458. }
  2459. device->buffer_type = {
  2460. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  2461. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  2462. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  2463. };
  2464. device->fence = device->device.createFence({});
  2465. device->idx = idx;
  2466. return device;
  2467. }
  2468. return vk_instance.devices[idx];
  2469. }
  2470. static void ggml_vk_print_gpu_info(size_t idx) {
  2471. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2472. size_t dev_num = vk_instance.device_indices[idx];
  2473. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  2474. GGML_ASSERT(vk_instance_initialized);
  2475. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2476. if (dev_num >= devices.size()) {
  2477. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2478. throw std::runtime_error("Device not found");
  2479. }
  2480. vk::PhysicalDevice physical_device = devices[dev_num];
  2481. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  2482. vk::PhysicalDeviceProperties2 props2;
  2483. vk::PhysicalDeviceMaintenance3Properties props3;
  2484. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2485. vk::PhysicalDeviceDriverProperties driver_props;
  2486. props2.pNext = &props3;
  2487. props3.pNext = &subgroup_props;
  2488. subgroup_props.pNext = &driver_props;
  2489. physical_device.getProperties2(&props2);
  2490. vk_device_architecture arch = get_device_architecture(physical_device);
  2491. uint32_t default_subgroup_size = get_subgroup_size("", arch);
  2492. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  2493. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2494. bool fp16_storage = false;
  2495. bool fp16_compute = false;
  2496. bool coopmat_support = false;
  2497. bool coopmat2_support = false;
  2498. for (auto properties : ext_props) {
  2499. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2500. fp16_storage = true;
  2501. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2502. fp16_compute = true;
  2503. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2504. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2505. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2506. coopmat_support = true;
  2507. #endif
  2508. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2509. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2510. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2511. coopmat2_support = true;
  2512. #endif
  2513. }
  2514. }
  2515. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  2516. if (!ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture)) {
  2517. coopmat_support = false;
  2518. }
  2519. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  2520. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  2521. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2522. vk::PhysicalDeviceFeatures device_features = physical_device.getFeatures();
  2523. VkPhysicalDeviceFeatures2 device_features2;
  2524. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2525. device_features2.pNext = nullptr;
  2526. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2527. VkPhysicalDeviceVulkan11Features vk11_features;
  2528. vk11_features.pNext = nullptr;
  2529. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2530. device_features2.pNext = &vk11_features;
  2531. VkPhysicalDeviceVulkan12Features vk12_features;
  2532. vk12_features.pNext = nullptr;
  2533. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2534. vk11_features.pNext = &vk12_features;
  2535. // Pointer to the last chain element
  2536. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_features;
  2537. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2538. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2539. coopmat_features.pNext = nullptr;
  2540. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2541. coopmat_features.cooperativeMatrix = VK_FALSE;
  2542. if (coopmat_support) {
  2543. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2544. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2545. }
  2546. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  2547. fp16 = fp16 && vk12_features.shaderFloat16;
  2548. coopmat_support = coopmat_support && coopmat_features.cooperativeMatrix;
  2549. #endif
  2550. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  2551. std::string device_name = props2.properties.deviceName.data();
  2552. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | matrix cores: %s\n",
  2553. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  2554. props2.properties.limits.maxComputeSharedMemorySize, matrix_cores.c_str());
  2555. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  2556. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  2557. }
  2558. }
  2559. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2560. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2561. static void ggml_vk_instance_init() {
  2562. if (vk_instance_initialized) {
  2563. return;
  2564. }
  2565. VK_LOG_DEBUG("ggml_vk_instance_init()");
  2566. uint32_t api_version = vk::enumerateInstanceVersion();
  2567. if (api_version < VK_API_VERSION_1_2) {
  2568. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  2569. GGML_ABORT("fatal error");
  2570. }
  2571. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  2572. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  2573. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  2574. #ifdef __APPLE__
  2575. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  2576. #endif
  2577. std::vector<const char*> layers;
  2578. if (validation_ext) {
  2579. layers.push_back("VK_LAYER_KHRONOS_validation");
  2580. }
  2581. std::vector<const char*> extensions;
  2582. if (validation_ext) {
  2583. extensions.push_back("VK_EXT_validation_features");
  2584. }
  2585. #ifdef __APPLE__
  2586. if (portability_enumeration_ext) {
  2587. extensions.push_back("VK_KHR_portability_enumeration");
  2588. }
  2589. #endif
  2590. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  2591. #ifdef __APPLE__
  2592. if (portability_enumeration_ext) {
  2593. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  2594. }
  2595. #endif
  2596. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  2597. vk::ValidationFeaturesEXT validation_features;
  2598. if (validation_ext) {
  2599. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  2600. validation_features = {
  2601. features_enable,
  2602. {},
  2603. };
  2604. validation_features.setPNext(nullptr);
  2605. instance_create_info.setPNext(&validation_features);
  2606. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  2607. }
  2608. vk_instance.instance = vk::createInstance(instance_create_info);
  2609. vk_instance_initialized = true;
  2610. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  2611. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  2612. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  2613. if (devices_env != nullptr) {
  2614. std::string devices(devices_env);
  2615. std::replace(devices.begin(), devices.end(), ',', ' ');
  2616. std::stringstream ss(devices);
  2617. size_t tmp;
  2618. while (ss >> tmp) {
  2619. if(tmp >= num_available_devices) {
  2620. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  2621. throw std::runtime_error("Invalid Vulkan device index");
  2622. }
  2623. vk_instance.device_indices.push_back(tmp);
  2624. }
  2625. } else {
  2626. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2627. // Make sure at least one device exists
  2628. if (devices.empty()) {
  2629. std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
  2630. return;
  2631. }
  2632. // Default to using all dedicated GPUs
  2633. for (size_t i = 0; i < devices.size(); i++) {
  2634. vk::PhysicalDeviceProperties2 new_props;
  2635. vk::PhysicalDeviceDriverProperties new_driver;
  2636. vk::PhysicalDeviceIDProperties new_id;
  2637. new_props.pNext = &new_driver;
  2638. new_driver.pNext = &new_id;
  2639. devices[i].getProperties2(&new_props);
  2640. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  2641. // Check if there are two physical devices corresponding to the same GPU
  2642. auto old_device = std::find_if(
  2643. vk_instance.device_indices.begin(),
  2644. vk_instance.device_indices.end(),
  2645. [&devices, &new_id](const size_t k){
  2646. vk::PhysicalDeviceProperties2 old_props;
  2647. vk::PhysicalDeviceIDProperties old_id;
  2648. old_props.pNext = &old_id;
  2649. devices[k].getProperties2(&old_props);
  2650. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  2651. }
  2652. );
  2653. if (old_device == vk_instance.device_indices.end()) {
  2654. vk_instance.device_indices.push_back(i);
  2655. } else {
  2656. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  2657. // This can cause error when splitting layers aross the devices, need to keep only 1
  2658. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  2659. vk::PhysicalDeviceProperties2 old_props;
  2660. vk::PhysicalDeviceDriverProperties old_driver;
  2661. old_props.pNext = &old_driver;
  2662. devices[*old_device].getProperties2(&old_props);
  2663. std::map<vk::DriverId, int> driver_priorities {};
  2664. int old_priority = std::numeric_limits<int>::max();
  2665. int new_priority = std::numeric_limits<int>::max();
  2666. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  2667. // Smaller number -> higher priority
  2668. switch (old_props.properties.vendorID) {
  2669. case VK_VENDOR_ID_AMD:
  2670. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  2671. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  2672. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  2673. break;
  2674. case VK_VENDOR_ID_INTEL:
  2675. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  2676. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  2677. break;
  2678. case VK_VENDOR_ID_NVIDIA:
  2679. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  2680. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  2681. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  2682. #endif
  2683. break;
  2684. }
  2685. if (driver_priorities.count(old_driver.driverID)) {
  2686. old_priority = driver_priorities[old_driver.driverID];
  2687. }
  2688. if (driver_priorities.count(new_driver.driverID)) {
  2689. new_priority = driver_priorities[new_driver.driverID];
  2690. }
  2691. if (new_priority < old_priority) {
  2692. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  2693. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  2694. vk_instance.device_indices.push_back(i);
  2695. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  2696. }
  2697. else {
  2698. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  2699. }
  2700. }
  2701. }
  2702. }
  2703. // If no dedicated GPUs found, fall back to GPU 0
  2704. if (vk_instance.device_indices.empty()) {
  2705. vk_instance.device_indices.push_back(0);
  2706. }
  2707. }
  2708. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  2709. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  2710. ggml_vk_print_gpu_info(i);
  2711. }
  2712. }
  2713. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  2714. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  2715. ggml_vk_instance_init();
  2716. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2717. ctx->name = GGML_VK_NAME + std::to_string(idx);
  2718. ctx->device = ggml_vk_get_device(idx);
  2719. ctx->semaphore_idx = 0;
  2720. ctx->event_idx = 0;
  2721. ctx->prealloc_size_x = 0;
  2722. ctx->prealloc_size_y = 0;
  2723. ctx->prealloc_size_split_k = 0;
  2724. ctx->fence = ctx->device->device.createFence({});
  2725. #ifdef GGML_VULKAN_CHECK_RESULTS
  2726. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  2727. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  2728. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  2729. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  2730. #endif
  2731. }
  2732. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  2733. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  2734. switch (type) {
  2735. case GGML_TYPE_F32:
  2736. case GGML_TYPE_Q4_0:
  2737. case GGML_TYPE_Q4_1:
  2738. case GGML_TYPE_Q5_0:
  2739. case GGML_TYPE_Q5_1:
  2740. case GGML_TYPE_Q8_0:
  2741. case GGML_TYPE_Q2_K:
  2742. case GGML_TYPE_Q3_K:
  2743. case GGML_TYPE_Q4_K:
  2744. case GGML_TYPE_Q5_K:
  2745. case GGML_TYPE_Q6_K:
  2746. case GGML_TYPE_IQ1_S:
  2747. case GGML_TYPE_IQ1_M:
  2748. case GGML_TYPE_IQ2_XXS:
  2749. case GGML_TYPE_IQ2_XS:
  2750. case GGML_TYPE_IQ2_S:
  2751. case GGML_TYPE_IQ3_XXS:
  2752. case GGML_TYPE_IQ3_S:
  2753. case GGML_TYPE_IQ4_XS:
  2754. case GGML_TYPE_IQ4_NL:
  2755. break;
  2756. default:
  2757. return nullptr;
  2758. }
  2759. return ctx->device->pipeline_dequant[type];
  2760. }
  2761. 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) {
  2762. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  2763. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2764. return ctx->device->pipeline_matmul_f32;
  2765. }
  2766. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  2767. return ctx->device->pipeline_matmul_f32_f16;
  2768. }
  2769. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2770. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2771. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  2772. }
  2773. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2774. return ctx->device->pipeline_matmul_f16.f16acc;
  2775. }
  2776. } else {
  2777. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2778. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  2779. }
  2780. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2781. return ctx->device->pipeline_matmul_f16.f32acc;
  2782. }
  2783. }
  2784. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  2785. return nullptr;
  2786. }
  2787. switch (src0_type) {
  2788. case GGML_TYPE_Q4_0:
  2789. case GGML_TYPE_Q4_1:
  2790. case GGML_TYPE_Q5_0:
  2791. case GGML_TYPE_Q5_1:
  2792. case GGML_TYPE_Q8_0:
  2793. case GGML_TYPE_Q2_K:
  2794. case GGML_TYPE_Q3_K:
  2795. case GGML_TYPE_Q4_K:
  2796. case GGML_TYPE_Q5_K:
  2797. case GGML_TYPE_Q6_K:
  2798. case GGML_TYPE_IQ1_S:
  2799. case GGML_TYPE_IQ1_M:
  2800. case GGML_TYPE_IQ2_XXS:
  2801. case GGML_TYPE_IQ2_XS:
  2802. case GGML_TYPE_IQ2_S:
  2803. case GGML_TYPE_IQ3_XXS:
  2804. case GGML_TYPE_IQ3_S:
  2805. case GGML_TYPE_IQ4_XS:
  2806. case GGML_TYPE_IQ4_NL:
  2807. break;
  2808. default:
  2809. return nullptr;
  2810. }
  2811. if (ctx->device->coopmat2) {
  2812. assert(src1_type == GGML_TYPE_F16);
  2813. return ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc;
  2814. }
  2815. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  2816. }
  2817. 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) {
  2818. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  2819. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  2820. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  2821. switch (a_type) {
  2822. case GGML_TYPE_F32:
  2823. case GGML_TYPE_F16:
  2824. case GGML_TYPE_Q4_0:
  2825. case GGML_TYPE_Q4_1:
  2826. case GGML_TYPE_Q5_0:
  2827. case GGML_TYPE_Q5_1:
  2828. case GGML_TYPE_Q8_0:
  2829. case GGML_TYPE_Q2_K:
  2830. case GGML_TYPE_Q3_K:
  2831. case GGML_TYPE_Q4_K:
  2832. case GGML_TYPE_Q5_K:
  2833. case GGML_TYPE_Q6_K:
  2834. case GGML_TYPE_IQ1_S:
  2835. case GGML_TYPE_IQ1_M:
  2836. case GGML_TYPE_IQ2_XXS:
  2837. case GGML_TYPE_IQ2_XS:
  2838. case GGML_TYPE_IQ2_S:
  2839. case GGML_TYPE_IQ3_XXS:
  2840. case GGML_TYPE_IQ3_S:
  2841. case GGML_TYPE_IQ4_XS:
  2842. case GGML_TYPE_IQ4_NL:
  2843. break;
  2844. default:
  2845. return nullptr;
  2846. }
  2847. 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];
  2848. }
  2849. 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) {
  2850. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  2851. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2852. return ctx->device->pipeline_matmul_id_f32;
  2853. }
  2854. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2855. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2856. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  2857. }
  2858. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2859. return ctx->device->pipeline_matmul_id_f16.f16acc;
  2860. }
  2861. } else {
  2862. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2863. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  2864. }
  2865. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2866. return ctx->device->pipeline_matmul_id_f16.f32acc;
  2867. }
  2868. }
  2869. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  2870. switch (src0_type) {
  2871. case GGML_TYPE_Q4_0:
  2872. case GGML_TYPE_Q4_1:
  2873. case GGML_TYPE_Q5_0:
  2874. case GGML_TYPE_Q5_1:
  2875. case GGML_TYPE_Q8_0:
  2876. case GGML_TYPE_Q2_K:
  2877. case GGML_TYPE_Q3_K:
  2878. case GGML_TYPE_Q4_K:
  2879. case GGML_TYPE_Q5_K:
  2880. case GGML_TYPE_Q6_K:
  2881. case GGML_TYPE_IQ1_S:
  2882. case GGML_TYPE_IQ1_M:
  2883. case GGML_TYPE_IQ2_XXS:
  2884. case GGML_TYPE_IQ2_XS:
  2885. case GGML_TYPE_IQ2_S:
  2886. case GGML_TYPE_IQ3_XXS:
  2887. case GGML_TYPE_IQ3_S:
  2888. case GGML_TYPE_IQ4_XS:
  2889. case GGML_TYPE_IQ4_NL:
  2890. break;
  2891. default:
  2892. return nullptr;
  2893. }
  2894. 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;
  2895. }
  2896. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  2897. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  2898. GGML_ASSERT(b_type == GGML_TYPE_F32);
  2899. switch (a_type) {
  2900. case GGML_TYPE_F32:
  2901. case GGML_TYPE_F16:
  2902. case GGML_TYPE_Q4_0:
  2903. case GGML_TYPE_Q4_1:
  2904. case GGML_TYPE_Q5_0:
  2905. case GGML_TYPE_Q5_1:
  2906. case GGML_TYPE_Q8_0:
  2907. case GGML_TYPE_Q2_K:
  2908. case GGML_TYPE_Q3_K:
  2909. case GGML_TYPE_Q4_K:
  2910. case GGML_TYPE_Q5_K:
  2911. case GGML_TYPE_Q6_K:
  2912. case GGML_TYPE_IQ1_S:
  2913. case GGML_TYPE_IQ1_M:
  2914. case GGML_TYPE_IQ2_XXS:
  2915. case GGML_TYPE_IQ2_XS:
  2916. case GGML_TYPE_IQ2_S:
  2917. case GGML_TYPE_IQ3_XXS:
  2918. case GGML_TYPE_IQ3_S:
  2919. case GGML_TYPE_IQ4_XS:
  2920. case GGML_TYPE_IQ4_NL:
  2921. break;
  2922. default:
  2923. return nullptr;
  2924. }
  2925. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  2926. }
  2927. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  2928. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  2929. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  2930. int best_i = -1;
  2931. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  2932. int worst_i = -1;
  2933. size_t worst_size = 0; //largest unused buffer seen so far
  2934. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  2935. vk_buffer &b = ctx->buffer_pool[i];
  2936. if (b != nullptr && b->size >= size && b->size < best_size) {
  2937. best_i = i;
  2938. best_size = b->size;
  2939. }
  2940. if (b != nullptr && b->size > worst_size) {
  2941. worst_i = i;
  2942. worst_size = b->size;
  2943. }
  2944. }
  2945. if(best_i != -1) {
  2946. //found the smallest buffer that fits our needs
  2947. vk_buffer b = ctx->buffer_pool[best_i];
  2948. ctx->buffer_pool[best_i].reset();
  2949. return b;
  2950. }
  2951. if(worst_i != -1) {
  2952. //no buffer that fits our needs, resize largest one to save memory
  2953. vk_buffer& b = ctx->buffer_pool[worst_i];
  2954. ggml_vk_destroy_buffer(b);
  2955. }
  2956. return ggml_vk_create_buffer_device(ctx->device, size);
  2957. }
  2958. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  2959. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  2960. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  2961. vk_buffer& b = ctx->buffer_pool[i];
  2962. if (b == nullptr) {
  2963. b = buffer;
  2964. return;
  2965. }
  2966. }
  2967. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  2968. ggml_vk_destroy_buffer(buffer);
  2969. }
  2970. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  2971. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  2972. // Try to find existing temp buffer with enough capacity
  2973. for (auto& buffer : ctx->gc.temp_buffers) {
  2974. if (buffer->size >= size) {
  2975. return buffer;
  2976. }
  2977. }
  2978. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  2979. // Otherwise create new buffer
  2980. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  2981. ctx->gc.temp_buffers.push_back(buf);
  2982. return buf;
  2983. }
  2984. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  2985. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  2986. vk_buffer buf = ggml_vk_create_buffer(device, size,
  2987. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  2988. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  2989. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  2990. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  2991. size/1024.0/1024.0);
  2992. device->device.freeMemory(buf->device_memory);
  2993. device->device.destroyBuffer(buf->buffer);
  2994. return nullptr;
  2995. }
  2996. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  2997. return buf->ptr;
  2998. }
  2999. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3000. if (ptr == nullptr) {
  3001. return;
  3002. }
  3003. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3004. vk_buffer buf;
  3005. size_t index;
  3006. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3007. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3008. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3009. if (ptr >= addr && ptr < endr) {
  3010. buf = std::get<2>(device->pinned_memory[i]);
  3011. index = i;
  3012. break;
  3013. }
  3014. }
  3015. if (buf == nullptr) {
  3016. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3017. return;
  3018. }
  3019. ggml_vk_destroy_buffer(buf);
  3020. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3021. }
  3022. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3023. buf = nullptr;
  3024. buf_offset = 0;
  3025. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3026. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3027. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3028. if (ptr >= addr && ptr < endr) {
  3029. buf = std::get<2>(device->pinned_memory[i]);
  3030. buf_offset = ((const uint8_t *)ptr) - addr;
  3031. break;
  3032. }
  3033. }
  3034. }
  3035. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_queue& q, bool one_time = true) {
  3036. vk_submission s;
  3037. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  3038. if (one_time) {
  3039. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3040. } else {
  3041. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3042. }
  3043. return s;
  3044. }
  3045. 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, size_t push_constant_size, const void* push_constants, std::array<uint32_t, 3> elements) {
  3046. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3047. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3048. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3049. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3050. for (auto& buffer : descriptor_buffer_infos) {
  3051. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3052. }
  3053. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3054. GGML_ASSERT(pipeline->descriptor_set_idx < pipeline->descriptor_sets.size());
  3055. GGML_ASSERT(descriptor_buffer_infos.size() == pipeline->parameter_count);
  3056. vk::DescriptorSet& descriptor_set = pipeline->descriptor_sets[pipeline->descriptor_set_idx++];
  3057. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3058. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3059. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size, push_constants);
  3060. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3061. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3062. pipeline->layout,
  3063. 0,
  3064. { descriptor_set },
  3065. {});
  3066. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3067. }
  3068. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3069. s.buffer.end();
  3070. s.wait_semaphores = std::move(wait_semaphores);
  3071. s.signal_semaphores = std::move(signal_semaphores);
  3072. }
  3073. static void ggml_vk_ctx_end(vk_context& ctx) {
  3074. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3075. if (ctx->s == nullptr) {
  3076. return;
  3077. }
  3078. ctx->s->buffer.end();
  3079. ctx->s = nullptr;
  3080. }
  3081. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3082. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3083. if (subctx->s != nullptr) {
  3084. ggml_vk_ctx_end(subctx);
  3085. }
  3086. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->q) });
  3087. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3088. }
  3089. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3090. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3091. return CEIL_DIV(width, align) * align;
  3092. }
  3093. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3094. if (memcpys == nullptr) {
  3095. memcpy(dst, src, size);
  3096. } else {
  3097. memcpys->emplace_back(dst, src, size);
  3098. }
  3099. }
  3100. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3101. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3102. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3103. ggml_vk_destroy_buffer(device->sync_staging);
  3104. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3105. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3106. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3107. }
  3108. }
  3109. 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) {
  3110. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3111. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3112. // Buffer is already mapped
  3113. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3114. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3115. GGML_ABORT("fatal error");
  3116. }
  3117. // Check if src is pinned memory
  3118. vk_buffer buf = nullptr;
  3119. size_t buf_offset = 0;
  3120. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3121. const uint64_t ne0 = tensor->ne[0];
  3122. const uint64_t ne1 = tensor->ne[1];
  3123. const uint64_t ne2 = tensor->ne[2];
  3124. const uint64_t ne3 = tensor->ne[3];
  3125. const uint64_t nb0 = tensor->nb[0];
  3126. const uint64_t nb1 = tensor->nb[1];
  3127. const uint64_t nb2 = tensor->nb[2];
  3128. const uint64_t nb3 = tensor->nb[3];
  3129. const ggml_type type = tensor->type;
  3130. const uint64_t ts = ggml_type_size(type);
  3131. const uint64_t bs = ggml_blck_size(type);
  3132. const uint64_t dstnb0 = ts;
  3133. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3134. const uint64_t dstnb2 = dstnb1*ne1;
  3135. const uint64_t dstnb3 = dstnb2*ne2;
  3136. const uint64_t ne = ggml_nelements(tensor);
  3137. if (buf != nullptr) {
  3138. // Memory is pinned, use as staging buffer
  3139. std::vector<vk::BufferCopy> slices;
  3140. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3141. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3142. // Find longest contiguous slice
  3143. if (ne1*nb1 == dstnb2) {
  3144. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3145. } else {
  3146. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3147. if (ne0*nb0/bs == dstnb1) {
  3148. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3149. } else {
  3150. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3151. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3152. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3153. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3154. }
  3155. }
  3156. }
  3157. }
  3158. }
  3159. }
  3160. ggml_vk_sync_buffers(subctx);
  3161. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3162. return;
  3163. }
  3164. if (!sync_staging) {
  3165. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3166. }
  3167. // Staging buffer required
  3168. vk_buffer& staging = ctx->device->sync_staging;
  3169. const uint64_t copy_size = ts*ne/bs;
  3170. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3171. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3172. ggml_vk_sync_buffers(subctx);
  3173. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3174. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3175. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3176. // Find longest contiguous slice
  3177. if (ne1*nb1 == dstnb2) {
  3178. 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);
  3179. } else {
  3180. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3181. if (ne0*nb0/bs == dstnb1) {
  3182. 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);
  3183. } else {
  3184. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3185. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3186. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3187. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3188. }
  3189. }
  3190. }
  3191. }
  3192. }
  3193. }
  3194. }
  3195. 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) {
  3196. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3197. // Buffer is already mapped
  3198. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3199. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3200. GGML_ABORT("fatal error");
  3201. }
  3202. // Check if src is pinned memory
  3203. vk_buffer buf = nullptr;
  3204. size_t buf_offset = 0;
  3205. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3206. if (buf != nullptr) {
  3207. // Memory is pinned, use as staging buffer
  3208. std::vector<vk::BufferCopy> slices(1);
  3209. if (width == spitch) {
  3210. // Only do single write if stride is equal
  3211. slices[0].srcOffset = buf_offset;
  3212. slices[0].dstOffset = offset;
  3213. slices[0].size = width * height;
  3214. } else {
  3215. slices.resize(height);
  3216. for (size_t i = 0; i < height; i++) {
  3217. slices[i].srcOffset = buf_offset + i * spitch;
  3218. slices[i].dstOffset = offset + i * width;
  3219. slices[i].size = width;
  3220. }
  3221. }
  3222. ggml_vk_sync_buffers(subctx);
  3223. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3224. return;
  3225. }
  3226. VK_LOG_DEBUG("STAGING");
  3227. if (!sync_staging) {
  3228. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3229. }
  3230. // Staging buffer required
  3231. const size_t copy_size = width*height;
  3232. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3233. vk_buffer& staging_buffer = dst->device->sync_staging;
  3234. VkBufferCopy buf_copy = {
  3235. 0,
  3236. offset,
  3237. copy_size};
  3238. ggml_vk_sync_buffers(subctx);
  3239. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3240. if (width == spitch) {
  3241. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3242. } else {
  3243. for (size_t i = 0; i < height; i++) {
  3244. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3245. }
  3246. }
  3247. }
  3248. 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) {
  3249. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3250. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3251. }
  3252. 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) {
  3253. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3254. // Buffer is already mapped
  3255. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3256. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3257. for (size_t i = 0; i < height; i++) {
  3258. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3259. }
  3260. } else {
  3261. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3262. ggml_vk_ctx_begin(dst->device, subctx);
  3263. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3264. ggml_vk_ctx_end(subctx);
  3265. for (auto& cpy : subctx->in_memcpys) {
  3266. memcpy(cpy.dst, cpy.src, cpy.n);
  3267. }
  3268. ggml_vk_submit(subctx, dst->device->fence);
  3269. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3270. dst->device->device.resetFences({ dst->device->fence });
  3271. }
  3272. }
  3273. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3274. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3275. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3276. }
  3277. 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) {
  3278. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3279. GGML_ASSERT(width > 0);
  3280. GGML_ASSERT(height > 0);
  3281. GGML_ASSERT(src != nullptr);
  3282. // TODO: staging_offset is not used
  3283. // Check if dst is pinned memory
  3284. vk_buffer buf = nullptr;
  3285. size_t buf_offset = 0;
  3286. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3287. std::vector<vk::BufferCopy> slices(1);
  3288. if (width == spitch && width == dpitch) {
  3289. // Only do single write if stride is equal
  3290. slices[0].srcOffset = offset;
  3291. slices[0].dstOffset = buf_offset;
  3292. slices[0].size = width * height;
  3293. } else {
  3294. slices.resize(height);
  3295. for (size_t i = 0; i < height; i++) {
  3296. slices[i].srcOffset = offset + i * spitch;
  3297. slices[i].dstOffset = buf_offset + i * dpitch;
  3298. slices[i].size = width;
  3299. }
  3300. }
  3301. if (buf != nullptr) {
  3302. // Memory is pinned, use as staging buffer
  3303. ggml_vk_sync_buffers(subctx);
  3304. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3305. return;
  3306. }
  3307. VK_LOG_DEBUG("STAGING");
  3308. if (!sync_staging) {
  3309. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3310. }
  3311. // Fall back to staging buffer
  3312. const size_t copy_size = dpitch * height;
  3313. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  3314. vk_buffer& staging_buffer = src->device->sync_staging;
  3315. ggml_vk_sync_buffers(subctx);
  3316. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  3317. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  3318. }
  3319. 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) {
  3320. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  3321. }
  3322. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  3323. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  3324. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  3325. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  3326. // the HW device to host copy path.
  3327. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  3328. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3329. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  3330. } else {
  3331. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3332. ggml_vk_ctx_begin(src->device, subctx);
  3333. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  3334. ggml_vk_ctx_end(subctx);
  3335. ggml_vk_submit(subctx, src->device->fence);
  3336. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  3337. src->device->device.resetFences({ src->device->fence });
  3338. for (auto& cpy : subctx->out_memcpys) {
  3339. memcpy(cpy.dst, cpy.src, cpy.n);
  3340. }
  3341. }
  3342. }
  3343. 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) {
  3344. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  3345. // Make sure both buffers are on same device
  3346. GGML_ASSERT(src->device == dst->device);
  3347. VkBufferCopy bc{ src_offset, dst_offset, size };
  3348. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  3349. }
  3350. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  3351. if (src->device == dst->device) {
  3352. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  3353. // Copy within the device
  3354. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3355. ggml_vk_ctx_begin(src->device, subctx);
  3356. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  3357. ggml_vk_ctx_end(subctx);
  3358. ggml_vk_submit(subctx, src->device->fence);
  3359. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  3360. src->device->device.resetFences({ src->device->fence });
  3361. } else {
  3362. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  3363. // Copy device to device
  3364. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  3365. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  3366. // Copy to src staging buffer
  3367. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  3368. // memcpy to dst staging buffer
  3369. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  3370. // Copy to dst buffer
  3371. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  3372. }
  3373. }
  3374. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3375. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  3376. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3377. }
  3378. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3379. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  3380. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3381. ggml_vk_ctx_begin(dst->device, subctx);
  3382. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3383. ggml_vk_ctx_end(subctx);
  3384. ggml_vk_submit(subctx, dst->device->fence);
  3385. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  3386. dst->device->device.resetFences({ dst->device->fence });
  3387. }
  3388. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  3389. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  3390. uint32_t split_k = 1;
  3391. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  3392. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  3393. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  3394. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  3395. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  3396. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  3397. // Clamp to 2 or 4
  3398. split_k = std::min(split_k, 4u);
  3399. if (split_k == 3) {
  3400. split_k = 2;
  3401. }
  3402. }
  3403. }
  3404. return split_k;
  3405. }
  3406. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned, ggml_type src0_type) {
  3407. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  3408. if (ctx->device->coopmat2) {
  3409. // Use large shader when the N dimension is greater than the medium shader's tile size
  3410. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3411. 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])) {
  3412. return aligned ? mmp->a_l : mmp->l;
  3413. }
  3414. // Use medium shader when the N dimension is greater than the small shader's tile size
  3415. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3416. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  3417. return aligned ? mmp->a_m : mmp->m;
  3418. }
  3419. return aligned ? mmp->a_s : mmp->s;
  3420. }
  3421. 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])) {
  3422. return aligned ? mmp->a_s : mmp->s;
  3423. }
  3424. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  3425. return aligned ? mmp->a_m : mmp->m;
  3426. }
  3427. return aligned ? mmp->a_l : mmp->l;
  3428. }
  3429. 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) {
  3430. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  3431. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  3432. }
  3433. static void ggml_vk_matmul(
  3434. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3435. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  3436. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3437. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3438. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  3439. uint32_t padded_n) {
  3440. 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 << ")");
  3441. ggml_vk_sync_buffers(subctx);
  3442. if (split_k == 1) {
  3443. 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 };
  3444. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  3445. return;
  3446. }
  3447. GGML_ASSERT(batch_stride_d == m * n);
  3448. 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 };
  3449. // Make sure enough workgroups get assigned for split k to work
  3450. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, split_k_buffer }, sizeof(vk_mat_mat_push_constants), &pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  3451. ggml_vk_sync_buffers(subctx);
  3452. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  3453. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_matmul_split_k_reduce, { split_k_buffer, d }, pc2.size() * sizeof(uint32_t), pc2.data(), { m * n * batch, 1, 1 });
  3454. }
  3455. static vk_pipeline ggml_vk_guess_matmul_id_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned, ggml_type src0_type) {
  3456. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  3457. if (ctx->device->coopmat2) {
  3458. // Use large shader when the N dimension is greater than the medium shader's tile size
  3459. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3460. 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])) {
  3461. return aligned ? mmp->a_l : mmp->l;
  3462. }
  3463. // Use medium shader when the N dimension is greater than the small shader's tile size
  3464. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3465. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  3466. return aligned ? mmp->a_m : mmp->m;
  3467. }
  3468. return aligned ? mmp->a_s : mmp->s;
  3469. }
  3470. 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])) {
  3471. return aligned ? mmp->a_s : mmp->s;
  3472. }
  3473. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  3474. return aligned ? mmp->a_m : mmp->m;
  3475. }
  3476. return aligned ? mmp->a_l : mmp->l;
  3477. }
  3478. 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) {
  3479. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  3480. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  3481. }
  3482. static void ggml_vk_matmul_id(
  3483. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3484. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  3485. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3486. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3487. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  3488. uint32_t padded_n) {
  3489. 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 << "), " <<
  3490. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  3491. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  3492. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  3493. ggml_vk_sync_buffers(subctx);
  3494. 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,
  3495. nei0, nei1, nbi1, ne11, padded_n };
  3496. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, sizeof(vk_mat_mat_id_push_constants), &pc, { m, nei1, n_as });
  3497. }
  3498. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  3499. return
  3500. tensor->nb[0] == ggml_type_size(tensor->type) &&
  3501. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  3502. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  3503. }
  3504. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  3505. // Choose "contiguous copy" shader if src/dst are contiguous
  3506. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  3507. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  3508. if (contig) {
  3509. return ctx->device->pipeline_contig_cpy_f32_f32;
  3510. } else {
  3511. return ctx->device->pipeline_cpy_f32_f32;
  3512. }
  3513. }
  3514. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  3515. if (contig) {
  3516. return ctx->device->pipeline_contig_cpy_f32_f16;
  3517. } else {
  3518. return ctx->device->pipeline_cpy_f32_f16;
  3519. }
  3520. }
  3521. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  3522. if (contig) {
  3523. return ctx->device->pipeline_contig_cpy_f16_f16;
  3524. } else {
  3525. return ctx->device->pipeline_cpy_f16_f16;
  3526. }
  3527. }
  3528. if (src->type == GGML_TYPE_F32) {
  3529. switch (to) {
  3530. case GGML_TYPE_Q4_0:
  3531. case GGML_TYPE_Q4_1:
  3532. case GGML_TYPE_Q5_0:
  3533. case GGML_TYPE_Q5_1:
  3534. case GGML_TYPE_Q8_0:
  3535. case GGML_TYPE_IQ4_NL:
  3536. return ctx->device->pipeline_cpy_f32_quant[to];
  3537. default:
  3538. break;
  3539. }
  3540. }
  3541. if (to == GGML_TYPE_F32) {
  3542. switch (src->type) {
  3543. case GGML_TYPE_Q4_0:
  3544. case GGML_TYPE_Q4_1:
  3545. case GGML_TYPE_Q5_0:
  3546. case GGML_TYPE_Q5_1:
  3547. case GGML_TYPE_Q8_0:
  3548. case GGML_TYPE_IQ4_NL:
  3549. return ctx->device->pipeline_cpy_quant_f32[src->type];
  3550. default:
  3551. break;
  3552. }
  3553. }
  3554. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  3555. GGML_ABORT("fatal error");
  3556. }
  3557. 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) {
  3558. 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] << "), ";
  3559. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  3560. const int tensor_type_size = ggml_type_size(tensor->type);
  3561. const uint32_t ne = ggml_nelements(tensor);
  3562. std::array<uint32_t, 3> elements;
  3563. if (ne > 262144) {
  3564. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  3565. } else if (ne > 512) {
  3566. elements = { 512, CEIL_DIV(ne, 512), 1 };
  3567. } else {
  3568. elements = { ne, 1, 1 };
  3569. }
  3570. vk_op_unary_push_constants pc = {
  3571. (uint32_t)ne,
  3572. (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,
  3573. (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]),
  3574. 0,
  3575. 0.0f, 0.0f,
  3576. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3577. };
  3578. init_pushconst_fastdiv(pc);
  3579. ggml_vk_sync_buffers(subctx);
  3580. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(vk_op_unary_push_constants), &pc, elements);
  3581. }
  3582. 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) {
  3583. 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];
  3584. 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];
  3585. 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];
  3586. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3587. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3588. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3589. const uint64_t ne00 = src0->ne[0];
  3590. const uint64_t ne01 = src0->ne[1];
  3591. const uint64_t ne02 = src0->ne[2];
  3592. const uint64_t ne03 = src0->ne[3];
  3593. const uint64_t ne10 = src1->ne[0];
  3594. const uint64_t ne11 = src1->ne[1];
  3595. const uint64_t ne12 = src1->ne[2];
  3596. const uint64_t ne13 = src1->ne[3];
  3597. const uint64_t ne20 = dst->ne[0];
  3598. const uint64_t ne21 = dst->ne[1];
  3599. const uint64_t r2 = ne12 / ne02;
  3600. const uint64_t r3 = ne13 / ne03;
  3601. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3602. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3603. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3604. vk_buffer d_Qx = nullptr;
  3605. size_t qx_buf_offset = 0;
  3606. vk_buffer d_Qy = nullptr;
  3607. size_t qy_buf_offset = 0;
  3608. bool src0_uma = false;
  3609. bool src1_uma = false;
  3610. if (ctx->device->uma) {
  3611. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3612. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3613. src0_uma = d_Qx != nullptr;
  3614. src1_uma = d_Qy != nullptr;
  3615. }
  3616. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  3617. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  3618. !ggml_vk_dim01_contiguous(src0);
  3619. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  3620. !ggml_vk_dim01_contiguous(src1);
  3621. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  3622. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  3623. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  3624. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  3625. if (qx_needs_dequant) {
  3626. // Fall back to dequant + f16 mulmat
  3627. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  3628. }
  3629. // Not implemented
  3630. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3631. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11, qx_needs_dequant ? GGML_TYPE_F16 : src0->type));
  3632. const bool aligned = ne10 == kpad && ne01 > 8 && ne11 > 8;
  3633. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type);
  3634. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  3635. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  3636. const int x_ne = ne01 * ne00;
  3637. const int y_ne = padded_n * ne10;
  3638. const int d_ne = ne11 * ne01;
  3639. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  3640. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  3641. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3642. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  3643. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  3644. const uint64_t d_sz = sizeof(float) * d_ne;
  3645. vk_pipeline to_fp16_vk_0 = nullptr;
  3646. vk_pipeline to_fp16_vk_1 = nullptr;
  3647. if (x_non_contig) {
  3648. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  3649. } else {
  3650. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  3651. }
  3652. if (y_non_contig) {
  3653. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  3654. } else {
  3655. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3656. }
  3657. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3658. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3659. if (dryrun) {
  3660. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3661. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3662. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  3663. if (
  3664. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3665. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  3666. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  3667. GGML_ABORT("Requested preallocation size is too large");
  3668. }
  3669. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3670. ctx->prealloc_size_x = x_sz_upd;
  3671. }
  3672. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  3673. ctx->prealloc_size_y = y_sz_upd;
  3674. }
  3675. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  3676. ctx->prealloc_size_split_k = split_k_size;
  3677. }
  3678. // Request descriptor sets
  3679. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  3680. if (qx_needs_dequant) {
  3681. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  3682. }
  3683. if (qy_needs_dequant) {
  3684. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  3685. }
  3686. if (split_k > 1) {
  3687. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, 1);
  3688. }
  3689. return;
  3690. }
  3691. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3692. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3693. GGML_ASSERT(d_D != nullptr);
  3694. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  3695. vk_buffer d_X;
  3696. uint64_t x_buf_offset = 0;
  3697. vk_buffer d_Y;
  3698. uint64_t y_buf_offset = 0;
  3699. if (!src0_uma) {
  3700. d_Qx = src0_buf_ctx->dev_buffer;
  3701. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3702. GGML_ASSERT(d_Qx != nullptr);
  3703. }
  3704. if (!src1_uma) {
  3705. d_Qy = src1_buf_ctx->dev_buffer;
  3706. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3707. GGML_ASSERT(d_Qy != nullptr);
  3708. }
  3709. if (qx_needs_dequant) {
  3710. d_X = ctx->prealloc_x;
  3711. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  3712. } else {
  3713. d_X = d_Qx;
  3714. x_buf_offset = qx_buf_offset;
  3715. GGML_ASSERT(qx_sz == x_sz);
  3716. }
  3717. if (qy_needs_dequant) {
  3718. d_Y = ctx->prealloc_y;
  3719. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  3720. } else {
  3721. d_Y = d_Qy;
  3722. y_buf_offset = qy_buf_offset;
  3723. GGML_ASSERT(qy_sz == y_sz);
  3724. }
  3725. if (x_non_contig) {
  3726. 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 });
  3727. } else if (qx_needs_dequant) {
  3728. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  3729. ggml_vk_sync_buffers(subctx);
  3730. 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.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  3731. }
  3732. if (y_non_contig) {
  3733. 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 });
  3734. }
  3735. uint32_t stride_batch_x = ne00*ne01;
  3736. uint32_t stride_batch_y = ne10*ne11;
  3737. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3738. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3739. }
  3740. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  3741. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3742. }
  3743. // compute
  3744. ggml_vk_matmul(
  3745. ctx, subctx, pipeline,
  3746. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  3747. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  3748. ne01, ne11, ne10,
  3749. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  3750. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  3751. ); // NOLINT
  3752. }
  3753. 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) {
  3754. 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];
  3755. 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];
  3756. 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];
  3757. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  3758. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3759. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3760. const uint64_t ne00 = src0->ne[0];
  3761. const uint64_t ne01 = src0->ne[1];
  3762. const uint64_t ne02 = src0->ne[2];
  3763. const uint64_t ne03 = src0->ne[3];
  3764. const uint64_t ne10 = src1->ne[0];
  3765. const uint64_t ne11 = src1->ne[1];
  3766. const uint64_t ne12 = src1->ne[2];
  3767. const uint64_t ne13 = src1->ne[3];
  3768. const uint64_t ne20 = dst->ne[0];
  3769. const uint64_t ne21 = dst->ne[1];
  3770. const uint64_t ne22 = dst->ne[2];
  3771. const uint64_t ne23 = dst->ne[3];
  3772. const uint64_t r2 = ne12 / ne02;
  3773. const uint64_t r3 = ne13 / ne03;
  3774. // batch_n indicates that we need to compute a few vector results, and this assumes
  3775. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  3776. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  3777. bool batch_n = ne11 > 1;
  3778. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3779. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3780. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3781. vk_buffer d_Qx = nullptr;
  3782. size_t qx_buf_offset = 0;
  3783. vk_buffer d_Qy = nullptr;
  3784. size_t qy_buf_offset = 0;
  3785. bool src0_uma = false;
  3786. bool src1_uma = false;
  3787. if (ctx->device->uma) {
  3788. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3789. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3790. src0_uma = d_Qx != nullptr;
  3791. src1_uma = d_Qy != nullptr;
  3792. }
  3793. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  3794. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  3795. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  3796. const bool qx_needs_dequant = x_non_contig;
  3797. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  3798. // Not implemented
  3799. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3800. const uint64_t x_ne = ne01 * ne00;
  3801. const uint64_t y_ne = ne11 * ne10;
  3802. const uint64_t d_ne = ne11 * ne01;
  3803. 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);
  3804. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3805. 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;
  3806. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  3807. const uint64_t d_sz = sizeof(float) * d_ne;
  3808. vk_pipeline to_fp16_vk_0 = nullptr;
  3809. vk_pipeline to_fp16_vk_1 = nullptr;
  3810. if (x_non_contig) {
  3811. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  3812. }
  3813. if (y_non_contig) {
  3814. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  3815. } else {
  3816. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3817. }
  3818. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  3819. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3820. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3821. GGML_ASSERT(dmmv != nullptr);
  3822. if (dryrun) {
  3823. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3824. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3825. if (
  3826. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3827. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  3828. GGML_ABORT("Requested preallocation size is too large");
  3829. }
  3830. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3831. ctx->prealloc_size_x = x_sz_upd;
  3832. }
  3833. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  3834. ctx->prealloc_size_y = y_sz_upd;
  3835. }
  3836. // Request descriptor sets
  3837. if (qx_needs_dequant) {
  3838. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  3839. }
  3840. if (qy_needs_dequant) {
  3841. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  3842. }
  3843. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  3844. return;
  3845. }
  3846. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3847. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3848. GGML_ASSERT(d_D != nullptr);
  3849. vk_buffer d_X;
  3850. uint64_t x_buf_offset = 0;
  3851. vk_buffer d_Y;
  3852. uint64_t y_buf_offset = 0;
  3853. if(!src0_uma) {
  3854. d_Qx = src0_buf_ctx->dev_buffer;
  3855. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3856. GGML_ASSERT(d_Qx != nullptr);
  3857. }
  3858. if(!src1_uma) {
  3859. d_Qy = src1_buf_ctx->dev_buffer;
  3860. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3861. GGML_ASSERT(d_Qy != nullptr);
  3862. }
  3863. if (qx_needs_dequant) {
  3864. d_X = ctx->prealloc_x;
  3865. } else {
  3866. d_X = d_Qx;
  3867. x_buf_offset = qx_buf_offset;
  3868. GGML_ASSERT(qx_sz == x_sz);
  3869. }
  3870. if (qy_needs_dequant) {
  3871. d_Y = ctx->prealloc_y;
  3872. } else {
  3873. d_Y = d_Qy;
  3874. y_buf_offset = qy_buf_offset;
  3875. GGML_ASSERT(qy_sz == y_sz);
  3876. }
  3877. if (x_non_contig) {
  3878. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  3879. 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 });
  3880. }
  3881. if (y_non_contig) {
  3882. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  3883. 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 });
  3884. }
  3885. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  3886. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  3887. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  3888. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  3889. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3890. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3891. }
  3892. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  3893. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3894. }
  3895. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  3896. uint32_t groups_x = ne01;
  3897. uint32_t groups_z = 1;
  3898. if (ne01 > max_groups_x) {
  3899. groups_z = 64;
  3900. groups_x = CEIL_DIV(groups_x, groups_z);
  3901. }
  3902. // compute
  3903. const vk_mat_vec_push_constants pc = {
  3904. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  3905. stride_batch_x, stride_batch_y, stride_batch_d,
  3906. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  3907. };
  3908. ggml_vk_sync_buffers(subctx);
  3909. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  3910. { 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} },
  3911. sizeof(vk_mat_vec_push_constants), &pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  3912. }
  3913. 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) {
  3914. 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];
  3915. 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];
  3916. 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];
  3917. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3918. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  3919. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  3920. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  3921. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3922. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3923. const uint64_t ne00 = src0->ne[0];
  3924. const uint64_t ne01 = src0->ne[1];
  3925. const uint64_t ne02 = src0->ne[2];
  3926. // const uint64_t ne03 = src0->ne[3];
  3927. const uint64_t ne10 = src1->ne[0];
  3928. const uint64_t ne11 = src1->ne[1];
  3929. const uint64_t ne12 = src1->ne[2];
  3930. // const uint64_t ne13 = src1->ne[3];
  3931. GGML_ASSERT(ne11 == 1);
  3932. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3933. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3934. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3935. vk_buffer d_Qy = nullptr;
  3936. size_t qy_buf_offset = 0;
  3937. bool src1_uma = false;
  3938. if (ctx->device->uma) {
  3939. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3940. src1_uma = d_Qy != nullptr;
  3941. }
  3942. const uint64_t x_ne = ne00 * ne01 * ne02;
  3943. const uint64_t y_ne = ne10 * ne11 * ne12;
  3944. const uint64_t d_ne = ne01 * ne11 * ne12;
  3945. 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);
  3946. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3947. const uint64_t d_sz = sizeof(float) * d_ne;
  3948. if (dryrun) {
  3949. // Request descriptor sets
  3950. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, 1);
  3951. return;
  3952. }
  3953. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3954. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3955. GGML_ASSERT(d_D != nullptr);
  3956. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  3957. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3958. GGML_ASSERT(d_Qx != nullptr);
  3959. if (!src1_uma) {
  3960. d_Qy = src1_buf_ctx->dev_buffer;
  3961. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3962. GGML_ASSERT(d_Qx != nullptr);
  3963. }
  3964. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  3965. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  3966. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  3967. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  3968. // compute
  3969. 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)) };
  3970. ggml_vk_sync_buffers(subctx);
  3971. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, { 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 } }, 6 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  3972. }
  3973. 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) {
  3974. 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];
  3975. 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];
  3976. 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];
  3977. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3978. GGML_ASSERT(!ggml_is_transposed(src0));
  3979. GGML_ASSERT(!ggml_is_transposed(src1));
  3980. GGML_ASSERT(!ggml_is_permuted(src0));
  3981. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3982. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3983. const uint64_t ne00 = src0->ne[0];
  3984. const uint64_t ne01 = src0->ne[1];
  3985. const uint64_t ne02 = src0->ne[2];
  3986. // const uint64_t ne03 = src0->ne[3];
  3987. const uint64_t nb01 = src0->nb[1];
  3988. const uint64_t nb02 = src0->nb[2];
  3989. // const uint64_t ne10 = src1->ne[0];
  3990. const uint64_t ne11 = src1->ne[1];
  3991. const uint64_t ne12 = src1->ne[2];
  3992. // const uint64_t ne13 = src1->ne[3];
  3993. GGML_ASSERT(ne11 == 1);
  3994. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3995. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3996. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3997. vk_buffer d_Qy = nullptr;
  3998. size_t qy_buf_offset = 0;
  3999. bool src1_uma = false;
  4000. if (ctx->device->uma) {
  4001. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4002. src1_uma = d_Qy != nullptr;
  4003. }
  4004. const uint64_t d_ne = ne01 * ne11 * ne12;
  4005. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4006. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4007. const uint64_t qx_sz = ggml_nbytes(src0);
  4008. const uint64_t qy_sz = ggml_nbytes(src1);
  4009. const uint64_t d_sz = sizeof(float) * d_ne;
  4010. if (dryrun) {
  4011. // Request descriptor sets
  4012. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4013. return;
  4014. }
  4015. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4016. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4017. GGML_ASSERT(d_D != nullptr);
  4018. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4019. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4020. GGML_ASSERT(d_Qx != nullptr);
  4021. if (!src1_uma) {
  4022. d_Qy = src1_buf_ctx->dev_buffer;
  4023. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4024. GGML_ASSERT(d_Qx != nullptr);
  4025. }
  4026. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4027. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4028. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4029. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4030. // compute
  4031. const std::array<uint32_t, 7> pc = { (uint32_t)ne00, (uint32_t)ne01, row_stride_x, channel_stride_x, (uint32_t)(ne12 / ne02), (uint32_t)(qy_shader_offset / ggml_type_size(src1->type)), (uint32_t)(d_shader_offset / ggml_type_size(dst->type)) };
  4032. ggml_vk_sync_buffers(subctx);
  4033. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4034. { 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 } }, 7 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  4035. }
  4036. 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) {
  4037. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4038. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4039. // detect 0213 permutation, and batch size of 1
  4040. src0->nb[0] <= src0->nb[2] &&
  4041. src0->nb[2] <= src0->nb[1] &&
  4042. src0->nb[1] <= src0->nb[3] &&
  4043. src1->nb[0] <= src1->nb[2] &&
  4044. src1->nb[2] <= src1->nb[1] &&
  4045. src1->nb[1] <= src1->nb[3] &&
  4046. src0->ne[3] == 1 &&
  4047. src1->ne[3] == 1) {
  4048. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4049. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4050. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4051. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4052. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4053. // when ne12 and ne13 are one.
  4054. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4055. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4056. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4057. } else {
  4058. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4059. }
  4060. }
  4061. 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) {
  4062. 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];
  4063. 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];
  4064. 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];
  4065. 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] << "),)");
  4066. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4067. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4068. const uint64_t ne00 = src0->ne[0];
  4069. const uint64_t ne01 = src0->ne[1];
  4070. const uint64_t ne02 = src0->ne[2];
  4071. const uint64_t ne03 = src0->ne[3];
  4072. const uint64_t ne10 = src1->ne[0];
  4073. const uint64_t ne11 = src1->ne[1];
  4074. const uint64_t ne12 = src1->ne[2];
  4075. const uint64_t ne13 = src1->ne[3];
  4076. const uint64_t nei0 = ids->ne[0];
  4077. const uint64_t nei1 = ids->ne[1];
  4078. GGML_ASSERT(nei0 * nei1 <= 3072);
  4079. const uint32_t nbi1 = ids->nb[1];
  4080. const uint32_t nbi2 = ids->nb[2];
  4081. const uint64_t ne20 = dst->ne[0];
  4082. const uint64_t ne21 = dst->ne[1];
  4083. const uint64_t ne22 = dst->ne[2];
  4084. const uint64_t ne23 = dst->ne[3];
  4085. const uint64_t n_as = ne02;
  4086. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4087. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4088. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4089. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4090. vk_buffer d_Qx = nullptr;
  4091. size_t qx_buf_offset = 0;
  4092. vk_buffer d_Qy = nullptr;
  4093. size_t qy_buf_offset = 0;
  4094. vk_buffer d_ids = nullptr;
  4095. size_t ids_buf_offset = 0;
  4096. bool src0_uma = false;
  4097. bool src1_uma = false;
  4098. bool ids_uma = false;
  4099. if (ctx->device->uma) {
  4100. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4101. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4102. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4103. src0_uma = d_Qx != nullptr;
  4104. src1_uma = d_Qy != nullptr;
  4105. ids_uma = d_ids != nullptr;
  4106. }
  4107. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4108. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4109. !ggml_vk_dim01_contiguous(src0);
  4110. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4111. !ggml_vk_dim01_contiguous(src1);
  4112. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4113. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  4114. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4115. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  4116. if (qx_needs_dequant) {
  4117. // Fall back to dequant + f16 mulmat
  4118. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  4119. }
  4120. // Not implemented
  4121. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4122. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? GGML_TYPE_F16 : src0->type));
  4123. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4124. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type);
  4125. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4126. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4127. const uint64_t x_ne = ne01 * ne00;
  4128. const uint64_t y_ne = padded_n * ne10;
  4129. const uint64_t d_ne = ne21 * ne20;
  4130. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4131. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4132. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4133. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4134. const uint64_t ids_sz = nbi2;
  4135. const uint64_t d_sz = sizeof(float) * d_ne;
  4136. vk_pipeline to_fp16_vk_0 = nullptr;
  4137. vk_pipeline to_fp16_vk_1 = nullptr;
  4138. if (x_non_contig) {
  4139. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  4140. } else {
  4141. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4142. }
  4143. if (y_non_contig) {
  4144. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  4145. } else {
  4146. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4147. }
  4148. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4149. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4150. if (dryrun) {
  4151. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4152. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4153. if (
  4154. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4155. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4156. GGML_ABORT("Requested preallocation size is too large");
  4157. }
  4158. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4159. ctx->prealloc_size_x = x_sz_upd;
  4160. }
  4161. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4162. ctx->prealloc_size_y = y_sz_upd;
  4163. }
  4164. // Request descriptor sets
  4165. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4166. if (qx_needs_dequant) {
  4167. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4168. }
  4169. if (qy_needs_dequant) {
  4170. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4171. }
  4172. return;
  4173. }
  4174. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4175. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4176. GGML_ASSERT(d_D != nullptr);
  4177. vk_buffer d_X;
  4178. uint64_t x_buf_offset = 0;
  4179. vk_buffer d_Y;
  4180. uint64_t y_buf_offset = 0;
  4181. if (!src0_uma) {
  4182. d_Qx = src0_buf_ctx->dev_buffer;
  4183. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4184. GGML_ASSERT(d_Qx != nullptr);
  4185. }
  4186. if (!src1_uma) {
  4187. d_Qy = src1_buf_ctx->dev_buffer;
  4188. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4189. GGML_ASSERT(d_Qy != nullptr);
  4190. }
  4191. if (!ids_uma) {
  4192. d_ids = ids_buf_ctx->dev_buffer;
  4193. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4194. GGML_ASSERT(d_ids != nullptr);
  4195. }
  4196. if (qx_needs_dequant) {
  4197. d_X = ctx->prealloc_x;
  4198. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4199. } else {
  4200. d_X = d_Qx;
  4201. x_buf_offset = qx_buf_offset;
  4202. GGML_ASSERT(qx_sz == x_sz);
  4203. }
  4204. if (qy_needs_dequant) {
  4205. d_Y = ctx->prealloc_y;
  4206. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4207. } else {
  4208. d_Y = d_Qy;
  4209. y_buf_offset = qy_buf_offset;
  4210. GGML_ASSERT(qy_sz == y_sz);
  4211. }
  4212. if (x_non_contig) {
  4213. 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 });
  4214. } else if (qx_needs_dequant) {
  4215. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4216. ggml_vk_sync_buffers(subctx);
  4217. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  4218. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  4219. }
  4220. if (y_non_contig) {
  4221. 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 });
  4222. }
  4223. uint32_t stride_batch_x = ne00*ne01;
  4224. uint32_t stride_batch_y = ne10*ne11;
  4225. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4226. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4227. }
  4228. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4229. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4230. }
  4231. // compute
  4232. ggml_vk_matmul_id(
  4233. ctx, subctx, pipeline,
  4234. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4235. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  4236. ne01, ne21, ne10, ne10, ne10, ne01,
  4237. stride_batch_x, stride_batch_y, ne20*ne21,
  4238. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  4239. ); // NOLINT
  4240. }
  4241. 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) {
  4242. 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];
  4243. 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];
  4244. 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];
  4245. 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];
  4246. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4247. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  4248. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4249. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4250. const uint64_t ne00 = src0->ne[0];
  4251. const uint64_t ne01 = src0->ne[1];
  4252. const uint64_t ne02 = src0->ne[2];
  4253. const uint64_t ne03 = src0->ne[3];
  4254. const uint64_t ne10 = src1->ne[0];
  4255. const uint64_t ne11 = src1->ne[1];
  4256. const uint64_t ne12 = src1->ne[2];
  4257. const uint64_t ne13 = src1->ne[3];
  4258. const uint64_t nei0 = ids->ne[0];
  4259. const uint64_t nei1 = ids->ne[1];
  4260. const uint64_t nbi2 = ids->nb[2];
  4261. GGML_ASSERT(nei1 == 1);
  4262. const uint64_t ne20 = dst->ne[0];
  4263. const uint64_t ne21 = dst->ne[1];
  4264. const uint64_t ne22 = dst->ne[2];
  4265. const uint64_t ne23 = dst->ne[3];
  4266. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4267. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4268. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4269. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4270. vk_buffer d_Qx = nullptr;
  4271. size_t qx_buf_offset = 0;
  4272. vk_buffer d_Qy = nullptr;
  4273. size_t qy_buf_offset = 0;
  4274. vk_buffer d_ids = nullptr;
  4275. size_t ids_buf_offset = 0;
  4276. bool src0_uma = false;
  4277. bool src1_uma = false;
  4278. bool ids_uma = false;
  4279. if (ctx->device->uma) {
  4280. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4281. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4282. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4283. src0_uma = d_Qx != nullptr;
  4284. src1_uma = d_Qy != nullptr;
  4285. ids_uma = d_ids != nullptr;
  4286. }
  4287. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4288. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4289. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4290. const bool qx_needs_dequant = x_non_contig;
  4291. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4292. // Not implemented
  4293. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4294. const uint64_t x_ne = ne01 * ne00;
  4295. const uint64_t y_ne = ne11 * ne10;
  4296. const uint64_t d_ne = ne21 * ne20;
  4297. 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);
  4298. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4299. 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;
  4300. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4301. const uint64_t ids_sz = nbi2;
  4302. const uint64_t d_sz = sizeof(float) * d_ne;
  4303. vk_pipeline to_fp16_vk_0 = nullptr;
  4304. vk_pipeline to_fp16_vk_1 = nullptr;
  4305. if (x_non_contig) {
  4306. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4307. }
  4308. if (y_non_contig) {
  4309. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4310. } else {
  4311. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4312. }
  4313. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  4314. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4315. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4316. GGML_ASSERT(dmmv != nullptr);
  4317. if (dryrun) {
  4318. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4319. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4320. if (
  4321. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4322. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4323. GGML_ABORT("Requested preallocation size is too large");
  4324. }
  4325. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4326. ctx->prealloc_size_x = x_sz_upd;
  4327. }
  4328. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4329. ctx->prealloc_size_y = y_sz_upd;
  4330. }
  4331. // Request descriptor sets
  4332. if (qx_needs_dequant) {
  4333. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4334. }
  4335. if (qy_needs_dequant) {
  4336. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4337. }
  4338. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  4339. return;
  4340. }
  4341. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4342. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4343. GGML_ASSERT(d_D != nullptr);
  4344. vk_buffer d_X;
  4345. uint64_t x_buf_offset = 0;
  4346. vk_buffer d_Y;
  4347. uint64_t y_buf_offset = 0;
  4348. if(!src0_uma) {
  4349. d_Qx = src0_buf_ctx->dev_buffer;
  4350. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4351. GGML_ASSERT(d_Qx != nullptr);
  4352. }
  4353. if(!src1_uma) {
  4354. d_Qy = src1_buf_ctx->dev_buffer;
  4355. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4356. GGML_ASSERT(d_Qy != nullptr);
  4357. }
  4358. if(!ids_uma) {
  4359. d_ids = ids_buf_ctx->dev_buffer;
  4360. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4361. GGML_ASSERT(d_ids != nullptr);
  4362. }
  4363. if (qx_needs_dequant) {
  4364. d_X = ctx->prealloc_x;
  4365. } else {
  4366. d_X = d_Qx;
  4367. x_buf_offset = qx_buf_offset;
  4368. GGML_ASSERT(qx_sz == x_sz);
  4369. }
  4370. if (qy_needs_dequant) {
  4371. d_Y = ctx->prealloc_y;
  4372. } else {
  4373. d_Y = d_Qy;
  4374. y_buf_offset = qy_buf_offset;
  4375. GGML_ASSERT(qy_sz == y_sz);
  4376. }
  4377. if (x_non_contig) {
  4378. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4379. 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 });
  4380. }
  4381. if (y_non_contig) {
  4382. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4383. 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 });
  4384. }
  4385. uint32_t stride_batch_y = ne10*ne11;
  4386. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4387. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4388. }
  4389. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4390. uint32_t groups_x = ne01;
  4391. uint32_t groups_z = 1;
  4392. if (ne01 > max_groups_x) {
  4393. groups_z = 64;
  4394. groups_x = CEIL_DIV(groups_x, groups_z);
  4395. }
  4396. // compute
  4397. const vk_mat_vec_id_push_constants pc = {
  4398. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4399. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  4400. (uint32_t)nei0, (uint32_t)ne11,
  4401. };
  4402. ggml_vk_sync_buffers(subctx);
  4403. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4404. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  4405. 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 } },
  4406. sizeof(vk_mat_vec_id_push_constants), &pc, { groups_x, (uint32_t)nei0, groups_z });
  4407. }
  4408. 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) {
  4409. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  4410. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4411. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4412. } else {
  4413. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4414. }
  4415. }
  4416. 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) {
  4417. 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];
  4418. 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];
  4419. 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];
  4420. 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];
  4421. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4422. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  4423. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  4424. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  4425. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  4426. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  4427. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  4428. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  4429. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  4430. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  4431. const uint32_t nbm1 = mask ? mask->nb[1] : 0;
  4432. const uint32_t D = neq0;
  4433. const uint32_t N = neq1;
  4434. const uint32_t KV = nek1;
  4435. GGML_ASSERT(ne0 == D);
  4436. GGML_ASSERT(ne2 == N);
  4437. // input tensor rows must be contiguous
  4438. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  4439. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  4440. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  4441. GGML_ASSERT(neq0 == D);
  4442. GGML_ASSERT(nek0 == D);
  4443. GGML_ASSERT(nev0 == D);
  4444. GGML_ASSERT(neq1 == N);
  4445. GGML_ASSERT(nev0 == D);
  4446. GGML_ASSERT(nev1 == nek1);
  4447. // dst cannot be transposed or permuted
  4448. GGML_ASSERT(nb0 == sizeof(float));
  4449. GGML_ASSERT(nb0 <= nb1);
  4450. GGML_ASSERT(nb1 <= nb2);
  4451. GGML_ASSERT(nb2 <= nb3);
  4452. assert(dst->type == GGML_TYPE_F32);
  4453. assert(q->type == GGML_TYPE_F32);
  4454. assert(k->type == v->type);
  4455. vk_pipeline *pipelines;
  4456. // XXX TODO other backends may be changing accumulator precision to default to f32 soon
  4457. bool f32acc = dst->op_params[3] == GGML_PREC_F32;
  4458. bool small_rows = N <= flash_attention_num_small_rows;
  4459. switch (D) {
  4460. case 64: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D64[k->type][f32acc][small_rows][0]; break;
  4461. case 80: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D80[k->type][f32acc][small_rows][0]; break;
  4462. case 96: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D96[k->type][f32acc][small_rows][0]; break;
  4463. case 112: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D112[k->type][f32acc][small_rows][0]; break;
  4464. case 128: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D128[k->type][f32acc][small_rows][0]; break;
  4465. case 256: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D256[k->type][f32acc][small_rows][0]; break;
  4466. default:
  4467. assert(!"unsupported D value");
  4468. return;
  4469. }
  4470. assert(pipelines);
  4471. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  4472. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  4473. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  4474. bool aligned = (KV % pipelines[1]->align) == 0 &&
  4475. // the "aligned" shader variant will forcibly align strides, for performance
  4476. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  4477. vk_pipeline pipeline = pipelines[aligned];
  4478. assert(pipeline);
  4479. if (dryrun) {
  4480. // Request descriptor sets
  4481. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4482. return;
  4483. }
  4484. float scale = 1.0f;
  4485. float max_bias = 0.0f;
  4486. float logit_softcap = 0.0f;
  4487. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  4488. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  4489. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  4490. if (logit_softcap != 0) {
  4491. scale /= logit_softcap;
  4492. }
  4493. const uint32_t n_head_kv = neq2;
  4494. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  4495. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  4496. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  4497. ggml_vk_sync_buffers(subctx);
  4498. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  4499. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  4500. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  4501. if (ctx->device->uma) {
  4502. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  4503. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  4504. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  4505. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  4506. Q_uma = d_Q != nullptr;
  4507. K_uma = d_K != nullptr;
  4508. V_uma = d_V != nullptr;
  4509. D_uma = d_D != nullptr;
  4510. if (mask) {
  4511. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  4512. M_uma = d_M != nullptr;
  4513. }
  4514. }
  4515. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4516. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  4517. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  4518. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  4519. if (!Q_uma) {
  4520. d_Q = q_buf_ctx->dev_buffer;
  4521. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  4522. }
  4523. if (!K_uma) {
  4524. d_K = k_buf_ctx->dev_buffer;
  4525. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  4526. }
  4527. if (!V_uma) {
  4528. d_V = v_buf_ctx->dev_buffer;
  4529. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  4530. }
  4531. if (!D_uma) {
  4532. d_D = d_buf_ctx->dev_buffer;
  4533. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4534. }
  4535. if (!M_uma) {
  4536. d_M = d_Q;
  4537. m_buf_offset = q_buf_offset;
  4538. if (mask) {
  4539. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  4540. d_M = m_buf_ctx->dev_buffer;
  4541. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  4542. }
  4543. }
  4544. const vk_flash_attn_push_constants pc = { N, KV,
  4545. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  4546. (uint32_t)neq2, (uint32_t)neq3,
  4547. (uint32_t)nek2, (uint32_t)nek3,
  4548. (uint32_t)nev2, (uint32_t)nev3,
  4549. nem1,
  4550. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  4551. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  4552. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  4553. nbm1,
  4554. scale, max_bias, logit_softcap,
  4555. mask != nullptr, n_head_log2, m0, m1 };
  4556. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  4557. {
  4558. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  4559. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  4560. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  4561. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  4562. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  4563. },
  4564. sizeof(vk_flash_attn_push_constants), &pc, { (uint32_t)neq1, (uint32_t)neq2, (uint32_t)neq3 });
  4565. }
  4566. 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) {
  4567. switch (op) {
  4568. case GGML_OP_GET_ROWS:
  4569. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4570. if (dst->type == GGML_TYPE_F16) {
  4571. return ctx->device->pipeline_get_rows[src0->type];
  4572. }
  4573. if (dst->type == GGML_TYPE_F32) {
  4574. return ctx->device->pipeline_get_rows_f32[src0->type];
  4575. }
  4576. return nullptr;
  4577. case GGML_OP_ACC:
  4578. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4579. return ctx->device->pipeline_acc_f32;
  4580. }
  4581. return nullptr;
  4582. case GGML_OP_ADD:
  4583. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4584. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f32_norepeat : ctx->device->pipeline_add_f32;
  4585. }
  4586. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4587. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f16_f32_f16_norepeat : ctx->device->pipeline_add_f16_f32_f16;
  4588. }
  4589. return nullptr;
  4590. case GGML_OP_SUB:
  4591. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4592. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_f32_norepeat : ctx->device->pipeline_sub_f32;
  4593. }
  4594. return nullptr;
  4595. case GGML_OP_MUL:
  4596. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4597. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_f32_norepeat : ctx->device->pipeline_mul_f32;
  4598. }
  4599. return nullptr;
  4600. case GGML_OP_DIV:
  4601. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4602. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_f32_norepeat : ctx->device->pipeline_div_f32;
  4603. }
  4604. return nullptr;
  4605. case GGML_OP_CONCAT:
  4606. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4607. return ctx->device->pipeline_concat_f32;
  4608. }
  4609. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4610. return ctx->device->pipeline_concat_f16;
  4611. }
  4612. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  4613. return ctx->device->pipeline_concat_i32;
  4614. }
  4615. return nullptr;
  4616. case GGML_OP_UPSCALE:
  4617. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4618. return ctx->device->pipeline_upscale_f32;
  4619. }
  4620. return nullptr;
  4621. case GGML_OP_SCALE:
  4622. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4623. return ctx->device->pipeline_scale_f32;
  4624. }
  4625. return nullptr;
  4626. case GGML_OP_SQR:
  4627. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4628. return ctx->device->pipeline_sqr_f32;
  4629. }
  4630. return nullptr;
  4631. case GGML_OP_SIN:
  4632. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4633. return ctx->device->pipeline_sin_f32;
  4634. }
  4635. return nullptr;
  4636. case GGML_OP_COS:
  4637. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4638. return ctx->device->pipeline_cos_f32;
  4639. }
  4640. return nullptr;
  4641. case GGML_OP_CLAMP:
  4642. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4643. return ctx->device->pipeline_clamp_f32;
  4644. }
  4645. return nullptr;
  4646. case GGML_OP_PAD:
  4647. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4648. return ctx->device->pipeline_pad_f32;
  4649. }
  4650. return nullptr;
  4651. case GGML_OP_REPEAT:
  4652. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  4653. return ctx->device->pipeline_repeat_f32;
  4654. }
  4655. return nullptr;
  4656. case GGML_OP_REPEAT_BACK:
  4657. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4658. return ctx->device->pipeline_repeat_back_f32;
  4659. }
  4660. return nullptr;
  4661. case GGML_OP_CPY:
  4662. case GGML_OP_CONT:
  4663. case GGML_OP_DUP:
  4664. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  4665. case GGML_OP_SILU_BACK:
  4666. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4667. return ctx->device->pipeline_silu_back_f32;
  4668. }
  4669. return nullptr;
  4670. case GGML_OP_NORM:
  4671. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4672. return ctx->device->pipeline_norm_f32;
  4673. }
  4674. return nullptr;
  4675. case GGML_OP_GROUP_NORM:
  4676. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4677. return ctx->device->pipeline_group_norm_f32;
  4678. }
  4679. return nullptr;
  4680. case GGML_OP_RMS_NORM:
  4681. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4682. return ctx->device->pipeline_rms_norm_f32;
  4683. }
  4684. return nullptr;
  4685. case GGML_OP_RMS_NORM_BACK:
  4686. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4687. return ctx->device->pipeline_rms_norm_back_f32;
  4688. }
  4689. return nullptr;
  4690. case GGML_OP_L2_NORM:
  4691. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4692. return ctx->device->pipeline_l2_norm_f32;
  4693. }
  4694. return nullptr;
  4695. case GGML_OP_UNARY:
  4696. switch (ggml_get_unary_op(dst)) {
  4697. case GGML_UNARY_OP_SILU:
  4698. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4699. return ctx->device->pipeline_silu_f32;
  4700. }
  4701. break;
  4702. case GGML_UNARY_OP_GELU:
  4703. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4704. return ctx->device->pipeline_gelu_f32;
  4705. }
  4706. break;
  4707. case GGML_UNARY_OP_GELU_QUICK:
  4708. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4709. return ctx->device->pipeline_gelu_quick_f32;
  4710. }
  4711. break;
  4712. case GGML_UNARY_OP_RELU:
  4713. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4714. return ctx->device->pipeline_relu_f32;
  4715. }
  4716. break;
  4717. case GGML_UNARY_OP_TANH:
  4718. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4719. return ctx->device->pipeline_tanh_f32;
  4720. }
  4721. break;
  4722. case GGML_UNARY_OP_SIGMOID:
  4723. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4724. return ctx->device->pipeline_sigmoid_f32;
  4725. }
  4726. break;
  4727. default:
  4728. break;
  4729. }
  4730. return nullptr;
  4731. case GGML_OP_DIAG_MASK_INF:
  4732. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4733. return ctx->device->pipeline_diag_mask_inf_f32;
  4734. }
  4735. return nullptr;
  4736. case GGML_OP_SOFT_MAX:
  4737. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  4738. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  4739. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  4740. }
  4741. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  4742. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  4743. }
  4744. return nullptr;
  4745. case GGML_OP_SOFT_MAX_BACK:
  4746. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4747. return ctx->device->pipeline_soft_max_back_f32;
  4748. }
  4749. return nullptr;
  4750. case GGML_OP_ROPE:
  4751. case GGML_OP_ROPE_BACK:
  4752. {
  4753. const int mode = ((const int32_t *) dst->op_params)[2];
  4754. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  4755. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  4756. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  4757. if (is_neox) {
  4758. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4759. return ctx->device->pipeline_rope_neox_f32;
  4760. }
  4761. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4762. return ctx->device->pipeline_rope_neox_f16;
  4763. }
  4764. } else if (is_mrope && !is_vision) {
  4765. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4766. return ctx->device->pipeline_rope_multi_f32;
  4767. }
  4768. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4769. return ctx->device->pipeline_rope_multi_f16;
  4770. }
  4771. } else if (is_vision) {
  4772. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4773. return ctx->device->pipeline_rope_vision_f32;
  4774. }
  4775. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4776. return ctx->device->pipeline_rope_vision_f16;
  4777. }
  4778. } else {
  4779. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4780. return ctx->device->pipeline_rope_norm_f32;
  4781. }
  4782. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4783. return ctx->device->pipeline_rope_norm_f16;
  4784. }
  4785. }
  4786. return nullptr;
  4787. }
  4788. case GGML_OP_ARGSORT:
  4789. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  4790. return ctx->device->pipeline_argsort_f32;
  4791. }
  4792. return nullptr;
  4793. case GGML_OP_SUM:
  4794. case GGML_OP_SUM_ROWS:
  4795. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4796. return ctx->device->pipeline_sum_rows_f32;
  4797. }
  4798. return nullptr;
  4799. case GGML_OP_ARGMAX:
  4800. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  4801. return ctx->device->pipeline_argmax_f32;
  4802. }
  4803. return nullptr;
  4804. case GGML_OP_COUNT_EQUAL:
  4805. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  4806. return ctx->device->pipeline_count_equal_i32;
  4807. }
  4808. return nullptr;
  4809. case GGML_OP_IM2COL:
  4810. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4811. return ctx->device->pipeline_im2col_f32;
  4812. }
  4813. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4814. return ctx->device->pipeline_im2col_f32_f16;
  4815. }
  4816. return nullptr;
  4817. case GGML_OP_TIMESTEP_EMBEDDING:
  4818. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4819. return ctx->device->pipeline_timestep_embedding_f32;
  4820. }
  4821. return nullptr;
  4822. case GGML_OP_POOL_2D:
  4823. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4824. return ctx->device->pipeline_pool2d_f32;
  4825. }
  4826. return nullptr;
  4827. case GGML_OP_RWKV_WKV6:
  4828. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4829. return ctx->device->pipeline_rwkv_wkv6_f32;
  4830. }
  4831. return nullptr;
  4832. case GGML_OP_RWKV_WKV7:
  4833. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4834. return ctx->device->pipeline_rwkv_wkv7_f32;
  4835. }
  4836. return nullptr;
  4837. case GGML_OP_OPT_STEP_ADAMW:
  4838. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4839. return ctx->device->pipeline_opt_step_adamw_f32;
  4840. }
  4841. return nullptr;
  4842. case GGML_OP_LEAKY_RELU:
  4843. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4844. return ctx->device->pipeline_leaky_relu_f32;
  4845. }
  4846. return nullptr;
  4847. default:
  4848. return nullptr;
  4849. }
  4850. GGML_UNUSED(src2);
  4851. }
  4852. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  4853. switch (op) {
  4854. case GGML_OP_CPY:
  4855. case GGML_OP_GET_ROWS:
  4856. case GGML_OP_ADD:
  4857. case GGML_OP_SUB:
  4858. case GGML_OP_MUL:
  4859. case GGML_OP_DIV:
  4860. case GGML_OP_CONCAT:
  4861. case GGML_OP_UPSCALE:
  4862. case GGML_OP_SQR:
  4863. case GGML_OP_SIN:
  4864. case GGML_OP_COS:
  4865. case GGML_OP_CLAMP:
  4866. case GGML_OP_PAD:
  4867. case GGML_OP_REPEAT:
  4868. case GGML_OP_REPEAT_BACK:
  4869. case GGML_OP_ROPE:
  4870. return true;
  4871. default:
  4872. return false;
  4873. }
  4874. }
  4875. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  4876. {
  4877. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  4878. }
  4879. 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) {
  4880. GGML_UNUSED(p);
  4881. GGML_UNUSED(src0);
  4882. GGML_UNUSED(src1);
  4883. GGML_UNUSED(src2);
  4884. GGML_UNUSED(dst);
  4885. static_assert(!std::is_const<T>::value, "unexpected type");
  4886. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  4887. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  4888. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  4889. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  4890. }
  4891. 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) {
  4892. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  4893. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  4894. p.misalign_offsets = (a_offset << 16) | d_offset;
  4895. GGML_UNUSED(src1);
  4896. GGML_UNUSED(src2);
  4897. }
  4898. 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) {
  4899. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  4900. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  4901. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  4902. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  4903. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  4904. GGML_UNUSED(src2);
  4905. }
  4906. 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) {
  4907. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  4908. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  4909. p.a_offset = a_offset;
  4910. p.d_offset = d_offset;
  4911. GGML_UNUSED(src1);
  4912. GGML_UNUSED(src2);
  4913. }
  4914. template<typename PC>
  4915. 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) {
  4916. 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];
  4917. if (src1 != nullptr) {
  4918. 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];
  4919. }
  4920. if (src2 != nullptr) {
  4921. 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];
  4922. }
  4923. 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];
  4924. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  4925. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  4926. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  4927. GGML_ASSERT(dst->buffer != nullptr);
  4928. const uint64_t ne00 = src0->ne[0];
  4929. const uint64_t ne01 = src0->ne[1];
  4930. const uint64_t ne02 = src0->ne[2];
  4931. const uint64_t ne03 = src0->ne[3];
  4932. const uint64_t ne0 = ne00 * ne01;
  4933. const bool use_src1 = src1 != nullptr;
  4934. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  4935. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  4936. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  4937. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  4938. const uint64_t ne1 = ne10 * ne11;
  4939. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  4940. const bool use_src2 = src2 != nullptr;
  4941. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  4942. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  4943. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  4944. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  4945. const uint64_t ne2 = ne20 * ne21;
  4946. const uint64_t ned0 = dst->ne[0];
  4947. const uint64_t ned1 = dst->ne[1];
  4948. const uint64_t ned2 = dst->ne[2];
  4949. const uint64_t ned3 = dst->ne[3];
  4950. const uint64_t ned = ned0 * ned1;
  4951. init_pushconst_fastdiv(pc);
  4952. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  4953. if (pipeline == nullptr) {
  4954. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  4955. if (src1 != nullptr) {
  4956. std::cerr << " and " << ggml_type_name(src1->type);
  4957. }
  4958. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  4959. GGML_ABORT("fatal error");
  4960. }
  4961. if (dryrun) {
  4962. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4963. return;
  4964. }
  4965. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  4966. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4967. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4968. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  4969. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  4970. vk_buffer d_X = nullptr;
  4971. size_t x_buf_offset = 0;
  4972. vk_buffer d_Y = nullptr;
  4973. size_t y_buf_offset = 0;
  4974. vk_buffer d_Z = nullptr;
  4975. size_t z_buf_offset = 0;
  4976. bool src0_uma = false;
  4977. bool src1_uma = false;
  4978. bool src2_uma = false;
  4979. if (ctx->device->uma) {
  4980. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  4981. src0_uma = d_X != nullptr;
  4982. if (use_src1) {
  4983. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  4984. src1_uma = d_Y != nullptr;
  4985. }
  4986. if (use_src2) {
  4987. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  4988. src2_uma = d_Z != nullptr;
  4989. }
  4990. }
  4991. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  4992. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  4993. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  4994. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  4995. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4996. // Workaround for tiny tensor inputs on ROPE
  4997. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  4998. y_sz = VK_WHOLE_SIZE;
  4999. }
  5000. GGML_ASSERT(d_D != nullptr);
  5001. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5002. if(!src0_uma) {
  5003. d_X = src0_buf_ctx->dev_buffer;
  5004. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5005. GGML_ASSERT(d_X != nullptr);
  5006. }
  5007. if (use_src1 && !src1_uma) {
  5008. d_Y = src1_buf_ctx->dev_buffer;
  5009. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5010. GGML_ASSERT(d_Y != nullptr);
  5011. }
  5012. if (use_src2 && !src2_uma) {
  5013. d_Z = src2_buf_ctx->dev_buffer;
  5014. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  5015. GGML_ASSERT(d_Z != nullptr);
  5016. }
  5017. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  5018. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  5019. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5020. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5021. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5022. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5023. if (op_supports_incontiguous) {
  5024. x_sz = ggml_nbytes(src0);
  5025. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  5026. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  5027. d_sz = ggml_nbytes(dst);
  5028. if (x_buf_offset + x_sz >= d_X->size) {
  5029. x_sz = VK_WHOLE_SIZE;
  5030. }
  5031. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  5032. y_sz = VK_WHOLE_SIZE;
  5033. }
  5034. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  5035. z_sz = VK_WHOLE_SIZE;
  5036. }
  5037. if (d_buf_offset + d_sz >= d_D->size) {
  5038. d_sz = VK_WHOLE_SIZE;
  5039. }
  5040. }
  5041. std::array<uint32_t, 3> elements;
  5042. // Single call if dimension 2 is contiguous
  5043. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  5044. switch (op) {
  5045. case GGML_OP_NORM:
  5046. case GGML_OP_RMS_NORM:
  5047. case GGML_OP_RMS_NORM_BACK:
  5048. case GGML_OP_L2_NORM:
  5049. case GGML_OP_SOFT_MAX:
  5050. case GGML_OP_SOFT_MAX_BACK:
  5051. case GGML_OP_SUM_ROWS:
  5052. case GGML_OP_ARGMAX:
  5053. {
  5054. const uint32_t nr = ggml_nrows(src0);
  5055. if (nr > 262144) {
  5056. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  5057. } else if (nr > 512) {
  5058. elements = { 512, CEIL_DIV(nr, 512), 1 };
  5059. } else {
  5060. elements = { nr, 1, 1 };
  5061. }
  5062. } break;
  5063. case GGML_OP_SUM:
  5064. // We use GGML_OP_SUM_ROWS with 1 row.
  5065. elements = { 1, 1, 1 };
  5066. break;
  5067. case GGML_OP_GROUP_NORM:
  5068. {
  5069. const uint32_t num_groups = dst->op_params[0];
  5070. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  5071. } break;
  5072. case GGML_OP_DIAG_MASK_INF:
  5073. case GGML_OP_ROPE:
  5074. case GGML_OP_ROPE_BACK:
  5075. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  5076. break;
  5077. case GGML_OP_GET_ROWS:
  5078. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  5079. break;
  5080. case GGML_OP_ARGSORT:
  5081. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  5082. break;
  5083. case GGML_OP_IM2COL:
  5084. {
  5085. const bool is_2D = dst->op_params[6] == 1;
  5086. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5087. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5088. const uint32_t KW = src0->ne[0];
  5089. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5090. const uint32_t OW = dst->ne[1];
  5091. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  5092. elements = { OW * KW * KH, OH, batch * IC };
  5093. } break;
  5094. case GGML_OP_TIMESTEP_EMBEDDING:
  5095. {
  5096. const uint32_t dim = dst->op_params[0];
  5097. uint32_t half_ceil = (dim + 1) / 2;
  5098. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  5099. } break;
  5100. case GGML_OP_POOL_2D:
  5101. {
  5102. const uint32_t N = dst->ne[3];
  5103. const uint32_t OC = dst->ne[2];
  5104. const uint32_t OH = dst->ne[1];
  5105. const uint32_t OW = dst->ne[0];
  5106. elements = { N * OC * OH * OW, 1, 1};
  5107. } break;
  5108. case GGML_OP_ADD:
  5109. case GGML_OP_SUB:
  5110. case GGML_OP_DIV:
  5111. case GGML_OP_MUL:
  5112. case GGML_OP_SCALE:
  5113. case GGML_OP_SQR:
  5114. case GGML_OP_SIN:
  5115. case GGML_OP_COS:
  5116. case GGML_OP_CLAMP:
  5117. case GGML_OP_PAD:
  5118. case GGML_OP_REPEAT:
  5119. case GGML_OP_REPEAT_BACK:
  5120. case GGML_OP_CPY:
  5121. case GGML_OP_CONCAT:
  5122. case GGML_OP_UPSCALE:
  5123. case GGML_OP_UNARY:
  5124. {
  5125. const uint32_t ne = ggml_nelements(dst);
  5126. if (ne > 262144) {
  5127. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  5128. } else if (ne > 512) {
  5129. elements = { 512, CEIL_DIV(ne, 512), 1 };
  5130. } else {
  5131. elements = { ne, 1, 1 };
  5132. }
  5133. } break;
  5134. default:
  5135. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  5136. break;
  5137. }
  5138. if (!op_supports_incontiguous) {
  5139. if (x_sz != VK_WHOLE_SIZE) {
  5140. x_sz *= ne02 * ne03;
  5141. }
  5142. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  5143. y_sz *= ne12 * ne13;
  5144. }
  5145. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  5146. z_sz *= ne22 * ne23;
  5147. }
  5148. if (d_sz != VK_WHOLE_SIZE) {
  5149. d_sz *= ned2 * ned3;
  5150. }
  5151. }
  5152. if (op == GGML_OP_SOFT_MAX) {
  5153. // Empty src1 is possible in soft_max, but the shader needs a buffer
  5154. vk_subbuffer subbuf_y;
  5155. if (use_src1) {
  5156. subbuf_y = { d_Y, y_buf_offset, y_sz };
  5157. } else {
  5158. subbuf_y = { d_X, 0, x_sz };
  5159. }
  5160. ggml_vk_sync_buffers(subctx);
  5161. 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 } }, sizeof(PC), &pc, elements);
  5162. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  5163. // Empty src2 is possible in rope, but the shader needs a buffer
  5164. vk_subbuffer subbuf_z;
  5165. if (use_src2) {
  5166. subbuf_z = { d_Z, z_buf_offset, z_sz };
  5167. } else {
  5168. subbuf_z = { d_X, 0, x_sz };
  5169. }
  5170. ggml_vk_sync_buffers(subctx);
  5171. 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 } }, sizeof(PC), &pc, elements);
  5172. } else if (op == GGML_OP_IM2COL) {
  5173. // im2col uses only src1 and dst buffers
  5174. ggml_vk_sync_buffers(subctx);
  5175. 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 } }, sizeof(PC), &pc, elements);
  5176. } else if (op == GGML_OP_COUNT_EQUAL) {
  5177. ggml_vk_sync_buffers(subctx);
  5178. // count_equal assumes that destination buffer is initialized with zeroes
  5179. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  5180. ggml_vk_sync_buffers(subctx);
  5181. 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 } }, sizeof(PC), &pc, elements);
  5182. } else if (use_src2) {
  5183. ggml_vk_sync_buffers(subctx);
  5184. 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 } }, sizeof(PC), &pc, elements);
  5185. } else if (use_src1) {
  5186. ggml_vk_sync_buffers(subctx);
  5187. 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 } }, sizeof(PC), &pc, elements);
  5188. } else {
  5189. ggml_vk_sync_buffers(subctx);
  5190. 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 } }, sizeof(PC), &pc, elements);
  5191. }
  5192. }
  5193. 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) {
  5194. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5195. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5196. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5197. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  5198. (uint32_t)ggml_nelements(src0),
  5199. (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,
  5200. (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,
  5201. (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,
  5202. 0,
  5203. 0.0f, 0.0f, 0,
  5204. }, dryrun);
  5205. }
  5206. 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) {
  5207. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5208. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5209. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5210. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5211. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5212. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5213. int offset = dst->op_params[3] / 4; // offset in bytes
  5214. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  5215. (uint32_t)ggml_nelements(src0),
  5216. (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,
  5217. (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,
  5218. (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,
  5219. 0,
  5220. 0.0f, 0.0f, offset,
  5221. }, dryrun);
  5222. }
  5223. 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) {
  5224. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5225. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5226. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5227. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  5228. (uint32_t)ggml_nelements(src0),
  5229. (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,
  5230. (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,
  5231. (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,
  5232. 0,
  5233. 0.0f, 0.0f, 0,
  5234. }, dryrun);
  5235. }
  5236. 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) {
  5237. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5238. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5239. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5240. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  5241. (uint32_t)ggml_nelements(src0),
  5242. (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,
  5243. (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,
  5244. (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,
  5245. 0,
  5246. 0.0f, 0.0f, 0,
  5247. }, dryrun);
  5248. }
  5249. 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) {
  5250. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5251. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5252. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5253. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  5254. (uint32_t)ggml_nelements(src0),
  5255. (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,
  5256. (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,
  5257. (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,
  5258. 0,
  5259. 0.0f, 0.0f, 0,
  5260. }, dryrun);
  5261. }
  5262. 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) {
  5263. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5264. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5265. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5266. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  5267. (uint32_t)ggml_nelements(src0),
  5268. (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,
  5269. (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,
  5270. (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,
  5271. 0,
  5272. 0.0f, 0.0f, 0,
  5273. }, dryrun);
  5274. }
  5275. 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) {
  5276. GGML_ASSERT(version == 6 || version == 7);
  5277. int num_srcs = version == 6 ? 6 : 7;
  5278. for (int i = 0; i < num_srcs; i++) {
  5279. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  5280. }
  5281. GGML_ASSERT(dst->buffer != nullptr);
  5282. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  5283. GGML_ASSERT(pipeline != nullptr);
  5284. if (dryrun) {
  5285. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5286. return;
  5287. }
  5288. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5289. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5290. for (int i = 0; i < num_srcs; i++) {
  5291. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  5292. }
  5293. ggml_vk_sync_buffers(subctx);
  5294. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5295. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5296. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  5297. if (ctx->device->uma) {
  5298. for (int i = 0; i < num_srcs; i++) {
  5299. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  5300. srcs_uma[i] = d_srcs[i] != nullptr;
  5301. }
  5302. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  5303. dst_uma = d_D != nullptr;
  5304. }
  5305. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5306. for (int i = 0; i < num_srcs; i++) {
  5307. src_sizes[i] = ggml_nbytes(dst->src[i]);
  5308. if (!srcs_uma[i]) {
  5309. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  5310. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  5311. }
  5312. }
  5313. const uint64_t dst_size = ggml_nbytes(dst);
  5314. if (!dst_uma) {
  5315. d_D = dst_buf_ctx->dev_buffer;
  5316. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  5317. }
  5318. std::array<uint32_t, 3> elements = {
  5319. (uint32_t)(pc.B * pc.H),
  5320. 1,
  5321. 1
  5322. };
  5323. if (version == 6) {
  5324. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5325. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5326. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5327. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5328. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5329. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5330. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5331. vk_subbuffer{ d_D, dst_offset, dst_size }
  5332. }, sizeof(vk_op_rwkv_wkv6_push_constants), &pc, elements);
  5333. } else if (version == 7) {
  5334. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5335. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5336. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5337. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5338. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5339. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5340. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5341. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  5342. vk_subbuffer{ d_D, dst_offset, dst_size }
  5343. }, sizeof(vk_op_rwkv_wkv7_push_constants), &pc, elements);
  5344. } else {
  5345. // shouldn't happen
  5346. GGML_ASSERT(false);
  5347. }
  5348. }
  5349. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5350. const size_t seq_length = dst->src[0]->ne[2];
  5351. const size_t n_embed = dst->ne[0];
  5352. const size_t n_heads = dst->src[0]->ne[1];
  5353. const size_t n_seqs = dst->src[5]->ne[1];
  5354. ggml_vk_op_f32_wkv(
  5355. ctx, subctx, dst,
  5356. {
  5357. (uint32_t)n_seqs,
  5358. (uint32_t)seq_length,
  5359. (uint32_t)n_embed,
  5360. (uint32_t)n_heads,
  5361. },
  5362. 6,
  5363. dryrun
  5364. );
  5365. }
  5366. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5367. const size_t seq_length = dst->src[0]->ne[2];
  5368. const size_t n_embed = dst->ne[0];
  5369. const size_t n_heads = dst->src[0]->ne[1];
  5370. const size_t n_seqs = dst->src[6]->ne[1];
  5371. ggml_vk_op_f32_wkv(
  5372. ctx, subctx, dst,
  5373. {
  5374. (uint32_t)n_seqs,
  5375. (uint32_t)seq_length,
  5376. (uint32_t)n_embed,
  5377. (uint32_t)n_heads,
  5378. },
  5379. 7,
  5380. dryrun
  5381. );
  5382. }
  5383. 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) {
  5384. const ggml_tensor * x = dst->src[0];
  5385. const ggml_tensor * g = dst->src[1];
  5386. const ggml_tensor * gm = dst->src[2];
  5387. const ggml_tensor * gv = dst->src[3];
  5388. const ggml_tensor * p = dst->src[4];
  5389. GGML_ASSERT(x->type == GGML_TYPE_F32);
  5390. GGML_ASSERT(g->type == GGML_TYPE_F32);
  5391. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  5392. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  5393. GGML_ASSERT(p->type == GGML_TYPE_F32);
  5394. GGML_ASSERT(dst->buffer != nullptr);
  5395. GGML_ASSERT(ggml_is_contiguous(x));
  5396. GGML_ASSERT(ggml_is_contiguous(g));
  5397. GGML_ASSERT(ggml_is_contiguous(gm));
  5398. GGML_ASSERT(ggml_is_contiguous(gv));
  5399. GGML_ASSERT(ggml_is_contiguous(p));
  5400. GGML_ASSERT(ggml_are_same_shape(x, g));
  5401. GGML_ASSERT(ggml_are_same_shape(x, gm));
  5402. GGML_ASSERT(ggml_are_same_shape(x, gv));
  5403. GGML_ASSERT(ggml_nelements(p) == 7);
  5404. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  5405. GGML_ASSERT(pipeline != nullptr);
  5406. if (dryrun) {
  5407. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5408. return;
  5409. }
  5410. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  5411. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  5412. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  5413. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  5414. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  5415. ggml_vk_sync_buffers(subctx);
  5416. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  5417. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  5418. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  5419. if (ctx->device->uma) {
  5420. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  5421. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  5422. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  5423. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  5424. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  5425. X_uma = d_X != nullptr;
  5426. G_uma = d_G != nullptr;
  5427. GM_uma = d_GM != nullptr;
  5428. GV_uma = d_GV != nullptr;
  5429. P_uma = d_P != nullptr;
  5430. }
  5431. if (!X_uma) {
  5432. d_X = x_buf_ctx->dev_buffer;
  5433. x_offset = vk_tensor_offset(x) + x->view_offs;
  5434. }
  5435. if (!G_uma) {
  5436. d_G = g_buf_ctx->dev_buffer;
  5437. g_offset = vk_tensor_offset(g) + g->view_offs;
  5438. }
  5439. if (!GM_uma) {
  5440. d_GM = gm_buf_ctx->dev_buffer;
  5441. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  5442. }
  5443. if (!GV_uma) {
  5444. d_GV = gv_buf_ctx->dev_buffer;
  5445. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  5446. }
  5447. if (!P_uma) {
  5448. d_P = p_buf_ctx->dev_buffer;
  5449. p_offset = vk_tensor_offset(p) + p->view_offs;
  5450. }
  5451. const uint64_t x_size = ggml_nbytes(x);
  5452. const uint64_t g_size = ggml_nbytes(g);
  5453. const uint64_t gm_size = ggml_nbytes(gm);
  5454. const uint64_t gv_size = ggml_nbytes(gv);
  5455. const uint64_t p_size = ggml_nbytes(p);
  5456. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  5457. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5458. vk_subbuffer{ d_X, x_offset, x_size },
  5459. vk_subbuffer{ d_G, g_offset, g_size },
  5460. vk_subbuffer{ d_GM, gm_offset, gm_size },
  5461. vk_subbuffer{ d_GV, gv_offset, gv_size },
  5462. vk_subbuffer{ d_P, p_offset, p_size },
  5463. }, sizeof(vk_op_push_constants), &pc, elements);
  5464. }
  5465. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5466. const size_t n = ggml_nelements(dst->src[0]);
  5467. ggml_vk_op_f32_opt_step_adamw(
  5468. ctx, subctx, dst,
  5469. { (uint32_t)n, 0, 0.0f, 0.0f },
  5470. dryrun
  5471. );
  5472. }
  5473. 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) {
  5474. int * op_params = (int *)dst->op_params;
  5475. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5476. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5477. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5478. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  5479. (uint32_t)ggml_nelements(dst),
  5480. (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,
  5481. (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,
  5482. (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,
  5483. 0,
  5484. 0.0f, 0.0f, op_params[0],
  5485. }, dryrun);
  5486. }
  5487. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5488. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5489. const float sf0 = (float)dst->ne[0] / src0->ne[0];
  5490. const float sf1 = (float)dst->ne[1] / src0->ne[1];
  5491. const float sf2 = (float)dst->ne[2] / src0->ne[2];
  5492. const float sf3 = (float)dst->ne[3] / src0->ne[3];
  5493. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  5494. (uint32_t)ggml_nelements(dst), 0, 0,
  5495. (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,
  5496. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  5497. sf0, sf1, sf2, sf3,
  5498. }, dryrun);
  5499. }
  5500. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5501. float * op_params = (float *)dst->op_params;
  5502. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5503. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5504. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  5505. (uint32_t)ggml_nelements(src0),
  5506. (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,
  5507. (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,
  5508. 0,
  5509. op_params[0], 0.0f,
  5510. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5511. }, dryrun);
  5512. }
  5513. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5514. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5515. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5516. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  5517. (uint32_t)ggml_nelements(src0),
  5518. (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,
  5519. (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,
  5520. 0,
  5521. 0.0f, 0.0f,
  5522. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5523. }, dryrun);
  5524. }
  5525. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5526. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5527. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5528. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, {
  5529. (uint32_t)ggml_nelements(src0),
  5530. (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,
  5531. (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,
  5532. 0,
  5533. 0.0f, 0.0f,
  5534. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5535. }, dryrun);
  5536. }
  5537. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5538. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5539. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5540. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, {
  5541. (uint32_t)ggml_nelements(src0),
  5542. (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,
  5543. (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,
  5544. 0,
  5545. 0.0f, 0.0f,
  5546. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5547. }, dryrun);
  5548. }
  5549. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5550. float * op_params = (float *)dst->op_params;
  5551. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5552. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5553. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  5554. (uint32_t)ggml_nelements(src0),
  5555. (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,
  5556. (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,
  5557. 0,
  5558. op_params[0], op_params[1],
  5559. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5560. }, dryrun);
  5561. }
  5562. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5563. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5564. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5565. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, {
  5566. (uint32_t)ggml_nelements(dst),
  5567. (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,
  5568. (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,
  5569. 0,
  5570. 0.0f, 0.0f,
  5571. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5572. }, dryrun);
  5573. }
  5574. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5575. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5576. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5577. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, {
  5578. (uint32_t)ggml_nelements(dst),
  5579. (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,
  5580. (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,
  5581. 0,
  5582. 0.0f, 0.0f,
  5583. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5584. }, dryrun);
  5585. }
  5586. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5587. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5588. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5589. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, {
  5590. (uint32_t)ggml_nelements(dst),
  5591. (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,
  5592. (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,
  5593. 0,
  5594. 0.0f, 0.0f,
  5595. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5596. }, dryrun);
  5597. }
  5598. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5599. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5600. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5601. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  5602. (uint32_t)ggml_nelements(src0),
  5603. (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,
  5604. (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,
  5605. 0,
  5606. 0.0f, 0.0f,
  5607. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5608. }, dryrun);
  5609. }
  5610. 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) {
  5611. 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);
  5612. }
  5613. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5614. float * op_params = (float *)dst->op_params;
  5615. 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);
  5616. }
  5617. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5618. const int * int_op_params = (const int *)dst->op_params;
  5619. const float * float_op_params = (const float *)dst->op_params;
  5620. const uint32_t num_groups = int_op_params[0];
  5621. const float eps = float_op_params[1];
  5622. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5623. 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);
  5624. }
  5625. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5626. float * op_params = (float *)dst->op_params;
  5627. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_RMS_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5628. }
  5629. 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) {
  5630. float * op_params = (float *)dst->op_params;
  5631. 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);
  5632. }
  5633. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5634. float * op_params = (float *)dst->op_params;
  5635. 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);
  5636. }
  5637. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5638. 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);
  5639. }
  5640. 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) {
  5641. int32_t * op_params = (int32_t *)dst->op_params;
  5642. 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);
  5643. }
  5644. 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) {
  5645. float * op_params = (float *)dst->op_params;
  5646. float scale = op_params[0];
  5647. float max_bias = op_params[1];
  5648. const uint32_t ncols = (uint32_t)src0->ne[0];
  5649. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  5650. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  5651. const uint32_t n_head_kv = nrows_x/nrows_y;
  5652. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5653. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5654. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5655. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  5656. ncols,
  5657. src1 != nullptr ? nrows_y : (uint32_t)0,
  5658. scale, max_bias,
  5659. m0, m1,
  5660. n_head_log2,
  5661. nrows_x,
  5662. }, dryrun);
  5663. }
  5664. 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) {
  5665. float * op_params = (float *)dst->op_params;
  5666. 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);
  5667. }
  5668. 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) {
  5669. const int n_dims = ((int32_t *) dst->op_params)[1];
  5670. const int mode = ((int32_t *) dst->op_params)[2];
  5671. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  5672. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  5673. const float freq_base = ((float *) dst->op_params)[5];
  5674. const float freq_scale = ((float *) dst->op_params)[6];
  5675. const float ext_factor = ((float *) dst->op_params)[7];
  5676. const float attn_factor = ((float *) dst->op_params)[8];
  5677. const float beta_fast = ((float *) dst->op_params)[9];
  5678. const float beta_slow = ((float *) dst->op_params)[10];
  5679. int sections[4] {};
  5680. if (mode & GGML_ROPE_TYPE_MROPE) {
  5681. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  5682. }
  5683. float corr_dims[2];
  5684. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  5685. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5686. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  5687. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  5688. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  5689. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  5690. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  5691. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  5692. sections[0], sections[1], sections[2], sections[3], backprop
  5693. }, dryrun);
  5694. }
  5695. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5696. int32_t * op_params = (int32_t *)dst->op_params;
  5697. uint32_t ncols = src0->ne[0];
  5698. uint32_t ncols_pad = 1;
  5699. while (ncols_pad < ncols) {
  5700. ncols_pad *= 2;
  5701. }
  5702. GGML_ASSERT(ncols_pad <= 1024);
  5703. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  5704. ncols,
  5705. ncols_pad,
  5706. op_params[0],
  5707. }, dryrun);
  5708. }
  5709. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5710. 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);
  5711. }
  5712. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5713. 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);
  5714. }
  5715. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5716. 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);
  5717. }
  5718. 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) {
  5719. 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);
  5720. }
  5721. 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) {
  5722. const int32_t s0 = dst->op_params[0];
  5723. const int32_t s1 = dst->op_params[1];
  5724. const int32_t p0 = dst->op_params[2];
  5725. const int32_t p1 = dst->op_params[3];
  5726. const int32_t d0 = dst->op_params[4];
  5727. const int32_t d1 = dst->op_params[5];
  5728. const bool is_2D = dst->op_params[6] == 1;
  5729. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5730. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  5731. const uint32_t IW = src1->ne[0];
  5732. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5733. const uint32_t KW = src0->ne[0];
  5734. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5735. const uint32_t OW = dst->ne[1];
  5736. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  5737. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  5738. const uint32_t pelements = OW * KW * KH;
  5739. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  5740. batch_offset, offset_delta,
  5741. IC, IW, IH, OW, OH, KW, KH,
  5742. pelements,
  5743. IC * KH * KW,
  5744. s0, s1, p0, p1, d0, d1,
  5745. }, dryrun);
  5746. }
  5747. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5748. const uint32_t dim = dst->op_params[0];
  5749. const uint32_t max_period = dst->op_params[1];
  5750. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  5751. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  5752. nb1, dim, max_period,
  5753. }, dryrun);
  5754. }
  5755. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5756. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  5757. const int32_t k1 = dst->op_params[1];
  5758. const int32_t k0 = dst->op_params[2];
  5759. const int32_t s1 = dst->op_params[3];
  5760. const int32_t s0 = dst->op_params[4];
  5761. const int32_t p1 = dst->op_params[5];
  5762. const int32_t p0 = dst->op_params[6];
  5763. const uint32_t IH = src0->ne[1];
  5764. const uint32_t IW = src0->ne[0];
  5765. const uint32_t N = dst->ne[3];
  5766. const uint32_t OC = dst->ne[2];
  5767. const uint32_t OH = dst->ne[1];
  5768. const uint32_t OW = dst->ne[0];
  5769. const uint32_t parallel_elements = N * OC * OH * OW;
  5770. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  5771. IW, IH, OW, OH, OC,
  5772. parallel_elements,
  5773. op,
  5774. k0, k1, s0, s1, p0, p1,
  5775. }, dryrun);
  5776. }
  5777. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5778. const float * op_params = (const float *)dst->op_params;
  5779. 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);
  5780. }
  5781. #ifdef GGML_VULKAN_RUN_TESTS
  5782. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  5783. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  5784. return;
  5785. }
  5786. i0 = std::max(i0, 5);
  5787. i1 = std::max(i1, 5);
  5788. i2 = std::max(i2, 0);
  5789. fprintf(stderr, " ");
  5790. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5791. fprintf(stderr, "%7d ", idx1);
  5792. }
  5793. fprintf(stderr, "\n");
  5794. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  5795. fprintf(stderr, "%7d: ", idx0);
  5796. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5797. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  5798. float val;
  5799. if (type == GGML_TYPE_F32) {
  5800. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  5801. } else if (type == GGML_TYPE_F16) {
  5802. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  5803. } else {
  5804. GGML_ABORT("fatal error");
  5805. }
  5806. fprintf(stderr, "% 7.2f ", val);
  5807. } else {
  5808. fprintf(stderr, " ");
  5809. }
  5810. }
  5811. fprintf(stderr, "\n");
  5812. }
  5813. }
  5814. template <typename X_TYPE, typename Y_TYPE>
  5815. 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) {
  5816. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  5817. const size_t x_ne = m * k * batch;
  5818. const size_t y_ne = k * n * batch;
  5819. const size_t d_ne = m * n * batch;
  5820. vk_pipeline p;
  5821. std::string shname;
  5822. if (shader_size == 0) {
  5823. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5824. p = ctx->device->pipeline_matmul_f32->a_s;
  5825. shname = "F32_ALIGNED_S";
  5826. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5827. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  5828. shname = "F32_F16_ALIGNED_S";
  5829. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5830. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  5831. shname = "F16_F32_ALIGNED_S";
  5832. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5833. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  5834. shname = "F16_ALIGNED_S";
  5835. } else {
  5836. GGML_ABORT("fatal error");
  5837. }
  5838. } else if (shader_size == 1) {
  5839. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5840. p = ctx->device->pipeline_matmul_f32->a_m;
  5841. shname = "F32_ALIGNED_M";
  5842. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5843. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  5844. shname = "F32_F16_ALIGNED_M";
  5845. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5846. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  5847. shname = "F16_F32_ALIGNED_M";
  5848. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5849. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  5850. shname = "F16_ALIGNED_M";
  5851. } else {
  5852. GGML_ABORT("fatal error");
  5853. }
  5854. } else if (shader_size == 2) {
  5855. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5856. p = ctx->device->pipeline_matmul_f32->a_l;
  5857. shname = "F32_ALIGNED_L";
  5858. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5859. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  5860. shname = "F32_F16_ALIGNED_L";
  5861. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5862. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  5863. shname = "F16_F32_ALIGNED_L";
  5864. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5865. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  5866. shname = "F16_ALIGNED_L";
  5867. } else {
  5868. GGML_ABORT("fatal error");
  5869. }
  5870. } else {
  5871. GGML_ASSERT(0);
  5872. }
  5873. const size_t kpad = ggml_vk_align_size(k, p->align);
  5874. if (k != kpad) {
  5875. if (shader_size == 0) {
  5876. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5877. p = ctx->device->pipeline_matmul_f32->s;
  5878. shname = "F32_S";
  5879. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5880. p = ctx->device->pipeline_matmul_f32_f16->s;
  5881. shname = "F32_F16_S";
  5882. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5883. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  5884. shname = "F16_F32_S";
  5885. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5886. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  5887. shname = "F16_S";
  5888. }
  5889. } else if (shader_size == 1) {
  5890. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5891. p = ctx->device->pipeline_matmul_f32->m;
  5892. shname = "F32_M";
  5893. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5894. p = ctx->device->pipeline_matmul_f32_f16->m;
  5895. shname = "F32_F16_M";
  5896. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5897. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  5898. shname = "F16_F32_M";
  5899. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5900. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  5901. shname = "F16_M";
  5902. }
  5903. } else if (shader_size == 2) {
  5904. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5905. p = ctx->device->pipeline_matmul_f32->l;
  5906. shname = "F32_L";
  5907. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5908. p = ctx->device->pipeline_matmul_f32_f16->l;
  5909. shname = "F32_F16_L";
  5910. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5911. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  5912. shname = "F16_F32_L";
  5913. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5914. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  5915. shname = "F16_L";
  5916. }
  5917. }
  5918. }
  5919. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  5920. if (split_k > 1) {
  5921. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  5922. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  5923. // Resize buffer
  5924. if (ctx->prealloc_split_k != nullptr) {
  5925. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  5926. }
  5927. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5928. }
  5929. }
  5930. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  5931. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5932. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5933. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  5934. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  5935. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  5936. float* d = (float *) malloc(sizeof(float) * d_ne);
  5937. for (size_t i = 0; i < x_ne; i++) {
  5938. if (std::is_same<float, X_TYPE>()) {
  5939. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  5940. // x[i] = 1.0f;
  5941. // x[i] = i + 1;
  5942. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  5943. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  5944. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  5945. // x[i] = ggml_fp32_to_fp16(1.0f);
  5946. // x[i] = ggml_fp32_to_fp16(i + 1);
  5947. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  5948. } else {
  5949. GGML_ABORT("fatal error");
  5950. }
  5951. }
  5952. for (size_t i = 0; i < y_ne; i++) {
  5953. if (std::is_same<float, Y_TYPE>()) {
  5954. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  5955. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  5956. // y[i] = i + 1;
  5957. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  5958. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  5959. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  5960. // y[i] = ggml_fp32_to_fp16(i + 1);
  5961. } else {
  5962. GGML_ABORT("fatal error");
  5963. }
  5964. }
  5965. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  5966. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  5967. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  5968. ggml_vk_ctx_begin(ctx->device, subctx);
  5969. for (size_t i = 0; i < num_it; i++) {
  5970. ggml_vk_matmul(
  5971. 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),
  5972. m, n, k,
  5973. k, k, m, k*m, k*n, m*n,
  5974. split_k, batch, batch, batch, 1, 1, n
  5975. );
  5976. }
  5977. ggml_vk_ctx_end(subctx);
  5978. auto begin = std::chrono::high_resolution_clock::now();
  5979. ggml_vk_submit(subctx, ctx->fence);
  5980. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  5981. ctx->device->device.resetFences({ ctx->fence });
  5982. auto end = std::chrono::high_resolution_clock::now();
  5983. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  5984. // copy dst to host
  5985. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  5986. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  5987. ggml_init_params iparams = {
  5988. /*.mem_size =*/ 1024*1024*1024,
  5989. /*.mem_buffer =*/ NULL,
  5990. /*.no_alloc =*/ true,
  5991. };
  5992. ggml_context * ggml_ctx = ggml_init(iparams);
  5993. ggml_type src0_type;
  5994. ggml_type src1_type;
  5995. if (std::is_same<float, X_TYPE>()) {
  5996. src0_type = GGML_TYPE_F32;
  5997. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  5998. src0_type = GGML_TYPE_F16;
  5999. } else {
  6000. GGML_ABORT("fatal error");
  6001. }
  6002. if (std::is_same<float, Y_TYPE>()) {
  6003. src1_type = GGML_TYPE_F32;
  6004. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6005. src1_type = GGML_TYPE_F16;
  6006. } else {
  6007. GGML_ABORT("fatal error");
  6008. }
  6009. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  6010. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  6011. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6012. src0_ggml->data = x;
  6013. src1_ggml->data = y;
  6014. tensor_ggml->data = d_chk;
  6015. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6016. ggml_build_forward_expand(cgraph, tensor_ggml);
  6017. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6018. ggml_free(ggml_ctx);
  6019. double avg_err = 0.0;
  6020. int first_err_n = -1;
  6021. int first_err_m = -1;
  6022. int first_err_b = -1;
  6023. for (size_t i = 0; i < m*n*batch; i++) {
  6024. double err = std::fabs(d[i] - d_chk[i]);
  6025. avg_err += err;
  6026. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6027. first_err_b = i / (m * n);
  6028. first_err_n = (i % (m * n)) / m;
  6029. first_err_m = (i % (m * n)) % m;
  6030. }
  6031. }
  6032. avg_err /= m * n;
  6033. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6034. 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;
  6035. if (avg_err > 0.1 || std::isnan(avg_err)) {
  6036. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6037. std::cerr << "Actual result: " << std::endl << std::endl;
  6038. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6039. std::cerr << "Expected result: " << std::endl << std::endl;
  6040. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6041. if (split_k > 1) {
  6042. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6043. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6044. std::cerr << "d_buf0: " << std::endl << std::endl;
  6045. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6046. std::cerr << "d_buf1: " << std::endl << std::endl;
  6047. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6048. std::cerr << "d_buf2: " << std::endl << std::endl;
  6049. 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);
  6050. std::cerr << "d_buf3: " << std::endl << std::endl;
  6051. 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);
  6052. free(split_k_buf);
  6053. }
  6054. }
  6055. free(d_chk);
  6056. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  6057. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  6058. ggml_vk_destroy_buffer(d_X);
  6059. ggml_vk_destroy_buffer(d_Y);
  6060. ggml_vk_destroy_buffer(d_D);
  6061. ggml_pipeline_cleanup(p);
  6062. ggml_pipeline_cleanup(ctx->device->pipeline_matmul_split_k_reduce);
  6063. free(x);
  6064. free(y);
  6065. free(d);
  6066. }
  6067. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  6068. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  6069. return;
  6070. }
  6071. i0 = std::max(i0, 5);
  6072. i1 = std::max(i1, 5);
  6073. i2 = std::max(i2, 0);
  6074. i3 = std::max(i3, 0);
  6075. fprintf(stderr, " ");
  6076. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6077. fprintf(stderr, "%7d ", idx1);
  6078. }
  6079. fprintf(stderr, "\n");
  6080. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6081. fprintf(stderr, "%7d: ", idx0);
  6082. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6083. 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]) {
  6084. float val;
  6085. if (tensor->type == GGML_TYPE_F32) {
  6086. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  6087. } else if (tensor->type == GGML_TYPE_F16) {
  6088. 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]));
  6089. } else {
  6090. GGML_ABORT("fatal error");
  6091. }
  6092. fprintf(stderr, "% 7.2f ", val);
  6093. } else {
  6094. fprintf(stderr, " ");
  6095. }
  6096. }
  6097. fprintf(stderr, "\n");
  6098. }
  6099. }
  6100. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  6101. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  6102. }
  6103. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  6104. if (quant == GGML_TYPE_F32) {
  6105. memcpy(to, from, sizeof(float) * ne);
  6106. return;
  6107. }
  6108. const auto * tt = ggml_get_type_traits(quant);
  6109. ggml_to_float_t dequant_fn = tt->to_float;
  6110. dequant_fn(from, to, ne);
  6111. }
  6112. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  6113. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  6114. const size_t x_sz = sizeof(float) * ne;
  6115. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  6116. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6117. float * x = (float *) malloc(x_sz);
  6118. void * qx = malloc(qx_sz);
  6119. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6120. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6121. float * x_ref = (float *) malloc(x_sz);
  6122. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  6123. for (size_t i = 0; i < ne; i++) {
  6124. x[i] = rand() / (float)RAND_MAX;
  6125. }
  6126. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  6127. ggml_vk_quantize_data(x, qx, ne, quant);
  6128. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  6129. ggml_pipeline_request_descriptor_sets(ctx->device, p, 1);
  6130. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6131. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6132. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6133. ggml_vk_ctx_begin(ctx->device, subctx);
  6134. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  6135. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc.size() * sizeof(int), pc.data(), { (uint32_t)ne, 1, 1});
  6136. ggml_vk_ctx_end(subctx);
  6137. auto begin = std::chrono::high_resolution_clock::now();
  6138. ggml_vk_submit(subctx, ctx->fence);
  6139. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6140. ctx->device->device.resetFences({ ctx->fence });
  6141. auto end = std::chrono::high_resolution_clock::now();
  6142. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6143. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  6144. int first_err = -1;
  6145. double avg_err = 0.0;
  6146. for (size_t i = 0; i < ne; i++) {
  6147. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  6148. avg_err += error;
  6149. if (first_err < 0 && error > 0.05) {
  6150. first_err = i;
  6151. }
  6152. }
  6153. avg_err /= ne;
  6154. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  6155. if (avg_err > 0.1) {
  6156. std::cerr << "first_error = " << first_err << std::endl;
  6157. std::cerr << "Actual result: " << std::endl << std::endl;
  6158. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6159. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  6160. }
  6161. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  6162. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6163. std::cerr << x_ref[i] << ", ";
  6164. }
  6165. std::cerr << std::endl;
  6166. }
  6167. ggml_vk_destroy_buffer(x_buf);
  6168. ggml_vk_destroy_buffer(qx_buf);
  6169. free(x);
  6170. free(qx);
  6171. free(x_ref);
  6172. free(x_chk);
  6173. }
  6174. 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) {
  6175. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  6176. const size_t x_ne = m * k * batch;
  6177. const size_t y_ne = k * n * batch;
  6178. const size_t d_ne = m * n * batch;
  6179. vk_pipeline p;
  6180. std::string shname;
  6181. if (shader_size == 0) {
  6182. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->a_s : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->a_s;
  6183. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  6184. } else if (shader_size == 1) {
  6185. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->a_m : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->a_m;
  6186. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  6187. } else if (shader_size == 2) {
  6188. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->a_l : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->a_l;
  6189. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  6190. } else {
  6191. GGML_ASSERT(0);
  6192. }
  6193. const size_t kpad = ggml_vk_align_size(k, p->align);
  6194. if (k != kpad) {
  6195. if (shader_size == 0) {
  6196. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->s : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->s;
  6197. shname = std::string(ggml_type_name(quant)) + "_S";
  6198. } else if (shader_size == 1) {
  6199. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->m : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->m;
  6200. shname = std::string(ggml_type_name(quant)) + "_M";
  6201. } else if (shader_size == 2) {
  6202. p = ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[quant].f16acc->l : ctx->device->pipeline_dequant_mul_mat_mat[quant].f32acc->l;
  6203. shname = std::string(ggml_type_name(quant)) + "_L";
  6204. } else {
  6205. GGML_ASSERT(0);
  6206. }
  6207. }
  6208. const size_t x_sz = sizeof(float) * x_ne;
  6209. const size_t y_sz = sizeof(float) * y_ne;
  6210. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6211. const size_t d_sz = sizeof(float) * d_ne;
  6212. float * x = (float *) malloc(x_sz);
  6213. float * y = (float *) malloc(y_sz);
  6214. void * qx = malloc(qx_sz);
  6215. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6216. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6217. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6218. float * d = (float *) malloc(d_sz);
  6219. float * d_chk = (float *) malloc(d_sz);
  6220. for (size_t i = 0; i < x_ne; i++) {
  6221. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6222. }
  6223. ggml_vk_quantize_data(x, qx, x_ne, quant);
  6224. for (size_t i = 0; i < y_ne; i++) {
  6225. // y[i] = rand() / (float)RAND_MAX;
  6226. y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6227. }
  6228. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  6229. if (split_k > 1) {
  6230. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6231. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6232. // Resize buffer
  6233. if (ctx->prealloc_split_k != nullptr) {
  6234. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6235. }
  6236. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6237. }
  6238. }
  6239. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6240. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6241. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  6242. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6243. ggml_vk_ctx_begin(ctx->device, subctx);
  6244. for (size_t i = 0; i < num_it; i++) {
  6245. ggml_vk_matmul(
  6246. ctx, subctx, p, ggml_vk_subbuffer(qx_buf), ggml_vk_subbuffer(y_buf), ggml_vk_subbuffer(d_buf), ggml_vk_subbuffer(ctx->prealloc_split_k),
  6247. m, n, k,
  6248. k, k, m, k*m, k*n, m*n,
  6249. split_k, batch, batch, batch, 1, 1, n
  6250. );
  6251. }
  6252. ggml_vk_ctx_end(subctx);
  6253. auto begin = std::chrono::high_resolution_clock::now();
  6254. ggml_vk_submit(subctx, ctx->fence);
  6255. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6256. ctx->device->device.resetFences({ ctx->fence });
  6257. auto end = std::chrono::high_resolution_clock::now();
  6258. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6259. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  6260. ggml_init_params iparams = {
  6261. /*.mem_size =*/ 1024*1024*1024,
  6262. /*.mem_buffer =*/ NULL,
  6263. /*.no_alloc =*/ true,
  6264. };
  6265. ggml_context * ggml_ctx = ggml_init(iparams);
  6266. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  6267. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  6268. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6269. src0_ggml->data = qx;
  6270. src1_ggml->data = y;
  6271. tensor_ggml->data = d_chk;
  6272. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6273. ggml_build_forward_expand(cgraph, tensor_ggml);
  6274. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6275. ggml_free(ggml_ctx);
  6276. double avg_err = 0.0;
  6277. int first_err_n = -1;
  6278. int first_err_m = -1;
  6279. int first_err_b = -1;
  6280. for (size_t i = 0; i < m*n*batch; i++) {
  6281. double err = std::fabs(d[i] - d_chk[i]);
  6282. avg_err += err;
  6283. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6284. first_err_b = i / (m * n);
  6285. first_err_n = (i % (m * n)) / m;
  6286. first_err_m = (i % (m * n)) % m;
  6287. }
  6288. }
  6289. avg_err /= m * n;
  6290. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6291. std::cerr << "TEST MMQ " << shname << " 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;
  6292. if (avg_err > 0.01 || std::isnan(avg_err)) {
  6293. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6294. std::cerr << "Actual result: " << std::endl << std::endl;
  6295. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6296. std::cerr << std::endl;
  6297. std::cerr << "Expected result: " << std::endl << std::endl;
  6298. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6299. if (split_k > 1) {
  6300. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6301. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6302. std::cerr << "d_buf0: " << std::endl << std::endl;
  6303. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6304. std::cerr << "d_buf1: " << std::endl << std::endl;
  6305. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6306. std::cerr << "d_buf2: " << std::endl << std::endl;
  6307. 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);
  6308. std::cerr << "d_buf3: " << std::endl << std::endl;
  6309. 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);
  6310. free(split_k_buf);
  6311. }
  6312. }
  6313. ggml_vk_destroy_buffer(qx_buf);
  6314. ggml_vk_destroy_buffer(y_buf);
  6315. ggml_vk_destroy_buffer(d_buf);
  6316. free(x);
  6317. free(qx);
  6318. free(y);
  6319. free(d);
  6320. free(d_chk);
  6321. }
  6322. #endif
  6323. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  6324. #if defined(GGML_VULKAN_RUN_TESTS)
  6325. const std::vector<size_t> vals {
  6326. 512, 512, 128,
  6327. 128, 512, 512,
  6328. 4096, 512, 4096,
  6329. 11008, 512, 4096,
  6330. 4096, 512, 11008,
  6331. 32000, 512, 4096,
  6332. 8, 8, 8,
  6333. 100, 46, 576,
  6334. 623, 111, 128,
  6335. 100, 46, 558,
  6336. 512, 1, 256,
  6337. 128, 110, 622,
  6338. 511, 511, 127,
  6339. 511, 511, 7,
  6340. 511, 511, 17,
  6341. 49, 49, 128,
  6342. 128, 49, 49,
  6343. 4096, 49, 4096,
  6344. };
  6345. const size_t num_it = 100;
  6346. for (size_t i = 0; i < vals.size(); i += 3) {
  6347. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  6348. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  6349. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  6350. std::cerr << '\n';
  6351. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  6352. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  6353. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  6354. std::cerr << '\n';
  6355. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  6356. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  6357. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  6358. std::cerr << '\n' << std::endl;
  6359. if (vals[i + 2] % 32 == 0) {
  6360. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  6361. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  6362. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  6363. std::cerr << '\n';
  6364. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  6365. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  6366. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  6367. std::cerr << '\n';
  6368. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  6369. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  6370. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  6371. std::cerr << '\n' << std::endl;
  6372. }
  6373. if (vals[i + 2] % 256 == 0) {
  6374. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  6375. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  6376. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  6377. std::cerr << '\n';
  6378. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  6379. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  6380. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  6381. std::cerr << '\n';
  6382. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  6383. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  6384. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  6385. std::cerr << '\n' << std::endl;
  6386. }
  6387. }
  6388. GGML_ABORT("fatal error");
  6389. #endif
  6390. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  6391. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  6392. // Resize buffer
  6393. if (ctx->prealloc_x != nullptr) {
  6394. ggml_vk_destroy_buffer(ctx->prealloc_x);
  6395. }
  6396. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  6397. }
  6398. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  6399. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  6400. // Resize buffer
  6401. if (ctx->prealloc_y != nullptr) {
  6402. ggml_vk_destroy_buffer(ctx->prealloc_y);
  6403. }
  6404. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  6405. }
  6406. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  6407. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  6408. // Resize buffer
  6409. if (ctx->prealloc_split_k != nullptr) {
  6410. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6411. }
  6412. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  6413. }
  6414. }
  6415. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* tensor, int tensor_idx, bool use_fence);
  6416. // Returns true if node has enqueued work into the queue, false otherwise
  6417. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  6418. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool submit){
  6419. if (ggml_is_empty(node) || !node->buffer) {
  6420. return false;
  6421. }
  6422. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  6423. ctx->semaphore_idx = 0;
  6424. const ggml_tensor * src0 = node->src[0];
  6425. const ggml_tensor * src1 = node->src[1];
  6426. const ggml_tensor * src2 = node->src[2];
  6427. const ggml_tensor * src3 = node->src[3];
  6428. switch (node->op) {
  6429. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  6430. case GGML_OP_RESHAPE:
  6431. case GGML_OP_VIEW:
  6432. case GGML_OP_PERMUTE:
  6433. case GGML_OP_TRANSPOSE:
  6434. case GGML_OP_NONE:
  6435. return false;
  6436. case GGML_OP_UNARY:
  6437. switch (ggml_get_unary_op(node)) {
  6438. case GGML_UNARY_OP_SILU:
  6439. case GGML_UNARY_OP_GELU:
  6440. case GGML_UNARY_OP_GELU_QUICK:
  6441. case GGML_UNARY_OP_RELU:
  6442. case GGML_UNARY_OP_TANH:
  6443. case GGML_UNARY_OP_SIGMOID:
  6444. break;
  6445. default:
  6446. return false;
  6447. }
  6448. break;
  6449. case GGML_OP_REPEAT:
  6450. case GGML_OP_REPEAT_BACK:
  6451. case GGML_OP_GET_ROWS:
  6452. case GGML_OP_ADD:
  6453. case GGML_OP_ACC:
  6454. case GGML_OP_SUB:
  6455. case GGML_OP_MUL:
  6456. case GGML_OP_DIV:
  6457. case GGML_OP_CONCAT:
  6458. case GGML_OP_UPSCALE:
  6459. case GGML_OP_SCALE:
  6460. case GGML_OP_SQR:
  6461. case GGML_OP_SIN:
  6462. case GGML_OP_COS:
  6463. case GGML_OP_CLAMP:
  6464. case GGML_OP_PAD:
  6465. case GGML_OP_CPY:
  6466. case GGML_OP_CONT:
  6467. case GGML_OP_DUP:
  6468. case GGML_OP_SILU_BACK:
  6469. case GGML_OP_NORM:
  6470. case GGML_OP_GROUP_NORM:
  6471. case GGML_OP_RMS_NORM:
  6472. case GGML_OP_RMS_NORM_BACK:
  6473. case GGML_OP_L2_NORM:
  6474. case GGML_OP_DIAG_MASK_INF:
  6475. case GGML_OP_SOFT_MAX:
  6476. case GGML_OP_SOFT_MAX_BACK:
  6477. case GGML_OP_ROPE:
  6478. case GGML_OP_ROPE_BACK:
  6479. case GGML_OP_MUL_MAT:
  6480. case GGML_OP_MUL_MAT_ID:
  6481. case GGML_OP_ARGSORT:
  6482. case GGML_OP_SUM:
  6483. case GGML_OP_SUM_ROWS:
  6484. case GGML_OP_ARGMAX:
  6485. case GGML_OP_COUNT_EQUAL:
  6486. case GGML_OP_IM2COL:
  6487. case GGML_OP_TIMESTEP_EMBEDDING:
  6488. case GGML_OP_POOL_2D:
  6489. case GGML_OP_RWKV_WKV6:
  6490. case GGML_OP_RWKV_WKV7:
  6491. case GGML_OP_LEAKY_RELU:
  6492. case GGML_OP_FLASH_ATTN_EXT:
  6493. case GGML_OP_OPT_STEP_ADAMW:
  6494. break;
  6495. default:
  6496. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  6497. GGML_ABORT("fatal error");
  6498. return false;
  6499. }
  6500. vk_context compute_ctx;
  6501. if (!dryrun) {
  6502. if (ctx->compute_ctx.expired()) {
  6503. compute_ctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6504. ctx->compute_ctx = compute_ctx;
  6505. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  6506. } else {
  6507. compute_ctx = ctx->compute_ctx.lock();
  6508. }
  6509. } else {
  6510. switch (node->op) {
  6511. case GGML_OP_REPEAT:
  6512. case GGML_OP_REPEAT_BACK:
  6513. case GGML_OP_ACC:
  6514. case GGML_OP_GET_ROWS:
  6515. case GGML_OP_ADD:
  6516. case GGML_OP_SUB:
  6517. case GGML_OP_MUL:
  6518. case GGML_OP_DIV:
  6519. case GGML_OP_CONCAT:
  6520. case GGML_OP_UPSCALE:
  6521. case GGML_OP_SCALE:
  6522. case GGML_OP_SQR:
  6523. case GGML_OP_SIN:
  6524. case GGML_OP_COS:
  6525. case GGML_OP_CLAMP:
  6526. case GGML_OP_PAD:
  6527. case GGML_OP_CPY:
  6528. case GGML_OP_CONT:
  6529. case GGML_OP_DUP:
  6530. case GGML_OP_SILU_BACK:
  6531. case GGML_OP_NORM:
  6532. case GGML_OP_GROUP_NORM:
  6533. case GGML_OP_RMS_NORM:
  6534. case GGML_OP_RMS_NORM_BACK:
  6535. case GGML_OP_L2_NORM:
  6536. case GGML_OP_UNARY:
  6537. case GGML_OP_DIAG_MASK_INF:
  6538. case GGML_OP_SOFT_MAX:
  6539. case GGML_OP_SOFT_MAX_BACK:
  6540. case GGML_OP_ROPE:
  6541. case GGML_OP_ROPE_BACK:
  6542. case GGML_OP_ARGSORT:
  6543. case GGML_OP_SUM:
  6544. case GGML_OP_SUM_ROWS:
  6545. case GGML_OP_ARGMAX:
  6546. case GGML_OP_COUNT_EQUAL:
  6547. case GGML_OP_IM2COL:
  6548. case GGML_OP_TIMESTEP_EMBEDDING:
  6549. case GGML_OP_POOL_2D:
  6550. case GGML_OP_LEAKY_RELU:
  6551. {
  6552. // These operations all go through ggml_vk_op_f32, so short-circuit and
  6553. // do the only thing needed for the dryrun.
  6554. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  6555. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  6556. return false;
  6557. }
  6558. default:
  6559. break;
  6560. }
  6561. }
  6562. switch (node->op) {
  6563. case GGML_OP_REPEAT:
  6564. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  6565. break;
  6566. case GGML_OP_REPEAT_BACK:
  6567. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  6568. break;
  6569. case GGML_OP_ACC:
  6570. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  6571. break;
  6572. case GGML_OP_GET_ROWS:
  6573. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  6574. break;
  6575. case GGML_OP_ADD:
  6576. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  6577. break;
  6578. case GGML_OP_SUB:
  6579. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  6580. break;
  6581. case GGML_OP_MUL:
  6582. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  6583. break;
  6584. case GGML_OP_DIV:
  6585. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  6586. break;
  6587. case GGML_OP_CONCAT:
  6588. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  6589. break;
  6590. case GGML_OP_UPSCALE:
  6591. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  6592. break;
  6593. case GGML_OP_SCALE:
  6594. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  6595. break;
  6596. case GGML_OP_SQR:
  6597. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  6598. break;
  6599. case GGML_OP_SIN:
  6600. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  6601. break;
  6602. case GGML_OP_COS:
  6603. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  6604. break;
  6605. case GGML_OP_CLAMP:
  6606. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  6607. break;
  6608. case GGML_OP_PAD:
  6609. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  6610. break;
  6611. case GGML_OP_CPY:
  6612. case GGML_OP_CONT:
  6613. case GGML_OP_DUP:
  6614. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  6615. break;
  6616. case GGML_OP_SILU_BACK:
  6617. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6618. break;
  6619. case GGML_OP_NORM:
  6620. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  6621. break;
  6622. case GGML_OP_GROUP_NORM:
  6623. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  6624. break;
  6625. case GGML_OP_RMS_NORM:
  6626. ggml_vk_rms_norm(ctx, compute_ctx, src0, node, dryrun);
  6627. break;
  6628. case GGML_OP_RMS_NORM_BACK:
  6629. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6630. break;
  6631. case GGML_OP_L2_NORM:
  6632. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  6633. break;
  6634. case GGML_OP_UNARY:
  6635. switch (ggml_get_unary_op(node)) {
  6636. case GGML_UNARY_OP_SILU:
  6637. case GGML_UNARY_OP_GELU:
  6638. case GGML_UNARY_OP_GELU_QUICK:
  6639. case GGML_UNARY_OP_RELU:
  6640. case GGML_UNARY_OP_TANH:
  6641. case GGML_UNARY_OP_SIGMOID:
  6642. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  6643. break;
  6644. default:
  6645. return false;
  6646. }
  6647. break;
  6648. case GGML_OP_DIAG_MASK_INF:
  6649. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  6650. break;
  6651. case GGML_OP_SOFT_MAX:
  6652. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  6653. break;
  6654. case GGML_OP_SOFT_MAX_BACK:
  6655. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6656. break;
  6657. case GGML_OP_ROPE:
  6658. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  6659. break;
  6660. case GGML_OP_ROPE_BACK:
  6661. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  6662. break;
  6663. case GGML_OP_ARGSORT:
  6664. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  6665. break;
  6666. case GGML_OP_SUM:
  6667. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  6668. break;
  6669. case GGML_OP_SUM_ROWS:
  6670. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  6671. break;
  6672. case GGML_OP_ARGMAX:
  6673. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  6674. break;
  6675. case GGML_OP_COUNT_EQUAL:
  6676. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  6677. break;
  6678. case GGML_OP_IM2COL:
  6679. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  6680. break;
  6681. case GGML_OP_TIMESTEP_EMBEDDING:
  6682. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  6683. break;
  6684. case GGML_OP_POOL_2D:
  6685. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  6686. break;
  6687. case GGML_OP_LEAKY_RELU:
  6688. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  6689. break;
  6690. case GGML_OP_MUL_MAT:
  6691. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  6692. break;
  6693. case GGML_OP_MUL_MAT_ID:
  6694. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  6695. break;
  6696. case GGML_OP_FLASH_ATTN_EXT:
  6697. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  6698. break;
  6699. case GGML_OP_RWKV_WKV6:
  6700. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  6701. break;
  6702. case GGML_OP_RWKV_WKV7:
  6703. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  6704. break;
  6705. case GGML_OP_OPT_STEP_ADAMW:
  6706. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  6707. break;
  6708. default:
  6709. return false;
  6710. }
  6711. if (dryrun) {
  6712. return false;
  6713. }
  6714. ctx->tensor_ctxs[node_idx] = compute_ctx;
  6715. #if defined(GGML_VULKAN_CHECK_RESULTS) || defined(GGML_VULKAN_PERF)
  6716. // Force context reset on each node so that each tensor ends up in its own context
  6717. // and can be run and compared to its CPU equivalent separately
  6718. last_node = true;
  6719. #endif
  6720. if (submit || last_node) {
  6721. ggml_vk_ctx_end(compute_ctx);
  6722. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  6723. if (last_node) {
  6724. compute_ctx->exit_tensor_idx = node_idx_begin;
  6725. }
  6726. else {
  6727. compute_ctx->exit_tensor_idx = -1;
  6728. }
  6729. ctx->compute_ctx.reset();
  6730. bool ok = ggml_vk_compute_forward(ctx, node_begin, node_idx_begin, false);
  6731. if (!ok) {
  6732. if (node->op == GGML_OP_UNARY) {
  6733. 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;
  6734. }
  6735. else {
  6736. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  6737. }
  6738. }
  6739. }
  6740. return true;
  6741. }
  6742. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_tensor * tensor, int tensor_idx, bool use_fence = true){
  6743. ggml_backend_buffer * buf = nullptr;
  6744. switch (tensor->op) {
  6745. case GGML_OP_ADD:
  6746. case GGML_OP_ACC:
  6747. case GGML_OP_GET_ROWS:
  6748. case GGML_OP_SUB:
  6749. case GGML_OP_MUL:
  6750. case GGML_OP_DIV:
  6751. case GGML_OP_CONCAT:
  6752. case GGML_OP_UPSCALE:
  6753. case GGML_OP_SCALE:
  6754. case GGML_OP_SQR:
  6755. case GGML_OP_SIN:
  6756. case GGML_OP_COS:
  6757. case GGML_OP_CLAMP:
  6758. case GGML_OP_PAD:
  6759. case GGML_OP_CPY:
  6760. case GGML_OP_CONT:
  6761. case GGML_OP_DUP:
  6762. case GGML_OP_SILU_BACK:
  6763. case GGML_OP_NORM:
  6764. case GGML_OP_GROUP_NORM:
  6765. case GGML_OP_RMS_NORM:
  6766. case GGML_OP_RMS_NORM_BACK:
  6767. case GGML_OP_L2_NORM:
  6768. case GGML_OP_DIAG_MASK_INF:
  6769. case GGML_OP_SOFT_MAX:
  6770. case GGML_OP_SOFT_MAX_BACK:
  6771. case GGML_OP_ROPE:
  6772. case GGML_OP_ROPE_BACK:
  6773. case GGML_OP_RESHAPE:
  6774. case GGML_OP_VIEW:
  6775. case GGML_OP_PERMUTE:
  6776. case GGML_OP_TRANSPOSE:
  6777. case GGML_OP_NONE:
  6778. case GGML_OP_ARGSORT:
  6779. case GGML_OP_SUM:
  6780. case GGML_OP_SUM_ROWS:
  6781. case GGML_OP_ARGMAX:
  6782. case GGML_OP_COUNT_EQUAL:
  6783. case GGML_OP_IM2COL:
  6784. case GGML_OP_TIMESTEP_EMBEDDING:
  6785. case GGML_OP_POOL_2D:
  6786. case GGML_OP_RWKV_WKV6:
  6787. case GGML_OP_RWKV_WKV7:
  6788. case GGML_OP_LEAKY_RELU:
  6789. case GGML_OP_REPEAT:
  6790. case GGML_OP_REPEAT_BACK:
  6791. case GGML_OP_OPT_STEP_ADAMW:
  6792. buf = tensor->buffer;
  6793. break;
  6794. case GGML_OP_UNARY:
  6795. switch (ggml_get_unary_op(tensor)) {
  6796. case GGML_UNARY_OP_SILU:
  6797. case GGML_UNARY_OP_GELU:
  6798. case GGML_UNARY_OP_GELU_QUICK:
  6799. case GGML_UNARY_OP_RELU:
  6800. case GGML_UNARY_OP_TANH:
  6801. case GGML_UNARY_OP_SIGMOID:
  6802. buf = tensor->buffer;
  6803. break;
  6804. default:
  6805. return false;
  6806. }
  6807. break;
  6808. case GGML_OP_MUL_MAT:
  6809. case GGML_OP_MUL_MAT_ID:
  6810. case GGML_OP_FLASH_ATTN_EXT:
  6811. buf = tensor->buffer;
  6812. break;
  6813. default:
  6814. return false;
  6815. }
  6816. if (buf == nullptr) {
  6817. return false;
  6818. }
  6819. 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 << ")");
  6820. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  6821. // always wait for the GPU work to be done for the last submit
  6822. if (tensor_idx == subctx->exit_tensor_idx) {
  6823. use_fence = true;
  6824. }
  6825. // Only run if ctx hasn't been submitted yet
  6826. if (!subctx->seqs.empty()) {
  6827. #ifdef GGML_VULKAN_CHECK_RESULTS
  6828. ggml_vk_check_results_0(tensor);
  6829. use_fence = true;
  6830. #endif
  6831. // Do staging buffer copies
  6832. for (auto& cpy : subctx->in_memcpys) {
  6833. memcpy(cpy.dst, cpy.src, cpy.n);
  6834. }
  6835. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  6836. if (use_fence) {
  6837. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_compute_forward waitForFences");
  6838. ctx->device->device.resetFences({ ctx->fence });
  6839. }
  6840. #ifdef GGML_VULKAN_CHECK_RESULTS
  6841. ggml_vk_check_results_1(tensor);
  6842. #endif
  6843. }
  6844. if (tensor_idx == subctx->exit_tensor_idx) {
  6845. // Do staging buffer copies
  6846. for (auto& cpy : subctx->out_memcpys) {
  6847. memcpy(cpy.dst, cpy.src, cpy.n);
  6848. }
  6849. subctx->in_memcpys.clear();
  6850. subctx->out_memcpys.clear();
  6851. }
  6852. return true;
  6853. }
  6854. // Clean up after graph processing is done
  6855. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  6856. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  6857. for (auto& buffer : ctx->gc.temp_buffers) {
  6858. ggml_vk_pool_free(ctx, buffer);
  6859. }
  6860. ctx->gc.temp_buffers.clear();
  6861. for (auto& dsr : ctx->device->pipeline_descriptor_set_requirements) {
  6862. vk_pipeline_ref plr = ctx->device->pipelines[dsr.first];
  6863. if (plr.expired()) {
  6864. continue;
  6865. }
  6866. vk_pipeline pl = plr.lock();
  6867. ggml_pipeline_cleanup(pl);
  6868. }
  6869. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  6870. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  6871. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  6872. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  6873. }
  6874. ctx->gc.semaphores.clear();
  6875. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  6876. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  6877. }
  6878. ctx->gc.tl_semaphores.clear();
  6879. ctx->semaphore_idx = 0;
  6880. ctx->event_idx = 0;
  6881. for (auto& event : ctx->gc.events) {
  6882. ctx->device->device.resetEvent(event);
  6883. }
  6884. ctx->tensor_ctxs.clear();
  6885. ctx->gc.contexts.clear();
  6886. ctx->device->pipeline_descriptor_set_requirements.clear();
  6887. }
  6888. // Clean up on backend free
  6889. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  6890. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  6891. ggml_vk_graph_cleanup(ctx);
  6892. ggml_vk_destroy_buffer(ctx->prealloc_x);
  6893. ggml_vk_destroy_buffer(ctx->prealloc_y);
  6894. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6895. for (auto& buffer : ctx->buffer_pool) {
  6896. ggml_vk_destroy_buffer(buffer);
  6897. }
  6898. ctx->prealloc_size_x = 0;
  6899. ctx->prealloc_size_y = 0;
  6900. ctx->prealloc_size_split_k = 0;
  6901. for (auto& event : ctx->gc.events) {
  6902. ctx->device->device.destroyEvent(event);
  6903. }
  6904. ctx->gc.events.clear();
  6905. ctx->device->device.destroyFence(ctx->fence);
  6906. }
  6907. static int ggml_vk_get_device_count() {
  6908. ggml_vk_instance_init();
  6909. return vk_instance.device_indices.size();
  6910. }
  6911. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  6912. ggml_vk_instance_init();
  6913. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  6914. vk::PhysicalDeviceProperties props;
  6915. devices[device].getProperties(&props);
  6916. snprintf(description, description_size, "%s", props.deviceName.data());
  6917. }
  6918. // backend interface
  6919. #define UNUSED GGML_UNUSED
  6920. // device backend
  6921. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  6922. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  6923. }
  6924. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  6925. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  6926. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6927. ggml_vk_destroy_buffer(ctx->dev_buffer);
  6928. delete ctx;
  6929. }
  6930. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  6931. return vk_ptr_base;
  6932. UNUSED(buffer);
  6933. }
  6934. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  6935. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  6936. if (tensor->view_src != nullptr) {
  6937. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  6938. }
  6939. return GGML_STATUS_SUCCESS;
  6940. }
  6941. 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) {
  6942. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  6943. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6944. vk_buffer buf = buf_ctx->dev_buffer;
  6945. uint32_t val32 = (uint32_t)value * 0x01010101;
  6946. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  6947. }
  6948. 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) {
  6949. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  6950. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6951. vk_buffer buf = buf_ctx->dev_buffer;
  6952. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  6953. }
  6954. 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) {
  6955. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  6956. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6957. vk_buffer buf = buf_ctx->dev_buffer;
  6958. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  6959. }
  6960. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  6961. if (ggml_backend_buffer_is_vk(src->buffer)) {
  6962. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  6963. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  6964. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  6965. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  6966. 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));
  6967. return true;
  6968. }
  6969. return false;
  6970. UNUSED(buffer);
  6971. }
  6972. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  6973. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  6974. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  6975. }
  6976. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  6977. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  6978. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  6979. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  6980. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  6981. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  6982. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  6983. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  6984. /* .clear = */ ggml_backend_vk_buffer_clear,
  6985. /* .reset = */ NULL,
  6986. };
  6987. // vk buffer type
  6988. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  6989. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  6990. return ctx->name.c_str();
  6991. }
  6992. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  6993. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  6994. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  6995. vk_buffer dev_buffer = nullptr;
  6996. try {
  6997. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  6998. } catch (const vk::SystemError& e) {
  6999. return nullptr;
  7000. }
  7001. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  7002. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  7003. }
  7004. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7005. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7006. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  7007. }
  7008. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  7009. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7010. return ctx->device->suballocation_block_size;
  7011. }
  7012. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  7013. return ggml_nbytes(tensor);
  7014. UNUSED(buft);
  7015. }
  7016. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  7017. ggml_vk_instance_init();
  7018. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  7019. vk_device dev = ggml_vk_get_device(dev_num);
  7020. return &dev->buffer_type;
  7021. }
  7022. // host buffer type
  7023. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  7024. return GGML_VK_NAME "_Host";
  7025. UNUSED(buft);
  7026. }
  7027. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  7028. return GGML_VK_NAME "_Host";
  7029. UNUSED(buffer);
  7030. }
  7031. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7032. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  7033. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  7034. }
  7035. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7036. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  7037. size += 32; // Behave like the CPU buffer type
  7038. void * ptr = nullptr;
  7039. try {
  7040. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  7041. } catch (vk::SystemError& e) {
  7042. std::cerr << "ggml_vulkan: Failed to allocate pinned memory." << std::endl;
  7043. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  7044. // fallback to cpu buffer
  7045. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7046. }
  7047. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7048. buffer->buft = buft;
  7049. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  7050. return buffer;
  7051. UNUSED(buft);
  7052. }
  7053. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7054. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  7055. UNUSED(buft);
  7056. }
  7057. // Should be changed to return device-specific host buffer type
  7058. // but that probably requires changes in llama.cpp
  7059. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  7060. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  7061. /* .iface = */ {
  7062. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  7063. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  7064. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  7065. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  7066. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7067. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7068. },
  7069. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  7070. /* .context = */ nullptr,
  7071. };
  7072. // Make sure device 0 is initialized
  7073. ggml_vk_instance_init();
  7074. ggml_vk_get_device(0);
  7075. return &ggml_backend_vk_buffer_type_host;
  7076. }
  7077. // backend
  7078. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  7079. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7080. return ctx->name.c_str();
  7081. }
  7082. static void ggml_backend_vk_free(ggml_backend_t backend) {
  7083. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7084. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  7085. ggml_vk_cleanup(ctx);
  7086. delete ctx;
  7087. delete backend;
  7088. }
  7089. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  7090. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7091. return &ctx->device->buffer_type;
  7092. }
  7093. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7094. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  7095. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7096. 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");
  7097. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7098. vk_context transfer_ctx;
  7099. if (ctx->transfer_ctx.expired()) {
  7100. // Initialize new transfer context
  7101. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7102. ctx->transfer_ctx = transfer_ctx;
  7103. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7104. } else {
  7105. transfer_ctx = ctx->transfer_ctx.lock();
  7106. }
  7107. vk_buffer buf = buf_ctx->dev_buffer;
  7108. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7109. }
  7110. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7111. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  7112. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7113. 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");
  7114. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7115. vk_context transfer_ctx;
  7116. if (ctx->transfer_ctx.expired()) {
  7117. // Initialize new transfer context
  7118. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7119. ctx->transfer_ctx = transfer_ctx;
  7120. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7121. } else {
  7122. transfer_ctx = ctx->transfer_ctx.lock();
  7123. }
  7124. vk_buffer buf = buf_ctx->dev_buffer;
  7125. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7126. }
  7127. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  7128. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  7129. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7130. 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)) {
  7131. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  7132. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7133. vk_context transfer_ctx;
  7134. if (ctx->transfer_ctx.expired()) {
  7135. // Initialize new transfer context
  7136. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7137. ctx->transfer_ctx = transfer_ctx;
  7138. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7139. } else {
  7140. transfer_ctx = ctx->transfer_ctx.lock();
  7141. }
  7142. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  7143. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  7144. 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));
  7145. return true;
  7146. }
  7147. return false;
  7148. }
  7149. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  7150. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  7151. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7152. if(ctx->transfer_ctx.expired()) {
  7153. return;
  7154. }
  7155. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  7156. ggml_vk_ctx_end(transfer_ctx);
  7157. for (auto& cpy : transfer_ctx->in_memcpys) {
  7158. memcpy(cpy.dst, cpy.src, cpy.n);
  7159. }
  7160. ggml_vk_submit(transfer_ctx, ctx->fence);
  7161. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_backend_vk_synchronize waitForFences");
  7162. ctx->device->device.resetFences({ ctx->fence });
  7163. for (auto& cpy : transfer_ctx->out_memcpys) {
  7164. memcpy(cpy.dst, cpy.src, cpy.n);
  7165. }
  7166. ctx->transfer_ctx.reset();
  7167. }
  7168. static bool ggml_vk_is_empty(ggml_tensor * node) {
  7169. 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;
  7170. }
  7171. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7172. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  7173. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7174. for (int i = 0; i < cgraph->n_nodes; i++) {
  7175. ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false);
  7176. }
  7177. if (ctx->device->need_compiles) {
  7178. ggml_vk_load_shaders(ctx->device);
  7179. }
  7180. ggml_vk_preallocate_buffers(ctx);
  7181. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  7182. int last_node = cgraph->n_nodes - 1;
  7183. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  7184. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  7185. last_node -= 1;
  7186. }
  7187. // Reserve tensor context space for all nodes
  7188. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  7189. bool first_node_in_batch = true; // true if next node will be first node in a batch
  7190. int submit_node_idx = 0; // index to first node in a batch
  7191. // Submit work every nodes_per_submit nodes to overlap CPU cmdbuffer generation with GPU execution.
  7192. // Start with a smaller count to get work submitted right away, and increase it after each submit.
  7193. int nodes_per_submit = 20;
  7194. int submitted_nodes = 0;
  7195. int submit_count = 0;
  7196. for (int i = 0; i < cgraph->n_nodes; i++) {
  7197. if (first_node_in_batch) {
  7198. submit_node_idx = i;
  7199. }
  7200. bool submit = (submitted_nodes >= nodes_per_submit) || (i == last_node);
  7201. bool enqueued = ggml_vk_build_graph(ctx, cgraph->nodes[i], i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i == last_node, submit);
  7202. if (enqueued) {
  7203. ++submitted_nodes;
  7204. #ifndef GGML_VULKAN_CHECK_RESULTS
  7205. if (first_node_in_batch) {
  7206. first_node_in_batch = false;
  7207. }
  7208. #endif
  7209. }
  7210. if (submit) {
  7211. first_node_in_batch = true;
  7212. submitted_nodes = 0;
  7213. switch (submit_count) {
  7214. case 0:
  7215. nodes_per_submit = 50;
  7216. break;
  7217. default:
  7218. nodes_per_submit = 100;
  7219. break;
  7220. }
  7221. submit_count++;
  7222. }
  7223. }
  7224. #ifdef GGML_VULKAN_PERF
  7225. ctx->device->perf_logger->print_timings();
  7226. #endif
  7227. ggml_vk_graph_cleanup(ctx);
  7228. return GGML_STATUS_SUCCESS;
  7229. UNUSED(backend);
  7230. }
  7231. // TODO: enable async and synchronize
  7232. static ggml_backend_i ggml_backend_vk_interface = {
  7233. /* .get_name = */ ggml_backend_vk_name,
  7234. /* .free = */ ggml_backend_vk_free,
  7235. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  7236. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  7237. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  7238. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  7239. /* .graph_plan_create = */ NULL,
  7240. /* .graph_plan_free = */ NULL,
  7241. /* .graph_plan_update = */ NULL,
  7242. /* .graph_plan_compute = */ NULL,
  7243. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  7244. /* .event_record = */ NULL,
  7245. /* .event_wait = */ NULL,
  7246. };
  7247. static ggml_guid_t ggml_backend_vk_guid() {
  7248. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  7249. return &guid;
  7250. }
  7251. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  7252. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  7253. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  7254. ggml_vk_init(ctx, dev_num);
  7255. ggml_backend_t vk_backend = new ggml_backend {
  7256. /* .guid = */ ggml_backend_vk_guid(),
  7257. /* .interface = */ ggml_backend_vk_interface,
  7258. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  7259. /* .context = */ ctx,
  7260. };
  7261. return vk_backend;
  7262. }
  7263. bool ggml_backend_is_vk(ggml_backend_t backend) {
  7264. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  7265. }
  7266. int ggml_backend_vk_get_device_count() {
  7267. return ggml_vk_get_device_count();
  7268. }
  7269. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  7270. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7271. int dev_idx = vk_instance.device_indices[device];
  7272. ggml_vk_get_device_description(dev_idx, description, description_size);
  7273. }
  7274. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  7275. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7276. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  7277. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  7278. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  7279. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  7280. *total = heap.size;
  7281. *free = heap.size;
  7282. break;
  7283. }
  7284. }
  7285. }
  7286. //////////////////////////
  7287. struct ggml_backend_vk_device_context {
  7288. size_t device;
  7289. std::string name;
  7290. std::string description;
  7291. };
  7292. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  7293. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7294. return ctx->name.c_str();
  7295. }
  7296. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  7297. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7298. return ctx->description.c_str();
  7299. }
  7300. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  7301. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  7302. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  7303. }
  7304. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  7305. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7306. return ggml_backend_vk_buffer_type(ctx->device);
  7307. }
  7308. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  7309. UNUSED(dev);
  7310. return ggml_backend_vk_host_buffer_type();
  7311. }
  7312. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  7313. UNUSED(dev);
  7314. return GGML_BACKEND_DEVICE_TYPE_GPU;
  7315. }
  7316. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  7317. props->name = ggml_backend_vk_device_get_name(dev);
  7318. props->description = ggml_backend_vk_device_get_description(dev);
  7319. props->type = ggml_backend_vk_device_get_type(dev);
  7320. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  7321. props->caps = {
  7322. /* .async = */ false,
  7323. /* .host_buffer = */ true,
  7324. /* .buffer_from_host_ptr = */ false,
  7325. /* .events = */ false,
  7326. };
  7327. }
  7328. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  7329. UNUSED(params);
  7330. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7331. return ggml_backend_vk_init(ctx->device);
  7332. }
  7333. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7334. switch (op->op) {
  7335. case GGML_OP_UNARY:
  7336. switch (ggml_get_unary_op(op)) {
  7337. case GGML_UNARY_OP_GELU:
  7338. case GGML_UNARY_OP_GELU_QUICK:
  7339. case GGML_UNARY_OP_SILU:
  7340. case GGML_UNARY_OP_RELU:
  7341. case GGML_UNARY_OP_TANH:
  7342. case GGML_UNARY_OP_SIGMOID:
  7343. return ggml_is_contiguous(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
  7344. default:
  7345. return false;
  7346. }
  7347. break;
  7348. case GGML_OP_MUL_MAT:
  7349. case GGML_OP_MUL_MAT_ID:
  7350. {
  7351. ggml_type src0_type = op->src[0]->type;
  7352. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7353. const vk_device& device = ggml_vk_get_device(ctx->device);
  7354. if (op->op == GGML_OP_MUL_MAT_ID && !device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  7355. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  7356. return false;
  7357. }
  7358. switch (src0_type) {
  7359. case GGML_TYPE_F32:
  7360. case GGML_TYPE_F16:
  7361. case GGML_TYPE_Q4_0:
  7362. case GGML_TYPE_Q4_1:
  7363. case GGML_TYPE_Q5_0:
  7364. case GGML_TYPE_Q5_1:
  7365. case GGML_TYPE_Q8_0:
  7366. case GGML_TYPE_Q2_K:
  7367. case GGML_TYPE_Q3_K:
  7368. case GGML_TYPE_Q4_K:
  7369. case GGML_TYPE_Q5_K:
  7370. case GGML_TYPE_Q6_K:
  7371. case GGML_TYPE_IQ1_S:
  7372. case GGML_TYPE_IQ1_M:
  7373. case GGML_TYPE_IQ2_XXS:
  7374. case GGML_TYPE_IQ2_XS:
  7375. case GGML_TYPE_IQ2_S:
  7376. case GGML_TYPE_IQ3_XXS:
  7377. case GGML_TYPE_IQ3_S:
  7378. case GGML_TYPE_IQ4_XS:
  7379. case GGML_TYPE_IQ4_NL:
  7380. break;
  7381. default:
  7382. return false;
  7383. }
  7384. struct ggml_tensor * a;
  7385. struct ggml_tensor * b;
  7386. if (op->op == GGML_OP_MUL_MAT) {
  7387. a = op->src[0];
  7388. b = op->src[1];
  7389. } else {
  7390. a = op->src[2];
  7391. b = op->src[1];
  7392. }
  7393. if (a->ne[3] != b->ne[3]) {
  7394. return false;
  7395. }
  7396. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) ||
  7397. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  7398. return false;
  7399. }
  7400. return true;
  7401. } break;
  7402. case GGML_OP_FLASH_ATTN_EXT:
  7403. {
  7404. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7405. if (!ggml_vk_get_device(ctx->device)->coopmat2) {
  7406. return false;
  7407. }
  7408. switch (op->src[0]->ne[0]) {
  7409. case 64:
  7410. case 80:
  7411. case 96:
  7412. case 112:
  7413. case 128:
  7414. case 256:
  7415. break;
  7416. default:
  7417. return false;
  7418. }
  7419. if (op->src[0]->type != GGML_TYPE_F32) {
  7420. return false;
  7421. }
  7422. if (op->type != GGML_TYPE_F32) {
  7423. return false;
  7424. }
  7425. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  7426. return false;
  7427. }
  7428. // It's straightforward to support different K/V dequant, but would
  7429. // significantly increase the number of pipelines
  7430. if (op->src[1]->type != op->src[2]->type) {
  7431. return false;
  7432. }
  7433. switch (op->src[1]->type) {
  7434. case GGML_TYPE_F16:
  7435. case GGML_TYPE_Q4_0:
  7436. case GGML_TYPE_Q4_1:
  7437. case GGML_TYPE_Q5_0:
  7438. case GGML_TYPE_Q5_1:
  7439. case GGML_TYPE_Q8_0:
  7440. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  7441. //case GGML_TYPE_Q2_K:
  7442. //case GGML_TYPE_Q3_K:
  7443. //case GGML_TYPE_Q4_K:
  7444. //case GGML_TYPE_Q5_K:
  7445. //case GGML_TYPE_Q6_K:
  7446. //case GGML_TYPE_IQ1_S:
  7447. //case GGML_TYPE_IQ1_M:
  7448. //case GGML_TYPE_IQ2_XXS:
  7449. //case GGML_TYPE_IQ2_XS:
  7450. //case GGML_TYPE_IQ2_S:
  7451. //case GGML_TYPE_IQ3_XXS:
  7452. //case GGML_TYPE_IQ3_S:
  7453. //case GGML_TYPE_IQ4_XS:
  7454. case GGML_TYPE_IQ4_NL:
  7455. break;
  7456. default:
  7457. return false;
  7458. }
  7459. return true;
  7460. }
  7461. case GGML_OP_GET_ROWS:
  7462. {
  7463. switch (op->src[0]->type) {
  7464. case GGML_TYPE_F32:
  7465. case GGML_TYPE_F16:
  7466. case GGML_TYPE_Q4_0:
  7467. case GGML_TYPE_Q4_1:
  7468. case GGML_TYPE_Q5_0:
  7469. case GGML_TYPE_Q5_1:
  7470. case GGML_TYPE_Q8_0:
  7471. case GGML_TYPE_IQ1_S:
  7472. case GGML_TYPE_IQ1_M:
  7473. case GGML_TYPE_IQ2_XXS:
  7474. case GGML_TYPE_IQ2_XS:
  7475. case GGML_TYPE_IQ2_S:
  7476. case GGML_TYPE_IQ3_XXS:
  7477. case GGML_TYPE_IQ3_S:
  7478. case GGML_TYPE_IQ4_XS:
  7479. case GGML_TYPE_IQ4_NL:
  7480. return true;
  7481. default:
  7482. return false;
  7483. }
  7484. } break;
  7485. case GGML_OP_CONT:
  7486. case GGML_OP_CPY:
  7487. case GGML_OP_DUP:
  7488. {
  7489. ggml_type src0_type = op->src[0]->type;
  7490. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  7491. if (src0_type == GGML_TYPE_F32) {
  7492. switch (src1_type) {
  7493. case GGML_TYPE_F32:
  7494. case GGML_TYPE_F16:
  7495. case GGML_TYPE_Q4_0:
  7496. case GGML_TYPE_Q4_1:
  7497. case GGML_TYPE_Q5_0:
  7498. case GGML_TYPE_Q5_1:
  7499. case GGML_TYPE_Q8_0:
  7500. case GGML_TYPE_IQ4_NL:
  7501. return true;
  7502. default:
  7503. break;
  7504. }
  7505. }
  7506. if (src1_type == GGML_TYPE_F32) {
  7507. switch (src0_type) {
  7508. case GGML_TYPE_Q4_0:
  7509. case GGML_TYPE_Q4_1:
  7510. case GGML_TYPE_Q5_0:
  7511. case GGML_TYPE_Q5_1:
  7512. case GGML_TYPE_Q8_0:
  7513. case GGML_TYPE_IQ4_NL:
  7514. return true;
  7515. default:
  7516. break;
  7517. }
  7518. }
  7519. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  7520. return true;
  7521. }
  7522. return false;
  7523. } break;
  7524. case GGML_OP_REPEAT:
  7525. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  7526. case GGML_OP_REPEAT_BACK:
  7527. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  7528. case GGML_OP_ROPE:
  7529. case GGML_OP_ROPE_BACK:
  7530. case GGML_OP_NONE:
  7531. case GGML_OP_RESHAPE:
  7532. case GGML_OP_VIEW:
  7533. case GGML_OP_PERMUTE:
  7534. case GGML_OP_TRANSPOSE:
  7535. return true;
  7536. case GGML_OP_NORM:
  7537. case GGML_OP_GROUP_NORM:
  7538. case GGML_OP_RMS_NORM:
  7539. case GGML_OP_L2_NORM:
  7540. return ggml_is_contiguous(op->src[0]);
  7541. case GGML_OP_ADD:
  7542. case GGML_OP_SUB:
  7543. case GGML_OP_MUL:
  7544. case GGML_OP_DIV:
  7545. case GGML_OP_SILU_BACK:
  7546. case GGML_OP_RMS_NORM_BACK:
  7547. case GGML_OP_SQR:
  7548. case GGML_OP_SIN:
  7549. case GGML_OP_COS:
  7550. case GGML_OP_CLAMP:
  7551. return op->src[0]->type == GGML_TYPE_F32;
  7552. case GGML_OP_ACC:
  7553. case GGML_OP_CONCAT:
  7554. case GGML_OP_UPSCALE:
  7555. case GGML_OP_SCALE:
  7556. case GGML_OP_PAD:
  7557. case GGML_OP_DIAG_MASK_INF:
  7558. case GGML_OP_SOFT_MAX:
  7559. case GGML_OP_SOFT_MAX_BACK:
  7560. case GGML_OP_ARGSORT:
  7561. case GGML_OP_SUM:
  7562. case GGML_OP_SUM_ROWS:
  7563. case GGML_OP_ARGMAX:
  7564. case GGML_OP_COUNT_EQUAL:
  7565. case GGML_OP_IM2COL:
  7566. case GGML_OP_TIMESTEP_EMBEDDING:
  7567. case GGML_OP_POOL_2D:
  7568. case GGML_OP_RWKV_WKV6:
  7569. case GGML_OP_RWKV_WKV7:
  7570. case GGML_OP_LEAKY_RELU:
  7571. case GGML_OP_OPT_STEP_ADAMW:
  7572. return true;
  7573. default:
  7574. return false;
  7575. }
  7576. UNUSED(dev);
  7577. }
  7578. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  7579. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  7580. return false;
  7581. }
  7582. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7583. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  7584. return buft_ctx->device->idx == ctx->device;
  7585. }
  7586. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7587. const int min_batch_size = 32;
  7588. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  7589. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  7590. UNUSED(dev);
  7591. }
  7592. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  7593. /* .get_name = */ ggml_backend_vk_device_get_name,
  7594. /* .get_description = */ ggml_backend_vk_device_get_description,
  7595. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  7596. /* .get_type = */ ggml_backend_vk_device_get_type,
  7597. /* .get_props = */ ggml_backend_vk_device_get_props,
  7598. /* .init_backend = */ ggml_backend_vk_device_init,
  7599. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  7600. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  7601. /* .buffer_from_host_ptr = */ NULL,
  7602. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  7603. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  7604. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  7605. /* .event_new = */ NULL,
  7606. /* .event_free = */ NULL,
  7607. /* .event_synchronize = */ NULL,
  7608. };
  7609. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  7610. UNUSED(reg);
  7611. return GGML_VK_NAME;
  7612. }
  7613. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  7614. UNUSED(reg);
  7615. return ggml_backend_vk_get_device_count();
  7616. }
  7617. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  7618. static std::vector<ggml_backend_dev_t> devices;
  7619. static bool initialized = false;
  7620. {
  7621. static std::mutex mutex;
  7622. std::lock_guard<std::mutex> lock(mutex);
  7623. if (!initialized) {
  7624. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  7625. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  7626. char desc[256];
  7627. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  7628. ctx->device = i;
  7629. ctx->name = GGML_VK_NAME + std::to_string(i);
  7630. ctx->description = desc;
  7631. devices.push_back(new ggml_backend_device {
  7632. /* .iface = */ ggml_backend_vk_device_i,
  7633. /* .reg = */ reg,
  7634. /* .context = */ ctx,
  7635. });
  7636. }
  7637. initialized = true;
  7638. }
  7639. }
  7640. GGML_ASSERT(device < devices.size());
  7641. return devices[device];
  7642. }
  7643. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  7644. /* .get_name = */ ggml_backend_vk_reg_get_name,
  7645. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  7646. /* .get_device = */ ggml_backend_vk_reg_get_device,
  7647. /* .get_proc_address = */ NULL,
  7648. };
  7649. ggml_backend_reg_t ggml_backend_vk_reg() {
  7650. static ggml_backend_reg reg = {
  7651. /* .api_version = */ GGML_BACKEND_API_VERSION,
  7652. /* .iface = */ ggml_backend_vk_reg_i,
  7653. /* .context = */ nullptr,
  7654. };
  7655. try {
  7656. ggml_vk_instance_init();
  7657. return &reg;
  7658. } catch (const vk::SystemError& e) {
  7659. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  7660. return nullptr;
  7661. }
  7662. }
  7663. // Extension availability
  7664. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  7665. #ifdef GGML_VULKAN_VALIDATE
  7666. bool portability_enumeration_ext = false;
  7667. // Check for portability enumeration extension for MoltenVK support
  7668. for (const auto& properties : instance_extensions) {
  7669. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  7670. return true;
  7671. }
  7672. }
  7673. if (!portability_enumeration_ext) {
  7674. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  7675. }
  7676. #endif
  7677. return false;
  7678. UNUSED(instance_extensions);
  7679. }
  7680. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  7681. #ifdef __APPLE__
  7682. bool portability_enumeration_ext = false;
  7683. // Check for portability enumeration extension for MoltenVK support
  7684. for (const auto& properties : instance_extensions) {
  7685. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  7686. return true;
  7687. }
  7688. }
  7689. if (!portability_enumeration_ext) {
  7690. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  7691. }
  7692. #endif
  7693. return false;
  7694. UNUSED(instance_extensions);
  7695. }
  7696. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  7697. switch (props.vendorID) {
  7698. case VK_VENDOR_ID_INTEL:
  7699. // Intel drivers don't support coopmat properly yet
  7700. return false;
  7701. case VK_VENDOR_ID_AMD:
  7702. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  7703. // Workaround for AMD proprietary driver reporting support on all GPUs
  7704. return arch == vk_device_architecture::AMD_RDNA3;
  7705. }
  7706. return true;
  7707. default:
  7708. return true;
  7709. }
  7710. }
  7711. // checks
  7712. #ifdef GGML_VULKAN_CHECK_RESULTS
  7713. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  7714. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  7715. return;
  7716. }
  7717. for (int j = 0; j < level; j++) {
  7718. std::cerr << " ";
  7719. }
  7720. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  7721. done.push_back(tensor);
  7722. for (int i = 0; i < GGML_MAX_SRC; i++) {
  7723. if (tensor->src[i] != nullptr) {
  7724. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  7725. }
  7726. }
  7727. }
  7728. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  7729. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  7730. return;
  7731. }
  7732. i0 = std::max(i0, 5);
  7733. i1 = std::max(i1, 5);
  7734. i2 = std::max(i2, 0);
  7735. i3 = std::max(i3, 0);
  7736. fprintf(stderr, " ");
  7737. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7738. fprintf(stderr, "%7d ", idx1);
  7739. }
  7740. fprintf(stderr, "\n");
  7741. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  7742. fprintf(stderr, "%7d: ", idx0);
  7743. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  7744. 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]) {
  7745. float val;
  7746. if (tensor->type == GGML_TYPE_F32) {
  7747. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7748. } else if (tensor->type == GGML_TYPE_F16) {
  7749. 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]));
  7750. } else if (tensor->type == GGML_TYPE_I32) {
  7751. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  7752. } else {
  7753. GGML_ABORT("fatal error");
  7754. }
  7755. fprintf(stderr, "% 7.2f ", val);
  7756. } else {
  7757. fprintf(stderr, " ");
  7758. }
  7759. }
  7760. fprintf(stderr, "\n");
  7761. }
  7762. }
  7763. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  7764. void * tensor_data = tensor->data;
  7765. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  7766. if (is_gpu) {
  7767. const size_t tensor_size = ggml_nbytes(tensor);
  7768. tensor_data = malloc(tensor_size);
  7769. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7770. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  7771. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  7772. }
  7773. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  7774. 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;
  7775. if (tensor->src[0] != nullptr) {
  7776. 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;
  7777. }
  7778. if (tensor->src[1] != nullptr) {
  7779. 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;
  7780. }
  7781. std::cerr << std::endl << "Result:" << std::endl;
  7782. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  7783. std::cerr << std::endl;
  7784. std::vector<const ggml_tensor *> done;
  7785. ggml_vk_print_graph_origin(tensor, done);
  7786. if (is_gpu) {
  7787. free(tensor_data);
  7788. }
  7789. }
  7790. void * comp_result;
  7791. size_t comp_size;
  7792. size_t comp_nb[GGML_MAX_DIMS];
  7793. size_t check_counter = 0;
  7794. static void ggml_vk_check_results_0(ggml_tensor * tensor) {
  7795. if (tensor->op == GGML_OP_TRANSPOSE) {
  7796. return;
  7797. }
  7798. check_counter++;
  7799. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  7800. return;
  7801. }
  7802. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  7803. ggml_tensor * src0 = tensor->src[0];
  7804. ggml_tensor * src1 = tensor->src[1];
  7805. struct ggml_init_params iparams = {
  7806. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  7807. /*.mem_buffer =*/ NULL,
  7808. /*.no_alloc =*/ false,
  7809. };
  7810. struct ggml_context * ggml_ctx = ggml_init(iparams);
  7811. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  7812. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  7813. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  7814. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  7815. struct ggml_tensor * tensor_clone = nullptr;
  7816. for (int i = 0; i < 6; i++) {
  7817. ggml_tensor * srci = tensor->src[i];
  7818. if (srci == nullptr) {
  7819. continue;
  7820. }
  7821. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  7822. size_t srci_size = ggml_nbytes(srci);
  7823. src_clone[i] = srci_clone;
  7824. src_size[i] = ggml_nbytes(srci);
  7825. src_buffer[i] = malloc(srci_size);
  7826. srci_clone->data = src_buffer[i];
  7827. if (ggml_backend_buffer_is_host(srci->buffer)) {
  7828. memcpy(srci_clone->data, srci->data, srci_size);
  7829. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  7830. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  7831. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  7832. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  7833. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  7834. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  7835. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  7836. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  7837. const int idx = i3*srci->ne[2] + i2;
  7838. 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]);
  7839. }
  7840. }
  7841. srci_clone->nb[0] = srci->nb[0];
  7842. srci_clone->nb[1] = srci->nb[1];
  7843. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  7844. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  7845. }
  7846. } else {
  7847. if (offset + srci_size >= buffer_gpu->size) {
  7848. srci_size = buffer_gpu->size - offset;
  7849. }
  7850. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  7851. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  7852. }
  7853. } else {
  7854. GGML_ABORT("fatal error");
  7855. }
  7856. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  7857. ggml_vk_print_tensor(srci, srci_name[i]);
  7858. }
  7859. }
  7860. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  7861. const float *params = (const float *)tensor->op_params;
  7862. 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]);
  7863. } else if (tensor->op == GGML_OP_MUL_MAT) {
  7864. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  7865. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  7866. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  7867. } else if (tensor->op == GGML_OP_SUB) {
  7868. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  7869. } else if (tensor->op == GGML_OP_MUL) {
  7870. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  7871. } else if (tensor->op == GGML_OP_DIV) {
  7872. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  7873. } else if (tensor->op == GGML_OP_CONCAT) {
  7874. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  7875. } else if (tensor->op == GGML_OP_UPSCALE) {
  7876. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  7877. } else if (tensor->op == GGML_OP_SCALE) {
  7878. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], ((float *)tensor->op_params)[0]);
  7879. } else if (tensor->op == GGML_OP_SQR) {
  7880. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  7881. } else if (tensor->op == GGML_OP_SIN) {
  7882. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  7883. } else if (tensor->op == GGML_OP_COS) {
  7884. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  7885. } else if (tensor->op == GGML_OP_CLAMP) {
  7886. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  7887. } else if (tensor->op == GGML_OP_PAD) {
  7888. 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]);
  7889. } else if (tensor->op == GGML_OP_REPEAT) {
  7890. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  7891. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  7892. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  7893. } else if (tensor->op == GGML_OP_ADD) {
  7894. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  7895. } else if (tensor->op == GGML_OP_ACC) {
  7896. 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]);
  7897. } else if (tensor->op == GGML_OP_NORM) {
  7898. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  7899. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  7900. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], *(int *)tensor->op_params, ((float *)tensor->op_params)[1]);
  7901. } else if (tensor->op == GGML_OP_RMS_NORM) {
  7902. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  7903. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  7904. const float eps = ((float *) tensor->op_params)[0];
  7905. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  7906. } else if (tensor->op == GGML_OP_SILU_BACK) {
  7907. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  7908. } else if (tensor->op == GGML_OP_L2_NORM) {
  7909. const float eps = ((float *) tensor->op_params)[0];
  7910. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  7911. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  7912. if (src1 != nullptr) {
  7913. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  7914. } else {
  7915. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  7916. }
  7917. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  7918. 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]);
  7919. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  7920. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], *(int *)tensor->op_params);
  7921. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  7922. const int n_dims = ((int32_t *) tensor->op_params)[1];
  7923. const int mode = ((int32_t *) tensor->op_params)[2];
  7924. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  7925. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  7926. const float freq_base = ((float *) tensor->op_params)[5];
  7927. const float freq_scale = ((float *) tensor->op_params)[6];
  7928. const float ext_factor = ((float *) tensor->op_params)[7];
  7929. const float attn_factor = ((float *) tensor->op_params)[8];
  7930. const float beta_fast = ((float *) tensor->op_params)[9];
  7931. const float beta_slow = ((float *) tensor->op_params)[10];
  7932. if (mode & GGML_ROPE_TYPE_MROPE) {
  7933. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  7934. if (tensor->op == GGML_OP_ROPE) {
  7935. 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);
  7936. } else {
  7937. 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);
  7938. }
  7939. } else {
  7940. if (tensor->op == GGML_OP_ROPE) {
  7941. 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);
  7942. } else {
  7943. 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);
  7944. }
  7945. }
  7946. } else if (tensor->op == GGML_OP_UNARY) {
  7947. switch (ggml_get_unary_op(tensor)) {
  7948. case GGML_UNARY_OP_SILU:
  7949. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  7950. break;
  7951. case GGML_UNARY_OP_GELU:
  7952. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  7953. break;
  7954. case GGML_UNARY_OP_GELU_QUICK:
  7955. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  7956. break;
  7957. case GGML_UNARY_OP_RELU:
  7958. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  7959. break;
  7960. case GGML_UNARY_OP_TANH:
  7961. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  7962. break;
  7963. case GGML_UNARY_OP_SIGMOID:
  7964. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  7965. break;
  7966. default:
  7967. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  7968. GGML_ABORT("fatal error");
  7969. }
  7970. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  7971. if (src1 == nullptr) {
  7972. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  7973. tensor_clone->type = tensor->type;
  7974. } else {
  7975. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  7976. }
  7977. } else if (tensor->op == GGML_OP_CONT) {
  7978. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  7979. } else if (tensor->op == GGML_OP_RESHAPE) {
  7980. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  7981. } else if (tensor->op == GGML_OP_VIEW) {
  7982. 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]);
  7983. } else if (tensor->op == GGML_OP_PERMUTE) {
  7984. int32_t * params = (int32_t *)tensor->op_params;
  7985. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  7986. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  7987. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  7988. } else if (tensor->op == GGML_OP_GET_ROWS) {
  7989. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  7990. } else if (tensor->op == GGML_OP_ARGSORT) {
  7991. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  7992. } else if (tensor->op == GGML_OP_SUM) {
  7993. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  7994. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  7995. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  7996. } else if (tensor->op == GGML_OP_ARGMAX) {
  7997. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  7998. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  7999. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  8000. } else if (tensor->op == GGML_OP_IM2COL) {
  8001. const int32_t s0 = tensor->op_params[0];
  8002. const int32_t s1 = tensor->op_params[1];
  8003. const int32_t p0 = tensor->op_params[2];
  8004. const int32_t p1 = tensor->op_params[3];
  8005. const int32_t d0 = tensor->op_params[4];
  8006. const int32_t d1 = tensor->op_params[5];
  8007. const bool is_2D = tensor->op_params[6] == 1;
  8008. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  8009. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  8010. const int32_t dim = tensor->op_params[0];
  8011. const int32_t max_period = tensor->op_params[1];
  8012. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  8013. } else if (tensor->op == GGML_OP_POOL_2D) {
  8014. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  8015. const int32_t k0 = tensor->op_params[1];
  8016. const int32_t k1 = tensor->op_params[2];
  8017. const int32_t s0 = tensor->op_params[3];
  8018. const int32_t s1 = tensor->op_params[4];
  8019. const int32_t p0 = tensor->op_params[5];
  8020. const int32_t p1 = tensor->op_params[6];
  8021. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  8022. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  8023. const float * op_params = (const float *)tensor->op_params;
  8024. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  8025. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  8026. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  8027. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  8028. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  8029. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  8030. src_clone[4], src_clone[5], src_clone[6]);
  8031. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  8032. src_clone[0]->flags = src0->flags;
  8033. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  8034. src_clone[2], src_clone[3], src_clone[4]);
  8035. }
  8036. else {
  8037. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  8038. GGML_ABORT("fatal error");
  8039. }
  8040. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8041. ggml_build_forward_expand(cgraph, tensor_clone);
  8042. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  8043. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8044. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  8045. }
  8046. comp_size = ggml_nbytes(tensor_clone);
  8047. comp_result = malloc(comp_size);
  8048. memcpy(comp_result, tensor_clone->data, comp_size);
  8049. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8050. for (int i = 0; i < 6; i++) {
  8051. if (src_buffer[i] != nullptr) {
  8052. free(src_buffer[i]);
  8053. }
  8054. }
  8055. ggml_free(ggml_ctx);
  8056. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  8057. }
  8058. static void ggml_vk_check_results_1(ggml_tensor * tensor) {
  8059. if (tensor->op == GGML_OP_TRANSPOSE) {
  8060. return;
  8061. }
  8062. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  8063. return;
  8064. }
  8065. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  8066. ggml_tensor * src0 = tensor->src[0];
  8067. ggml_tensor * src1 = tensor->src[1];
  8068. ggml_tensor * src2 = tensor->src[2];
  8069. ggml_tensor * src3 = tensor->src[3];
  8070. void * tensor_data = tensor->data;
  8071. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8072. size_t tensor_size = ggml_nbytes(tensor);
  8073. tensor_data = malloc(tensor_size);
  8074. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8075. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  8076. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  8077. if (offset + tensor_size >= buffer_gpu->size) {
  8078. tensor_size = buffer_gpu->size - offset;
  8079. }
  8080. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  8081. }
  8082. float first_error_result = -1.0f;
  8083. float first_error_correct = -1.0f;
  8084. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  8085. double avg_err = 0.0;
  8086. size_t counter = 0;
  8087. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  8088. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  8089. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  8090. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  8091. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  8092. float correct = 0.0f;
  8093. float result = 0.0f;
  8094. if (buffer_size_fit) {
  8095. if (tensor->type == GGML_TYPE_F32) {
  8096. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8097. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8098. } else if (tensor->type == GGML_TYPE_F16) {
  8099. 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]));
  8100. 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]));
  8101. } else if (tensor->type == GGML_TYPE_I32) {
  8102. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8103. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8104. } else if (tensor->type == GGML_TYPE_I64) {
  8105. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8106. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8107. } else {
  8108. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  8109. }
  8110. } else {
  8111. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  8112. GGML_ABORT("fatal error");
  8113. }
  8114. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  8115. 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;
  8116. 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;
  8117. if (src0 != nullptr) {
  8118. 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;
  8119. }
  8120. if (src1 != nullptr) {
  8121. 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;
  8122. }
  8123. if (src2 != nullptr) {
  8124. 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;
  8125. }
  8126. if (src3 != nullptr) {
  8127. 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;
  8128. }
  8129. 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;
  8130. std::cerr << std::endl << "Result:" << std::endl;
  8131. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  8132. std::cerr << std::endl << "Correct:" << std::endl;
  8133. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  8134. std::cerr << std::endl;
  8135. std::vector<const ggml_tensor *> done;
  8136. ggml_vk_print_graph_origin(tensor, done);
  8137. GGML_ABORT("fatal error");
  8138. }
  8139. if (first_error[0] == -1 && std::fabs(correct - result) > 0.1f) {
  8140. first_error[0] = i0;
  8141. first_error[1] = i1;
  8142. first_error[2] = i2;
  8143. first_error[3] = i3;
  8144. first_error_result = result;
  8145. first_error_correct = correct;
  8146. }
  8147. // Special case, value is infinite, avoid NaN result in avg_err
  8148. // NaN also appears in results, if both are nan error is 0
  8149. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  8150. avg_err += std::fabs(correct - result);
  8151. }
  8152. counter++;
  8153. }
  8154. }
  8155. }
  8156. }
  8157. avg_err /= counter;
  8158. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8159. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8160. 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;
  8161. if (src0 != nullptr) {
  8162. 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;
  8163. }
  8164. if (src1 != nullptr) {
  8165. 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;
  8166. }
  8167. if (src2 != nullptr) {
  8168. 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;
  8169. }
  8170. if (src3 != nullptr) {
  8171. 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;
  8172. }
  8173. 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;
  8174. std::cerr << std::endl << "Result:" << std::endl;
  8175. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  8176. std::cerr << std::endl << "Correct:" << std::endl;
  8177. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  8178. std::cerr << std::endl;
  8179. std::vector<const ggml_tensor *> done;
  8180. ggml_vk_print_graph_origin(tensor, done);
  8181. }
  8182. if (avg_err > 0.05 || std::isnan(avg_err)) {
  8183. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8184. 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;
  8185. if (src0 != nullptr) {
  8186. 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;
  8187. }
  8188. if (src1 != nullptr) {
  8189. 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;
  8190. }
  8191. if (src2 != nullptr) {
  8192. 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;
  8193. }
  8194. if (src3 != nullptr) {
  8195. 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;
  8196. }
  8197. 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;
  8198. std::cerr << std::endl << "Result:" << std::endl;
  8199. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  8200. std::cerr << std::endl << "Correct:" << std::endl;
  8201. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  8202. std::cerr << std::endl;
  8203. std::vector<const ggml_tensor *> done;
  8204. ggml_vk_print_graph_origin(tensor, done);
  8205. GGML_ABORT("fatal error");
  8206. } else {
  8207. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  8208. }
  8209. free(comp_result);
  8210. comp_result = nullptr;
  8211. comp_size = 0;
  8212. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8213. free(tensor_data);
  8214. }
  8215. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  8216. }
  8217. #endif
  8218. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)