ggml-vulkan.cpp 381 KB

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