ggml-vulkan.cpp 381 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111
  1. #include "ggml-vulkan.h"
  2. #ifdef GGML_VULKAN_RUN_TESTS
  3. #include <chrono>
  4. #endif
  5. #include <vulkan/vulkan.hpp>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <iostream>
  9. #include <limits>
  10. #include <tuple>
  11. #include <vector>
  12. #include <sstream>
  13. #include <utility>
  14. #include <memory>
  15. #include "ggml.h"
  16. #include "ggml-backend-impl.h"
  17. #include "ggml-vulkan-shaders.hpp"
  18. #define VK_API_VERSION VK_API_VERSION_1_2
  19. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  20. #define VK_VENDOR_ID_AMD 0x1002
  21. #define VK_VENDOR_ID_APPLE 0x106b
  22. #define VK_VENDOR_ID_INTEL 0x8086
  23. #define VK_VENDOR_ID_NVIDIA 0x10de
  24. #define VK_DEVICE_DESCRIPTOR_POOL_MODE_UNKNOWN 0
  25. #define VK_DEVICE_DESCRIPTOR_POOL_MODE_MULTI 1
  26. #define VK_DEVICE_DESCRIPTOR_POOL_MODE_SINGLE 2
  27. #define VK_NUM_TYPES 16
  28. #define GGML_VK_MAX_NODES 8192
  29. #define MAX_VK_BUFFERS 256
  30. #ifndef K_QUANTS_PER_ITERATION
  31. #define K_QUANTS_PER_ITERATION 1
  32. #else
  33. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  34. #endif
  35. #define VK_CHECK(err, msg) \
  36. do { \
  37. vk::Result err_ = (err); \
  38. if (err_ != vk::Result::eSuccess) { \
  39. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  40. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  41. exit(1); \
  42. } \
  43. } while (0)
  44. struct ggml_backend_vk_context;
  45. struct vk_queue {
  46. uint32_t queue_family_index;
  47. vk::Queue queue;
  48. vk::CommandPool pool;
  49. uint32_t cmd_buffer_idx;
  50. std::vector<vk::CommandBuffer> cmd_buffers;
  51. vk::PipelineStageFlags stage_flags;
  52. };
  53. struct vk_pipeline_struct {
  54. std::string name;
  55. vk::ShaderModule shader_module;
  56. vk::DescriptorSetLayout dsl;
  57. std::vector<vk::DescriptorPool> descriptor_pools;
  58. std::vector<vk::DescriptorSet> descriptor_sets;
  59. uint32_t descriptor_set_idx;
  60. vk::PipelineLayout layout;
  61. vk::Pipeline pipeline;
  62. uint32_t push_constant_size;
  63. uint32_t parameter_count;
  64. std::array<uint32_t, 3> wg_denoms;
  65. uint32_t align;
  66. };
  67. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  68. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  69. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  70. struct vk_matmul_pipeline_struct {
  71. vk_pipeline l, m, s;
  72. vk_pipeline a_l, a_m, a_s;
  73. };
  74. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  75. struct vk_device {
  76. vk::PhysicalDevice physical_device;
  77. vk::PhysicalDeviceProperties properties;
  78. std::string name;
  79. uint64_t max_memory_allocation_size;
  80. bool fp16;
  81. vk::Device device;
  82. uint32_t vendor_id;
  83. vk_queue compute_queue;
  84. vk_queue transfer_queue;
  85. bool single_queue;
  86. uint32_t descriptor_set_mode;
  87. uint32_t subgroup_size;
  88. bool uma;
  89. bool initialized;
  90. size_t idx;
  91. vk_matmul_pipeline pipeline_matmul_f32;
  92. vk_matmul_pipeline pipeline_matmul_f32_f16;
  93. vk_matmul_pipeline pipeline_matmul_f16;
  94. vk_matmul_pipeline pipeline_matmul_f16_f32;
  95. vk_pipeline pipeline_matmul_split_k_reduce;
  96. vk_matmul_pipeline pipeline_dequant_mul_mat_mat[VK_NUM_TYPES];
  97. vk_matmul_pipeline pipeline_matmul_id_f32;
  98. vk_matmul_pipeline pipeline_matmul_id_f16;
  99. vk_matmul_pipeline pipeline_matmul_id_f16_f32;
  100. vk_matmul_pipeline pipeline_dequant_mul_mat_mat_id[VK_NUM_TYPES];
  101. vk_pipeline pipeline_dequant[VK_NUM_TYPES];
  102. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[VK_NUM_TYPES];
  103. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[VK_NUM_TYPES];
  104. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[VK_NUM_TYPES];
  105. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32;
  106. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  107. vk_pipeline pipeline_get_rows[VK_NUM_TYPES];
  108. vk_pipeline pipeline_get_rows_f32[VK_NUM_TYPES];
  109. vk_pipeline pipeline_mul_f32;
  110. vk_pipeline pipeline_add_f32;
  111. vk_pipeline pipeline_scale_f32;
  112. vk_pipeline pipeline_sqr_f32;
  113. vk_pipeline pipeline_clamp_f32;
  114. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16;
  115. vk_pipeline pipeline_norm_f32;
  116. vk_pipeline pipeline_rms_norm_f32;
  117. vk_pipeline pipeline_gelu_f32;
  118. vk_pipeline pipeline_silu_f32;
  119. vk_pipeline pipeline_relu_f32;
  120. vk_pipeline pipeline_diag_mask_inf_f32;
  121. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  122. vk_pipeline pipeline_rope_f32, pipeline_rope_f16;
  123. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  124. vk_pipeline pipeline_argsort_f32;
  125. std::vector<vk_pipeline_ref> pipelines;
  126. ~vk_device() {
  127. #ifdef GGML_VULKAN_DEBUG
  128. std::cerr << "destroy device " << name << std::endl;
  129. #endif
  130. device.destroyCommandPool(compute_queue.pool);
  131. if (!single_queue) {
  132. device.destroyCommandPool(transfer_queue.pool);
  133. }
  134. for (auto& pipeline : pipelines) {
  135. if (pipeline.expired()) {
  136. continue;
  137. }
  138. vk_pipeline pl = pipeline.lock();
  139. ggml_vk_destroy_pipeline(device, pl);
  140. }
  141. pipelines.clear();
  142. device.destroy();
  143. }
  144. };
  145. struct vk_buffer_struct {
  146. vk::Buffer buffer;
  147. vk::DeviceMemory device_memory;
  148. vk::MemoryPropertyFlags memory_property_flags;
  149. void * ptr;
  150. size_t size = 0;
  151. ggml_backend_vk_context * ctx;
  152. std::shared_ptr<vk_device> device;
  153. ~vk_buffer_struct() {
  154. if (size == 0) {
  155. return;
  156. }
  157. #ifdef GGML_VULKAN_DEBUG
  158. std::cerr << "~vk_buffer_struct(" << buffer << ", " << size << ")" << std::endl;
  159. #endif
  160. device->device.freeMemory(device_memory);
  161. device->device.destroyBuffer(buffer);
  162. }
  163. };
  164. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  165. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  166. struct vk_subbuffer {
  167. vk_buffer buffer;
  168. uint64_t offset;
  169. uint64_t size;
  170. };
  171. struct vk_semaphore {
  172. vk::Semaphore s;
  173. uint64_t value;
  174. };
  175. struct vk_submission {
  176. vk::CommandBuffer buffer;
  177. std::vector<vk_semaphore> wait_semaphores;
  178. std::vector<vk_semaphore> signal_semaphores;
  179. };
  180. typedef std::vector<vk_submission> vk_sequence;
  181. struct vk_mat_mat_push_constants {
  182. uint32_t M; uint32_t N; uint32_t K;
  183. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d; uint32_t k_split;
  184. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  185. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  186. uint32_t expert_stride_b; uint32_t expert_stride_d;
  187. uint32_t idx; uint32_t nbi1; uint32_t n_as;
  188. };
  189. struct vk_mat_vec_push_constants {
  190. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  191. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  192. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  193. };
  194. struct vk_op_push_constants {
  195. uint32_t KX;
  196. uint32_t KY;
  197. float param1;
  198. float param2;
  199. };
  200. struct vk_op_unary_push_constants {
  201. uint32_t ne;
  202. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  203. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  204. uint32_t d_offset;
  205. float param1; float param2;
  206. };
  207. struct vk_op_binary_push_constants {
  208. uint32_t ne;
  209. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  210. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  211. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  212. uint32_t d_offset;
  213. float param1; float param2;
  214. };
  215. struct vk_op_diag_mask_push_constants {
  216. uint32_t ncols;
  217. uint32_t rows_per_channel;
  218. int32_t n_past;
  219. };
  220. struct vk_op_rope_push_constants {
  221. uint32_t ncols;
  222. float freq_scale;
  223. uint32_t p_delta_rows;
  224. float freq_base;
  225. float ext_factor;
  226. float attn_factor;
  227. float corr_dims[4];
  228. };
  229. struct vk_op_rope_neox_push_constants {
  230. uint32_t ncols;
  231. uint32_t ndims;
  232. float freq_scale;
  233. uint32_t p_delta_rows;
  234. float freq_base;
  235. float ext_factor;
  236. float attn_factor;
  237. float corr_dims[4];
  238. float theta_scale;
  239. float inv_ndims;
  240. uint32_t has_freq_facs;
  241. };
  242. struct vk_op_soft_max_push_constants {
  243. uint32_t KX;
  244. uint32_t KY;
  245. float scale;
  246. float max_bias;
  247. float m0;
  248. float m1;
  249. uint32_t n_head_log2;
  250. };
  251. struct vk_op_argsort_push_constants {
  252. uint32_t ncols;
  253. uint32_t ncols_pad;
  254. int32_t order;
  255. };
  256. // Allow pre-recording command buffers
  257. struct vk_staging_memcpy {
  258. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  259. void * dst;
  260. const void * src;
  261. size_t n;
  262. };
  263. struct vk_context {
  264. size_t idx;
  265. vk_submission * s;
  266. std::vector<vk_sequence> seqs;
  267. ggml_tensor * exit_tensor;
  268. std::vector<vk_staging_memcpy> in_memcpys;
  269. std::vector<vk_staging_memcpy> out_memcpys;
  270. vk_queue * q;
  271. };
  272. struct ggml_tensor_extra_gpu {
  273. bool ready;
  274. size_t ctx_idx;
  275. vk_buffer_ref buffer_gpu;
  276. uint64_t offset;
  277. void reset() {
  278. ready = false;
  279. ctx_idx = 0;
  280. buffer_gpu.reset();
  281. offset = 0;
  282. }
  283. };
  284. struct ggml_vk_garbage_collector {
  285. std::vector<vk_semaphore> tl_semaphores;
  286. std::vector<vk_semaphore> semaphores;
  287. std::vector<vk::Event> events;
  288. std::vector<vk_buffer> temp_buffers;
  289. std::vector<vk_context> contexts;
  290. };
  291. struct ggml_backend_vk_context {
  292. std::string name;
  293. std::shared_ptr<vk_device> device;
  294. size_t semaphore_idx, event_idx;
  295. ggml_vk_garbage_collector gc;
  296. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  297. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  298. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  299. vk::Fence fence;
  300. vk_buffer staging;
  301. size_t staging_size;
  302. size_t staging_offset;
  303. vk_buffer sync_staging;
  304. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  305. vk_context * compute_ctx;
  306. vk_context * transfer_ctx;
  307. bool initialized;
  308. size_t idx;
  309. };
  310. struct vk_instance_t {
  311. vk::Instance instance;
  312. std::vector<size_t> device_indices;
  313. ggml_backend_t backends[GGML_VK_MAX_DEVICES];
  314. ggml_backend_vk_context contexts[GGML_VK_MAX_DEVICES];
  315. ggml_backend_buffer_type buffer_types[GGML_VK_MAX_DEVICES];
  316. bool initialized[GGML_VK_MAX_DEVICES];
  317. };
  318. static std::shared_ptr<vk_device> ggml_vk_get_device(size_t idx) {
  319. #ifdef GGML_VULKAN_DEBUG
  320. std::cerr << "ggml_vk_get_device(" << idx << ")" << std::endl;
  321. #endif
  322. static std::weak_ptr<vk_device> devices[GGML_VK_MAX_DEVICES];
  323. if (devices[idx].expired()) {
  324. #ifdef GGML_VULKAN_DEBUG
  325. std::cerr << "Initializing new vk_device" << std::endl;
  326. #endif
  327. std::shared_ptr<vk_device> device = std::make_shared<vk_device>();
  328. device->initialized = false;
  329. devices[idx] = device;
  330. return device;
  331. }
  332. return devices[idx].lock();
  333. }
  334. #ifdef GGML_VULKAN_CHECK_RESULTS
  335. static size_t vk_skip_checks;
  336. static size_t vk_output_tensor;
  337. static void ggml_vk_print_tensor(ggml_backend * ctx, const ggml_tensor * tensor, const char * name);
  338. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor);
  339. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor);
  340. #endif
  341. 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);
  342. static bool vk_instance_initialized = false;
  343. static vk_instance_t vk_instance;
  344. GGML_CALL static void ggml_backend_vk_free(ggml_backend_t backend);
  345. static void ggml_vk_create_pipeline(ggml_backend_vk_context * ctx, vk_pipeline& pipeline, const std::string& name, size_t spv_size, const void* spv_data, const std::string& entrypoint, uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t>&& specialization_constants, uint32_t align) {
  346. #ifdef GGML_VULKAN_DEBUG
  347. std::cerr << "ggml_vk_create_pipeline(" << name << ", " << entrypoint << ", " << parameter_count << ", " << push_constant_size << ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " << align << ")" << std::endl;
  348. #endif
  349. GGML_ASSERT(parameter_count > 0);
  350. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  351. pipeline = std::make_shared<vk_pipeline_struct>();
  352. pipeline->name = name;
  353. pipeline->parameter_count = parameter_count;
  354. pipeline->push_constant_size = push_constant_size;
  355. pipeline->wg_denoms = wg_denoms;
  356. pipeline->align = align;
  357. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  358. pipeline->shader_module = ctx->device->device.createShaderModule(shader_module_create_info);
  359. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  360. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  361. for (uint32_t i = 0; i < parameter_count; i++) {
  362. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  363. dsl_binding_flags.push_back({});
  364. }
  365. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  366. vk::PushConstantRange pcr(
  367. vk::ShaderStageFlagBits::eCompute,
  368. 0,
  369. pipeline->push_constant_size
  370. );
  371. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  372. {},
  373. dsl_binding);
  374. descriptor_set_layout_create_info.setPNext(&dslbfci);
  375. pipeline->dsl = ctx->device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  376. // Check if device supports multiple descriptors per pool
  377. if (ctx->device->descriptor_set_mode == VK_DEVICE_DESCRIPTOR_POOL_MODE_UNKNOWN) {
  378. const uint32_t alloc_count = 2;
  379. // Try allocating multiple sets from one pool
  380. // This fails on AMD for some reason, so add a fall back to allocating one pool per set
  381. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count);
  382. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, alloc_count, descriptor_pool_size);
  383. vk::DescriptorPool pool = ctx->device->device.createDescriptorPool(descriptor_pool_create_info);
  384. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  385. for (uint32_t i = 0; i < alloc_count; i++) {
  386. layouts[i] = pipeline->dsl;
  387. }
  388. try {
  389. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pool, alloc_count, layouts.data());
  390. std::vector<vk::DescriptorSet> sets = ctx->device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  391. } catch(vk::OutOfPoolMemoryError const&) {
  392. ctx->device->descriptor_set_mode = VK_DEVICE_DESCRIPTOR_POOL_MODE_SINGLE;
  393. }
  394. ctx->device->device.destroyDescriptorPool(pool);
  395. }
  396. if (ctx->device->descriptor_set_mode == VK_DEVICE_DESCRIPTOR_POOL_MODE_MULTI) {
  397. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count);
  398. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, 128, descriptor_pool_size);
  399. pipeline->descriptor_pools.push_back(ctx->device->device.createDescriptorPool(descriptor_pool_create_info));
  400. }
  401. pipeline->descriptor_set_idx = 0;
  402. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), pipeline->dsl, pcr);
  403. pipeline->layout = ctx->device->device.createPipelineLayout(pipeline_layout_create_info);
  404. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  405. for (size_t i = 0; i < specialization_constants.size(); i++) {
  406. specialization_entries[i].constantID = i;
  407. specialization_entries[i].offset = i * sizeof(uint32_t);
  408. specialization_entries[i].size = sizeof(uint32_t);
  409. }
  410. vk::SpecializationInfo specialization_info(
  411. specialization_entries.size(),
  412. specialization_entries.data(),
  413. specialization_constants.size() * sizeof(uint32_t),
  414. specialization_constants.data()
  415. );
  416. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  417. vk::PipelineShaderStageCreateFlags(),
  418. vk::ShaderStageFlagBits::eCompute,
  419. pipeline->shader_module,
  420. entrypoint.c_str(),
  421. &specialization_info);
  422. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  423. vk::PipelineCreateFlags(),
  424. pipeline_shader_create_info,
  425. pipeline->layout);
  426. pipeline->pipeline = ctx->device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  427. ctx->device->pipelines.push_back(pipeline);
  428. }
  429. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  430. #ifdef GGML_VULKAN_DEBUG
  431. std::cerr << "ggml_pipeline_destroy_pipeline(" << pipeline->name << ")" << std::endl;
  432. #endif
  433. for (auto& pool : pipeline->descriptor_pools) {
  434. device.destroyDescriptorPool(pool);
  435. }
  436. pipeline->descriptor_pools.clear();
  437. pipeline->descriptor_sets.clear();
  438. pipeline->descriptor_set_idx = 0;
  439. device.destroyDescriptorSetLayout(pipeline->dsl);
  440. device.destroyPipelineLayout(pipeline->layout);
  441. device.destroyShaderModule(pipeline->shader_module);
  442. device.destroyPipeline(pipeline->pipeline);
  443. }
  444. static void ggml_pipeline_allocate_descriptor_sets(ggml_backend_vk_context * ctx, vk_pipeline& pipeline, uint32_t n) {
  445. #ifdef GGML_VULKAN_DEBUG
  446. std::cerr << "ggml_pipeline_allocate_descriptor_sets(" << pipeline->name << ", " << n << ")" << std::endl;
  447. #endif
  448. if (pipeline->descriptor_sets.size() >= pipeline->descriptor_set_idx + n) {
  449. // Enough descriptors are available
  450. return;
  451. }
  452. if (ctx->device->descriptor_set_mode == VK_DEVICE_DESCRIPTOR_POOL_MODE_MULTI) {
  453. const uint32_t alloc_count = pipeline->descriptor_set_idx + n - pipeline->descriptor_sets.size();
  454. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  455. for (uint32_t i = 0; i < alloc_count; i++) {
  456. layouts[i] = pipeline->dsl;
  457. }
  458. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[0], alloc_count, layouts.data());
  459. std::vector<vk::DescriptorSet> sets = ctx->device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  460. pipeline->descriptor_sets.insert(pipeline->descriptor_sets.end(), sets.begin(), sets.end());
  461. } else {
  462. for (uint32_t i = pipeline->descriptor_sets.size(); i < pipeline->descriptor_set_idx + n; i++) {
  463. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count);
  464. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, 1, descriptor_pool_size);
  465. pipeline->descriptor_pools.push_back(ctx->device->device.createDescriptorPool(descriptor_pool_create_info));
  466. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[i], 1, &pipeline->dsl);
  467. std::vector<vk::DescriptorSet> sets = ctx->device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  468. pipeline->descriptor_sets.push_back(sets[0]);
  469. }
  470. }
  471. }
  472. static void ggml_pipeline_cleanup(vk_pipeline& pipeline) {
  473. #ifdef GGML_VULKAN_DEBUG
  474. std::cerr << "ggml_pipeline_cleanup(" << pipeline->name << ")" << std::endl;
  475. #endif
  476. pipeline->descriptor_set_idx = 0;
  477. }
  478. static vk::CommandBuffer ggml_vk_create_cmd_buffer(ggml_backend_vk_context * ctx, vk_queue& q) {
  479. #ifdef GGML_VULKAN_DEBUG
  480. std::cerr << "ggml_vk_create_cmd_buffer()" << std::endl;
  481. #endif
  482. if (q.cmd_buffers.size() > q.cmd_buffer_idx) {
  483. // Reuse command buffer
  484. return q.cmd_buffers[q.cmd_buffer_idx++];
  485. }
  486. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  487. q.pool,
  488. vk::CommandBufferLevel::ePrimary,
  489. 1);
  490. const std::vector<vk::CommandBuffer> cmd_buffers = ctx->device->device.allocateCommandBuffers(command_buffer_alloc_info);
  491. auto buf = cmd_buffers.front();
  492. q.cmd_buffers.push_back(buf);
  493. q.cmd_buffer_idx++;
  494. return buf;
  495. }
  496. static vk_submission ggml_vk_create_submission(ggml_backend_vk_context * ctx, vk_queue& q, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  497. #ifdef GGML_VULKAN_DEBUG
  498. std::cerr << "ggml_vk_create_submission()" << std::endl;
  499. #endif
  500. vk_submission s;
  501. s.buffer = ggml_vk_create_cmd_buffer(ctx, q);
  502. s.wait_semaphores = std::move(wait_semaphores);
  503. s.signal_semaphores = std::move(signal_semaphores);
  504. return s;
  505. }
  506. static void ggml_vk_submit(vk_context * ctx, vk::Fence fence) {
  507. #ifdef GGML_VULKAN_DEBUG
  508. std::cerr << "ggml_vk_submit(" << ctx->seqs.size() << ", " << fence << ")" << std::endl;
  509. #endif
  510. if (ctx->seqs.empty()) {
  511. return;
  512. }
  513. std::vector<std::vector<uint64_t>> tl_wait_vals;
  514. std::vector<std::vector<uint64_t>> tl_signal_vals;
  515. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  516. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  517. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  518. std::vector<vk::SubmitInfo> submit_infos;
  519. int idx = -1;
  520. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  521. size_t reserve = 0;
  522. for (const auto& sequence : ctx->seqs) {
  523. reserve += sequence.size();
  524. }
  525. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  526. tl_wait_semaphores.reserve(reserve);
  527. tl_wait_vals.reserve(reserve);
  528. tl_signal_semaphores.reserve(reserve);
  529. tl_signal_vals.reserve(reserve);
  530. tl_submit_infos.reserve(reserve);
  531. submit_infos.reserve(reserve);
  532. stage_flags.reserve(reserve);
  533. for (const auto& sequence : ctx->seqs) {
  534. for (const auto& submission : sequence) {
  535. stage_flags.push_back({});
  536. idx++;
  537. tl_wait_vals.push_back({});
  538. tl_wait_semaphores.push_back({});
  539. tl_signal_vals.push_back({});
  540. tl_signal_semaphores.push_back({});
  541. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  542. stage_flags[idx].push_back(ctx->q->stage_flags);
  543. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  544. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  545. }
  546. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  547. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  548. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  549. }
  550. tl_submit_infos.push_back({
  551. (uint32_t) submission.wait_semaphores.size(),
  552. tl_wait_vals[idx].data(),
  553. (uint32_t) submission.signal_semaphores.size(),
  554. tl_signal_vals[idx].data(),
  555. });
  556. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  557. tl_submit_infos[idx].pNext = nullptr;
  558. vk::SubmitInfo si{
  559. (uint32_t) submission.wait_semaphores.size(),
  560. tl_wait_semaphores[idx].data(),
  561. stage_flags[idx].data(),
  562. 1,
  563. &submission.buffer,
  564. (uint32_t) submission.signal_semaphores.size(),
  565. tl_signal_semaphores[idx].data(),
  566. };
  567. si.setPNext(&tl_submit_infos[idx]);
  568. submit_infos.push_back(si);
  569. }
  570. }
  571. ctx->q->queue.submit(submit_infos, fence);
  572. ctx->seqs.clear();
  573. }
  574. 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) {
  575. #ifdef GGML_VULKAN_DEBUG
  576. std::cerr << "ggml_vk_find_queue_family_index()" << std::endl;
  577. #endif
  578. const uint32_t qfsize = queue_family_props.size();
  579. // Try with avoid preferences first
  580. for (uint32_t i = 0; i < qfsize; i++) {
  581. 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)) {
  582. return i;
  583. }
  584. }
  585. // Fall back to only required
  586. for (size_t i = 0; i < qfsize; i++) {
  587. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  588. return i;
  589. }
  590. }
  591. // Fall back to reusing compute queue
  592. for (size_t i = 0; i < qfsize; i++) {
  593. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  594. return i;
  595. }
  596. }
  597. // Fall back to ignoring min_num_queries
  598. for (size_t i = 0; i < qfsize; i++) {
  599. if (queue_family_props[i].queueFlags & required) {
  600. return i;
  601. }
  602. }
  603. // 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.
  604. // 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.
  605. if (compute_index >= 0) {
  606. return compute_index;
  607. }
  608. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  609. for(auto &q_family : queue_family_props) {
  610. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  611. }
  612. abort();
  613. }
  614. static void ggml_vk_create_queue(ggml_backend_vk_context * ctx, vk_queue& q, uint32_t queue_family_index, uint32_t queue_index, vk::PipelineStageFlags&& stage_flags) {
  615. #ifdef GGML_VULKAN_DEBUG
  616. std::cerr << "ggml_vk_create_queue()" << std::endl;
  617. #endif
  618. q.queue_family_index = queue_family_index;
  619. vk::CommandPoolCreateInfo command_pool_create_info_compute(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), queue_family_index);
  620. q.pool = ctx->device->device.createCommandPool(command_pool_create_info_compute);
  621. q.cmd_buffer_idx = 0;
  622. q.queue = ctx->device->device.getQueue(queue_family_index, queue_index);
  623. q.stage_flags = stage_flags;
  624. }
  625. static vk_context * ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_queue& q) {
  626. #ifdef GGML_VULKAN_DEBUG
  627. std::cerr << "ggml_vk_create_context()" << std::endl;
  628. #endif
  629. ctx->gc.contexts.emplace_back();
  630. vk_context * result = &ctx->gc.contexts[ctx->gc.contexts.size() - 1];
  631. memset((void *) result, 0, sizeof(vk_context));
  632. result->idx = ctx->gc.contexts.size() - 1;
  633. result->q = &q;
  634. return result;
  635. }
  636. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  637. #ifdef GGML_VULKAN_DEBUG
  638. std::cerr << "ggml_vk_create_timeline_semaphore()" << std::endl;
  639. #endif
  640. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  641. vk::SemaphoreCreateInfo ci{};
  642. ci.setPNext(&tci);
  643. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  644. ctx->gc.semaphores.push_back({ semaphore, 0 });
  645. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  646. }
  647. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  648. #ifdef GGML_VULKAN_DEBUG
  649. std::cerr << "ggml_vk_create_timeline_semaphore()" << std::endl;
  650. #endif
  651. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  652. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  653. vk::SemaphoreCreateInfo ci{};
  654. ci.setPNext(&tci);
  655. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  656. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  657. }
  658. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  659. }
  660. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  661. if (ctx->event_idx >= ctx->gc.events.size()) {
  662. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  663. }
  664. return ctx->gc.events[ctx->event_idx++];
  665. }
  666. static void ggml_vk_queue_cleanup(ggml_backend_vk_context * ctx, vk_queue& q) {
  667. #ifdef GGML_VULKAN_DEBUG
  668. std::cerr << "ggml_vk_queue_cleanup()" << std::endl;
  669. #endif
  670. // Requires command buffers to be done
  671. ctx->device->device.resetCommandPool(q.pool);
  672. q.cmd_buffer_idx = 0;
  673. }
  674. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  675. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  676. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  677. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  678. (flags & memory_type.propertyFlags) == flags &&
  679. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  680. return static_cast<int32_t>(i);
  681. }
  682. }
  683. return UINT32_MAX;
  684. }
  685. static vk_buffer ggml_vk_create_buffer(ggml_backend_vk_context * ctx, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  686. #ifdef GGML_VULKAN_DEBUG
  687. std::cerr << "ggml_vk_create_buffer(device " << ctx->idx << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")" << std::endl;
  688. #endif
  689. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  690. if (size == 0) {
  691. buf->size = 0;
  692. return buf;
  693. }
  694. buf->size = size;
  695. vk::BufferCreateInfo buffer_create_info{
  696. vk::BufferCreateFlags(),
  697. size,
  698. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  699. vk::SharingMode::eExclusive,
  700. 0,
  701. nullptr,
  702. };
  703. buf->buffer = ctx->device->device.createBuffer(buffer_create_info);
  704. vk::MemoryRequirements mem_req = ctx->device->device.getBufferMemoryRequirements(buf->buffer);
  705. vk::PhysicalDeviceMemoryProperties mem_props = ctx->device->physical_device.getMemoryProperties();
  706. uint32_t memory_type_index = UINT32_MAX;
  707. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  708. buf->memory_property_flags = req_flags;
  709. if (memory_type_index == UINT32_MAX && fallback_flags) {
  710. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  711. buf->memory_property_flags = fallback_flags;
  712. }
  713. if (memory_type_index == UINT32_MAX) {
  714. ctx->device->device.destroyBuffer(buf->buffer);
  715. buf->size = 0;
  716. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  717. }
  718. try {
  719. buf->device_memory = ctx->device->device.allocateMemory({ mem_req.size, memory_type_index });
  720. } catch (const vk::SystemError& e) {
  721. // Out of Host/Device memory, clean up buffer
  722. ctx->device->device.destroyBuffer(buf->buffer);
  723. buf->size = 0;
  724. throw e;
  725. }
  726. buf->ptr = nullptr;
  727. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  728. buf->ptr = ctx->device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  729. }
  730. ctx->device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  731. buf->ctx = ctx;
  732. buf->device = ctx->device;
  733. #ifdef GGML_VULKAN_DEBUG
  734. std::cerr << "Created buffer " << buf->buffer << std::endl;
  735. #endif
  736. return buf;
  737. }
  738. static vk_buffer ggml_vk_create_buffer_check(ggml_backend_vk_context * ctx, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  739. try {
  740. return ggml_vk_create_buffer(ctx, size, req_flags, fallback_flags);
  741. } catch (const vk::SystemError& e) {
  742. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  743. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  744. throw e;
  745. }
  746. }
  747. static vk_buffer ggml_vk_create_buffer_device(ggml_backend_vk_context * ctx, size_t size) {
  748. vk_buffer buf;
  749. try {
  750. if (ctx->device->uma) {
  751. // Fall back to host memory type
  752. buf = ggml_vk_create_buffer(ctx, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  753. } else {
  754. buf = ggml_vk_create_buffer(ctx, size, vk::MemoryPropertyFlagBits::eDeviceLocal);
  755. }
  756. } catch (const vk::SystemError& e) {
  757. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  758. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  759. throw e;
  760. }
  761. return buf;
  762. }
  763. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  764. buf.reset();
  765. }
  766. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  767. return { buf, 0, VK_WHOLE_SIZE };
  768. }
  769. static void ggml_vk_sync_buffers(vk_context * ctx) {
  770. #ifdef GGML_VULKAN_DEBUG
  771. std::cerr << "ggml_vk_sync_buffers()" << std::endl;
  772. #endif
  773. const std::vector<vk::MemoryBarrier> mem_barriers{ { { vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite }, { vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite } } };
  774. ctx->s->buffer.pipelineBarrier(
  775. ctx->q->stage_flags,
  776. ctx->q->stage_flags,
  777. {},
  778. mem_barriers,
  779. {},
  780. {}
  781. );
  782. }
  783. static void ggml_vk_wait_events(vk_context * ctx, std::vector<vk::Event>&& events) {
  784. #ifdef GGML_VULKAN_DEBUG
  785. std::cerr << "ggml_vk_wait_events()" << std::endl;
  786. #endif
  787. if (events.empty()) {
  788. return;
  789. }
  790. ctx->s->buffer.waitEvents(
  791. events,
  792. ctx->q->stage_flags,
  793. ctx->q->stage_flags,
  794. {},
  795. {},
  796. {}
  797. );
  798. }
  799. static bool ggml_vk_build_shader(ggml_type type) {
  800. switch(type) {
  801. case GGML_TYPE_F16:
  802. case GGML_TYPE_Q4_0:
  803. case GGML_TYPE_Q4_1:
  804. case GGML_TYPE_Q5_0:
  805. case GGML_TYPE_Q5_1:
  806. case GGML_TYPE_Q8_0:
  807. case GGML_TYPE_Q2_K:
  808. case GGML_TYPE_Q3_K:
  809. case GGML_TYPE_Q4_K:
  810. case GGML_TYPE_Q5_K:
  811. case GGML_TYPE_Q6_K:
  812. return true;
  813. default:
  814. return false;
  815. }
  816. }
  817. static void ggml_vk_load_shaders(ggml_backend_vk_context * ctx) {
  818. #ifdef GGML_VULKAN_DEBUG
  819. std::cerr << "ggml_vk_load_shaders(" << ctx->name << ")" << std::endl;
  820. #endif
  821. const std::shared_ptr<vk_device> device = ctx->device;
  822. // mulmat
  823. std::initializer_list<uint32_t> warptile_l = { 128, 128, 128, 16, device->subgroup_size * 2, 64, 2, 4, 4, device->subgroup_size };
  824. std::initializer_list<uint32_t> warptile_m = { 128, 64, 64, 16, device->subgroup_size, 32, 2, 4, 2, device->subgroup_size };
  825. std::initializer_list<uint32_t> warptile_s = { device->subgroup_size, 32, 32, 16, 32, 32, 2, 2, 2, device->subgroup_size };
  826. std::initializer_list<uint32_t> warptile_mmq_l = { 128, 128, 128, 32, device->subgroup_size * 2, 64, 2, 4, 4, device->subgroup_size };
  827. std::initializer_list<uint32_t> warptile_mmq_m = { 128, 64, 64, 32, device->subgroup_size, 32, 2, 4, 2, device->subgroup_size };
  828. std::initializer_list<uint32_t> warptile_mmq_s = { device->subgroup_size, 32, 32, 32, 32, 32, 2, 2, 2, device->subgroup_size };
  829. std::array<uint32_t, 3> l_wg_denoms = {128, 128, 1 };
  830. std::array<uint32_t, 3> m_wg_denoms = { 64, 64, 1 };
  831. std::array<uint32_t, 3> s_wg_denoms = { 32, 32, 1 };
  832. uint32_t l_align = 128;
  833. uint32_t m_align = 64;
  834. uint32_t s_align = 32;
  835. ctx->device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  836. ctx->device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  837. ctx->device->pipeline_matmul_f16_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  838. ctx->device->pipeline_matmul_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  839. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0] = std::make_shared<vk_matmul_pipeline_struct>();
  840. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1] = std::make_shared<vk_matmul_pipeline_struct>();
  841. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0] = std::make_shared<vk_matmul_pipeline_struct>();
  842. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1] = std::make_shared<vk_matmul_pipeline_struct>();
  843. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0] = std::make_shared<vk_matmul_pipeline_struct>();
  844. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K] = std::make_shared<vk_matmul_pipeline_struct>();
  845. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K] = std::make_shared<vk_matmul_pipeline_struct>();
  846. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K] = std::make_shared<vk_matmul_pipeline_struct>();
  847. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K] = std::make_shared<vk_matmul_pipeline_struct>();
  848. ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K] = std::make_shared<vk_matmul_pipeline_struct>();
  849. /*ctx->device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  850. ctx->device->pipeline_matmul_id_f16_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  851. ctx->device->pipeline_matmul_id_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  852. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0] = std::make_shared<vk_matmul_pipeline_struct>();
  853. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1] = std::make_shared<vk_matmul_pipeline_struct>();
  854. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0] = std::make_shared<vk_matmul_pipeline_struct>();
  855. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1] = std::make_shared<vk_matmul_pipeline_struct>();
  856. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0] = std::make_shared<vk_matmul_pipeline_struct>();
  857. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K] = std::make_shared<vk_matmul_pipeline_struct>();
  858. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K] = std::make_shared<vk_matmul_pipeline_struct>();
  859. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K] = std::make_shared<vk_matmul_pipeline_struct>();
  860. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K] = std::make_shared<vk_matmul_pipeline_struct>();
  861. ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K] = std::make_shared<vk_matmul_pipeline_struct>();*/
  862. if (device->fp16) {
  863. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->l, "matmul_f32_l", matmul_f32_len, matmul_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  864. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->m, "matmul_f32_m", matmul_f32_len, matmul_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  865. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->s, "matmul_f32_s", matmul_f32_len, matmul_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  866. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_l, "matmul_f32_aligned_l", matmul_f32_aligned_len, matmul_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  867. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_m, "matmul_f32_aligned_m", matmul_f32_aligned_len, matmul_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  868. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_s, "matmul_f32_aligned_s", matmul_f32_aligned_len, matmul_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  869. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->l, "matmul_f32_f16_l", matmul_f32_f16_len, matmul_f32_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  870. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->m, "matmul_f32_f16_m", matmul_f32_f16_len, matmul_f32_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  871. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->s, "matmul_f32_f16_s", matmul_f32_f16_len, matmul_f32_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  872. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_l, "matmul_f32_f16_aligned_l", matmul_f32_f16_aligned_len, matmul_f32_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  873. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_m, "matmul_f32_f16_aligned_m", matmul_f32_f16_aligned_len, matmul_f32_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  874. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_s, "matmul_f32_f16_aligned_s", matmul_f32_f16_aligned_len, matmul_f32_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  875. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->l, "matmul_f16_l", matmul_f16_len, matmul_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  876. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->m, "matmul_f16_m", matmul_f16_len, matmul_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  877. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->s, "matmul_f16_s", matmul_f16_len, matmul_f16_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  878. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_l, "matmul_f16_aligned_l", matmul_f16_aligned_len, matmul_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  879. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_m, "matmul_f16_aligned_m", matmul_f16_aligned_len, matmul_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  880. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_s, "matmul_f16_aligned_s", matmul_f16_aligned_len, matmul_f16_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  881. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->l, "matmul_f16_f32_l", matmul_f16_f32_len, matmul_f16_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  882. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->m, "matmul_f16_f32_m", matmul_f16_f32_len, matmul_f16_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  883. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->s, "matmul_f16_f32_s", matmul_f16_f32_len, matmul_f16_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  884. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_l, "matmul_f16_f32_aligned_l", matmul_f16_f32_aligned_len, matmul_f16_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  885. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_m, "matmul_f16_f32_aligned_m", matmul_f16_f32_aligned_len, matmul_f16_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  886. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_s, "matmul_f16_f32_aligned_s", matmul_f16_f32_aligned_len, matmul_f16_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  887. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->l, "matmul_q4_0_f32_l", matmul_q4_0_f32_len, matmul_q4_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  888. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->m, "matmul_q4_0_f32_m", matmul_q4_0_f32_len, matmul_q4_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  889. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->s, "matmul_q4_0_f32_s", matmul_q4_0_f32_len, matmul_q4_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  890. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_l, "matmul_q4_0_f32_aligned_l", matmul_q4_0_f32_aligned_len, matmul_q4_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  891. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_m, "matmul_q4_0_f32_aligned_m", matmul_q4_0_f32_aligned_len, matmul_q4_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  892. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_s, "matmul_q4_0_f32_aligned_s", matmul_q4_0_f32_aligned_len, matmul_q4_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  893. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->l, "matmul_q4_0_f32_l", matmul_q4_1_f32_len, matmul_q4_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  894. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->m, "matmul_q4_0_f32_m", matmul_q4_1_f32_len, matmul_q4_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  895. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->s, "matmul_q4_0_f32_s", matmul_q4_1_f32_len, matmul_q4_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  896. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_l, "matmul_q4_0_f32_aligned_l", matmul_q4_1_f32_aligned_len, matmul_q4_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  897. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_m, "matmul_q4_0_f32_aligned_m", matmul_q4_1_f32_aligned_len, matmul_q4_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  898. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_s, "matmul_q4_0_f32_aligned_s", matmul_q4_1_f32_aligned_len, matmul_q4_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  899. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->l, "matmul_q5_0_f32_l", matmul_q5_0_f32_len, matmul_q5_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  900. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->m, "matmul_q5_0_f32_m", matmul_q5_0_f32_len, matmul_q5_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  901. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->s, "matmul_q5_0_f32_s", matmul_q5_0_f32_len, matmul_q5_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  902. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_l, "matmul_q5_0_f32_aligned_l", matmul_q5_0_f32_aligned_len, matmul_q5_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  903. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_m, "matmul_q5_0_f32_aligned_m", matmul_q5_0_f32_aligned_len, matmul_q5_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  904. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_s, "matmul_q5_0_f32_aligned_s", matmul_q5_0_f32_aligned_len, matmul_q5_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  905. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->l, "matmul_q5_1_f32_l", matmul_q5_1_f32_len, matmul_q5_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  906. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->m, "matmul_q5_1_f32_m", matmul_q5_1_f32_len, matmul_q5_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  907. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->s, "matmul_q5_1_f32_s", matmul_q5_1_f32_len, matmul_q5_1_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  908. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_l, "matmul_q5_1_f32_aligned_l", matmul_q5_1_f32_aligned_len, matmul_q5_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  909. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_m, "matmul_q5_1_f32_aligned_m", matmul_q5_1_f32_aligned_len, matmul_q5_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  910. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_s, "matmul_q5_1_f32_aligned_s", matmul_q5_1_f32_aligned_len, matmul_q5_1_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  911. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->l, "matmul_q8_0_f32_l", matmul_q8_0_f32_len, matmul_q8_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  912. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->m, "matmul_q8_0_f32_m", matmul_q8_0_f32_len, matmul_q8_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  913. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->s, "matmul_q8_0_f32_s", matmul_q8_0_f32_len, matmul_q8_0_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  914. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_l, "matmul_q8_0_f32_aligned_l", matmul_q8_0_f32_aligned_len, matmul_q8_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  915. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_m, "matmul_q8_0_f32_aligned_m", matmul_q8_0_f32_aligned_len, matmul_q8_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  916. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_s, "matmul_q8_0_f32_aligned_s", matmul_q8_0_f32_aligned_len, matmul_q8_0_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  917. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->l, "matmul_q2_k_f32_l", matmul_q2_k_f32_len, matmul_q2_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  918. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->m, "matmul_q2_k_f32_m", matmul_q2_k_f32_len, matmul_q2_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  919. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->s, "matmul_q2_k_f32_s", matmul_q2_k_f32_len, matmul_q2_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  920. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_l, "matmul_q2_k_f32_aligned_l", matmul_q2_k_f32_aligned_len, matmul_q2_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  921. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_m, "matmul_q2_k_f32_aligned_m", matmul_q2_k_f32_aligned_len, matmul_q2_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  922. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_s, "matmul_q2_k_f32_aligned_s", matmul_q2_k_f32_aligned_len, matmul_q2_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  923. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->l, "matmul_q3_k_f32_l", matmul_q3_k_f32_len, matmul_q3_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  924. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->m, "matmul_q3_k_f32_m", matmul_q3_k_f32_len, matmul_q3_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  925. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->s, "matmul_q3_k_f32_s", matmul_q3_k_f32_len, matmul_q3_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  926. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_l, "matmul_q3_k_f32_aligned_l", matmul_q3_k_f32_aligned_len, matmul_q3_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  927. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_m, "matmul_q3_k_f32_aligned_m", matmul_q3_k_f32_aligned_len, matmul_q3_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  928. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_s, "matmul_q3_k_f32_aligned_s", matmul_q3_k_f32_aligned_len, matmul_q3_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  929. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->l, "matmul_q4_k_f32_l", matmul_q4_k_f32_len, matmul_q4_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  930. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->m, "matmul_q4_k_f32_m", matmul_q4_k_f32_len, matmul_q4_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  931. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->s, "matmul_q4_k_f32_s", matmul_q4_k_f32_len, matmul_q4_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  932. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_l, "matmul_q4_k_f32_aligned_l", matmul_q4_k_f32_aligned_len, matmul_q4_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  933. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_m, "matmul_q4_k_f32_aligned_m", matmul_q4_k_f32_aligned_len, matmul_q4_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  934. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_s, "matmul_q4_k_f32_aligned_s", matmul_q4_k_f32_aligned_len, matmul_q4_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  935. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->l, "matmul_q5_k_f32_l", matmul_q5_k_f32_len, matmul_q5_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  936. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->m, "matmul_q5_k_f32_m", matmul_q5_k_f32_len, matmul_q5_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  937. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->s, "matmul_q5_k_f32_s", matmul_q5_k_f32_len, matmul_q5_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  938. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_l, "matmul_q5_k_f32_aligned_l", matmul_q5_k_f32_aligned_len, matmul_q5_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  939. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_m, "matmul_q5_k_f32_aligned_m", matmul_q5_k_f32_aligned_len, matmul_q5_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  940. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_s, "matmul_q5_k_f32_aligned_s", matmul_q5_k_f32_aligned_len, matmul_q5_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  941. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->l, "matmul_q6_k_f32_l", matmul_q6_k_f32_len, matmul_q6_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  942. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->m, "matmul_q6_k_f32_m", matmul_q6_k_f32_len, matmul_q6_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  943. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->s, "matmul_q6_k_f32_s", matmul_q6_k_f32_len, matmul_q6_k_f32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  944. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_l, "matmul_q6_k_f32_aligned_l", matmul_q6_k_f32_aligned_len, matmul_q6_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  945. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_m, "matmul_q6_k_f32_aligned_m", matmul_q6_k_f32_aligned_len, matmul_q6_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  946. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_s, "matmul_q6_k_f32_aligned_s", matmul_q6_k_f32_aligned_len, matmul_q6_k_f32_aligned_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  947. /*ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->l, "matmul_id_f32_l", matmul_id_f32_len, matmul_id_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  948. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->m, "matmul_id_f32_m", matmul_id_f32_len, matmul_id_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  949. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->s, "matmul_id_f32_s", matmul_id_f32_len, matmul_id_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  950. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_l, "matmul_id_f32_aligned_l", matmul_id_f32_aligned_len, matmul_id_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  951. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_m, "matmul_id_f32_aligned_m", matmul_id_f32_aligned_len, matmul_id_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  952. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_s, "matmul_id_f32_aligned_s", matmul_id_f32_aligned_len, matmul_id_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  953. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->l, "matmul_id_f16_l", matmul_id_f16_len, matmul_id_f16_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  954. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->m, "matmul_id_f16_m", matmul_id_f16_len, matmul_id_f16_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  955. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->s, "matmul_id_f16_s", matmul_id_f16_len, matmul_id_f16_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  956. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_l, "matmul_id_f16_aligned_l", matmul_id_f16_aligned_len, matmul_id_f16_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  957. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_m, "matmul_id_f16_aligned_m", matmul_id_f16_aligned_len, matmul_id_f16_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  958. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_s, "matmul_id_f16_aligned_s", matmul_id_f16_aligned_len, matmul_id_f16_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  959. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->l, "matmul_id_f16_f32_l", matmul_id_f16_f32_len, matmul_id_f16_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  960. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->m, "matmul_id_f16_f32_m", matmul_id_f16_f32_len, matmul_id_f16_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  961. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->s, "matmul_id_f16_f32_s", matmul_id_f16_f32_len, matmul_id_f16_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  962. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_l, "matmul_id_f16_f32_aligned_l", matmul_id_f16_f32_aligned_len, matmul_id_f16_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  963. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_m, "matmul_id_f16_f32_aligned_m", matmul_id_f16_f32_aligned_len, matmul_id_f16_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  964. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_s, "matmul_id_f16_f32_aligned_s", matmul_id_f16_f32_aligned_len, matmul_id_f16_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  965. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->l, "matmul_id_q4_0_f32_l", matmul_id_q4_0_f32_len, matmul_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  966. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->m, "matmul_id_q4_0_f32_m", matmul_id_q4_0_f32_len, matmul_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  967. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->s, "matmul_id_q4_0_f32_s", matmul_id_q4_0_f32_len, matmul_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  968. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_l, "matmul_id_q4_0_f32_aligned_l", matmul_id_q4_0_f32_aligned_len, matmul_id_q4_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  969. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_m, "matmul_id_q4_0_f32_aligned_m", matmul_id_q4_0_f32_aligned_len, matmul_id_q4_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  970. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_s, "matmul_id_q4_0_f32_aligned_s", matmul_id_q4_0_f32_aligned_len, matmul_id_q4_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  971. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->l, "matmul_id_q4_0_f32_l", matmul_id_q4_1_f32_len, matmul_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  972. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->m, "matmul_id_q4_0_f32_m", matmul_id_q4_1_f32_len, matmul_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  973. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->s, "matmul_id_q4_0_f32_s", matmul_id_q4_1_f32_len, matmul_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  974. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_l, "matmul_id_q4_0_f32_aligned_l", matmul_id_q4_1_f32_aligned_len, matmul_id_q4_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  975. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_m, "matmul_id_q4_0_f32_aligned_m", matmul_id_q4_1_f32_aligned_len, matmul_id_q4_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  976. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_s, "matmul_id_q4_0_f32_aligned_s", matmul_id_q4_1_f32_aligned_len, matmul_id_q4_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  977. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->l, "matmul_id_q5_0_f32_l", matmul_id_q5_0_f32_len, matmul_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  978. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->m, "matmul_id_q5_0_f32_m", matmul_id_q5_0_f32_len, matmul_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  979. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->s, "matmul_id_q5_0_f32_s", matmul_id_q5_0_f32_len, matmul_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  980. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_l, "matmul_id_q5_0_f32_aligned_l", matmul_id_q5_0_f32_aligned_len, matmul_id_q5_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  981. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_m, "matmul_id_q5_0_f32_aligned_m", matmul_id_q5_0_f32_aligned_len, matmul_id_q5_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  982. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_s, "matmul_id_q5_0_f32_aligned_s", matmul_id_q5_0_f32_aligned_len, matmul_id_q5_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  983. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->l, "matmul_id_q5_1_f32_l", matmul_id_q5_1_f32_len, matmul_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  984. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->m, "matmul_id_q5_1_f32_m", matmul_id_q5_1_f32_len, matmul_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  985. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->s, "matmul_id_q5_1_f32_s", matmul_id_q5_1_f32_len, matmul_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  986. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_l, "matmul_id_q5_1_f32_aligned_l", matmul_id_q5_1_f32_aligned_len, matmul_id_q5_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  987. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_m, "matmul_id_q5_1_f32_aligned_m", matmul_id_q5_1_f32_aligned_len, matmul_id_q5_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  988. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_s, "matmul_id_q5_1_f32_aligned_s", matmul_id_q5_1_f32_aligned_len, matmul_id_q5_1_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  989. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->l, "matmul_id_q8_0_f32_l", matmul_id_q8_0_f32_len, matmul_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  990. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->m, "matmul_id_q8_0_f32_m", matmul_id_q8_0_f32_len, matmul_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  991. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->s, "matmul_id_q8_0_f32_s", matmul_id_q8_0_f32_len, matmul_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  992. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_l, "matmul_id_q8_0_f32_aligned_l", matmul_id_q8_0_f32_aligned_len, matmul_id_q8_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  993. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_m, "matmul_id_q8_0_f32_aligned_m", matmul_id_q8_0_f32_aligned_len, matmul_id_q8_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  994. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_s, "matmul_id_q8_0_f32_aligned_s", matmul_id_q8_0_f32_aligned_len, matmul_id_q8_0_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  995. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->l, "matmul_id_q2_k_f32_l", matmul_id_q2_k_f32_len, matmul_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  996. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->m, "matmul_id_q2_k_f32_m", matmul_id_q2_k_f32_len, matmul_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  997. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->s, "matmul_id_q2_k_f32_s", matmul_id_q2_k_f32_len, matmul_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  998. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_l, "matmul_id_q2_k_f32_aligned_l", matmul_id_q2_k_f32_aligned_len, matmul_id_q2_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  999. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_m, "matmul_id_q2_k_f32_aligned_m", matmul_id_q2_k_f32_aligned_len, matmul_id_q2_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1000. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_s, "matmul_id_q2_k_f32_aligned_s", matmul_id_q2_k_f32_aligned_len, matmul_id_q2_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1001. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->l, "matmul_id_q3_k_f32_l", matmul_id_q3_k_f32_len, matmul_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1002. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->m, "matmul_id_q3_k_f32_m", matmul_id_q3_k_f32_len, matmul_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1003. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->s, "matmul_id_q3_k_f32_s", matmul_id_q3_k_f32_len, matmul_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1004. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_l, "matmul_id_q3_k_f32_aligned_l", matmul_id_q3_k_f32_aligned_len, matmul_id_q3_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1005. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_m, "matmul_id_q3_k_f32_aligned_m", matmul_id_q3_k_f32_aligned_len, matmul_id_q3_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1006. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_s, "matmul_id_q3_k_f32_aligned_s", matmul_id_q3_k_f32_aligned_len, matmul_id_q3_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1007. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->l, "matmul_id_q4_k_f32_l", matmul_id_q4_k_f32_len, matmul_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1008. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->m, "matmul_id_q4_k_f32_m", matmul_id_q4_k_f32_len, matmul_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1009. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->s, "matmul_id_q4_k_f32_s", matmul_id_q4_k_f32_len, matmul_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1010. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_l, "matmul_id_q4_k_f32_aligned_l", matmul_id_q4_k_f32_aligned_len, matmul_id_q4_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1011. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_m, "matmul_id_q4_k_f32_aligned_m", matmul_id_q4_k_f32_aligned_len, matmul_id_q4_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1012. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_s, "matmul_id_q4_k_f32_aligned_s", matmul_id_q4_k_f32_aligned_len, matmul_id_q4_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1013. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->l, "matmul_id_q5_k_f32_l", matmul_id_q5_k_f32_len, matmul_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1014. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->m, "matmul_id_q5_k_f32_m", matmul_id_q5_k_f32_len, matmul_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1015. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->s, "matmul_id_q5_k_f32_s", matmul_id_q5_k_f32_len, matmul_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1016. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_l, "matmul_id_q5_k_f32_aligned_l", matmul_id_q5_k_f32_aligned_len, matmul_id_q5_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1017. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_m, "matmul_id_q5_k_f32_aligned_m", matmul_id_q5_k_f32_aligned_len, matmul_id_q5_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1018. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_s, "matmul_id_q5_k_f32_aligned_s", matmul_id_q5_k_f32_aligned_len, matmul_id_q5_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1019. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->l, "matmul_id_q6_k_f32_l", matmul_id_q6_k_f32_len, matmul_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1020. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->m, "matmul_id_q6_k_f32_m", matmul_id_q6_k_f32_len, matmul_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1021. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->s, "matmul_id_q6_k_f32_s", matmul_id_q6_k_f32_len, matmul_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1022. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_l, "matmul_id_q6_k_f32_aligned_l", matmul_id_q6_k_f32_aligned_len, matmul_id_q6_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1023. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_m, "matmul_id_q6_k_f32_aligned_m", matmul_id_q6_k_f32_aligned_len, matmul_id_q6_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1024. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_s, "matmul_id_q6_k_f32_aligned_s", matmul_id_q6_k_f32_aligned_len, matmul_id_q6_k_f32_aligned_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);*/
  1025. } else {
  1026. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->l, "matmul_f32_l", matmul_f32_fp32_len, matmul_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1027. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->m, "matmul_f32_m", matmul_f32_fp32_len, matmul_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1028. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->s, "matmul_f32_s", matmul_f32_fp32_len, matmul_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1029. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_l, "matmul_f32_aligned_l", matmul_f32_aligned_fp32_len, matmul_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1030. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_m, "matmul_f32_aligned_m", matmul_f32_aligned_fp32_len, matmul_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1031. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32->a_s, "matmul_f32_aligned_s", matmul_f32_aligned_fp32_len, matmul_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1032. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->l, "matmul_f32_f16_l", matmul_f32_f16_fp32_len, matmul_f32_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1033. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->m, "matmul_f32_f16_m", matmul_f32_f16_fp32_len, matmul_f32_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1034. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->s, "matmul_f32_f16_s", matmul_f32_f16_fp32_len, matmul_f32_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1035. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_l, "matmul_f32_f16_aligned_l", matmul_f32_f16_aligned_fp32_len, matmul_f32_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1036. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_m, "matmul_f32_f16_aligned_m", matmul_f32_f16_aligned_fp32_len, matmul_f32_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1037. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f32_f16->a_s, "matmul_f32_f16_aligned_s", matmul_f32_f16_aligned_fp32_len, matmul_f32_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1038. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->l, "matmul_f16_l", matmul_f16_fp32_len, matmul_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1039. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->m, "matmul_f16_m", matmul_f16_fp32_len, matmul_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1040. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->s, "matmul_f16_s", matmul_f16_fp32_len, matmul_f16_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1041. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_l, "matmul_f16_aligned_l", matmul_f16_aligned_fp32_len, matmul_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1042. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_m, "matmul_f16_aligned_m", matmul_f16_aligned_fp32_len, matmul_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1043. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16->a_s, "matmul_f16_aligned_s", matmul_f16_aligned_fp32_len, matmul_f16_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1044. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->l, "matmul_f16_f32_l", matmul_f16_f32_fp32_len, matmul_f16_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1045. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->m, "matmul_f16_f32_m", matmul_f16_f32_fp32_len, matmul_f16_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1046. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->s, "matmul_f16_f32_s", matmul_f16_f32_fp32_len, matmul_f16_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1047. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_l, "matmul_f16_f32_aligned_l", matmul_f16_f32_aligned_fp32_len, matmul_f16_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1048. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_m, "matmul_f16_f32_aligned_m", matmul_f16_f32_aligned_fp32_len, matmul_f16_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1049. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_f16_f32->a_s, "matmul_f16_f32_aligned_s", matmul_f16_f32_aligned_fp32_len, matmul_f16_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1050. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->l, "matmul_q4_0_f32_l", matmul_q4_0_f32_fp32_len, matmul_q4_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1051. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->m, "matmul_q4_0_f32_m", matmul_q4_0_f32_fp32_len, matmul_q4_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1052. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->s, "matmul_q4_0_f32_s", matmul_q4_0_f32_fp32_len, matmul_q4_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1053. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_l, "matmul_q4_0_f32_aligned_l", matmul_q4_0_f32_aligned_fp32_len, matmul_q4_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1054. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_m, "matmul_q4_0_f32_aligned_m", matmul_q4_0_f32_aligned_fp32_len, matmul_q4_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1055. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0]->a_s, "matmul_q4_0_f32_aligned_s", matmul_q4_0_f32_aligned_fp32_len, matmul_q4_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1056. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->l, "matmul_q4_1_f32_l", matmul_q4_1_f32_fp32_len, matmul_q4_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1057. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->m, "matmul_q4_1_f32_m", matmul_q4_1_f32_fp32_len, matmul_q4_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1058. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->s, "matmul_q4_1_f32_s", matmul_q4_1_f32_fp32_len, matmul_q4_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1059. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_l, "matmul_q4_1_f32_aligned_l", matmul_q4_1_f32_aligned_fp32_len, matmul_q4_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1060. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_m, "matmul_q4_1_f32_aligned_m", matmul_q4_1_f32_aligned_fp32_len, matmul_q4_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1061. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1]->a_s, "matmul_q4_1_f32_aligned_s", matmul_q4_1_f32_aligned_fp32_len, matmul_q4_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1062. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->l, "matmul_q5_0_f32_l", matmul_q5_0_f32_fp32_len, matmul_q5_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1063. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->m, "matmul_q5_0_f32_m", matmul_q5_0_f32_fp32_len, matmul_q5_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1064. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->s, "matmul_q5_0_f32_s", matmul_q5_0_f32_fp32_len, matmul_q5_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1065. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_l, "matmul_q5_0_f32_aligned_l", matmul_q5_0_f32_aligned_fp32_len, matmul_q5_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1066. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_m, "matmul_q5_0_f32_aligned_m", matmul_q5_0_f32_aligned_fp32_len, matmul_q5_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1067. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0]->a_s, "matmul_q5_0_f32_aligned_s", matmul_q5_0_f32_aligned_fp32_len, matmul_q5_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1068. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->l, "matmul_q5_1_f32_l", matmul_q5_1_f32_fp32_len, matmul_q5_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1069. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->m, "matmul_q5_1_f32_m", matmul_q5_1_f32_fp32_len, matmul_q5_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1070. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->s, "matmul_q5_1_f32_s", matmul_q5_1_f32_fp32_len, matmul_q5_1_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1071. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_l, "matmul_q5_1_f32_aligned_l", matmul_q5_1_f32_aligned_fp32_len, matmul_q5_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1072. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_m, "matmul_q5_1_f32_aligned_m", matmul_q5_1_f32_aligned_fp32_len, matmul_q5_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1073. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1]->a_s, "matmul_q5_1_f32_aligned_s", matmul_q5_1_f32_aligned_fp32_len, matmul_q5_1_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1074. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->l, "matmul_q8_0_f32_l", matmul_q8_0_f32_fp32_len, matmul_q8_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1075. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->m, "matmul_q8_0_f32_m", matmul_q8_0_f32_fp32_len, matmul_q8_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1076. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->s, "matmul_q8_0_f32_s", matmul_q8_0_f32_fp32_len, matmul_q8_0_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1077. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_l, "matmul_q8_0_f32_aligned_l", matmul_q8_0_f32_aligned_fp32_len, matmul_q8_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1078. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_m, "matmul_q8_0_f32_aligned_m", matmul_q8_0_f32_aligned_fp32_len, matmul_q8_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1079. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0]->a_s, "matmul_q8_0_f32_aligned_s", matmul_q8_0_f32_aligned_fp32_len, matmul_q8_0_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1080. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->l, "matmul_q2_k_f32_l", matmul_q2_k_f32_fp32_len, matmul_q2_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1081. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->m, "matmul_q2_k_f32_m", matmul_q2_k_f32_fp32_len, matmul_q2_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1082. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->s, "matmul_q2_k_f32_s", matmul_q2_k_f32_fp32_len, matmul_q2_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1083. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_l, "matmul_q2_k_f32_aligned_l", matmul_q2_k_f32_aligned_fp32_len, matmul_q2_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1084. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_m, "matmul_q2_k_f32_aligned_m", matmul_q2_k_f32_aligned_fp32_len, matmul_q2_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1085. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K]->a_s, "matmul_q2_k_f32_aligned_s", matmul_q2_k_f32_aligned_fp32_len, matmul_q2_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1086. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->l, "matmul_q3_k_f32_l", matmul_q3_k_f32_fp32_len, matmul_q3_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1087. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->m, "matmul_q3_k_f32_m", matmul_q3_k_f32_fp32_len, matmul_q3_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1088. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->s, "matmul_q3_k_f32_s", matmul_q3_k_f32_fp32_len, matmul_q3_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1089. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_l, "matmul_q3_k_f32_aligned_l", matmul_q3_k_f32_aligned_fp32_len, matmul_q3_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1090. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_m, "matmul_q3_k_f32_aligned_m", matmul_q3_k_f32_aligned_fp32_len, matmul_q3_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1091. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K]->a_s, "matmul_q3_k_f32_aligned_s", matmul_q3_k_f32_aligned_fp32_len, matmul_q3_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1092. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->l, "matmul_q4_k_f32_l", matmul_q4_k_f32_fp32_len, matmul_q4_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1093. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->m, "matmul_q4_k_f32_m", matmul_q4_k_f32_fp32_len, matmul_q4_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1094. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->s, "matmul_q4_k_f32_s", matmul_q4_k_f32_fp32_len, matmul_q4_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1095. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_l, "matmul_q4_k_f32_aligned_l", matmul_q4_k_f32_aligned_fp32_len, matmul_q4_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1096. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_m, "matmul_q4_k_f32_aligned_m", matmul_q4_k_f32_aligned_fp32_len, matmul_q4_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1097. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K]->a_s, "matmul_q4_k_f32_aligned_s", matmul_q4_k_f32_aligned_fp32_len, matmul_q4_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1098. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->l, "matmul_q5_k_f32_l", matmul_q5_k_f32_fp32_len, matmul_q5_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1099. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->m, "matmul_q5_k_f32_m", matmul_q5_k_f32_fp32_len, matmul_q5_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1100. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->s, "matmul_q5_k_f32_s", matmul_q5_k_f32_fp32_len, matmul_q5_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1101. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_l, "matmul_q5_k_f32_aligned_l", matmul_q5_k_f32_aligned_fp32_len, matmul_q5_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1102. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_m, "matmul_q5_k_f32_aligned_m", matmul_q5_k_f32_aligned_fp32_len, matmul_q5_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1103. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K]->a_s, "matmul_q5_k_f32_aligned_s", matmul_q5_k_f32_aligned_fp32_len, matmul_q5_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1104. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->l, "matmul_q6_k_f32_l", matmul_q6_k_f32_fp32_len, matmul_q6_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1105. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->m, "matmul_q6_k_f32_m", matmul_q6_k_f32_fp32_len, matmul_q6_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1106. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->s, "matmul_q6_k_f32_s", matmul_q6_k_f32_fp32_len, matmul_q6_k_f32_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1107. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_l, "matmul_q6_k_f32_aligned_l", matmul_q6_k_f32_aligned_fp32_len, matmul_q6_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1108. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_m, "matmul_q6_k_f32_aligned_m", matmul_q6_k_f32_aligned_fp32_len, matmul_q6_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1109. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K]->a_s, "matmul_q6_k_f32_aligned_s", matmul_q6_k_f32_aligned_fp32_len, matmul_q6_k_f32_aligned_fp32_data, "main", 3, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1110. /*ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->l, "matmul_id_f32_l", matmul_id_f32_fp32_len, matmul_id_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1111. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->m, "matmul_id_f32_m", matmul_id_f32_fp32_len, matmul_id_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1112. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->s, "matmul_id_f32_s", matmul_id_f32_fp32_len, matmul_id_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1113. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_l, "matmul_id_f32_aligned_l", matmul_id_f32_aligned_fp32_len, matmul_id_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1114. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_m, "matmul_id_f32_aligned_m", matmul_id_f32_aligned_fp32_len, matmul_id_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1115. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f32->a_s, "matmul_id_f32_aligned_s", matmul_id_f32_aligned_fp32_len, matmul_id_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1116. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->l, "matmul_id_f16_l", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1117. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->m, "matmul_id_f16_m", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1118. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->s, "matmul_id_f16_s", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1119. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_l, "matmul_id_f16_aligned_l", matmul_id_f16_aligned_fp32_len, matmul_id_f16_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1120. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_m, "matmul_id_f16_aligned_m", matmul_id_f16_aligned_fp32_len, matmul_id_f16_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1121. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16->a_s, "matmul_id_f16_aligned_s", matmul_id_f16_aligned_fp32_len, matmul_id_f16_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1122. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->l, "matmul_id_f16_f32_l", matmul_id_f16_f32_fp32_len, matmul_id_f16_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, 1);
  1123. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->m, "matmul_id_f16_f32_m", matmul_id_f16_f32_fp32_len, matmul_id_f16_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, 1);
  1124. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->s, "matmul_id_f16_f32_s", matmul_id_f16_f32_fp32_len, matmul_id_f16_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, 1);
  1125. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_l, "matmul_id_f16_f32_aligned_l", matmul_id_f16_f32_aligned_fp32_len, matmul_id_f16_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_l, l_align);
  1126. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_m, "matmul_id_f16_f32_aligned_m", matmul_id_f16_f32_aligned_fp32_len, matmul_id_f16_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_m, m_align);
  1127. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_id_f16_f32->a_s, "matmul_id_f16_f32_aligned_s", matmul_id_f16_f32_aligned_fp32_len, matmul_id_f16_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_s, s_align);
  1128. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->l, "matmul_id_q4_0_f32_l", matmul_id_q4_0_f32_fp32_len, matmul_id_q4_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1129. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->m, "matmul_id_q4_0_f32_m", matmul_id_q4_0_f32_fp32_len, matmul_id_q4_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1130. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->s, "matmul_id_q4_0_f32_s", matmul_id_q4_0_f32_fp32_len, matmul_id_q4_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1131. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_l, "matmul_id_q4_0_f32_aligned_l", matmul_id_q4_0_f32_aligned_fp32_len, matmul_id_q4_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1132. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_m, "matmul_id_q4_0_f32_aligned_m", matmul_id_q4_0_f32_aligned_fp32_len, matmul_id_q4_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1133. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0]->a_s, "matmul_id_q4_0_f32_aligned_s", matmul_id_q4_0_f32_aligned_fp32_len, matmul_id_q4_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1134. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->l, "matmul_id_q4_0_f32_l", matmul_id_q4_1_f32_fp32_len, matmul_id_q4_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1135. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->m, "matmul_id_q4_0_f32_m", matmul_id_q4_1_f32_fp32_len, matmul_id_q4_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1136. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->s, "matmul_id_q4_0_f32_s", matmul_id_q4_1_f32_fp32_len, matmul_id_q4_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1137. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_l, "matmul_id_q4_0_f32_aligned_l", matmul_id_q4_1_f32_aligned_fp32_len, matmul_id_q4_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1138. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_m, "matmul_id_q4_0_f32_aligned_m", matmul_id_q4_1_f32_aligned_fp32_len, matmul_id_q4_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1139. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1]->a_s, "matmul_id_q4_0_f32_aligned_s", matmul_id_q4_1_f32_aligned_fp32_len, matmul_id_q4_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1140. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->l, "matmul_id_q5_0_f32_l", matmul_id_q5_0_f32_fp32_len, matmul_id_q5_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1141. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->m, "matmul_id_q5_0_f32_m", matmul_id_q5_0_f32_fp32_len, matmul_id_q5_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1142. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->s, "matmul_id_q5_0_f32_s", matmul_id_q5_0_f32_fp32_len, matmul_id_q5_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1143. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_l, "matmul_id_q5_0_f32_aligned_l", matmul_id_q5_0_f32_aligned_fp32_len, matmul_id_q5_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1144. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_m, "matmul_id_q5_0_f32_aligned_m", matmul_id_q5_0_f32_aligned_fp32_len, matmul_id_q5_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1145. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0]->a_s, "matmul_id_q5_0_f32_aligned_s", matmul_id_q5_0_f32_aligned_fp32_len, matmul_id_q5_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1146. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->l, "matmul_id_q5_1_f32_l", matmul_id_q5_1_f32_fp32_len, matmul_id_q5_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1147. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->m, "matmul_id_q5_1_f32_m", matmul_id_q5_1_f32_fp32_len, matmul_id_q5_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1148. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->s, "matmul_id_q5_1_f32_s", matmul_id_q5_1_f32_fp32_len, matmul_id_q5_1_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1149. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_l, "matmul_id_q5_1_f32_aligned_l", matmul_id_q5_1_f32_aligned_fp32_len, matmul_id_q5_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1150. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_m, "matmul_id_q5_1_f32_aligned_m", matmul_id_q5_1_f32_aligned_fp32_len, matmul_id_q5_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1151. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1]->a_s, "matmul_id_q5_1_f32_aligned_s", matmul_id_q5_1_f32_aligned_fp32_len, matmul_id_q5_1_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1152. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->l, "matmul_id_q8_0_f32_l", matmul_id_q8_0_f32_fp32_len, matmul_id_q8_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1153. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->m, "matmul_id_q8_0_f32_m", matmul_id_q8_0_f32_fp32_len, matmul_id_q8_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1154. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->s, "matmul_id_q8_0_f32_s", matmul_id_q8_0_f32_fp32_len, matmul_id_q8_0_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1155. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_l, "matmul_id_q8_0_f32_aligned_l", matmul_id_q8_0_f32_aligned_fp32_len, matmul_id_q8_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1156. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_m, "matmul_id_q8_0_f32_aligned_m", matmul_id_q8_0_f32_aligned_fp32_len, matmul_id_q8_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1157. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0]->a_s, "matmul_id_q8_0_f32_aligned_s", matmul_id_q8_0_f32_aligned_fp32_len, matmul_id_q8_0_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1158. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->l, "matmul_id_q2_k_f32_l", matmul_id_q2_k_f32_fp32_len, matmul_id_q2_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1159. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->m, "matmul_id_q2_k_f32_m", matmul_id_q2_k_f32_fp32_len, matmul_id_q2_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1160. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->s, "matmul_id_q2_k_f32_s", matmul_id_q2_k_f32_fp32_len, matmul_id_q2_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1161. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_l, "matmul_id_q2_k_f32_aligned_l", matmul_id_q2_k_f32_aligned_fp32_len, matmul_id_q2_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1162. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_m, "matmul_id_q2_k_f32_aligned_m", matmul_id_q2_k_f32_aligned_fp32_len, matmul_id_q2_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1163. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K]->a_s, "matmul_id_q2_k_f32_aligned_s", matmul_id_q2_k_f32_aligned_fp32_len, matmul_id_q2_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1164. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->l, "matmul_id_q3_k_f32_l", matmul_id_q3_k_f32_fp32_len, matmul_id_q3_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1165. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->m, "matmul_id_q3_k_f32_m", matmul_id_q3_k_f32_fp32_len, matmul_id_q3_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1166. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->s, "matmul_id_q3_k_f32_s", matmul_id_q3_k_f32_fp32_len, matmul_id_q3_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1167. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_l, "matmul_id_q3_k_f32_aligned_l", matmul_id_q3_k_f32_aligned_fp32_len, matmul_id_q3_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1168. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_m, "matmul_id_q3_k_f32_aligned_m", matmul_id_q3_k_f32_aligned_fp32_len, matmul_id_q3_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1169. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K]->a_s, "matmul_id_q3_k_f32_aligned_s", matmul_id_q3_k_f32_aligned_fp32_len, matmul_id_q3_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1170. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->l, "matmul_id_q4_k_f32_l", matmul_id_q4_k_f32_fp32_len, matmul_id_q4_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1171. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->m, "matmul_id_q4_k_f32_m", matmul_id_q4_k_f32_fp32_len, matmul_id_q4_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1172. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->s, "matmul_id_q4_k_f32_s", matmul_id_q4_k_f32_fp32_len, matmul_id_q4_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1173. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_l, "matmul_id_q4_k_f32_aligned_l", matmul_id_q4_k_f32_aligned_fp32_len, matmul_id_q4_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1174. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_m, "matmul_id_q4_k_f32_aligned_m", matmul_id_q4_k_f32_aligned_fp32_len, matmul_id_q4_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1175. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K]->a_s, "matmul_id_q4_k_f32_aligned_s", matmul_id_q4_k_f32_aligned_fp32_len, matmul_id_q4_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1176. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->l, "matmul_id_q5_k_f32_l", matmul_id_q5_k_f32_fp32_len, matmul_id_q5_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1177. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->m, "matmul_id_q5_k_f32_m", matmul_id_q5_k_f32_fp32_len, matmul_id_q5_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1178. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->s, "matmul_id_q5_k_f32_s", matmul_id_q5_k_f32_fp32_len, matmul_id_q5_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1179. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_l, "matmul_id_q5_k_f32_aligned_l", matmul_id_q5_k_f32_aligned_fp32_len, matmul_id_q5_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1180. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_m, "matmul_id_q5_k_f32_aligned_m", matmul_id_q5_k_f32_aligned_fp32_len, matmul_id_q5_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1181. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K]->a_s, "matmul_id_q5_k_f32_aligned_s", matmul_id_q5_k_f32_aligned_fp32_len, matmul_id_q5_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1182. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->l, "matmul_id_q6_k_f32_l", matmul_id_q6_k_f32_fp32_len, matmul_id_q6_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1183. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->m, "matmul_id_q6_k_f32_m", matmul_id_q6_k_f32_fp32_len, matmul_id_q6_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1184. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->s, "matmul_id_q6_k_f32_s", matmul_id_q6_k_f32_fp32_len, matmul_id_q6_k_f32_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);
  1185. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_l, "matmul_id_q6_k_f32_aligned_l", matmul_id_q6_k_f32_aligned_fp32_len, matmul_id_q6_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), l_wg_denoms, warptile_mmq_l, l_align);
  1186. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_m, "matmul_id_q6_k_f32_aligned_m", matmul_id_q6_k_f32_aligned_fp32_len, matmul_id_q6_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), m_wg_denoms, warptile_mmq_m, m_align);
  1187. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K]->a_s, "matmul_id_q6_k_f32_aligned_s", matmul_id_q6_k_f32_aligned_fp32_len, matmul_id_q6_k_f32_aligned_fp32_data, "main", 4, sizeof(vk_mat_mat_push_constants), s_wg_denoms, warptile_mmq_s, s_align);*/
  1188. }
  1189. // mul mat vec
  1190. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ], "mul_mat_vec_f16_f32_f32", mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1191. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0], "mul_mat_vec_q4_0_f32_f32", mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1192. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1], "mul_mat_vec_q4_1_f32_f32", mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1193. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0], "mul_mat_vec_q5_0_f32_f32", mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1194. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1], "mul_mat_vec_q5_1_f32_f32", mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1195. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0], "mul_mat_vec_q8_0_f32_f32", 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, 1, 1}, { device->subgroup_size }, 1);
  1196. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K], "mul_mat_vec_q2_K_f32_f32", mul_mat_vec_q2_K_f32_f32_len, mul_mat_vec_q2_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1197. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K], "mul_mat_vec_q3_K_f32_f32", mul_mat_vec_q3_K_f32_f32_len, mul_mat_vec_q3_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1198. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K], "mul_mat_vec_q4_K_f32_f32", mul_mat_vec_q4_K_f32_f32_len, mul_mat_vec_q4_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1199. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K], "mul_mat_vec_q5_K_f32_f32", mul_mat_vec_q5_K_f32_f32_len, mul_mat_vec_q5_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1200. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K], "mul_mat_vec_q6_K_f32_f32", mul_mat_vec_q6_K_f32_f32_len, mul_mat_vec_q6_K_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1201. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ], "mul_mat_vec_f16_f16_f32", mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1202. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0], "mul_mat_vec_q4_0_f16_f32", mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1203. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1], "mul_mat_vec_q4_1_f16_f32", mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1204. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0], "mul_mat_vec_q5_0_f16_f32", mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1205. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1], "mul_mat_vec_q5_1_f16_f32", mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1206. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0], "mul_mat_vec_q8_0_f16_f32", 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, 1, 1}, { device->subgroup_size }, 1);
  1207. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K], "mul_mat_vec_q2_K_f16_f32", mul_mat_vec_q2_K_f16_f32_len, mul_mat_vec_q2_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1208. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K], "mul_mat_vec_q3_K_f16_f32", mul_mat_vec_q3_K_f16_f32_len, mul_mat_vec_q3_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1209. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K], "mul_mat_vec_q4_K_f16_f32", mul_mat_vec_q4_K_f16_f32_len, mul_mat_vec_q4_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1210. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K], "mul_mat_vec_q5_K_f16_f32", mul_mat_vec_q5_K_f16_f32_len, mul_mat_vec_q5_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1211. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K], "mul_mat_vec_q6_K_f16_f32", mul_mat_vec_q6_K_f16_f32_len, mul_mat_vec_q6_K_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1212. /*ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1213. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1214. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1215. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1216. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1217. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1218. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1219. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1220. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1221. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  1222. ggml_vk_create_pipeline(ctx, ctx->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_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);*/
  1223. // dequant shaders
  1224. ggml_vk_create_pipeline(ctx, ctx->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);
  1225. ggml_vk_create_pipeline(ctx, ctx->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);
  1226. ggml_vk_create_pipeline(ctx, ctx->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);
  1227. ggml_vk_create_pipeline(ctx, ctx->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);
  1228. ggml_vk_create_pipeline(ctx, ctx->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);
  1229. ggml_vk_create_pipeline(ctx, ctx->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);
  1230. ggml_vk_create_pipeline(ctx, ctx->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);
  1231. ggml_vk_create_pipeline(ctx, ctx->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);
  1232. ggml_vk_create_pipeline(ctx, ctx->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);
  1233. ggml_vk_create_pipeline(ctx, ctx->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);
  1234. ggml_vk_create_pipeline(ctx, ctx->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);
  1235. // get_rows
  1236. ggml_vk_create_pipeline(ctx, ctx->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);
  1237. ggml_vk_create_pipeline(ctx, ctx->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);
  1238. ggml_vk_create_pipeline(ctx, ctx->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);
  1239. ggml_vk_create_pipeline(ctx, ctx->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);
  1240. ggml_vk_create_pipeline(ctx, ctx->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);
  1241. ggml_vk_create_pipeline(ctx, ctx->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);
  1242. ggml_vk_create_pipeline(ctx, ctx->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);
  1243. ggml_vk_create_pipeline(ctx, ctx->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);
  1244. ggml_vk_create_pipeline(ctx, ctx->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);
  1245. ggml_vk_create_pipeline(ctx, ctx->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);
  1246. ggml_vk_create_pipeline(ctx, ctx->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);
  1247. ggml_vk_create_pipeline(ctx, ctx->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);
  1248. ggml_vk_create_pipeline(ctx, ctx->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);
  1249. ggml_vk_create_pipeline(ctx, ctx->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);
  1250. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_matmul_split_k_reduce, "split_k_reduce", split_k_reduce_len, split_k_reduce_data, "main", 2, 2 * sizeof(uint32_t), {256, 1, 1}, {}, 1);
  1251. ggml_vk_create_pipeline(ctx, ctx->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);
  1252. ggml_vk_create_pipeline(ctx, ctx->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);
  1253. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_norm_f32, "norm_f32", norm_f32_len, norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  1254. ggml_vk_create_pipeline(ctx, ctx->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);
  1255. ggml_vk_create_pipeline(ctx, ctx->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);
  1256. ggml_vk_create_pipeline(ctx, ctx->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);
  1257. ggml_vk_create_pipeline(ctx, ctx->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);
  1258. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_add_f32, "add_f32", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1259. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_mul_f32, "mul_f32", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  1260. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_scale_f32, "scale_f32", scale_f32_len, scale_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1261. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_sqr_f32, "sqr_f32", sqr_f32_len, sqr_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1262. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_clamp_f32, "clamp_f32", clamp_f32_len, clamp_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  1263. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_gelu_f32, "gelu_f32", gelu_f32_len, gelu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1264. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_silu_f32, "silu_f32", silu_f32_len, silu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1265. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_relu_f32, "relu_f32", relu_f32_len, relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  1266. ggml_vk_create_pipeline(ctx, ctx->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), {512, 1, 1}, {}, 1);
  1267. ggml_vk_create_pipeline(ctx, ctx->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}, {}, 1);
  1268. ggml_vk_create_pipeline(ctx, ctx->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}, {}, 1);
  1269. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_rope_f32, "rope_f32", rope_f32_len, rope_f32_data, "main", 3, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  1270. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_rope_f16, "rope_f16", rope_f16_len, rope_f16_data, "main", 3, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  1271. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_rope_neox_f32, "rope_neox_f32", rope_neox_f32_len, rope_neox_f32_data, "main", 4, sizeof(vk_op_rope_neox_push_constants), {1, 512, 1}, {}, 1);
  1272. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_len, rope_neox_f16_data, "main", 4, sizeof(vk_op_rope_neox_push_constants), {1, 512, 1}, {}, 1);
  1273. ggml_vk_create_pipeline(ctx, ctx->device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  1274. }
  1275. static void ggml_vk_print_gpu_info(size_t idx) {
  1276. GGML_ASSERT(idx < vk_instance.device_indices.size());
  1277. size_t dev_num = vk_instance.device_indices[idx];
  1278. #ifdef GGML_VULKAN_DEBUG
  1279. std::cerr << "ggml_vk_print_gpu_info(" << dev_num << ")" << std::endl;
  1280. #endif
  1281. GGML_ASSERT(vk_instance.initialized);
  1282. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  1283. if (dev_num >= devices.size()) {
  1284. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  1285. throw std::runtime_error("Device not found");
  1286. }
  1287. vk::PhysicalDevice physical_device = devices[dev_num];
  1288. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  1289. vk::PhysicalDeviceProperties2 props2;
  1290. vk::PhysicalDeviceMaintenance3Properties props3;
  1291. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  1292. props2.pNext = &props3;
  1293. props3.pNext = &subgroup_props;
  1294. physical_device.getProperties2(&props2);
  1295. const size_t subgroup_size = subgroup_props.subgroupSize;
  1296. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  1297. bool fp16_storage = false;
  1298. bool fp16_compute = false;
  1299. for (auto properties : ext_props) {
  1300. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  1301. fp16_storage = true;
  1302. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  1303. fp16_compute = true;
  1304. }
  1305. }
  1306. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  1307. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  1308. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  1309. vk::PhysicalDeviceFeatures device_features = physical_device.getFeatures();
  1310. VkPhysicalDeviceFeatures2 device_features2;
  1311. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  1312. device_features2.pNext = nullptr;
  1313. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  1314. VkPhysicalDeviceVulkan11Features vk11_features;
  1315. vk11_features.pNext = nullptr;
  1316. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  1317. device_features2.pNext = &vk11_features;
  1318. VkPhysicalDeviceVulkan12Features vk12_features;
  1319. vk12_features.pNext = nullptr;
  1320. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  1321. vk11_features.pNext = &vk12_features;
  1322. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  1323. fp16 = fp16 && vk12_features.shaderFloat16;
  1324. std::string device_name = props2.properties.deviceName.data();
  1325. std::cerr << GGML_VK_NAME << idx << ": " << device_name << " | uma: " << uma << " | fp16: " << fp16 << " | warp size: " << subgroup_size << std::endl;
  1326. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  1327. std::cerr << "ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want." << std::endl;
  1328. }
  1329. }
  1330. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  1331. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  1332. void ggml_vk_instance_init() {
  1333. if (vk_instance_initialized) {
  1334. return;
  1335. }
  1336. #ifdef GGML_VULKAN_DEBUG
  1337. std::cerr << "ggml_vk_instance_init()" << std::endl;
  1338. #endif
  1339. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, VK_API_VERSION };
  1340. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  1341. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  1342. #ifdef __APPLE__
  1343. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  1344. #endif
  1345. std::vector<const char*> layers;
  1346. if (validation_ext) {
  1347. layers.push_back("VK_LAYER_KHRONOS_validation");
  1348. }
  1349. std::vector<const char*> extensions;
  1350. if (validation_ext) {
  1351. extensions.push_back("VK_EXT_validation_features");
  1352. }
  1353. #ifdef __APPLE__
  1354. if (portability_enumeration_ext) {
  1355. extensions.push_back("VK_KHR_portability_enumeration");
  1356. }
  1357. #endif
  1358. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  1359. #ifdef __APPLE__
  1360. if (portability_enumeration_ext) {
  1361. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  1362. }
  1363. #endif
  1364. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  1365. vk::ValidationFeaturesEXT validation_features;
  1366. if (validation_ext) {
  1367. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  1368. validation_features = {
  1369. features_enable,
  1370. {},
  1371. };
  1372. validation_features.setPNext(nullptr);
  1373. instance_create_info.setPNext(&validation_features);
  1374. std::cerr << "ggml_vulkan: Validation layers enabled" << std::endl;
  1375. }
  1376. vk_instance.instance = vk::createInstance(instance_create_info);
  1377. memset(vk_instance.initialized, 0, sizeof(bool) * GGML_VK_MAX_DEVICES);
  1378. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  1379. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  1380. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  1381. if (devices_env != nullptr) {
  1382. std::string devices(devices_env);
  1383. std::replace(devices.begin(), devices.end(), ',', ' ');
  1384. std::stringstream ss(devices);
  1385. size_t tmp;
  1386. while (ss >> tmp) {
  1387. if(tmp >= num_available_devices) {
  1388. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  1389. throw std::runtime_error("Invalid Vulkan device index");
  1390. }
  1391. vk_instance.device_indices.push_back(tmp);
  1392. }
  1393. } else {
  1394. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  1395. // Make sure at least one device exists
  1396. if (devices.empty()) {
  1397. std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
  1398. GGML_ASSERT(false);
  1399. }
  1400. // Default to using all dedicated GPUs
  1401. for (size_t i = 0; i < devices.size(); i++) {
  1402. vk::PhysicalDeviceProperties props = devices[i].getProperties();
  1403. if (props.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  1404. vk_instance.device_indices.push_back(i);
  1405. }
  1406. }
  1407. // If no dedicated GPUs found, fall back to GPU 0
  1408. if (vk_instance.device_indices.empty()) {
  1409. vk_instance.device_indices.push_back(0);
  1410. }
  1411. }
  1412. std::cerr << "ggml_vulkan: Found " << vk_instance.device_indices.size() << " Vulkan devices:" << std::endl;
  1413. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  1414. ggml_vk_print_gpu_info(i);
  1415. }
  1416. vk_instance_initialized = true;
  1417. }
  1418. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  1419. GGML_ASSERT(idx < vk_instance.device_indices.size());
  1420. size_t dev_num = vk_instance.device_indices[idx];
  1421. #ifdef GGML_VULKAN_DEBUG
  1422. std::cerr << "ggml_vk_init(" << ctx->name << ", " << dev_num << ")" << std::endl;
  1423. #endif
  1424. ggml_vk_instance_init();
  1425. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  1426. if (dev_num >= devices.size()) {
  1427. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  1428. throw std::runtime_error("Device not found");
  1429. }
  1430. ctx->device = ggml_vk_get_device(idx);
  1431. if (!ctx->device->initialized) {
  1432. ctx->device->physical_device = devices[dev_num];
  1433. const std::vector<vk::ExtensionProperties> ext_props = ctx->device->physical_device.enumerateDeviceExtensionProperties();
  1434. bool maintenance4_support = false;
  1435. // Check if maintenance4 is supported
  1436. for (const auto& properties : ext_props) {
  1437. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  1438. maintenance4_support = true;
  1439. }
  1440. }
  1441. vk::PhysicalDeviceProperties2 props2;
  1442. vk::PhysicalDeviceMaintenance3Properties props3;
  1443. vk::PhysicalDeviceMaintenance4Properties props4;
  1444. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  1445. props2.pNext = &props3;
  1446. props3.pNext = &subgroup_props;
  1447. if (maintenance4_support) {
  1448. subgroup_props.pNext = &props4;
  1449. }
  1450. ctx->device->physical_device.getProperties2(&props2);
  1451. ctx->device->properties = props2.properties;
  1452. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  1453. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  1454. ctx->device->max_memory_allocation_size = std::stoi(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  1455. } else if (maintenance4_support) {
  1456. ctx->device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  1457. } else {
  1458. ctx->device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  1459. }
  1460. ctx->device->vendor_id = ctx->device->properties.vendorID;
  1461. ctx->device->subgroup_size = subgroup_props.subgroupSize;
  1462. ctx->device->uma = ctx->device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  1463. bool fp16_storage = false;
  1464. bool fp16_compute = false;
  1465. for (const auto& properties : ext_props) {
  1466. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  1467. fp16_storage = true;
  1468. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  1469. fp16_compute = true;
  1470. }
  1471. }
  1472. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  1473. const bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  1474. ctx->device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  1475. std::vector<vk::QueueFamilyProperties> queue_family_props = ctx->device->physical_device.getQueueFamilyProperties();
  1476. // Try to find a non-graphics compute queue and transfer-focused queues
  1477. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  1478. 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);
  1479. const float priorities[] = { 1.0f, 1.0f };
  1480. ctx->device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  1481. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  1482. if (compute_queue_family_index != transfer_queue_family_index) {
  1483. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  1484. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  1485. } else if(!ctx->device->single_queue) {
  1486. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  1487. } else {
  1488. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  1489. }
  1490. vk::DeviceCreateInfo device_create_info;
  1491. std::vector<const char *> device_extensions;
  1492. vk::PhysicalDeviceFeatures device_features = ctx->device->physical_device.getFeatures();
  1493. VkPhysicalDeviceFeatures2 device_features2;
  1494. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  1495. device_features2.pNext = nullptr;
  1496. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  1497. VkPhysicalDeviceVulkan11Features vk11_features;
  1498. vk11_features.pNext = nullptr;
  1499. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  1500. device_features2.pNext = &vk11_features;
  1501. VkPhysicalDeviceVulkan12Features vk12_features;
  1502. vk12_features.pNext = nullptr;
  1503. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  1504. vk11_features.pNext = &vk12_features;
  1505. vkGetPhysicalDeviceFeatures2(ctx->device->physical_device, &device_features2);
  1506. ctx->device->fp16 = ctx->device->fp16 && vk12_features.shaderFloat16;
  1507. if (!vk11_features.storageBuffer16BitAccess) {
  1508. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  1509. throw std::runtime_error("Unsupported device");
  1510. }
  1511. device_extensions.push_back("VK_KHR_16bit_storage");
  1512. #ifdef GGML_VULKAN_VALIDATE
  1513. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  1514. #endif
  1515. if (ctx->device->fp16) {
  1516. device_extensions.push_back("VK_KHR_shader_float16_int8");
  1517. }
  1518. ctx->device->name = ctx->device->properties.deviceName.data();
  1519. device_create_info = {
  1520. vk::DeviceCreateFlags(),
  1521. device_queue_create_infos,
  1522. {},
  1523. device_extensions
  1524. };
  1525. device_create_info.setPNext(&device_features2);
  1526. ctx->device->device = ctx->device->physical_device.createDevice(device_create_info);
  1527. ctx->device->descriptor_set_mode = VK_DEVICE_DESCRIPTOR_POOL_MODE_UNKNOWN;
  1528. // Queues
  1529. ggml_vk_create_queue(ctx, ctx->device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer });
  1530. // Shaders
  1531. ggml_vk_load_shaders(ctx);
  1532. if (!ctx->device->single_queue) {
  1533. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  1534. ggml_vk_create_queue(ctx, ctx->device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer });
  1535. } else {
  1536. // TODO: Use pointer or reference to avoid copy
  1537. ctx->device->transfer_queue = ctx->device->compute_queue;
  1538. }
  1539. ctx->device->idx = dev_num;
  1540. ctx->device->initialized = true;
  1541. } else if (ctx->device->idx != dev_num) {
  1542. std::cerr << "ggml_vulkan: Device " << ctx->device->name << " already initialized with index " << ctx->device->idx << ", but trying to reinitialize with index " << dev_num << std::endl;
  1543. throw std::runtime_error("Device already initialized");
  1544. }
  1545. ctx->fence = ctx->device->device.createFence({});
  1546. ctx->compute_ctx = nullptr;
  1547. ctx->transfer_ctx = nullptr;
  1548. ctx->initialized = true;
  1549. ctx->idx = idx;
  1550. #ifdef GGML_VULKAN_CHECK_RESULTS
  1551. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  1552. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  1553. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  1554. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  1555. #endif
  1556. }
  1557. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  1558. #ifdef GGML_VULKAN_DEBUG
  1559. std::cerr << "ggml_vk_get_to_fp16()" << std::endl;
  1560. #endif
  1561. switch (type) {
  1562. case GGML_TYPE_F32:
  1563. case GGML_TYPE_Q4_0:
  1564. case GGML_TYPE_Q4_1:
  1565. case GGML_TYPE_Q5_0:
  1566. case GGML_TYPE_Q5_1:
  1567. case GGML_TYPE_Q8_0:
  1568. case GGML_TYPE_Q2_K:
  1569. case GGML_TYPE_Q3_K:
  1570. case GGML_TYPE_Q4_K:
  1571. case GGML_TYPE_Q5_K:
  1572. case GGML_TYPE_Q6_K:
  1573. break;
  1574. default:
  1575. return nullptr;
  1576. }
  1577. return ctx->device->pipeline_dequant[type];
  1578. }
  1579. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type) {
  1580. #ifdef GGML_VULKAN_DEBUG
  1581. std::cerr << "ggml_vk_get_mul_mat_mat_pipeline()" << std::endl;
  1582. #endif
  1583. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  1584. return ctx->device->pipeline_matmul_f32;
  1585. }
  1586. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  1587. return ctx->device->pipeline_matmul_f32_f16;
  1588. }
  1589. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  1590. return ctx->device->pipeline_matmul_f16_f32;
  1591. }
  1592. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  1593. return ctx->device->pipeline_matmul_f16;
  1594. }
  1595. GGML_ASSERT(src1_type == GGML_TYPE_F32);
  1596. switch (src0_type) {
  1597. case GGML_TYPE_Q4_0:
  1598. case GGML_TYPE_Q4_1:
  1599. case GGML_TYPE_Q5_0:
  1600. case GGML_TYPE_Q5_1:
  1601. case GGML_TYPE_Q8_0:
  1602. case GGML_TYPE_Q2_K:
  1603. case GGML_TYPE_Q3_K:
  1604. case GGML_TYPE_Q4_K:
  1605. case GGML_TYPE_Q5_K:
  1606. case GGML_TYPE_Q6_K:
  1607. break;
  1608. default:
  1609. return nullptr;
  1610. }
  1611. return ctx->device->pipeline_dequant_mul_mat_mat[src0_type];
  1612. }
  1613. 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) {
  1614. #ifdef GGML_VULKAN_DEBUG
  1615. std::cerr << "ggml_vk_get_mul_mat_mat_id_pipeline()" << std::endl;
  1616. #endif
  1617. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  1618. return ctx->device->pipeline_matmul_id_f32;
  1619. }
  1620. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  1621. return ctx->device->pipeline_matmul_id_f16_f32;
  1622. }
  1623. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  1624. return ctx->device->pipeline_matmul_id_f16;
  1625. }
  1626. GGML_ASSERT(src1_type == GGML_TYPE_F32);
  1627. switch (src0_type) {
  1628. case GGML_TYPE_Q4_0:
  1629. case GGML_TYPE_Q4_1:
  1630. case GGML_TYPE_Q5_0:
  1631. case GGML_TYPE_Q5_1:
  1632. case GGML_TYPE_Q8_0:
  1633. case GGML_TYPE_Q2_K:
  1634. case GGML_TYPE_Q3_K:
  1635. case GGML_TYPE_Q4_K:
  1636. case GGML_TYPE_Q5_K:
  1637. case GGML_TYPE_Q6_K:
  1638. break;
  1639. default:
  1640. return nullptr;
  1641. }
  1642. return ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type];
  1643. }
  1644. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  1645. #ifdef GGML_VULKAN_DEBUG
  1646. std::cerr << "ggml_vk_get_dequantize_mul_mat_vec()" << std::endl;
  1647. #endif
  1648. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  1649. switch (a_type) {
  1650. case GGML_TYPE_F16:
  1651. case GGML_TYPE_Q4_0:
  1652. case GGML_TYPE_Q4_1:
  1653. case GGML_TYPE_Q5_0:
  1654. case GGML_TYPE_Q5_1:
  1655. case GGML_TYPE_Q8_0:
  1656. case GGML_TYPE_Q2_K:
  1657. case GGML_TYPE_Q3_K:
  1658. case GGML_TYPE_Q4_K:
  1659. case GGML_TYPE_Q5_K:
  1660. case GGML_TYPE_Q6_K:
  1661. break;
  1662. default:
  1663. return nullptr;
  1664. }
  1665. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type];
  1666. }
  1667. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  1668. #ifdef GGML_VULKAN_DEBUG
  1669. std::cerr << "ggml_vk_pool_malloc(" << size << ")" << std::endl;
  1670. #endif
  1671. int best_i = -1;
  1672. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  1673. int worst_i = -1;
  1674. size_t worst_size = 0; //largest unused buffer seen so far
  1675. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  1676. vk_buffer &b = ctx->buffer_pool[i];
  1677. if (b != nullptr && b->size >= size && b->size < best_size) {
  1678. best_i = i;
  1679. best_size = b->size;
  1680. }
  1681. if (b != nullptr && b->size > worst_size) {
  1682. worst_i = i;
  1683. worst_size = b->size;
  1684. }
  1685. }
  1686. if(best_i != -1) {
  1687. //found the smallest buffer that fits our needs
  1688. vk_buffer b = ctx->buffer_pool[best_i];
  1689. ctx->buffer_pool[best_i].reset();
  1690. return b;
  1691. }
  1692. if(worst_i != -1) {
  1693. //no buffer that fits our needs, resize largest one to save memory
  1694. vk_buffer& b = ctx->buffer_pool[worst_i];
  1695. ggml_vk_destroy_buffer(b);
  1696. }
  1697. return ggml_vk_create_buffer_check(ctx, size, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1698. }
  1699. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  1700. #ifdef GGML_VULKAN_DEBUG
  1701. std::cerr << "ggml_vk_pool_free(" << buffer->size << ")" << std::endl;
  1702. #endif
  1703. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  1704. vk_buffer& b = ctx->buffer_pool[i];
  1705. if (b == nullptr) {
  1706. b = buffer;
  1707. return;
  1708. }
  1709. }
  1710. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  1711. ggml_vk_destroy_buffer(buffer);
  1712. }
  1713. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  1714. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  1715. // Try to find existing temp buffer with enough capacity
  1716. for (auto& buffer : ctx->gc.temp_buffers) {
  1717. if (buffer->size >= size) {
  1718. return buffer;
  1719. }
  1720. }
  1721. // Otherwise create new buffer
  1722. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  1723. ctx->gc.temp_buffers.push_back(buf);
  1724. return buf;
  1725. }
  1726. static void * ggml_vk_host_malloc(ggml_backend_vk_context * ctx, size_t size) {
  1727. #ifdef GGML_VULKAN_DEBUG
  1728. std::cerr << "ggml_vk_host_malloc(" << size << ")" << std::endl;
  1729. #endif
  1730. vk_buffer buf = ggml_vk_create_buffer(ctx, size,
  1731. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  1732. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1733. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  1734. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  1735. size/1024.0/1024.0);
  1736. ctx->device->device.freeMemory(buf->device_memory);
  1737. ctx->device->device.destroyBuffer(buf->buffer);
  1738. return nullptr;
  1739. }
  1740. ctx->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  1741. return buf->ptr;
  1742. }
  1743. static void ggml_vk_host_free(ggml_backend_vk_context * ctx, void* ptr) {
  1744. if (ptr == nullptr) {
  1745. return;
  1746. }
  1747. #ifdef GGML_VULKAN_DEBUG
  1748. std::cerr << "ggml_vk_host_free(" << ptr << ")" << std::endl;
  1749. #endif
  1750. vk_buffer buf;
  1751. size_t index;
  1752. for (size_t i = 0; i < ctx->pinned_memory.size(); i++) {
  1753. const uint8_t* addr = (const uint8_t*) std::get<0>(ctx->pinned_memory[i]);
  1754. const uint8_t* endr = addr + std::get<1>(ctx->pinned_memory[i]);
  1755. if (ptr >= addr && ptr < endr) {
  1756. buf = std::get<2>(ctx->pinned_memory[i]);
  1757. index = i;
  1758. break;
  1759. }
  1760. }
  1761. if (buf == nullptr) {
  1762. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  1763. return;
  1764. }
  1765. ggml_vk_destroy_buffer(buf);
  1766. ctx->pinned_memory.erase(ctx->pinned_memory.begin() + index);
  1767. }
  1768. static void ggml_vk_host_get(ggml_backend_vk_context * ctx, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  1769. buf = nullptr;
  1770. buf_offset = 0;
  1771. for (size_t i = 0; i < ctx->pinned_memory.size(); i++) {
  1772. const uint8_t* addr = (const uint8_t*) std::get<0>(ctx->pinned_memory[i]);
  1773. const uint8_t* endr = addr + std::get<1>(ctx->pinned_memory[i]);
  1774. if (ptr >= addr && ptr < endr) {
  1775. buf = std::get<2>(ctx->pinned_memory[i]);
  1776. buf_offset = ((const uint8_t *)ptr) - addr;
  1777. break;
  1778. }
  1779. }
  1780. }
  1781. static vk_submission ggml_vk_begin_submission(ggml_backend_vk_context * ctx, vk_queue& q, bool one_time = true) {
  1782. vk_submission s;
  1783. s.buffer = ggml_vk_create_cmd_buffer(ctx, q);
  1784. if (one_time) {
  1785. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  1786. } else {
  1787. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  1788. }
  1789. return s;
  1790. }
  1791. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context * ctx, vk_context * subctx, vk_pipeline& pipeline, std::vector<vk_subbuffer>&& buffers, size_t push_constant_size, const void* push_constants, std::array<uint32_t, 3> elements) {
  1792. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  1793. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  1794. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  1795. #ifdef GGML_VULKAN_DEBUG
  1796. std::cerr << "ggml_vk_dispatch_pipeline(" << pipeline->name << ", (" << wg0 << "," << wg1 << "," << wg2 << "))" << std::endl;
  1797. #endif
  1798. std::vector<vk::DescriptorBufferInfo> descriptor_buffer_infos;
  1799. std::vector<vk::WriteDescriptorSet> write_descriptor_sets;
  1800. GGML_ASSERT(pipeline->descriptor_set_idx < pipeline->descriptor_sets.size());
  1801. GGML_ASSERT(buffers.size() == pipeline->parameter_count);
  1802. vk::DescriptorSet& descriptor_set = pipeline->descriptor_sets[pipeline->descriptor_set_idx++];
  1803. for (uint32_t i = 0; i < pipeline->parameter_count; i++) {
  1804. descriptor_buffer_infos.push_back({buffers[i].buffer->buffer, buffers[i].offset, buffers[i].size});
  1805. }
  1806. for (uint32_t i = 0; i < pipeline->parameter_count; i++) {
  1807. write_descriptor_sets.push_back({descriptor_set, i, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &descriptor_buffer_infos[i]});
  1808. }
  1809. ctx->device->device.updateDescriptorSets(write_descriptor_sets, {});
  1810. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size, push_constants);
  1811. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  1812. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  1813. pipeline->layout,
  1814. 0,
  1815. { descriptor_set },
  1816. {});
  1817. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  1818. }
  1819. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  1820. s.buffer.end();
  1821. s.wait_semaphores = std::move(wait_semaphores);
  1822. s.signal_semaphores = std::move(signal_semaphores);
  1823. }
  1824. static void ggml_vk_ctx_end(vk_context * ctx) {
  1825. #ifdef GGML_VULKAN_DEBUG
  1826. std::cerr << "ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")" << std::endl;
  1827. #endif
  1828. if (ctx->s == nullptr) {
  1829. return;
  1830. }
  1831. ctx->s->buffer.end();
  1832. ctx->s = nullptr;
  1833. }
  1834. static void ggml_vk_ctx_begin(ggml_backend_vk_context * ctx, vk_context * subctx) {
  1835. #ifdef GGML_VULKAN_DEBUG
  1836. std::cerr << "ggml_vk_ctx_begin(" << ctx << ")" << std::endl;
  1837. #endif
  1838. if (subctx->s != nullptr) {
  1839. ggml_vk_ctx_end(subctx);
  1840. }
  1841. subctx->seqs.push_back({ ggml_vk_begin_submission(ctx, *subctx->q) });
  1842. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  1843. }
  1844. static size_t ggml_vk_align_size(size_t width, size_t align) {
  1845. #ifdef GGML_VULKAN_DEBUG
  1846. std::cerr << "ggml_vk_align_size(" << width << ", " << align << ")" << std::endl;
  1847. #endif
  1848. return CEIL_DIV(width, align) * align;
  1849. }
  1850. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  1851. if (memcpys == nullptr) {
  1852. memcpy(dst, src, size);
  1853. } else {
  1854. memcpys->emplace_back(dst, src, size);
  1855. }
  1856. }
  1857. static void ggml_vk_ensure_sync_staging_buffer(ggml_backend_vk_context * ctx, size_t size) {
  1858. if (ctx->sync_staging == nullptr || ctx->sync_staging->size < size) {
  1859. ggml_vk_destroy_buffer(ctx->sync_staging);
  1860. ctx->sync_staging = ggml_vk_create_buffer_check(ctx, size,
  1861. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  1862. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1863. }
  1864. }
  1865. 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) {
  1866. #ifdef GGML_VULKAN_DEBUG
  1867. std::cerr << "ggml_vk_buffer_write_nc_async(" << tensor << ")" << std::endl;
  1868. #endif
  1869. GGML_ASSERT(!ggml_is_contiguous(tensor));
  1870. // Buffer is already mapped
  1871. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1872. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  1873. GGML_ASSERT(false);
  1874. }
  1875. // Check if src is pinned memory
  1876. vk_buffer buf;
  1877. size_t buf_offset;
  1878. ggml_vk_host_get(ctx, tensor->data, buf, buf_offset);
  1879. const uint64_t ne0 = tensor->ne[0];
  1880. const uint64_t ne1 = tensor->ne[1];
  1881. const uint64_t ne2 = tensor->ne[2];
  1882. const uint64_t ne3 = tensor->ne[3];
  1883. const uint64_t nb0 = tensor->nb[0];
  1884. const uint64_t nb1 = tensor->nb[1];
  1885. const uint64_t nb2 = tensor->nb[2];
  1886. const uint64_t nb3 = tensor->nb[3];
  1887. const ggml_type type = tensor->type;
  1888. const uint64_t ts = ggml_type_size(type);
  1889. const uint64_t bs = ggml_blck_size(type);
  1890. const uint64_t dstnb0 = ts;
  1891. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  1892. const uint64_t dstnb2 = dstnb1*ne1;
  1893. const uint64_t dstnb3 = dstnb2*ne2;
  1894. const uint64_t ne = ggml_nelements(tensor);
  1895. if (buf != nullptr) {
  1896. // Memory is pinned, use as staging buffer
  1897. std::vector<vk::BufferCopy> slices;
  1898. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  1899. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  1900. // Find longest contiguous slice
  1901. if (ne1*nb1 == dstnb2) {
  1902. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  1903. } else {
  1904. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  1905. if (ne0*nb0/bs == dstnb1) {
  1906. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  1907. } else {
  1908. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  1909. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  1910. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  1911. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  1912. }
  1913. }
  1914. }
  1915. }
  1916. }
  1917. }
  1918. ggml_vk_sync_buffers(subctx);
  1919. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  1920. return;
  1921. }
  1922. // Staging buffer required
  1923. vk_buffer staging = ctx->staging;
  1924. size_t staging_offset = ctx->staging_offset;
  1925. const size_t copy_size = ts*ne/bs;
  1926. if (ctx->staging->size < ctx->staging_offset + copy_size) {
  1927. if (sync_staging) {
  1928. // Create temporary larger buffer
  1929. ggml_vk_ensure_sync_staging_buffer(ctx, copy_size);
  1930. staging = ctx->sync_staging;
  1931. staging_offset = 0;
  1932. } else {
  1933. GGML_ASSERT(false);
  1934. }
  1935. }
  1936. VkBufferCopy buf_copy{ staging_offset, offset, copy_size };
  1937. ggml_vk_sync_buffers(subctx);
  1938. vkCmdCopyBuffer(subctx->s->buffer, staging->buffer, dst->buffer, 1, &buf_copy);
  1939. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  1940. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  1941. // Find longest contiguous slice
  1942. if (ne1*nb1 == dstnb2) {
  1943. deferred_memcpy((uint8_t *)staging->ptr + staging_offset + i3*dstnb3 + i2*dstnb2, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2, dstnb2, &subctx->in_memcpys);
  1944. } else {
  1945. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  1946. if (ne0*nb0/bs == dstnb1) {
  1947. deferred_memcpy((uint8_t *)staging->ptr + staging_offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2 + i1*nb1, dstnb1, &subctx->in_memcpys);
  1948. } else {
  1949. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  1950. const uint64_t d_off = staging_offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  1951. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  1952. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  1953. }
  1954. }
  1955. }
  1956. }
  1957. }
  1958. }
  1959. }
  1960. static void ggml_vk_buffer_write_2d_async(ggml_backend_vk_context * ctx, 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) {
  1961. #ifdef GGML_VULKAN_DEBUG
  1962. std::cerr << "ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")" << std::endl;
  1963. #endif
  1964. // Make sure ctx owns the buffer
  1965. GGML_ASSERT(dst->ctx == ctx);
  1966. // Buffer is already mapped
  1967. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1968. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  1969. GGML_ASSERT(false);
  1970. }
  1971. // Check if src is pinned memory
  1972. vk_buffer buf = nullptr;
  1973. size_t buf_offset;
  1974. ggml_vk_host_get(ctx, src, buf, buf_offset);
  1975. if (buf != nullptr) {
  1976. // Memory is pinned, use as staging buffer
  1977. std::vector<vk::BufferCopy> slices(1);
  1978. if (width == spitch) {
  1979. // Only do single write if stride is equal
  1980. slices[0].srcOffset = buf_offset;
  1981. slices[0].dstOffset = offset;
  1982. slices[0].size = width * height;
  1983. } else {
  1984. slices.resize(height);
  1985. for (size_t i = 0; i < height; i++) {
  1986. slices[i].srcOffset = buf_offset + i * spitch;
  1987. slices[i].dstOffset = offset + i * width;
  1988. slices[i].size = width;
  1989. }
  1990. }
  1991. ggml_vk_sync_buffers(subctx);
  1992. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  1993. return;
  1994. }
  1995. #ifdef GGML_VULKAN_DEBUG
  1996. std::cerr << "STAGING" << std::endl;
  1997. #endif
  1998. // Staging buffer required
  1999. vk_buffer staging = ctx->staging;
  2000. size_t staging_offset = ctx->staging_offset;
  2001. const size_t copy_size = width*height;
  2002. if (ctx->staging == nullptr || ctx->staging->size < ctx->staging_offset + copy_size) {
  2003. if (sync_staging) {
  2004. ggml_vk_ensure_sync_staging_buffer(ctx, copy_size);
  2005. staging = ctx->sync_staging;
  2006. staging_offset = 0;
  2007. } else {
  2008. GGML_ASSERT(false);
  2009. }
  2010. }
  2011. VkBufferCopy buf_copy = {
  2012. staging_offset,
  2013. offset,
  2014. copy_size};
  2015. ggml_vk_sync_buffers(subctx);
  2016. vkCmdCopyBuffer(subctx->s->buffer, staging->buffer, dst->buffer, 1, &buf_copy);
  2017. if (width == spitch) {
  2018. deferred_memcpy((uint8_t *)staging->ptr + staging_offset, src, width * height, &subctx->in_memcpys);
  2019. } else {
  2020. for (size_t i = 0; i < height; i++) {
  2021. deferred_memcpy((uint8_t *)staging->ptr + staging_offset + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  2022. }
  2023. }
  2024. }
  2025. static void ggml_vk_buffer_write_async(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& dst, size_t offset, const void * src, size_t size, bool sync_staging = false) {
  2026. #ifdef GGML_VULKAN_DEBUG
  2027. std::cerr << "ggml_vk_buffer_write_async(" << size << ")" << std::endl;
  2028. #endif
  2029. return ggml_vk_buffer_write_2d_async(ctx, subctx, dst, offset, src, size, size, 1, sync_staging);
  2030. }
  2031. static void ggml_vk_buffer_write_2d(ggml_backend_vk_context * ctx, vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height) {
  2032. #ifdef GGML_VULKAN_DEBUG
  2033. std::cerr << "ggml_vk_buffer_write_2d(" << width << ", " << height << ")" << std::endl;
  2034. #endif
  2035. // Buffer is already mapped
  2036. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  2037. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  2038. for (size_t i = 0; i < height; i++) {
  2039. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  2040. }
  2041. } else {
  2042. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2043. ggml_vk_ctx_begin(ctx, subctx);
  2044. ggml_vk_buffer_write_2d_async(ctx, subctx, dst, offset, src, spitch, width, height, true);
  2045. ggml_vk_ctx_end(subctx);
  2046. for (auto& cpy : subctx->in_memcpys) {
  2047. memcpy(cpy.dst, cpy.src, cpy.n);
  2048. }
  2049. ggml_vk_submit(subctx, ctx->fence);
  2050. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  2051. ctx->device->device.resetFences({ ctx->fence });
  2052. }
  2053. }
  2054. static void ggml_vk_buffer_write(ggml_backend_vk_context * ctx, vk_buffer& dst, size_t offset, const void * src, size_t size) {
  2055. #ifdef GGML_VULKAN_DEBUG
  2056. std::cerr << "ggml_vk_buffer_write(" << size << ")" << std::endl;
  2057. #endif
  2058. ggml_vk_buffer_write_2d(ctx, dst, offset, src, 0, size, 1);
  2059. }
  2060. static void ggml_vk_buffer_read_2d_async(ggml_backend_vk_context * ctx, 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) {
  2061. #ifdef GGML_VULKAN_DEBUG
  2062. std::cerr << "ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")" << std::endl;
  2063. #endif
  2064. GGML_ASSERT(width > 0);
  2065. GGML_ASSERT(height > 0);
  2066. GGML_ASSERT(src != nullptr);
  2067. // Make sure ctx owns the buffer
  2068. GGML_ASSERT(src->ctx == ctx);
  2069. // Check if dst is pinned memory
  2070. vk_buffer buf = nullptr;
  2071. size_t buf_offset;
  2072. ggml_vk_host_get(ctx, dst, buf, buf_offset);
  2073. std::vector<vk::BufferCopy> slices(1);
  2074. if (width == spitch && width == dpitch) {
  2075. // Only do single write if stride is equal
  2076. slices[0].srcOffset = offset;
  2077. slices[0].dstOffset = buf_offset;
  2078. slices[0].size = width * height;
  2079. } else {
  2080. slices.resize(height);
  2081. for (size_t i = 0; i < height; i++) {
  2082. slices[i].srcOffset = offset + i * spitch;
  2083. slices[i].dstOffset = buf_offset + i * dpitch;
  2084. slices[i].size = width;
  2085. }
  2086. }
  2087. if (buf != nullptr) {
  2088. // Memory is pinned, use as staging buffer
  2089. ggml_vk_sync_buffers(subctx);
  2090. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  2091. return;
  2092. }
  2093. #ifdef GGML_VULKAN_DEBUG
  2094. std::cerr << "STAGING" << std::endl;
  2095. #endif
  2096. // Fall back to staging buffer
  2097. vk_buffer staging = ctx->staging;
  2098. const size_t copy_size = dpitch * height;
  2099. if (ctx->staging == nullptr || ctx->staging->size < ctx->staging_offset + copy_size) {
  2100. if (sync_staging) {
  2101. // Create temporary larger buffer
  2102. ggml_vk_ensure_sync_staging_buffer(ctx, copy_size);
  2103. staging = ctx->sync_staging;
  2104. } else {
  2105. GGML_ASSERT(false);
  2106. }
  2107. }
  2108. ggml_vk_sync_buffers(subctx);
  2109. subctx->s->buffer.copyBuffer(src->buffer, staging->buffer, slices);
  2110. deferred_memcpy(dst, staging->ptr, copy_size, &subctx->out_memcpys);
  2111. }
  2112. static void ggml_vk_buffer_read_async(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& src, size_t offset, void * dst, size_t size, bool sync_staging = false) {
  2113. return ggml_vk_buffer_read_2d_async(ctx, subctx, src, offset, dst, size, size, size, 1, sync_staging);
  2114. }
  2115. static void ggml_vk_buffer_read(ggml_backend_vk_context * ctx, vk_buffer& src, size_t offset, void * dst, size_t size) {
  2116. #ifdef GGML_VULKAN_DEBUG
  2117. std::cerr << "ggml_vk_buffer_read(" << offset << ", " << size << ")" << std::endl;
  2118. #endif
  2119. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  2120. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  2121. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  2122. } else {
  2123. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2124. ggml_vk_ctx_begin(ctx, subctx);
  2125. ggml_vk_buffer_read_async(ctx, subctx, src, offset, dst, size, true);
  2126. ggml_vk_ctx_end(subctx);
  2127. ggml_vk_submit(subctx, ctx->fence);
  2128. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  2129. ctx->device->device.resetFences({ ctx->fence });
  2130. for (auto& cpy : subctx->out_memcpys) {
  2131. memcpy(cpy.dst, cpy.src, cpy.n);
  2132. }
  2133. }
  2134. }
  2135. 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) {
  2136. #ifdef GGML_VULKAN_DEBUG
  2137. std::cerr << "ggml_vk_buffer_copy_async(" << size << ")" << std::endl;
  2138. #endif
  2139. // Make sure both buffers are on same ctx
  2140. GGML_ASSERT(src->ctx == dst->ctx);
  2141. VkBufferCopy bc{ src_offset, dst_offset, size };
  2142. vkCmdCopyBuffer(ctx->s->buffer, src->buffer, dst->buffer, 1, &bc);
  2143. }
  2144. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  2145. if (src->ctx == dst->ctx) {
  2146. #ifdef GGML_VULKAN_DEBUG
  2147. std::cerr << "ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")" << std::endl;
  2148. #endif
  2149. // Copy within the device
  2150. ggml_backend_vk_context * ctx = src->ctx;
  2151. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2152. ggml_vk_ctx_begin(ctx, subctx);
  2153. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  2154. ggml_vk_ctx_end(subctx);
  2155. ggml_vk_submit(subctx, ctx->fence);
  2156. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  2157. ctx->device->device.resetFences({ ctx->fence });
  2158. } else {
  2159. #ifdef GGML_VULKAN_DEBUG
  2160. std::cerr << "ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")" << std::endl;
  2161. #endif
  2162. // Copy device to device
  2163. ggml_backend_vk_context * src_ctx = src->ctx;
  2164. ggml_backend_vk_context * dst_ctx = dst->ctx;
  2165. ggml_vk_ensure_sync_staging_buffer(src_ctx, size);
  2166. ggml_vk_ensure_sync_staging_buffer(dst_ctx, size);
  2167. // Copy to src staging buffer
  2168. ggml_vk_buffer_copy(src_ctx->sync_staging, 0, src, src_offset, size);
  2169. // memcpy to dst staging buffer
  2170. memcpy(dst_ctx->sync_staging->ptr, src_ctx->sync_staging->ptr, size);
  2171. // Copy to dst buffer
  2172. ggml_vk_buffer_copy(dst, dst_offset, dst_ctx->sync_staging, 0, size);
  2173. }
  2174. }
  2175. static void ggml_vk_buffer_memset(ggml_backend_vk_context * ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  2176. #ifdef GGML_VULKAN_DEBUG
  2177. std::cerr << "ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")" << std::endl;
  2178. #endif
  2179. // Make sure ctx owns the buffer
  2180. GGML_ASSERT(dst->ctx == ctx);
  2181. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  2182. ggml_vk_ctx_begin(ctx, subctx);
  2183. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  2184. ggml_vk_ctx_end(subctx);
  2185. ggml_vk_submit(subctx, ctx->fence);
  2186. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  2187. ctx->device->device.resetFences({ ctx->fence });
  2188. }
  2189. static void ggml_vk_h2d_tensor_2d(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& dst, size_t offset, const ggml_tensor * src, uint64_t i3, uint64_t i2, uint64_t i1) {
  2190. #ifdef GGML_VULKAN_DEBUG
  2191. std::cerr << "ggml_vk_h2d_tensor_2d(dst=" << dst << ", offset=" << offset << ", src=" << src << ", i3=" << i3 << ", i2=" << i2 << ", i1=" << i1 << ")" << std::endl;
  2192. #endif
  2193. const uint64_t ne0 = src->ne[0];
  2194. const uint64_t ne1 = src->ne[1];
  2195. const uint64_t nb0 = src->nb[0];
  2196. const uint64_t nb1 = src->nb[1];
  2197. const uint64_t nb2 = src->nb[2];
  2198. const uint64_t nb3 = src->nb[3];
  2199. const enum ggml_type type = src->type;
  2200. const size_t ts = ggml_type_size(type);
  2201. const size_t bs = ggml_blck_size(type);
  2202. const size_t row_length = ts*ne0/bs;
  2203. const void * x = (const void *) ((const char *) src->data + i2*nb2 + i3*nb3);
  2204. if (nb0 == ts && nb1 == row_length) {
  2205. return ggml_vk_buffer_write_async(ctx, subctx, dst, offset, x, i1*nb1);
  2206. }
  2207. if (nb0 == ts && (i1 == ne1 || !ggml_is_permuted(src))) {
  2208. return ggml_vk_buffer_write_2d_async(ctx, subctx, dst, offset, x, nb1, row_length, i1);
  2209. }
  2210. GGML_ASSERT(i3 == 0);
  2211. GGML_ASSERT(i2 == 0);
  2212. GGML_ASSERT(i1 == (uint64_t) ggml_nrows(src));
  2213. return ggml_vk_buffer_write_nc_async(ctx, subctx, dst, offset, src);
  2214. }
  2215. static void ggml_vk_d2h_tensor_2d(ggml_backend_vk_context * ctx, vk_context * subctx, vk_buffer& src, size_t offset, const ggml_tensor * dst) {
  2216. #ifdef GGML_VULKAN_DEBUG
  2217. std::cerr << "ggml_vk_d2h_tensor_2d()" << std::endl;
  2218. #endif
  2219. const uint64_t ne0 = dst->ne[0];
  2220. const uint64_t ne1 = dst->ne[1];
  2221. const uint64_t ne2 = dst->ne[2];
  2222. const uint64_t ne3 = dst->ne[3];
  2223. const uint64_t nb0 = dst->nb[0];
  2224. const uint64_t nb1 = dst->nb[1];
  2225. // const uint64_t nb2 = dst->nb[2];
  2226. // const uint64_t nb3 = dst->nb[3];
  2227. const enum ggml_type type = dst->type;
  2228. const size_t ts = ggml_type_size(type);
  2229. const size_t bs = ggml_blck_size(type);
  2230. const size_t row_length = ts*ne0/bs;
  2231. if (ggml_is_contiguous(dst)) {
  2232. return ggml_vk_buffer_read_async(ctx, subctx, src, offset, dst->data, ne1*nb1*ne2*ne3);
  2233. }
  2234. if (nb0 == ts) {
  2235. return ggml_vk_buffer_read_2d_async(ctx, subctx, src, offset, dst->data, nb1, nb1, row_length, ne1*ne2*ne3);
  2236. }
  2237. GGML_ASSERT(false);
  2238. }
  2239. static uint32_t ggml_vk_guess_split_k(int m, int n, int k) {
  2240. #ifdef GGML_VULKAN_DEBUG
  2241. std::cerr << "ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")" << std::endl;
  2242. #endif
  2243. // if (k > 128 && (m < 128 || n < 128) && m > 2 && n > 2) {
  2244. // return 4;
  2245. // }
  2246. return 1;
  2247. GGML_UNUSED(m); GGML_UNUSED(n); GGML_UNUSED(k);
  2248. }
  2249. static vk_pipeline ggml_vk_guess_matmul_pipeline_amd(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned) {
  2250. if (m <= 32 || n <= 32) {
  2251. return aligned ? mmp->a_s : mmp->s;
  2252. }
  2253. return aligned ? mmp->a_m : mmp->m;
  2254. GGML_UNUSED(ctx);
  2255. }
  2256. static vk_pipeline ggml_vk_guess_matmul_pipeline_apple(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, bool aligned) {
  2257. return aligned ? mmp->a_m : mmp->m;
  2258. GGML_UNUSED(ctx);
  2259. }
  2260. static vk_pipeline ggml_vk_guess_matmul_pipeline_intel(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, bool aligned) {
  2261. return aligned ? mmp->a_s : mmp->s;
  2262. GGML_UNUSED(ctx);
  2263. }
  2264. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, bool aligned) {
  2265. #ifdef GGML_VULKAN_DEBUG
  2266. std::cerr << "ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ")" << std::endl;
  2267. #endif
  2268. switch (ctx->device->vendor_id) {
  2269. case VK_VENDOR_ID_AMD:
  2270. return ggml_vk_guess_matmul_pipeline_amd(ctx, mmp, m, n, aligned);
  2271. case VK_VENDOR_ID_APPLE:
  2272. return ggml_vk_guess_matmul_pipeline_apple(ctx, mmp, aligned);
  2273. case VK_VENDOR_ID_INTEL:
  2274. return ggml_vk_guess_matmul_pipeline_intel(ctx, mmp, aligned);
  2275. default:
  2276. break;
  2277. }
  2278. if (m <= 32 || n <= 32) {
  2279. return aligned ? mmp->a_s : mmp->s;
  2280. }
  2281. if (m <= 64 || n <= 64) {
  2282. return aligned ? mmp->a_m : mmp->m;
  2283. }
  2284. return aligned ? mmp->a_l : mmp->l;
  2285. }
  2286. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n) {
  2287. #ifdef GGML_VULKAN_DEBUG
  2288. std::cerr << "ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ")" << std::endl;
  2289. #endif
  2290. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true)->align;
  2291. }
  2292. static void ggml_vk_matmul(
  2293. ggml_backend_vk_context * ctx, vk_context * subctx, vk_pipeline& pipeline,
  2294. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  2295. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  2296. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  2297. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  2298. uint32_t expert_stride_b, uint32_t expert_stride_d, uint32_t idx, uint32_t nbi1, uint32_t n_as) {
  2299. #ifdef GGML_VULKAN_DEBUG
  2300. std::cerr << "ggml_vk_matmul(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), c: (" << 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 << ", split_k: " << split_k << ", batch: " << batch << ", ne02: " << ne02 << ", ne12: " << ne12 << ", broadcast2: " << broadcast2 << ", broadcast3: " << broadcast3 << ", batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ")" << std::endl;
  2301. #endif
  2302. ggml_vk_sync_buffers(subctx);
  2303. if (split_k == 1) {
  2304. const vk_mat_mat_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, k, ne02, ne12, broadcast2, broadcast3, batch_stride_a, batch_stride_b, batch_stride_d, expert_stride_b, expert_stride_d, idx, nbi1, n_as };
  2305. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  2306. return;
  2307. }
  2308. GGML_ASSERT(batch_stride_d == m * n);
  2309. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, batch_stride_a, batch_stride_b, batch_stride_d, expert_stride_b, expert_stride_d, idx, nbi1, n_as };
  2310. // Make sure enough workgroups get assigned for split k to work
  2311. 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 });
  2312. ggml_vk_sync_buffers(subctx);
  2313. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  2314. 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 });
  2315. }
  2316. static void ggml_vk_matmul_id(
  2317. ggml_backend_vk_context * ctx, vk_context * subctx, vk_pipeline& pipeline,
  2318. vk_subbuffer&& ids, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& a, vk_subbuffer&& split_k_buffer,
  2319. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  2320. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  2321. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  2322. uint32_t expert_stride_b, uint32_t expert_stride_d, uint32_t idx, uint32_t nbi1, uint32_t n_as) {
  2323. #ifdef GGML_VULKAN_DEBUG
  2324. std::cerr << "ggml_vk_matmul(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), c: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), split_k: (" << split_k_buffer.buffer->buffer << ", " << 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 << ", split_k: " << split_k << ", batch: " << batch << ", ne02: " << ne02 << ", ne12: " << ne12 << ", broadcast2: " << broadcast2 << ", broadcast3: " << broadcast3 << ", batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ")" << std::endl;
  2325. #endif
  2326. ggml_vk_sync_buffers(subctx);
  2327. if (split_k == 1) {
  2328. const vk_mat_mat_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, k, ne02, ne12, broadcast2, broadcast3, batch_stride_a, batch_stride_b, batch_stride_d, expert_stride_b, expert_stride_d, idx, nbi1, n_as };
  2329. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { ids, b, d, a }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  2330. return;
  2331. }
  2332. GGML_ASSERT(batch_stride_d == m * n);
  2333. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, batch_stride_a, batch_stride_b, batch_stride_d, expert_stride_b, expert_stride_d, idx, nbi1, n_as };
  2334. // Make sure enough workgroups get assigned for split k to work
  2335. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { ids, b, split_k_buffer, a }, sizeof(vk_mat_mat_push_constants), &pc1, { (CEIL_DIV(m, pipeline->wg_denoms[0]) * pipeline->wg_denoms[0]) * split_k, n, batch });
  2336. ggml_vk_sync_buffers(subctx);
  2337. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  2338. 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 });
  2339. }
  2340. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  2341. return
  2342. tensor->nb[0] == ggml_type_size(tensor->type) &&
  2343. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  2344. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  2345. }
  2346. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, ggml_type from, ggml_type to) {
  2347. if (from == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  2348. return ctx->device->pipeline_cpy_f32_f32;
  2349. }
  2350. if (from == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  2351. return ctx->device->pipeline_cpy_f32_f16;
  2352. }
  2353. if (from == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  2354. return ctx->device->pipeline_cpy_f16_f16;
  2355. }
  2356. std::cerr << "Missing CPY op for types: " << ggml_type_name(from) << " " << ggml_type_name(to) << std::endl;
  2357. GGML_ASSERT(false);
  2358. }
  2359. 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) {
  2360. #ifdef GGML_VULKAN_DEBUG
  2361. std::cerr << "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] << "), ";
  2362. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")" << std::endl;
  2363. #endif
  2364. const int tensor_type_size = ggml_type_size(tensor->type);
  2365. const uint32_t ne = ggml_nelements(tensor);
  2366. const vk_op_unary_push_constants pc = {
  2367. (uint32_t)ne,
  2368. (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,
  2369. (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]),
  2370. 0,
  2371. 0.0f, 0.0f,
  2372. };
  2373. ggml_vk_sync_buffers(subctx);
  2374. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(vk_op_unary_push_constants), &pc, { ne, 1, 1 });
  2375. }
  2376. 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) {
  2377. #ifdef GGML_VULKAN_DEBUG
  2378. std::cerr << "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];
  2379. 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];
  2380. 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] << "),)" << std::endl;
  2381. #endif
  2382. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  2383. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2384. const uint64_t ne00 = src0->ne[0];
  2385. const uint64_t ne01 = src0->ne[1];
  2386. const uint64_t ne02 = src0->ne[2];
  2387. const uint64_t ne03 = src0->ne[3];
  2388. const uint64_t ne10 = src1->ne[0];
  2389. const uint64_t ne11 = src1->ne[1];
  2390. const uint64_t ne12 = src1->ne[2];
  2391. const uint64_t ne13 = src1->ne[3];
  2392. const uint64_t ne20 = dst->ne[0];
  2393. const uint64_t ne21 = dst->ne[1];
  2394. const uint64_t r2 = ne12 / ne02;
  2395. const uint64_t r3 = ne13 / ne03;
  2396. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2397. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2398. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2399. vk_buffer d_Qx;
  2400. size_t qx_buf_offset = 0;
  2401. vk_buffer d_Qy;
  2402. size_t qy_buf_offset = 0;
  2403. bool src0_uma = false;
  2404. bool src1_uma = false;
  2405. if (ctx->device->uma) {
  2406. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2407. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2408. src0_uma = d_Qx != nullptr;
  2409. src1_uma = d_Qy != nullptr;
  2410. }
  2411. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2412. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2413. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  2414. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type);
  2415. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  2416. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  2417. if (mmp == nullptr) {
  2418. // Fall back to dequant + f16 mulmat
  2419. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16);
  2420. }
  2421. // Not implemented
  2422. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2423. const int x_ne = ne01 * ne00;
  2424. const int y_ne = ne11 * ne10;
  2425. const int d_ne = ne11 * ne01;
  2426. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11));
  2427. const bool aligned = ne10 == kpad && ne01 > 8 && ne11 > 8;
  2428. const uint32_t split_k = ggml_vk_guess_split_k(ne01, ne11, ne10);
  2429. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned);
  2430. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  2431. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2432. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  2433. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2434. const uint64_t d_sz = sizeof(float) * d_ne;
  2435. vk_buffer d_D = extra->buffer_gpu.lock();
  2436. const uint64_t d_buf_offset = extra->offset;
  2437. GGML_ASSERT(d_D != nullptr);
  2438. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  2439. vk_buffer d_X;
  2440. uint64_t x_buf_offset = 0;
  2441. vk_buffer d_Y;
  2442. uint64_t y_buf_offset = 0;
  2443. if (!src0_uma) {
  2444. d_Qx = extra_src0->buffer_gpu.lock();
  2445. qx_buf_offset = extra_src0->offset;
  2446. GGML_ASSERT(d_Qx != nullptr);
  2447. }
  2448. if (!src1_uma) {
  2449. d_Qy = extra_src1->buffer_gpu.lock();
  2450. qy_buf_offset = extra_src1->offset;
  2451. GGML_ASSERT(d_Qy != nullptr);
  2452. }
  2453. if (qx_needs_dequant) {
  2454. d_X = ctx->prealloc_x;
  2455. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  2456. } else {
  2457. d_X = d_Qx;
  2458. x_buf_offset = qx_buf_offset;
  2459. GGML_ASSERT(qx_sz == x_sz);
  2460. }
  2461. if (qy_needs_dequant) {
  2462. d_Y = ctx->prealloc_y;
  2463. GGML_ASSERT(d_Y->size >= y_sz * ne02 * ne03);
  2464. } else {
  2465. d_Y = d_Qy;
  2466. y_buf_offset = qy_buf_offset;
  2467. GGML_ASSERT(qy_sz == y_sz);
  2468. }
  2469. vk_pipeline to_fp16_vk_0 = nullptr;
  2470. vk_pipeline to_fp16_vk_1 = nullptr;
  2471. if (x_non_contig) {
  2472. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, GGML_TYPE_F16);
  2473. } else {
  2474. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  2475. }
  2476. if (y_non_contig) {
  2477. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, GGML_TYPE_F16);
  2478. } else {
  2479. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  2480. }
  2481. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  2482. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  2483. // Allocate descriptor sets
  2484. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, 1);
  2485. if (qx_needs_dequant) {
  2486. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  2487. }
  2488. if (qy_needs_dequant) {
  2489. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, 1);
  2490. }
  2491. if (split_k > 1) {
  2492. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  2493. }
  2494. if (x_non_contig) {
  2495. 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 });
  2496. } else if (qx_needs_dequant) {
  2497. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  2498. ggml_vk_sync_buffers(subctx);
  2499. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { { d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, { d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  2500. }
  2501. if (y_non_contig) {
  2502. 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 });
  2503. }
  2504. uint32_t stride_batch_x = ne00*ne01;
  2505. uint32_t stride_batch_y = ne10*ne11;
  2506. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  2507. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  2508. }
  2509. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  2510. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  2511. }
  2512. // compute
  2513. ggml_vk_matmul(
  2514. ctx, subctx, pipeline,
  2515. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  2516. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  2517. ne01, ne11, ne10, ne10, ne10, ne01, split_k, ne12*ne13, ne02, ne12, r2, r3, stride_batch_x, stride_batch_y, ne20*ne21,
  2518. 0, 0, 0, 0, 1
  2519. ); // NOLINT
  2520. }
  2521. 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) {
  2522. #ifdef GGML_VULKAN_DEBUG
  2523. std::cerr << "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];
  2524. 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];
  2525. 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] << "),)" << std::endl;
  2526. #endif
  2527. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  2528. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2529. const uint64_t ne00 = src0->ne[0];
  2530. const uint64_t ne01 = src0->ne[1];
  2531. const uint64_t ne02 = src0->ne[2];
  2532. const uint64_t ne03 = src0->ne[3];
  2533. const uint64_t ne10 = src1->ne[0];
  2534. const uint64_t ne11 = src1->ne[1];
  2535. const uint64_t ne12 = src1->ne[2];
  2536. const uint64_t ne13 = src1->ne[3];
  2537. GGML_ASSERT(ne11 == 1);
  2538. const uint64_t ne20 = dst->ne[0];
  2539. const uint64_t ne21 = dst->ne[1];
  2540. const uint64_t ne22 = dst->ne[2];
  2541. const uint64_t ne23 = dst->ne[3];
  2542. const uint64_t r2 = ne12 / ne02;
  2543. const uint64_t r3 = ne13 / ne03;
  2544. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2545. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2546. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2547. vk_buffer d_Qx;
  2548. size_t qx_buf_offset = 0;
  2549. vk_buffer d_Qy;
  2550. size_t qy_buf_offset = 0;
  2551. bool src0_uma = false;
  2552. bool src1_uma = false;
  2553. if (ctx->device->uma) {
  2554. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2555. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2556. src0_uma = d_Qx != nullptr;
  2557. src1_uma = d_Qy != nullptr;
  2558. }
  2559. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2560. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2561. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  2562. const bool qx_needs_dequant = x_non_contig;
  2563. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  2564. // Not implemented
  2565. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2566. const uint64_t x_ne = ne01 * ne00;
  2567. const uint64_t y_ne = ne11 * ne10;
  2568. const uint64_t d_ne = ne11 * ne01;
  2569. 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);
  2570. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2571. 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;
  2572. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2573. const uint64_t d_sz = sizeof(float) * d_ne;
  2574. vk_buffer d_D = extra->buffer_gpu.lock();
  2575. const uint64_t d_buf_offset = extra->offset;
  2576. GGML_ASSERT(d_D != nullptr);
  2577. vk_buffer d_X;
  2578. uint64_t x_buf_offset = 0;
  2579. vk_buffer d_Y;
  2580. uint64_t y_buf_offset = 0;
  2581. if(!src0_uma) {
  2582. d_Qx = extra_src0->buffer_gpu.lock();
  2583. qx_buf_offset = extra_src0->offset;
  2584. GGML_ASSERT(d_Qx != nullptr);
  2585. }
  2586. if(!src1_uma) {
  2587. d_Qy = extra_src1->buffer_gpu.lock();
  2588. qy_buf_offset = extra_src1->offset;
  2589. GGML_ASSERT(d_Qy != nullptr);
  2590. }
  2591. if (qx_needs_dequant) {
  2592. d_X = ctx->prealloc_x;
  2593. } else {
  2594. d_X = d_Qx;
  2595. x_buf_offset = qx_buf_offset;
  2596. GGML_ASSERT(qx_sz == x_sz);
  2597. }
  2598. if (qy_needs_dequant) {
  2599. d_Y = ctx->prealloc_y;
  2600. } else {
  2601. d_Y = d_Qy;
  2602. y_buf_offset = qy_buf_offset;
  2603. GGML_ASSERT(qy_sz == y_sz);
  2604. }
  2605. vk_pipeline to_fp16_vk_0 = nullptr;
  2606. vk_pipeline to_fp16_vk_1 = nullptr;
  2607. if (x_non_contig) {
  2608. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, src0->type);
  2609. }
  2610. if (y_non_contig) {
  2611. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, src1->type);
  2612. } else {
  2613. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  2614. }
  2615. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type);
  2616. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  2617. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  2618. GGML_ASSERT(dmmv != nullptr);
  2619. // Allocate descriptor sets
  2620. if (qx_needs_dequant) {
  2621. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  2622. }
  2623. if (qy_needs_dequant) {
  2624. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, y_non_contig ? 1 : ne12 * ne13);
  2625. }
  2626. ggml_pipeline_allocate_descriptor_sets(ctx, dmmv, ne12 * ne13);
  2627. if (x_non_contig) {
  2628. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  2629. 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 });
  2630. }
  2631. if (y_non_contig) {
  2632. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  2633. 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 });
  2634. }
  2635. uint32_t stride_batch_x = ne00*ne01;
  2636. uint32_t stride_batch_y = ne10*ne11;
  2637. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  2638. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  2639. }
  2640. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  2641. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  2642. }
  2643. // compute
  2644. const vk_mat_vec_push_constants pc = {
  2645. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  2646. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  2647. stride_batch_x, stride_batch_y, (uint32_t)(ne20*ne21),
  2648. };
  2649. ggml_vk_sync_buffers(subctx);
  2650. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv, { { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 }, { d_D, d_buf_offset, d_sz * ne22 * ne23} }, sizeof(vk_mat_vec_push_constants), &pc, { (uint32_t)ne01, (uint32_t)(ne12 * ne13), 1});
  2651. }
  2652. 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) {
  2653. #ifdef GGML_VULKAN_DEBUG
  2654. std::cerr << "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];
  2655. 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];
  2656. 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] << "),)" << std::endl;
  2657. #endif
  2658. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  2659. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  2660. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  2661. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2662. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2663. const uint64_t ne00 = src0->ne[0];
  2664. const uint64_t ne01 = src0->ne[1];
  2665. const uint64_t ne02 = src0->ne[2];
  2666. // const uint64_t ne03 = src0->ne[3];
  2667. const uint64_t ne10 = src1->ne[0];
  2668. const uint64_t ne11 = src1->ne[1];
  2669. const uint64_t ne12 = src1->ne[2];
  2670. // const uint64_t ne13 = src1->ne[3];
  2671. GGML_ASSERT(ne11 == 1);
  2672. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2673. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2674. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2675. vk_buffer d_Qy;
  2676. size_t qy_buf_offset = 0;
  2677. bool src1_uma = false;
  2678. if (ctx->device->uma) {
  2679. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2680. src1_uma = d_Qy != nullptr;
  2681. }
  2682. const uint64_t x_ne = ne00 * ne01 * ne02;
  2683. const uint64_t y_ne = ne10 * ne11 * ne12;
  2684. const uint64_t d_ne = ne01 * ne11 * ne12;
  2685. 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);
  2686. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2687. const uint64_t d_sz = sizeof(float) * d_ne;
  2688. vk_buffer d_D = extra->buffer_gpu.lock();
  2689. const uint64_t d_buf_offset = extra->offset;
  2690. GGML_ASSERT(d_D != nullptr);
  2691. vk_buffer d_Qx = extra_src0->buffer_gpu.lock();
  2692. const uint64_t qx_buf_offset = extra_src0->offset;
  2693. GGML_ASSERT(d_Qx != nullptr);
  2694. if (!src1_uma) {
  2695. d_Qy = extra_src1->buffer_gpu.lock();
  2696. qy_buf_offset = extra_src1->offset;
  2697. GGML_ASSERT(d_Qx != nullptr);
  2698. }
  2699. // Allocate descriptor sets
  2700. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, 1);
  2701. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2702. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  2703. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2704. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  2705. // compute
  2706. 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)) };
  2707. ggml_vk_sync_buffers(subctx);
  2708. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32, { { d_Qx, qx_buf_offset, qx_sz }, { d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, { d_D, d_buffer_offset, d_sz + d_shader_offset } }, 6 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  2709. }
  2710. 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) {
  2711. #ifdef GGML_VULKAN_DEBUG
  2712. std::cerr << "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];
  2713. 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];
  2714. 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] << "),)" << std::endl;
  2715. #endif
  2716. GGML_ASSERT(!ggml_is_transposed(src0));
  2717. GGML_ASSERT(!ggml_is_transposed(src1));
  2718. GGML_ASSERT(!ggml_is_permuted(src0));
  2719. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  2720. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2721. const uint64_t ne00 = src0->ne[0];
  2722. const uint64_t ne01 = src0->ne[1];
  2723. const uint64_t ne02 = src0->ne[2];
  2724. // const uint64_t ne03 = src0->ne[3];
  2725. const uint64_t nb01 = src0->nb[1];
  2726. const uint64_t nb02 = src0->nb[2];
  2727. // const uint64_t ne10 = src1->ne[0];
  2728. const uint64_t ne11 = src1->ne[1];
  2729. const uint64_t ne12 = src1->ne[2];
  2730. // const uint64_t ne13 = src1->ne[3];
  2731. GGML_ASSERT(ne11 == 1);
  2732. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2733. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2734. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2735. vk_buffer d_Qy = nullptr;
  2736. size_t qy_buf_offset = 0;
  2737. bool src1_uma = false;
  2738. if (ctx->device->uma) {
  2739. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2740. src1_uma = d_Qy != nullptr;
  2741. }
  2742. const uint64_t d_ne = ne01 * ne11 * ne12;
  2743. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  2744. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  2745. const uint64_t qx_sz = ggml_nbytes(src0);
  2746. const uint64_t qy_sz = ggml_nbytes(src1);
  2747. const uint64_t d_sz = sizeof(float) * d_ne;
  2748. vk_buffer d_D = extra->buffer_gpu.lock();
  2749. const uint64_t d_buf_offset = extra->offset;
  2750. GGML_ASSERT(d_D != nullptr);
  2751. vk_buffer d_Qx = extra_src0->buffer_gpu.lock();
  2752. const uint64_t qx_buf_offset = extra_src0->offset;
  2753. GGML_ASSERT(d_Qx != nullptr);
  2754. if (!src1_uma) {
  2755. d_Qy = extra_src1->buffer_gpu.lock();
  2756. qy_buf_offset = extra_src1->offset;
  2757. GGML_ASSERT(d_Qx != nullptr);
  2758. }
  2759. // Allocate descriptor sets
  2760. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  2761. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2762. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  2763. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  2764. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  2765. // compute
  2766. 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)) };
  2767. ggml_vk_sync_buffers(subctx);
  2768. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, { { d_Qx, qx_buf_offset, qx_sz }, { d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, { d_D, d_buffer_offset, d_sz + d_shader_offset } }, 7 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  2769. }
  2770. 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) {
  2771. #ifdef GGML_VULKAN_DEBUG
  2772. std::cerr << "ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")" << std::endl;
  2773. #endif
  2774. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  2775. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst);
  2776. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  2777. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst);
  2778. } else if (src1->ne[1] == 1 && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  2779. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst);
  2780. } else {
  2781. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst);
  2782. }
  2783. }
  2784. /*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) {
  2785. #ifdef GGML_VULKAN_DEBUG
  2786. std::cerr << "ggml_vk_mul_mat_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", backend=" << src0->backend << ", 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];
  2787. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", backend=" << src1->backend << ", 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];
  2788. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", backend=" << ids->backend << ", 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];
  2789. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", backend=" << dst->backend << ", 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] << "),)" << std::endl;
  2790. #endif
  2791. GGML_ASSERT(src0->type == GGML_TYPE_I32);
  2792. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2793. const uint64_t ne00 = src0->ne[0];
  2794. const uint64_t ne01 = src0->ne[1];
  2795. const uint64_t ne02 = src0->ne[2];
  2796. const uint64_t ne03 = src0->ne[3];
  2797. const uint64_t ne10 = src1->ne[0];
  2798. const uint64_t ne11 = src1->ne[1];
  2799. const uint64_t ne12 = src1->ne[2];
  2800. const uint64_t ne13 = src1->ne[3];
  2801. const uint32_t nb11 = src1->nb[1];
  2802. const uint64_t ne20 = dst->ne[0];
  2803. const uint64_t ne21 = dst->ne[1];
  2804. const uint64_t r2 = ne12 / ne02;
  2805. const uint64_t r3 = ne13 / ne03;
  2806. const uint32_t nbi1 = src0->nb[1];
  2807. const uint32_t idx = ((uint32_t *) dst->op_params)[0];
  2808. const uint64_t n_as = ne02;
  2809. GGML_ASSERT(n_as <= 8);
  2810. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2811. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2812. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2813. ggml_tensor_extra_gpu * extra_ids = (ggml_tensor_extra_gpu *) ids->extra;
  2814. vk_buffer d_Qx;
  2815. size_t qx_buf_offset = 0;
  2816. vk_buffer d_Qy;
  2817. size_t qy_buf_offset = 0;
  2818. bool src0_uma = false;
  2819. bool src1_uma = false;
  2820. if (ctx->device->uma) {
  2821. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2822. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2823. src0_uma = d_Qx != nullptr;
  2824. src1_uma = d_Qy != nullptr;
  2825. }
  2826. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2827. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2828. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  2829. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type);
  2830. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  2831. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  2832. if (mmp == nullptr) {
  2833. GGML_ASSERT(false);
  2834. }
  2835. // Not implemented
  2836. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2837. const int x_ne = ne01 * ne00;
  2838. const int y_ne = ne11 * ne10;
  2839. const int d_ne = ne11 * ne01;
  2840. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11));
  2841. const bool aligned = ne10 == kpad && ne01 > 8 && ne11 > 8;
  2842. const uint32_t split_k = ggml_vk_guess_split_k(ne01, ne11, ne10);
  2843. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned);
  2844. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  2845. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2846. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  2847. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2848. const uint64_t d_sz = sizeof(float) * d_ne;
  2849. vk_buffer d_D = extra->buffer_gpu.lock();
  2850. const uint64_t d_buf_offset = extra->offset;
  2851. GGML_ASSERT(d_D != nullptr);
  2852. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  2853. vk_buffer d_X;
  2854. uint64_t x_buf_offset = 0;
  2855. vk_buffer d_Y;
  2856. uint64_t y_buf_offset = 0;
  2857. if (!src0_uma) {
  2858. d_Qx = extra_src0->buffer_gpu.lock();
  2859. qx_buf_offset = extra_src0->offset;
  2860. GGML_ASSERT(d_Qx != nullptr);
  2861. }
  2862. if (!src1_uma) {
  2863. d_Qy = extra_src1->buffer_gpu.lock();
  2864. qy_buf_offset = extra_src1->offset;
  2865. GGML_ASSERT(d_Qy != nullptr);
  2866. }
  2867. if (qx_needs_dequant) {
  2868. d_X = ctx->prealloc_x;
  2869. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  2870. } else {
  2871. d_X = d_Qx;
  2872. x_buf_offset = qx_buf_offset;
  2873. GGML_ASSERT(qx_sz == x_sz);
  2874. }
  2875. if (qy_needs_dequant) {
  2876. d_Y = ctx->prealloc_y;
  2877. GGML_ASSERT(d_Y->size >= y_sz * ne02 * ne03);
  2878. } else {
  2879. d_Y = d_Qy;
  2880. y_buf_offset = qy_buf_offset;
  2881. GGML_ASSERT(qy_sz == y_sz);
  2882. }
  2883. vk_pipeline to_fp16_vk_0 = nullptr;
  2884. vk_pipeline to_fp16_vk_1 = nullptr;
  2885. if (x_non_contig) {
  2886. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, GGML_TYPE_F16);
  2887. } else {
  2888. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  2889. }
  2890. if (y_non_contig) {
  2891. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, GGML_TYPE_F16);
  2892. } else {
  2893. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  2894. }
  2895. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  2896. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  2897. // Allocate descriptor sets
  2898. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, 1);
  2899. if (qx_needs_dequant) {
  2900. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  2901. }
  2902. if (qy_needs_dequant) {
  2903. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, 1);
  2904. }
  2905. if (split_k > 1) {
  2906. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, 1);
  2907. }
  2908. if (x_non_contig) {
  2909. 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 });
  2910. } else if (qx_needs_dequant) {
  2911. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  2912. ggml_vk_sync_buffers(subctx);
  2913. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { { d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, { d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  2914. }
  2915. if (y_non_contig) {
  2916. 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 });
  2917. }
  2918. uint32_t stride_batch_x = ne00*ne01;
  2919. uint32_t stride_batch_y = ne10*ne11;
  2920. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  2921. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  2922. }
  2923. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  2924. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  2925. }
  2926. // compute
  2927. ggml_vk_matmul(
  2928. ctx, subctx, pipeline,
  2929. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  2930. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  2931. ne01, ne11, ne10, ne10, ne10, ne01, split_k, ne12*ne13, ne02, ne12, r2, r3, stride_batch_x, stride_batch_y, ne20*ne21,
  2932. nb11 / ggml_type_size(src1->type), ne20, idx, nbi1, n_as
  2933. ); // NOLINT
  2934. }
  2935. 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, ggml_tensor * dst) {
  2936. #ifdef GGML_VULKAN_DEBUG
  2937. std::cerr << "ggml_vk_mul_mat_vec_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", backend=" << src0->backend << ", 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];
  2938. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", backend=" << src1->backend << ", 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];
  2939. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", backend=" << dst->backend << ", 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] << "),)" << std::endl;
  2940. #endif
  2941. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  2942. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  2943. const uint64_t ne00 = src0->ne[0];
  2944. const uint64_t ne01 = src0->ne[1];
  2945. const uint64_t ne02 = src0->ne[2];
  2946. const uint64_t ne03 = src0->ne[3];
  2947. const uint64_t ne10 = src1->ne[0];
  2948. const uint64_t ne11 = src1->ne[1];
  2949. const uint64_t ne12 = src1->ne[2];
  2950. const uint64_t ne13 = src1->ne[3];
  2951. GGML_ASSERT(ne11 == 1);
  2952. const uint64_t ne20 = dst->ne[0];
  2953. const uint64_t ne21 = dst->ne[1];
  2954. const uint64_t ne22 = dst->ne[2];
  2955. const uint64_t ne23 = dst->ne[3];
  2956. const uint64_t nb22 = dst->nb[2];
  2957. const uint64_t nb23 = dst->nb[3];
  2958. const uint64_t r2 = ne12 / ne02;
  2959. const uint64_t r3 = ne13 / ne03;
  2960. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  2961. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  2962. ggml_tensor_extra_gpu * extra_src1 = (ggml_tensor_extra_gpu *) src1->extra;
  2963. vk_buffer d_Qx;
  2964. size_t qx_buf_offset = 0;
  2965. vk_buffer d_Qy;
  2966. size_t qy_buf_offset = 0;
  2967. bool src0_uma = false;
  2968. bool src1_uma = false;
  2969. if (ctx->device->uma) {
  2970. ggml_vk_host_get(ctx, src0->data, d_Qx, qx_buf_offset);
  2971. ggml_vk_host_get(ctx, src1->data, d_Qy, qy_buf_offset);
  2972. src0_uma = d_Qx != nullptr;
  2973. src1_uma = d_Qy != nullptr;
  2974. }
  2975. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  2976. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  2977. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  2978. const bool qx_needs_dequant = x_non_contig;
  2979. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  2980. // Not implemented
  2981. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  2982. const uint64_t x_ne = ne01 * ne00;
  2983. const uint64_t y_ne = ne11 * ne10;
  2984. const uint64_t d_ne = ne11 * ne01;
  2985. 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);
  2986. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  2987. 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;
  2988. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  2989. const uint64_t d_sz = sizeof(float) * d_ne;
  2990. vk_buffer d_D = extra->buffer_gpu.lock();
  2991. const uint64_t d_buf_offset = extra->offset;
  2992. GGML_ASSERT(d_D != nullptr);
  2993. vk_buffer d_X;
  2994. uint64_t x_buf_offset = 0;
  2995. vk_buffer d_Y;
  2996. uint64_t y_buf_offset = 0;
  2997. if(!src0_uma) {
  2998. d_Qx = extra_src0->buffer_gpu.lock();
  2999. qx_buf_offset = extra_src0->offset;
  3000. GGML_ASSERT(d_Qx != nullptr);
  3001. }
  3002. if(!src1_uma) {
  3003. d_Qy = extra_src1->buffer_gpu.lock();
  3004. qy_buf_offset = extra_src1->offset;
  3005. GGML_ASSERT(d_Qy != nullptr);
  3006. }
  3007. if (qx_needs_dequant) {
  3008. d_X = ctx->prealloc_x;
  3009. } else {
  3010. d_X = d_Qx;
  3011. x_buf_offset = qx_buf_offset;
  3012. GGML_ASSERT(qx_sz == x_sz);
  3013. }
  3014. if (qy_needs_dequant) {
  3015. d_Y = ctx->prealloc_y;
  3016. } else {
  3017. d_Y = d_Qy;
  3018. y_buf_offset = qy_buf_offset;
  3019. GGML_ASSERT(qy_sz == y_sz);
  3020. }
  3021. vk_pipeline to_fp16_vk_0 = nullptr;
  3022. vk_pipeline to_fp16_vk_1 = nullptr;
  3023. if (x_non_contig) {
  3024. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0->type, src0->type);
  3025. }
  3026. if (y_non_contig) {
  3027. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1->type, src1->type);
  3028. } else {
  3029. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3030. }
  3031. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type);
  3032. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3033. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3034. GGML_ASSERT(dmmv != nullptr);
  3035. // Allocate descriptor sets
  3036. if (qx_needs_dequant) {
  3037. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_0, 1);
  3038. }
  3039. if (qy_needs_dequant) {
  3040. ggml_pipeline_allocate_descriptor_sets(ctx, to_fp16_vk_1, y_non_contig ? 1 : ne12 * ne13);
  3041. }
  3042. ggml_pipeline_allocate_descriptor_sets(ctx, dmmv, ne12 * ne13);
  3043. if (x_non_contig) {
  3044. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  3045. 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 });
  3046. }
  3047. if (y_non_contig) {
  3048. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  3049. 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 });
  3050. }
  3051. uint32_t stride_batch_x = ne00*ne01;
  3052. uint32_t stride_batch_y = ne10*ne11;
  3053. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3054. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3055. }
  3056. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  3057. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3058. }
  3059. // compute
  3060. const vk_mat_vec_push_constants pc = {
  3061. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  3062. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  3063. stride_batch_x, stride_batch_y, (uint32_t)(ne20*ne21),
  3064. // 0, 0, 0, 0, 1
  3065. };
  3066. ggml_vk_sync_buffers(subctx);
  3067. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv, { { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 }, { d_D, d_buf_offset, d_sz * ne22 * ne23} }, sizeof(vk_mat_vec_push_constants), &pc, { (uint32_t)ne01, (uint32_t)(ne12 * ne13), 1});
  3068. }*/
  3069. static void ggml_vk_op_repeat(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3070. // guaranteed to be an integer due to the check in ggml_can_repeat
  3071. const uint64_t ne0 = dst->ne[0];
  3072. const uint64_t ne1 = dst->ne[1];
  3073. const uint64_t ne2 = dst->ne[2];
  3074. const uint64_t ne3 = dst->ne[3];
  3075. const uint64_t ne00 = src0->ne[0];
  3076. const uint64_t ne01 = src0->ne[1];
  3077. const uint64_t ne02 = src0->ne[2];
  3078. const uint64_t ne03 = src0->ne[3];
  3079. const uint64_t nb0 = dst->nb[0];
  3080. const uint64_t nb1 = dst->nb[1];
  3081. const uint64_t nb2 = dst->nb[2];
  3082. const uint64_t nb3 = dst->nb[3];
  3083. const uint64_t nb00 = src0->nb[0];
  3084. const uint64_t nb01 = src0->nb[1];
  3085. const uint64_t nb02 = src0->nb[2];
  3086. const uint64_t nb03 = src0->nb[3];
  3087. const uint64_t nr0 = ne0/ne00;
  3088. const uint64_t nr1 = ne1/ne01;
  3089. const uint64_t nr2 = ne2/ne02;
  3090. const uint64_t nr3 = ne3/ne03;
  3091. // TODO: support for transposed / permuted tensors
  3092. GGML_ASSERT(nb0 == sizeof(float));
  3093. GGML_ASSERT(nb00 == sizeof(float));
  3094. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  3095. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  3096. const vk_buffer src_buf = extra_src0->buffer_gpu.lock();
  3097. const uint64_t src_offset = extra_src0->offset;
  3098. vk_buffer dst_buf = extra->buffer_gpu.lock();
  3099. const uint64_t dst_offset = extra->offset;
  3100. std::vector<vk::BufferCopy> copies;
  3101. for (uint64_t i3 = 0; i3 < nr3; i3++) {
  3102. for (uint64_t k3 = 0; k3 < ne03; k3++) {
  3103. for (uint64_t i2 = 0; i2 < nr2; i2++) {
  3104. for (uint64_t k2 = 0; k2 < ne02; k2++) {
  3105. for (uint64_t i1 = 0; i1 < nr1; i1++) {
  3106. for (uint64_t k1 = 0; k1 < ne01; k1++) {
  3107. for (uint64_t i0 = 0; i0 < nr0; i0++) {
  3108. copies.push_back({
  3109. src_offset + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0,
  3110. dst_offset + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01,
  3111. ne00*nb0,
  3112. });
  3113. }
  3114. }
  3115. }
  3116. }
  3117. }
  3118. }
  3119. }
  3120. ggml_vk_sync_buffers(subctx);
  3121. subctx->s->buffer.copyBuffer(src_buf->buffer, dst_buf->buffer, copies);
  3122. GGML_UNUSED(ctx);
  3123. GGML_UNUSED(src1);
  3124. }
  3125. 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) {
  3126. switch (op) {
  3127. case GGML_OP_ADD:
  3128. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3129. return ctx->device->pipeline_add_f32;
  3130. }
  3131. return nullptr;
  3132. case GGML_OP_GET_ROWS:
  3133. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  3134. if (dst->type == GGML_TYPE_F16) {
  3135. return ctx->device->pipeline_get_rows[src0->type];
  3136. }
  3137. if (dst->type == GGML_TYPE_F32) {
  3138. return ctx->device->pipeline_get_rows_f32[src0->type];
  3139. }
  3140. return nullptr;
  3141. case GGML_OP_MUL:
  3142. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3143. return ctx->device->pipeline_mul_f32;
  3144. }
  3145. return nullptr;
  3146. case GGML_OP_SCALE:
  3147. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3148. return ctx->device->pipeline_scale_f32;
  3149. }
  3150. return nullptr;
  3151. case GGML_OP_SQR:
  3152. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3153. return ctx->device->pipeline_sqr_f32;
  3154. }
  3155. return nullptr;
  3156. case GGML_OP_CLAMP:
  3157. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3158. return ctx->device->pipeline_clamp_f32;
  3159. }
  3160. return nullptr;
  3161. case GGML_OP_CPY:
  3162. case GGML_OP_CONT:
  3163. case GGML_OP_DUP:
  3164. return ggml_vk_get_cpy_pipeline(ctx, src0->type, dst->type);
  3165. case GGML_OP_NORM:
  3166. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3167. return ctx->device->pipeline_norm_f32;
  3168. }
  3169. return nullptr;
  3170. case GGML_OP_RMS_NORM:
  3171. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3172. return ctx->device->pipeline_rms_norm_f32;
  3173. }
  3174. return nullptr;
  3175. case GGML_OP_UNARY:
  3176. switch (ggml_get_unary_op(dst)) {
  3177. case GGML_UNARY_OP_SILU:
  3178. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3179. return ctx->device->pipeline_silu_f32;
  3180. }
  3181. break;
  3182. case GGML_UNARY_OP_GELU:
  3183. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3184. return ctx->device->pipeline_gelu_f32;
  3185. }
  3186. break;
  3187. case GGML_UNARY_OP_RELU:
  3188. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3189. return ctx->device->pipeline_relu_f32;
  3190. }
  3191. break;
  3192. default:
  3193. break;
  3194. }
  3195. return nullptr;
  3196. case GGML_OP_DIAG_MASK_INF:
  3197. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3198. return ctx->device->pipeline_diag_mask_inf_f32;
  3199. }
  3200. return nullptr;
  3201. case GGML_OP_SOFT_MAX:
  3202. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  3203. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  3204. return ctx->device->pipeline_soft_max_f32;
  3205. }
  3206. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  3207. return ctx->device->pipeline_soft_max_f32_f16;
  3208. }
  3209. return nullptr;
  3210. case GGML_OP_ROPE:
  3211. {
  3212. const int mode = ((const int32_t *) dst->op_params)[2];
  3213. const bool is_neox = mode & 2;
  3214. const bool is_glm = mode & 4;
  3215. if (is_glm) {
  3216. return nullptr;
  3217. }
  3218. if (is_neox) {
  3219. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3220. return ctx->device->pipeline_rope_neox_f32;
  3221. }
  3222. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  3223. return ctx->device->pipeline_rope_neox_f16;
  3224. }
  3225. } else {
  3226. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  3227. return ctx->device->pipeline_rope_f32;
  3228. }
  3229. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  3230. return ctx->device->pipeline_rope_f16;
  3231. }
  3232. }
  3233. return nullptr;
  3234. }
  3235. case GGML_OP_ARGSORT:
  3236. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  3237. return ctx->device->pipeline_argsort_f32;
  3238. }
  3239. return nullptr;
  3240. default:
  3241. return nullptr;
  3242. }
  3243. GGML_UNUSED(src2);
  3244. }
  3245. static ggml_vk_func_t ggml_vk_op_get_func(ggml_op op) {
  3246. switch(op) {
  3247. case GGML_OP_REPEAT:
  3248. return ggml_vk_op_repeat;
  3249. default:
  3250. return nullptr;
  3251. }
  3252. }
  3253. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  3254. switch (op) {
  3255. case GGML_OP_CPY:
  3256. case GGML_OP_GET_ROWS:
  3257. case GGML_OP_ADD:
  3258. case GGML_OP_MUL:
  3259. case GGML_OP_SCALE:
  3260. case GGML_OP_SQR:
  3261. case GGML_OP_CLAMP:
  3262. return true;
  3263. default:
  3264. return false;
  3265. }
  3266. }
  3267. template<typename PC>
  3268. 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, const PC&& pc) {
  3269. #ifdef GGML_VULKAN_DEBUG
  3270. std::cerr << "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];
  3271. if (src1 != nullptr) {
  3272. 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];
  3273. }
  3274. if (src2 != nullptr) {
  3275. 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];
  3276. }
  3277. 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] << "), " << ggml_op_name(op) << ")" << std::endl;
  3278. #endif
  3279. GGML_ASSERT(op == GGML_OP_GET_ROWS || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  3280. GGML_ASSERT(op == GGML_OP_CPY || ggml_vk_dim01_contiguous(src0)); // NOLINT
  3281. GGML_ASSERT(dst->extra != nullptr);
  3282. const uint64_t ne00 = src0->ne[0];
  3283. const uint64_t ne01 = src0->ne[1];
  3284. const uint64_t ne02 = src0->ne[2];
  3285. const uint64_t ne03 = src0->ne[3];
  3286. const uint64_t ne0 = ne00 * ne01;
  3287. const bool use_src1 = src1 != nullptr;
  3288. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  3289. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  3290. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  3291. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  3292. const uint64_t ne1 = ne10 * ne11;
  3293. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  3294. const bool use_src2 = src2 != nullptr;
  3295. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  3296. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  3297. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  3298. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  3299. const uint64_t ne2 = ne20 * ne21;
  3300. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  3301. ggml_vk_func_t op_func;
  3302. if (pipeline == nullptr) {
  3303. op_func = ggml_vk_op_get_func(op);
  3304. if (op_func == nullptr) {
  3305. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  3306. if (src1 != nullptr) {
  3307. std::cerr << " and " << ggml_type_name(src1->type);
  3308. }
  3309. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  3310. GGML_ASSERT(false);
  3311. }
  3312. op_func(ctx, subctx, src0, src1, dst);
  3313. return;
  3314. }
  3315. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  3316. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  3317. ggml_tensor_extra_gpu * extra_src0 = (ggml_tensor_extra_gpu *) src0->extra;
  3318. ggml_tensor_extra_gpu * extra_src1 = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  3319. ggml_tensor_extra_gpu * extra_src2 = use_src2 ? (ggml_tensor_extra_gpu *) src2->extra : nullptr;
  3320. vk_buffer d_X = nullptr;
  3321. size_t x_buf_offset = 0;
  3322. vk_buffer d_Y = nullptr;
  3323. size_t y_buf_offset = 0;
  3324. vk_buffer d_Z = nullptr;
  3325. size_t z_buf_offset = 0;
  3326. bool src0_uma = false;
  3327. bool src1_uma = false;
  3328. bool src2_uma = false;
  3329. if (ctx->device->uma) {
  3330. ggml_vk_host_get(ctx, src0->data, d_X, x_buf_offset);
  3331. src0_uma = d_X != nullptr;
  3332. if (use_src1) {
  3333. ggml_vk_host_get(ctx, src1->data, d_Y, y_buf_offset);
  3334. src1_uma = d_Y != nullptr;
  3335. }
  3336. if (use_src2) {
  3337. ggml_vk_host_get(ctx, src2->data, d_Z, z_buf_offset);
  3338. src2_uma = d_Z != nullptr;
  3339. }
  3340. }
  3341. uint64_t x_sz = ggml_vk_align_size(ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0, ctx->device->properties.limits.minStorageBufferOffsetAlignment);
  3342. uint64_t y_sz = use_src1 ? ggml_vk_align_size(ggml_type_size(src1->type) * ne1, ctx->device->properties.limits.minStorageBufferOffsetAlignment) : 0;
  3343. uint64_t z_sz = use_src2 ? ggml_vk_align_size(ggml_type_size(src2->type) * ne2, ctx->device->properties.limits.minStorageBufferOffsetAlignment) : 0;
  3344. uint64_t d_sz = ggml_type_size(dst->type) * ne0;
  3345. vk_buffer d_D = extra->buffer_gpu.lock();
  3346. // Workaround for tiny tensor inputs on ROPE
  3347. if (use_src1 && y_sz > d_D->size) {
  3348. y_sz = VK_WHOLE_SIZE;
  3349. }
  3350. GGML_ASSERT(d_D != nullptr);
  3351. uint64_t d_buf_offset = (extra->offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  3352. GGML_ASSERT(d_buf_offset == extra->offset || op == GGML_OP_CPY); // NOLINT
  3353. if(!src0_uma) {
  3354. d_X = extra_src0->buffer_gpu.lock();
  3355. x_buf_offset = extra_src0->offset;
  3356. GGML_ASSERT(d_X != nullptr);
  3357. }
  3358. if (use_src1 && !src1_uma) {
  3359. d_Y = extra_src1->buffer_gpu.lock();
  3360. y_buf_offset = extra_src1->offset;
  3361. GGML_ASSERT(d_Y != nullptr);
  3362. }
  3363. if (use_src2 && !src2_uma) {
  3364. d_Z = extra_src2->buffer_gpu.lock();
  3365. z_buf_offset = extra_src2->offset;
  3366. GGML_ASSERT(d_Z != nullptr);
  3367. }
  3368. if (op_supports_incontiguous) {
  3369. x_sz = ggml_nbytes(src0);
  3370. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  3371. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  3372. d_sz = ggml_nbytes(dst);
  3373. if (x_buf_offset + x_sz >= d_X->size) {
  3374. x_sz = VK_WHOLE_SIZE;
  3375. }
  3376. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  3377. y_sz = VK_WHOLE_SIZE;
  3378. }
  3379. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  3380. z_sz = VK_WHOLE_SIZE;
  3381. }
  3382. if (d_buf_offset + d_sz >= d_D->size) {
  3383. d_sz = VK_WHOLE_SIZE;
  3384. }
  3385. }
  3386. std::array<uint32_t, 3> elements;
  3387. // Single call if dimension 2 is contiguous
  3388. if (op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1)))) {
  3389. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, 1);
  3390. switch (dst->op) {
  3391. case GGML_OP_NORM:
  3392. case GGML_OP_RMS_NORM:
  3393. case GGML_OP_SOFT_MAX:
  3394. elements = { (uint32_t)ggml_nrows(src0), 1, 1 };
  3395. break;
  3396. case GGML_OP_DIAG_MASK_INF:
  3397. case GGML_OP_ROPE:
  3398. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  3399. break;
  3400. case GGML_OP_GET_ROWS:
  3401. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  3402. break;
  3403. case GGML_OP_ARGSORT:
  3404. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  3405. break;
  3406. default:
  3407. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  3408. break;
  3409. }
  3410. if (!op_supports_incontiguous) {
  3411. if (x_sz != VK_WHOLE_SIZE) {
  3412. x_sz *= ne02 * ne03;
  3413. }
  3414. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  3415. y_sz *= ne12 * ne13;
  3416. }
  3417. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  3418. z_sz *= ne22 * ne23;
  3419. }
  3420. if (d_sz != VK_WHOLE_SIZE) {
  3421. d_sz *= ne02 * ne03;
  3422. }
  3423. }
  3424. if (op == GGML_OP_SOFT_MAX) {
  3425. // Empty src1 is possible in soft_max, but the shader needs a buffer
  3426. vk_subbuffer subbuf_y;
  3427. if (use_src1) {
  3428. subbuf_y = { d_Y, y_buf_offset, y_sz };
  3429. } else {
  3430. subbuf_y = { d_X, 0, d_X->size };
  3431. }
  3432. ggml_vk_sync_buffers(subctx);
  3433. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, subbuf_y, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3434. } else if (op == GGML_OP_ROPE) {
  3435. const int mode = ((int32_t *) dst->op_params)[2];
  3436. const bool is_neox = mode & 2;
  3437. if (is_neox) {
  3438. // Empty src2 is possible in rope, but the shader needs a buffer
  3439. vk_subbuffer subbuf_z;
  3440. if (use_src2) {
  3441. subbuf_z = { d_Z, z_buf_offset, z_sz };
  3442. } else {
  3443. subbuf_z = { d_X, 0, d_X->size };
  3444. }
  3445. ggml_vk_sync_buffers(subctx);
  3446. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, subbuf_z, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3447. } else {
  3448. ggml_vk_sync_buffers(subctx);
  3449. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3450. }
  3451. } else if (use_src2) {
  3452. ggml_vk_sync_buffers(subctx);
  3453. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, { d_Z, z_buf_offset, z_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3454. } else if (use_src1) {
  3455. ggml_vk_sync_buffers(subctx);
  3456. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_Y, y_buf_offset, y_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3457. } else {
  3458. ggml_vk_sync_buffers(subctx);
  3459. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset, x_sz }, { d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  3460. }
  3461. } else {
  3462. GGML_ASSERT(op != GGML_OP_SOFT_MAX);
  3463. GGML_ASSERT(op != GGML_OP_ARGSORT);
  3464. GGML_ASSERT(!use_src2);
  3465. ggml_pipeline_allocate_descriptor_sets(ctx, pipeline, ne02 * ne03);
  3466. switch (dst->op) {
  3467. case GGML_OP_NORM:
  3468. case GGML_OP_RMS_NORM:
  3469. elements = { (uint32_t)ne01, 1, 1 };
  3470. break;
  3471. case GGML_OP_DIAG_MASK_INF:
  3472. case GGML_OP_ROPE:
  3473. elements = { (uint32_t)ne01, (uint32_t)ne00, 1 };
  3474. break;
  3475. case GGML_OP_GET_ROWS:
  3476. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  3477. break;
  3478. default:
  3479. elements = { (uint32_t)ne0, 1, 1 };
  3480. break;
  3481. }
  3482. for (uint64_t i03 = 0; i03 < ne03; i03++) {
  3483. for (uint64_t i02 = 0; i02 < ne02; i02++) {
  3484. const uint32_t it_idx0 = (i03 * ne02 + i02);
  3485. const uint32_t it_idx1 = use_src1 ? ((i03 % ne13) * ne12 + (i02 % ne12)) : 0;
  3486. const uint32_t x_offset = x_sz * it_idx0;
  3487. const uint32_t y_offset = y_sz * it_idx1;
  3488. const uint32_t d_offset = d_sz * it_idx0;
  3489. if (use_src1) {
  3490. ggml_vk_sync_buffers(subctx);
  3491. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset + x_offset, x_sz }, { d_Y, y_buf_offset + y_offset, y_sz }, { d_D, d_buf_offset + d_offset, d_sz } }, sizeof(PC), &pc, elements);
  3492. } else {
  3493. ggml_vk_sync_buffers(subctx);
  3494. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { { d_X, x_buf_offset + x_offset, x_sz }, { d_D, d_buf_offset + d_offset, d_sz } }, sizeof(PC), &pc, elements);
  3495. }
  3496. }
  3497. }
  3498. }
  3499. }
  3500. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3501. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_REPEAT, { (uint32_t)ggml_nelements(src0), (uint32_t)ggml_nelements(src1), 0.0f, 0.0f });
  3502. }
  3503. 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) {
  3504. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3505. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3506. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3507. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  3508. (uint32_t)ggml_nelements(src0),
  3509. (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,
  3510. (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,
  3511. (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,
  3512. 0,
  3513. 0.0f, 0.0f,
  3514. });
  3515. }
  3516. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3517. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3518. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3519. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3520. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  3521. (uint32_t)ggml_nelements(src0),
  3522. (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,
  3523. (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,
  3524. (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,
  3525. 0,
  3526. 0.0f, 0.0f,
  3527. });
  3528. }
  3529. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3530. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3531. const uint32_t src1_type_size = ggml_type_size(src1->type);
  3532. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3533. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  3534. (uint32_t)ggml_nelements(src0),
  3535. (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,
  3536. (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,
  3537. (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,
  3538. 0,
  3539. 0.0f, 0.0f,
  3540. });
  3541. }
  3542. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3543. float * op_params = (float *)dst->op_params;
  3544. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3545. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3546. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  3547. (uint32_t)ggml_nelements(src0),
  3548. (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,
  3549. (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,
  3550. 0,
  3551. op_params[0], 0.0f
  3552. });
  3553. }
  3554. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3555. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3556. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3557. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  3558. (uint32_t)ggml_nelements(src0),
  3559. (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,
  3560. (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,
  3561. 0,
  3562. 0.0f, 0.0f,
  3563. });
  3564. }
  3565. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3566. float * op_params = (float *)dst->op_params;
  3567. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3568. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3569. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  3570. (uint32_t)ggml_nelements(src0),
  3571. (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,
  3572. (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,
  3573. 0,
  3574. op_params[0], op_params[1],
  3575. });
  3576. }
  3577. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3578. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) dst->extra;
  3579. const uint32_t src0_type_size = ggml_type_size(src0->type);
  3580. const uint32_t dst_type_size = ggml_type_size(dst->type);
  3581. const uint32_t d_offset = (extra->offset % ctx->device->properties.limits.minStorageBufferOffsetAlignment) / dst_type_size;
  3582. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  3583. (uint32_t)ggml_nelements(src0),
  3584. (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,
  3585. (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,
  3586. d_offset,
  3587. 0.0f, 0.0f,
  3588. });
  3589. }
  3590. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3591. float * op_params = (float *)dst->op_params;
  3592. 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 });
  3593. }
  3594. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3595. float * op_params = (float *)dst->op_params;
  3596. 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 });
  3597. }
  3598. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3599. 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 });
  3600. }
  3601. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3602. int32_t * op_params = (int32_t *)dst->op_params;
  3603. 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] });
  3604. }
  3605. 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) {
  3606. float * op_params = (float *)dst->op_params;
  3607. float scale = op_params[0];
  3608. float max_bias = op_params[1];
  3609. const uint32_t ncols = (uint32_t)src0->ne[0];
  3610. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  3611. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  3612. const uint32_t n_head_kv = nrows_x/nrows_y;
  3613. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  3614. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  3615. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  3616. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  3617. ncols,
  3618. src1 != nullptr ? nrows_y : (uint32_t)0,
  3619. scale, max_bias,
  3620. m0, m1,
  3621. n_head_log2,
  3622. });
  3623. }
  3624. 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) {
  3625. const int n_dims = ((int32_t *) dst->op_params)[1];
  3626. const int mode = ((int32_t *) dst->op_params)[2];
  3627. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  3628. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  3629. const float freq_base = ((float *) dst->op_params)[5];
  3630. const float freq_scale = ((float *) dst->op_params)[6];
  3631. const float ext_factor = ((float *) dst->op_params)[7];
  3632. const float attn_factor = ((float *) dst->op_params)[8];
  3633. const float beta_fast = ((float *) dst->op_params)[9];
  3634. const float beta_slow = ((float *) dst->op_params)[10];
  3635. const bool is_neox = mode & 2;
  3636. const bool is_glm = mode & 4;
  3637. GGML_ASSERT(!is_glm);
  3638. float corr_dims[2];
  3639. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims);
  3640. if (is_neox) {
  3641. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  3642. const float inv_ndims = -1.0f / n_dims;
  3643. ggml_vk_op_f32<vk_op_rope_neox_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  3644. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  3645. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1], 0.0f, 0.0f}, theta_scale, inv_ndims,
  3646. src2 != nullptr,
  3647. });
  3648. } else {
  3649. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  3650. (uint32_t)src0->ne[0], freq_scale, (uint32_t)src0->ne[1],
  3651. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1], 0.0f, 0.0f}
  3652. });
  3653. }
  3654. }
  3655. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context * subctx, const ggml_tensor * src0, ggml_tensor * dst) {
  3656. int32_t * op_params = (int32_t *)dst->op_params;
  3657. uint32_t ncols = src0->ne[0];
  3658. uint32_t ncols_pad = 1;
  3659. while (ncols_pad < ncols) {
  3660. ncols_pad *= 2;
  3661. }
  3662. GGML_ASSERT(ncols_pad <= 1024);
  3663. std::cerr << "ncols=" << ncols << " ncols_pad=" << ncols_pad << " ascending=" << op_params[0] << std::endl;
  3664. std::cerr << ((ggml_sort_order) op_params[0]) << " " << GGML_SORT_ORDER_ASC << std::endl;
  3665. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  3666. ncols,
  3667. ncols_pad,
  3668. op_params[0],
  3669. });
  3670. }
  3671. #ifdef GGML_VULKAN_RUN_TESTS
  3672. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  3673. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  3674. return;
  3675. }
  3676. i0 = std::max(i0, 5);
  3677. i1 = std::max(i1, 5);
  3678. i2 = std::max(i2, 0);
  3679. fprintf(stderr, " ");
  3680. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  3681. fprintf(stderr, "%7d ", idx1);
  3682. }
  3683. fprintf(stderr, "\n");
  3684. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  3685. fprintf(stderr, "%7d: ", idx0);
  3686. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  3687. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  3688. float val;
  3689. if (type == GGML_TYPE_F32) {
  3690. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  3691. } else if (type == GGML_TYPE_F16) {
  3692. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  3693. } else {
  3694. GGML_ASSERT(false);
  3695. }
  3696. fprintf(stderr, "% 7.2f ", val);
  3697. } else {
  3698. fprintf(stderr, " ");
  3699. }
  3700. }
  3701. fprintf(stderr, "\n");
  3702. }
  3703. }
  3704. template <typename X_TYPE, typename Y_TYPE>
  3705. 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) {
  3706. #ifdef GGML_VULKAN_DEBUG
  3707. std::cerr << "ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")" << std::endl;
  3708. #endif
  3709. const size_t x_ne = m * k * batch;
  3710. const size_t y_ne = k * n * batch;
  3711. const size_t d_ne = m * n * batch;
  3712. vk_pipeline p;
  3713. std::string shname;
  3714. if (shader_size == 0) {
  3715. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3716. p = ctx->device->pipeline_matmul_f32->a_s;
  3717. shname = "F32_ALIGNED_S";
  3718. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3719. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  3720. shname = "F32_F16_ALIGNED_S";
  3721. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3722. p = ctx->device->pipeline_matmul_f16_f32->a_s;
  3723. shname = "F16_F32_ALIGNED_S";
  3724. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3725. p = ctx->device->pipeline_matmul_f16->a_s;
  3726. shname = "F16_ALIGNED_S";
  3727. } else {
  3728. GGML_ASSERT(false);
  3729. }
  3730. } else if (shader_size == 1) {
  3731. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3732. p = ctx->device->pipeline_matmul_f32->a_m;
  3733. shname = "F32_ALIGNED_M";
  3734. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3735. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  3736. shname = "F32_F16_ALIGNED_M";
  3737. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3738. p = ctx->device->pipeline_matmul_f16_f32->a_m;
  3739. shname = "F16_F32_ALIGNED_M";
  3740. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3741. p = ctx->device->pipeline_matmul_f16->a_m;
  3742. shname = "F16_ALIGNED_M";
  3743. } else {
  3744. GGML_ASSERT(false);
  3745. }
  3746. } else if (shader_size == 2) {
  3747. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3748. p = ctx->device->pipeline_matmul_f32->a_l;
  3749. shname = "F32_ALIGNED_L";
  3750. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3751. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  3752. shname = "F32_F16_ALIGNED_L";
  3753. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3754. p = ctx->device->pipeline_matmul_f16_f32->a_l;
  3755. shname = "F16_F32_ALIGNED_L";
  3756. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3757. p = ctx->device->pipeline_matmul_f16->a_l;
  3758. shname = "F16_ALIGNED_L";
  3759. } else {
  3760. GGML_ASSERT(false);
  3761. }
  3762. } else {
  3763. GGML_ASSERT(0);
  3764. }
  3765. const size_t kpad = ggml_vk_align_size(k, p->align);
  3766. if (k != kpad) {
  3767. if (shader_size == 0) {
  3768. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3769. p = ctx->device->pipeline_matmul_f32->s;
  3770. shname = "F32_S";
  3771. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3772. p = ctx->device->pipeline_matmul_f32_f16->s;
  3773. shname = "F32_F16_S";
  3774. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3775. p = ctx->device->pipeline_matmul_f16_f32->s;
  3776. shname = "F16_F32_S";
  3777. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3778. p = ctx->device->pipeline_matmul_f16->s;
  3779. shname = "F16_S";
  3780. }
  3781. } else if (shader_size == 1) {
  3782. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3783. p = ctx->device->pipeline_matmul_f32->m;
  3784. shname = "F32_M";
  3785. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3786. p = ctx->device->pipeline_matmul_f32_f16->m;
  3787. shname = "F32_F16_M";
  3788. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3789. p = ctx->device->pipeline_matmul_f16_f32->m;
  3790. shname = "F16_F32_M";
  3791. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3792. p = ctx->device->pipeline_matmul_f16->m;
  3793. shname = "F16_M";
  3794. }
  3795. } else if (shader_size == 2) {
  3796. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3797. p = ctx->device->pipeline_matmul_f32->l;
  3798. shname = "F32_L";
  3799. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3800. p = ctx->device->pipeline_matmul_f32_f16->l;
  3801. shname = "F32_F16_L";
  3802. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  3803. p = ctx->device->pipeline_matmul_f16_f32->l;
  3804. shname = "F16_F32_L";
  3805. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3806. p = ctx->device->pipeline_matmul_f16->l;
  3807. shname = "F16_L";
  3808. }
  3809. }
  3810. }
  3811. ggml_pipeline_allocate_descriptor_sets(ctx, p, num_it);
  3812. if (split_k > 1) {
  3813. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  3814. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  3815. // Resize buffer
  3816. if (ctx->prealloc_split_k != nullptr) {
  3817. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  3818. }
  3819. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3820. }
  3821. }
  3822. vk_buffer d_X = ggml_vk_create_buffer_check(ctx, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3823. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3824. vk_buffer d_D = ggml_vk_create_buffer_check(ctx, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  3825. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  3826. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  3827. float* d = (float *) malloc(sizeof(float) * d_ne);
  3828. for (size_t i = 0; i < x_ne; i++) {
  3829. if (std::is_same<float, X_TYPE>()) {
  3830. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  3831. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  3832. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  3833. } else {
  3834. GGML_ASSERT(false);
  3835. }
  3836. }
  3837. for (size_t i = 0; i < y_ne; i++) {
  3838. if (std::is_same<float, Y_TYPE>()) {
  3839. // y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  3840. y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  3841. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3842. // y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  3843. y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  3844. } else {
  3845. GGML_ASSERT(false);
  3846. }
  3847. }
  3848. ggml_vk_buffer_write(ctx, d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  3849. ggml_vk_buffer_write(ctx, d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  3850. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  3851. for (size_t i = 0; i < num_it; i++) {
  3852. ggml_vk_ctx_begin(ctx, subctx);
  3853. ggml_vk_matmul(
  3854. 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),
  3855. m, n, k, k, k, m, split_k, batch, batch, batch, 1, 1, k*m, k*n, m*n, 0, 0, 0, 0, 1
  3856. );
  3857. ggml_vk_ctx_end(subctx);
  3858. }
  3859. auto begin = std::chrono::high_resolution_clock::now();
  3860. ggml_vk_submit(subctx, ctx->fence);
  3861. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  3862. ctx->device->device.resetFences({ ctx->fence });
  3863. auto end = std::chrono::high_resolution_clock::now();
  3864. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  3865. // copy dst to host
  3866. ggml_vk_buffer_read(ctx, d_D, 0, d, sizeof(float) * d_ne);
  3867. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  3868. ggml_init_params iparams = {
  3869. /*.mem_size =*/ 1024*1024*1024,
  3870. /*.mem_buffer =*/ NULL,
  3871. /*.no_alloc =*/ true,
  3872. };
  3873. ggml_context * ggml_ctx = ggml_init(iparams);
  3874. ggml_type src0_type;
  3875. ggml_type src1_type;
  3876. if (std::is_same<float, X_TYPE>()) {
  3877. src0_type = GGML_TYPE_F32;
  3878. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  3879. src0_type = GGML_TYPE_F16;
  3880. } else {
  3881. GGML_ASSERT(false);
  3882. }
  3883. if (std::is_same<float, Y_TYPE>()) {
  3884. src1_type = GGML_TYPE_F32;
  3885. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  3886. src1_type = GGML_TYPE_F16;
  3887. } else {
  3888. GGML_ASSERT(false);
  3889. }
  3890. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  3891. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  3892. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  3893. src0_ggml->data = x;
  3894. src1_ggml->data = y;
  3895. tensor_ggml->data = d_chk;
  3896. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  3897. ggml_build_forward_expand(cgraph, tensor_ggml);
  3898. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  3899. ggml_free(ggml_ctx);
  3900. double avg_err = 0.0;
  3901. int first_err_n = -1;
  3902. int first_err_m = -1;
  3903. int first_err_b = -1;
  3904. for (size_t i = 0; i < m*n*batch; i++) {
  3905. double err = std::fabs(d[i] - d_chk[i]);
  3906. avg_err += err;
  3907. if (err > 0.05f && first_err_n == -1) {
  3908. first_err_b = i / (m * n);
  3909. first_err_n = (i % (m * n)) / m;
  3910. first_err_m = (i % (m * n)) % m;
  3911. }
  3912. }
  3913. avg_err /= m * n;
  3914. std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms avg_err=" << avg_err << std::endl;
  3915. if (avg_err > 0.1) {
  3916. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  3917. std::cerr << "Actual result: " << std::endl << std::endl;
  3918. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3919. std::cerr << std::endl;
  3920. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n + 15, first_err_b);
  3921. std::cerr << "Expected result: " << std::endl << std::endl;
  3922. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3923. if (split_k > 1) {
  3924. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  3925. ggml_vk_buffer_read(ctx, ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  3926. std::cerr << "d_buf0: " << std::endl << std::endl;
  3927. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3928. std::cerr << "d_buf1: " << std::endl << std::endl;
  3929. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  3930. std::cerr << "d_buf2: " << std::endl << std::endl;
  3931. 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);
  3932. std::cerr << "d_buf3: " << std::endl << std::endl;
  3933. 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);
  3934. free(split_k_buf);
  3935. }
  3936. }
  3937. free(d_chk);
  3938. ggml_vk_queue_cleanup(ctx, ctx->device->transfer_queue);
  3939. ggml_vk_queue_cleanup(ctx, ctx->device->compute_queue);
  3940. ggml_vk_destroy_buffer(d_X);
  3941. ggml_vk_destroy_buffer(d_Y);
  3942. ggml_vk_destroy_buffer(d_D);
  3943. ggml_pipeline_cleanup(p);
  3944. ggml_pipeline_cleanup(ctx->device->pipeline_matmul_split_k_reduce);
  3945. free(x);
  3946. free(y);
  3947. free(d);
  3948. }
  3949. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  3950. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  3951. return;
  3952. }
  3953. i0 = std::max(i0, 5);
  3954. i1 = std::max(i1, 5);
  3955. i2 = std::max(i2, 0);
  3956. i3 = std::max(i3, 0);
  3957. fprintf(stderr, " ");
  3958. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  3959. fprintf(stderr, "%7d ", idx1);
  3960. }
  3961. fprintf(stderr, "\n");
  3962. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  3963. fprintf(stderr, "%7d: ", idx0);
  3964. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  3965. 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]) {
  3966. float val;
  3967. if (tensor->type == GGML_TYPE_F32) {
  3968. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  3969. } else if (tensor->type == GGML_TYPE_F16) {
  3970. 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]));
  3971. } else {
  3972. GGML_ASSERT(false);
  3973. }
  3974. fprintf(stderr, "% 7.2f ", val);
  3975. } else {
  3976. fprintf(stderr, " ");
  3977. }
  3978. }
  3979. fprintf(stderr, "\n");
  3980. }
  3981. }
  3982. static void ggml_vk_test_h2d_nc(ggml_backend_vk_context * ctx, size_t ne0, size_t ne1, size_t ne2, size_t ne3) {
  3983. const size_t ne = ne0 * ne1 * ne2 * ne3;
  3984. ggml_init_params iparams = {
  3985. /*.mem_size =*/ 1024*1024*1024,
  3986. /*.mem_buffer =*/ NULL,
  3987. /*.no_alloc =*/ true,
  3988. };
  3989. ggml_context * ggml_ctx = ggml_init(iparams);
  3990. ggml_tensor * tensor = ggml_new_tensor_4d(ggml_ctx, GGML_TYPE_F32, ne0, ne2, ne1, ne3); // NOLINT
  3991. ggml_tensor * result_tensor = ggml_new_tensor_4d(ggml_ctx, GGML_TYPE_F32, ne0, ne1, ne2, ne3);
  3992. float * data = (float *) ggml_vk_host_malloc(ctx, ggml_nbytes(tensor));
  3993. tensor->data = data;
  3994. float * result_data = (float *) malloc(ggml_nbytes(tensor));
  3995. result_tensor->data = result_data;
  3996. // Permute
  3997. {
  3998. size_t tmp = tensor->nb[2];
  3999. tensor->nb[2] = tensor->nb[1];
  4000. tensor->nb[1] = tmp;
  4001. tensor->ne[2] = ne2;
  4002. tensor->ne[1] = ne1;
  4003. }
  4004. for (size_t i = 0; i < ne; i++) {
  4005. data[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  4006. }
  4007. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4008. ggml_vk_ctx_begin(ctx, subctx);
  4009. vk_buffer buffer = ggml_vk_create_buffer_check(ctx, ggml_nbytes(tensor), vk::MemoryPropertyFlagBits::eDeviceLocal);
  4010. ggml_vk_h2d_tensor_2d(ctx, subctx, buffer, 0, tensor, 0, 0, ggml_nrows(tensor));
  4011. ggml_vk_ctx_end(subctx);
  4012. ggml_vk_submit(subctx, ctx->fence);
  4013. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_h2d_nc waitForFences");
  4014. ctx->device->device.resetFences({ ctx->fence });
  4015. ggml_vk_buffer_read(ctx, buffer, 0, result_data, ggml_nbytes(tensor));
  4016. double avg_err = 0.0;
  4017. int first_err_i0 = -1;
  4018. int first_err_i1 = -1;
  4019. int first_err_i2 = -1;
  4020. int first_err_i3 = -1;
  4021. for (size_t i3 = 0; i3 < ne3; i3++) {
  4022. for (size_t i2 = 0; i2 < ne2; i2++) {
  4023. for (size_t i1 = 0; i1 < ne1; i1++) {
  4024. for (size_t i0 = 0; i0 < ne0; i0++) {
  4025. float correct = *(float *) ((char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  4026. float result = *(float *) ((char *) result_data + i3*ne2*ne1*ne0*sizeof(float) + i2*ne1*ne0*sizeof(float) + i1*ne0*sizeof(float) + i0*sizeof(float));
  4027. double err = std::fabs(result - correct);
  4028. avg_err += err;
  4029. if (err > 0.05f && first_err_i0 == -1) {
  4030. first_err_i0 = i0;
  4031. first_err_i1 = i1;
  4032. first_err_i2 = i2;
  4033. first_err_i3 = i3;
  4034. }
  4035. }
  4036. }
  4037. }
  4038. }
  4039. avg_err /= ne;
  4040. std::cerr << "TEST nc copy ne0=" << ne0 << " ne1=" << ne1 << " ne2=" << ne2 << " ne3=" << ne3 << " avg_err=" << avg_err << std::endl;
  4041. if (avg_err > 0.1) {
  4042. std::cerr << "i0 = " << first_err_i0 << " i1 = " << first_err_i1 << " i2 = " << first_err_i2 << " i3 = " << first_err_i3 << std::endl;
  4043. std::cerr << "Actual result: " << std::endl << std::endl;
  4044. ggml_vk_print_tensor_area(result_tensor, first_err_i0, first_err_i1, first_err_i2, first_err_i3);
  4045. std::cerr << "Expected result: " << std::endl << std::endl;
  4046. ggml_vk_print_tensor_area(tensor, first_err_i0, first_err_i1, first_err_i2, first_err_i3);
  4047. }
  4048. ggml_free(ggml_ctx);
  4049. ggml_vk_destroy_buffer(buffer);
  4050. ggml_vk_host_free(ctx, data);
  4051. free(result_data);
  4052. }
  4053. static void ggml_vk_test_transfer(ggml_backend_vk_context * ctx, size_t ne, bool pinned) {
  4054. #ifdef GGML_VULKAN_DEBUG
  4055. std::cerr << "ggml_vk_test_transfer(" << ne << ")" << std::endl;
  4056. #endif
  4057. // Check transfers are correct
  4058. vk_buffer buffer = ggml_vk_create_buffer_check(ctx, sizeof(float) * ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4059. float * x;
  4060. float * y;
  4061. if (pinned) {
  4062. x = (float *) ggml_vk_host_malloc(ctx, sizeof(float) * ne);
  4063. y = (float *) ggml_vk_host_malloc(ctx, sizeof(float) * ne);
  4064. } else {
  4065. x = (float *) malloc(sizeof(float) * ne);
  4066. y = (float *) malloc(sizeof(float) * ne);
  4067. }
  4068. for (size_t i = 0; i < ne; i++) {
  4069. x[i] = rand() / (float)RAND_MAX;
  4070. }
  4071. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4072. ggml_vk_ctx_begin(ctx, subctx);
  4073. auto begin = std::chrono::high_resolution_clock::now();
  4074. ggml_vk_buffer_write_async(ctx, subctx, buffer, 0, x, sizeof(float) * ne);
  4075. for (auto& cpy : subctx->in_memcpys) {
  4076. memcpy(cpy.dst, cpy.src, cpy.n);
  4077. }
  4078. subctx->in_memcpys.clear();
  4079. ggml_vk_ctx_end(subctx);
  4080. ggml_vk_submit(subctx, ctx->fence);
  4081. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_transfer waitForFences");
  4082. ctx->device->device.resetFences({ ctx->fence });
  4083. auto end = std::chrono::high_resolution_clock::now();
  4084. double ms_to_gpu = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4085. ggml_vk_ctx_begin(ctx, subctx);
  4086. begin = std::chrono::high_resolution_clock::now();
  4087. ggml_vk_buffer_read_async(ctx, subctx, buffer, 0, y, sizeof(float) * ne);
  4088. ggml_vk_ctx_end(subctx);
  4089. ggml_vk_submit(subctx, ctx->fence);
  4090. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_transfer waitForFences");
  4091. ctx->device->device.resetFences({ ctx->fence });
  4092. for (auto& cpy : subctx->out_memcpys) {
  4093. memcpy(cpy.dst, cpy.src, cpy.n);
  4094. }
  4095. subctx->out_memcpys.clear();
  4096. end = std::chrono::high_resolution_clock::now();
  4097. double ms_from_gpu = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4098. double avg_err = 0.0;
  4099. for (size_t i = 0; i < ne; i++) {
  4100. avg_err += std::fabs(x[i] - y[i]);
  4101. }
  4102. double kb = ne * sizeof(float) / 1024.0;
  4103. std::cerr << "TEST TRANSFER " << kb << " KB to_gpu " << ms_to_gpu << "ms (" << kb / ms_to_gpu * 1000.0 / 1024.0 << " MB/s) from_gpu " << ms_from_gpu << "ms (" << kb / ms_from_gpu * 1000.0 / 1024.0 << " MB/s) avg_err=" << avg_err / ne << std::endl;
  4104. ggml_vk_destroy_buffer(buffer);
  4105. if (pinned) {
  4106. ggml_vk_host_free(ctx, x);
  4107. ggml_vk_host_free(ctx, y);
  4108. } else {
  4109. free(x);
  4110. free(y);
  4111. }
  4112. }
  4113. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  4114. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  4115. }
  4116. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  4117. #ifdef GGML_VULKAN_DEBUG
  4118. std::cerr << "ggml_vk_test_dequant(" << ne << ")" << std::endl;
  4119. #endif
  4120. const size_t x_sz = sizeof(float) * ne;
  4121. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  4122. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  4123. float * x = (float *) malloc(x_sz);
  4124. void * qx = malloc(qx_sz);
  4125. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4126. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4127. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  4128. for (size_t i = 0; i < ne; i++) {
  4129. x[i] = rand() / (float)RAND_MAX;
  4130. }
  4131. vk_pipeline p = ctx->device->pipeline_dequant[quant];
  4132. ggml_vk_quantize_data(x, qx, ne, quant);
  4133. ggml_pipeline_allocate_descriptor_sets(ctx, p, 1);
  4134. ggml_vk_buffer_write(ctx, qx_buf, 0, qx, qx_sz);
  4135. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4136. ggml_vk_ctx_begin(ctx, subctx);
  4137. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  4138. ggml_vk_dispatch_pipeline(ctx, subctx, p, { { qx_buf, 0, qx_sz }, { x_buf, 0, x_sz_f16 } }, pc.size() * sizeof(int), pc.data(), { (uint32_t)ne, 1, 1});
  4139. ggml_vk_ctx_end(subctx);
  4140. auto begin = std::chrono::high_resolution_clock::now();
  4141. ggml_vk_submit(subctx, ctx->fence);
  4142. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  4143. ctx->device->device.resetFences({ ctx->fence });
  4144. auto end = std::chrono::high_resolution_clock::now();
  4145. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4146. ggml_vk_buffer_read(ctx, x_buf, 0, x_chk, x_sz_f16);
  4147. int first_err = -1;
  4148. double avg_err = 0.0;
  4149. for (size_t i = 0; i < ne; i++) {
  4150. double error = std::fabs(x[i] - ggml_fp16_to_fp32(x_chk[i]));
  4151. avg_err += error;
  4152. if (first_err < 0 && error > 0.05) {
  4153. first_err = i;
  4154. }
  4155. }
  4156. avg_err /= ne;
  4157. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  4158. if (avg_err > 0.1) {
  4159. std::cerr << "first_error = " << first_err << std::endl;
  4160. std::cerr << "Actual result: " << std::endl << std::endl;
  4161. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  4162. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  4163. }
  4164. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  4165. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  4166. std::cerr << x[i] << ", ";
  4167. }
  4168. std::cerr << std::endl;
  4169. }
  4170. ggml_vk_destroy_buffer(x_buf);
  4171. ggml_vk_destroy_buffer(qx_buf);
  4172. free(x);
  4173. free(qx);
  4174. free(x_chk);
  4175. }
  4176. 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) {
  4177. #ifdef GGML_VULKAN_DEBUG
  4178. std::cerr << "ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")" << std::endl;
  4179. #endif
  4180. const size_t x_ne = m * k * batch;
  4181. const size_t y_ne = k * n * batch;
  4182. const size_t d_ne = m * n * batch;
  4183. vk_pipeline p;
  4184. std::string shname;
  4185. if (shader_size == 0) {
  4186. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->a_s;
  4187. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  4188. } else if (shader_size == 1) {
  4189. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->a_m;
  4190. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  4191. } else if (shader_size == 2) {
  4192. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->a_l;
  4193. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  4194. } else {
  4195. GGML_ASSERT(0);
  4196. }
  4197. const size_t kpad = ggml_vk_align_size(k, p->align);
  4198. if (k != kpad) {
  4199. if (shader_size == 0) {
  4200. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->s;
  4201. shname = std::string(ggml_type_name(quant)) + "_S";
  4202. } else if (shader_size == 1) {
  4203. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->m;
  4204. shname = std::string(ggml_type_name(quant)) + "_M";
  4205. } else if (shader_size == 2) {
  4206. p = ctx->device->pipeline_dequant_mul_mat_mat[quant]->l;
  4207. shname = std::string(ggml_type_name(quant)) + "_L";
  4208. } else {
  4209. GGML_ASSERT(0);
  4210. }
  4211. }
  4212. const size_t x_sz = sizeof(float) * x_ne;
  4213. const size_t y_sz = sizeof(float) * y_ne;
  4214. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  4215. const size_t d_sz = sizeof(float) * d_ne;
  4216. float * x = (float *) malloc(x_sz);
  4217. float * y = (float *) malloc(y_sz);
  4218. void * qx = malloc(qx_sz);
  4219. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4220. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4221. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4222. float * d = (float *) malloc(d_sz);
  4223. float * d_chk = (float *) malloc(d_sz);
  4224. for (size_t i = 0; i < x_ne; i++) {
  4225. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  4226. }
  4227. ggml_vk_quantize_data(x, qx, x_ne, quant);
  4228. for (size_t i = 0; i < y_ne; i++) {
  4229. // y[i] = rand() / (float)RAND_MAX;
  4230. y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  4231. }
  4232. ggml_pipeline_allocate_descriptor_sets(ctx, p, num_it);
  4233. if (split_k > 1) {
  4234. ggml_pipeline_allocate_descriptor_sets(ctx, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  4235. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  4236. // Resize buffer
  4237. if (ctx->prealloc_split_k != nullptr) {
  4238. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  4239. }
  4240. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  4241. }
  4242. }
  4243. ggml_vk_buffer_write(ctx, qx_buf, 0, qx, qx_sz);
  4244. ggml_vk_buffer_write(ctx, y_buf, 0, y, y_sz);
  4245. vk_context * subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4246. for (size_t i = 0; i < num_it; i++) {
  4247. ggml_vk_ctx_begin(ctx, subctx);
  4248. ggml_vk_matmul(
  4249. 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),
  4250. m, n, k, k, k, m, split_k, batch, batch, batch, 1, 1, k*m, k*n, m*n, 0, 0, 0, 0, 1
  4251. );
  4252. ggml_vk_ctx_end(subctx);
  4253. }
  4254. auto begin = std::chrono::high_resolution_clock::now();
  4255. ggml_vk_submit(subctx, ctx->fence);
  4256. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  4257. ctx->device->device.resetFences({ ctx->fence });
  4258. auto end = std::chrono::high_resolution_clock::now();
  4259. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  4260. ggml_vk_buffer_read(ctx, d_buf, 0, d, d_sz);
  4261. ggml_init_params iparams = {
  4262. /*.mem_size =*/ 1024*1024*1024,
  4263. /*.mem_buffer =*/ NULL,
  4264. /*.no_alloc =*/ true,
  4265. };
  4266. ggml_context * ggml_ctx = ggml_init(iparams);
  4267. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  4268. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  4269. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  4270. src0_ggml->data = qx;
  4271. src1_ggml->data = y;
  4272. tensor_ggml->data = d_chk;
  4273. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  4274. ggml_build_forward_expand(cgraph, tensor_ggml);
  4275. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  4276. ggml_free(ggml_ctx);
  4277. double avg_err = 0.0;
  4278. int first_err_n = -1;
  4279. int first_err_m = -1;
  4280. int first_err_b = -1;
  4281. for (size_t i = 0; i < m*n*batch; i++) {
  4282. double err = std::fabs(d[i] - d_chk[i]);
  4283. avg_err += err;
  4284. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  4285. first_err_b = i / (m * n);
  4286. first_err_n = (i % (m * n)) / m;
  4287. first_err_m = (i % (m * n)) % m;
  4288. }
  4289. }
  4290. avg_err /= m * n;
  4291. std::cerr << "TEST MMQ " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms avg_err=" << avg_err << std::endl;
  4292. if (avg_err > 0.01 || std::isnan(avg_err)) {
  4293. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  4294. std::cerr << "Actual result: " << std::endl << std::endl;
  4295. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4296. std::cerr << std::endl;
  4297. std::cerr << "Expected result: " << std::endl << std::endl;
  4298. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4299. if (split_k > 1) {
  4300. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  4301. ggml_vk_buffer_read(ctx, ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  4302. std::cerr << "d_buf0: " << std::endl << std::endl;
  4303. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4304. std::cerr << "d_buf1: " << std::endl << std::endl;
  4305. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  4306. std::cerr << "d_buf2: " << std::endl << std::endl;
  4307. 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);
  4308. std::cerr << "d_buf3: " << std::endl << std::endl;
  4309. 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);
  4310. free(split_k_buf);
  4311. }
  4312. }
  4313. ggml_vk_destroy_buffer(qx_buf);
  4314. ggml_vk_destroy_buffer(y_buf);
  4315. ggml_vk_destroy_buffer(d_buf);
  4316. free(x);
  4317. free(qx);
  4318. free(y);
  4319. free(d);
  4320. free(d_chk);
  4321. }
  4322. #endif
  4323. static ggml_tensor_extra_gpu * ggml_vk_tensor_create_extra(ggml_tensor * tensor) {
  4324. #ifdef GGML_VULKAN_DEBUG
  4325. std::cerr << "ggml_vk_create_extra(" << tensor << " (" << tensor->name << ", " << ggml_op_name(tensor->op) << "))" << std::endl;
  4326. #endif
  4327. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu;
  4328. extra->reset();
  4329. tensor->extra = extra;
  4330. return extra;
  4331. }
  4332. static void ggml_vk_preallocate_buffers_graph(ggml_backend_vk_context * ctx, ggml_tensor * node){
  4333. #ifdef GGML_VULKAN_DEBUG
  4334. std::cerr << "ggml_vk_preallocate_buffers_graph(" << node << ")" << std::endl;
  4335. #endif
  4336. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) node->extra;
  4337. if (extra == nullptr) {
  4338. return;
  4339. }
  4340. ggml_tensor * src0 = node->src[0];
  4341. ggml_tensor * src1 = node->src[1];
  4342. const bool use_src0 = src0 != nullptr;
  4343. const int64_t ne00 = use_src0 ? src0->ne[0] : 0;
  4344. const int64_t ne01 = use_src0 ? src0->ne[1] : 0;
  4345. const int64_t ne02 = use_src0 ? src0->ne[2] : 0;
  4346. const int64_t ne03 = use_src0 ? src0->ne[3] : 0;
  4347. const bool use_src1 = src1 != nullptr && node->op != GGML_OP_CPY && node->op != GGML_OP_CONT && node->op != GGML_OP_DUP;
  4348. const int64_t ne10 = use_src1 ? src1->ne[0] : 0;
  4349. const int64_t ne11 = use_src1 ? src1->ne[1] : 0;
  4350. const int64_t ne12 = use_src1 ? src1->ne[2] : 0;
  4351. const int64_t ne13 = use_src1 ? src1->ne[3] : 0;
  4352. const int64_t ne20 = node->ne[0];
  4353. const int64_t ne21 = node->ne[1];
  4354. const int64_t ne22 = node->ne[2];
  4355. const int64_t ne23 = node->ne[3];
  4356. const ggml_type src0_type = (use_src0 && src0->type == GGML_TYPE_F32) ? src0->type : GGML_TYPE_F16;
  4357. const ggml_type src1_type = (use_src1 && src1->type == GGML_TYPE_F32) ? src1->type : GGML_TYPE_F16;
  4358. const bool x_non_contig = use_src0 && !ggml_vk_dim01_contiguous(src0);
  4359. const bool y_non_contig = use_src1 && !ggml_vk_dim01_contiguous(src1);
  4360. const bool y_f32_kernel = use_src1 && src1->type == GGML_TYPE_F32 && !y_non_contig;
  4361. bool mmp = (use_src0 && use_src1 && src1_type == GGML_TYPE_F32) ? ggml_vk_get_mul_mat_mat_pipeline(ctx, src0_type, y_non_contig ? GGML_TYPE_F16 : src1->type) != nullptr : false;
  4362. const bool qx_needs_dequant = use_src0 && (mmp || x_non_contig);
  4363. const bool qy_needs_dequant = use_src1 && ((src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig);
  4364. int split_k;
  4365. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  4366. split_k = ggml_vk_guess_split_k(ne01, ne11, ne10);
  4367. } else {
  4368. split_k = 1;
  4369. }
  4370. const uint32_t x_ne = ne00 * ne01;
  4371. const uint32_t y_ne = ne10 * ne11;
  4372. const uint32_t d_ne = ne20 * ne21;
  4373. const uint64_t x_sz = (use_src0 && qx_needs_dequant) ? ggml_vk_align_size(sizeof(src0_type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ne02 * ne03 : 0;
  4374. const uint64_t y_sz = (use_src1 && qy_needs_dequant) ? ggml_vk_align_size(sizeof(src1_type) * y_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ne12 * ne13 : 0;
  4375. uint64_t d_sz = ggml_vk_align_size(ggml_type_size(node->type) * d_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ne22 * ne23;
  4376. const uint64_t split_k_size = split_k > 1 ? d_sz * 4 : 0;
  4377. if (extra->buffer_gpu.expired()) {
  4378. // Workaround for CPU backend BLAS matmul calls
  4379. extra->buffer_gpu = ggml_vk_create_buffer_temp(ctx, d_sz);
  4380. }
  4381. switch (node->op) {
  4382. case GGML_OP_REPEAT:
  4383. case GGML_OP_GET_ROWS:
  4384. case GGML_OP_RESHAPE:
  4385. case GGML_OP_VIEW:
  4386. case GGML_OP_PERMUTE:
  4387. case GGML_OP_TRANSPOSE:
  4388. case GGML_OP_ADD:
  4389. case GGML_OP_SCALE:
  4390. case GGML_OP_SQR:
  4391. case GGML_OP_CLAMP:
  4392. case GGML_OP_CPY:
  4393. case GGML_OP_CONT:
  4394. case GGML_OP_DUP:
  4395. case GGML_OP_MUL:
  4396. case GGML_OP_NORM:
  4397. case GGML_OP_RMS_NORM:
  4398. case GGML_OP_DIAG_MASK_INF:
  4399. case GGML_OP_SOFT_MAX:
  4400. case GGML_OP_ROPE:
  4401. case GGML_OP_ARGSORT:
  4402. break;
  4403. case GGML_OP_UNARY:
  4404. switch (ggml_get_unary_op(node)) {
  4405. case GGML_UNARY_OP_SILU:
  4406. case GGML_UNARY_OP_GELU:
  4407. case GGML_UNARY_OP_RELU:
  4408. break;
  4409. default:
  4410. return;
  4411. }
  4412. break;
  4413. case GGML_OP_MUL_MAT:
  4414. case GGML_OP_MUL_MAT_ID:
  4415. if (ctx->prealloc_size_x < x_sz) {
  4416. ctx->prealloc_size_x = x_sz;
  4417. }
  4418. if (ctx->prealloc_size_y < y_sz) {
  4419. ctx->prealloc_size_y = y_sz;
  4420. }
  4421. if (ctx->prealloc_size_split_k < split_k_size) {
  4422. ctx->prealloc_size_split_k = split_k_size;
  4423. }
  4424. if (ctx->staging_size < x_sz + y_sz) {
  4425. ctx->staging_size = x_sz + y_sz;
  4426. }
  4427. break;
  4428. default:
  4429. return;
  4430. }
  4431. }
  4432. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  4433. #ifdef GGML_VULKAN_DEBUG
  4434. std::cerr << "ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << " y_size: " << ctx->prealloc_size_y << " split_k_size: " << ctx->prealloc_size_split_k << ")" << std::endl;
  4435. #endif
  4436. #if defined(GGML_VULKAN_RUN_TESTS)
  4437. ctx->staging = ggml_vk_create_buffer_check(ctx, 100ul * 1024ul * 1024ul,
  4438. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4439. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4440. ggml_vk_test_transfer(ctx, 8192 * 1000, false);
  4441. ggml_vk_test_transfer(ctx, 8192 * 1000, true);
  4442. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_F32);
  4443. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q4_0);
  4444. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q4_1);
  4445. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q5_0);
  4446. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q5_1);
  4447. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q8_0);
  4448. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q2_K);
  4449. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q3_K);
  4450. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q4_K);
  4451. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q5_K);
  4452. ggml_vk_test_dequant(ctx, 7680, GGML_TYPE_Q6_K);
  4453. ggml_vk_test_matmul<ggml_fp16_t, ggml_fp16_t>(ctx, 512, 512, 100, 32, 100, 1, 2);
  4454. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 1, 0);
  4455. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 1, 1);
  4456. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 1, 2);
  4457. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 4, 0);
  4458. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 4, 1);
  4459. ggml_vk_test_matmul<float, float>(ctx, 128, 512, 512, 2, 100, 4, 2);
  4460. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q4_0);
  4461. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q4_0);
  4462. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q4_0);
  4463. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q4_0);
  4464. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q4_0);
  4465. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q4_0);
  4466. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q4_1);
  4467. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q4_1);
  4468. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q4_1);
  4469. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q4_1);
  4470. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q4_1);
  4471. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q4_1);
  4472. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q5_0);
  4473. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q5_0);
  4474. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q5_0);
  4475. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q5_0);
  4476. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q5_0);
  4477. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q5_0);
  4478. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q5_1);
  4479. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q5_1);
  4480. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q5_1);
  4481. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q5_1);
  4482. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q5_1);
  4483. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q5_1);
  4484. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q8_0);
  4485. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q8_0);
  4486. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q8_0);
  4487. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q8_0);
  4488. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q8_0);
  4489. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q8_0);
  4490. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q2_K);
  4491. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q2_K);
  4492. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q2_K);
  4493. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q2_K);
  4494. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q2_K);
  4495. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q2_K);
  4496. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q3_K);
  4497. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q3_K);
  4498. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q3_K);
  4499. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q3_K);
  4500. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q3_K);
  4501. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q3_K);
  4502. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q4_K);
  4503. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q4_K);
  4504. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q4_K);
  4505. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q4_K);
  4506. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q4_K);
  4507. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q4_K);
  4508. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q5_K);
  4509. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q5_K);
  4510. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q5_K);
  4511. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q5_K);
  4512. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q5_K);
  4513. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q5_K);
  4514. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 0, GGML_TYPE_Q6_K);
  4515. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 1, GGML_TYPE_Q6_K);
  4516. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 1, 2, GGML_TYPE_Q6_K);
  4517. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 0, GGML_TYPE_Q6_K);
  4518. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 1, GGML_TYPE_Q6_K);
  4519. ggml_vk_test_dequant_matmul(ctx, 128, 512, 512, 2, 100, 4, 2, GGML_TYPE_Q6_K);
  4520. std::cerr << std::endl;
  4521. const std::vector<size_t> vals {
  4522. 8, 8, 8,
  4523. 100, 46, 576,
  4524. 623, 111, 128,
  4525. 100, 46, 558,
  4526. 512, 1, 256,
  4527. 128, 110, 622,
  4528. 511, 511, 127,
  4529. 511, 511, 7,
  4530. 511, 511, 17,
  4531. 49, 49, 128,
  4532. 128, 49, 49,
  4533. 4096, 49, 4096,
  4534. 11008, 49, 4096,
  4535. 4096, 49, 11008,
  4536. 32000, 49, 4096,
  4537. 512, 512, 128,
  4538. 128, 512, 512,
  4539. 4096, 512, 4096,
  4540. 11008, 512, 4096,
  4541. 4096, 512, 11008,
  4542. 32000, 512, 4096,
  4543. };
  4544. const size_t num_it = 1;
  4545. for (size_t i = 0; i < vals.size(); i += 3) {
  4546. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  4547. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  4548. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  4549. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  4550. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  4551. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  4552. std::cerr << std::endl;
  4553. }
  4554. GGML_ASSERT(false);
  4555. #endif
  4556. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  4557. // Resize buffer
  4558. if (ctx->prealloc_x != nullptr) {
  4559. ggml_vk_destroy_buffer(ctx->prealloc_x);
  4560. }
  4561. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx, ctx->prealloc_size_x);
  4562. }
  4563. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  4564. // Resize buffer
  4565. if (ctx->prealloc_y != nullptr) {
  4566. ggml_vk_destroy_buffer(ctx->prealloc_y);
  4567. }
  4568. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx, ctx->prealloc_size_y);
  4569. }
  4570. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  4571. // Resize buffer
  4572. if (ctx->prealloc_split_k != nullptr) {
  4573. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  4574. }
  4575. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx, ctx->prealloc_size_split_k);
  4576. }
  4577. if (ctx->staging == nullptr || (ctx->staging_size > 0 && ctx->staging->size < ctx->staging_size)) {
  4578. // Resize buffer
  4579. if (ctx->staging != nullptr) {
  4580. ggml_vk_destroy_buffer(ctx->staging);
  4581. }
  4582. ctx->staging = ggml_vk_create_buffer_check(ctx, ctx->staging_size,
  4583. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  4584. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  4585. }
  4586. }
  4587. static void ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, bool last_node){
  4588. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) node->extra;
  4589. if (ggml_is_empty(node) || extra == nullptr) {
  4590. return;
  4591. }
  4592. #ifdef GGML_VULKAN_DEBUG
  4593. std::cerr << "ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")" << std::endl;
  4594. #endif
  4595. ctx->semaphore_idx = 0;
  4596. ctx->staging_offset = 0;
  4597. const ggml_tensor * src0 = node->src[0];
  4598. const ggml_tensor * src1 = node->src[1];
  4599. const ggml_tensor * src2 = node->src[2];
  4600. switch (node->op) {
  4601. case GGML_OP_UNARY:
  4602. switch (ggml_get_unary_op(node)) {
  4603. case GGML_UNARY_OP_SILU:
  4604. case GGML_UNARY_OP_GELU:
  4605. case GGML_UNARY_OP_RELU:
  4606. break;
  4607. default:
  4608. return;
  4609. }
  4610. break;
  4611. case GGML_OP_REPEAT:
  4612. case GGML_OP_GET_ROWS:
  4613. case GGML_OP_ADD:
  4614. case GGML_OP_MUL:
  4615. case GGML_OP_SCALE:
  4616. case GGML_OP_SQR:
  4617. case GGML_OP_CLAMP:
  4618. case GGML_OP_CPY:
  4619. case GGML_OP_CONT:
  4620. case GGML_OP_DUP:
  4621. case GGML_OP_RESHAPE:
  4622. case GGML_OP_VIEW:
  4623. case GGML_OP_PERMUTE:
  4624. case GGML_OP_TRANSPOSE:
  4625. case GGML_OP_NORM:
  4626. case GGML_OP_RMS_NORM:
  4627. case GGML_OP_DIAG_MASK_INF:
  4628. case GGML_OP_SOFT_MAX:
  4629. case GGML_OP_ROPE:
  4630. case GGML_OP_MUL_MAT:
  4631. case GGML_OP_MUL_MAT_ID:
  4632. case GGML_OP_NONE:
  4633. case GGML_OP_ARGSORT:
  4634. break;
  4635. default:
  4636. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  4637. GGML_ASSERT(false);
  4638. return;
  4639. }
  4640. if (ctx->compute_ctx == nullptr) {
  4641. ctx->compute_ctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  4642. ggml_vk_ctx_begin(ctx, ctx->compute_ctx);
  4643. }
  4644. switch (node->op) {
  4645. case GGML_OP_REPEAT:
  4646. ggml_vk_repeat(ctx, ctx->compute_ctx, src0, src1, node);
  4647. break;
  4648. case GGML_OP_GET_ROWS:
  4649. ggml_vk_get_rows(ctx, ctx->compute_ctx, src0, src1, node);
  4650. break;
  4651. case GGML_OP_ADD:
  4652. ggml_vk_add(ctx, ctx->compute_ctx, src0, src1, node);
  4653. break;
  4654. case GGML_OP_MUL:
  4655. ggml_vk_mul(ctx, ctx->compute_ctx, src0, src1, node);
  4656. break;
  4657. case GGML_OP_SCALE:
  4658. ggml_vk_scale(ctx, ctx->compute_ctx, src0, node);
  4659. break;
  4660. case GGML_OP_SQR:
  4661. ggml_vk_sqr(ctx, ctx->compute_ctx, src0, node);
  4662. break;
  4663. case GGML_OP_CLAMP:
  4664. ggml_vk_clamp(ctx, ctx->compute_ctx, src0, node);
  4665. break;
  4666. case GGML_OP_CPY:
  4667. case GGML_OP_CONT:
  4668. case GGML_OP_DUP:
  4669. ggml_vk_cpy(ctx, ctx->compute_ctx, src0, node);
  4670. break;
  4671. case GGML_OP_RESHAPE:
  4672. case GGML_OP_VIEW:
  4673. case GGML_OP_PERMUTE:
  4674. case GGML_OP_TRANSPOSE:
  4675. case GGML_OP_NONE:
  4676. break;
  4677. case GGML_OP_NORM:
  4678. ggml_vk_norm(ctx, ctx->compute_ctx, src0, node);
  4679. break;
  4680. case GGML_OP_RMS_NORM:
  4681. ggml_vk_rms_norm(ctx, ctx->compute_ctx, src0, node);
  4682. break;
  4683. case GGML_OP_UNARY:
  4684. switch (ggml_get_unary_op(node)) {
  4685. case GGML_UNARY_OP_SILU:
  4686. case GGML_UNARY_OP_GELU:
  4687. case GGML_UNARY_OP_RELU:
  4688. ggml_vk_unary(ctx, ctx->compute_ctx, src0, node);
  4689. break;
  4690. default:
  4691. return;
  4692. }
  4693. break;
  4694. case GGML_OP_DIAG_MASK_INF:
  4695. ggml_vk_diag_mask_inf(ctx, ctx->compute_ctx, src0, node);
  4696. break;
  4697. case GGML_OP_SOFT_MAX:
  4698. ggml_vk_soft_max(ctx, ctx->compute_ctx, src0, src1, node);
  4699. break;
  4700. case GGML_OP_ROPE:
  4701. ggml_vk_rope(ctx, ctx->compute_ctx, src0, src1, src2, node);
  4702. break;
  4703. case GGML_OP_ARGSORT:
  4704. ggml_vk_argsort(ctx, ctx->compute_ctx, src0, node);
  4705. break;
  4706. case GGML_OP_MUL_MAT:
  4707. ggml_vk_mul_mat(ctx, ctx->compute_ctx, src0, src1, node);
  4708. break;
  4709. case GGML_OP_MUL_MAT_ID:
  4710. //ggml_vk_mul_mat_id(ctx, ctx->compute_ctx, src0, src1, node);
  4711. std::cerr << "ggml_vulkan: GGML_OP_MUL_MAT_ID not implemented yet." << std::endl;
  4712. GGML_ASSERT(false);
  4713. break;
  4714. default:
  4715. return;
  4716. }
  4717. extra->ready = true;
  4718. extra->ctx_idx = ctx->compute_ctx->idx;
  4719. #ifdef GGML_VULKAN_CHECK_RESULTS
  4720. // Force context reset on each node so that each tensor ends up in its own context
  4721. // and can be run and compared to its CPU equivalent separately
  4722. last_node = true;
  4723. #endif
  4724. if (last_node) {
  4725. ggml_vk_ctx_end(ctx->compute_ctx);
  4726. ctx->compute_ctx->exit_tensor = node;
  4727. ctx->compute_ctx = nullptr;
  4728. }
  4729. }
  4730. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor){
  4731. ggml_tensor_extra_gpu * extra = nullptr;
  4732. switch (tensor->op) {
  4733. case GGML_OP_ADD:
  4734. case GGML_OP_GET_ROWS:
  4735. case GGML_OP_MUL:
  4736. case GGML_OP_SCALE:
  4737. case GGML_OP_SQR:
  4738. case GGML_OP_CLAMP:
  4739. case GGML_OP_CPY:
  4740. case GGML_OP_CONT:
  4741. case GGML_OP_DUP:
  4742. case GGML_OP_NORM:
  4743. case GGML_OP_RMS_NORM:
  4744. case GGML_OP_DIAG_MASK_INF:
  4745. case GGML_OP_SOFT_MAX:
  4746. case GGML_OP_ROPE:
  4747. case GGML_OP_RESHAPE:
  4748. case GGML_OP_VIEW:
  4749. case GGML_OP_PERMUTE:
  4750. case GGML_OP_TRANSPOSE:
  4751. case GGML_OP_NONE:
  4752. case GGML_OP_ARGSORT:
  4753. extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4754. break;
  4755. case GGML_OP_UNARY:
  4756. switch (ggml_get_unary_op(tensor)) {
  4757. case GGML_UNARY_OP_SILU:
  4758. case GGML_UNARY_OP_GELU:
  4759. case GGML_UNARY_OP_RELU:
  4760. extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4761. break;
  4762. default:
  4763. return false;
  4764. }
  4765. break;
  4766. case GGML_OP_MUL_MAT:
  4767. case GGML_OP_MUL_MAT_ID:
  4768. extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4769. break;
  4770. default:
  4771. return false;
  4772. }
  4773. if (extra == nullptr) {
  4774. return false;
  4775. }
  4776. if (params->ith != 0) {
  4777. return true;
  4778. }
  4779. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  4780. return true;
  4781. }
  4782. #ifdef GGML_VULKAN_DEBUG
  4783. std::cerr << "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 << ")" << std::endl;
  4784. #endif
  4785. #ifdef GGML_VULKAN_CHECK_RESULTS
  4786. ggml_vk_check_results_0(ctx, params, tensor);
  4787. #endif
  4788. GGML_ASSERT(extra->ready);
  4789. vk_context& subctx = ctx->gc.contexts[extra->ctx_idx];
  4790. // Only run if ctx hasn't been submitted yet
  4791. if (!subctx.seqs.empty()) {
  4792. // Do staging buffer copies
  4793. for (auto& cpy : subctx.in_memcpys) {
  4794. memcpy(cpy.dst, cpy.src, cpy.n);
  4795. }
  4796. ggml_vk_submit(&subctx, ctx->fence);
  4797. }
  4798. if (tensor == subctx.exit_tensor) {
  4799. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_compute_forward waitForFences");
  4800. ctx->device->device.resetFences({ ctx->fence });
  4801. // Do staging buffer copies
  4802. for (auto& cpy : subctx.out_memcpys) {
  4803. memcpy(cpy.dst, cpy.src, cpy.n);
  4804. }
  4805. subctx.in_memcpys.clear();
  4806. subctx.out_memcpys.clear();
  4807. }
  4808. extra->ready = false;
  4809. return true;
  4810. }
  4811. // Clean up after graph processing is done
  4812. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  4813. #ifdef GGML_VULKAN_DEBUG
  4814. std::cerr << "ggml_vk_graph_cleanup()" << std::endl;
  4815. #endif
  4816. for (auto& buffer : ctx->gc.temp_buffers) {
  4817. ggml_vk_pool_free(ctx, buffer);
  4818. }
  4819. ctx->gc.temp_buffers.clear();
  4820. for (auto& pipeline : ctx->device->pipelines) {
  4821. if (pipeline.expired()) {
  4822. continue;
  4823. }
  4824. vk_pipeline pl = pipeline.lock();
  4825. ggml_pipeline_cleanup(pl);
  4826. }
  4827. ggml_vk_queue_cleanup(ctx, ctx->device->compute_queue);
  4828. ggml_vk_queue_cleanup(ctx, ctx->device->transfer_queue);
  4829. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  4830. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  4831. }
  4832. ctx->gc.semaphores.clear();
  4833. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  4834. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  4835. }
  4836. ctx->gc.tl_semaphores.clear();
  4837. ctx->semaphore_idx = 0;
  4838. ctx->event_idx = 0;
  4839. for (auto& event : ctx->gc.events) {
  4840. ctx->device->device.resetEvent(event);
  4841. }
  4842. ctx->staging_offset = 0;
  4843. ctx->compute_ctx = nullptr;
  4844. ctx->transfer_ctx = nullptr;
  4845. ctx->gc.contexts.clear();
  4846. }
  4847. // Clean up on backend free
  4848. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  4849. #ifdef GGML_VULKAN_DEBUG
  4850. std::cerr << "ggml_vk_cleanup(" << ctx->idx << ")" << std::endl;
  4851. #endif
  4852. ggml_vk_graph_cleanup(ctx);
  4853. ggml_vk_destroy_buffer(ctx->prealloc_x);
  4854. ggml_vk_destroy_buffer(ctx->prealloc_y);
  4855. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  4856. ggml_vk_destroy_buffer(ctx->staging);
  4857. ggml_vk_destroy_buffer(ctx->sync_staging);
  4858. for (auto& buffer : ctx->buffer_pool) {
  4859. ggml_vk_destroy_buffer(buffer);
  4860. }
  4861. ctx->prealloc_size_x = 0;
  4862. ctx->prealloc_size_y = 0;
  4863. ctx->prealloc_size_split_k = 0;
  4864. ctx->staging_size = 0;
  4865. for (auto& event : ctx->gc.events) {
  4866. ctx->device->device.destroyEvent(event);
  4867. }
  4868. ctx->gc.events.clear();
  4869. ctx->device->device.destroyFence(ctx->fence);
  4870. }
  4871. GGML_CALL static int ggml_vk_get_device_count() {
  4872. ggml_vk_instance_init();
  4873. return vk_instance.device_indices.size();
  4874. }
  4875. GGML_CALL static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  4876. ggml_vk_instance_init();
  4877. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  4878. vk::PhysicalDeviceProperties props;
  4879. devices[device].getProperties(&props);
  4880. snprintf(description, description_size, "%s", props.deviceName.data());
  4881. }
  4882. // backend interface
  4883. #define UNUSED GGML_UNUSED
  4884. // device backend
  4885. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  4886. struct ggml_backend_vk_buffer_context {
  4887. ggml_backend_vk_context * ctx;
  4888. vk_buffer dev_buffer;
  4889. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  4890. size_t temp_tensor_extra_index = 0;
  4891. std::string name;
  4892. ggml_backend_vk_buffer_context(ggml_backend_vk_context * ctx, vk_buffer&& dev_buffer, std::string& name) :
  4893. ctx(ctx),
  4894. dev_buffer(dev_buffer),
  4895. name(name) {
  4896. }
  4897. ~ggml_backend_vk_buffer_context() {
  4898. ggml_vk_destroy_buffer(dev_buffer);
  4899. delete[] temp_tensor_extras;
  4900. }
  4901. ggml_tensor_extra_gpu * ggml_vk_alloc_temp_tensor_extra() {
  4902. if (temp_tensor_extras == nullptr) {
  4903. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_VK_MAX_NODES];
  4904. }
  4905. size_t alloc_index = temp_tensor_extra_index;
  4906. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_VK_MAX_NODES;
  4907. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  4908. extra->reset();
  4909. return extra;
  4910. }
  4911. };
  4912. GGML_CALL static const char * ggml_backend_vk_buffer_get_name(ggml_backend_buffer_t buffer) {
  4913. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4914. return ctx->name.c_str();
  4915. }
  4916. GGML_CALL static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  4917. return buffer->iface.get_name == ggml_backend_vk_buffer_get_name;
  4918. }
  4919. GGML_CALL static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  4920. #ifdef GGML_VULKAN_DEBUG
  4921. std::cerr << "ggml_backend_vk_buffer_free_buffer()" << std::endl;
  4922. #endif
  4923. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4924. ggml_vk_destroy_buffer(ctx->dev_buffer);
  4925. delete ctx;
  4926. }
  4927. GGML_CALL static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  4928. return vk_ptr_base;
  4929. UNUSED(buffer);
  4930. }
  4931. GGML_CALL static void ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  4932. #ifdef GGML_VULKAN_DEBUG
  4933. std::cerr << "ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")" << std::endl;
  4934. #endif
  4935. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4936. ggml_tensor_extra_gpu * extra = ctx->ggml_vk_alloc_temp_tensor_extra();
  4937. if (tensor->view_src != nullptr && tensor->view_src->extra != nullptr) {
  4938. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  4939. ggml_tensor_extra_gpu * extra_view = (ggml_tensor_extra_gpu *) tensor->view_src->extra;
  4940. extra->buffer_gpu = extra_view->buffer_gpu;
  4941. extra->offset = extra_view->offset + tensor->view_offs;
  4942. } else {
  4943. extra->buffer_gpu = ctx->dev_buffer;
  4944. extra->offset = (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  4945. }
  4946. tensor->extra = extra;
  4947. }
  4948. GGML_CALL 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) {
  4949. #ifdef GGML_VULKAN_DEBUG
  4950. std::cerr << "ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")" << std::endl;
  4951. #endif
  4952. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4953. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4954. vk_buffer buf = extra->buffer_gpu.lock();
  4955. ggml_vk_buffer_write(ctx->ctx, buf, extra->offset + offset, data, size);
  4956. }
  4957. GGML_CALL 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) {
  4958. #ifdef GGML_VULKAN_DEBUG
  4959. std::cerr << "ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")" << std::endl;
  4960. #endif
  4961. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4962. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  4963. vk_buffer buf = extra->buffer_gpu.lock();
  4964. ggml_vk_buffer_read(ctx->ctx, buf, extra->offset + offset, data, size);
  4965. }
  4966. GGML_CALL static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  4967. if (ggml_backend_buffer_is_vk(src->buffer)) {
  4968. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  4969. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  4970. vk_buffer src_buf = src_extra->buffer_gpu.lock();
  4971. vk_buffer dst_buf = dst_extra->buffer_gpu.lock();
  4972. ggml_vk_buffer_copy(dst_buf, dst_extra->offset, src_buf, src_extra->offset, ggml_nbytes(src));
  4973. return true;
  4974. }
  4975. return false;
  4976. UNUSED(buffer);
  4977. }
  4978. GGML_CALL static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  4979. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  4980. ggml_vk_buffer_memset(ctx->ctx, ctx->dev_buffer, 0, value, buffer->size);
  4981. }
  4982. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  4983. /* .get_name = */ ggml_backend_vk_buffer_get_name,
  4984. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  4985. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  4986. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  4987. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  4988. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  4989. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  4990. /* .clear = */ ggml_backend_vk_buffer_clear,
  4991. /* .reset = */ NULL,
  4992. };
  4993. // vk buffer type
  4994. struct ggml_backend_vk_buffer_type_context {
  4995. std::string name;
  4996. ggml_backend_vk_context * ctx;
  4997. };
  4998. GGML_CALL static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  4999. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  5000. return ctx->name.c_str();
  5001. }
  5002. GGML_CALL static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  5003. #ifdef GGML_VULKAN_DEBUG
  5004. std::cerr << "ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")" << std::endl;
  5005. #endif
  5006. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  5007. vk_buffer dev_buffer = ggml_vk_create_buffer_device(ctx->ctx, size);
  5008. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->ctx, std::move(dev_buffer), ctx->name);
  5009. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  5010. }
  5011. GGML_CALL static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  5012. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  5013. return ctx->ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  5014. }
  5015. GGML_CALL static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  5016. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  5017. return ctx->ctx->device->max_memory_allocation_size;
  5018. }
  5019. GGML_CALL static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  5020. return ggml_nbytes(tensor);
  5021. UNUSED(buft);
  5022. }
  5023. GGML_CALL static bool ggml_backend_vk_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  5024. if (!ggml_backend_is_vk(backend)) {
  5025. return false;
  5026. }
  5027. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  5028. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5029. return buft_ctx->ctx->idx == ctx->idx;
  5030. }
  5031. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  5032. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  5033. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  5034. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  5035. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  5036. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  5037. /* .supports_backend = */ ggml_backend_vk_buffer_type_supports_backend,
  5038. /* .is_host = */ NULL,
  5039. };
  5040. GGML_CALL ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  5041. ggml_vk_instance_init();
  5042. #ifdef GGML_VULKAN_DEBUG
  5043. std::cerr << "ggml_backend_vk_buffer_type(" << dev_num << ")" << std::endl;
  5044. #endif
  5045. GGML_ASSERT(dev_num < vk_instance.device_indices.size());
  5046. ggml_backend_vk_init(dev_num);
  5047. return &vk_instance.buffer_types[dev_num];
  5048. }
  5049. // host buffer type
  5050. GGML_CALL static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  5051. return GGML_VK_NAME "_Host";
  5052. UNUSED(buft);
  5053. }
  5054. GGML_CALL static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  5055. return GGML_VK_NAME "_Host";
  5056. UNUSED(buffer);
  5057. }
  5058. GGML_CALL static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  5059. #ifdef GGML_VULKAN_DEBUG
  5060. std::cerr << "ggml_backend_vk_host_buffer_free_buffer()" << std::endl;
  5061. #endif
  5062. ggml_vk_host_free(&vk_instance.contexts[0], buffer->context);
  5063. }
  5064. GGML_CALL static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  5065. #ifdef GGML_VULKAN_DEBUG
  5066. std::cerr << "ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")" << std::endl;
  5067. #endif
  5068. size += 32; // Behave like the CPU buffer type
  5069. void * ptr = nullptr;
  5070. try {
  5071. ptr = ggml_vk_host_malloc(&vk_instance.contexts[0], size);
  5072. } catch (vk::SystemError& e) {
  5073. std::cerr << "ggml_vulkan: Failed to allocate pinned memory." << std::endl;
  5074. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  5075. // fallback to cpu buffer
  5076. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  5077. }
  5078. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  5079. buffer->buft = buft;
  5080. buffer->iface.get_name = ggml_backend_vk_host_buffer_name;
  5081. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  5082. return buffer;
  5083. }
  5084. GGML_CALL static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  5085. return vk_instance.contexts[0].device->properties.limits.minMemoryMapAlignment;
  5086. UNUSED(buft);
  5087. }
  5088. GGML_CALL ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  5089. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  5090. /* .iface = */ {
  5091. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  5092. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  5093. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  5094. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  5095. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  5096. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  5097. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  5098. },
  5099. /* .context = */ nullptr,
  5100. };
  5101. if (!vk_instance.contexts[0].initialized) {
  5102. // Fall back to CPU
  5103. return ggml_backend_cpu_buffer_type();
  5104. }
  5105. return &ggml_backend_vk_buffer_type_host;
  5106. }
  5107. // backend
  5108. GGML_CALL static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  5109. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5110. return ctx->name.c_str();
  5111. }
  5112. GGML_CALL static void ggml_backend_vk_free(ggml_backend_t backend) {
  5113. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5114. #ifdef GGML_VULKAN_DEBUG
  5115. std::cerr << "ggml_backend_vk_free(" << ctx->name << ")" << std::endl;
  5116. #endif
  5117. size_t idx = ctx->idx;
  5118. ggml_vk_cleanup(ctx);
  5119. ctx->device.reset();
  5120. ctx->initialized = false;
  5121. vk_instance.initialized[idx] = false;
  5122. vk_instance.backends[idx] = nullptr;
  5123. memset(&vk_instance.buffer_types[idx], 0, sizeof(ggml_backend_buffer_type));
  5124. delete backend;
  5125. }
  5126. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  5127. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5128. GGML_ASSERT(ctx->initialized);
  5129. return ggml_backend_vk_buffer_type(ctx->idx);
  5130. }
  5131. GGML_CALL static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  5132. #ifdef GGML_VULKAN_DEBUG
  5133. std::cerr << "ggml_backend_vk_set_tensor_async(" << size << ")" << std::endl;
  5134. #endif
  5135. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5136. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_buffer_type(ctx->idx) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  5137. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5138. if (ctx->transfer_ctx == nullptr) {
  5139. // Initialize new transfer context
  5140. ctx->transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  5141. ggml_vk_ctx_begin(ctx, ctx->transfer_ctx);
  5142. }
  5143. vk_buffer buf = extra->buffer_gpu.lock();
  5144. ggml_vk_buffer_write_async(ctx, ctx->transfer_ctx, buf, extra->offset + offset, data, size);
  5145. }
  5146. GGML_CALL static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  5147. #ifdef GGML_VULKAN_DEBUG
  5148. std::cerr << "ggml_backend_vk_get_tensor_async(" << size << ")" << std::endl;
  5149. #endif
  5150. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5151. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_buffer_type(ctx->idx) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  5152. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5153. if (ctx->transfer_ctx == nullptr) {
  5154. // Initialize new transfer context
  5155. ctx->transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  5156. ggml_vk_ctx_begin(ctx, ctx->transfer_ctx);
  5157. }
  5158. vk_buffer buf = extra->buffer_gpu.lock();
  5159. ggml_vk_buffer_read_async(ctx, ctx->transfer_ctx, buf, extra->offset + offset, data, size);
  5160. }
  5161. GGML_CALL static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  5162. #ifdef GGML_VULKAN_DEBUG
  5163. std::cerr << "ggml_backend_vk_cpy_tensor_async()" << std::endl;
  5164. #endif
  5165. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5166. if ((dst->buffer->buft == ggml_backend_vk_buffer_type(ctx->idx) || dst->buffer->buft == ggml_backend_vk_host_buffer_type()) && ggml_backend_buffer_is_vk(src->buffer)) {
  5167. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  5168. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5169. if (ctx->transfer_ctx == nullptr) {
  5170. // Initialize new transfer context
  5171. ctx->transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  5172. ggml_vk_ctx_begin(ctx, ctx->transfer_ctx);
  5173. }
  5174. vk_buffer src_buf = src_extra->buffer_gpu.lock();
  5175. vk_buffer dst_buf = dst_extra->buffer_gpu.lock();
  5176. ggml_vk_buffer_copy_async(ctx->transfer_ctx, dst_buf, dst_extra->offset, src_buf, src_extra->offset, ggml_nbytes(src));
  5177. return true;
  5178. }
  5179. return false;
  5180. }
  5181. GGML_CALL static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  5182. #ifdef GGML_VULKAN_DEBUG
  5183. std::cerr << "ggml_backend_vk_synchronize()" << std::endl;
  5184. #endif
  5185. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5186. if(ctx->transfer_ctx == nullptr) {
  5187. return;
  5188. }
  5189. ggml_vk_ctx_end(ctx->transfer_ctx);
  5190. for (auto& cpy : ctx->transfer_ctx->in_memcpys) {
  5191. memcpy(cpy.dst, cpy.src, cpy.n);
  5192. }
  5193. ggml_vk_submit(ctx->transfer_ctx, ctx->fence);
  5194. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_backend_vk_synchronize waitForFences");
  5195. ctx->device->device.resetFences({ ctx->fence });
  5196. for (auto& cpy : ctx->transfer_ctx->out_memcpys) {
  5197. memcpy(cpy.dst, cpy.src, cpy.n);
  5198. }
  5199. ctx->transfer_ctx = nullptr;
  5200. }
  5201. static bool ggml_vk_is_empty(ggml_tensor * node) {
  5202. 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;
  5203. }
  5204. GGML_CALL static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  5205. #ifdef GGML_VULKAN_DEBUG
  5206. std::cerr << "ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)" << std::endl;
  5207. #endif
  5208. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  5209. for (int i = 0; i < cgraph->n_nodes; i++) {
  5210. ggml_vk_preallocate_buffers_graph(ctx, cgraph->nodes[i]);
  5211. }
  5212. ggml_vk_preallocate_buffers(ctx);
  5213. int last_node = cgraph->n_nodes - 1;
  5214. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  5215. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  5216. last_node -= 1;
  5217. }
  5218. for (int i = 0; i < cgraph->n_nodes; i++) {
  5219. ggml_vk_build_graph(ctx,cgraph->nodes[i], i == last_node);
  5220. }
  5221. ggml_compute_params params = {};
  5222. params.type = GGML_TASK_TYPE_COMPUTE;
  5223. params.ith = 0;
  5224. for (int i = 0; i < cgraph->n_nodes; i++) {
  5225. ggml_tensor * node = cgraph->nodes[i];
  5226. if (ggml_vk_is_empty(node)) {
  5227. continue;
  5228. }
  5229. bool ok = ggml_vk_compute_forward(ctx, &params, node);
  5230. if (!ok) {
  5231. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  5232. }
  5233. #ifdef GGML_VULKAN_CHECK_RESULTS
  5234. else {
  5235. ggml_vk_check_results_1(ctx, &params, node);
  5236. }
  5237. #endif
  5238. GGML_ASSERT(ok);
  5239. }
  5240. ggml_vk_graph_cleanup(ctx);
  5241. return GGML_STATUS_SUCCESS;
  5242. UNUSED(backend);
  5243. }
  5244. GGML_CALL static bool ggml_backend_vk_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  5245. // ggml_backend_vk_context * ctx = (ggml_backend_vk_context *) backend->context;
  5246. switch (op->op) {
  5247. case GGML_OP_UNARY:
  5248. switch (ggml_get_unary_op(op)) {
  5249. case GGML_UNARY_OP_GELU:
  5250. case GGML_UNARY_OP_SILU:
  5251. case GGML_UNARY_OP_RELU:
  5252. return true;
  5253. default:
  5254. return false;
  5255. }
  5256. break;
  5257. case GGML_OP_MUL_MAT:
  5258. // case GGML_OP_MUL_MAT_ID:
  5259. {
  5260. switch (op->src[0]->type) {
  5261. case GGML_TYPE_F32:
  5262. case GGML_TYPE_F16:
  5263. case GGML_TYPE_Q4_0:
  5264. case GGML_TYPE_Q4_1:
  5265. case GGML_TYPE_Q5_0:
  5266. case GGML_TYPE_Q5_1:
  5267. case GGML_TYPE_Q8_0:
  5268. case GGML_TYPE_Q2_K:
  5269. case GGML_TYPE_Q3_K:
  5270. case GGML_TYPE_Q4_K:
  5271. case GGML_TYPE_Q5_K:
  5272. case GGML_TYPE_Q6_K:
  5273. break;
  5274. default:
  5275. return false;
  5276. }
  5277. struct ggml_tensor * a;
  5278. struct ggml_tensor * b;
  5279. if (op->op == GGML_OP_MUL_MAT) {
  5280. a = op->src[0];
  5281. b = op->src[1];
  5282. } else {
  5283. a = op->src[2];
  5284. b = op->src[1];
  5285. }
  5286. if (a->ne[3] != b->ne[3]) {
  5287. return false;
  5288. }
  5289. return true;
  5290. } break;
  5291. case GGML_OP_GET_ROWS:
  5292. {
  5293. switch (op->src[0]->type) {
  5294. case GGML_TYPE_F32:
  5295. case GGML_TYPE_F16:
  5296. case GGML_TYPE_Q4_0:
  5297. case GGML_TYPE_Q4_1:
  5298. case GGML_TYPE_Q5_0:
  5299. case GGML_TYPE_Q5_1:
  5300. case GGML_TYPE_Q8_0:
  5301. return true;
  5302. default:
  5303. return false;
  5304. }
  5305. } break;
  5306. case GGML_OP_CPY:
  5307. case GGML_OP_DUP:
  5308. {
  5309. ggml_type src0_type = op->src[0]->type;
  5310. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  5311. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  5312. return true;
  5313. }
  5314. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  5315. return true;
  5316. }
  5317. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  5318. return true;
  5319. }
  5320. return false;
  5321. } break;
  5322. // case GGML_OP_REPEAT:
  5323. // {
  5324. // ggml_type src0_type = op->src[0]->type;
  5325. // return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  5326. // } break;
  5327. case GGML_OP_ROPE:
  5328. {
  5329. const int mode = ((const int32_t *) op->op_params)[2];
  5330. const bool is_glm = mode & 4;
  5331. return !is_glm;
  5332. } break;
  5333. case GGML_OP_NONE:
  5334. case GGML_OP_RESHAPE:
  5335. case GGML_OP_VIEW:
  5336. case GGML_OP_PERMUTE:
  5337. case GGML_OP_TRANSPOSE:
  5338. case GGML_OP_NORM:
  5339. case GGML_OP_ADD:
  5340. case GGML_OP_MUL:
  5341. case GGML_OP_RMS_NORM:
  5342. case GGML_OP_SCALE:
  5343. case GGML_OP_SQR:
  5344. case GGML_OP_CLAMP:
  5345. case GGML_OP_CONT:
  5346. case GGML_OP_DIAG_MASK_INF:
  5347. case GGML_OP_SOFT_MAX:
  5348. case GGML_OP_ARGSORT:
  5349. return true;
  5350. default:
  5351. return false;
  5352. }
  5353. UNUSED(backend);
  5354. }
  5355. GGML_CALL static bool ggml_backend_vk_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  5356. const ggml_tensor * dst = op;
  5357. const int min_batch_size = 32;
  5358. if (dst->ne[1] > min_batch_size && dst->op != GGML_OP_GET_ROWS) {
  5359. return true;
  5360. }
  5361. return false;
  5362. UNUSED(backend);
  5363. }
  5364. // TODO: enable async and synchronize
  5365. static ggml_backend_i ggml_backend_vk_interface = {
  5366. /* .get_name = */ ggml_backend_vk_name,
  5367. /* .free = */ ggml_backend_vk_free,
  5368. /* .get_default_buffer_type = */ ggml_backend_vk_get_default_buffer_type,
  5369. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  5370. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  5371. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  5372. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  5373. /* .graph_plan_create = */ NULL,
  5374. /* .graph_plan_free = */ NULL,
  5375. /* .graph_plan_compute = */ NULL,
  5376. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  5377. /* .supports_op = */ ggml_backend_vk_supports_op,
  5378. /* .offload_op = */ ggml_backend_vk_offload_op,
  5379. /* .event_new = */ NULL,
  5380. /* .event_free = */ NULL,
  5381. /* .event_record = */ NULL,
  5382. /* .event_wait = */ NULL,
  5383. /* .event_synchronize = */ NULL,
  5384. };
  5385. static ggml_guid_t ggml_backend_vk_guid() {
  5386. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  5387. return &guid;
  5388. }
  5389. GGML_CALL ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  5390. if (vk_instance.initialized[dev_num]) {
  5391. return vk_instance.backends[dev_num];
  5392. }
  5393. #ifdef GGML_VULKAN_DEBUG
  5394. std::cerr << "ggml_backend_vk_init(" << dev_num << ")" << std::endl;
  5395. #endif
  5396. ggml_backend_vk_context * ctx = &vk_instance.contexts[dev_num];
  5397. ggml_vk_init(ctx, dev_num);
  5398. ctx->name = GGML_VK_NAME + std::to_string(dev_num);
  5399. vk_instance.buffer_types[dev_num] = {
  5400. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  5401. /* .context = */ new ggml_backend_vk_buffer_type_context{ ctx->name, ctx },
  5402. };
  5403. vk_instance.initialized[dev_num] = true;
  5404. ggml_backend_t vk_backend = new ggml_backend {
  5405. /* .guid = */ ggml_backend_vk_guid(),
  5406. /* .interface = */ ggml_backend_vk_interface,
  5407. /* .context = */ &vk_instance.contexts[ctx->idx],
  5408. };
  5409. vk_instance.backends[dev_num] = vk_backend;
  5410. return vk_backend;
  5411. }
  5412. GGML_CALL bool ggml_backend_is_vk(ggml_backend_t backend) {
  5413. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  5414. }
  5415. GGML_CALL int ggml_backend_vk_get_device_count() {
  5416. return ggml_vk_get_device_count();
  5417. }
  5418. GGML_CALL void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  5419. ggml_vk_get_device_description(device, description, description_size);
  5420. }
  5421. GGML_CALL void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  5422. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  5423. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  5424. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  5425. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  5426. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  5427. *total = heap.size;
  5428. *free = heap.size;
  5429. break;
  5430. }
  5431. }
  5432. }
  5433. // backend registry
  5434. GGML_CALL static ggml_backend_t ggml_backend_reg_vk_init(const char * params, void * user_data) {
  5435. ggml_backend_t vk_backend = ggml_backend_vk_init((int) (intptr_t) user_data);
  5436. return vk_backend;
  5437. UNUSED(params);
  5438. }
  5439. extern "C" GGML_CALL int ggml_backend_vk_reg_devices();
  5440. GGML_CALL int ggml_backend_vk_reg_devices() {
  5441. ggml_vk_instance_init();
  5442. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  5443. char name[128];
  5444. snprintf(name, sizeof(name), "%s%ld", GGML_VK_NAME, i);
  5445. ggml_backend_register(name, ggml_backend_reg_vk_init, ggml_backend_vk_buffer_type(i), (void *) (intptr_t) i); // NOLINT
  5446. }
  5447. return vk_instance.device_indices.size();
  5448. }
  5449. // Extension availability
  5450. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  5451. #ifdef GGML_VULKAN_VALIDATE
  5452. bool portability_enumeration_ext = false;
  5453. // Check for portability enumeration extension for MoltenVK support
  5454. for (const auto& properties : instance_extensions) {
  5455. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  5456. return true;
  5457. }
  5458. }
  5459. if (!portability_enumeration_ext) {
  5460. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  5461. }
  5462. #endif
  5463. return false;
  5464. UNUSED(instance_extensions);
  5465. }
  5466. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  5467. #ifdef __APPLE__
  5468. bool portability_enumeration_ext = false;
  5469. // Check for portability enumeration extension for MoltenVK support
  5470. for (const auto& properties : instance_extensions) {
  5471. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  5472. return true;
  5473. }
  5474. }
  5475. if (!portability_enumeration_ext) {
  5476. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  5477. }
  5478. #endif
  5479. return false;
  5480. UNUSED(instance_extensions);
  5481. }
  5482. // checks
  5483. #ifdef GGML_VULKAN_CHECK_RESULTS
  5484. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  5485. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  5486. return;
  5487. }
  5488. for (int j = 0; j < level; j++) {
  5489. std::cerr << " ";
  5490. }
  5491. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  5492. done.push_back(tensor);
  5493. for (int i = 0; i < GGML_MAX_SRC; i++) {
  5494. if (tensor->src[i] != nullptr) {
  5495. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  5496. }
  5497. }
  5498. }
  5499. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  5500. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  5501. return;
  5502. }
  5503. i0 = std::max(i0, 5);
  5504. i1 = std::max(i1, 5);
  5505. i2 = std::max(i2, 0);
  5506. i3 = std::max(i3, 0);
  5507. fprintf(stderr, " ");
  5508. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5509. fprintf(stderr, "%7d ", idx1);
  5510. }
  5511. fprintf(stderr, "\n");
  5512. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  5513. fprintf(stderr, "%7d: ", idx0);
  5514. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5515. 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]) {
  5516. float val;
  5517. if (tensor->type == GGML_TYPE_F32) {
  5518. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  5519. } else if (tensor->type == GGML_TYPE_F16) {
  5520. 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]));
  5521. } else if (tensor->type == GGML_TYPE_I32) {
  5522. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  5523. } else {
  5524. GGML_ASSERT(false);
  5525. }
  5526. fprintf(stderr, "% 7.2f ", val);
  5527. } else {
  5528. fprintf(stderr, " ");
  5529. }
  5530. }
  5531. fprintf(stderr, "\n");
  5532. }
  5533. }
  5534. static void ggml_vk_print_tensor(ggml_backend_vk_context * ctx, const ggml_tensor * tensor, const char * name) {
  5535. void * tensor_data = tensor->data;
  5536. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5537. const size_t tensor_size = ggml_nbytes(tensor);
  5538. tensor_data = malloc(tensor_size);
  5539. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5540. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5541. ggml_vk_buffer_read(ctx, buffer_gpu, extra->offset, tensor_data, tensor_size);
  5542. }
  5543. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  5544. 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;
  5545. if (tensor->src[0] != nullptr) {
  5546. 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;
  5547. }
  5548. if (tensor->src[1] != nullptr) {
  5549. 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;
  5550. }
  5551. std::cerr << std::endl << "Result:" << std::endl;
  5552. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  5553. std::cerr << std::endl;
  5554. std::cerr << std::endl << "Result:" << std::endl;
  5555. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 1, 0);
  5556. std::cerr << std::endl;
  5557. std::vector<const ggml_tensor *> done;
  5558. ggml_vk_print_graph_origin(tensor, done);
  5559. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5560. free(tensor_data);
  5561. }
  5562. }
  5563. void * comp_result;
  5564. size_t comp_size;
  5565. size_t comp_nb[GGML_MAX_DIMS];
  5566. size_t check_counter = 0;
  5567. static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor) {
  5568. if (params->ith != 0) {
  5569. return;
  5570. }
  5571. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE || tensor->op == GGML_OP_TRANSPOSE) {
  5572. return;
  5573. }
  5574. check_counter++;
  5575. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  5576. return;
  5577. }
  5578. #ifdef GGML_VULKAN_DEBUG
  5579. std::cerr << "ggml_vk_check_results_0(" << tensor->name << ")" << std::endl;
  5580. #endif
  5581. ggml_tensor * src0 = tensor->src[0];
  5582. ggml_tensor * src1 = tensor->src[1];
  5583. ggml_tensor * src2 = tensor->src[2];
  5584. struct ggml_init_params iparams = {
  5585. /*.mem_size =*/ 1024*1024*1024,
  5586. /*.mem_buffer =*/ NULL,
  5587. /*.no_alloc =*/ false,
  5588. };
  5589. struct ggml_context * ggml_ctx = ggml_init(iparams);
  5590. struct ggml_tensor * src0_clone = nullptr;
  5591. struct ggml_tensor * src1_clone = nullptr;
  5592. struct ggml_tensor * src2_clone = nullptr;
  5593. struct ggml_tensor * tensor_clone = nullptr;
  5594. size_t src0_size;
  5595. size_t src1_size;
  5596. size_t src2_size;
  5597. void * src0_buffer;
  5598. void * src1_buffer;
  5599. void * src2_buffer;
  5600. if (src0 != nullptr) {
  5601. src0_clone = ggml_dup_tensor(ggml_ctx, src0);
  5602. src0_size = ggml_nbytes(src0);
  5603. src0_buffer = malloc(src0_size);
  5604. src0_clone->data = src0_buffer;
  5605. if (ggml_backend_buffer_is_host(src0->buffer)) {
  5606. memcpy(src0_clone->data, src0->data, src0_size);
  5607. memcpy(src0_clone->nb, src0->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5608. } else if (ggml_backend_buffer_is_vk(src0->buffer)) {
  5609. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src0->extra;
  5610. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5611. uint64_t offset = extra->offset;
  5612. if (!ggml_is_contiguous(src0) && ggml_vk_dim01_contiguous(src0)) {
  5613. for (int i3 = 0; i3 < src0->ne[3]; i3++) {
  5614. for (int i2 = 0; i2 < src0->ne[2]; i2++) {
  5615. const int idx = i3*src0->ne[2] + i2;
  5616. ggml_vk_buffer_read(ctx, buffer_gpu, offset + idx * src0->nb[2], ((char *)src0_clone->data + idx * src0_clone->nb[2]), src0->ne[1] * src0->nb[1]);
  5617. }
  5618. }
  5619. src0_clone->nb[0] = src0->nb[0];
  5620. src0_clone->nb[1] = src0->nb[1];
  5621. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  5622. src0_clone->nb[i] = src0_clone->nb[i - 1]*src0_clone->ne[i - 1];
  5623. }
  5624. } else {
  5625. if (offset + src0_size >= buffer_gpu->size) {
  5626. src0_size = buffer_gpu->size - offset;
  5627. }
  5628. ggml_vk_buffer_read(ctx, buffer_gpu, offset, src0_clone->data, src0_size);
  5629. memcpy(src0_clone->nb, src0->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5630. }
  5631. } else {
  5632. GGML_ASSERT(false);
  5633. }
  5634. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5635. ggml_vk_print_tensor(ctx, src0, "src0");
  5636. }
  5637. }
  5638. if (src1 != nullptr) {
  5639. src1_clone = ggml_dup_tensor(ggml_ctx, src1);
  5640. src1_size = ggml_nbytes(src1);
  5641. src1_buffer = malloc(src1_size);
  5642. src1_clone->data = src1_buffer;
  5643. if (ggml_backend_buffer_is_host(src1->buffer)) {
  5644. memcpy(src1_clone->data, src1->data, src1_size);
  5645. memcpy(src1_clone->nb, src1->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5646. } else if (ggml_backend_buffer_is_vk(src1->buffer)) {
  5647. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src1->extra;
  5648. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5649. uint64_t offset = extra->offset;
  5650. if (!ggml_is_contiguous(src1) && ggml_vk_dim01_contiguous(src1)) {
  5651. for (int i3 = 0; i3 < src1->ne[3]; i3++) {
  5652. for (int i2 = 0; i2 < src1->ne[2]; i2++) {
  5653. const int idx = i3*src1->ne[2] + i2;
  5654. ggml_vk_buffer_read(ctx, buffer_gpu, offset + idx * src1->nb[2], ((char *)src1_clone->data + idx * src1_clone->nb[2]), src1->ne[1] * src1->nb[1]);
  5655. }
  5656. }
  5657. src1_clone->nb[0] = src1->nb[0];
  5658. src1_clone->nb[1] = src1->nb[1];
  5659. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  5660. src1_clone->nb[i] = src1_clone->nb[i - 1]*src1_clone->ne[i - 1];
  5661. }
  5662. } else {
  5663. if (offset + src1_size >= buffer_gpu->size) {
  5664. src1_size = buffer_gpu->size - offset;
  5665. }
  5666. ggml_vk_buffer_read(ctx, buffer_gpu, offset, src1_clone->data, src1_size);
  5667. memcpy(src1_clone->nb, src1->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5668. }
  5669. } else {
  5670. GGML_ASSERT(false);
  5671. }
  5672. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5673. ggml_vk_print_tensor(ctx, src1, "src1");
  5674. std::cerr << "TENSOR CHECK: " << ggml_op_name(src1_clone->op) << " (check " << check_counter << ")" << std::endl;
  5675. std::cerr << "src1_clone=" << tensor << " src1_clone->type: " << ggml_type_name(src1_clone->type) << " ne0=" << src1_clone->ne[0] << " nb0=" << src1_clone->nb[0] << " ne1=" << src1_clone->ne[1] << " nb1=" << src1_clone->nb[1] << " ne2=" << src1_clone->ne[2] << " nb2=" << src1_clone->nb[2] << " ne3=" << src1_clone->ne[3] << " nb3=" << src1_clone->nb[3] << std::endl;
  5676. if (src1->src[0] != nullptr) {
  5677. std::cerr << "src1->src[0]=" << src1->src[0] << " op=" << ggml_op_name(src1->src[0]->op) << " type=" << ggml_type_name(src1->src[0]->type) << " ne0=" << src1->src[0]->ne[0] << " nb0=" << src1->src[0]->nb[0] << " ne1=" << src1->src[0]->ne[1] << " nb1=" << src1->src[0]->nb[1] << " ne2=" << src1->src[0]->ne[2] << " nb2=" << src1->src[0]->nb[2] << " ne3=" << src1->src[0]->ne[3] << " nb3=" << src1->src[0]->nb[3] << std::endl;
  5678. }
  5679. if (src1->src[1] != nullptr) {
  5680. std::cerr << "src1->src[1]=" << src1->src[1] << " op=" << ggml_op_name(src1->src[1]->op) << " type=" << ggml_type_name(src1->src[1]->type) << " ne0=" << src1->src[1]->ne[0] << " nb0=" << src1->src[1]->nb[0] << " ne1=" << src1->src[1]->ne[1] << " nb1=" << src1->src[1]->nb[1] << " ne2=" << src1->src[1]->ne[2] << " nb2=" << src1->src[1]->nb[2] << " ne3=" << src1->src[1]->ne[3] << " nb3=" << src1->src[1]->nb[3] << std::endl;
  5681. }
  5682. std::cerr << std::endl << "Result:" << std::endl;
  5683. ggml_vk_print_tensor_area(src1_clone, src1_clone->data, 5, 5, 0, 0);
  5684. std::cerr << std::endl;
  5685. std::cerr << std::endl << "Result:" << std::endl;
  5686. ggml_vk_print_tensor_area(src1_clone, src1_clone->data, 5, 5, 1, 0);
  5687. std::cerr << std::endl;
  5688. std::vector<const ggml_tensor *> done;
  5689. ggml_vk_print_graph_origin(src1_clone, done);
  5690. }
  5691. }
  5692. if (src2 != nullptr) {
  5693. src2_clone = ggml_dup_tensor(ggml_ctx, src2);
  5694. src2_size = ggml_nbytes(src2);
  5695. src2_buffer = malloc(src2_size);
  5696. src2_clone->data = src2_buffer;
  5697. if (ggml_backend_buffer_is_host(src2->buffer)) {
  5698. memcpy(src2_clone->data, src2->data, src2_size);
  5699. memcpy(src2_clone->nb, src2->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5700. } else if (ggml_backend_buffer_is_vk(src2->buffer)) {
  5701. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src2->extra;
  5702. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5703. uint64_t offset = extra->offset;
  5704. if (!ggml_is_contiguous(src2) && ggml_vk_dim01_contiguous(src2)) {
  5705. for (int i3 = 0; i3 < src2->ne[3]; i3++) {
  5706. for (int i2 = 0; i2 < src2->ne[2]; i2++) {
  5707. const int idx = i3*src2->ne[2] + i2;
  5708. ggml_vk_buffer_read(ctx, buffer_gpu, offset + idx * src2->nb[2], ((char *)src2_clone->data + idx * src2_clone->nb[2]), src2->ne[1] * src2->nb[1]);
  5709. }
  5710. }
  5711. src2_clone->nb[0] = src2->nb[0];
  5712. src2_clone->nb[1] = src2->nb[1];
  5713. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  5714. src2_clone->nb[i] = src2_clone->nb[i - 1]*src2_clone->ne[i - 1];
  5715. }
  5716. } else {
  5717. if (offset + src2_size >= buffer_gpu->size) {
  5718. src2_size = buffer_gpu->size - offset;
  5719. }
  5720. ggml_vk_buffer_read(ctx, buffer_gpu, offset, src2_clone->data, src2_size);
  5721. memcpy(src2_clone->nb, src2->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5722. }
  5723. } else {
  5724. GGML_ASSERT(false);
  5725. }
  5726. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5727. ggml_vk_print_tensor(ctx, src2, "src2");
  5728. std::cerr << "TENSOR CHECK: " << ggml_op_name(src2_clone->op) << " (check " << check_counter << ")" << std::endl;
  5729. std::cerr << "src2_clone=" << tensor << " src2_clone->type: " << ggml_type_name(src2_clone->type) << " ne0=" << src2_clone->ne[0] << " nb0=" << src2_clone->nb[0] << " ne1=" << src2_clone->ne[1] << " nb1=" << src2_clone->nb[1] << " ne2=" << src2_clone->ne[2] << " nb2=" << src2_clone->nb[2] << " ne3=" << src2_clone->ne[3] << " nb3=" << src2_clone->nb[3] << std::endl;
  5730. if (src2->src[0] != nullptr) {
  5731. std::cerr << "src2->src[0]=" << src2->src[0] << " op=" << ggml_op_name(src2->src[0]->op) << " type=" << ggml_type_name(src2->src[0]->type) << " ne0=" << src2->src[0]->ne[0] << " nb0=" << src2->src[0]->nb[0] << " ne1=" << src2->src[0]->ne[1] << " nb1=" << src2->src[0]->nb[1] << " ne2=" << src2->src[0]->ne[2] << " nb2=" << src2->src[0]->nb[2] << " ne3=" << src2->src[0]->ne[3] << " nb3=" << src2->src[0]->nb[3] << std::endl;
  5732. }
  5733. if (src2->src[1] != nullptr) {
  5734. std::cerr << "src2->src[1]=" << src2->src[1] << " op=" << ggml_op_name(src2->src[1]->op) << " type=" << ggml_type_name(src2->src[1]->type) << " ne0=" << src2->src[1]->ne[0] << " nb0=" << src2->src[1]->nb[0] << " ne1=" << src2->src[1]->ne[1] << " nb1=" << src2->src[1]->nb[1] << " ne2=" << src2->src[1]->ne[2] << " nb2=" << src2->src[1]->nb[2] << " ne3=" << src2->src[1]->ne[3] << " nb3=" << src2->src[1]->nb[3] << std::endl;
  5735. }
  5736. std::cerr << std::endl << "Result:" << std::endl;
  5737. ggml_vk_print_tensor_area(src2_clone, src2_clone->data, 5, 5, 0, 0);
  5738. std::cerr << std::endl;
  5739. std::cerr << std::endl << "Result:" << std::endl;
  5740. ggml_vk_print_tensor_area(src2_clone, src2_clone->data, 5, 5, 1, 0);
  5741. std::cerr << std::endl;
  5742. std::vector<const ggml_tensor *> done;
  5743. ggml_vk_print_graph_origin(src2_clone, done);
  5744. }
  5745. }
  5746. if (tensor->op == GGML_OP_MUL_MAT) {
  5747. tensor_clone = ggml_mul_mat(ggml_ctx, src0_clone, src1_clone);
  5748. } else if (tensor->op == GGML_OP_MUL) {
  5749. tensor_clone = ggml_mul(ggml_ctx, src0_clone, src1_clone);
  5750. } else if (tensor->op == GGML_OP_SCALE) {
  5751. tensor_clone = ggml_scale(ggml_ctx, src0_clone, ((float *)tensor->op_params)[0]);
  5752. } else if (tensor->op == GGML_OP_SQR) {
  5753. tensor_clone = ggml_sqr(ggml_ctx, src0_clone);
  5754. } else if (tensor->op == GGML_OP_CLAMP) {
  5755. tensor_clone = ggml_clamp(ggml_ctx, src0_clone, ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  5756. } else if (tensor->op == GGML_OP_ADD) {
  5757. tensor_clone = ggml_add(ggml_ctx, src0_clone, src1_clone);
  5758. } else if (tensor->op == GGML_OP_NORM) {
  5759. tensor_clone = ggml_norm(ggml_ctx, src0_clone, *(float *)tensor->op_params);
  5760. } else if (tensor->op == GGML_OP_RMS_NORM) {
  5761. tensor_clone = ggml_rms_norm(ggml_ctx, src0_clone, *(float *)tensor->op_params);
  5762. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  5763. if (src1 != nullptr) {
  5764. tensor_clone = ggml_soft_max_ext(ggml_ctx, src0_clone, src1_clone, ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  5765. } else {
  5766. tensor_clone = ggml_soft_max(ggml_ctx, src0_clone);
  5767. }
  5768. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  5769. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src0_clone, *(int *)tensor->op_params);
  5770. } else if (tensor->op == GGML_OP_ROPE) {
  5771. const int n_dims = ((int32_t *) tensor->op_params)[1];
  5772. const int mode = ((int32_t *) tensor->op_params)[2];
  5773. const int n_ggml_ctx = ((int32_t *) tensor->op_params)[3];
  5774. const int n_orig_ggml_ctx = ((int32_t *) tensor->op_params)[4];
  5775. float freq_base = ((float *) tensor->op_params)[5];
  5776. float freq_scale = ((float *) tensor->op_params)[6];
  5777. float ext_factor = ((float *) tensor->op_params)[7];
  5778. float attn_factor = ((float *) tensor->op_params)[8];
  5779. float beta_fast = ((float *) tensor->op_params)[9];
  5780. float beta_slow = ((float *) tensor->op_params)[10];
  5781. tensor_clone = ggml_rope_ext(ggml_ctx, src0_clone, src1_clone, src2_clone, n_dims, mode, n_ggml_ctx, n_orig_ggml_ctx, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  5782. } else if (tensor->op == GGML_OP_UNARY) {
  5783. switch (ggml_get_unary_op(tensor)) {
  5784. case GGML_UNARY_OP_SILU:
  5785. tensor_clone = ggml_silu(ggml_ctx, src0_clone);
  5786. break;
  5787. case GGML_UNARY_OP_GELU:
  5788. tensor_clone = ggml_gelu(ggml_ctx, src0_clone);
  5789. break;
  5790. case GGML_UNARY_OP_RELU:
  5791. tensor_clone = ggml_relu(ggml_ctx, src0_clone);
  5792. break;
  5793. default:
  5794. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  5795. GGML_ASSERT(false);
  5796. }
  5797. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  5798. if (src1 == nullptr) {
  5799. tensor_clone = ggml_dup(ggml_ctx, src0_clone);
  5800. tensor_clone->type = tensor->type;
  5801. } else {
  5802. tensor_clone = ggml_cpy(ggml_ctx, src0_clone, src1_clone);
  5803. }
  5804. } else if (tensor->op == GGML_OP_CONT) {
  5805. tensor_clone = ggml_cont_4d(ggml_ctx, src0_clone, tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  5806. } else if (tensor->op == GGML_OP_RESHAPE) {
  5807. tensor_clone = ggml_reshape_4d(ggml_ctx, src0_clone, tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  5808. } else if (tensor->op == GGML_OP_VIEW) {
  5809. tensor_clone = ggml_view_4d(ggml_ctx, src0_clone, 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]);
  5810. } else if (tensor->op == GGML_OP_PERMUTE) {
  5811. int32_t * params = (int32_t *)tensor->op_params;
  5812. tensor_clone = ggml_permute(ggml_ctx, src0_clone, params[0], params[1], params[2], params[3]);
  5813. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  5814. tensor_clone = ggml_transpose(ggml_ctx, src0_clone);
  5815. } else if (tensor->op == GGML_OP_GET_ROWS) {
  5816. tensor_clone = ggml_get_rows(ggml_ctx, src0_clone, src1_clone);
  5817. } else if (tensor->op == GGML_OP_ARGSORT) {
  5818. tensor_clone = ggml_argsort(ggml_ctx, src0_clone, (ggml_sort_order) *(int *)tensor->op_params);
  5819. } else {
  5820. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  5821. GGML_ASSERT(false);
  5822. }
  5823. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  5824. ggml_build_forward_expand(cgraph, tensor_clone);
  5825. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  5826. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5827. ggml_vk_print_tensor(ctx, tensor_clone, "tensor_clone");
  5828. }
  5829. comp_size = ggml_nbytes(tensor_clone);
  5830. comp_result = malloc(comp_size);
  5831. memcpy(comp_result, tensor_clone->data, comp_size);
  5832. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  5833. if (src0 != nullptr) {
  5834. free(src0_buffer);
  5835. }
  5836. if (src1 != nullptr) {
  5837. free(src1_buffer);
  5838. }
  5839. ggml_free(ggml_ctx);
  5840. }
  5841. static void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_compute_params * params, ggml_tensor * tensor) {
  5842. if (params->ith != 0) {
  5843. return;
  5844. }
  5845. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE || tensor->op == GGML_OP_TRANSPOSE) {
  5846. return;
  5847. }
  5848. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  5849. return;
  5850. }
  5851. #ifdef GGML_VULKAN_DEBUG
  5852. std::cerr << "ggml_vk_check_results_1(" << tensor->name << ")" << std::endl;
  5853. #endif
  5854. ggml_tensor * src0 = tensor->src[0];
  5855. ggml_tensor * src1 = tensor->src[1];
  5856. void * tensor_data = tensor->data;
  5857. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5858. size_t tensor_size = ggml_nbytes(tensor);
  5859. tensor_data = malloc(tensor_size);
  5860. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5861. vk_buffer buffer_gpu = extra->buffer_gpu.lock();
  5862. if (extra->offset + tensor_size >= buffer_gpu->size) {
  5863. tensor_size = buffer_gpu->size - (extra->offset);
  5864. }
  5865. ggml_vk_buffer_read(ctx, buffer_gpu, extra->offset, tensor_data, tensor_size);
  5866. }
  5867. float first_error_result = -1.0f;
  5868. float first_error_correct = -1.0f;
  5869. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  5870. double avg_err = 0.0;
  5871. size_t counter = 0;
  5872. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  5873. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  5874. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  5875. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  5876. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  5877. float correct = 0.0f;
  5878. float result = 0.0f;
  5879. if (buffer_size_fit) {
  5880. if (tensor->type == GGML_TYPE_F32) {
  5881. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  5882. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  5883. } else if (tensor->type == GGML_TYPE_F16) {
  5884. 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]));
  5885. 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]));
  5886. } else if (tensor->type == GGML_TYPE_I32) {
  5887. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  5888. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  5889. } else {
  5890. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  5891. }
  5892. } else {
  5893. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  5894. GGML_ASSERT(false);
  5895. }
  5896. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  5897. 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;
  5898. 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;
  5899. if (src0 != nullptr) {
  5900. 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;
  5901. }
  5902. if (src1 != nullptr) {
  5903. 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;
  5904. }
  5905. 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;
  5906. std::cerr << std::endl << "Result:" << std::endl;
  5907. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  5908. std::cerr << std::endl << "Correct:" << std::endl;
  5909. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  5910. std::cerr << std::endl;
  5911. std::vector<const ggml_tensor *> done;
  5912. ggml_vk_print_graph_origin(tensor, done);
  5913. GGML_ASSERT(false);
  5914. }
  5915. if (first_error[0] == -1 && std::fabs(correct - result) > 0.1f) {
  5916. first_error[0] = i0;
  5917. first_error[1] = i1;
  5918. first_error[2] = i2;
  5919. first_error[3] = i3;
  5920. first_error_result = result;
  5921. first_error_correct = correct;
  5922. }
  5923. // Special case, value is infinite, avoid NaN result in avg_err
  5924. // NaN also appears in results, if both are nan error is 0
  5925. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  5926. avg_err += std::fabs(correct - result);
  5927. }
  5928. counter++;
  5929. }
  5930. }
  5931. }
  5932. }
  5933. avg_err /= counter;
  5934. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  5935. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  5936. 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;
  5937. if (src0 != nullptr) {
  5938. 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;
  5939. }
  5940. if (src1 != nullptr) {
  5941. 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;
  5942. }
  5943. 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;
  5944. std::cerr << std::endl << "Result:" << std::endl;
  5945. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  5946. std::cerr << std::endl << "Correct:" << std::endl;
  5947. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  5948. std::cerr << std::endl;
  5949. std::cerr << std::endl << "Result:" << std::endl;
  5950. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 1, 0);
  5951. std::cerr << std::endl << "Correct:" << std::endl;
  5952. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 1, 0);
  5953. std::cerr << std::endl;
  5954. std::vector<const ggml_tensor *> done;
  5955. ggml_vk_print_graph_origin(tensor, done);
  5956. }
  5957. if (avg_err > 0.05 || std::isnan(avg_err)) {
  5958. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  5959. 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;
  5960. if (src0 != nullptr) {
  5961. 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;
  5962. }
  5963. if (src1 != nullptr) {
  5964. 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;
  5965. }
  5966. 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;
  5967. std::cerr << std::endl << "Result:" << std::endl;
  5968. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  5969. std::cerr << std::endl << "Correct:" << std::endl;
  5970. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  5971. std::cerr << std::endl;
  5972. std::vector<const ggml_tensor *> done;
  5973. ggml_vk_print_graph_origin(tensor, done);
  5974. GGML_ASSERT(false);
  5975. } else {
  5976. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  5977. }
  5978. free(comp_result);
  5979. comp_result = nullptr;
  5980. comp_size = 0;
  5981. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  5982. free(tensor_data);
  5983. }
  5984. }
  5985. #endif