ggml-vulkan.cpp 510 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010
  1. #include "ggml-vulkan.h"
  2. #include <vulkan/vulkan_core.h>
  3. #if defined(GGML_VULKAN_RUN_TESTS) || defined(GGML_VULKAN_PERF) || defined(GGML_VULKAN_CHECK_RESULTS)
  4. #include <chrono>
  5. #include "ggml-cpu.h"
  6. #endif
  7. #include <vulkan/vulkan.hpp>
  8. #include <algorithm>
  9. #include <cmath>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <tuple>
  13. #include <vector>
  14. #include <sstream>
  15. #include <utility>
  16. #include <memory>
  17. #include <limits>
  18. #include <map>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <future>
  23. #include <thread>
  24. #include "ggml-impl.h"
  25. #include "ggml-backend-impl.h"
  26. #include "ggml-vulkan-shaders.hpp"
  27. #define ROUNDUP_POW2(M, N) (((M) + (N) - 1) & ~((N) - 1))
  28. #define CEIL_DIV(M, N) (((M) + (N)-1) / (N))
  29. #define VK_VENDOR_ID_AMD 0x1002
  30. #define VK_VENDOR_ID_APPLE 0x106b
  31. #define VK_VENDOR_ID_INTEL 0x8086
  32. #define VK_VENDOR_ID_NVIDIA 0x10de
  33. #define VK_DEVICE_DESCRIPTOR_POOL_SIZE 32
  34. #define GGML_VK_MAX_NODES 8192
  35. #define MAX_VK_BUFFERS 256
  36. #define VK_CHECK(err, msg) \
  37. do { \
  38. vk::Result err_ = (err); \
  39. if (err_ != vk::Result::eSuccess) { \
  40. fprintf(stderr, "ggml_vulkan: %s error %s at %s:%d\n", \
  41. #err, to_string(err_).c_str(), __FILE__, __LINE__); \
  42. exit(1); \
  43. } \
  44. } while (0)
  45. #ifdef GGML_VULKAN_DEBUG
  46. #define VK_LOG_DEBUG(msg) std::cerr << msg << std::endl
  47. #else
  48. #define VK_LOG_DEBUG(msg) ((void) 0)
  49. #endif // GGML_VULKAN_DEBUG
  50. struct ggml_backend_vk_context;
  51. struct vk_queue {
  52. uint32_t queue_family_index;
  53. vk::Queue queue;
  54. vk::CommandPool pool;
  55. uint32_t cmd_buffer_idx;
  56. std::vector<vk::CommandBuffer> cmd_buffers;
  57. vk::PipelineStageFlags stage_flags;
  58. bool transfer_only;
  59. };
  60. struct vk_pipeline_struct {
  61. std::string name;
  62. vk::ShaderModule shader_module;
  63. vk::DescriptorSetLayout dsl;
  64. std::vector<vk::DescriptorPool> descriptor_pools;
  65. std::vector<vk::DescriptorSet> descriptor_sets;
  66. uint32_t descriptor_set_idx;
  67. vk::PipelineLayout layout;
  68. vk::Pipeline pipeline;
  69. uint32_t push_constant_size;
  70. uint32_t parameter_count;
  71. std::array<uint32_t, 3> wg_denoms;
  72. uint32_t align;
  73. // set to true to request the pipeline is compiled after the dryrun
  74. bool needed {};
  75. // set to true when the shader has been compiled
  76. bool compiled {};
  77. };
  78. typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
  79. typedef std::weak_ptr<vk_pipeline_struct> vk_pipeline_ref;
  80. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
  81. struct vk_matmul_pipeline_struct {
  82. vk_pipeline l, m, s;
  83. vk_pipeline a_l, a_m, a_s;
  84. };
  85. typedef std::shared_ptr<vk_matmul_pipeline_struct> vk_matmul_pipeline;
  86. struct vk_matmul_pipeline2 {
  87. vk_matmul_pipeline2() {
  88. f16acc = std::make_shared<vk_matmul_pipeline_struct>();
  89. f32acc = std::make_shared<vk_matmul_pipeline_struct>();
  90. }
  91. vk_matmul_pipeline f32acc;
  92. vk_matmul_pipeline f16acc;
  93. };
  94. struct vk_device_struct;
  95. typedef std::shared_ptr<vk_device_struct> vk_device;
  96. typedef std::weak_ptr<vk_device_struct> vk_device_ref;
  97. struct vk_buffer_struct;
  98. typedef std::shared_ptr<vk_buffer_struct> vk_buffer;
  99. typedef std::weak_ptr<vk_buffer_struct> vk_buffer_ref;
  100. struct ggml_backend_vk_buffer_type_context {
  101. std::string name;
  102. vk_device device;
  103. };
  104. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft);
  105. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size);
  106. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft);
  107. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft);
  108. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor);
  109. static ggml_backend_buffer_type_i ggml_backend_vk_buffer_type_interface = {
  110. /* .get_name = */ ggml_backend_vk_buffer_type_name,
  111. /* .alloc_buffer = */ ggml_backend_vk_buffer_type_alloc_buffer,
  112. /* .get_alignment = */ ggml_backend_vk_buffer_type_get_alignment,
  113. /* .get_max_size = */ ggml_backend_vk_buffer_type_get_max_size,
  114. /* .get_alloc_size = */ ggml_backend_vk_buffer_type_get_alloc_size,
  115. /* .is_host = */ NULL,
  116. };
  117. #ifdef GGML_VULKAN_MEMORY_DEBUG
  118. class vk_memory_logger;
  119. #endif
  120. #ifdef GGML_VULKAN_PERF
  121. class vk_perf_logger;
  122. #endif
  123. static void ggml_vk_destroy_buffer(vk_buffer& buf);
  124. static constexpr uint32_t mul_mat_vec_max_cols = 8;
  125. static constexpr uint32_t p021_max_gqa_ratio = 8;
  126. enum vk_device_architecture {
  127. OTHER,
  128. AMD_GCN,
  129. AMD_RDNA1,
  130. AMD_RDNA2,
  131. AMD_RDNA3,
  132. };
  133. static vk_device_architecture get_device_architecture(const vk::PhysicalDevice& device) {
  134. vk::PhysicalDeviceProperties props = device.getProperties();
  135. if (props.vendorID == VK_VENDOR_ID_AMD) {
  136. const std::vector<vk::ExtensionProperties> ext_props = device.enumerateDeviceExtensionProperties();
  137. bool amd_shader_core_properties = false;
  138. bool integer_dot_product = false;
  139. bool subgroup_size_control = false;
  140. for (const auto& properties : ext_props) {
  141. if (strcmp("VK_AMD_shader_core_properties", properties.extensionName) == 0) {
  142. amd_shader_core_properties = true;
  143. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0) {
  144. integer_dot_product = true;
  145. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  146. subgroup_size_control = true;
  147. }
  148. }
  149. if (!amd_shader_core_properties || !integer_dot_product || !subgroup_size_control) {
  150. return vk_device_architecture::OTHER;
  151. }
  152. vk::PhysicalDeviceProperties2 props2;
  153. vk::PhysicalDeviceShaderCorePropertiesAMD shader_core_props_amd;
  154. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR integer_dot_props;
  155. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  156. props2.pNext = &shader_core_props_amd;
  157. shader_core_props_amd.pNext = &integer_dot_props;
  158. integer_dot_props.pNext = &subgroup_size_control_props;
  159. device.getProperties2(&props2);
  160. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 64) {
  161. return vk_device_architecture::AMD_GCN;
  162. }
  163. if (subgroup_size_control_props.maxSubgroupSize == 64 && subgroup_size_control_props.minSubgroupSize == 32) {
  164. // RDNA
  165. if (shader_core_props_amd.wavefrontsPerSimd == 20) {
  166. return vk_device_architecture::AMD_RDNA1;
  167. }
  168. if (integer_dot_props.integerDotProduct4x8BitPackedMixedSignednessAccelerated) {
  169. return vk_device_architecture::AMD_RDNA3;
  170. }
  171. return vk_device_architecture::AMD_RDNA2;
  172. }
  173. }
  174. return vk_device_architecture::OTHER;
  175. }
  176. struct vk_device_struct {
  177. std::mutex mutex;
  178. vk::PhysicalDevice physical_device;
  179. vk::PhysicalDeviceProperties properties;
  180. std::string name;
  181. uint64_t max_memory_allocation_size;
  182. uint64_t suballocation_block_size;
  183. bool fp16;
  184. bool pipeline_robustness;
  185. vk::Device device;
  186. uint32_t vendor_id;
  187. vk_device_architecture architecture;
  188. vk_queue compute_queue;
  189. vk_queue transfer_queue;
  190. bool single_queue;
  191. uint32_t subgroup_size;
  192. uint32_t shader_core_count;
  193. bool uma;
  194. bool prefer_host_memory;
  195. bool float_controls_rte_fp16;
  196. bool subgroup_add;
  197. bool integer_dot_product;
  198. bool subgroup_size_control;
  199. uint32_t subgroup_min_size;
  200. uint32_t subgroup_max_size;
  201. bool subgroup_require_full_support;
  202. bool coopmat_support;
  203. bool coopmat_acc_f32_support;
  204. bool coopmat_acc_f16_support;
  205. uint32_t coopmat_m;
  206. uint32_t coopmat_n;
  207. uint32_t coopmat_k;
  208. bool coopmat_int_support;
  209. uint32_t coopmat_int_m;
  210. uint32_t coopmat_int_n;
  211. uint32_t coopmat_int_k;
  212. bool coopmat2;
  213. size_t idx;
  214. bool mul_mat_l[GGML_TYPE_COUNT];
  215. bool mul_mat_m[GGML_TYPE_COUNT];
  216. bool mul_mat_s[GGML_TYPE_COUNT];
  217. bool mul_mat_id_l[GGML_TYPE_COUNT];
  218. bool mul_mat_id_m[GGML_TYPE_COUNT];
  219. bool mul_mat_id_s[GGML_TYPE_COUNT];
  220. // set to true to indicate that some shaders need to be compiled after the dryrun
  221. bool need_compiles {};
  222. vk_matmul_pipeline pipeline_matmul_f32 {};
  223. vk_matmul_pipeline pipeline_matmul_f32_f16 {};
  224. vk_matmul_pipeline2 pipeline_matmul_f16;
  225. vk_matmul_pipeline2 pipeline_matmul_f16_f32;
  226. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat[GGML_TYPE_COUNT];
  227. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_COUNT];
  228. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_COUNT];
  229. vk_matmul_pipeline pipeline_matmul_id_f32 {};
  230. vk_matmul_pipeline2 pipeline_matmul_id_f16;
  231. vk_matmul_pipeline2 pipeline_matmul_id_f16_f32;
  232. vk_matmul_pipeline2 pipeline_dequant_mul_mat_mat_id[GGML_TYPE_COUNT];
  233. vk_pipeline pipeline_matmul_split_k_reduce;
  234. vk_pipeline pipeline_quantize_q8_1;
  235. vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
  236. vk_pipeline pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  237. vk_pipeline pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_COUNT][mul_mat_vec_max_cols];
  238. vk_pipeline pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_COUNT];
  239. vk_pipeline pipeline_mul_mat_vec_p021_f16_f32[p021_max_gqa_ratio];
  240. vk_pipeline pipeline_mul_mat_vec_nc_f16_f32;
  241. vk_pipeline pipeline_get_rows[GGML_TYPE_COUNT];
  242. vk_pipeline pipeline_get_rows_f32[GGML_TYPE_COUNT];
  243. vk_pipeline pipeline_acc_f32;
  244. vk_pipeline pipeline_add_f32, pipeline_add_f32_norepeat;
  245. vk_pipeline pipeline_add_f16_f32_f16, pipeline_add_f16_f32_f16_norepeat;
  246. vk_pipeline pipeline_sub_f32, pipeline_sub_f32_norepeat;
  247. vk_pipeline pipeline_mul_f32, pipeline_mul_f32_norepeat;
  248. vk_pipeline pipeline_div_f32, pipeline_div_f32_norepeat;
  249. vk_pipeline pipeline_concat_f32, pipeline_concat_f16, pipeline_concat_i32;
  250. vk_pipeline pipeline_upscale_f32;
  251. vk_pipeline pipeline_scale_f32;
  252. vk_pipeline pipeline_sqr_f32;
  253. vk_pipeline pipeline_sin_f32;
  254. vk_pipeline pipeline_cos_f32;
  255. vk_pipeline pipeline_clamp_f32;
  256. vk_pipeline pipeline_pad_f32;
  257. vk_pipeline pipeline_repeat_f32, pipeline_repeat_back_f32;
  258. vk_pipeline pipeline_cpy_f32_f32, pipeline_cpy_f32_f16, pipeline_cpy_f16_f16;
  259. vk_pipeline pipeline_contig_cpy_f32_f32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_f16_f16;
  260. vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
  261. vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
  262. vk_pipeline pipeline_norm_f32;
  263. vk_pipeline pipeline_group_norm_f32;
  264. vk_pipeline pipeline_rms_norm_f32;
  265. vk_pipeline pipeline_rms_norm_back_f32;
  266. vk_pipeline pipeline_l2_norm_f32;
  267. vk_pipeline pipeline_gelu_f32;
  268. vk_pipeline pipeline_gelu_quick_f32;
  269. vk_pipeline pipeline_silu_f32;
  270. vk_pipeline pipeline_silu_back_f32;
  271. vk_pipeline pipeline_relu_f32;
  272. vk_pipeline pipeline_leaky_relu_f32;
  273. vk_pipeline pipeline_tanh_f32;
  274. vk_pipeline pipeline_sigmoid_f32;
  275. vk_pipeline pipeline_diag_mask_inf_f32;
  276. vk_pipeline pipeline_soft_max_f32, pipeline_soft_max_f32_f16;
  277. vk_pipeline pipeline_soft_max_f32_wg512, pipeline_soft_max_f32_f16_wg512;
  278. vk_pipeline pipeline_soft_max_back_f32;
  279. vk_pipeline pipeline_rope_norm_f32, pipeline_rope_norm_f16;
  280. vk_pipeline pipeline_rope_neox_f32, pipeline_rope_neox_f16;
  281. vk_pipeline pipeline_rope_multi_f32, pipeline_rope_multi_f16;
  282. vk_pipeline pipeline_rope_vision_f32, pipeline_rope_vision_f16;
  283. vk_pipeline pipeline_argsort_f32;
  284. vk_pipeline pipeline_sum_rows_f32;
  285. vk_pipeline pipeline_argmax_f32;
  286. vk_pipeline pipeline_count_equal_i32;
  287. vk_pipeline pipeline_im2col_f32, pipeline_im2col_f32_f16;
  288. vk_pipeline pipeline_timestep_embedding_f32;
  289. vk_pipeline pipeline_pool2d_f32;
  290. vk_pipeline pipeline_rwkv_wkv6_f32;
  291. vk_pipeline pipeline_rwkv_wkv7_f32;
  292. vk_pipeline pipeline_opt_step_adamw_f32;
  293. // [2][2][2] is for {f16acc,f32acc}x{large,small_rows}x{unaligned, aligned}
  294. vk_pipeline pipeline_flash_attn_f32_f16_D64[GGML_TYPE_COUNT][2][2][2];
  295. vk_pipeline pipeline_flash_attn_f32_f16_D80[GGML_TYPE_COUNT][2][2][2];
  296. vk_pipeline pipeline_flash_attn_f32_f16_D96[GGML_TYPE_COUNT][2][2][2];
  297. vk_pipeline pipeline_flash_attn_f32_f16_D112[GGML_TYPE_COUNT][2][2][2];
  298. vk_pipeline pipeline_flash_attn_f32_f16_D128[GGML_TYPE_COUNT][2][2][2];
  299. vk_pipeline pipeline_flash_attn_f32_f16_D256[GGML_TYPE_COUNT][2][2][2];
  300. std::unordered_map<std::string, vk_pipeline_ref> pipelines;
  301. std::unordered_map<std::string, uint64_t> pipeline_descriptor_set_requirements;
  302. std::vector<std::tuple<void*, size_t, vk_buffer>> pinned_memory;
  303. vk::Fence fence;
  304. vk_buffer sync_staging;
  305. ggml_backend_buffer_type buffer_type;
  306. #ifdef GGML_VULKAN_MEMORY_DEBUG
  307. std::unique_ptr<vk_memory_logger> memory_logger;
  308. #endif
  309. #ifdef GGML_VULKAN_PERF
  310. std::unique_ptr<vk_perf_logger> perf_logger;
  311. #endif
  312. ~vk_device_struct() {
  313. VK_LOG_DEBUG("destroy device " << name);
  314. device.destroyFence(fence);
  315. ggml_vk_destroy_buffer(sync_staging);
  316. device.destroyCommandPool(compute_queue.pool);
  317. if (!single_queue) {
  318. device.destroyCommandPool(transfer_queue.pool);
  319. }
  320. for (auto& pipeline : pipelines) {
  321. if (pipeline.second.expired()) {
  322. continue;
  323. }
  324. vk_pipeline pl = pipeline.second.lock();
  325. ggml_vk_destroy_pipeline(device, pl);
  326. }
  327. pipelines.clear();
  328. device.destroy();
  329. }
  330. };
  331. struct vk_buffer_struct {
  332. vk::Buffer buffer = VK_NULL_HANDLE;
  333. vk::DeviceMemory device_memory = VK_NULL_HANDLE;
  334. vk::MemoryPropertyFlags memory_property_flags;
  335. void * ptr;
  336. size_t size = 0;
  337. vk_device device;
  338. ~vk_buffer_struct() {
  339. if (size == 0) {
  340. return;
  341. }
  342. VK_LOG_DEBUG("~vk_buffer_struct(" << buffer << ", " << size << ")");
  343. device->device.freeMemory(device_memory);
  344. device->device.destroyBuffer(buffer);
  345. }
  346. };
  347. struct vk_subbuffer {
  348. vk_buffer buffer;
  349. uint64_t offset;
  350. uint64_t size;
  351. operator vk::DescriptorBufferInfo() const {
  352. return { buffer->buffer, offset, size };
  353. }
  354. };
  355. struct vk_semaphore {
  356. vk::Semaphore s;
  357. uint64_t value;
  358. };
  359. struct vk_submission {
  360. vk::CommandBuffer buffer;
  361. std::vector<vk_semaphore> wait_semaphores;
  362. std::vector<vk_semaphore> signal_semaphores;
  363. };
  364. typedef std::vector<vk_submission> vk_sequence;
  365. struct vk_mat_mat_push_constants {
  366. uint32_t M; uint32_t N; uint32_t K;
  367. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  368. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  369. uint32_t k_split;
  370. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  371. uint32_t padded_N;
  372. };
  373. struct vk_mat_vec_push_constants {
  374. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  375. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  376. uint32_t ne02; uint32_t ne12; uint32_t broadcast2; uint32_t broadcast3;
  377. };
  378. struct vk_mat_mat_id_push_constants {
  379. uint32_t M; uint32_t N; uint32_t K;
  380. uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  381. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  382. uint32_t nei0; uint32_t nei1; uint32_t nbi1; uint32_t ne11;
  383. uint32_t padded_N;
  384. };
  385. struct vk_mat_vec_id_push_constants {
  386. uint32_t ncols; uint32_t stride_a; uint32_t stride_b; uint32_t stride_d;
  387. uint32_t batch_stride_a; uint32_t batch_stride_b; uint32_t batch_stride_d;
  388. uint32_t nei0; uint32_t ne11;
  389. };
  390. struct vk_flash_attn_push_constants {
  391. uint32_t N;
  392. uint32_t KV;
  393. uint32_t ne1;
  394. uint32_t ne2;
  395. uint32_t ne3;
  396. uint32_t neq2;
  397. uint32_t neq3;
  398. uint32_t nek2;
  399. uint32_t nek3;
  400. uint32_t nev2;
  401. uint32_t nev3;
  402. uint32_t nem1;
  403. uint32_t nb01;
  404. uint32_t nb02;
  405. uint32_t nb03;
  406. uint32_t nb11;
  407. uint32_t nb12;
  408. uint32_t nb13;
  409. uint32_t nb21;
  410. uint32_t nb22;
  411. uint32_t nb23;
  412. uint32_t nb31;
  413. float scale;
  414. float max_bias;
  415. float logit_softcap;
  416. uint32_t mask;
  417. uint32_t n_head_log2;
  418. float m0;
  419. float m1;
  420. };
  421. struct vk_op_push_constants {
  422. uint32_t KX;
  423. uint32_t KY;
  424. float param1;
  425. float param2;
  426. };
  427. struct vk_op_unary_push_constants {
  428. uint32_t ne;
  429. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  430. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  431. uint32_t misalign_offsets;
  432. float param1; float param2;
  433. uint32_t ne0_012mp; uint32_t ne0_012L;
  434. uint32_t ne0_01mp; uint32_t ne0_01L;
  435. uint32_t ne0_0mp; uint32_t ne0_0L;
  436. uint32_t ne1_012mp; uint32_t ne1_012L;
  437. uint32_t ne1_01mp; uint32_t ne1_01L;
  438. uint32_t ne1_0mp; uint32_t ne1_0L;
  439. };
  440. static_assert(sizeof(vk_op_unary_push_constants) <= 128, "sizeof(vk_op_unary_push_constants) must be <= 128");
  441. // See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.
  442. // Precompute mp (m' in the paper) and L such that division
  443. // can be computed using a multiply (high 32b of 64b result)
  444. // and a shift:
  445. //
  446. // n/d = (mulhi(n, mp) + n) >> L;
  447. static void init_fastdiv_values(uint32_t d, uint32_t &mp, uint32_t &L)
  448. {
  449. // compute L = ceil(log2(d));
  450. L = 0;
  451. while (L < 32 && (uint32_t{1} << L) < d) {
  452. L++;
  453. }
  454. mp = (uint32_t)((uint64_t{1} << 32) * ((uint64_t{1} << L) - d) / d + 1);
  455. }
  456. template <typename T> void init_pushconst_fastdiv(T &p) {
  457. GGML_UNUSED(p);
  458. static_assert(!std::is_const<T>::value, "unexpected type");
  459. }
  460. template <> void init_pushconst_fastdiv(vk_op_unary_push_constants &p) {
  461. // Compute magic values to divide by these six numbers.
  462. init_fastdiv_values(p.ne02*p.ne01*p.ne00, p.ne0_012mp, p.ne0_012L);
  463. init_fastdiv_values(p.ne01*p.ne00, p.ne0_01mp, p.ne0_01L);
  464. init_fastdiv_values(p.ne00, p.ne0_0mp, p.ne0_0L);
  465. init_fastdiv_values(p.ne12*p.ne11*p.ne10, p.ne1_012mp, p.ne1_012L);
  466. init_fastdiv_values(p.ne11*p.ne10, p.ne1_01mp, p.ne1_01L);
  467. init_fastdiv_values(p.ne10, p.ne1_0mp, p.ne1_0L);
  468. }
  469. struct vk_op_binary_push_constants {
  470. uint32_t ne;
  471. uint32_t ne00; uint32_t ne01; uint32_t ne02; uint32_t ne03; uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  472. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13; uint32_t nb10; uint32_t nb11; uint32_t nb12; uint32_t nb13;
  473. uint32_t ne20; uint32_t ne21; uint32_t ne22; uint32_t ne23; uint32_t nb20; uint32_t nb21; uint32_t nb22; uint32_t nb23;
  474. uint32_t misalign_offsets;
  475. float param1; float param2; int32_t param3;
  476. };
  477. struct vk_op_diag_mask_push_constants {
  478. uint32_t ncols;
  479. uint32_t rows_per_channel;
  480. int32_t n_past;
  481. };
  482. struct vk_op_rope_push_constants {
  483. uint32_t ncols;
  484. uint32_t n_dims;
  485. float freq_scale;
  486. uint32_t p_delta_rows;
  487. float freq_base;
  488. float ext_factor;
  489. float attn_factor;
  490. float corr_dims[2];
  491. float theta_scale;
  492. uint32_t has_ff;
  493. uint32_t ne02;
  494. uint32_t s1;
  495. uint32_t s2;
  496. int32_t sections[4];
  497. uint32_t is_back;
  498. };
  499. struct vk_op_soft_max_push_constants {
  500. uint32_t KX;
  501. uint32_t KY;
  502. float scale;
  503. float max_bias;
  504. float m0;
  505. float m1;
  506. uint32_t n_head_log2;
  507. uint32_t nrows_x;
  508. };
  509. struct vk_op_argsort_push_constants {
  510. uint32_t ncols;
  511. uint32_t ncols_pad;
  512. int32_t order;
  513. };
  514. struct vk_op_im2col_push_constants {
  515. uint32_t batch_offset; uint32_t offset_delta;
  516. uint32_t IC;
  517. uint32_t IW; uint32_t IH;
  518. uint32_t OW; uint32_t OH;
  519. uint32_t KW; uint32_t KH;
  520. uint32_t pelements;
  521. uint32_t CHW;
  522. int32_t s0; int32_t s1;
  523. int32_t p0; int32_t p1;
  524. int32_t d0; int32_t d1;
  525. };
  526. struct vk_op_timestep_embedding_push_constants {
  527. uint32_t nb1;
  528. uint32_t dim;
  529. uint32_t max_period;
  530. };
  531. struct vk_op_pool2d_push_constants {
  532. uint32_t IW; uint32_t IH;
  533. uint32_t OW; uint32_t OH;
  534. uint32_t OC;
  535. uint32_t pelements;
  536. uint32_t op;
  537. int32_t k0; int32_t k1;
  538. int32_t s0; int32_t s1;
  539. int32_t p0; int32_t p1;
  540. };
  541. struct vk_op_rwkv_wkv6_push_constants {
  542. uint32_t B;
  543. uint32_t T;
  544. uint32_t C;
  545. uint32_t H;
  546. };
  547. struct vk_op_rwkv_wkv7_push_constants {
  548. uint32_t B;
  549. uint32_t T;
  550. uint32_t C;
  551. uint32_t H;
  552. };
  553. struct vk_op_upscale_push_constants {
  554. uint32_t ne; uint32_t a_offset; uint32_t d_offset;
  555. uint32_t nb00; uint32_t nb01; uint32_t nb02; uint32_t nb03;
  556. uint32_t ne10; uint32_t ne11; uint32_t ne12; uint32_t ne13;
  557. float sf0; float sf1; float sf2; float sf3;
  558. };
  559. // Allow pre-recording command buffers
  560. struct vk_staging_memcpy {
  561. vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
  562. void * dst;
  563. const void * src;
  564. size_t n;
  565. };
  566. struct vk_context_struct {
  567. vk_submission * s;
  568. std::vector<vk_sequence> seqs;
  569. int exit_tensor_idx;
  570. std::vector<vk_staging_memcpy> in_memcpys;
  571. std::vector<vk_staging_memcpy> out_memcpys;
  572. vk_queue * q;
  573. };
  574. typedef std::shared_ptr<vk_context_struct> vk_context;
  575. typedef std::weak_ptr<vk_context_struct> vk_context_ref;
  576. struct ggml_vk_garbage_collector {
  577. std::vector<vk_semaphore> tl_semaphores;
  578. std::vector<vk_semaphore> semaphores;
  579. std::vector<vk::Event> events;
  580. std::vector<vk_buffer> temp_buffers;
  581. std::vector<vk_context> contexts;
  582. };
  583. #if defined(GGML_VULKAN_MEMORY_DEBUG) || defined(GGML_VULKAN_DEBUG)
  584. #define VK_LOG_MEMORY(msg) std::cerr << "ggml_vulkan memory: " << msg << std::endl
  585. static std::string format_size(size_t size) {
  586. const size_t kib = 1024;
  587. const size_t mib = kib * 1024;
  588. const size_t gib = mib * 1024;
  589. std::ostringstream oss;
  590. oss << std::fixed << std::setprecision(2);
  591. if (size >= gib) {
  592. oss << static_cast<double>(size) / gib << " GiB";
  593. } else if (size >= mib) {
  594. oss << static_cast<double>(size) / mib << " MiB";
  595. } else if (size >= kib) {
  596. oss << static_cast<double>(size) / kib << " KiB";
  597. } else {
  598. oss << size << " B";
  599. }
  600. return oss.str();
  601. }
  602. static std::mutex log_mutex;
  603. class vk_memory_logger {
  604. public:
  605. vk_memory_logger(): total_device(0), total_host(0) {}
  606. void log_allocation(vk_buffer_ref buf_ref, size_t size);
  607. void log_deallocation(vk_buffer_ref buf_ref);
  608. private:
  609. std::map<vk::Buffer, size_t> allocations; // Track allocations
  610. size_t total_device;
  611. size_t total_host;
  612. };
  613. #else
  614. #define VK_LOG_MEMORY(msg) ((void) 0)
  615. #endif // GGML_VULKAN_MEMORY_DEBUG
  616. #if defined(GGML_VULKAN_PERF)
  617. class vk_perf_logger {
  618. public:
  619. void print_timings() {
  620. std::cerr << "----------------\nVulkan Timings:" << std::endl;
  621. for (const auto& t : timings) {
  622. uint64_t total = 0;
  623. for (const auto& time : t.second) {
  624. total += time;
  625. }
  626. std::cerr << t.first << ": " << t.second.size() << " x " << (total / t.second.size() / 1000.0) << " ms" << std::endl;
  627. }
  628. timings.clear();
  629. }
  630. void log_timing(const ggml_tensor * node, uint64_t time) {
  631. if (node->op == GGML_OP_UNARY) {
  632. timings[ggml_unary_op_name(ggml_get_unary_op(node))].push_back(time);
  633. return;
  634. }
  635. if (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) {
  636. const uint64_t m = node->src[0]->ne[1];
  637. const uint64_t n = node->src[1]->ne[1];
  638. const uint64_t k = node->src[1]->ne[0];
  639. std::string name = ggml_op_name(node->op);
  640. if (n == 1) {
  641. name += "_VEC m=" + std::to_string(m) + " k=" + std::to_string(k);
  642. } else {
  643. name += " m=" + std::to_string(m) + " n=" + std::to_string(n) + " k=" + std::to_string(k);
  644. }
  645. timings[name].push_back(time);
  646. return;
  647. }
  648. timings[ggml_op_name(node->op)].push_back(time);
  649. }
  650. private:
  651. std::map<std::string, std::vector<uint64_t>> timings;
  652. };
  653. #endif // GGML_VULKAN_PERF
  654. struct ggml_backend_vk_context {
  655. std::string name;
  656. vk_device device;
  657. size_t semaphore_idx, event_idx;
  658. ggml_vk_garbage_collector gc;
  659. size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k;
  660. vk_buffer prealloc_x, prealloc_y, prealloc_split_k;
  661. vk::Fence fence;
  662. vk_buffer buffer_pool[MAX_VK_BUFFERS];
  663. vk_context_ref compute_ctx;
  664. vk_context_ref transfer_ctx;
  665. std::vector<vk_context_ref> tensor_ctxs;
  666. };
  667. static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
  668. static uint64_t vk_tensor_offset(const ggml_tensor * tensor) {
  669. if (tensor->view_src) {
  670. return (uint8_t *) tensor->view_src->data - (uint8_t *) vk_ptr_base;
  671. }
  672. return (uint8_t *) tensor->data - (uint8_t *) vk_ptr_base;
  673. }
  674. struct ggml_backend_vk_buffer_context {
  675. vk_device_ref device;
  676. vk_buffer dev_buffer;
  677. std::string name;
  678. ggml_backend_vk_buffer_context(vk_device_ref device, vk_buffer&& dev_buffer, std::string& name) :
  679. device(device),
  680. dev_buffer(dev_buffer),
  681. name(name) {
  682. }
  683. ~ggml_backend_vk_buffer_context() {
  684. ggml_vk_destroy_buffer(dev_buffer);
  685. }
  686. };
  687. #ifdef GGML_VULKAN_MEMORY_DEBUG
  688. void vk_memory_logger::log_allocation(vk_buffer_ref buf_ref, size_t size) {
  689. std::lock_guard<std::mutex> guard(log_mutex);
  690. vk_buffer buf = buf_ref.lock();
  691. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  692. const std::string type = device ? "device" : "host";
  693. allocations[buf->buffer] = size;
  694. total_device += device ? size : 0;
  695. total_host += device ? 0 : size;
  696. VK_LOG_MEMORY(buf->device->name << ": +" << format_size(size) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host));
  697. }
  698. void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
  699. if (buf_ref.expired() || buf_ref.lock()->size == 0) {
  700. return;
  701. }
  702. std::lock_guard<std::mutex> guard(log_mutex);
  703. vk_buffer buf = buf_ref.lock();
  704. const bool device = bool(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eDeviceLocal);
  705. std::string type = device ? "device" : "host";
  706. auto it = allocations.find(buf->buffer);
  707. total_device -= device ? it->second : 0;
  708. total_host -= device ? 0 : it->second;
  709. if (it != allocations.end()) {
  710. VK_LOG_MEMORY(buf->device->name << ": -" << format_size(it->second) << " " << type << " at " << buf->buffer << ". Total device: " << format_size(total_device) << ", total host: " << format_size(total_host));
  711. allocations.erase(it);
  712. } else {
  713. VK_LOG_MEMORY("ERROR " << buf->device->name << ": Attempted to deallocate unknown " << type << " memory at " << buf->buffer);
  714. }
  715. }
  716. #endif // GGML_VULKAN_MEMORY_DEBUG
  717. struct vk_instance_t {
  718. vk::Instance instance;
  719. std::vector<size_t> device_indices;
  720. vk_device devices[GGML_VK_MAX_DEVICES];
  721. };
  722. static bool vk_instance_initialized = false;
  723. static vk_instance_t vk_instance;
  724. #ifdef GGML_VULKAN_CHECK_RESULTS
  725. static size_t vk_skip_checks;
  726. static size_t vk_output_tensor;
  727. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name);
  728. static void ggml_vk_check_results_0(ggml_tensor * tensor);
  729. static void ggml_vk_check_results_1(ggml_tensor * tensor);
  730. #endif
  731. 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);
  732. static void ggml_backend_vk_free(ggml_backend_t backend);
  733. // variables to track number of compiles in progress
  734. static uint32_t compile_count = 0;
  735. static std::mutex compile_count_mutex;
  736. static std::condition_variable compile_count_cond;
  737. static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipeline, size_t spv_size, const void* spv_data, const std::string entrypoint,
  738. uint32_t parameter_count, std::array<uint32_t, 3> wg_denoms, std::vector<uint32_t> specialization_constants,
  739. bool disable_robustness, bool require_full_subgroups, uint32_t required_subgroup_size) {
  740. VK_LOG_DEBUG("ggml_vk_create_pipeline(" << device->name << ", " << pipeline->name << ", " << entrypoint << ", " << parameter_count <<
  741. ", (" << wg_denoms[0] << "," << wg_denoms[1] << "," << wg_denoms[2] << "), specialization_constants, " <<
  742. disable_robustness << ", " << require_full_subgroups << ", " << required_subgroup_size << ")");
  743. GGML_ASSERT(parameter_count > 0);
  744. GGML_ASSERT(wg_denoms[0] > 0 && wg_denoms[1] > 0 && wg_denoms[2] > 0); // NOLINT
  745. vk::ShaderModuleCreateInfo shader_module_create_info({}, spv_size, reinterpret_cast<const uint32_t *>(spv_data));
  746. pipeline->shader_module = device->device.createShaderModule(shader_module_create_info);
  747. std::vector<vk::DescriptorSetLayoutBinding> dsl_binding;
  748. std::vector<vk::DescriptorBindingFlags> dsl_binding_flags;
  749. for (uint32_t i = 0; i < parameter_count; i++) {
  750. dsl_binding.push_back({i, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eCompute});
  751. dsl_binding_flags.push_back({});
  752. }
  753. vk::DescriptorSetLayoutBindingFlagsCreateInfo dslbfci = { dsl_binding_flags };
  754. vk::PushConstantRange pcr(
  755. vk::ShaderStageFlagBits::eCompute,
  756. 0,
  757. pipeline->push_constant_size
  758. );
  759. vk::DescriptorSetLayoutCreateInfo descriptor_set_layout_create_info(
  760. {},
  761. dsl_binding);
  762. descriptor_set_layout_create_info.setPNext(&dslbfci);
  763. pipeline->dsl = device->device.createDescriptorSetLayout(descriptor_set_layout_create_info);
  764. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  765. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  766. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  767. pipeline->descriptor_set_idx = 0;
  768. vk::PipelineLayoutCreateInfo pipeline_layout_create_info(vk::PipelineLayoutCreateFlags(), pipeline->dsl, pcr);
  769. pipeline->layout = device->device.createPipelineLayout(pipeline_layout_create_info);
  770. std::vector<vk::SpecializationMapEntry> specialization_entries(specialization_constants.size());
  771. for (size_t i = 0; i < specialization_constants.size(); i++) {
  772. specialization_entries[i].constantID = i;
  773. specialization_entries[i].offset = i * sizeof(uint32_t);
  774. specialization_entries[i].size = sizeof(uint32_t);
  775. }
  776. vk::SpecializationInfo specialization_info(
  777. specialization_entries.size(),
  778. specialization_entries.data(),
  779. specialization_constants.size() * sizeof(uint32_t),
  780. specialization_constants.data()
  781. );
  782. vk::PipelineShaderStageCreateFlags pipeline_shader_stage_create_flags{};
  783. if (device->subgroup_require_full_support && require_full_subgroups) {
  784. pipeline_shader_stage_create_flags |= vk::PipelineShaderStageCreateFlagBits::eRequireFullSubgroupsEXT;
  785. }
  786. vk::PipelineShaderStageCreateInfo pipeline_shader_create_info(
  787. pipeline_shader_stage_create_flags,
  788. vk::ShaderStageFlagBits::eCompute,
  789. pipeline->shader_module,
  790. entrypoint.c_str(),
  791. &specialization_info);
  792. vk::PipelineShaderStageRequiredSubgroupSizeCreateInfoEXT pipeline_shader_stage_required_subgroup_size_create_info;
  793. pipeline_shader_stage_required_subgroup_size_create_info.requiredSubgroupSize = required_subgroup_size;
  794. if (device->subgroup_size_control && required_subgroup_size > 0) {
  795. GGML_ASSERT(device->subgroup_min_size <= required_subgroup_size && required_subgroup_size <= device->subgroup_max_size);
  796. pipeline_shader_create_info.setPNext(&pipeline_shader_stage_required_subgroup_size_create_info);
  797. }
  798. vk::ComputePipelineCreateInfo compute_pipeline_create_info(
  799. vk::PipelineCreateFlags{},
  800. pipeline_shader_create_info,
  801. pipeline->layout);
  802. vk::PipelineRobustnessCreateInfoEXT rci;
  803. if (device->pipeline_robustness && disable_robustness) {
  804. rci.storageBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  805. rci.uniformBuffers = vk::PipelineRobustnessBufferBehaviorEXT::eDisabled;
  806. compute_pipeline_create_info.setPNext(&rci);
  807. }
  808. try {
  809. pipeline->pipeline = device->device.createComputePipeline(VK_NULL_HANDLE, compute_pipeline_create_info).value;
  810. } catch (const vk::SystemError& e) {
  811. std::cerr << "ggml_vulkan: Compute pipeline creation failed for " << pipeline->name << std::endl;
  812. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  813. throw e;
  814. }
  815. pipeline->compiled = true;
  816. {
  817. std::lock_guard<std::mutex> guard(device->mutex);
  818. device->pipelines.insert({ pipeline->name, pipeline });
  819. }
  820. {
  821. std::lock_guard<std::mutex> guard(compile_count_mutex);
  822. assert(compile_count > 0);
  823. compile_count--;
  824. }
  825. compile_count_cond.notify_all();
  826. }
  827. static void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline) {
  828. VK_LOG_DEBUG("ggml_pipeline_destroy_pipeline(" << pipeline->name << ")");
  829. for (auto& pool : pipeline->descriptor_pools) {
  830. device.destroyDescriptorPool(pool);
  831. }
  832. pipeline->descriptor_pools.clear();
  833. pipeline->descriptor_sets.clear();
  834. pipeline->descriptor_set_idx = 0;
  835. device.destroyDescriptorSetLayout(pipeline->dsl);
  836. device.destroyPipelineLayout(pipeline->layout);
  837. device.destroyShaderModule(pipeline->shader_module);
  838. device.destroyPipeline(pipeline->pipeline);
  839. }
  840. static void ggml_pipeline_request_descriptor_sets(vk_device& device, vk_pipeline& pipeline, uint32_t n) {
  841. VK_LOG_DEBUG("ggml_pipeline_request_descriptor_sets(" << pipeline->name << ", " << n << ")");
  842. device->pipeline_descriptor_set_requirements[pipeline->name] += n;
  843. if (!pipeline->compiled) {
  844. pipeline->needed = true;
  845. device->need_compiles = true;
  846. }
  847. }
  848. static void ggml_pipeline_allocate_descriptor_sets(vk_device& device) {
  849. std::lock_guard<std::mutex> guard(device->mutex);
  850. for (auto& pair : device->pipeline_descriptor_set_requirements) {
  851. vk_pipeline pipeline = device->pipelines.at(pair.first).lock();
  852. const uint64_t n = pair.second;
  853. VK_LOG_DEBUG("ggml_pipeline_allocate_descriptor_sets(" << pipeline->name << ", " << n << ")");
  854. if (pipeline->descriptor_sets.size() >= pipeline->descriptor_set_idx + n) {
  855. // Enough descriptors are available
  856. continue;
  857. }
  858. uint32_t to_alloc = pipeline->descriptor_set_idx + n - pipeline->descriptor_sets.size();
  859. uint32_t pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE - pipeline->descriptor_sets.size() % VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  860. uint32_t pool_idx = pipeline->descriptor_sets.size() / VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  861. while (to_alloc > 0) {
  862. const uint32_t alloc_count = std::min(pool_remaining, to_alloc);
  863. to_alloc -= alloc_count;
  864. pool_remaining = VK_DEVICE_DESCRIPTOR_POOL_SIZE;
  865. if (pool_idx >= pipeline->descriptor_pools.size()) {
  866. vk::DescriptorPoolSize descriptor_pool_size(vk::DescriptorType::eStorageBuffer, pipeline->parameter_count * VK_DEVICE_DESCRIPTOR_POOL_SIZE);
  867. vk::DescriptorPoolCreateInfo descriptor_pool_create_info({}, VK_DEVICE_DESCRIPTOR_POOL_SIZE, descriptor_pool_size);
  868. pipeline->descriptor_pools.push_back(device->device.createDescriptorPool(descriptor_pool_create_info));
  869. }
  870. std::vector<vk::DescriptorSetLayout> layouts(alloc_count);
  871. for (uint32_t i = 0; i < alloc_count; i++) {
  872. layouts[i] = pipeline->dsl;
  873. }
  874. vk::DescriptorSetAllocateInfo descriptor_set_alloc_info(pipeline->descriptor_pools[pool_idx], alloc_count, layouts.data());
  875. std::vector<vk::DescriptorSet> sets = device->device.allocateDescriptorSets(descriptor_set_alloc_info);
  876. pipeline->descriptor_sets.insert(pipeline->descriptor_sets.end(), sets.begin(), sets.end());
  877. pool_idx++;
  878. }
  879. }
  880. }
  881. static void ggml_pipeline_cleanup(vk_pipeline& pipeline) {
  882. VK_LOG_DEBUG("ggml_pipeline_cleanup(" << pipeline->name << ")");
  883. pipeline->descriptor_set_idx = 0;
  884. }
  885. static vk::CommandBuffer ggml_vk_create_cmd_buffer(vk_device& device, vk_queue& q) {
  886. VK_LOG_DEBUG("ggml_vk_create_cmd_buffer()");
  887. std::lock_guard<std::mutex> guard(device->mutex);
  888. if (q.cmd_buffers.size() > q.cmd_buffer_idx) {
  889. // Reuse command buffer
  890. return q.cmd_buffers[q.cmd_buffer_idx++];
  891. }
  892. vk::CommandBufferAllocateInfo command_buffer_alloc_info(
  893. q.pool,
  894. vk::CommandBufferLevel::ePrimary,
  895. 1);
  896. const std::vector<vk::CommandBuffer> cmd_buffers = device->device.allocateCommandBuffers(command_buffer_alloc_info);
  897. auto buf = cmd_buffers.front();
  898. q.cmd_buffers.push_back(buf);
  899. q.cmd_buffer_idx++;
  900. return buf;
  901. }
  902. static vk_submission ggml_vk_create_submission(vk_device& device, vk_queue& q, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  903. VK_LOG_DEBUG("ggml_vk_create_submission()");
  904. vk_submission s;
  905. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  906. s.wait_semaphores = std::move(wait_semaphores);
  907. s.signal_semaphores = std::move(signal_semaphores);
  908. return s;
  909. }
  910. static void ggml_vk_submit(vk_context& ctx, vk::Fence fence) {
  911. if (ctx->seqs.empty()) {
  912. if (fence) {
  913. ctx->q->queue.submit({}, fence);
  914. }
  915. return;
  916. }
  917. VK_LOG_DEBUG("ggml_vk_submit(" << ctx << ", " << fence << ")");
  918. std::vector<std::vector<uint64_t>> tl_wait_vals;
  919. std::vector<std::vector<uint64_t>> tl_signal_vals;
  920. std::vector<std::vector<vk::Semaphore>> tl_wait_semaphores;
  921. std::vector<std::vector<vk::Semaphore>> tl_signal_semaphores;
  922. std::vector<vk::TimelineSemaphoreSubmitInfo> tl_submit_infos;
  923. std::vector<vk::SubmitInfo> submit_infos;
  924. int idx = -1;
  925. std::vector<std::vector<vk::PipelineStageFlags>> stage_flags;
  926. size_t reserve = 0;
  927. for (const auto& sequence : ctx->seqs) {
  928. reserve += sequence.size();
  929. }
  930. // Pre-reserve vectors to prevent reallocation, which invalidates pointers
  931. tl_wait_semaphores.reserve(reserve);
  932. tl_wait_vals.reserve(reserve);
  933. tl_signal_semaphores.reserve(reserve);
  934. tl_signal_vals.reserve(reserve);
  935. tl_submit_infos.reserve(reserve);
  936. submit_infos.reserve(reserve);
  937. stage_flags.reserve(reserve);
  938. for (const auto& sequence : ctx->seqs) {
  939. for (const auto& submission : sequence) {
  940. stage_flags.push_back({});
  941. idx++;
  942. tl_wait_vals.push_back({});
  943. tl_wait_semaphores.push_back({});
  944. tl_signal_vals.push_back({});
  945. tl_signal_semaphores.push_back({});
  946. for (size_t i = 0; i < submission.wait_semaphores.size(); i++) {
  947. stage_flags[idx].push_back(ctx->q->stage_flags);
  948. tl_wait_vals[idx].push_back(submission.wait_semaphores[i].value);
  949. tl_wait_semaphores[idx].push_back(submission.wait_semaphores[i].s);
  950. }
  951. for (size_t i = 0; i < submission.signal_semaphores.size(); i++) {
  952. tl_signal_vals[idx].push_back(submission.signal_semaphores[i].value);
  953. tl_signal_semaphores[idx].push_back(submission.signal_semaphores[i].s);
  954. }
  955. tl_submit_infos.push_back({
  956. (uint32_t) submission.wait_semaphores.size(),
  957. tl_wait_vals[idx].data(),
  958. (uint32_t) submission.signal_semaphores.size(),
  959. tl_signal_vals[idx].data(),
  960. });
  961. tl_submit_infos[idx].sType = vk::StructureType::eTimelineSemaphoreSubmitInfo;
  962. tl_submit_infos[idx].pNext = nullptr;
  963. vk::SubmitInfo si{
  964. (uint32_t) submission.wait_semaphores.size(),
  965. tl_wait_semaphores[idx].data(),
  966. stage_flags[idx].data(),
  967. 1,
  968. &submission.buffer,
  969. (uint32_t) submission.signal_semaphores.size(),
  970. tl_signal_semaphores[idx].data(),
  971. };
  972. si.setPNext(&tl_submit_infos[idx]);
  973. submit_infos.push_back(si);
  974. }
  975. }
  976. ctx->q->queue.submit(submit_infos, fence);
  977. ctx->seqs.clear();
  978. }
  979. 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) {
  980. VK_LOG_DEBUG("ggml_vk_find_queue_family_index()");
  981. const uint32_t qfsize = queue_family_props.size();
  982. // Try with avoid preferences first
  983. for (uint32_t i = 0; i < qfsize; i++) {
  984. 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)) {
  985. return i;
  986. }
  987. }
  988. // Fall back to only required
  989. for (size_t i = 0; i < qfsize; i++) {
  990. if (queue_family_props[i].queueCount >= min_num_queues && (compute_index < 0 || i != (uint32_t) compute_index) && queue_family_props[i].queueFlags & required) {
  991. return i;
  992. }
  993. }
  994. // Fall back to reusing compute queue
  995. for (size_t i = 0; i < qfsize; i++) {
  996. if (queue_family_props[i].queueCount >= min_num_queues && queue_family_props[i].queueFlags & required) {
  997. return i;
  998. }
  999. }
  1000. // Fall back to ignoring min_num_queries
  1001. for (size_t i = 0; i < qfsize; i++) {
  1002. if (queue_family_props[i].queueFlags & required) {
  1003. return i;
  1004. }
  1005. }
  1006. // 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.
  1007. // 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.
  1008. if (compute_index >= 0) {
  1009. return compute_index;
  1010. }
  1011. std::cerr << "ggml_vulkan: No suitable queue family index found." << std::endl;
  1012. for(auto &q_family : queue_family_props) {
  1013. std::cerr << "Queue number: " + std::to_string(q_family.queueCount) << " flags: " + to_string(q_family.queueFlags) << std::endl;
  1014. }
  1015. abort();
  1016. }
  1017. static void ggml_vk_create_queue(vk_device& device, vk_queue& q, uint32_t queue_family_index, uint32_t queue_index, vk::PipelineStageFlags&& stage_flags, bool transfer_only) {
  1018. VK_LOG_DEBUG("ggml_vk_create_queue()");
  1019. std::lock_guard<std::mutex> guard(device->mutex);
  1020. q.queue_family_index = queue_family_index;
  1021. q.transfer_only = transfer_only;
  1022. vk::CommandPoolCreateInfo command_pool_create_info_compute(vk::CommandPoolCreateFlags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT), queue_family_index);
  1023. q.pool = device->device.createCommandPool(command_pool_create_info_compute);
  1024. q.cmd_buffer_idx = 0;
  1025. q.queue = device->device.getQueue(queue_family_index, queue_index);
  1026. q.stage_flags = stage_flags;
  1027. }
  1028. static vk_context ggml_vk_create_context(ggml_backend_vk_context * ctx, vk_queue& q) {
  1029. vk_context result = std::make_shared<vk_context_struct>();
  1030. VK_LOG_DEBUG("ggml_vk_create_context(" << result << ")");
  1031. ctx->gc.contexts.emplace_back(result);
  1032. result->q = &q;
  1033. return result;
  1034. }
  1035. static vk_context ggml_vk_create_temporary_context(vk_queue& q) {
  1036. vk_context result = std::make_shared<vk_context_struct>();
  1037. VK_LOG_DEBUG("ggml_vk_create_temporary_context(" << result << ")");
  1038. result->q = &q;
  1039. return result;
  1040. }
  1041. static vk_semaphore * ggml_vk_create_binary_semaphore(ggml_backend_vk_context * ctx) {
  1042. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1043. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eBinary, 0 };
  1044. vk::SemaphoreCreateInfo ci{};
  1045. ci.setPNext(&tci);
  1046. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1047. ctx->gc.semaphores.push_back({ semaphore, 0 });
  1048. return &ctx->gc.semaphores[ctx->gc.semaphores.size() - 1];
  1049. }
  1050. static vk_semaphore * ggml_vk_create_timeline_semaphore(ggml_backend_vk_context * ctx) {
  1051. VK_LOG_DEBUG("ggml_vk_create_timeline_semaphore()");
  1052. if (ctx->semaphore_idx >= ctx->gc.tl_semaphores.size()) {
  1053. vk::SemaphoreTypeCreateInfo tci{ vk::SemaphoreType::eTimeline, 0 };
  1054. vk::SemaphoreCreateInfo ci{};
  1055. ci.setPNext(&tci);
  1056. vk::Semaphore semaphore = ctx->device->device.createSemaphore(ci);
  1057. ctx->gc.tl_semaphores.push_back({ semaphore, 0 });
  1058. }
  1059. return &ctx->gc.tl_semaphores[ctx->semaphore_idx++];
  1060. }
  1061. static vk::Event ggml_vk_create_event(ggml_backend_vk_context * ctx) {
  1062. if (ctx->event_idx >= ctx->gc.events.size()) {
  1063. ctx->gc.events.push_back(ctx->device->device.createEvent({}));
  1064. }
  1065. return ctx->gc.events[ctx->event_idx++];
  1066. }
  1067. static void ggml_vk_queue_cleanup(vk_device& device, vk_queue& q) {
  1068. VK_LOG_DEBUG("ggml_vk_queue_cleanup()");
  1069. std::lock_guard<std::mutex> guard(device->mutex);
  1070. // Requires command buffers to be done
  1071. device->device.resetCommandPool(q.pool);
  1072. q.cmd_buffer_idx = 0;
  1073. }
  1074. static uint32_t find_properties(const vk::PhysicalDeviceMemoryProperties* mem_props, vk::MemoryRequirements* mem_req, vk::MemoryPropertyFlags flags) {
  1075. for (uint32_t i = 0; i < mem_props->memoryTypeCount; ++i) {
  1076. vk::MemoryType memory_type = mem_props->memoryTypes[i];
  1077. if ((mem_req->memoryTypeBits & ((uint64_t)1 << i)) &&
  1078. (flags & memory_type.propertyFlags) == flags &&
  1079. mem_props->memoryHeaps[memory_type.heapIndex].size >= mem_req->size) {
  1080. return static_cast<int32_t>(i);
  1081. }
  1082. }
  1083. return UINT32_MAX;
  1084. }
  1085. static vk_buffer ggml_vk_create_buffer(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1086. VK_LOG_DEBUG("ggml_vk_create_buffer(" << device->name << ", " << size << ", " << to_string(req_flags) << ", " << to_string(fallback_flags) << ")");
  1087. if (size > device->max_memory_allocation_size) {
  1088. throw vk::OutOfDeviceMemoryError("Requested buffer size exceeds device memory allocation limit");
  1089. }
  1090. std::lock_guard<std::mutex> guard(device->mutex);
  1091. vk_buffer buf = std::make_shared<vk_buffer_struct>();
  1092. if (size == 0) {
  1093. buf->size = 0;
  1094. return buf;
  1095. }
  1096. vk::BufferCreateInfo buffer_create_info{
  1097. vk::BufferCreateFlags(),
  1098. size,
  1099. vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst,
  1100. vk::SharingMode::eExclusive,
  1101. 0,
  1102. nullptr,
  1103. };
  1104. buf->buffer = device->device.createBuffer(buffer_create_info);
  1105. vk::MemoryRequirements mem_req = device->device.getBufferMemoryRequirements(buf->buffer);
  1106. vk::PhysicalDeviceMemoryProperties mem_props = device->physical_device.getMemoryProperties();
  1107. uint32_t memory_type_index = UINT32_MAX;
  1108. memory_type_index = find_properties(&mem_props, &mem_req, req_flags);
  1109. buf->memory_property_flags = req_flags;
  1110. if (memory_type_index == UINT32_MAX && fallback_flags) {
  1111. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1112. buf->memory_property_flags = fallback_flags;
  1113. }
  1114. if (memory_type_index == UINT32_MAX) {
  1115. device->device.destroyBuffer(buf->buffer);
  1116. throw vk::OutOfDeviceMemoryError("No suitable memory type found");
  1117. }
  1118. try {
  1119. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1120. } catch (const vk::SystemError& e) {
  1121. if (buf->memory_property_flags != fallback_flags) {
  1122. // Try again with fallback flags
  1123. memory_type_index = find_properties(&mem_props, &mem_req, fallback_flags);
  1124. buf->memory_property_flags = fallback_flags;
  1125. try {
  1126. buf->device_memory = device->device.allocateMemory({ mem_req.size, memory_type_index });
  1127. }
  1128. catch (const vk::SystemError& e) {
  1129. device->device.destroyBuffer(buf->buffer);
  1130. throw e;
  1131. }
  1132. } else {
  1133. // Out of Host/Device memory, clean up buffer
  1134. device->device.destroyBuffer(buf->buffer);
  1135. throw e;
  1136. }
  1137. }
  1138. buf->ptr = nullptr;
  1139. if (buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  1140. buf->ptr = device->device.mapMemory(buf->device_memory, 0, VK_WHOLE_SIZE);
  1141. }
  1142. device->device.bindBufferMemory(buf->buffer, buf->device_memory, 0);
  1143. buf->device = device;
  1144. buf->size = size;
  1145. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1146. device->memory_logger->log_allocation(buf, size);
  1147. #endif
  1148. return buf;
  1149. }
  1150. static vk_buffer ggml_vk_create_buffer_check(vk_device& device, size_t size, vk::MemoryPropertyFlags req_flags, vk::MemoryPropertyFlags fallback_flags = vk::MemoryPropertyFlags(0)) {
  1151. try {
  1152. return ggml_vk_create_buffer(device, size, req_flags, fallback_flags);
  1153. } catch (const vk::SystemError& e) {
  1154. std::cerr << "ggml_vulkan: Memory allocation of size " << size << " failed." << std::endl;
  1155. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1156. throw e;
  1157. }
  1158. }
  1159. static vk_buffer ggml_vk_create_buffer_device(vk_device& device, size_t size) {
  1160. vk_buffer buf;
  1161. try {
  1162. if (device->prefer_host_memory) {
  1163. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1164. } else if (device->uma) {
  1165. // Fall back to host memory type
  1166. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  1167. } else {
  1168. // use rebar if available, otherwise fallback to device only visible memory
  1169. buf = ggml_vk_create_buffer(device, size, vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, vk::MemoryPropertyFlagBits::eDeviceLocal);
  1170. }
  1171. } catch (const vk::SystemError& e) {
  1172. std::cerr << "ggml_vulkan: Device memory allocation of size " << size << " failed." << std::endl;
  1173. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  1174. throw e;
  1175. }
  1176. return buf;
  1177. }
  1178. static void ggml_vk_destroy_buffer(vk_buffer& buf) {
  1179. if (buf == nullptr) {
  1180. return;
  1181. }
  1182. #ifdef GGML_VULKAN_MEMORY_DEBUG
  1183. if (buf->device != nullptr) {
  1184. buf->device->memory_logger->log_deallocation(buf);
  1185. }
  1186. #endif
  1187. buf.reset();
  1188. }
  1189. static vk_subbuffer ggml_vk_subbuffer(vk_buffer& buf) {
  1190. return { buf, 0, VK_WHOLE_SIZE };
  1191. }
  1192. static void ggml_vk_sync_buffers(vk_context& ctx) {
  1193. VK_LOG_DEBUG("ggml_vk_sync_buffers()");
  1194. const bool transfer_queue = ctx->q->transfer_only;
  1195. ctx->s->buffer.pipelineBarrier(
  1196. ctx->q->stage_flags,
  1197. ctx->q->stage_flags,
  1198. {},
  1199. { {
  1200. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) },
  1201. { !transfer_queue ? (vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite | vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) : (vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite) }
  1202. } },
  1203. {},
  1204. {}
  1205. );
  1206. }
  1207. static void ggml_vk_wait_events(vk_context& ctx, std::vector<vk::Event>&& events) {
  1208. VK_LOG_DEBUG("ggml_vk_wait_events()");
  1209. if (events.empty()) {
  1210. return;
  1211. }
  1212. ctx->s->buffer.waitEvents(
  1213. events,
  1214. ctx->q->stage_flags,
  1215. ctx->q->stage_flags,
  1216. {},
  1217. {},
  1218. {}
  1219. );
  1220. }
  1221. // number of rows/cols for flash attention shader
  1222. static constexpr uint32_t flash_attention_num_small_rows = 32;
  1223. static std::array<uint32_t, 2> fa_rows_cols(uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) {
  1224. GGML_UNUSED(clamp);
  1225. // small rows, large cols
  1226. if (small_rows) {
  1227. return {flash_attention_num_small_rows, 128};
  1228. }
  1229. // small cols to reduce register count
  1230. if (ggml_is_quantized(type) || D == 256) {
  1231. return {64, 32};
  1232. }
  1233. return {64, 64};
  1234. };
  1235. static bool ggml_vk_matmul_shmem_support(const vk_device& device, const std::vector<uint32_t>& warptile, bool mul_mat_id, ggml_type src0_type) {
  1236. uint32_t lut_size = 0;
  1237. switch (src0_type) {
  1238. case GGML_TYPE_IQ1_S:
  1239. case GGML_TYPE_IQ1_M:
  1240. lut_size = 2*2048;
  1241. break;
  1242. case GGML_TYPE_IQ2_XXS:
  1243. lut_size = 8*256;
  1244. break;
  1245. case GGML_TYPE_IQ2_XS:
  1246. lut_size = 8*512;
  1247. break;
  1248. case GGML_TYPE_IQ2_S:
  1249. lut_size = 8*1024;
  1250. break;
  1251. case GGML_TYPE_IQ3_XXS:
  1252. lut_size = 4*256;
  1253. break;
  1254. case GGML_TYPE_IQ3_S:
  1255. lut_size = 4*512;
  1256. break;
  1257. case GGML_TYPE_IQ4_NL:
  1258. case GGML_TYPE_IQ4_XS:
  1259. lut_size = 4*16;
  1260. break;
  1261. default:
  1262. break;
  1263. }
  1264. // Needs to be kept up to date on shader changes
  1265. const uint32_t bank_conflict_offset = device->coopmat_support ? 8 : 1;
  1266. const uint32_t type_size = device->fp16 ? sizeof(ggml_fp16_t) : sizeof(float);
  1267. const uint32_t warps = warptile[0] / warptile[10];
  1268. const uint32_t load_bufs = (warptile[1] + warptile[2]) * (warptile[3] + bank_conflict_offset) * type_size;
  1269. const uint32_t mmid_row_ids = mul_mat_id ? 3072 * sizeof(uint32_t) : 0;
  1270. const uint32_t coopmat_stage = device->coopmat_support ? warptile[7] * warptile[8] / warps * sizeof(float) : 0;
  1271. const uint32_t total_size = load_bufs + mmid_row_ids + coopmat_stage + lut_size;
  1272. const bool supported = total_size <= device->properties.limits.maxComputeSharedMemorySize;
  1273. VK_LOG_DEBUG("ggml_vk_matmul_shmem_support(warptile=(" << warptile[0] << "," << warptile[1] << "," << warptile[2] << "), "
  1274. "mul_mat_id=" << mul_mat_id << ", src0_type=" << ggml_type_name(src0_type) << ", supported=" << supported);
  1275. return supported;
  1276. }
  1277. struct GpuPipelineConfig {
  1278. // GPU architecture identifier.
  1279. // Example: vk_device_architecture::AMD_GCN
  1280. vk_device_architecture arch;
  1281. // Mapping of pipeline names to their specific subgroup sizes.
  1282. // Example: {"soft_max_f32", 64}
  1283. std::unordered_map<std::string, uint32_t> pipelines;
  1284. // Default subgroup size for this GPU.
  1285. // Defaults to 0 if not explicitly provided.
  1286. uint32_t default_subgroup_size = 0;
  1287. };
  1288. // Pipeline configuration for RDNA1 GPUs.
  1289. static const std::unordered_map<std::string, uint32_t> rdna1_pipelines = {
  1290. {"soft_max", 64}, {"im2col", 64},
  1291. {"argmax", 64}, {"mul_mat_vec", 64},
  1292. {"mul_mat_vec_f16", 32}, {"mul_mat_vec_f32_f16", 32}
  1293. };
  1294. // Pipeline configuration for RDNA2 GPUs.
  1295. static const std::unordered_map<std::string, uint32_t> rdna2_pipelines = {
  1296. {"soft_max", 64}, {"im2col", 64},
  1297. };
  1298. static constexpr uint32_t RDNA_DEFAULT_SUBGROUP_SIZE = 32;
  1299. // Define configurations for different GPUs.
  1300. static std::vector<GpuPipelineConfig> gpu_pipeline_configs = {
  1301. {
  1302. vk_device_architecture::AMD_RDNA1,
  1303. {
  1304. rdna1_pipelines,
  1305. },
  1306. RDNA_DEFAULT_SUBGROUP_SIZE
  1307. },
  1308. {
  1309. vk_device_architecture::AMD_RDNA2,
  1310. {
  1311. rdna2_pipelines,
  1312. },
  1313. RDNA_DEFAULT_SUBGROUP_SIZE
  1314. },
  1315. };
  1316. static uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch) {
  1317. for (const auto &config : gpu_pipeline_configs) {
  1318. if (config.arch == arch) {
  1319. auto pipIt = config.pipelines.find(pipeline_name);
  1320. if (pipIt != config.pipelines.end()) {
  1321. return pipIt->second;
  1322. }
  1323. std::vector<std::pair<std::string, uint32_t>> sorted_pipelines(config.pipelines.begin(), config.pipelines.end());
  1324. std::sort(sorted_pipelines.begin(), sorted_pipelines.end(),
  1325. [](const auto &a, const auto &b) { return a.first.size() > b.first.size(); });
  1326. for (const auto &entry : sorted_pipelines) {
  1327. if (pipeline_name.find(entry.first) != std::string::npos) {
  1328. return entry.second;
  1329. }
  1330. }
  1331. return config.default_subgroup_size;
  1332. }
  1333. }
  1334. return 0; // If no matching configuration is found
  1335. }
  1336. static void ggml_vk_load_shaders(vk_device& device) {
  1337. VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
  1338. // some shaders have a minimum subgroup size
  1339. const uint32_t subgroup_size_8 = std::max(device->subgroup_size, 8u);
  1340. const uint32_t subgroup_size_16 = std::max(device->subgroup_size, 16u);
  1341. const uint32_t subgroup_size_32 = std::max(device->subgroup_size, 32u);
  1342. // mulmat
  1343. std::vector<uint32_t> l_warptile, m_warptile, s_warptile,
  1344. l_warptile_mmq, m_warptile_mmq, s_warptile_mmq,
  1345. l_warptile_mmq_int, m_warptile_mmq_int, s_warptile_mmq_int,
  1346. l_warptile_mmq_k, m_warptile_mmq_k, s_warptile_mmq_k,
  1347. l_warptile_mmqid, m_warptile_mmqid, s_warptile_mmqid;
  1348. std::array<uint32_t, 3> l_wg_denoms, m_wg_denoms, s_wg_denoms,
  1349. l_mmq_wg_denoms, m_mmq_wg_denoms, s_mmq_wg_denoms,
  1350. l_mmq_wg_denoms_k, m_mmq_wg_denoms_k, s_mmq_wg_denoms_k,
  1351. l_mmqid_wg_denoms, m_mmqid_wg_denoms, s_mmqid_wg_denoms;
  1352. uint32_t l_align, m_align, s_align;
  1353. if (device->coopmat2) {
  1354. // spec constants and tile sizes for non-quant matmul/matmul_id
  1355. l_warptile = { 256, 128, 256, 64, 1 };
  1356. m_warptile = { 256, 128, 128, 64, 0 };
  1357. s_warptile = { 128, 64, 64, 64, 0 };
  1358. l_wg_denoms = {128, 256, 1 };
  1359. m_wg_denoms = {128, 128, 1 };
  1360. s_wg_denoms = { 64, 64, 1 };
  1361. // spec constants and tile sizes for quant matmul (non-Qi_K)
  1362. l_warptile_mmq = { 256, 128, 256, 64, 1 };
  1363. m_warptile_mmq = { 256, 128, 128, 64, 1 };
  1364. s_warptile_mmq = { 256, 32, 64, 128, 0 };
  1365. l_mmq_wg_denoms = { 128, 256, 1 };
  1366. m_mmq_wg_denoms = { 128, 128, 1 };
  1367. s_mmq_wg_denoms = { 32, 64, 1 };
  1368. // spec constants and tile sizes for quant matmul (Qi_K)
  1369. l_warptile_mmq_k = { 256, 64, 128, 64, 1 };
  1370. m_warptile_mmq_k = { 256, 32, 64, 64, 0 };
  1371. s_warptile_mmq_k = { 256, 32, 32, 128, 0 };
  1372. l_mmq_wg_denoms_k = { 64, 128, 1 };
  1373. m_mmq_wg_denoms_k = { 32, 64, 1 };
  1374. s_mmq_wg_denoms_k = { 32, 32, 1 };
  1375. // spec constants and tile sizes for quant matmul_id
  1376. l_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1377. m_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1378. s_warptile_mmqid = { 256, 128, 64, 16, 0 };
  1379. l_mmqid_wg_denoms = { 128, 64, 1 };
  1380. m_mmqid_wg_denoms = { 128, 64, 1 };
  1381. s_mmqid_wg_denoms = { 128, 64, 1 };
  1382. l_align = 128;
  1383. m_align = 64;
  1384. s_align = 32;
  1385. } else {
  1386. // Matrix cores require different warp group sizes
  1387. const uint32_t tm_l = device->coopmat_support ? device->coopmat_m : 4;
  1388. const uint32_t tm_m = device->coopmat_support ? device->coopmat_m : 4;
  1389. const uint32_t tm_s = device->coopmat_support ? device->coopmat_m : 2;
  1390. const uint32_t tn_l = device->coopmat_support ? device->coopmat_n : 4;
  1391. const uint32_t tn_m = device->coopmat_support ? device->coopmat_n : 2;
  1392. const uint32_t tn_s = device->coopmat_support ? device->coopmat_n : 2;
  1393. const uint32_t tk_l = device->coopmat_support ? device->coopmat_k : 1;
  1394. const uint32_t tk_m = device->coopmat_support ? device->coopmat_k : 1;
  1395. const uint32_t tk_s = device->coopmat_support ? device->coopmat_k : 1;
  1396. l_warptile = { 128, 128, 128, 16, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1397. m_warptile = { 128, 64, 64, 16, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1398. s_warptile = { subgroup_size_16, 32, 32, 16, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1399. l_warptile_mmq = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_l, tn_l, tk_l, subgroup_size_8 };
  1400. m_warptile_mmq = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_m, tn_m, tk_m, subgroup_size_8 };
  1401. s_warptile_mmq = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_s, tn_s, tk_s, subgroup_size_8 };
  1402. const uint32_t tm_int_l = device->coopmat_int_support ? device->coopmat_int_m : 4;
  1403. const uint32_t tm_int_m = device->coopmat_int_support ? device->coopmat_int_m : 4;
  1404. const uint32_t tm_int_s = device->coopmat_int_support ? device->coopmat_int_m : 2;
  1405. const uint32_t tn_int_l = device->coopmat_int_support ? device->coopmat_int_n : 4;
  1406. const uint32_t tn_int_m = device->coopmat_int_support ? device->coopmat_int_n : 2;
  1407. const uint32_t tn_int_s = device->coopmat_int_support ? device->coopmat_int_n : 2;
  1408. const uint32_t tk_int_l = device->coopmat_int_support ? device->coopmat_int_k : 1;
  1409. const uint32_t tk_int_m = device->coopmat_int_support ? device->coopmat_int_k : 1;
  1410. const uint32_t tk_int_s = device->coopmat_int_support ? device->coopmat_int_k : 1;
  1411. l_warptile_mmq_int = { 128, 128, 128, 32, subgroup_size_8 * 2, 64, 2, tm_int_l, tn_int_l, tk_int_l, subgroup_size_8 };
  1412. m_warptile_mmq_int = { 128, 64, 64, 32, subgroup_size_8, 32, 2, tm_int_m, tn_int_m, tk_int_m, subgroup_size_8 };
  1413. s_warptile_mmq_int = { subgroup_size_32, 32, 32, 32, 32, 32, 2, tm_int_s, tn_int_s, tk_int_s, subgroup_size_8 };
  1414. l_mmq_wg_denoms = l_wg_denoms = {128, 128, 1 };
  1415. m_mmq_wg_denoms = m_wg_denoms = { 64, 64, 1 };
  1416. s_mmq_wg_denoms = s_wg_denoms = { 32, 32, 1 };
  1417. l_align = 128;
  1418. m_align = 64;
  1419. s_align = 32;
  1420. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  1421. ggml_type t = (ggml_type)i;
  1422. // Disable medium and large matrix multiplication if not enough shared memory is available
  1423. // Check mmq warptiles as the largest configuration
  1424. // Throw an error if not enough for any matrix multiplication is available
  1425. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, false, t)) {
  1426. std::cerr << "ggml_vulkan: Error: Shared memory size too small for matrix multiplication." << std::endl;
  1427. throw std::runtime_error("Shared memory size too small for matrix multiplication.");
  1428. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, false, t)) {
  1429. device->mul_mat_m[i] = false;
  1430. device->mul_mat_l[i] = false;
  1431. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, false, t)) {
  1432. device->mul_mat_l[i] = false;
  1433. }
  1434. // Disable mul_mat_id if not enough shared memory is available
  1435. if (!ggml_vk_matmul_shmem_support(device, s_warptile_mmq, true, t)) {
  1436. device->mul_mat_id_s[i] = false;
  1437. device->mul_mat_id_m[i] = false;
  1438. device->mul_mat_id_l[i] = false;
  1439. } else if (!ggml_vk_matmul_shmem_support(device, m_warptile_mmq, true, t)) {
  1440. device->mul_mat_id_m[i] = false;
  1441. device->mul_mat_id_l[i] = false;
  1442. } else if (!ggml_vk_matmul_shmem_support(device, l_warptile_mmq, true, t)) {
  1443. device->mul_mat_id_l[i] = false;
  1444. }
  1445. }
  1446. }
  1447. if (!device->pipeline_matmul_f32) {
  1448. device->pipeline_matmul_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1449. }
  1450. if (!device->pipeline_matmul_f32_f16) {
  1451. device->pipeline_matmul_f32_f16 = std::make_shared<vk_matmul_pipeline_struct>();
  1452. }
  1453. if (!device->pipeline_matmul_id_f32) {
  1454. device->pipeline_matmul_id_f32 = std::make_shared<vk_matmul_pipeline_struct>();
  1455. }
  1456. std::vector<std::future<void>> compiles;
  1457. auto const &ggml_vk_create_pipeline = [&](vk_device& device, vk_pipeline& pipeline, const std::string &name, size_t spv_size, const void* spv_data, const std::string &entrypoint,
  1458. uint32_t parameter_count, uint32_t push_constant_size, std::array<uint32_t, 3> wg_denoms, const std::vector<uint32_t>& specialization_constants,
  1459. uint32_t align, bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0) {
  1460. if (!require_full_subgroups && required_subgroup_size == 0) {
  1461. required_subgroup_size = get_subgroup_size(name, device->architecture);
  1462. }
  1463. if (!pipeline) {
  1464. pipeline = std::make_shared<vk_pipeline_struct>();
  1465. pipeline->name = name;
  1466. pipeline->parameter_count = parameter_count;
  1467. pipeline->push_constant_size = push_constant_size;
  1468. pipeline->wg_denoms = wg_denoms;
  1469. pipeline->align = align;
  1470. }
  1471. if (!pipeline->needed || pipeline->compiled) {
  1472. return;
  1473. }
  1474. {
  1475. // wait until fewer than N compiles are in progress
  1476. uint32_t N = std::max(1u, std::thread::hardware_concurrency());
  1477. std::unique_lock<std::mutex> guard(compile_count_mutex);
  1478. while (compile_count >= N) {
  1479. compile_count_cond.wait(guard);
  1480. }
  1481. compile_count++;
  1482. }
  1483. compiles.push_back(std::async(ggml_vk_create_pipeline_func, std::ref(device), std::ref(pipeline), spv_size, spv_data, entrypoint,
  1484. parameter_count, wg_denoms, specialization_constants, disable_robustness, require_full_subgroups, required_subgroup_size));
  1485. };
  1486. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1487. if (device->coopmat2) {
  1488. auto const &fa_wg_denoms = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::array<uint32_t, 3> {
  1489. return {fa_rows_cols(D, clamp, type, small_rows)[0], 1, 1};
  1490. };
  1491. auto const &fa_spec_constants = [&](uint32_t D, uint32_t clamp, ggml_type type, bool small_rows) -> std::vector<uint32_t> {
  1492. // For large number of rows, 128 invocations seems to work best.
  1493. // For small number of rows (e.g. N==1), 256 works better. But matrix granularity for 256 is 32, so we
  1494. // can't use 256 for D==80.
  1495. uint32_t wg_size = (small_rows && (D % 32) == 0) ? 256 : 128;
  1496. auto rows_cols = fa_rows_cols(D, clamp, type, small_rows);
  1497. return {wg_size, rows_cols[0], rows_cols[1], (D), clamp};
  1498. };
  1499. #define CREATE_FA2(TYPE, NAMELC, D) \
  1500. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][0], "flash_attn_f32_f16_D" #D "_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1501. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][0][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1502. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][0], "flash_attn_f32_f16_D" #D "_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,false), fa_spec_constants(D,1,TYPE,false), 1); \
  1503. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][0][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,false), fa_spec_constants(D,0,TYPE,false), fa_rows_cols(D,0,TYPE,false)[1]); \
  1504. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][0], "flash_attn_f32_f16_D" #D "_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1505. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][0][1][1], "flash_attn_f32_f16_D" #D "_aligned_f16acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_len, flash_attn_f32_f16_ ## NAMELC ## _f16acc_cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1506. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][0], "flash_attn_f32_f16_D" #D "_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,1,TYPE,true), fa_spec_constants(D,1,TYPE,true), 1); \
  1507. ggml_vk_create_pipeline(device, device->pipeline_flash_attn_f32_f16_D ## D[TYPE][1][1][1], "flash_attn_f32_f16_D" #D "_aligned_f32acc_smallrows" #NAMELC, flash_attn_f32_f16_ ## NAMELC ## _cm2_len, flash_attn_f32_f16_ ## NAMELC ## _cm2_data, "main", 5, sizeof(vk_flash_attn_push_constants), fa_wg_denoms(D,0,TYPE,true), fa_spec_constants(D,0,TYPE,true), fa_rows_cols(D,0,TYPE,true)[1]); \
  1508. #define CREATE_FA(TYPE, NAMELC) \
  1509. CREATE_FA2(TYPE, NAMELC, 64) \
  1510. CREATE_FA2(TYPE, NAMELC, 80) \
  1511. CREATE_FA2(TYPE, NAMELC, 96) \
  1512. CREATE_FA2(TYPE, NAMELC, 112) \
  1513. CREATE_FA2(TYPE, NAMELC, 128) \
  1514. CREATE_FA2(TYPE, NAMELC, 256)
  1515. CREATE_FA(GGML_TYPE_F16, f16)
  1516. CREATE_FA(GGML_TYPE_Q4_0, q4_0)
  1517. CREATE_FA(GGML_TYPE_Q4_1, q4_1)
  1518. CREATE_FA(GGML_TYPE_Q5_0, q5_0)
  1519. CREATE_FA(GGML_TYPE_Q5_1, q5_1)
  1520. CREATE_FA(GGML_TYPE_Q8_0, q8_0)
  1521. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  1522. //CREATE_FA(GGML_TYPE_Q2_K, q2_k)
  1523. //CREATE_FA(GGML_TYPE_Q3_K, q3_k)
  1524. //CREATE_FA(GGML_TYPE_Q4_K, q4_k)
  1525. //CREATE_FA(GGML_TYPE_Q5_K, q5_k)
  1526. //CREATE_FA(GGML_TYPE_Q6_K, q6_k)
  1527. //CREATE_FA(GGML_TYPE_IQ1_S, iq1_s)
  1528. //CREATE_FA(GGML_TYPE_IQ1_M, iq1_m)
  1529. //CREATE_FA(GGML_TYPE_IQ2_XXS, iq2_xxs)
  1530. //CREATE_FA(GGML_TYPE_IQ2_XS, iq2_xs)
  1531. //CREATE_FA(GGML_TYPE_IQ2_S, iq2_s)
  1532. //CREATE_FA(GGML_TYPE_IQ3_XXS, iq3_xxs)
  1533. //CREATE_FA(GGML_TYPE_IQ3_S, iq3_s)
  1534. //CREATE_FA(GGML_TYPE_IQ4_XS, iq4_xs)
  1535. CREATE_FA(GGML_TYPE_IQ4_NL, iq4_nl)
  1536. #undef CREATE_FA
  1537. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1538. #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1539. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1540. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1541. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _cm2_len, NAMELC ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1542. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1543. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1544. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _cm2_len, NAMELC ## _aligned ## F16ACC ## _cm2_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1545. // Create 2 variants, {f16,f32} accumulator
  1546. #define CREATE_MM2(PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1547. CREATE_MM(PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1548. CREATE_MM(PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT) \
  1549. CREATE_MM2(pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3)
  1550. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1551. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1552. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1553. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1554. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1555. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1556. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1557. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1558. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1559. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f16, _f16acc, mmq_wg_denoms_k, warptile_mmq_k, vk_mat_mat_push_constants, 3)
  1560. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1561. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1562. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1563. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1564. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1565. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1566. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1567. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1568. CREATE_MM(pipeline_dequant_mul_mat_mat_f16[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f16, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3)
  1569. CREATE_MM2(pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_id_push_constants, 4)
  1570. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1571. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1572. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1573. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1574. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1575. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1576. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1577. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1578. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1579. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1580. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1581. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1582. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1583. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1584. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1585. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1586. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1587. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1588. CREATE_MM(pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f16, , mmqid_wg_denoms, warptile_mmqid, vk_mat_mat_id_push_constants, 4)
  1589. #undef CREATE_MM
  1590. #undef CREATE_MM2
  1591. } else
  1592. #endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  1593. #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1594. if (device->coopmat_support) {
  1595. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1596. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1597. if (device->mul_mat ## ID ## _l[TYPE]) \
  1598. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1, false, true); \
  1599. if (device->mul_mat ## ID ## _m[TYPE]) \
  1600. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1, false, true); \
  1601. if (device->mul_mat ## ID ## _s[TYPE]) \
  1602. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _coopmat_len, NAMELC ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1, false, true); \
  1603. if (device->mul_mat ## ID ## _l[TYPE]) \
  1604. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align, false, true); \
  1605. if (device->mul_mat ## ID ## _m[TYPE]) \
  1606. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align, false, true); \
  1607. if (device->mul_mat ## ID ## _s[TYPE]) \
  1608. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _coopmat_len, NAMELC ## _aligned ## F16ACC ## _coopmat_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align, false, true); \
  1609. // Create 2 variants, {f16,f32} accumulator
  1610. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1611. if (device->coopmat_acc_f16_support) { \
  1612. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1613. } \
  1614. if (device->coopmat_acc_f32_support) { \
  1615. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1616. } \
  1617. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1618. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1619. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1620. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1621. if (device->coopmat_acc_f16_support) {
  1622. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1623. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1624. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1625. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1626. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1627. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1628. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1629. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1630. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1631. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1632. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1633. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1634. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1635. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1636. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1637. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1638. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1639. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1640. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1641. } else {
  1642. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1643. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1644. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1645. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1646. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1647. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1648. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1649. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1650. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1651. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1652. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1653. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1654. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1655. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1656. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1657. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1658. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1659. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1660. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1661. }
  1662. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1663. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1664. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1665. if (device->coopmat_acc_f16_support) {
  1666. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1667. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1668. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1669. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1670. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1671. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1672. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1673. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1674. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1675. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1676. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1677. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1678. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1679. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1680. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1681. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1682. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1683. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1684. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1685. } else {
  1686. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1687. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1688. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1689. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1690. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1691. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1692. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1693. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1694. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1695. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1696. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1697. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1698. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1699. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1700. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1701. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1702. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1703. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1704. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1705. }
  1706. #undef CREATE_MM2
  1707. #undef CREATE_MM
  1708. } else
  1709. #endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  1710. if (device->fp16) {
  1711. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1712. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1713. if (device->mul_mat ## ID ## _l[TYPE]) \
  1714. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1715. if (device->mul_mat ## ID ## _m[TYPE]) \
  1716. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1717. if (device->mul_mat ## ID ## _s[TYPE]) \
  1718. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1719. if (device->mul_mat ## ID ## _l[TYPE]) \
  1720. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1721. if (device->mul_mat ## ID ## _m[TYPE]) \
  1722. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1723. if (device->mul_mat ## ID ## _s[TYPE]) \
  1724. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _len, NAMELC ## _aligned ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1725. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1726. if (device->mul_mat ## ID ## _l[TYPE]) \
  1727. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1728. if (device->mul_mat ## ID ## _m[TYPE]) \
  1729. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1730. if (device->mul_mat ## ID ## _s[TYPE]) \
  1731. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _len, NAMELC ## F16ACC ## _data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1732. // Create 2 variants, {f16,f32} accumulator
  1733. #define CREATE_MM2(TYPE, PIPELINE_NAME, NAMELC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1734. CREATE_MM(TYPE, PIPELINE_NAME . f16acc, NAMELC, _f16acc, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1735. CREATE_MM(TYPE, PIPELINE_NAME . f32acc, NAMELC, , WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1736. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1737. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1738. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16, matmul_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1739. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_f16_f32, matmul_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1740. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1741. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1742. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1743. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1744. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1745. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f16acc, matmul_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1746. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f16acc, matmul_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1747. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f16acc, matmul_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1748. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f16acc, matmul_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1749. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f16acc, matmul_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1750. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f16acc, matmul_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1751. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f16acc, matmul_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1752. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f16acc, matmul_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1753. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f16acc, matmul_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1754. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f16acc, matmul_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1755. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f16acc, matmul_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1756. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f16acc, matmul_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1757. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f16acc, matmul_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1758. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f16acc, matmul_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1759. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  1760. if (device->integer_dot_product) {
  1761. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0].f16acc, matmul_q4_0_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1762. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1].f16acc, matmul_q4_1_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1763. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0].f16acc, matmul_q5_0_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1764. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1].f16acc, matmul_q5_1_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1765. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0].f16acc, matmul_q8_0_q8_1, _f16acc, mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1766. }
  1767. #endif
  1768. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1769. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16, matmul_id_f16, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1770. CREATE_MM2(GGML_TYPE_F16, pipeline_matmul_id_f16_f32, matmul_id_f16_f32, wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1771. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f16acc, matmul_id_q4_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1772. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f16acc, matmul_id_q4_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1773. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f16acc, matmul_id_q5_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1774. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f16acc, matmul_id_q5_1_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1775. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f16acc, matmul_id_q8_0_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1776. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f16acc, matmul_id_q2_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1777. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f16acc, matmul_id_q3_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1778. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f16acc, matmul_id_q4_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1779. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f16acc, matmul_id_q5_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1780. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f16acc, matmul_id_q6_k_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1781. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f16acc, matmul_id_iq1_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1782. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f16acc, matmul_id_iq1_m_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1783. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f16acc, matmul_id_iq2_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1784. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f16acc, matmul_id_iq2_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1785. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f16acc, matmul_id_iq2_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1786. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f16acc, matmul_id_iq3_xxs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1787. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f16acc, matmul_id_iq3_s_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1788. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f16acc, matmul_id_iq4_xs_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1789. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f16acc, matmul_id_iq4_nl_f32, _f16acc, mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1790. #undef CREATE_MM2
  1791. #undef CREATE_MMQ
  1792. #undef CREATE_MM
  1793. } else {
  1794. // Create 6 variants, {s,m,l}x{unaligned,aligned}
  1795. #define CREATE_MM(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1796. if (device->mul_mat ## ID ## _l[TYPE]) \
  1797. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1798. if (device->mul_mat ## ID ## _m[TYPE]) \
  1799. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1800. if (device->mul_mat ## ID ## _s[TYPE]) \
  1801. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1802. if (device->mul_mat ## ID ## _l[TYPE]) \
  1803. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_l, #NAMELC #F16ACC "_aligned_l", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, l_align); \
  1804. if (device->mul_mat ## ID ## _m[TYPE]) \
  1805. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_m, #NAMELC #F16ACC "_aligned_m", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, m_align); \
  1806. if (device->mul_mat ## ID ## _s[TYPE]) \
  1807. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->a_s, #NAMELC #F16ACC "_aligned_s", NAMELC ## _aligned ## F16ACC ## _fp32_len, NAMELC ## _aligned ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, s_align); \
  1808. #define CREATE_MMQ(TYPE, PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
  1809. if (device->mul_mat ## ID ## _l[TYPE]) \
  1810. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->l, #NAMELC #F16ACC "_l", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), l_ ## WG_DENOMS, l_ ## WARPTILE, 1); \
  1811. if (device->mul_mat ## ID ## _m[TYPE]) \
  1812. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->m, #NAMELC #F16ACC "_m", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), m_ ## WG_DENOMS, m_ ## WARPTILE, 1); \
  1813. if (device->mul_mat ## ID ## _s[TYPE]) \
  1814. ggml_vk_create_pipeline(device, device-> PIPELINE_NAME ->s, #NAMELC #F16ACC "_s", NAMELC ## F16ACC ## _fp32_len, NAMELC ## F16ACC ## _fp32_data, "main", PARAMCOUNT, sizeof(PUSHCONST), s_ ## WG_DENOMS, s_ ## WARPTILE, 1); \
  1815. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32, matmul_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1816. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_f32_f16, matmul_f32_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1817. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16.f32acc, matmul_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1818. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_f16_f32.f32acc, matmul_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 3, );
  1819. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1820. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1821. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1822. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1823. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1824. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q2_K].f32acc, matmul_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1825. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q3_K].f32acc, matmul_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1826. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q4_K].f32acc, matmul_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1827. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q5_K].f32acc, matmul_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1828. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat[GGML_TYPE_Q6_K].f32acc, matmul_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1829. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_S].f32acc, matmul_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1830. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ1_M].f32acc, matmul_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1831. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XXS].f32acc, matmul_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1832. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_XS].f32acc, matmul_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1833. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ2_S].f32acc, matmul_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1834. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_XXS].f32acc, matmul_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1835. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ3_S].f32acc, matmul_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1836. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_XS].f32acc, matmul_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1837. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat[GGML_TYPE_IQ4_NL].f32acc, matmul_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_push_constants, 3, );
  1838. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  1839. if (device->integer_dot_product) {
  1840. CREATE_MMQ(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_0].f32acc, matmul_q4_0_q8_1, , mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1841. CREATE_MMQ(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q4_1].f32acc, matmul_q4_1_q8_1, , mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1842. CREATE_MMQ(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_0].f32acc, matmul_q5_0_q8_1, , mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1843. CREATE_MMQ(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q5_1].f32acc, matmul_q5_1_q8_1, , mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1844. CREATE_MMQ(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_q8_1[GGML_TYPE_Q8_0].f32acc, matmul_q8_0_q8_1, , mmq_wg_denoms, warptile_mmq_int, vk_mat_mat_push_constants, 3, );
  1845. }
  1846. #endif
  1847. CREATE_MM(GGML_TYPE_F32, pipeline_matmul_id_f32, matmul_id_f32_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1848. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16.f32acc, matmul_id_f16, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1849. CREATE_MM(GGML_TYPE_F16, pipeline_matmul_id_f16_f32.f32acc, matmul_id_f16_f32, , wg_denoms, warptile, vk_mat_mat_push_constants, 4, _id);
  1850. CREATE_MM(GGML_TYPE_Q4_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_0].f32acc, matmul_id_q4_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1851. CREATE_MM(GGML_TYPE_Q4_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_1].f32acc, matmul_id_q4_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1852. CREATE_MM(GGML_TYPE_Q5_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_0].f32acc, matmul_id_q5_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1853. CREATE_MM(GGML_TYPE_Q5_1, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_1].f32acc, matmul_id_q5_1_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1854. CREATE_MM(GGML_TYPE_Q8_0, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q8_0].f32acc, matmul_id_q8_0_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1855. CREATE_MM(GGML_TYPE_Q2_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q2_K].f32acc, matmul_id_q2_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1856. CREATE_MM(GGML_TYPE_Q3_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q3_K].f32acc, matmul_id_q3_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1857. CREATE_MM(GGML_TYPE_Q4_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q4_K].f32acc, matmul_id_q4_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1858. CREATE_MM(GGML_TYPE_Q5_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q5_K].f32acc, matmul_id_q5_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1859. CREATE_MM(GGML_TYPE_Q6_K, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_Q6_K].f32acc, matmul_id_q6_k_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1860. CREATE_MM(GGML_TYPE_IQ1_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_S].f32acc, matmul_id_iq1_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1861. CREATE_MM(GGML_TYPE_IQ1_M, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ1_M].f32acc, matmul_id_iq1_m_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1862. CREATE_MM(GGML_TYPE_IQ2_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XXS].f32acc, matmul_id_iq2_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1863. CREATE_MM(GGML_TYPE_IQ2_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_XS].f32acc, matmul_id_iq2_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1864. CREATE_MM(GGML_TYPE_IQ2_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ2_S].f32acc, matmul_id_iq2_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1865. CREATE_MM(GGML_TYPE_IQ3_XXS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_XXS].f32acc, matmul_id_iq3_xxs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1866. CREATE_MM(GGML_TYPE_IQ3_S, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ3_S].f32acc, matmul_id_iq3_s_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1867. CREATE_MM(GGML_TYPE_IQ4_XS, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_XS].f32acc, matmul_id_iq4_xs_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1868. CREATE_MM(GGML_TYPE_IQ4_NL, pipeline_dequant_mul_mat_mat_id[GGML_TYPE_IQ4_NL].f32acc, matmul_id_iq4_nl_f32, , mmq_wg_denoms, warptile_mmq, vk_mat_mat_id_push_constants, 4, _id);
  1869. #undef CREATE_MM
  1870. }
  1871. // mul mat vec
  1872. // the number of rows computed per shader depends on GPU model and quant
  1873. uint32_t rm_stdq = 1;
  1874. uint32_t rm_kq = 2;
  1875. if (device->vendor_id == VK_VENDOR_ID_AMD) {
  1876. if (device->architecture == AMD_GCN) {
  1877. rm_stdq = 2;
  1878. rm_kq = 4;
  1879. }
  1880. } else if (device->vendor_id == VK_VENDOR_ID_INTEL)
  1881. rm_stdq = 2;
  1882. uint32_t rm_iq = 2 * rm_kq;
  1883. for (uint32_t i = 0; i < mul_mat_vec_max_cols; ++i) {
  1884. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f32_f32_"+std::to_string(i+1), mul_mat_vec_f32_f32_f32_len, mul_mat_vec_f32_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1885. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f32_f32_"+std::to_string(i+1), mul_mat_vec_f16_f32_f32_len, mul_mat_vec_f16_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1886. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f32_f32_len, mul_mat_vec_q4_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1887. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f32_f32_len, mul_mat_vec_q4_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1888. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f32_f32_len, mul_mat_vec_q5_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1889. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f32_f32_len, mul_mat_vec_q5_1_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1890. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f32_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f32_f32_len, mul_mat_vec_q8_0_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  1891. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f32_f32_len, mul_mat_vec_q2_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1892. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f32_f32_len, mul_mat_vec_q3_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1893. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f32_f32_len, mul_mat_vec_q4_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1894. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f32_f32_len, mul_mat_vec_q5_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1895. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f32_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f32_f32_len, mul_mat_vec_q6_k_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1896. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f32_f32_len, mul_mat_vec_iq1_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1897. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f32_f32_len, mul_mat_vec_iq1_m_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1898. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f32_f32_len, mul_mat_vec_iq2_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1899. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f32_f32_len, mul_mat_vec_iq2_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1900. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f32_f32_len, mul_mat_vec_iq2_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1901. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f32_f32_len, mul_mat_vec_iq3_xxs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1902. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f32_f32_len, mul_mat_vec_iq3_s_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1903. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f32_f32_len, mul_mat_vec_iq4_xs_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1904. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f32_f32_len, mul_mat_vec_iq4_nl_f32_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1905. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32_"+std::to_string(i+1), mul_mat_vec_f32_f16_f32_len, mul_mat_vec_f32_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1906. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32_"+std::to_string(i+1), mul_mat_vec_f16_f16_f32_len, mul_mat_vec_f16_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {device->subgroup_size, 2, i+1}, 1);
  1907. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_0][i], "mul_mat_vec_q4_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_0_f16_f32_len, mul_mat_vec_q4_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1908. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_1][i], "mul_mat_vec_q4_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_1_f16_f32_len, mul_mat_vec_q4_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1909. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_0][i], "mul_mat_vec_q5_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_0_f16_f32_len, mul_mat_vec_q5_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1910. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_1][i], "mul_mat_vec_q5_1_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_1_f16_f32_len, mul_mat_vec_q5_1_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq, i+1}, 1, true);
  1911. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q8_0][i], "mul_mat_vec_q8_0_f16_f32_"+std::to_string(i+1), mul_mat_vec_q8_0_f16_f32_len, mul_mat_vec_q8_0_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq, i+1}, 1, true);
  1912. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q2_K][i], "mul_mat_vec_q2_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q2_k_f16_f32_len, mul_mat_vec_q2_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1913. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q3_K][i], "mul_mat_vec_q3_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q3_k_f16_f32_len, mul_mat_vec_q3_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1914. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q4_K][i], "mul_mat_vec_q4_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q4_k_f16_f32_len, mul_mat_vec_q4_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1915. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q5_K][i], "mul_mat_vec_q5_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q5_k_f16_f32_len, mul_mat_vec_q5_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1916. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_Q6_K][i], "mul_mat_vec_q6_k_f16_f32_"+std::to_string(i+1), mul_mat_vec_q6_k_f16_f32_len, mul_mat_vec_q6_k_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq, i+1}, 1, true);
  1917. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_S][i], "mul_mat_vec_iq1_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_s_f16_f32_len, mul_mat_vec_iq1_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1918. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ1_M][i], "mul_mat_vec_iq1_m_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq1_m_f16_f32_len, mul_mat_vec_iq1_m_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1919. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XXS][i], "mul_mat_vec_iq2_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xxs_f16_f32_len, mul_mat_vec_iq2_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1920. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_XS][i], "mul_mat_vec_iq2_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_xs_f16_f32_len, mul_mat_vec_iq2_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1921. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ2_S][i], "mul_mat_vec_iq2_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq2_s_f16_f32_len, mul_mat_vec_iq2_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1922. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_XXS][i], "mul_mat_vec_iq3_xxs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_xxs_f16_f32_len, mul_mat_vec_iq3_xxs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1923. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ3_S][i], "mul_mat_vec_iq3_s_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq3_s_f16_f32_len, mul_mat_vec_iq3_s_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1924. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_XS][i], "mul_mat_vec_iq4_xs_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_xs_f16_f32_len, mul_mat_vec_iq4_xs_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1925. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32_"+std::to_string(i+1), mul_mat_vec_iq4_nl_f16_f32_len, mul_mat_vec_iq4_nl_f16_f32_data, "main", 3, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq, i+1}, 1, true);
  1926. }
  1927. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F32 ], "mul_mat_vec_id_f32_f32", mul_mat_vec_id_f32_f32_len, mul_mat_vec_id_f32_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  1928. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_F16 ], "mul_mat_vec_id_f16_f32", mul_mat_vec_id_f16_f32_len, mul_mat_vec_id_f16_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2, 1, 1}, {device->subgroup_size, 2}, 1);
  1929. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_0], "mul_mat_vec_id_q4_0_f32", mul_mat_vec_id_q4_0_f32_len, mul_mat_vec_id_q4_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1930. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_1], "mul_mat_vec_id_q4_1_f32", mul_mat_vec_id_q4_1_f32_len, mul_mat_vec_id_q4_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1931. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_0], "mul_mat_vec_id_q5_0_f32", mul_mat_vec_id_q5_0_f32_len, mul_mat_vec_id_q5_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1932. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_1], "mul_mat_vec_id_q5_1_f32", mul_mat_vec_id_q5_1_f32_len, mul_mat_vec_id_q5_1_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {2*rm_stdq, 1, 1}, {device->subgroup_size, 2*rm_stdq}, 1, true);
  1933. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q8_0], "mul_mat_vec_id_q8_0_f32", mul_mat_vec_id_q8_0_f32_len, mul_mat_vec_id_q8_0_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {1*rm_stdq, 1, 1}, {device->subgroup_size, 1*rm_stdq}, 1, true);
  1934. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q2_K], "mul_mat_vec_id_q2_k_f32", mul_mat_vec_id_q2_k_f32_len, mul_mat_vec_id_q2_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1935. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q3_K], "mul_mat_vec_id_q3_k_f32", mul_mat_vec_id_q3_k_f32_len, mul_mat_vec_id_q3_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1936. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q4_K], "mul_mat_vec_id_q4_k_f32", mul_mat_vec_id_q4_k_f32_len, mul_mat_vec_id_q4_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1937. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q5_K], "mul_mat_vec_id_q5_k_f32", mul_mat_vec_id_q5_k_f32_len, mul_mat_vec_id_q5_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1938. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_Q6_K], "mul_mat_vec_id_q6_k_f32", mul_mat_vec_id_q6_k_f32_len, mul_mat_vec_id_q6_k_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_kq, 1, 1}, {subgroup_size_16, rm_kq}, 1, true);
  1939. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_S], "mul_mat_vec_id_iq1_s_f32", mul_mat_vec_id_iq1_s_f32_len, mul_mat_vec_id_iq1_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1940. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ1_M], "mul_mat_vec_id_iq1_m_f32", mul_mat_vec_id_iq1_m_f32_len, mul_mat_vec_id_iq1_m_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1941. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XXS], "mul_mat_vec_id_iq2_xxs_f32", mul_mat_vec_id_iq2_xxs_f32_len, mul_mat_vec_id_iq2_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1942. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_XS], "mul_mat_vec_id_iq2_xs_f32", mul_mat_vec_id_iq2_xs_f32_len, mul_mat_vec_id_iq2_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1943. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ2_S], "mul_mat_vec_id_iq2_s_f32", mul_mat_vec_id_iq2_s_f32_len, mul_mat_vec_id_iq2_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1944. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_XXS], "mul_mat_vec_id_iq3_xxs_f32", mul_mat_vec_id_iq3_xxs_f32_len, mul_mat_vec_id_iq3_xxs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1945. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ3_S], "mul_mat_vec_id_iq3_s_f32", mul_mat_vec_id_iq3_s_f32_len, mul_mat_vec_id_iq3_s_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1946. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_XS], "mul_mat_vec_id_iq4_xs_f32", mul_mat_vec_id_iq4_xs_f32_len, mul_mat_vec_id_iq4_xs_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1947. ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[GGML_TYPE_IQ4_NL], "mul_mat_vec_id_iq4_nl_f32", mul_mat_vec_id_iq4_nl_f32_len, mul_mat_vec_id_iq4_nl_f32_data, "main", 4, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {subgroup_size_16, rm_iq}, 1, true);
  1948. // dequant shaders
  1949. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_F32 ], "f32_to_f16", dequant_f32_len, dequant_f32_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1950. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_0], "dequant_q4_0", dequant_q4_0_len, dequant_q4_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1951. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_1], "dequant_q4_1", dequant_q4_1_len, dequant_q4_1_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1952. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_0], "dequant_q5_0", dequant_q5_0_len, dequant_q5_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1953. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_1], "dequant_q5_1", dequant_q5_1_len, dequant_q5_1_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1954. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q8_0], "dequant_q8_0", dequant_q8_0_len, dequant_q8_0_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1955. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q2_K], "dequant_q2_k", dequant_q2_k_len, dequant_q2_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1956. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q3_K], "dequant_q3_k", dequant_q3_k_len, dequant_q3_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1957. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q4_K], "dequant_q4_k", dequant_q4_k_len, dequant_q4_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1958. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q5_K], "dequant_q5_k", dequant_q5_k_len, dequant_q5_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1959. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_Q6_K], "dequant_q6_k", dequant_q6_k_len, dequant_q6_k_data, "main", 2, 5 * sizeof(uint32_t), {256 * 64, 1, 1}, {}, 1);
  1960. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_S], "dequant_iq1_s", dequant_iq1_s_len, dequant_iq1_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1961. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ1_M], "dequant_iq1_m", dequant_iq1_m_len, dequant_iq1_m_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1962. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XXS], "dequant_iq2_xxs", dequant_iq2_xxs_len, dequant_iq2_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1963. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_XS], "dequant_iq2_xs", dequant_iq2_xs_len, dequant_iq2_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1964. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ2_S], "dequant_iq2_s", dequant_iq2_s_len, dequant_iq2_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1965. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_XXS], "dequant_iq3_xxs", dequant_iq3_xxs_len, dequant_iq3_xxs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1966. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ3_S], "dequant_iq3_s", dequant_iq3_s_len, dequant_iq3_s_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1967. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_XS], "dequant_iq4_xs", dequant_iq4_xs_len, dequant_iq4_xs_data, "main", 2, 5 * sizeof(uint32_t), {256 * 32, 1, 1}, {}, 1);
  1968. ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_NL], "dequant_iq4_nl", dequant_iq4_nl_len, dequant_iq4_nl_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
  1969. // get_rows
  1970. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F32 ], "get_rows_f32", get_rows_f32_len, get_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1971. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F16 ], "get_rows_f16", get_rows_f16_len, get_rows_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1972. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_0], "get_rows_q4_0", get_rows_q4_0_len, get_rows_q4_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1973. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q4_1], "get_rows_q4_1", get_rows_q4_1_len, get_rows_q4_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1974. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_0], "get_rows_q5_0", get_rows_q5_0_len, get_rows_q5_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1975. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q5_1], "get_rows_q5_1", get_rows_q5_1_len, get_rows_q5_1_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1976. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_Q8_0], "get_rows_q8_0", get_rows_q8_0_len, get_rows_q8_0_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1977. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_S], "get_rows_iq1_s", get_rows_iq1_s_len, get_rows_iq1_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1978. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ1_M], "get_rows_iq1_m", get_rows_iq1_m_len, get_rows_iq1_m_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1979. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs", get_rows_iq2_xxs_len, get_rows_iq2_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1980. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs", get_rows_iq2_xs_len, get_rows_iq2_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1981. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ2_S], "get_rows_iq2_s", get_rows_iq2_s_len, get_rows_iq2_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1982. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs", get_rows_iq3_xxs_len, get_rows_iq3_xxs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1983. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ3_S], "get_rows_iq3_s", get_rows_iq3_s_len, get_rows_iq3_s_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1984. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs", get_rows_iq4_xs_len, get_rows_iq4_xs_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1985. ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl", get_rows_iq4_nl_len, get_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1986. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F32 ], "get_rows_f32_f32", get_rows_f32_f32_len, get_rows_f32_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1987. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F16 ], "get_rows_f16_f32", get_rows_f16_f32_len, get_rows_f16_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
  1988. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_0], "get_rows_q4_0_f32", get_rows_q4_0_f32_len, get_rows_q4_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1989. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q4_1], "get_rows_q4_1_f32", get_rows_q4_1_f32_len, get_rows_q4_1_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1990. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_0], "get_rows_q5_0_f32", get_rows_q5_0_f32_len, get_rows_q5_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1991. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q5_1], "get_rows_q5_1_f32", get_rows_q5_1_f32_len, get_rows_q5_1_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1992. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_Q8_0], "get_rows_q8_0_f32", get_rows_q8_0_f32_len, get_rows_q8_0_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1993. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_S], "get_rows_iq1_s_f32", get_rows_iq1_s_f32_len, get_rows_iq1_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1994. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ1_M], "get_rows_iq1_m_f32", get_rows_iq1_m_f32_len, get_rows_iq1_m_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1995. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XXS], "get_rows_iq2_xxs_f32", get_rows_iq2_xxs_f32_len, get_rows_iq2_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1996. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_XS], "get_rows_iq2_xs_f32", get_rows_iq2_xs_f32_len, get_rows_iq2_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1997. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ2_S], "get_rows_iq2_s_f32", get_rows_iq2_s_f32_len, get_rows_iq2_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1998. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_XXS], "get_rows_iq3_xxs_f32", get_rows_iq3_xxs_f32_len, get_rows_iq3_xxs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  1999. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ3_S], "get_rows_iq3_s_f32", get_rows_iq3_s_f32_len, get_rows_iq3_s_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2000. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_XS], "get_rows_iq4_xs_f32", get_rows_iq4_xs_f32_len, get_rows_iq4_xs_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2001. ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl_f32", get_rows_iq4_nl_f32_len, get_rows_iq4_nl_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
  2002. ggml_vk_create_pipeline(device, device->pipeline_matmul_split_k_reduce, "split_k_reduce", split_k_reduce_len, split_k_reduce_data, "main", 2, 2 * sizeof(uint32_t), {256 * 4, 1, 1}, {}, 1);
  2003. ggml_vk_create_pipeline(device, device->pipeline_quantize_q8_1, "quantize_q8_1", quantize_q8_1_len, quantize_q8_1_data, "main", 2, 1 * sizeof(uint32_t), {32 * device->subgroup_size / 8, 1, 1}, { device->subgroup_size }, 1);
  2004. for (uint32_t i = 0; i < p021_max_gqa_ratio; ++i) {
  2005. if (device->subgroup_add && device->subgroup_require_full_support) {
  2006. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_subgroup_add_len, mul_mat_vec_p021_f16_f32_subgroup_add_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true, true);
  2007. } else {
  2008. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_p021_f16_f32[i], "mul_mat_vec_p021_f16_f32"+std::to_string(i+1), mul_mat_vec_p021_f16_f32_len, mul_mat_vec_p021_f16_f32_data, "main", 3, 6 * sizeof(uint32_t), {1, 1, 1}, {device->subgroup_size, i + 1}, 1, true);
  2009. }
  2010. }
  2011. ggml_vk_create_pipeline(device, device->pipeline_mul_mat_vec_nc_f16_f32, "mul_mat_vec_nc_f16_f32", mul_mat_vec_nc_f16_f32_len, mul_mat_vec_nc_f16_f32_data, "main", 3, 7 * sizeof(uint32_t), {1, 1, 1}, {}, 1);
  2012. ggml_vk_create_pipeline(device, device->pipeline_norm_f32, "norm_f32", norm_f32_len, norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2013. ggml_vk_create_pipeline(device, device->pipeline_group_norm_f32, "group_norm_f32", group_norm_f32_len, group_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2014. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2015. ggml_vk_create_pipeline(device, device->pipeline_rms_norm_back_f32, "rms_norm_back_f32", rms_norm_back_f32_len, rms_norm_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2016. ggml_vk_create_pipeline(device, device->pipeline_l2_norm_f32, "l2_norm_f32", l2_norm_f32_len, l2_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
  2017. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_f32, "cpy_f32_f32", cpy_f32_f32_len, cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2018. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_f16, "cpy_f32_f16", cpy_f32_f16_len, cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2019. ggml_vk_create_pipeline(device, device->pipeline_cpy_f16_f16, "cpy_f16_f16", cpy_f16_f16_len, cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2020. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f32, "contig_cpy_f32_f32", contig_cpy_f32_f32_len, contig_cpy_f32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2021. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_f16, "contig_cpy_f32_f16", contig_cpy_f32_f16_len, contig_cpy_f32_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2022. ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f16_f16, "contig_cpy_f16_f16", contig_cpy_f16_f16_len, contig_cpy_f16_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2023. if (device->float_controls_rte_fp16) {
  2024. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_rte_len, cpy_f32_q4_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2025. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_rte_len, cpy_f32_q4_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2026. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_rte_len, cpy_f32_q5_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2027. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_rte_len, cpy_f32_q5_1_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2028. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_rte_len, cpy_f32_q8_0_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2029. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_rte_len, cpy_f32_iq4_nl_rte_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2030. } else {
  2031. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_0], "cpy_f32_q4_0", cpy_f32_q4_0_len, cpy_f32_q4_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2032. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q4_1], "cpy_f32_q4_1", cpy_f32_q4_1_len, cpy_f32_q4_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2033. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_0], "cpy_f32_q5_0", cpy_f32_q5_0_len, cpy_f32_q5_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2034. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q5_1], "cpy_f32_q5_1", cpy_f32_q5_1_len, cpy_f32_q5_1_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2035. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_Q8_0], "cpy_f32_q8_0", cpy_f32_q8_0_len, cpy_f32_q8_0_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2036. ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_quant[GGML_TYPE_IQ4_NL], "cpy_f32_iq4_nl", cpy_f32_iq4_nl_len, cpy_f32_iq4_nl_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2037. }
  2038. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_0], "cpy_q4_0_f32", cpy_q4_0_f32_len, cpy_q4_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_0), 1, 1}, {}, 1);
  2039. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q4_1], "cpy_q4_1_f32", cpy_q4_1_f32_len, cpy_q4_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q4_1), 1, 1}, {}, 1);
  2040. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_0], "cpy_q5_0_f32", cpy_q5_0_f32_len, cpy_q5_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_0), 1, 1}, {}, 1);
  2041. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q5_1], "cpy_q5_1_f32", cpy_q5_1_f32_len, cpy_q5_1_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q5_1), 1, 1}, {}, 1);
  2042. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_Q8_0], "cpy_q8_0_f32", cpy_q8_0_f32_len, cpy_q8_0_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_Q8_0), 1, 1}, {}, 1);
  2043. ggml_vk_create_pipeline(device, device->pipeline_cpy_quant_f32[GGML_TYPE_IQ4_NL], "cpy_iq4_nl_f32", cpy_iq4_nl_f32_len, cpy_iq4_nl_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {(uint32_t)ggml_blck_size(GGML_TYPE_IQ4_NL), 1, 1}, {}, 1);
  2044. ggml_vk_create_pipeline(device, device->pipeline_add_f32, "add_f32", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2045. ggml_vk_create_pipeline(device, device->pipeline_add_f32_norepeat, "add_f32_norepeat", add_f32_len, add_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2046. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16, "add_f16_f32_f16", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2047. ggml_vk_create_pipeline(device, device->pipeline_add_f16_f32_f16_norepeat, "add_f16_f32_f16_norepeat", add_f16_f32_f16_len, add_f16_f32_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2048. ggml_vk_create_pipeline(device, device->pipeline_acc_f32, "acc_f32", acc_f32_len, acc_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2049. ggml_vk_create_pipeline(device, device->pipeline_sub_f32, "sub_f32", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2050. ggml_vk_create_pipeline(device, device->pipeline_sub_f32_norepeat, "sub_f32_norepeat", sub_f32_len, sub_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2051. ggml_vk_create_pipeline(device, device->pipeline_mul_f32, "mul_f32", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2052. ggml_vk_create_pipeline(device, device->pipeline_mul_f32_norepeat, "mul_f32_norepeat", mul_f32_len, mul_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2053. ggml_vk_create_pipeline(device, device->pipeline_div_f32, "div_f32", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {0}, 1);
  2054. ggml_vk_create_pipeline(device, device->pipeline_div_f32_norepeat, "div_f32_norepeat", div_f32_len, div_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {1}, 1);
  2055. ggml_vk_create_pipeline(device, device->pipeline_concat_f32, "concat_f32", concat_f32_len, concat_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2056. ggml_vk_create_pipeline(device, device->pipeline_concat_f16, "concat_f16", concat_f16_len, concat_f16_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2057. ggml_vk_create_pipeline(device, device->pipeline_concat_i32, "concat_i32", concat_i32_len, concat_i32_data, "main", 3, sizeof(vk_op_binary_push_constants), {512, 1, 1}, {}, 1);
  2058. ggml_vk_create_pipeline(device, device->pipeline_upscale_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {}, 1);
  2059. ggml_vk_create_pipeline(device, device->pipeline_scale_f32, "scale_f32", scale_f32_len, scale_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2060. ggml_vk_create_pipeline(device, device->pipeline_sqr_f32, "sqr_f32", sqr_f32_len, sqr_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2061. ggml_vk_create_pipeline(device, device->pipeline_sin_f32, "sin_f32", sin_f32_len, sin_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2062. ggml_vk_create_pipeline(device, device->pipeline_cos_f32, "cos_f32", cos_f32_len, cos_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2063. ggml_vk_create_pipeline(device, device->pipeline_clamp_f32, "clamp_f32", clamp_f32_len, clamp_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2064. ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2065. ggml_vk_create_pipeline(device, device->pipeline_repeat_f32, "repeat_f32", repeat_f32_len, repeat_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2066. ggml_vk_create_pipeline(device, device->pipeline_repeat_back_f32, "repeat_back_f32", repeat_back_f32_len, repeat_back_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
  2067. ggml_vk_create_pipeline(device, device->pipeline_gelu_f32, "gelu_f32", gelu_f32_len, gelu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2068. ggml_vk_create_pipeline(device, device->pipeline_gelu_quick_f32, "gelu_quick_f32", gelu_quick_f32_len, gelu_quick_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2069. ggml_vk_create_pipeline(device, device->pipeline_silu_f32, "silu_f32", silu_f32_len, silu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2070. ggml_vk_create_pipeline(device, device->pipeline_silu_back_f32, "silu_back_f32", silu_back_f32_len, silu_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2071. ggml_vk_create_pipeline(device, device->pipeline_relu_f32, "relu_f32", relu_f32_len, relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2072. ggml_vk_create_pipeline(device, device->pipeline_leaky_relu_f32, "leaky_relu_f32", leaky_relu_f32_len, leaky_relu_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2073. ggml_vk_create_pipeline(device, device->pipeline_tanh_f32, "tanh_f32", tanh_f32_len, tanh_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2074. ggml_vk_create_pipeline(device, device->pipeline_sigmoid_f32, "sigmoid_f32", sigmoid_f32_len, sigmoid_f32_data, "main", 2, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2075. ggml_vk_create_pipeline(device, device->pipeline_diag_mask_inf_f32, "diag_mask_inf_f32", diag_mask_inf_f32_len, diag_mask_inf_f32_data, "main", 2, sizeof(vk_op_diag_mask_push_constants), {1, 512, 1}, {}, 1, true);
  2076. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32, "soft_max_f32", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2077. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_wg512, "soft_max_f32_wg512", soft_max_f32_len, soft_max_f32_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2078. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16, "soft_max_f32_f16", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2079. ggml_vk_create_pipeline(device, device->pipeline_soft_max_f32_f16_wg512, "soft_max_f32_f16_wg512", soft_max_f32_f16_len, soft_max_f32_f16_data, "main", 3, sizeof(vk_op_soft_max_push_constants), {1, 1, 1}, { 512 }, 1);
  2080. ggml_vk_create_pipeline(device, device->pipeline_soft_max_back_f32, "soft_max_back_f32", soft_max_back_f32_len, soft_max_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2081. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f32, "rope_norm_f32", rope_norm_f32_len, rope_norm_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2082. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f32, "rope_neox_f32", rope_neox_f32_len, rope_neox_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2083. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f32, "rope_multi_f32", rope_multi_f32_len, rope_multi_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2084. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f32, "rope_vision_f32", rope_vision_f32_len, rope_vision_f32_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2085. if (device->float_controls_rte_fp16) {
  2086. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_rte_len, rope_norm_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2087. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_rte_len, rope_neox_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2088. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_rte_len, rope_multi_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2089. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_rte_len, rope_vision_f16_rte_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2090. } else {
  2091. ggml_vk_create_pipeline(device, device->pipeline_rope_norm_f16, "rope_norm_f16", rope_norm_f16_len, rope_norm_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2092. ggml_vk_create_pipeline(device, device->pipeline_rope_neox_f16, "rope_neox_f16", rope_neox_f16_len, rope_neox_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2093. ggml_vk_create_pipeline(device, device->pipeline_rope_multi_f16, "rope_multi_f16", rope_multi_f16_len, rope_multi_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2094. ggml_vk_create_pipeline(device, device->pipeline_rope_vision_f16, "rope_vision_f16", rope_vision_f16_len, rope_vision_f16_data, "main", 4, sizeof(vk_op_rope_push_constants), {1, 512, 1}, {}, 1);
  2095. }
  2096. ggml_vk_create_pipeline(device, device->pipeline_argsort_f32, "argsort_f32", argsort_f32_len, argsort_f32_data, "main", 2, sizeof(vk_op_argsort_push_constants), {1024, 1, 1}, {}, 1);
  2097. ggml_vk_create_pipeline(device, device->pipeline_argmax_f32, "argmax_f32", argmax_f32_len, argmax_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2098. ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
  2099. ggml_vk_create_pipeline(device, device->pipeline_count_equal_i32, "count_equal_i32", count_equal_i32_len, count_equal_i32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, { device->subgroup_size }, 1);
  2100. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2101. if (device->float_controls_rte_fp16) {
  2102. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2103. } else {
  2104. ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
  2105. }
  2106. ggml_vk_create_pipeline(device, device->pipeline_timestep_embedding_f32, "timestep_embedding_f32", timestep_embedding_f32_len, timestep_embedding_f32_data, "main", 2, sizeof(vk_op_timestep_embedding_push_constants), {256, 1, 1}, {}, 1);
  2107. ggml_vk_create_pipeline(device, device->pipeline_pool2d_f32, "pool2d_f32", pool2d_f32_len, pool2d_f32_data, "main", 2, sizeof(vk_op_pool2d_push_constants), {512, 1, 1}, {}, 1);
  2108. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv6_f32, "rwkv_wkv6_f32", rwkv_wkv6_f32_len, rwkv_wkv6_f32_data, "main", 7, sizeof(vk_op_rwkv_wkv6_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2109. ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv7_f32, "rwkv_wkv7_f32", rwkv_wkv7_f32_len, rwkv_wkv7_f32_data, "main", 8, sizeof(vk_op_rwkv_wkv7_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
  2110. ggml_vk_create_pipeline(device, device->pipeline_opt_step_adamw_f32, "opt_step_adamw_f32", opt_step_adamw_f32_len, opt_step_adamw_f32_data, "main", 5, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
  2111. for (auto &c : compiles) {
  2112. c.wait();
  2113. }
  2114. device->need_compiles = false;
  2115. }
  2116. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch);
  2117. static vk_device ggml_vk_get_device(size_t idx) {
  2118. VK_LOG_DEBUG("ggml_vk_get_device(" << idx << ")");
  2119. if (vk_instance.devices[idx] == nullptr) {
  2120. VK_LOG_DEBUG("Initializing new vk_device");
  2121. vk_device device = std::make_shared<vk_device_struct>();
  2122. vk_instance.devices[idx] = device;
  2123. #ifdef GGML_VULKAN_MEMORY_DEBUG
  2124. device->memory_logger = std::unique_ptr<vk_memory_logger>(new vk_memory_logger());
  2125. #endif
  2126. #ifdef GGML_VULKAN_PERF
  2127. device->perf_logger = std::unique_ptr<vk_perf_logger>(new vk_perf_logger());
  2128. #endif
  2129. size_t dev_num = vk_instance.device_indices[idx];
  2130. std::vector<vk::PhysicalDevice> physical_devices = vk_instance.instance.enumeratePhysicalDevices();
  2131. if (dev_num >= physical_devices.size()) {
  2132. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2133. throw std::runtime_error("Device not found");
  2134. }
  2135. device->physical_device = physical_devices[dev_num];
  2136. const std::vector<vk::ExtensionProperties> ext_props = device->physical_device.enumerateDeviceExtensionProperties();
  2137. device->architecture = get_device_architecture(device->physical_device);
  2138. const char* GGML_VK_PREFER_HOST_MEMORY = getenv("GGML_VK_PREFER_HOST_MEMORY");
  2139. device->prefer_host_memory = GGML_VK_PREFER_HOST_MEMORY != nullptr;
  2140. bool fp16_storage = false;
  2141. bool fp16_compute = false;
  2142. bool maintenance4_support = false;
  2143. bool sm_builtins = false;
  2144. bool amd_shader_core_properties2 = false;
  2145. bool pipeline_robustness = false;
  2146. bool coopmat2_support = false;
  2147. device->coopmat_support = false;
  2148. device->integer_dot_product = false;
  2149. for (const auto& properties : ext_props) {
  2150. if (strcmp("VK_KHR_maintenance4", properties.extensionName) == 0) {
  2151. maintenance4_support = true;
  2152. } else if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2153. fp16_storage = true;
  2154. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2155. fp16_compute = true;
  2156. } else if (strcmp("VK_NV_shader_sm_builtins", properties.extensionName) == 0) {
  2157. sm_builtins = true;
  2158. } else if (strcmp("VK_AMD_shader_core_properties2", properties.extensionName) == 0) {
  2159. amd_shader_core_properties2 = true;
  2160. } else if (strcmp("VK_EXT_pipeline_robustness", properties.extensionName) == 0) {
  2161. pipeline_robustness = true;
  2162. } else if (strcmp("VK_EXT_subgroup_size_control", properties.extensionName) == 0) {
  2163. device->subgroup_size_control = true;
  2164. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2165. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2166. device->coopmat_support = true;
  2167. device->coopmat_m = 0;
  2168. device->coopmat_n = 0;
  2169. device->coopmat_k = 0;
  2170. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2171. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2172. coopmat2_support = true;
  2173. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2174. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2175. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2176. device->integer_dot_product = true;
  2177. #endif
  2178. }
  2179. }
  2180. vk::PhysicalDeviceProperties2 props2;
  2181. vk::PhysicalDeviceMaintenance3Properties props3;
  2182. vk::PhysicalDeviceMaintenance4Properties props4;
  2183. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2184. vk::PhysicalDeviceDriverProperties driver_props;
  2185. vk::PhysicalDeviceShaderSMBuiltinsPropertiesNV sm_props;
  2186. vk::PhysicalDeviceShaderCoreProperties2AMD amd_shader_core_properties2_props;
  2187. vk::PhysicalDeviceVulkan11Properties vk11_props;
  2188. vk::PhysicalDeviceVulkan12Properties vk12_props;
  2189. vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_size_control_props;
  2190. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2191. props2.pNext = &props3;
  2192. props3.pNext = &subgroup_props;
  2193. subgroup_props.pNext = &driver_props;
  2194. driver_props.pNext = &vk11_props;
  2195. vk11_props.pNext = &vk12_props;
  2196. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_props;
  2197. if (maintenance4_support) {
  2198. last_struct->pNext = (VkBaseOutStructure *)&props4;
  2199. last_struct = (VkBaseOutStructure *)&props4;
  2200. }
  2201. if (sm_builtins) {
  2202. last_struct->pNext = (VkBaseOutStructure *)&sm_props;
  2203. last_struct = (VkBaseOutStructure *)&sm_props;
  2204. }
  2205. if (amd_shader_core_properties2) {
  2206. last_struct->pNext = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2207. last_struct = (VkBaseOutStructure *)&amd_shader_core_properties2_props;
  2208. }
  2209. if (device->subgroup_size_control) {
  2210. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_props;
  2211. last_struct = (VkBaseOutStructure *)&subgroup_size_control_props;
  2212. }
  2213. #if defined(VK_NV_cooperative_matrix2)
  2214. vk::PhysicalDeviceCooperativeMatrix2PropertiesNV coopmat2_props;
  2215. if (coopmat2_support) {
  2216. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_props;
  2217. last_struct = (VkBaseOutStructure *)&coopmat2_props;
  2218. }
  2219. #endif
  2220. if (device->integer_dot_product) {
  2221. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2222. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2223. }
  2224. device->physical_device.getProperties2(&props2);
  2225. device->properties = props2.properties;
  2226. device->vendor_id = device->properties.vendorID;
  2227. const char* GGML_VK_FORCE_MAX_ALLOCATION_SIZE = getenv("GGML_VK_FORCE_MAX_ALLOCATION_SIZE");
  2228. if (GGML_VK_FORCE_MAX_ALLOCATION_SIZE != nullptr) {
  2229. device->max_memory_allocation_size = std::stoul(GGML_VK_FORCE_MAX_ALLOCATION_SIZE);
  2230. } else if (maintenance4_support) {
  2231. device->max_memory_allocation_size = std::min(props3.maxMemoryAllocationSize, props4.maxBufferSize);
  2232. } else {
  2233. device->max_memory_allocation_size = props3.maxMemoryAllocationSize;
  2234. }
  2235. const char* GGML_VK_SUBALLOCATION_BLOCK_SIZE = getenv("GGML_VK_SUBALLOCATION_BLOCK_SIZE");
  2236. if (GGML_VK_SUBALLOCATION_BLOCK_SIZE != nullptr) {
  2237. device->suballocation_block_size = std::stoul(GGML_VK_SUBALLOCATION_BLOCK_SIZE);
  2238. } else {
  2239. // Limit batching of allocations to 1GB by default to avoid fragmentation issues
  2240. device->suballocation_block_size = 1024*1024*1024;
  2241. }
  2242. device->suballocation_block_size = std::min(device->suballocation_block_size, device->max_memory_allocation_size);
  2243. device->subgroup_size = subgroup_props.subgroupSize;
  2244. device->uma = device->properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2245. if (sm_builtins) {
  2246. device->shader_core_count = sm_props.shaderSMCount;
  2247. } else if (amd_shader_core_properties2) {
  2248. device->shader_core_count = amd_shader_core_properties2_props.activeComputeUnitCount;
  2249. } else {
  2250. device->shader_core_count = 0;
  2251. }
  2252. device->float_controls_rte_fp16 = vk12_props.shaderRoundingModeRTEFloat16;
  2253. device->subgroup_add = (vk11_props.subgroupSupportedStages & vk::ShaderStageFlagBits::eCompute) &&
  2254. (vk11_props.subgroupSupportedOperations & vk::SubgroupFeatureFlagBits::eArithmetic);
  2255. const bool force_disable_f16 = getenv("GGML_VK_DISABLE_F16") != nullptr;
  2256. device->fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2257. if (!ggml_vk_khr_cooperative_matrix_support(device->properties, driver_props, device->architecture)) {
  2258. device->coopmat_support = false;
  2259. }
  2260. device->integer_dot_product = device->integer_dot_product && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated;
  2261. std::vector<vk::QueueFamilyProperties> queue_family_props = device->physical_device.getQueueFamilyProperties();
  2262. // Try to find a non-graphics compute queue and transfer-focused queues
  2263. const uint32_t compute_queue_family_index = ggml_vk_find_queue_family_index(queue_family_props, vk::QueueFlagBits::eCompute, vk::QueueFlagBits::eGraphics, -1, 1);
  2264. 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);
  2265. const float priorities[] = { 1.0f, 1.0f };
  2266. device->single_queue = compute_queue_family_index == transfer_queue_family_index && queue_family_props[compute_queue_family_index].queueCount == 1;
  2267. std::vector<vk::DeviceQueueCreateInfo> device_queue_create_infos;
  2268. if (compute_queue_family_index != transfer_queue_family_index) {
  2269. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2270. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), transfer_queue_family_index, 1, priorities + 1});
  2271. } else if(!device->single_queue) {
  2272. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 2, priorities});
  2273. } else {
  2274. device_queue_create_infos.push_back({vk::DeviceQueueCreateFlags(), compute_queue_family_index, 1, priorities});
  2275. }
  2276. vk::DeviceCreateInfo device_create_info;
  2277. std::vector<const char *> device_extensions;
  2278. vk::PhysicalDeviceFeatures device_features = device->physical_device.getFeatures();
  2279. VkPhysicalDeviceFeatures2 device_features2;
  2280. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2281. device_features2.pNext = nullptr;
  2282. device_features2.features = (VkPhysicalDeviceFeatures)device_features;
  2283. VkPhysicalDeviceVulkan11Features vk11_features;
  2284. vk11_features.pNext = nullptr;
  2285. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2286. device_features2.pNext = &vk11_features;
  2287. VkPhysicalDeviceVulkan12Features vk12_features;
  2288. vk12_features.pNext = nullptr;
  2289. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2290. vk11_features.pNext = &vk12_features;
  2291. last_struct = (VkBaseOutStructure *)&vk12_features;
  2292. VkPhysicalDevicePipelineRobustnessFeaturesEXT pl_robustness_features;
  2293. pl_robustness_features.pNext = nullptr;
  2294. pl_robustness_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT;
  2295. pl_robustness_features.pipelineRobustness = VK_FALSE;
  2296. if (pipeline_robustness) {
  2297. last_struct->pNext = (VkBaseOutStructure *)&pl_robustness_features;
  2298. last_struct = (VkBaseOutStructure *)&pl_robustness_features;
  2299. device_extensions.push_back("VK_EXT_pipeline_robustness");
  2300. }
  2301. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_size_control_features;
  2302. subgroup_size_control_features.pNext = nullptr;
  2303. subgroup_size_control_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  2304. subgroup_size_control_features.computeFullSubgroups = false;
  2305. subgroup_size_control_features.subgroupSizeControl = false;
  2306. if (device->subgroup_size_control) {
  2307. last_struct->pNext = (VkBaseOutStructure *)&subgroup_size_control_features;
  2308. last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
  2309. }
  2310. #if defined(VK_KHR_cooperative_matrix)
  2311. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2312. coopmat_features.pNext = nullptr;
  2313. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2314. coopmat_features.cooperativeMatrix = VK_FALSE;
  2315. if (device->coopmat_support) {
  2316. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2317. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2318. }
  2319. #endif
  2320. #if defined(VK_NV_cooperative_matrix2)
  2321. VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
  2322. coopmat2_features.pNext = nullptr;
  2323. coopmat2_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_2_FEATURES_NV;
  2324. if (coopmat2_support) {
  2325. last_struct->pNext = (VkBaseOutStructure *)&coopmat2_features;
  2326. last_struct = (VkBaseOutStructure *)&coopmat2_features;
  2327. device_extensions.push_back("VK_NV_cooperative_matrix2");
  2328. }
  2329. #endif
  2330. VkPhysicalDeviceMaintenance4Features maint4_features {};
  2331. maint4_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_4_FEATURES;
  2332. if (maintenance4_support) {
  2333. last_struct->pNext = (VkBaseOutStructure *)&maint4_features;
  2334. last_struct = (VkBaseOutStructure *)&maint4_features;
  2335. device_extensions.push_back("VK_KHR_maintenance4");
  2336. }
  2337. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2338. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2339. if (device->integer_dot_product) {
  2340. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2341. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2342. device_extensions.push_back("VK_KHR_shader_integer_dot_product");
  2343. }
  2344. vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
  2345. device->fp16 = device->fp16 && vk12_features.shaderFloat16;
  2346. device->pipeline_robustness = pl_robustness_features.pipelineRobustness;
  2347. if (device->subgroup_size_control) {
  2348. device->subgroup_min_size = subgroup_size_control_props.minSubgroupSize;
  2349. device->subgroup_max_size = subgroup_size_control_props.maxSubgroupSize;
  2350. device_extensions.push_back("VK_EXT_subgroup_size_control");
  2351. }
  2352. device->subgroup_size_control = device->subgroup_size_control &&
  2353. (subgroup_size_control_props.requiredSubgroupSizeStages & vk::ShaderStageFlagBits::eCompute) &&
  2354. subgroup_size_control_features.subgroupSizeControl;
  2355. if (device->subgroup_size_control) {
  2356. device->subgroup_require_full_support = subgroup_size_control_features.computeFullSubgroups;
  2357. }
  2358. #if defined(VK_KHR_cooperative_matrix)
  2359. device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
  2360. #endif
  2361. if (coopmat2_support) {
  2362. #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2363. if (coopmat2_features.cooperativeMatrixWorkgroupScope &&
  2364. coopmat2_features.cooperativeMatrixFlexibleDimensions &&
  2365. coopmat2_features.cooperativeMatrixReductions &&
  2366. coopmat2_features.cooperativeMatrixConversions &&
  2367. coopmat2_features.cooperativeMatrixPerElementOperations &&
  2368. coopmat2_features.cooperativeMatrixTensorAddressing &&
  2369. coopmat2_features.cooperativeMatrixBlockLoads &&
  2370. vk12_features.bufferDeviceAddress) {
  2371. std::vector<VkCooperativeMatrixFlexibleDimensionsPropertiesNV> flexible_dimensions;
  2372. uint32_t count = 0;
  2373. PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV
  2374. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV =
  2375. (PFN_vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV)
  2376. vk_instance.instance.getProcAddr("vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV");
  2377. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, nullptr);
  2378. VkCooperativeMatrixFlexibleDimensionsPropertiesNV empty_prop {};
  2379. empty_prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_FLEXIBLE_DIMENSIONS_PROPERTIES_NV;
  2380. flexible_dimensions.resize(count, empty_prop);
  2381. _vkGetPhysicalDeviceCooperativeMatrixFlexibleDimensionsPropertiesNV(device->physical_device, &count, flexible_dimensions.data());
  2382. bool found_fp16_128 = false,
  2383. found_fp16_256 = false,
  2384. found_fp32_128 = false,
  2385. found_fp32_256 = false;
  2386. // need to support fp16*fp16 with fp16/fp32 accumulator, for workgroupsize 128
  2387. // with 32x16x16 and 256 with 32x32x16.
  2388. for (auto &prop : flexible_dimensions) {
  2389. if (prop.saturatingAccumulation == VK_FALSE &&
  2390. prop.scope == VK_SCOPE_WORKGROUP_KHR &&
  2391. prop.AType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2392. prop.BType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2393. if (prop.workgroupInvocations == 128 &&
  2394. prop.MGranularity <= 32 &&
  2395. prop.NGranularity <= 16 &&
  2396. prop.KGranularity <= 16) {
  2397. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2398. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2399. found_fp16_128 = true;
  2400. }
  2401. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2402. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2403. found_fp32_128 = true;
  2404. }
  2405. }
  2406. if (prop.workgroupInvocations == 256 &&
  2407. prop.MGranularity <= 32 &&
  2408. prop.NGranularity <= 32 &&
  2409. prop.KGranularity <= 16) {
  2410. if (prop.CType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
  2411. prop.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR) {
  2412. found_fp16_256 = true;
  2413. }
  2414. if (prop.CType == VK_COMPONENT_TYPE_FLOAT32_KHR &&
  2415. prop.ResultType == VK_COMPONENT_TYPE_FLOAT32_KHR) {
  2416. found_fp32_256 = true;
  2417. }
  2418. }
  2419. }
  2420. }
  2421. if (found_fp16_128 && found_fp16_256 &&
  2422. found_fp32_128 && found_fp32_256 &&
  2423. coopmat2_props.cooperativeMatrixFlexibleDimensionsMaxDimension >= 512) {
  2424. device->coopmat2 = true;
  2425. }
  2426. }
  2427. #endif
  2428. }
  2429. if (!vk11_features.storageBuffer16BitAccess) {
  2430. std::cerr << "ggml_vulkan: device " << GGML_VK_NAME << idx << " does not support 16-bit storage." << std::endl;
  2431. throw std::runtime_error("Unsupported device");
  2432. }
  2433. device_extensions.push_back("VK_KHR_16bit_storage");
  2434. #ifdef GGML_VULKAN_VALIDATE
  2435. device_extensions.push_back("VK_KHR_shader_non_semantic_info");
  2436. #endif
  2437. if (device->fp16) {
  2438. device_extensions.push_back("VK_KHR_shader_float16_int8");
  2439. }
  2440. #if defined(VK_KHR_cooperative_matrix)
  2441. if (device->coopmat_support) {
  2442. // Query supported shapes
  2443. std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
  2444. PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR =
  2445. (PFN_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR)vkGetInstanceProcAddr(vk_instance.instance, "vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR");
  2446. uint32_t cm_props_num;
  2447. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, nullptr);
  2448. cm_props.resize(cm_props_num);
  2449. for (auto& prop : cm_props) {
  2450. prop.sType = VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR;
  2451. }
  2452. pfn_vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(device->physical_device, &cm_props_num, cm_props.data());
  2453. VK_LOG_DEBUG("ggml_vulkan: Cooperative Matrix Shapes: " << cm_props.size());
  2454. for (auto& prop : cm_props) {
  2455. VK_LOG_DEBUG("ggml_vulkan: M: " << prop.MSize << " N: " << prop.NSize << " K: " << prop.KSize << " A: " << vk::to_string((vk::ComponentTypeKHR)prop.AType) << " B: " << vk::to_string((vk::ComponentTypeKHR)prop.BType) << " C: " << vk::to_string((vk::ComponentTypeKHR)prop.CType) << " Result: " << vk::to_string((vk::ComponentTypeKHR)prop.ResultType) << " saturatingAccumulation: " << prop.saturatingAccumulation << " scope: " << vk::to_string((vk::ScopeKHR)prop.scope));
  2456. if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eFloat16 &&
  2457. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eFloat16 &&
  2458. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup
  2459. ) {
  2460. if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat32 &&
  2461. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat32) {
  2462. // coopmat sizes not set yet
  2463. if (device->coopmat_m == 0) {
  2464. device->coopmat_acc_f32_support = true;
  2465. device->coopmat_m = prop.MSize;
  2466. device->coopmat_n = prop.NSize;
  2467. device->coopmat_k = prop.KSize;
  2468. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2469. // Only enable if shape is identical
  2470. device->coopmat_acc_f32_support = true;
  2471. }
  2472. } else if ((vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eFloat16 &&
  2473. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eFloat16) {
  2474. // coopmat sizes not set yet
  2475. if (device->coopmat_m == 0) {
  2476. device->coopmat_acc_f16_support = true;
  2477. device->coopmat_m = prop.MSize;
  2478. device->coopmat_n = prop.NSize;
  2479. device->coopmat_k = prop.KSize;
  2480. } else if (device->coopmat_m == prop.MSize && device->coopmat_n == prop.NSize && device->coopmat_k == prop.KSize) {
  2481. // Only enable if shape is identical
  2482. device->coopmat_acc_f16_support = true;
  2483. }
  2484. }
  2485. } else if ((vk::ComponentTypeKHR)prop.AType == vk::ComponentTypeKHR::eSint8 &&
  2486. (vk::ComponentTypeKHR)prop.BType == vk::ComponentTypeKHR::eSint8 &&
  2487. (vk::ComponentTypeKHR)prop.CType == vk::ComponentTypeKHR::eSint32 &&
  2488. (vk::ComponentTypeKHR)prop.ResultType == vk::ComponentTypeKHR::eSint32 &&
  2489. (vk::ScopeKHR)prop.scope == vk::ScopeKHR::eSubgroup &&
  2490. device->coopmat_int_m == 0
  2491. ) {
  2492. device->coopmat_int_support = true;
  2493. device->coopmat_int_m = prop.MSize;
  2494. device->coopmat_int_n = prop.NSize;
  2495. device->coopmat_int_k = prop.KSize;
  2496. }
  2497. }
  2498. if (device->coopmat_m == 0 || !device->coopmat_acc_f32_support) {
  2499. // No suitable matmul mode found
  2500. GGML_LOG_DEBUG("ggml_vulkan: WARNING: No suitable matrix core mode found. Disabling matrix cores.\n");
  2501. device->coopmat_support = false;
  2502. }
  2503. }
  2504. if (device->coopmat_support) {
  2505. device_extensions.push_back("VK_KHR_cooperative_matrix");
  2506. }
  2507. #endif
  2508. device->name = GGML_VK_NAME + std::to_string(idx);
  2509. device_create_info = {
  2510. vk::DeviceCreateFlags(),
  2511. device_queue_create_infos,
  2512. {},
  2513. device_extensions
  2514. };
  2515. device_create_info.setPNext(&device_features2);
  2516. device->device = device->physical_device.createDevice(device_create_info);
  2517. // Queues
  2518. ggml_vk_create_queue(device, device->compute_queue, compute_queue_family_index, 0, { vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer }, false);
  2519. // Shaders
  2520. // Disable matmul tile sizes early if performance low or not supported
  2521. for (uint32_t i = 0; i < GGML_TYPE_COUNT; ++i) {
  2522. switch (device->vendor_id) {
  2523. #ifndef GGML_VULKAN_RUN_TESTS
  2524. case VK_VENDOR_ID_AMD:
  2525. case VK_VENDOR_ID_INTEL:
  2526. device->mul_mat_l[i] = false;
  2527. device->mul_mat_m[i] = true;
  2528. device->mul_mat_s[i] = true;
  2529. device->mul_mat_id_l[i] = false;
  2530. device->mul_mat_id_m[i] = true;
  2531. device->mul_mat_id_s[i] = true;
  2532. break;
  2533. case VK_VENDOR_ID_APPLE:
  2534. device->mul_mat_l[i] = false;
  2535. device->mul_mat_m[i] = true;
  2536. device->mul_mat_s[i] = false;
  2537. device->mul_mat_id_l[i] = false;
  2538. device->mul_mat_id_m[i] = true;
  2539. device->mul_mat_id_s[i] = false;
  2540. break;
  2541. #endif
  2542. default:
  2543. device->mul_mat_l[i] = true;
  2544. device->mul_mat_m[i] = true;
  2545. device->mul_mat_s[i] = true;
  2546. device->mul_mat_id_l[i] = true;
  2547. device->mul_mat_id_m[i] = true;
  2548. device->mul_mat_id_s[i] = true;
  2549. break;
  2550. }
  2551. }
  2552. ggml_vk_load_shaders(device);
  2553. if (!device->single_queue) {
  2554. const uint32_t transfer_queue_index = compute_queue_family_index == transfer_queue_family_index ? 1 : 0;
  2555. ggml_vk_create_queue(device, device->transfer_queue, transfer_queue_family_index, transfer_queue_index, { vk::PipelineStageFlagBits::eTransfer }, true);
  2556. } else {
  2557. // TODO: Use pointer or reference to avoid copy
  2558. device->transfer_queue = device->compute_queue;
  2559. }
  2560. device->buffer_type = {
  2561. /* .iface = */ ggml_backend_vk_buffer_type_interface,
  2562. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), idx),
  2563. /* .context = */ new ggml_backend_vk_buffer_type_context{ device->name, device },
  2564. };
  2565. device->fence = device->device.createFence({});
  2566. device->idx = idx;
  2567. return device;
  2568. }
  2569. return vk_instance.devices[idx];
  2570. }
  2571. static void ggml_vk_print_gpu_info(size_t idx) {
  2572. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2573. size_t dev_num = vk_instance.device_indices[idx];
  2574. VK_LOG_DEBUG("ggml_vk_print_gpu_info(" << dev_num << ")");
  2575. GGML_ASSERT(vk_instance_initialized);
  2576. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2577. if (dev_num >= devices.size()) {
  2578. std::cerr << "ggml_vulkan: Device with index " << dev_num << " does not exist." << std::endl;
  2579. throw std::runtime_error("Device not found");
  2580. }
  2581. vk::PhysicalDevice physical_device = devices[dev_num];
  2582. std::vector<vk::ExtensionProperties> ext_props = physical_device.enumerateDeviceExtensionProperties();
  2583. bool fp16_storage = false;
  2584. bool fp16_compute = false;
  2585. bool coopmat_support = false;
  2586. bool coopmat2_support = false;
  2587. bool integer_dot_product = false;
  2588. for (auto properties : ext_props) {
  2589. if (strcmp("VK_KHR_16bit_storage", properties.extensionName) == 0) {
  2590. fp16_storage = true;
  2591. } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
  2592. fp16_compute = true;
  2593. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2594. } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
  2595. !getenv("GGML_VK_DISABLE_COOPMAT")) {
  2596. coopmat_support = true;
  2597. #endif
  2598. #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
  2599. } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
  2600. !getenv("GGML_VK_DISABLE_COOPMAT2")) {
  2601. coopmat2_support = true;
  2602. #endif
  2603. #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
  2604. } else if (strcmp("VK_KHR_shader_integer_dot_product", properties.extensionName) == 0 &&
  2605. !getenv("GGML_VK_DISABLE_INTEGER_DOT_PRODUCT")) {
  2606. integer_dot_product = true;
  2607. #endif
  2608. }
  2609. }
  2610. const vk_device_architecture device_architecture = get_device_architecture(physical_device);
  2611. const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
  2612. bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
  2613. bool fp16 = !force_disable_f16 && fp16_storage && fp16_compute;
  2614. vk::PhysicalDeviceProperties2 props2;
  2615. vk::PhysicalDeviceMaintenance3Properties props3;
  2616. vk::PhysicalDeviceSubgroupProperties subgroup_props;
  2617. vk::PhysicalDeviceDriverProperties driver_props;
  2618. vk::PhysicalDeviceShaderIntegerDotProductPropertiesKHR shader_integer_dot_product_props;
  2619. props2.pNext = &props3;
  2620. props3.pNext = &subgroup_props;
  2621. subgroup_props.pNext = &driver_props;
  2622. // Pointer to the last chain element
  2623. VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&driver_props;
  2624. if (integer_dot_product) {
  2625. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2626. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_props;
  2627. }
  2628. physical_device.getProperties2(&props2);
  2629. VkPhysicalDeviceFeatures2 device_features2;
  2630. device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  2631. device_features2.pNext = nullptr;
  2632. VkPhysicalDeviceVulkan11Features vk11_features;
  2633. vk11_features.pNext = nullptr;
  2634. vk11_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
  2635. device_features2.pNext = &vk11_features;
  2636. VkPhysicalDeviceVulkan12Features vk12_features;
  2637. vk12_features.pNext = nullptr;
  2638. vk12_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
  2639. vk11_features.pNext = &vk12_features;
  2640. // Pointer to the last chain element
  2641. last_struct = (VkBaseOutStructure *)&vk12_features;
  2642. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2643. VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
  2644. coopmat_features.pNext = nullptr;
  2645. coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
  2646. coopmat_features.cooperativeMatrix = VK_FALSE;
  2647. if (coopmat_support) {
  2648. last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
  2649. last_struct = (VkBaseOutStructure *)&coopmat_features;
  2650. }
  2651. #endif
  2652. VkPhysicalDeviceShaderIntegerDotProductFeaturesKHR shader_integer_dot_product_features {};
  2653. shader_integer_dot_product_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR;
  2654. if (integer_dot_product) {
  2655. last_struct->pNext = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2656. last_struct = (VkBaseOutStructure *)&shader_integer_dot_product_features;
  2657. }
  2658. vkGetPhysicalDeviceFeatures2(physical_device, &device_features2);
  2659. fp16 = fp16 && vk12_features.shaderFloat16;
  2660. uint32_t default_subgroup_size = get_subgroup_size("", device_architecture);
  2661. const size_t subgroup_size = (default_subgroup_size != 0) ? default_subgroup_size : subgroup_props.subgroupSize;
  2662. const bool uma = props2.properties.deviceType == vk::PhysicalDeviceType::eIntegratedGpu;
  2663. integer_dot_product = integer_dot_product
  2664. && shader_integer_dot_product_props.integerDotProduct4x8BitPackedSignedAccelerated
  2665. && shader_integer_dot_product_features.shaderIntegerDotProduct;
  2666. coopmat_support = coopmat_support
  2667. #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
  2668. && coopmat_features.cooperativeMatrix
  2669. #endif
  2670. && ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props, device_architecture);
  2671. std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
  2672. std::string device_name = props2.properties.deviceName.data();
  2673. GGML_LOG_DEBUG("ggml_vulkan: %zu = %s (%s) | uma: %d | fp16: %d | warp size: %zu | shared memory: %d | int dot: %d | matrix cores: %s\n",
  2674. idx, device_name.c_str(), driver_props.driverName.data(), uma, fp16, subgroup_size,
  2675. props2.properties.limits.maxComputeSharedMemorySize, integer_dot_product, matrix_cores.c_str());
  2676. if (props2.properties.deviceType == vk::PhysicalDeviceType::eCpu) {
  2677. GGML_LOG_DEBUG("ggml_vulkan: Warning: Device type is CPU. This is probably not the device you want.\n");
  2678. }
  2679. }
  2680. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2681. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
  2682. static void ggml_vk_instance_init() {
  2683. if (vk_instance_initialized) {
  2684. return;
  2685. }
  2686. VK_LOG_DEBUG("ggml_vk_instance_init()");
  2687. uint32_t api_version = vk::enumerateInstanceVersion();
  2688. if (api_version < VK_API_VERSION_1_2) {
  2689. std::cerr << "ggml_vulkan: Error: Vulkan 1.2 required." << std::endl;
  2690. GGML_ABORT("fatal error");
  2691. }
  2692. vk::ApplicationInfo app_info{ "ggml-vulkan", 1, nullptr, 0, api_version };
  2693. const std::vector<vk::ExtensionProperties> instance_extensions = vk::enumerateInstanceExtensionProperties();
  2694. const bool validation_ext = ggml_vk_instance_validation_ext_available(instance_extensions);
  2695. #ifdef __APPLE__
  2696. const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
  2697. #endif
  2698. std::vector<const char*> layers;
  2699. if (validation_ext) {
  2700. layers.push_back("VK_LAYER_KHRONOS_validation");
  2701. }
  2702. std::vector<const char*> extensions;
  2703. if (validation_ext) {
  2704. extensions.push_back("VK_EXT_validation_features");
  2705. }
  2706. #ifdef __APPLE__
  2707. if (portability_enumeration_ext) {
  2708. extensions.push_back("VK_KHR_portability_enumeration");
  2709. }
  2710. #endif
  2711. vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
  2712. #ifdef __APPLE__
  2713. if (portability_enumeration_ext) {
  2714. instance_create_info.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
  2715. }
  2716. #endif
  2717. std::vector<vk::ValidationFeatureEnableEXT> features_enable;
  2718. vk::ValidationFeaturesEXT validation_features;
  2719. if (validation_ext) {
  2720. features_enable = { vk::ValidationFeatureEnableEXT::eBestPractices };
  2721. validation_features = {
  2722. features_enable,
  2723. {},
  2724. };
  2725. validation_features.setPNext(nullptr);
  2726. instance_create_info.setPNext(&validation_features);
  2727. GGML_LOG_DEBUG("ggml_vulkan: Validation layers enabled\n");
  2728. }
  2729. vk_instance.instance = vk::createInstance(instance_create_info);
  2730. vk_instance_initialized = true;
  2731. size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
  2732. // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
  2733. char * devices_env = getenv("GGML_VK_VISIBLE_DEVICES");
  2734. if (devices_env != nullptr) {
  2735. std::string devices(devices_env);
  2736. std::replace(devices.begin(), devices.end(), ',', ' ');
  2737. std::stringstream ss(devices);
  2738. size_t tmp;
  2739. while (ss >> tmp) {
  2740. if(tmp >= num_available_devices) {
  2741. std::cerr << "ggml_vulkan: Invalid device index " << tmp << " in GGML_VK_VISIBLE_DEVICES." << std::endl;
  2742. throw std::runtime_error("Invalid Vulkan device index");
  2743. }
  2744. vk_instance.device_indices.push_back(tmp);
  2745. }
  2746. } else {
  2747. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  2748. // Make sure at least one device exists
  2749. if (devices.empty()) {
  2750. std::cerr << "ggml_vulkan: Error: No devices found." << std::endl;
  2751. return;
  2752. }
  2753. // Default to using all dedicated GPUs
  2754. for (size_t i = 0; i < devices.size(); i++) {
  2755. vk::PhysicalDeviceProperties2 new_props;
  2756. vk::PhysicalDeviceDriverProperties new_driver;
  2757. vk::PhysicalDeviceIDProperties new_id;
  2758. new_props.pNext = &new_driver;
  2759. new_driver.pNext = &new_id;
  2760. devices[i].getProperties2(&new_props);
  2761. if (new_props.properties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
  2762. // Check if there are two physical devices corresponding to the same GPU
  2763. auto old_device = std::find_if(
  2764. vk_instance.device_indices.begin(),
  2765. vk_instance.device_indices.end(),
  2766. [&devices, &new_id](const size_t k){
  2767. vk::PhysicalDeviceProperties2 old_props;
  2768. vk::PhysicalDeviceIDProperties old_id;
  2769. old_props.pNext = &old_id;
  2770. devices[k].getProperties2(&old_props);
  2771. return std::equal(std::begin(old_id.deviceUUID), std::end(old_id.deviceUUID), std::begin(new_id.deviceUUID));
  2772. }
  2773. );
  2774. if (old_device == vk_instance.device_indices.end()) {
  2775. vk_instance.device_indices.push_back(i);
  2776. } else {
  2777. // There can be two physical devices corresponding to the same GPU if there are 2 different drivers
  2778. // This can cause error when splitting layers aross the devices, need to keep only 1
  2779. VK_LOG_DEBUG("Device " << i << " and device " << *old_device << " have the same deviceUUID");
  2780. vk::PhysicalDeviceProperties2 old_props;
  2781. vk::PhysicalDeviceDriverProperties old_driver;
  2782. old_props.pNext = &old_driver;
  2783. devices[*old_device].getProperties2(&old_props);
  2784. std::map<vk::DriverId, int> driver_priorities {};
  2785. int old_priority = std::numeric_limits<int>::max();
  2786. int new_priority = std::numeric_limits<int>::max();
  2787. // Check https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkDriverId.html for the list of driver id
  2788. // Smaller number -> higher priority
  2789. switch (old_props.properties.vendorID) {
  2790. case VK_VENDOR_ID_AMD:
  2791. driver_priorities[vk::DriverId::eMesaRadv] = 1;
  2792. driver_priorities[vk::DriverId::eAmdOpenSource] = 2;
  2793. driver_priorities[vk::DriverId::eAmdProprietary] = 3;
  2794. break;
  2795. case VK_VENDOR_ID_INTEL:
  2796. driver_priorities[vk::DriverId::eIntelOpenSourceMESA] = 1;
  2797. driver_priorities[vk::DriverId::eIntelProprietaryWindows] = 2;
  2798. break;
  2799. case VK_VENDOR_ID_NVIDIA:
  2800. driver_priorities[vk::DriverId::eNvidiaProprietary] = 1;
  2801. #if defined(VK_API_VERSION_1_3) && VK_HEADER_VERSION >= 235
  2802. driver_priorities[vk::DriverId::eMesaNvk] = 2;
  2803. #endif
  2804. break;
  2805. }
  2806. if (driver_priorities.count(old_driver.driverID)) {
  2807. old_priority = driver_priorities[old_driver.driverID];
  2808. }
  2809. if (driver_priorities.count(new_driver.driverID)) {
  2810. new_priority = driver_priorities[new_driver.driverID];
  2811. }
  2812. if (new_priority < old_priority) {
  2813. auto r = std::remove(vk_instance.device_indices.begin(), vk_instance.device_indices.end(), *old_device);
  2814. vk_instance.device_indices.erase(r, vk_instance.device_indices.end());
  2815. vk_instance.device_indices.push_back(i);
  2816. VK_LOG_DEBUG("Prioritize device " << i << " driver " << new_driver.driverName << " over device " << *old_device << " driver " << old_driver.driverName);
  2817. }
  2818. else {
  2819. VK_LOG_DEBUG("Prioritize device " << *old_device << " driver " << old_driver.driverName << " over device " << i << " driver " << new_driver.driverName << std::endl);
  2820. }
  2821. }
  2822. }
  2823. }
  2824. // If no dedicated GPUs found, fall back to GPU 0
  2825. if (vk_instance.device_indices.empty()) {
  2826. vk_instance.device_indices.push_back(0);
  2827. }
  2828. }
  2829. GGML_LOG_DEBUG("ggml_vulkan: Found %zu Vulkan devices:\n", vk_instance.device_indices.size());
  2830. for (size_t i = 0; i < vk_instance.device_indices.size(); i++) {
  2831. ggml_vk_print_gpu_info(i);
  2832. }
  2833. }
  2834. static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) {
  2835. VK_LOG_DEBUG("ggml_vk_init(" << ctx->name << ", " << idx << ")");
  2836. ggml_vk_instance_init();
  2837. GGML_ASSERT(idx < vk_instance.device_indices.size());
  2838. ctx->name = GGML_VK_NAME + std::to_string(idx);
  2839. ctx->device = ggml_vk_get_device(idx);
  2840. ctx->semaphore_idx = 0;
  2841. ctx->event_idx = 0;
  2842. ctx->prealloc_size_x = 0;
  2843. ctx->prealloc_size_y = 0;
  2844. ctx->prealloc_size_split_k = 0;
  2845. ctx->fence = ctx->device->device.createFence({});
  2846. #ifdef GGML_VULKAN_CHECK_RESULTS
  2847. const char* skip_checks = getenv("GGML_VULKAN_SKIP_CHECKS");
  2848. vk_skip_checks = (skip_checks == NULL ? 0 : atoi(skip_checks));
  2849. const char* output_tensor = getenv("GGML_VULKAN_OUTPUT_TENSOR");
  2850. vk_output_tensor = (output_tensor == NULL ? 0 : atoi(output_tensor));
  2851. #endif
  2852. }
  2853. static vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
  2854. VK_LOG_DEBUG("ggml_vk_get_to_fp16()");
  2855. switch (type) {
  2856. case GGML_TYPE_F32:
  2857. case GGML_TYPE_Q4_0:
  2858. case GGML_TYPE_Q4_1:
  2859. case GGML_TYPE_Q5_0:
  2860. case GGML_TYPE_Q5_1:
  2861. case GGML_TYPE_Q8_0:
  2862. case GGML_TYPE_Q2_K:
  2863. case GGML_TYPE_Q3_K:
  2864. case GGML_TYPE_Q4_K:
  2865. case GGML_TYPE_Q5_K:
  2866. case GGML_TYPE_Q6_K:
  2867. case GGML_TYPE_IQ1_S:
  2868. case GGML_TYPE_IQ1_M:
  2869. case GGML_TYPE_IQ2_XXS:
  2870. case GGML_TYPE_IQ2_XS:
  2871. case GGML_TYPE_IQ2_S:
  2872. case GGML_TYPE_IQ3_XXS:
  2873. case GGML_TYPE_IQ3_S:
  2874. case GGML_TYPE_IQ4_XS:
  2875. case GGML_TYPE_IQ4_NL:
  2876. break;
  2877. default:
  2878. return nullptr;
  2879. }
  2880. return ctx->device->pipeline_dequant[type];
  2881. }
  2882. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  2883. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_pipeline(" << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  2884. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2885. return ctx->device->pipeline_matmul_f32;
  2886. }
  2887. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  2888. return ctx->device->pipeline_matmul_f32_f16;
  2889. }
  2890. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2891. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2892. return ctx->device->pipeline_matmul_f16_f32.f16acc;
  2893. }
  2894. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2895. return ctx->device->pipeline_matmul_f16.f16acc;
  2896. }
  2897. } else {
  2898. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2899. return ctx->device->pipeline_matmul_f16_f32.f32acc;
  2900. }
  2901. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2902. return ctx->device->pipeline_matmul_f16.f32acc;
  2903. }
  2904. }
  2905. // MMQ
  2906. if (src1_type == GGML_TYPE_Q8_1) {
  2907. vk_matmul_pipeline pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1[src0_type].f16acc;
  2908. if (pipelines->s == nullptr && pipelines->m == nullptr && pipelines->l == nullptr) {
  2909. return nullptr;
  2910. }
  2911. return pipelines;
  2912. }
  2913. if (src1_type != GGML_TYPE_F32 && !ctx->device->coopmat2) {
  2914. return nullptr;
  2915. }
  2916. switch (src0_type) {
  2917. case GGML_TYPE_Q4_0:
  2918. case GGML_TYPE_Q4_1:
  2919. case GGML_TYPE_Q5_0:
  2920. case GGML_TYPE_Q5_1:
  2921. case GGML_TYPE_Q8_0:
  2922. case GGML_TYPE_Q2_K:
  2923. case GGML_TYPE_Q3_K:
  2924. case GGML_TYPE_Q4_K:
  2925. case GGML_TYPE_Q5_K:
  2926. case GGML_TYPE_Q6_K:
  2927. case GGML_TYPE_IQ1_S:
  2928. case GGML_TYPE_IQ1_M:
  2929. case GGML_TYPE_IQ2_XXS:
  2930. case GGML_TYPE_IQ2_XS:
  2931. case GGML_TYPE_IQ2_S:
  2932. case GGML_TYPE_IQ3_XXS:
  2933. case GGML_TYPE_IQ3_S:
  2934. case GGML_TYPE_IQ4_XS:
  2935. case GGML_TYPE_IQ4_NL:
  2936. break;
  2937. default:
  2938. return nullptr;
  2939. }
  2940. if (ctx->device->coopmat2) {
  2941. assert(src1_type == GGML_TYPE_F16);
  2942. return ctx->device->pipeline_dequant_mul_mat_mat_f16[src0_type].f16acc;
  2943. }
  2944. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat[src0_type].f32acc;
  2945. }
  2946. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type, uint32_t num_cols) {
  2947. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  2948. GGML_ASSERT(b_type == GGML_TYPE_F32 || b_type == GGML_TYPE_F16);
  2949. GGML_ASSERT(num_cols >= 1 && num_cols <= mul_mat_vec_max_cols);
  2950. switch (a_type) {
  2951. case GGML_TYPE_F32:
  2952. case GGML_TYPE_F16:
  2953. case GGML_TYPE_Q4_0:
  2954. case GGML_TYPE_Q4_1:
  2955. case GGML_TYPE_Q5_0:
  2956. case GGML_TYPE_Q5_1:
  2957. case GGML_TYPE_Q8_0:
  2958. case GGML_TYPE_Q2_K:
  2959. case GGML_TYPE_Q3_K:
  2960. case GGML_TYPE_Q4_K:
  2961. case GGML_TYPE_Q5_K:
  2962. case GGML_TYPE_Q6_K:
  2963. case GGML_TYPE_IQ1_S:
  2964. case GGML_TYPE_IQ1_M:
  2965. case GGML_TYPE_IQ2_XXS:
  2966. case GGML_TYPE_IQ2_XS:
  2967. case GGML_TYPE_IQ2_S:
  2968. case GGML_TYPE_IQ3_XXS:
  2969. case GGML_TYPE_IQ3_S:
  2970. case GGML_TYPE_IQ4_XS:
  2971. case GGML_TYPE_IQ4_NL:
  2972. break;
  2973. default:
  2974. return nullptr;
  2975. }
  2976. return b_type == GGML_TYPE_F32 ? ctx->device->pipeline_dequant_mul_mat_vec_f32_f32[a_type][num_cols-1] : ctx->device->pipeline_dequant_mul_mat_vec_f16_f32[a_type][num_cols-1];
  2977. }
  2978. static vk_matmul_pipeline ggml_vk_get_mul_mat_mat_id_pipeline(ggml_backend_vk_context * ctx, ggml_type src0_type, ggml_type src1_type, ggml_prec prec) {
  2979. VK_LOG_DEBUG("ggml_vk_get_mul_mat_mat_id_pipeline()");
  2980. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  2981. return ctx->device->pipeline_matmul_id_f32;
  2982. }
  2983. if (prec == GGML_PREC_DEFAULT && ctx->device->fp16 && !(ctx->device->coopmat_support && !ctx->device->coopmat_acc_f16_support)) {
  2984. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2985. return ctx->device->pipeline_matmul_id_f16_f32.f16acc;
  2986. }
  2987. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2988. return ctx->device->pipeline_matmul_id_f16.f16acc;
  2989. }
  2990. } else {
  2991. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  2992. return ctx->device->pipeline_matmul_id_f16_f32.f32acc;
  2993. }
  2994. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  2995. return ctx->device->pipeline_matmul_id_f16.f32acc;
  2996. }
  2997. }
  2998. GGML_ASSERT(src1_type == GGML_TYPE_F32 || (ctx->device->coopmat2 && src1_type == GGML_TYPE_F16));
  2999. switch (src0_type) {
  3000. case GGML_TYPE_Q4_0:
  3001. case GGML_TYPE_Q4_1:
  3002. case GGML_TYPE_Q5_0:
  3003. case GGML_TYPE_Q5_1:
  3004. case GGML_TYPE_Q8_0:
  3005. case GGML_TYPE_Q2_K:
  3006. case GGML_TYPE_Q3_K:
  3007. case GGML_TYPE_Q4_K:
  3008. case GGML_TYPE_Q5_K:
  3009. case GGML_TYPE_Q6_K:
  3010. case GGML_TYPE_IQ1_S:
  3011. case GGML_TYPE_IQ1_M:
  3012. case GGML_TYPE_IQ2_XXS:
  3013. case GGML_TYPE_IQ2_XS:
  3014. case GGML_TYPE_IQ2_S:
  3015. case GGML_TYPE_IQ3_XXS:
  3016. case GGML_TYPE_IQ3_S:
  3017. case GGML_TYPE_IQ4_XS:
  3018. case GGML_TYPE_IQ4_NL:
  3019. break;
  3020. default:
  3021. return nullptr;
  3022. }
  3023. return ctx->device->fp16 ? ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f16acc : ctx->device->pipeline_dequant_mul_mat_mat_id[src0_type].f32acc;
  3024. }
  3025. static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context * ctx, ggml_type a_type, ggml_type b_type) {
  3026. VK_LOG_DEBUG("ggml_vk_get_dequantize_mul_mat_vec()");
  3027. GGML_ASSERT(b_type == GGML_TYPE_F32);
  3028. switch (a_type) {
  3029. case GGML_TYPE_F32:
  3030. case GGML_TYPE_F16:
  3031. case GGML_TYPE_Q4_0:
  3032. case GGML_TYPE_Q4_1:
  3033. case GGML_TYPE_Q5_0:
  3034. case GGML_TYPE_Q5_1:
  3035. case GGML_TYPE_Q8_0:
  3036. case GGML_TYPE_Q2_K:
  3037. case GGML_TYPE_Q3_K:
  3038. case GGML_TYPE_Q4_K:
  3039. case GGML_TYPE_Q5_K:
  3040. case GGML_TYPE_Q6_K:
  3041. case GGML_TYPE_IQ1_S:
  3042. case GGML_TYPE_IQ1_M:
  3043. case GGML_TYPE_IQ2_XXS:
  3044. case GGML_TYPE_IQ2_XS:
  3045. case GGML_TYPE_IQ2_S:
  3046. case GGML_TYPE_IQ3_XXS:
  3047. case GGML_TYPE_IQ3_S:
  3048. case GGML_TYPE_IQ4_XS:
  3049. case GGML_TYPE_IQ4_NL:
  3050. break;
  3051. default:
  3052. return nullptr;
  3053. }
  3054. return ctx->device->pipeline_dequant_mul_mat_vec_id_f32[a_type];
  3055. }
  3056. static vk_buffer ggml_vk_pool_malloc(ggml_backend_vk_context * ctx, size_t size) {
  3057. VK_LOG_DEBUG("ggml_vk_pool_malloc(" << size << ")");
  3058. VK_LOG_MEMORY("ggml_vk_pool_malloc");
  3059. int best_i = -1;
  3060. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  3061. int worst_i = -1;
  3062. size_t worst_size = 0; //largest unused buffer seen so far
  3063. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3064. vk_buffer &b = ctx->buffer_pool[i];
  3065. if (b != nullptr && b->size >= size && b->size < best_size) {
  3066. best_i = i;
  3067. best_size = b->size;
  3068. }
  3069. if (b != nullptr && b->size > worst_size) {
  3070. worst_i = i;
  3071. worst_size = b->size;
  3072. }
  3073. }
  3074. if(best_i != -1) {
  3075. //found the smallest buffer that fits our needs
  3076. vk_buffer b = ctx->buffer_pool[best_i];
  3077. ctx->buffer_pool[best_i].reset();
  3078. return b;
  3079. }
  3080. if(worst_i != -1) {
  3081. //no buffer that fits our needs, resize largest one to save memory
  3082. vk_buffer& b = ctx->buffer_pool[worst_i];
  3083. ggml_vk_destroy_buffer(b);
  3084. }
  3085. return ggml_vk_create_buffer_device(ctx->device, size);
  3086. }
  3087. static void ggml_vk_pool_free(ggml_backend_vk_context * ctx, vk_buffer& buffer) {
  3088. VK_LOG_DEBUG("ggml_vk_pool_free(" << buffer->size << ")");
  3089. for (int i = 0; i < MAX_VK_BUFFERS; ++i) {
  3090. vk_buffer& b = ctx->buffer_pool[i];
  3091. if (b == nullptr) {
  3092. b = buffer;
  3093. return;
  3094. }
  3095. }
  3096. std::cerr << "ggml_vulkan: WARNING: vk buffer pool full, increase MAX_VK_BUFFERS" << std::endl;
  3097. ggml_vk_destroy_buffer(buffer);
  3098. }
  3099. // Returns an available temporary buffer that may only be used temporarily, it will be reused
  3100. static vk_buffer ggml_vk_create_buffer_temp(ggml_backend_vk_context * ctx, size_t size) {
  3101. // Try to find existing temp buffer with enough capacity
  3102. for (auto& buffer : ctx->gc.temp_buffers) {
  3103. if (buffer->size >= size) {
  3104. return buffer;
  3105. }
  3106. }
  3107. VK_LOG_MEMORY("ggml_vk_create_buffer_temp(" << size << ")");
  3108. // Otherwise create new buffer
  3109. vk_buffer buf = ggml_vk_pool_malloc(ctx, size);
  3110. ctx->gc.temp_buffers.push_back(buf);
  3111. return buf;
  3112. }
  3113. static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
  3114. VK_LOG_MEMORY("ggml_vk_host_malloc(" << size << ")");
  3115. vk_buffer buf = ggml_vk_create_buffer(device, size,
  3116. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3117. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3118. if(!(buf->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible)) {
  3119. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory\n",
  3120. size/1024.0/1024.0);
  3121. device->device.freeMemory(buf->device_memory);
  3122. device->device.destroyBuffer(buf->buffer);
  3123. return nullptr;
  3124. }
  3125. device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
  3126. return buf->ptr;
  3127. }
  3128. static void ggml_vk_host_free(vk_device& device, void* ptr) {
  3129. if (ptr == nullptr) {
  3130. return;
  3131. }
  3132. VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
  3133. vk_buffer buf;
  3134. size_t index;
  3135. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3136. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3137. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3138. if (ptr >= addr && ptr < endr) {
  3139. buf = std::get<2>(device->pinned_memory[i]);
  3140. index = i;
  3141. break;
  3142. }
  3143. }
  3144. if (buf == nullptr) {
  3145. fprintf(stderr, "WARNING: failed to free pinned memory: memory not in map\n");
  3146. return;
  3147. }
  3148. ggml_vk_destroy_buffer(buf);
  3149. device->pinned_memory.erase(device->pinned_memory.begin() + index);
  3150. }
  3151. static void ggml_vk_host_get(vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
  3152. buf = nullptr;
  3153. buf_offset = 0;
  3154. for (size_t i = 0; i < device->pinned_memory.size(); i++) {
  3155. const uint8_t* addr = (const uint8_t*) std::get<0>(device->pinned_memory[i]);
  3156. const uint8_t* endr = addr + std::get<1>(device->pinned_memory[i]);
  3157. if (ptr >= addr && ptr < endr) {
  3158. buf = std::get<2>(device->pinned_memory[i]);
  3159. buf_offset = ((const uint8_t *)ptr) - addr;
  3160. break;
  3161. }
  3162. }
  3163. }
  3164. static vk_submission ggml_vk_begin_submission(vk_device& device, vk_queue& q, bool one_time = true) {
  3165. vk_submission s;
  3166. s.buffer = ggml_vk_create_cmd_buffer(device, q);
  3167. if (one_time) {
  3168. s.buffer.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
  3169. } else {
  3170. s.buffer.begin({ vk::CommandBufferUsageFlags{} });
  3171. }
  3172. return s;
  3173. }
  3174. static void ggml_vk_dispatch_pipeline(ggml_backend_vk_context* ctx, vk_context& subctx, vk_pipeline& pipeline, std::initializer_list<vk::DescriptorBufferInfo> const& descriptor_buffer_infos, size_t push_constant_size, const void* push_constants, std::array<uint32_t, 3> elements) {
  3175. const uint32_t wg0 = CEIL_DIV(elements[0], pipeline->wg_denoms[0]);
  3176. const uint32_t wg1 = CEIL_DIV(elements[1], pipeline->wg_denoms[1]);
  3177. const uint32_t wg2 = CEIL_DIV(elements[2], pipeline->wg_denoms[2]);
  3178. VK_LOG_DEBUG("ggml_vk_dispatch_pipeline(" << pipeline->name << ", {";
  3179. for (auto& buffer : descriptor_buffer_infos) {
  3180. std::cerr << "(" << buffer.buffer << ", " << buffer.offset << ", " << buffer.range << "), ";
  3181. }
  3182. std::cerr << "}, (" << wg0 << "," << wg1 << "," << wg2 << "))");
  3183. GGML_ASSERT(pipeline->descriptor_set_idx < pipeline->descriptor_sets.size());
  3184. GGML_ASSERT(descriptor_buffer_infos.size() == pipeline->parameter_count);
  3185. vk::DescriptorSet& descriptor_set = pipeline->descriptor_sets[pipeline->descriptor_set_idx++];
  3186. vk::WriteDescriptorSet write_descriptor_set{ descriptor_set, 0, 0, pipeline->parameter_count, vk::DescriptorType::eStorageBuffer, nullptr, descriptor_buffer_infos.begin() };
  3187. ctx->device->device.updateDescriptorSets({ write_descriptor_set }, {});
  3188. subctx->s->buffer.pushConstants(pipeline->layout, vk::ShaderStageFlagBits::eCompute, 0, push_constant_size, push_constants);
  3189. subctx->s->buffer.bindPipeline(vk::PipelineBindPoint::eCompute, pipeline->pipeline);
  3190. subctx->s->buffer.bindDescriptorSets(vk::PipelineBindPoint::eCompute,
  3191. pipeline->layout,
  3192. 0,
  3193. { descriptor_set },
  3194. {});
  3195. subctx->s->buffer.dispatch(wg0, wg1, wg2);
  3196. }
  3197. static void ggml_vk_end_submission(vk_submission& s, std::vector<vk_semaphore> wait_semaphores, std::vector<vk_semaphore> signal_semaphores) {
  3198. s.buffer.end();
  3199. s.wait_semaphores = std::move(wait_semaphores);
  3200. s.signal_semaphores = std::move(signal_semaphores);
  3201. }
  3202. static void ggml_vk_ctx_end(vk_context& ctx) {
  3203. VK_LOG_DEBUG("ggml_vk_ctx_end(" << ctx << ", " << ctx->seqs.size() << ")");
  3204. if (ctx->s == nullptr) {
  3205. return;
  3206. }
  3207. ctx->s->buffer.end();
  3208. ctx->s = nullptr;
  3209. }
  3210. static void ggml_vk_ctx_begin(vk_device& device, vk_context& subctx) {
  3211. VK_LOG_DEBUG("ggml_vk_ctx_begin(" << device->name << ")");
  3212. if (subctx->s != nullptr) {
  3213. ggml_vk_ctx_end(subctx);
  3214. }
  3215. subctx->seqs.push_back({ ggml_vk_begin_submission(device, *subctx->q) });
  3216. subctx->s = subctx->seqs[subctx->seqs.size() - 1].data();
  3217. }
  3218. static size_t ggml_vk_align_size(size_t width, size_t align) {
  3219. VK_LOG_DEBUG("ggml_vk_align_size(" << width << ", " << align << ")");
  3220. return CEIL_DIV(width, align) * align;
  3221. }
  3222. static void deferred_memcpy(void * dst, const void * src, size_t size, std::vector<vk_staging_memcpy>* memcpys = nullptr) {
  3223. if (memcpys == nullptr) {
  3224. memcpy(dst, src, size);
  3225. } else {
  3226. memcpys->emplace_back(dst, src, size);
  3227. }
  3228. }
  3229. static void ggml_vk_ensure_sync_staging_buffer(vk_device& device, size_t size) {
  3230. if (device->sync_staging == nullptr || device->sync_staging->size < size) {
  3231. VK_LOG_MEMORY("ggml_vk_ensure_sync_staging_buffer(" << size << ")");
  3232. ggml_vk_destroy_buffer(device->sync_staging);
  3233. device->sync_staging = ggml_vk_create_buffer_check(device, size,
  3234. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostCached,
  3235. vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
  3236. }
  3237. }
  3238. 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) {
  3239. VK_LOG_DEBUG("ggml_vk_buffer_write_nc_async(" << tensor << ")");
  3240. GGML_ASSERT(!ggml_is_contiguous(tensor));
  3241. // Buffer is already mapped
  3242. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3243. std::cerr << "ggml_vulkan: buffer_write_nc_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3244. GGML_ABORT("fatal error");
  3245. }
  3246. // Check if src is pinned memory
  3247. vk_buffer buf = nullptr;
  3248. size_t buf_offset = 0;
  3249. ggml_vk_host_get(ctx->device, tensor->data, buf, buf_offset);
  3250. const uint64_t ne0 = tensor->ne[0];
  3251. const uint64_t ne1 = tensor->ne[1];
  3252. const uint64_t ne2 = tensor->ne[2];
  3253. const uint64_t ne3 = tensor->ne[3];
  3254. const uint64_t nb0 = tensor->nb[0];
  3255. const uint64_t nb1 = tensor->nb[1];
  3256. const uint64_t nb2 = tensor->nb[2];
  3257. const uint64_t nb3 = tensor->nb[3];
  3258. const ggml_type type = tensor->type;
  3259. const uint64_t ts = ggml_type_size(type);
  3260. const uint64_t bs = ggml_blck_size(type);
  3261. const uint64_t dstnb0 = ts;
  3262. const uint64_t dstnb1 = dstnb0*(ne0/bs);
  3263. const uint64_t dstnb2 = dstnb1*ne1;
  3264. const uint64_t dstnb3 = dstnb2*ne2;
  3265. const uint64_t ne = ggml_nelements(tensor);
  3266. if (buf != nullptr) {
  3267. // Memory is pinned, use as staging buffer
  3268. std::vector<vk::BufferCopy> slices;
  3269. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3270. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3271. // Find longest contiguous slice
  3272. if (ne1*nb1 == dstnb2) {
  3273. slices.push_back({ buf_offset + i3*nb3 + i2*nb2, offset + i3*dstnb3 + i2*dstnb2, dstnb2 });
  3274. } else {
  3275. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3276. if (ne0*nb0/bs == dstnb1) {
  3277. slices.push_back({ buf_offset + i3*nb3 + i2*nb2 + i1*nb1, offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, dstnb1 });
  3278. } else {
  3279. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3280. const uint64_t d_off = offset + i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3281. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3282. slices.push_back({ s_off + i1*nb0, d_off + i0*dstnb0, dstnb0 });
  3283. }
  3284. }
  3285. }
  3286. }
  3287. }
  3288. }
  3289. ggml_vk_sync_buffers(subctx);
  3290. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3291. return;
  3292. }
  3293. if (!sync_staging) {
  3294. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3295. }
  3296. // Staging buffer required
  3297. vk_buffer& staging = ctx->device->sync_staging;
  3298. const uint64_t copy_size = ts*ne/bs;
  3299. ggml_vk_ensure_sync_staging_buffer(ctx->device, copy_size);
  3300. VkBufferCopy buf_copy{ 0, offset, copy_size };
  3301. ggml_vk_sync_buffers(subctx);
  3302. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3303. for (uint64_t i3 = 0; i3 < ne3; i3++) {
  3304. for (uint64_t i2 = 0; i2 < ne2; i2++) {
  3305. // Find longest contiguous slice
  3306. if (ne1*nb1 == dstnb2) {
  3307. deferred_memcpy((uint8_t *)staging->ptr + i3*dstnb3 + i2*dstnb2, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2, dstnb2, &subctx->in_memcpys);
  3308. } else {
  3309. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  3310. if (ne0*nb0/bs == dstnb1) {
  3311. deferred_memcpy((uint8_t *)staging->ptr + i3*dstnb3 + i2*dstnb2 + i1*dstnb1, (const uint8_t *) tensor->data + buf_offset + i3*nb3 + i2*nb2 + i1*nb1, dstnb1, &subctx->in_memcpys);
  3312. } else {
  3313. const uint64_t s_off = buf_offset + i3*nb3 + i2*nb2 + i1*nb1;
  3314. const uint64_t d_off = i3*dstnb3 + i2*dstnb2 + i1*dstnb1;
  3315. for (uint64_t i0 = 0; i0 < ne0; i0++) {
  3316. deferred_memcpy((uint8_t *)staging->ptr + d_off + i0*dstnb0, (const uint8_t *) tensor->data + s_off + i0*nb0, dstnb0, &subctx->in_memcpys);
  3317. }
  3318. }
  3319. }
  3320. }
  3321. }
  3322. }
  3323. }
  3324. static void ggml_vk_buffer_write_2d_async(vk_context subctx, vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height, bool sync_staging = false) {
  3325. VK_LOG_DEBUG("ggml_vk_buffer_write_2d_async(" << width << ", " << height << ")");
  3326. // Buffer is already mapped
  3327. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3328. std::cerr << "ggml_vulkan: buffer_write_async dst buffer is host_visible. Use synchronous write." << std::endl;
  3329. GGML_ABORT("fatal error");
  3330. }
  3331. // Check if src is pinned memory
  3332. vk_buffer buf = nullptr;
  3333. size_t buf_offset = 0;
  3334. ggml_vk_host_get(dst->device, src, buf, buf_offset);
  3335. if (buf != nullptr) {
  3336. // Memory is pinned, use as staging buffer
  3337. std::vector<vk::BufferCopy> slices(1);
  3338. if (width == spitch) {
  3339. // Only do single write if stride is equal
  3340. slices[0].srcOffset = buf_offset;
  3341. slices[0].dstOffset = offset;
  3342. slices[0].size = width * height;
  3343. } else {
  3344. slices.resize(height);
  3345. for (size_t i = 0; i < height; i++) {
  3346. slices[i].srcOffset = buf_offset + i * spitch;
  3347. slices[i].dstOffset = offset + i * width;
  3348. slices[i].size = width;
  3349. }
  3350. }
  3351. ggml_vk_sync_buffers(subctx);
  3352. subctx->s->buffer.copyBuffer(buf->buffer, dst->buffer, slices);
  3353. return;
  3354. }
  3355. VK_LOG_DEBUG("STAGING");
  3356. if (!sync_staging) {
  3357. GGML_ABORT("Asynchronous write to non-pinned memory not supported");
  3358. }
  3359. // Staging buffer required
  3360. const size_t copy_size = width*height;
  3361. ggml_vk_ensure_sync_staging_buffer(dst->device, copy_size);
  3362. vk_buffer& staging_buffer = dst->device->sync_staging;
  3363. VkBufferCopy buf_copy = {
  3364. 0,
  3365. offset,
  3366. copy_size};
  3367. ggml_vk_sync_buffers(subctx);
  3368. vkCmdCopyBuffer(subctx->s->buffer, (VkBuffer)staging_buffer->buffer, (VkBuffer)dst->buffer, 1, &buf_copy);
  3369. if (width == spitch) {
  3370. deferred_memcpy((uint8_t *)staging_buffer->ptr, src, width * height, &subctx->in_memcpys);
  3371. } else {
  3372. for (size_t i = 0; i < height; i++) {
  3373. deferred_memcpy((uint8_t *)staging_buffer->ptr + i * width, (const uint8_t *) src + i * spitch, width, &subctx->in_memcpys);
  3374. }
  3375. }
  3376. }
  3377. static void ggml_vk_buffer_write_async(vk_context subctx, vk_buffer& dst, size_t offset, const void * src, size_t size, bool sync_staging = false) {
  3378. VK_LOG_DEBUG("ggml_vk_buffer_write_async(" << size << ")");
  3379. return ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, size, size, 1, sync_staging);
  3380. }
  3381. static void ggml_vk_buffer_write_2d(vk_buffer& dst, size_t offset, const void * src, size_t spitch, size_t width, size_t height) {
  3382. VK_LOG_DEBUG("ggml_vk_buffer_write_2d(" << width << ", " << height << ")");
  3383. // Buffer is already mapped
  3384. if(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible) {
  3385. GGML_ASSERT(dst->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3386. for (size_t i = 0; i < height; i++) {
  3387. memcpy((uint8_t *)dst->ptr + offset + i * width, (const uint8_t *) src + i * spitch, width);
  3388. }
  3389. } else {
  3390. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3391. ggml_vk_ctx_begin(dst->device, subctx);
  3392. ggml_vk_buffer_write_2d_async(subctx, dst, offset, src, spitch, width, height, true);
  3393. ggml_vk_ctx_end(subctx);
  3394. for (auto& cpy : subctx->in_memcpys) {
  3395. memcpy(cpy.dst, cpy.src, cpy.n);
  3396. }
  3397. ggml_vk_submit(subctx, dst->device->fence);
  3398. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_buffer_write_2d waitForFences");
  3399. dst->device->device.resetFences({ dst->device->fence });
  3400. }
  3401. }
  3402. static void ggml_vk_buffer_write(vk_buffer& dst, size_t offset, const void * src, size_t size) {
  3403. VK_LOG_DEBUG("ggml_vk_buffer_write(" << size << ")");
  3404. ggml_vk_buffer_write_2d(dst, offset, src, 0, size, 1);
  3405. }
  3406. static void ggml_vk_buffer_read_2d_async(vk_context subctx, vk_buffer& src, size_t offset, void * dst, size_t spitch, size_t dpitch, size_t width, size_t height, bool sync_staging = false) {
  3407. VK_LOG_DEBUG("ggml_vk_buffer_read_2d_async(offset=" << offset << ", width=" << width << ", height=" << height << ")");
  3408. GGML_ASSERT(width > 0);
  3409. GGML_ASSERT(height > 0);
  3410. GGML_ASSERT(src != nullptr);
  3411. // TODO: staging_offset is not used
  3412. // Check if dst is pinned memory
  3413. vk_buffer buf = nullptr;
  3414. size_t buf_offset = 0;
  3415. ggml_vk_host_get(src->device, dst, buf, buf_offset);
  3416. std::vector<vk::BufferCopy> slices(1);
  3417. if (width == spitch && width == dpitch) {
  3418. // Only do single write if stride is equal
  3419. slices[0].srcOffset = offset;
  3420. slices[0].dstOffset = buf_offset;
  3421. slices[0].size = width * height;
  3422. } else {
  3423. slices.resize(height);
  3424. for (size_t i = 0; i < height; i++) {
  3425. slices[i].srcOffset = offset + i * spitch;
  3426. slices[i].dstOffset = buf_offset + i * dpitch;
  3427. slices[i].size = width;
  3428. }
  3429. }
  3430. if (buf != nullptr) {
  3431. // Memory is pinned, use as staging buffer
  3432. ggml_vk_sync_buffers(subctx);
  3433. subctx->s->buffer.copyBuffer(src->buffer, buf->buffer, slices);
  3434. return;
  3435. }
  3436. VK_LOG_DEBUG("STAGING");
  3437. if (!sync_staging) {
  3438. GGML_ABORT("Asynchronous read from non-pinned memory not supported");
  3439. }
  3440. // Fall back to staging buffer
  3441. const size_t copy_size = dpitch * height;
  3442. ggml_vk_ensure_sync_staging_buffer(src->device, copy_size);
  3443. vk_buffer& staging_buffer = src->device->sync_staging;
  3444. ggml_vk_sync_buffers(subctx);
  3445. subctx->s->buffer.copyBuffer(src->buffer, staging_buffer->buffer, slices);
  3446. deferred_memcpy(dst, staging_buffer->ptr, copy_size, &subctx->out_memcpys);
  3447. }
  3448. static void ggml_vk_buffer_read_async(vk_context subctx, vk_buffer& src, size_t offset, void * dst, size_t size, bool sync_staging = false) {
  3449. return ggml_vk_buffer_read_2d_async(subctx, src, offset, dst, size, size, size, 1, sync_staging);
  3450. }
  3451. static void ggml_vk_buffer_read(vk_buffer& src, size_t offset, void * dst, size_t size) {
  3452. VK_LOG_DEBUG("ggml_vk_buffer_read(" << src->buffer << ", " << offset << ", " << size << ")");
  3453. // If the device is not an UMA device the memory is host-accessible through rebar. While writing
  3454. // through PCIe is sufficient fast reading back data from PCIe is slower than going through
  3455. // the HW device to host copy path.
  3456. if(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostVisible && src->device->uma) {
  3457. GGML_ASSERT(src->memory_property_flags & vk::MemoryPropertyFlagBits::eHostCoherent);
  3458. memcpy(dst, (uint8_t *) src->ptr + offset, size);
  3459. } else {
  3460. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3461. ggml_vk_ctx_begin(src->device, subctx);
  3462. ggml_vk_buffer_read_async(subctx, src, offset, dst, size, true);
  3463. ggml_vk_ctx_end(subctx);
  3464. ggml_vk_submit(subctx, src->device->fence);
  3465. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_read waitForFences");
  3466. src->device->device.resetFences({ src->device->fence });
  3467. for (auto& cpy : subctx->out_memcpys) {
  3468. memcpy(cpy.dst, cpy.src, cpy.n);
  3469. }
  3470. }
  3471. }
  3472. 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) {
  3473. VK_LOG_DEBUG("ggml_vk_buffer_copy_async(" << size << ")");
  3474. // Make sure both buffers are on same device
  3475. GGML_ASSERT(src->device == dst->device);
  3476. VkBufferCopy bc{ src_offset, dst_offset, size };
  3477. vkCmdCopyBuffer(ctx->s->buffer, (VkBuffer)src->buffer, (VkBuffer)dst->buffer, 1, &bc);
  3478. }
  3479. static void ggml_vk_buffer_copy(vk_buffer& dst, size_t dst_offset, vk_buffer& src, size_t src_offset, size_t size) {
  3480. if (src->device == dst->device) {
  3481. VK_LOG_DEBUG("ggml_vk_buffer_copy(SINGLE_DEVICE, " << size << ")");
  3482. // Copy within the device
  3483. vk_context subctx = ggml_vk_create_temporary_context(src->device->transfer_queue);
  3484. ggml_vk_ctx_begin(src->device, subctx);
  3485. ggml_vk_buffer_copy_async(subctx, dst, dst_offset, src, src_offset, size);
  3486. ggml_vk_ctx_end(subctx);
  3487. ggml_vk_submit(subctx, src->device->fence);
  3488. VK_CHECK(src->device->device.waitForFences({ src->device->fence }, true, UINT64_MAX), "vk_buffer_copy waitForFences");
  3489. src->device->device.resetFences({ src->device->fence });
  3490. } else {
  3491. VK_LOG_DEBUG("ggml_vk_buffer_copy(MULTI_DEVICE, " << size << ")");
  3492. // Copy device to device
  3493. ggml_vk_ensure_sync_staging_buffer(src->device, size);
  3494. ggml_vk_ensure_sync_staging_buffer(dst->device, size);
  3495. // Copy to src staging buffer
  3496. ggml_vk_buffer_copy(src->device->sync_staging, 0, src, src_offset, size);
  3497. // memcpy to dst staging buffer
  3498. memcpy(dst->device->sync_staging->ptr, src->device->sync_staging->ptr, size);
  3499. // Copy to dst buffer
  3500. ggml_vk_buffer_copy(dst, dst_offset, dst->device->sync_staging, 0, size);
  3501. }
  3502. }
  3503. static void ggml_vk_buffer_memset_async(vk_context& ctx, vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3504. VK_LOG_DEBUG("ggml_vk_buffer_memset_async(" << offset << ", " << c << ", " << size << ")");
  3505. ctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3506. }
  3507. static void ggml_vk_buffer_memset(vk_buffer& dst, size_t offset, uint32_t c, size_t size) {
  3508. VK_LOG_DEBUG("ggml_vk_buffer_memset(" << offset << ", " << c << ", " << size << ")");
  3509. vk_context subctx = ggml_vk_create_temporary_context(dst->device->transfer_queue);
  3510. ggml_vk_ctx_begin(dst->device, subctx);
  3511. subctx->s->buffer.fillBuffer(dst->buffer, offset, size, c);
  3512. ggml_vk_ctx_end(subctx);
  3513. ggml_vk_submit(subctx, dst->device->fence);
  3514. VK_CHECK(dst->device->device.waitForFences({ dst->device->fence }, true, UINT64_MAX), "vk_memset waitForFences");
  3515. dst->device->device.resetFences({ dst->device->fence });
  3516. }
  3517. static uint32_t ggml_vk_guess_split_k(ggml_backend_vk_context * ctx, int m, int n, int k, const vk_pipeline& pipeline) {
  3518. VK_LOG_DEBUG("ggml_vk_guess_split_k(" << m << ", " << n << ", " << k << ")");
  3519. uint32_t split_k = 1;
  3520. if (ctx->device->shader_core_count != 0 && m >= (int)pipeline->wg_denoms[0] && n >= (int)pipeline->wg_denoms[1]) {
  3521. // If k is 'large' and the SMs will fill less than halfway, use split_k.
  3522. uint32_t m_tiles = CEIL_DIV(m, pipeline->wg_denoms[0]);
  3523. uint32_t n_tiles = CEIL_DIV(n, pipeline->wg_denoms[1]);
  3524. if (k >= 2048 && m_tiles * n_tiles < ctx->device->shader_core_count / 2) {
  3525. split_k = ctx->device->shader_core_count / (m_tiles * n_tiles);
  3526. // Clamp to 2 or 4
  3527. split_k = std::min(split_k, 4u);
  3528. if (split_k == 3) {
  3529. split_k = 2;
  3530. }
  3531. }
  3532. }
  3533. return split_k;
  3534. }
  3535. static vk_pipeline ggml_vk_guess_matmul_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type, ggml_type src1_type) {
  3536. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  3537. if (ctx->device->coopmat2) {
  3538. // Use large shader when the N dimension is greater than the medium shader's tile size
  3539. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3540. if ((ctx->device->mul_mat_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_s[src0_type])) {
  3541. return aligned ? mmp->a_l : mmp->l;
  3542. }
  3543. // Use medium shader when the N dimension is greater than the small shader's tile size
  3544. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3545. if ((ctx->device->mul_mat_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_s[src0_type]) {
  3546. return aligned ? mmp->a_m : mmp->m;
  3547. }
  3548. return aligned ? mmp->a_s : mmp->s;
  3549. }
  3550. if ((ctx->device->mul_mat_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_m[src0_type] && !ctx->device->mul_mat_l[src0_type])) {
  3551. return aligned ? mmp->a_s : mmp->s;
  3552. }
  3553. if ((ctx->device->mul_mat_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_l[src0_type]) {
  3554. return aligned ? mmp->a_m : mmp->m;
  3555. }
  3556. return aligned ? mmp->a_l : mmp->l;
  3557. }
  3558. static uint32_t ggml_vk_guess_matmul_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type, ggml_type src1_type) {
  3559. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ", " << ggml_type_name(src1_type) << ")");
  3560. return ggml_vk_guess_matmul_pipeline(ctx, mmp, m, n, true, src0_type, src1_type)->align;
  3561. }
  3562. static void ggml_vk_matmul(
  3563. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3564. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& split_k_buffer,
  3565. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3566. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3567. uint32_t split_k, uint32_t batch, uint32_t ne02, uint32_t ne12, uint32_t broadcast2, uint32_t broadcast3,
  3568. uint32_t padded_n) {
  3569. VK_LOG_DEBUG("ggml_vk_matmul(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), split_k: (" << (split_k_buffer.buffer != nullptr ? split_k_buffer.buffer->buffer : VK_NULL_HANDLE) << ", " << split_k_buffer.offset << ", " << split_k_buffer.size << "), m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", split_k: " << split_k << ", batch: " << batch << ", ne02: " << ne02 << ", ne12: " << ne12 << ", broadcast2: " << broadcast2 << ", broadcast3: " << broadcast3 << ", padded_n: " << padded_n << ")");
  3570. ggml_vk_sync_buffers(subctx);
  3571. if (split_k == 1) {
  3572. const vk_mat_mat_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, k, ne02, ne12, broadcast2, broadcast3, padded_n };
  3573. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d }, sizeof(vk_mat_mat_push_constants), &pc, { m, n, batch });
  3574. return;
  3575. }
  3576. GGML_ASSERT(batch_stride_d == m * n);
  3577. const vk_mat_mat_push_constants pc1 = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d, CEIL_DIV(k, split_k), ne02, ne12, broadcast2, broadcast3, padded_n };
  3578. // Make sure enough workgroups get assigned for split k to work
  3579. 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 });
  3580. ggml_vk_sync_buffers(subctx);
  3581. const std::array<uint32_t, 2> pc2 = { (uint32_t)(m * n * batch), split_k };
  3582. 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 });
  3583. }
  3584. static vk_pipeline ggml_vk_guess_matmul_id_pipeline(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, uint32_t m, uint32_t n, bool aligned, ggml_type src0_type) {
  3585. VK_LOG_DEBUG("ggml_vk_guess_matmul_id_pipeline(" << m << ", " << n << ", " << aligned << ", " << ggml_type_name(src0_type) << ")");
  3586. if (ctx->device->coopmat2) {
  3587. // Use large shader when the N dimension is greater than the medium shader's tile size
  3588. uint32_t crossover_large = mmp->m->wg_denoms[1];
  3589. if ((ctx->device->mul_mat_id_l[src0_type] && (n > crossover_large)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_s[src0_type])) {
  3590. return aligned ? mmp->a_l : mmp->l;
  3591. }
  3592. // Use medium shader when the N dimension is greater than the small shader's tile size
  3593. uint32_t crossover_medium = mmp->s->wg_denoms[1];
  3594. if ((ctx->device->mul_mat_id_m[src0_type] && (n > crossover_medium)) || !ctx->device->mul_mat_id_s[src0_type]) {
  3595. return aligned ? mmp->a_m : mmp->m;
  3596. }
  3597. return aligned ? mmp->a_s : mmp->s;
  3598. }
  3599. if ((ctx->device->mul_mat_id_s[src0_type] && (m <= 32 || n <= 32)) || (!ctx->device->mul_mat_id_m[src0_type] && !ctx->device->mul_mat_id_l[src0_type])) {
  3600. return aligned ? mmp->a_s : mmp->s;
  3601. }
  3602. if ((ctx->device->mul_mat_id_m[src0_type] && (m <= 64 || n <= 64)) || !ctx->device->mul_mat_id_l[src0_type]) {
  3603. return aligned ? mmp->a_m : mmp->m;
  3604. }
  3605. return aligned ? mmp->a_l : mmp->l;
  3606. }
  3607. static uint32_t ggml_vk_guess_matmul_id_pipeline_align(ggml_backend_vk_context * ctx, vk_matmul_pipeline& mmp, int m, int n, ggml_type src0_type) {
  3608. VK_LOG_DEBUG("ggml_vk_guess_matmul_pipeline_align(" << m << ", " << n << ", " << ggml_type_name(src0_type) << ")");
  3609. return ggml_vk_guess_matmul_id_pipeline(ctx, mmp, m, n, true, src0_type)->align;
  3610. }
  3611. static void ggml_vk_matmul_id(
  3612. ggml_backend_vk_context * ctx, vk_context& subctx, vk_pipeline& pipeline,
  3613. vk_subbuffer&& a, vk_subbuffer&& b, vk_subbuffer&& d, vk_subbuffer&& ids,
  3614. uint32_t m, uint32_t n, uint32_t k, uint32_t stride_a, uint32_t stride_b, uint32_t stride_d,
  3615. uint32_t batch_stride_a, uint32_t batch_stride_b, uint32_t batch_stride_d,
  3616. uint32_t n_as, uint32_t nei0, uint32_t nei1, uint32_t nbi1, uint32_t ne11,
  3617. uint32_t padded_n) {
  3618. VK_LOG_DEBUG("ggml_vk_matmul_id(a: (" << a.buffer->buffer << ", " << a.offset << ", " << a.size << "), b: (" << b.buffer->buffer << ", " << b.offset << ", " << b.size << "), d: (" << d.buffer->buffer << ", " << d.offset << ", " << d.size << "), ids: (" << ids.buffer->buffer << ", " << ids.offset << ", " << ids.size << "), " <<
  3619. "m: " << m << ", n: " << n << ", k: " << k << ", stride_a: " << stride_a << ", stride_b: " << stride_b << ", stride_d: " << stride_d << ", " <<
  3620. "batch_stride_a: " << batch_stride_a << ", batch_stride_b: " << batch_stride_b << ", batch_stride_d: " << batch_stride_d << ", " <<
  3621. "n_as: " << n_as << ", nei0: " << nei0 << ", nei1: " << nei1 << ", nbi1: " << nbi1 << ", ne11: " << ne11 << ")");
  3622. ggml_vk_sync_buffers(subctx);
  3623. const vk_mat_mat_id_push_constants pc = { m, n, k, stride_a, stride_b, stride_d, batch_stride_a, batch_stride_b, batch_stride_d,
  3624. nei0, nei1, nbi1, ne11, padded_n };
  3625. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { a, b, d, ids }, sizeof(vk_mat_mat_id_push_constants), &pc, { m, nei1, n_as });
  3626. }
  3627. static bool ggml_vk_dim01_contiguous(const ggml_tensor * tensor) {
  3628. return
  3629. tensor->nb[0] == ggml_type_size(tensor->type) &&
  3630. tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
  3631. tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
  3632. }
  3633. static vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_tensor * src, const ggml_tensor * dst, ggml_type to) {
  3634. // Choose "contiguous copy" shader if src/dst are contiguous
  3635. bool contig = ggml_is_contiguous(src) && (!dst || ggml_is_contiguous(dst));
  3636. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F32) {
  3637. if (contig) {
  3638. return ctx->device->pipeline_contig_cpy_f32_f32;
  3639. } else {
  3640. return ctx->device->pipeline_cpy_f32_f32;
  3641. }
  3642. }
  3643. if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_F16) {
  3644. if (contig) {
  3645. return ctx->device->pipeline_contig_cpy_f32_f16;
  3646. } else {
  3647. return ctx->device->pipeline_cpy_f32_f16;
  3648. }
  3649. }
  3650. if (src->type == GGML_TYPE_F16 && to == GGML_TYPE_F16) {
  3651. if (contig) {
  3652. return ctx->device->pipeline_contig_cpy_f16_f16;
  3653. } else {
  3654. return ctx->device->pipeline_cpy_f16_f16;
  3655. }
  3656. }
  3657. if (src->type == GGML_TYPE_F32) {
  3658. switch (to) {
  3659. case GGML_TYPE_Q4_0:
  3660. case GGML_TYPE_Q4_1:
  3661. case GGML_TYPE_Q5_0:
  3662. case GGML_TYPE_Q5_1:
  3663. case GGML_TYPE_Q8_0:
  3664. case GGML_TYPE_IQ4_NL:
  3665. return ctx->device->pipeline_cpy_f32_quant[to];
  3666. default:
  3667. break;
  3668. }
  3669. }
  3670. if (to == GGML_TYPE_F32) {
  3671. switch (src->type) {
  3672. case GGML_TYPE_Q4_0:
  3673. case GGML_TYPE_Q4_1:
  3674. case GGML_TYPE_Q5_0:
  3675. case GGML_TYPE_Q5_1:
  3676. case GGML_TYPE_Q8_0:
  3677. case GGML_TYPE_IQ4_NL:
  3678. return ctx->device->pipeline_cpy_quant_f32[src->type];
  3679. default:
  3680. break;
  3681. }
  3682. }
  3683. std::cerr << "Missing CPY op for types: " << ggml_type_name(src->type) << " " << ggml_type_name(to) << std::endl;
  3684. GGML_ABORT("fatal error");
  3685. }
  3686. 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) {
  3687. VK_LOG_DEBUG("ggml_vk_cpy_to_contiguous((" << tensor << ", type=" << tensor->type << ", ne0=" << tensor->ne[0] << ", ne1=" << tensor->ne[1] << ", ne2=" << tensor->ne[2] << ", ne3=" << tensor->ne[3] << ", nb0=" << tensor->nb[0] << ", nb1=" << tensor->nb[1] << ", nb2=" << tensor->nb[2] << ", nb3=" << tensor->nb[3] << "), ";
  3688. std::cerr << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ")");
  3689. const int tensor_type_size = ggml_type_size(tensor->type);
  3690. const uint32_t ne = ggml_nelements(tensor);
  3691. std::array<uint32_t, 3> elements;
  3692. if (ne > 262144) {
  3693. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  3694. } else if (ne > 512) {
  3695. elements = { 512, CEIL_DIV(ne, 512), 1 };
  3696. } else {
  3697. elements = { ne, 1, 1 };
  3698. }
  3699. vk_op_unary_push_constants pc = {
  3700. (uint32_t)ne,
  3701. (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,
  3702. (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]),
  3703. 0,
  3704. 0.0f, 0.0f,
  3705. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3706. };
  3707. init_pushconst_fastdiv(pc);
  3708. ggml_vk_sync_buffers(subctx);
  3709. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(vk_op_unary_push_constants), &pc, elements);
  3710. }
  3711. static vk_pipeline ggml_vk_get_quantize_pipeline(ggml_backend_vk_context * ctx, ggml_type type) {
  3712. switch(type) {
  3713. case GGML_TYPE_Q8_1:
  3714. return ctx->device->pipeline_quantize_q8_1;
  3715. default:
  3716. std::cerr << "Missing quantize pipeline for type: " << ggml_type_name(type) << std::endl;
  3717. GGML_ABORT("fatal error");
  3718. }
  3719. }
  3720. static void ggml_vk_quantize_q8_1(ggml_backend_vk_context * ctx, vk_context& subctx, vk_subbuffer&& in, vk_subbuffer&& out, uint32_t ne) {
  3721. VK_LOG_DEBUG("ggml_vk_quantize_q8_1(" << "buffer in size=" << in.buffer->size << ", buffer out size=" << out.buffer->size << ", " << ne << ")");
  3722. vk_pipeline pipeline = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  3723. ggml_vk_sync_buffers(subctx);
  3724. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { in, out }, sizeof(uint32_t), &ne, { ne, 1, 1 });
  3725. }
  3726. static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  3727. VK_LOG_DEBUG("ggml_vk_mul_mat_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3728. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  3729. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  3730. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  3731. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3732. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3733. const uint64_t ne00 = src0->ne[0];
  3734. const uint64_t ne01 = src0->ne[1];
  3735. const uint64_t ne02 = src0->ne[2];
  3736. const uint64_t ne03 = src0->ne[3];
  3737. const uint64_t ne10 = src1->ne[0];
  3738. const uint64_t ne11 = src1->ne[1];
  3739. const uint64_t ne12 = src1->ne[2];
  3740. const uint64_t ne13 = src1->ne[3];
  3741. const uint64_t ne20 = dst->ne[0];
  3742. const uint64_t ne21 = dst->ne[1];
  3743. const uint64_t r2 = ne12 / ne02;
  3744. const uint64_t r3 = ne13 / ne03;
  3745. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3746. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3747. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3748. vk_buffer d_Qx = nullptr;
  3749. size_t qx_buf_offset = 0;
  3750. vk_buffer d_Qy = nullptr;
  3751. size_t qy_buf_offset = 0;
  3752. bool src0_uma = false;
  3753. bool src1_uma = false;
  3754. if (ctx->device->uma) {
  3755. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3756. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3757. src0_uma = d_Qx != nullptr;
  3758. src1_uma = d_Qy != nullptr;
  3759. }
  3760. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  3761. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  3762. !ggml_vk_dim01_contiguous(src0);
  3763. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  3764. !ggml_vk_dim01_contiguous(src1);
  3765. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  3766. bool quantize_y = ctx->device->integer_dot_product && src1->type == GGML_TYPE_F32 && ggml_is_contiguous(src1) && (ne11 * ne10) % 4 == 0;
  3767. // Check for mmq first
  3768. vk_matmul_pipeline mmp = quantize_y ? ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, GGML_TYPE_Q8_1, (ggml_prec)dst->op_params[0]) : nullptr;
  3769. if (mmp == nullptr) {
  3770. // Fall back to f16 dequant mul mat
  3771. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  3772. quantize_y = false;
  3773. }
  3774. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  3775. const bool qy_needs_dequant = !quantize_y && ((src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig);
  3776. if (qx_needs_dequant) {
  3777. // Fall back to dequant + f16 mulmat
  3778. mmp = ggml_vk_get_mul_mat_mat_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  3779. }
  3780. // Not implemented
  3781. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3782. const uint32_t kpad = quantize_y ? 0 : ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align(ctx, mmp, ne01, ne11, qx_needs_dequant ? GGML_TYPE_F16 : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type)));
  3783. const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
  3784. vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline(ctx, mmp, ne01, ne11, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type, quantize_y ? GGML_TYPE_Q8_1 : (y_f32_kernel ? GGML_TYPE_F32 : src1->type));
  3785. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  3786. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) : ne11;
  3787. const int x_ne = ne01 * ne00;
  3788. const int y_ne = padded_n * ne10;
  3789. const int d_ne = ne11 * ne01;
  3790. const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, pipeline);
  3791. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  3792. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3793. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  3794. const uint64_t y_sz = quantize_y ? (y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1)) : (y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne);
  3795. const uint64_t d_sz = sizeof(float) * d_ne;
  3796. vk_pipeline to_fp16_vk_0 = nullptr;
  3797. vk_pipeline to_fp16_vk_1 = nullptr;
  3798. vk_pipeline to_q8_1 = nullptr;
  3799. if (x_non_contig) {
  3800. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  3801. } else {
  3802. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  3803. }
  3804. if (y_non_contig) {
  3805. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  3806. } else {
  3807. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3808. }
  3809. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3810. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3811. if (quantize_y) {
  3812. to_q8_1 = ggml_vk_get_quantize_pipeline(ctx, GGML_TYPE_Q8_1);
  3813. }
  3814. if (dryrun) {
  3815. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3816. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3817. const uint64_t split_k_size = split_k > 1 ? d_sz * ne12 * ne13 * split_k : 0;
  3818. if (
  3819. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3820. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size) ||
  3821. (split_k > 1 && split_k_size > ctx->device->max_memory_allocation_size)) {
  3822. GGML_ABORT("Requested preallocation size is too large");
  3823. }
  3824. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3825. ctx->prealloc_size_x = x_sz_upd;
  3826. }
  3827. if ((qy_needs_dequant || quantize_y) && ctx->prealloc_size_y < y_sz_upd) {
  3828. ctx->prealloc_size_y = y_sz_upd;
  3829. }
  3830. if (split_k > 1 && ctx->prealloc_size_split_k < split_k_size) {
  3831. ctx->prealloc_size_split_k = split_k_size;
  3832. }
  3833. // Request descriptor sets
  3834. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  3835. if (qx_needs_dequant) {
  3836. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  3837. }
  3838. if (qy_needs_dequant) {
  3839. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  3840. }
  3841. if (quantize_y) {
  3842. ggml_pipeline_request_descriptor_sets(ctx->device, to_q8_1, 1);
  3843. }
  3844. if (split_k > 1) {
  3845. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, 1);
  3846. }
  3847. return;
  3848. }
  3849. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  3850. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  3851. GGML_ASSERT(d_D != nullptr);
  3852. GGML_ASSERT(d_D->size >= d_buf_offset + d_sz * ne02 * ne03);
  3853. vk_buffer d_X;
  3854. uint64_t x_buf_offset = 0;
  3855. vk_buffer d_Y;
  3856. uint64_t y_buf_offset = 0;
  3857. if (!src0_uma) {
  3858. d_Qx = src0_buf_ctx->dev_buffer;
  3859. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  3860. GGML_ASSERT(d_Qx != nullptr);
  3861. }
  3862. if (!src1_uma) {
  3863. d_Qy = src1_buf_ctx->dev_buffer;
  3864. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  3865. GGML_ASSERT(d_Qy != nullptr);
  3866. }
  3867. if (qx_needs_dequant) {
  3868. d_X = ctx->prealloc_x;
  3869. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  3870. } else {
  3871. d_X = d_Qx;
  3872. x_buf_offset = qx_buf_offset;
  3873. GGML_ASSERT(qx_sz == x_sz);
  3874. }
  3875. if (qy_needs_dequant) {
  3876. d_Y = ctx->prealloc_y;
  3877. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  3878. } else if (quantize_y) {
  3879. d_Y = ctx->prealloc_y;
  3880. GGML_ASSERT(d_Y->size >= y_ne * ggml_type_size(GGML_TYPE_Q8_1) / ggml_blck_size(GGML_TYPE_Q8_1));
  3881. } else {
  3882. d_Y = d_Qy;
  3883. y_buf_offset = qy_buf_offset;
  3884. GGML_ASSERT(qy_sz == y_sz);
  3885. }
  3886. if (x_non_contig) {
  3887. 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 });
  3888. } else if (qx_needs_dequant) {
  3889. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  3890. ggml_vk_sync_buffers(subctx);
  3891. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0, { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  3892. }
  3893. if (y_non_contig) {
  3894. 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 });
  3895. }
  3896. if (quantize_y) {
  3897. ggml_vk_quantize_q8_1(ctx, subctx, { d_Qy, qy_buf_offset, VK_WHOLE_SIZE }, { d_Y, 0, VK_WHOLE_SIZE }, y_ne * ne12 * ne13);
  3898. }
  3899. uint32_t stride_batch_x = ne00*ne01;
  3900. uint32_t stride_batch_y = ne10*ne11;
  3901. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  3902. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  3903. }
  3904. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant && !quantize_y) {
  3905. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  3906. }
  3907. // compute
  3908. ggml_vk_matmul(
  3909. ctx, subctx, pipeline,
  3910. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  3911. { d_D, d_buf_offset, d_sz * ne12 * ne13 }, { ctx->prealloc_split_k, 0, d_sz * ne12 * ne13 * split_k },
  3912. ne01, ne11, ne10,
  3913. ne10, ne10, ne01, stride_batch_x, stride_batch_y, ne20*ne21,
  3914. split_k, ne12*ne13, ne02, ne12, r2, r3, padded_n
  3915. ); // NOLINT
  3916. }
  3917. static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  3918. VK_LOG_DEBUG("ggml_vk_mul_mat_vec_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  3919. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  3920. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  3921. std::cerr << "), " << (dryrun ? "dryrun" : "") << "),)");
  3922. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  3923. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  3924. const uint64_t ne00 = src0->ne[0];
  3925. const uint64_t ne01 = src0->ne[1];
  3926. const uint64_t ne02 = src0->ne[2];
  3927. const uint64_t ne03 = src0->ne[3];
  3928. const uint64_t ne10 = src1->ne[0];
  3929. const uint64_t ne11 = src1->ne[1];
  3930. const uint64_t ne12 = src1->ne[2];
  3931. const uint64_t ne13 = src1->ne[3];
  3932. const uint64_t ne20 = dst->ne[0];
  3933. const uint64_t ne21 = dst->ne[1];
  3934. const uint64_t ne22 = dst->ne[2];
  3935. const uint64_t ne23 = dst->ne[3];
  3936. const uint64_t r2 = ne12 / ne02;
  3937. const uint64_t r3 = ne13 / ne03;
  3938. // batch_n indicates that we need to compute a few vector results, and this assumes
  3939. // ne12 and ne13 are 1. It overloads the batch_strides to hold the row strides.
  3940. GGML_ASSERT(ne11 == 1 || ne12 * ne13 == 1);
  3941. bool batch_n = ne11 > 1;
  3942. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  3943. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  3944. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  3945. vk_buffer d_Qx = nullptr;
  3946. size_t qx_buf_offset = 0;
  3947. vk_buffer d_Qy = nullptr;
  3948. size_t qy_buf_offset = 0;
  3949. bool src0_uma = false;
  3950. bool src1_uma = false;
  3951. if (ctx->device->uma) {
  3952. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  3953. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  3954. src0_uma = d_Qx != nullptr;
  3955. src1_uma = d_Qy != nullptr;
  3956. }
  3957. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  3958. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  3959. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  3960. const bool qx_needs_dequant = x_non_contig;
  3961. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  3962. // Not implemented
  3963. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  3964. const uint64_t x_ne = ne01 * ne00;
  3965. const uint64_t y_ne = ne11 * ne10;
  3966. const uint64_t d_ne = ne11 * ne01;
  3967. 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);
  3968. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  3969. 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;
  3970. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  3971. const uint64_t d_sz = sizeof(float) * d_ne;
  3972. vk_pipeline to_fp16_vk_0 = nullptr;
  3973. vk_pipeline to_fp16_vk_1 = nullptr;
  3974. if (x_non_contig) {
  3975. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  3976. }
  3977. if (y_non_contig) {
  3978. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  3979. } else {
  3980. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  3981. }
  3982. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec(ctx, src0->type, src1->type, ne11);
  3983. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  3984. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  3985. GGML_ASSERT(dmmv != nullptr);
  3986. if (dryrun) {
  3987. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  3988. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  3989. if (
  3990. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  3991. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  3992. GGML_ABORT("Requested preallocation size is too large");
  3993. }
  3994. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  3995. ctx->prealloc_size_x = x_sz_upd;
  3996. }
  3997. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  3998. ctx->prealloc_size_y = y_sz_upd;
  3999. }
  4000. // Request descriptor sets
  4001. if (qx_needs_dequant) {
  4002. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4003. }
  4004. if (qy_needs_dequant) {
  4005. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4006. }
  4007. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  4008. return;
  4009. }
  4010. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4011. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4012. GGML_ASSERT(d_D != nullptr);
  4013. vk_buffer d_X;
  4014. uint64_t x_buf_offset = 0;
  4015. vk_buffer d_Y;
  4016. uint64_t y_buf_offset = 0;
  4017. if(!src0_uma) {
  4018. d_Qx = src0_buf_ctx->dev_buffer;
  4019. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4020. GGML_ASSERT(d_Qx != nullptr);
  4021. }
  4022. if(!src1_uma) {
  4023. d_Qy = src1_buf_ctx->dev_buffer;
  4024. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4025. GGML_ASSERT(d_Qy != nullptr);
  4026. }
  4027. if (qx_needs_dequant) {
  4028. d_X = ctx->prealloc_x;
  4029. } else {
  4030. d_X = d_Qx;
  4031. x_buf_offset = qx_buf_offset;
  4032. GGML_ASSERT(qx_sz == x_sz);
  4033. }
  4034. if (qy_needs_dequant) {
  4035. d_Y = ctx->prealloc_y;
  4036. } else {
  4037. d_Y = d_Qy;
  4038. y_buf_offset = qy_buf_offset;
  4039. GGML_ASSERT(qy_sz == y_sz);
  4040. }
  4041. if (x_non_contig) {
  4042. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4043. 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 });
  4044. }
  4045. if (y_non_contig) {
  4046. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4047. 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 });
  4048. }
  4049. // For batch_n, the A matrix is the same for each batch, and B/D use the row stride as the batch stride
  4050. uint32_t stride_batch_x = batch_n ? 0 : ne00*ne01;
  4051. uint32_t stride_batch_y = batch_n ? ne10 : (ne10*ne11);
  4052. uint32_t stride_batch_d = batch_n ? ne20 : (ne20*ne21);
  4053. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4054. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4055. }
  4056. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4057. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4058. }
  4059. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4060. uint32_t groups_x = ne01;
  4061. uint32_t groups_z = 1;
  4062. if (ne01 > max_groups_x) {
  4063. groups_z = 64;
  4064. groups_x = CEIL_DIV(groups_x, groups_z);
  4065. }
  4066. // compute
  4067. const vk_mat_vec_push_constants pc = {
  4068. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4069. stride_batch_x, stride_batch_y, stride_batch_d,
  4070. (uint32_t)ne02, (uint32_t)ne12, (uint32_t)r2, (uint32_t)r3,
  4071. };
  4072. ggml_vk_sync_buffers(subctx);
  4073. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4074. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 }, vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23} },
  4075. sizeof(vk_mat_vec_push_constants), &pc, { groups_x, (uint32_t)(ne12 * ne13), groups_z });
  4076. }
  4077. static void ggml_vk_mul_mat_vec_p021_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4078. VK_LOG_DEBUG("ggml_vk_mul_mat_p021_f16_f32(" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4079. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  4080. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  4081. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4082. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  4083. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // NOLINT
  4084. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // NOLINT
  4085. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4086. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4087. const uint64_t ne00 = src0->ne[0];
  4088. const uint64_t ne01 = src0->ne[1];
  4089. const uint64_t ne02 = src0->ne[2];
  4090. // const uint64_t ne03 = src0->ne[3];
  4091. const uint64_t ne10 = src1->ne[0];
  4092. const uint64_t ne11 = src1->ne[1];
  4093. const uint64_t ne12 = src1->ne[2];
  4094. // const uint64_t ne13 = src1->ne[3];
  4095. GGML_ASSERT(ne11 == 1);
  4096. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4097. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4098. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4099. vk_buffer d_Qy = nullptr;
  4100. size_t qy_buf_offset = 0;
  4101. bool src1_uma = false;
  4102. if (ctx->device->uma) {
  4103. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4104. src1_uma = d_Qy != nullptr;
  4105. }
  4106. const uint64_t x_ne = ne00 * ne01 * ne02;
  4107. const uint64_t y_ne = ne10 * ne11 * ne12;
  4108. const uint64_t d_ne = ne01 * ne11 * ne12;
  4109. 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);
  4110. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4111. const uint64_t d_sz = sizeof(float) * d_ne;
  4112. // With grouped query attention there are > 1 Q matrices per K, V matrix.
  4113. uint32_t gqa_ratio = (uint32_t)ne12 / (uint32_t)ne02;
  4114. if (gqa_ratio > 8 || gqa_ratio == 0 || ne12 != ne02 * gqa_ratio) {
  4115. gqa_ratio = 1;
  4116. }
  4117. if (dryrun) {
  4118. // Request descriptor sets
  4119. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], 1);
  4120. return;
  4121. }
  4122. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4123. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4124. GGML_ASSERT(d_D != nullptr);
  4125. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4126. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4127. GGML_ASSERT(d_Qx != nullptr);
  4128. if (!src1_uma) {
  4129. d_Qy = src1_buf_ctx->dev_buffer;
  4130. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4131. GGML_ASSERT(d_Qx != nullptr);
  4132. }
  4133. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4134. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4135. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4136. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4137. // compute
  4138. 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)) };
  4139. uint32_t workgroups_z = (uint32_t)ne12;
  4140. // When gqa_ratio > 1, each invocation does multiple rows and we can launch fewer workgroups
  4141. if (gqa_ratio > 1) {
  4142. workgroups_z /= gqa_ratio;
  4143. }
  4144. ggml_vk_sync_buffers(subctx);
  4145. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_p021_f16_f32[gqa_ratio - 1], { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, 6 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, workgroups_z });
  4146. }
  4147. static void ggml_vk_mul_mat_vec_nc_f16_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4148. VK_LOG_DEBUG("ggml_vk_mul_mat_nc_f16_f32((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4149. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  4150. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  4151. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4152. GGML_ASSERT(!ggml_is_transposed(src0));
  4153. GGML_ASSERT(!ggml_is_transposed(src1));
  4154. GGML_ASSERT(!ggml_is_permuted(src0));
  4155. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4156. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4157. const uint64_t ne00 = src0->ne[0];
  4158. const uint64_t ne01 = src0->ne[1];
  4159. const uint64_t ne02 = src0->ne[2];
  4160. // const uint64_t ne03 = src0->ne[3];
  4161. const uint64_t nb01 = src0->nb[1];
  4162. const uint64_t nb02 = src0->nb[2];
  4163. // const uint64_t ne10 = src1->ne[0];
  4164. const uint64_t ne11 = src1->ne[1];
  4165. const uint64_t ne12 = src1->ne[2];
  4166. // const uint64_t ne13 = src1->ne[3];
  4167. GGML_ASSERT(ne11 == 1);
  4168. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4169. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4170. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4171. vk_buffer d_Qy = nullptr;
  4172. size_t qy_buf_offset = 0;
  4173. bool src1_uma = false;
  4174. if (ctx->device->uma) {
  4175. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4176. src1_uma = d_Qy != nullptr;
  4177. }
  4178. const uint64_t d_ne = ne01 * ne11 * ne12;
  4179. const uint32_t row_stride_x = nb01 / sizeof(ggml_fp16_t);
  4180. const uint32_t channel_stride_x = nb02 / sizeof(ggml_fp16_t);
  4181. const uint64_t qx_sz = ggml_nbytes(src0);
  4182. const uint64_t qy_sz = ggml_nbytes(src1);
  4183. const uint64_t d_sz = sizeof(float) * d_ne;
  4184. if (dryrun) {
  4185. // Request descriptor sets
  4186. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_mul_mat_vec_nc_f16_f32, 1);
  4187. return;
  4188. }
  4189. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4190. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4191. GGML_ASSERT(d_D != nullptr);
  4192. vk_buffer d_Qx = src0_buf_ctx->dev_buffer;
  4193. const uint64_t qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4194. GGML_ASSERT(d_Qx != nullptr);
  4195. if (!src1_uma) {
  4196. d_Qy = src1_buf_ctx->dev_buffer;
  4197. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4198. GGML_ASSERT(d_Qx != nullptr);
  4199. }
  4200. const uint64_t qy_buffer_offset = (qy_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4201. const uint64_t qy_shader_offset = qy_buf_offset - qy_buffer_offset;
  4202. const uint64_t d_buffer_offset = (d_buf_offset / ctx->device->properties.limits.minStorageBufferOffsetAlignment) * ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  4203. const uint64_t d_shader_offset = d_buf_offset - d_buffer_offset;
  4204. // compute
  4205. 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)) };
  4206. ggml_vk_sync_buffers(subctx);
  4207. ggml_vk_dispatch_pipeline(ctx, subctx, ctx->device->pipeline_mul_mat_vec_nc_f16_f32,
  4208. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz }, vk_subbuffer{ d_Qy, qy_buffer_offset, qy_sz + qy_shader_offset }, vk_subbuffer{ d_D, d_buffer_offset, d_sz + d_shader_offset } }, 7 * sizeof(uint32_t), &pc, { 1, (uint32_t)ne01, (uint32_t)ne12 });
  4209. }
  4210. static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  4211. VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
  4212. if (src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && dst->ne[1] == 1 &&
  4213. // detect 0213 permutation, and batch size of 1
  4214. src0->nb[0] <= src0->nb[2] &&
  4215. src0->nb[2] <= src0->nb[1] &&
  4216. src0->nb[1] <= src0->nb[3] &&
  4217. src1->nb[0] <= src1->nb[2] &&
  4218. src1->nb[2] <= src1->nb[1] &&
  4219. src1->nb[1] <= src1->nb[3] &&
  4220. src0->ne[3] == 1 &&
  4221. src1->ne[3] == 1) {
  4222. ggml_vk_mul_mat_vec_p021_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4223. } else if (src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && dst->ne[1] == 1 &&
  4224. !ggml_is_permuted(src0) && !ggml_is_permuted(src1)) {
  4225. ggml_vk_mul_mat_vec_nc_f16_f32(ctx, subctx, src0, src1, dst, dryrun);
  4226. // mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
  4227. // when ne12 and ne13 are one.
  4228. } else if ((dst->ne[1] == 1 || (dst->ne[1] <= mul_mat_vec_max_cols && src1->ne[2] * src1->ne[3] == 1)) &&
  4229. (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4230. ggml_vk_mul_mat_vec_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4231. } else {
  4232. ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, dryrun);
  4233. }
  4234. }
  4235. static void ggml_vk_mul_mat_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  4236. VK_LOG_DEBUG("ggml_vk_mul_mat_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4237. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  4238. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
  4239. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3] << "),)");
  4240. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4241. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4242. const uint64_t ne00 = src0->ne[0];
  4243. const uint64_t ne01 = src0->ne[1];
  4244. const uint64_t ne02 = src0->ne[2];
  4245. const uint64_t ne03 = src0->ne[3];
  4246. const uint64_t ne10 = src1->ne[0];
  4247. const uint64_t ne11 = src1->ne[1];
  4248. const uint64_t ne12 = src1->ne[2];
  4249. const uint64_t ne13 = src1->ne[3];
  4250. const uint64_t nei0 = ids->ne[0];
  4251. const uint64_t nei1 = ids->ne[1];
  4252. GGML_ASSERT(nei0 * nei1 <= 3072);
  4253. const uint32_t nbi1 = ids->nb[1];
  4254. const uint32_t nbi2 = ids->nb[2];
  4255. const uint64_t ne20 = dst->ne[0];
  4256. const uint64_t ne21 = dst->ne[1];
  4257. const uint64_t ne22 = dst->ne[2];
  4258. const uint64_t ne23 = dst->ne[3];
  4259. const uint64_t n_as = ne02;
  4260. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4261. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4262. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4263. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4264. vk_buffer d_Qx = nullptr;
  4265. size_t qx_buf_offset = 0;
  4266. vk_buffer d_Qy = nullptr;
  4267. size_t qy_buf_offset = 0;
  4268. vk_buffer d_ids = nullptr;
  4269. size_t ids_buf_offset = 0;
  4270. bool src0_uma = false;
  4271. bool src1_uma = false;
  4272. bool ids_uma = false;
  4273. if (ctx->device->uma) {
  4274. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4275. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4276. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4277. src0_uma = d_Qx != nullptr;
  4278. src1_uma = d_Qy != nullptr;
  4279. ids_uma = d_ids != nullptr;
  4280. }
  4281. // Reformat and convert to fp16 if non-contiguous, or for coopmat2 for better perf
  4282. const bool x_non_contig = (ctx->device->coopmat2 && src0->type == GGML_TYPE_F32) ||
  4283. !ggml_vk_dim01_contiguous(src0);
  4284. const bool y_non_contig = (ctx->device->coopmat2 && src1->type == GGML_TYPE_F32) ||
  4285. !ggml_vk_dim01_contiguous(src1);
  4286. const bool y_f32_kernel = src1->type == GGML_TYPE_F32 && !y_non_contig;
  4287. vk_matmul_pipeline mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, src0->type, y_non_contig ? GGML_TYPE_F16 : src1->type, (ggml_prec)dst->op_params[0]);
  4288. const bool qx_needs_dequant = mmp == nullptr || x_non_contig;
  4289. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !y_f32_kernel) || y_non_contig;
  4290. if (qx_needs_dequant) {
  4291. // Fall back to dequant + f16 mulmat
  4292. mmp = ggml_vk_get_mul_mat_mat_id_pipeline(ctx, GGML_TYPE_F16, y_f32_kernel ? GGML_TYPE_F32 : GGML_TYPE_F16, (ggml_prec)dst->op_params[0]);
  4293. }
  4294. // Not implemented
  4295. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4296. const uint32_t kpad = ggml_vk_align_size(ne10, ggml_vk_guess_matmul_id_pipeline_align(ctx, mmp, ne01, nei1, qx_needs_dequant ? GGML_TYPE_F16 : src0->type));
  4297. const bool aligned = ne10 == kpad && ne01 > 8 && nei1 > 8;
  4298. vk_pipeline pipeline = ggml_vk_guess_matmul_id_pipeline(ctx, mmp, ne01, nei1, aligned, qx_needs_dequant ? GGML_TYPE_F16 : src0->type);
  4299. // Reserve extra storage in the N dimension for the Y matrix, so we can avoid bounds-checking
  4300. uint32_t padded_n = qy_needs_dequant ? ROUNDUP_POW2(ne11, pipeline->wg_denoms[1]) :ne11;
  4301. const uint64_t x_ne = ne01 * ne00;
  4302. const uint64_t y_ne = padded_n * ne10;
  4303. const uint64_t d_ne = ne21 * ne20;
  4304. const uint64_t qx_sz = ggml_type_size(src0->type) * x_ne / ggml_blck_size(src0->type);
  4305. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4306. const uint64_t x_sz = !qx_needs_dequant ? qx_sz : sizeof(ggml_fp16_t) * x_ne;
  4307. const uint64_t y_sz = y_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4308. const uint64_t ids_sz = nbi2;
  4309. const uint64_t d_sz = sizeof(float) * d_ne;
  4310. vk_pipeline to_fp16_vk_0 = nullptr;
  4311. vk_pipeline to_fp16_vk_1 = nullptr;
  4312. if (x_non_contig) {
  4313. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, GGML_TYPE_F16);
  4314. } else {
  4315. to_fp16_vk_0 = ggml_vk_get_to_fp16(ctx, src0->type);
  4316. }
  4317. if (y_non_contig) {
  4318. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, GGML_TYPE_F16);
  4319. } else {
  4320. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4321. }
  4322. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4323. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4324. if (dryrun) {
  4325. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4326. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4327. if (
  4328. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4329. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4330. GGML_ABORT("Requested preallocation size is too large");
  4331. }
  4332. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4333. ctx->prealloc_size_x = x_sz_upd;
  4334. }
  4335. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4336. ctx->prealloc_size_y = y_sz_upd;
  4337. }
  4338. // Request descriptor sets
  4339. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4340. if (qx_needs_dequant) {
  4341. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4342. }
  4343. if (qy_needs_dequant) {
  4344. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4345. }
  4346. return;
  4347. }
  4348. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4349. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4350. GGML_ASSERT(d_D != nullptr);
  4351. vk_buffer d_X;
  4352. uint64_t x_buf_offset = 0;
  4353. vk_buffer d_Y;
  4354. uint64_t y_buf_offset = 0;
  4355. if (!src0_uma) {
  4356. d_Qx = src0_buf_ctx->dev_buffer;
  4357. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4358. GGML_ASSERT(d_Qx != nullptr);
  4359. }
  4360. if (!src1_uma) {
  4361. d_Qy = src1_buf_ctx->dev_buffer;
  4362. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4363. GGML_ASSERT(d_Qy != nullptr);
  4364. }
  4365. if (!ids_uma) {
  4366. d_ids = ids_buf_ctx->dev_buffer;
  4367. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4368. GGML_ASSERT(d_ids != nullptr);
  4369. }
  4370. if (qx_needs_dequant) {
  4371. d_X = ctx->prealloc_x;
  4372. GGML_ASSERT(d_X->size >= x_sz * ne02 * ne03);
  4373. } else {
  4374. d_X = d_Qx;
  4375. x_buf_offset = qx_buf_offset;
  4376. GGML_ASSERT(qx_sz == x_sz);
  4377. }
  4378. if (qy_needs_dequant) {
  4379. d_Y = ctx->prealloc_y;
  4380. GGML_ASSERT(d_Y->size >= y_sz * ne12 * ne13);
  4381. } else {
  4382. d_Y = d_Qy;
  4383. y_buf_offset = qy_buf_offset;
  4384. GGML_ASSERT(qy_sz == y_sz);
  4385. }
  4386. if (x_non_contig) {
  4387. 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 });
  4388. } else if (qx_needs_dequant) {
  4389. const std::vector<uint32_t> pc = { (uint32_t)ne01, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)(ggml_nelements(src0)) };
  4390. ggml_vk_sync_buffers(subctx);
  4391. ggml_vk_dispatch_pipeline(ctx, subctx, to_fp16_vk_0,
  4392. { vk_subbuffer{ d_Qx, qx_buf_offset, qx_sz * ne02 * ne03 }, vk_subbuffer{ d_X, 0, x_sz * ne02 * ne03 } }, pc.size() * sizeof(uint32_t), pc.data(), { (uint32_t)(x_ne * ne02 * ne03), 1, 1});
  4393. }
  4394. if (y_non_contig) {
  4395. 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 });
  4396. }
  4397. uint32_t stride_batch_x = ne00*ne01;
  4398. uint32_t stride_batch_y = ne10*ne11;
  4399. if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
  4400. stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
  4401. }
  4402. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4403. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4404. }
  4405. // compute
  4406. ggml_vk_matmul_id(
  4407. ctx, subctx, pipeline,
  4408. { d_X, x_buf_offset, x_sz * ne02 * ne03 }, { d_Y, y_buf_offset, y_sz * ne12 * ne13 },
  4409. { d_D, d_buf_offset, d_sz * ne22 * ne23 }, { d_ids, ids_buf_offset, ids_sz },
  4410. ne01, ne21, ne10, ne10, ne10, ne01,
  4411. stride_batch_x, stride_batch_y, ne20*ne21,
  4412. n_as, nei0, nei1, nbi1 / ggml_type_size(ids->type), ne11, padded_n
  4413. ); // NOLINT
  4414. }
  4415. static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, bool dryrun = false) {
  4416. VK_LOG_DEBUG("ggml_vk_mul_mat_vec_id_q_f16((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  4417. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  4418. std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
  4419. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  4420. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4421. GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); // NOLINT
  4422. GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
  4423. GGML_ASSERT(ids->type == GGML_TYPE_I32);
  4424. const uint64_t ne00 = src0->ne[0];
  4425. const uint64_t ne01 = src0->ne[1];
  4426. const uint64_t ne02 = src0->ne[2];
  4427. const uint64_t ne03 = src0->ne[3];
  4428. const uint64_t ne10 = src1->ne[0];
  4429. const uint64_t ne11 = src1->ne[1];
  4430. const uint64_t ne12 = src1->ne[2];
  4431. const uint64_t ne13 = src1->ne[3];
  4432. const uint64_t nei0 = ids->ne[0];
  4433. const uint64_t nei1 = ids->ne[1];
  4434. const uint64_t nbi2 = ids->nb[2];
  4435. GGML_ASSERT(nei1 == 1);
  4436. const uint64_t ne20 = dst->ne[0];
  4437. const uint64_t ne21 = dst->ne[1];
  4438. const uint64_t ne22 = dst->ne[2];
  4439. const uint64_t ne23 = dst->ne[3];
  4440. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4441. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  4442. ggml_backend_vk_buffer_context * src1_buf_ctx = (ggml_backend_vk_buffer_context *)src1->buffer->context;
  4443. ggml_backend_vk_buffer_context * ids_buf_ctx = (ggml_backend_vk_buffer_context *)ids->buffer->context;
  4444. vk_buffer d_Qx = nullptr;
  4445. size_t qx_buf_offset = 0;
  4446. vk_buffer d_Qy = nullptr;
  4447. size_t qy_buf_offset = 0;
  4448. vk_buffer d_ids = nullptr;
  4449. size_t ids_buf_offset = 0;
  4450. bool src0_uma = false;
  4451. bool src1_uma = false;
  4452. bool ids_uma = false;
  4453. if (ctx->device->uma) {
  4454. ggml_vk_host_get(ctx->device, src0->data, d_Qx, qx_buf_offset);
  4455. ggml_vk_host_get(ctx->device, src1->data, d_Qy, qy_buf_offset);
  4456. ggml_vk_host_get(ctx->device, ids->data, d_ids, ids_buf_offset);
  4457. src0_uma = d_Qx != nullptr;
  4458. src1_uma = d_Qy != nullptr;
  4459. ids_uma = d_ids != nullptr;
  4460. }
  4461. const bool x_non_contig = !ggml_vk_dim01_contiguous(src0);
  4462. const bool y_non_contig = !ggml_vk_dim01_contiguous(src1);
  4463. const bool f16_f32_kernel = src1->type == GGML_TYPE_F32;
  4464. const bool qx_needs_dequant = x_non_contig;
  4465. const bool qy_needs_dequant = (src1->type != GGML_TYPE_F16 && !f16_f32_kernel) || y_non_contig;
  4466. // Not implemented
  4467. GGML_ASSERT(y_non_contig || !qy_needs_dequant); // NOLINT
  4468. const uint64_t x_ne = ne01 * ne00;
  4469. const uint64_t y_ne = ne11 * ne10;
  4470. const uint64_t d_ne = ne21 * ne20;
  4471. 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);
  4472. const uint64_t qy_sz = ggml_type_size(src1->type) * y_ne / ggml_blck_size(src1->type);
  4473. 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;
  4474. const uint64_t y_sz = f16_f32_kernel ? sizeof(float) * y_ne : sizeof(ggml_fp16_t) * y_ne;
  4475. const uint64_t ids_sz = nbi2;
  4476. const uint64_t d_sz = sizeof(float) * d_ne;
  4477. vk_pipeline to_fp16_vk_0 = nullptr;
  4478. vk_pipeline to_fp16_vk_1 = nullptr;
  4479. if (x_non_contig) {
  4480. to_fp16_vk_0 = ggml_vk_get_cpy_pipeline(ctx, src0, nullptr, src0->type);
  4481. }
  4482. if (y_non_contig) {
  4483. to_fp16_vk_1 = ggml_vk_get_cpy_pipeline(ctx, src1, nullptr, src1->type);
  4484. } else {
  4485. to_fp16_vk_1 = ggml_vk_get_to_fp16(ctx, src1->type);
  4486. }
  4487. vk_pipeline dmmv = ggml_vk_get_dequantize_mul_mat_vec_id(ctx, src0->type, src1->type);
  4488. GGML_ASSERT(!qx_needs_dequant || to_fp16_vk_0 != nullptr); // NOLINT
  4489. GGML_ASSERT(!qy_needs_dequant || to_fp16_vk_1 != nullptr); // NOLINT
  4490. GGML_ASSERT(dmmv != nullptr);
  4491. if (dryrun) {
  4492. const uint64_t x_sz_upd = x_sz * ne02 * ne03;
  4493. const uint64_t y_sz_upd = y_sz * ne12 * ne13;
  4494. if (
  4495. (qx_needs_dequant && x_sz_upd > ctx->device->max_memory_allocation_size) ||
  4496. (qy_needs_dequant && y_sz_upd > ctx->device->max_memory_allocation_size)) {
  4497. GGML_ABORT("Requested preallocation size is too large");
  4498. }
  4499. if (qx_needs_dequant && ctx->prealloc_size_x < x_sz_upd) {
  4500. ctx->prealloc_size_x = x_sz_upd;
  4501. }
  4502. if (qy_needs_dequant && ctx->prealloc_size_y < y_sz_upd) {
  4503. ctx->prealloc_size_y = y_sz_upd;
  4504. }
  4505. // Request descriptor sets
  4506. if (qx_needs_dequant) {
  4507. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_0, 1);
  4508. }
  4509. if (qy_needs_dequant) {
  4510. ggml_pipeline_request_descriptor_sets(ctx->device, to_fp16_vk_1, 1);
  4511. }
  4512. ggml_pipeline_request_descriptor_sets(ctx->device, dmmv, 1);
  4513. return;
  4514. }
  4515. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  4516. const uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4517. GGML_ASSERT(d_D != nullptr);
  4518. vk_buffer d_X;
  4519. uint64_t x_buf_offset = 0;
  4520. vk_buffer d_Y;
  4521. uint64_t y_buf_offset = 0;
  4522. if(!src0_uma) {
  4523. d_Qx = src0_buf_ctx->dev_buffer;
  4524. qx_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  4525. GGML_ASSERT(d_Qx != nullptr);
  4526. }
  4527. if(!src1_uma) {
  4528. d_Qy = src1_buf_ctx->dev_buffer;
  4529. qy_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  4530. GGML_ASSERT(d_Qy != nullptr);
  4531. }
  4532. if(!ids_uma) {
  4533. d_ids = ids_buf_ctx->dev_buffer;
  4534. ids_buf_offset = vk_tensor_offset(ids) + ids->view_offs;
  4535. GGML_ASSERT(d_ids != nullptr);
  4536. }
  4537. if (qx_needs_dequant) {
  4538. d_X = ctx->prealloc_x;
  4539. } else {
  4540. d_X = d_Qx;
  4541. x_buf_offset = qx_buf_offset;
  4542. GGML_ASSERT(qx_sz == x_sz);
  4543. }
  4544. if (qy_needs_dequant) {
  4545. d_Y = ctx->prealloc_y;
  4546. } else {
  4547. d_Y = d_Qy;
  4548. y_buf_offset = qy_buf_offset;
  4549. GGML_ASSERT(qy_sz == y_sz);
  4550. }
  4551. if (x_non_contig) {
  4552. GGML_ASSERT(x_sz == ggml_vk_align_size(ggml_type_size(src0->type) * x_ne, ctx->device->properties.limits.minStorageBufferOffsetAlignment));
  4553. 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 });
  4554. }
  4555. if (y_non_contig) {
  4556. GGML_ASSERT(y_sz == ggml_type_size(src1->type) * y_ne);
  4557. 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 });
  4558. }
  4559. uint32_t stride_batch_y = ne10*ne11;
  4560. if (!ggml_vk_dim01_contiguous(src1) && !qy_needs_dequant) {
  4561. stride_batch_y = src1->nb[0] / ggml_type_size(src1->type);
  4562. }
  4563. const uint32_t max_groups_x = ctx->device->properties.limits.maxComputeWorkGroupCount[0];
  4564. uint32_t groups_x = ne01;
  4565. uint32_t groups_z = 1;
  4566. if (ne01 > max_groups_x) {
  4567. groups_z = 64;
  4568. groups_x = CEIL_DIV(groups_x, groups_z);
  4569. }
  4570. // compute
  4571. const vk_mat_vec_id_push_constants pc = {
  4572. (uint32_t)ne00, (uint32_t)ne10, (uint32_t)ne10, (uint32_t)ne01,
  4573. (uint32_t)x_ne, stride_batch_y, (uint32_t)(ne20*ne21),
  4574. (uint32_t)nei0, (uint32_t)ne11,
  4575. };
  4576. ggml_vk_sync_buffers(subctx);
  4577. ggml_vk_dispatch_pipeline(ctx, subctx, dmmv,
  4578. { vk_subbuffer{ d_X, x_buf_offset, x_sz * ne02 * ne03 },
  4579. vk_subbuffer{ d_Y, y_buf_offset, y_sz * ne12 * ne13 }, vk_subbuffer{ d_D, d_buf_offset, d_sz * ne22 * ne23}, vk_subbuffer{ d_ids, ids_buf_offset, ids_sz } },
  4580. sizeof(vk_mat_vec_id_push_constants), &pc, { groups_x, (uint32_t)nei0, groups_z });
  4581. }
  4582. static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool dryrun = false) {
  4583. VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
  4584. if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
  4585. ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4586. } else {
  4587. ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
  4588. }
  4589. }
  4590. static void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, ggml_tensor * dst, bool dryrun = false) {
  4591. VK_LOG_DEBUG("ggml_vk_flash_attn((" << q << ", name=" << q->name << ", type=" << q->type << ", ne0=" << q->ne[0] << ", ne1=" << q->ne[1] << ", ne2=" << q->ne[2] << ", ne3=" << q->ne[3] << ", nb0=" << q->nb[0] << ", nb1=" << q->nb[1] << ", nb2=" << q->nb[2] << ", nb3=" << q->nb[3];
  4592. std::cerr << "), (" << k << ", name=" << k->name << ", type=" << k->type << ", ne0=" << k->ne[0] << ", ne1=" << k->ne[1] << ", ne2=" << k->ne[2] << ", ne3=" << k->ne[3] << ", nb0=" << k->nb[0] << ", nb1=" << k->nb[1] << ", nb2=" << k->nb[2] << ", nb3=" << k->nb[3];
  4593. std::cerr << "), (" << v << ", name=" << v->name << ", type=" << v->type << ", ne0=" << v->ne[0] << ", ne1=" << v->ne[1] << ", ne2=" << v->ne[2] << ", ne3=" << v->ne[3] << ", nb0=" << v->nb[0] << ", nb1=" << v->nb[1] << ", nb2=" << v->nb[2] << ", nb3=" << v->nb[3];
  4594. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  4595. std::cerr << "), " << (dryrun ? "dryrun" : "") << ")");
  4596. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  4597. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  4598. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  4599. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  4600. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  4601. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  4602. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  4603. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  4604. const uint32_t nem1 = mask ? mask->ne[1] : 0;
  4605. const uint32_t nbm1 = mask ? mask->nb[1] : 0;
  4606. const uint32_t D = neq0;
  4607. const uint32_t N = neq1;
  4608. const uint32_t KV = nek1;
  4609. GGML_ASSERT(ne0 == D);
  4610. GGML_ASSERT(ne2 == N);
  4611. // input tensor rows must be contiguous
  4612. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  4613. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  4614. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  4615. GGML_ASSERT(neq0 == D);
  4616. GGML_ASSERT(nek0 == D);
  4617. GGML_ASSERT(nev0 == D);
  4618. GGML_ASSERT(neq1 == N);
  4619. GGML_ASSERT(nev0 == D);
  4620. GGML_ASSERT(nev1 == nek1);
  4621. // dst cannot be transposed or permuted
  4622. GGML_ASSERT(nb0 == sizeof(float));
  4623. GGML_ASSERT(nb0 <= nb1);
  4624. GGML_ASSERT(nb1 <= nb2);
  4625. GGML_ASSERT(nb2 <= nb3);
  4626. assert(dst->type == GGML_TYPE_F32);
  4627. assert(q->type == GGML_TYPE_F32);
  4628. assert(k->type == v->type);
  4629. vk_pipeline *pipelines;
  4630. // XXX TODO other backends may be changing accumulator precision to default to f32 soon
  4631. bool f32acc = dst->op_params[3] == GGML_PREC_F32;
  4632. bool small_rows = N <= flash_attention_num_small_rows;
  4633. switch (D) {
  4634. case 64: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D64[k->type][f32acc][small_rows][0]; break;
  4635. case 80: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D80[k->type][f32acc][small_rows][0]; break;
  4636. case 96: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D96[k->type][f32acc][small_rows][0]; break;
  4637. case 112: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D112[k->type][f32acc][small_rows][0]; break;
  4638. case 128: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D128[k->type][f32acc][small_rows][0]; break;
  4639. case 256: pipelines = &ctx->device->pipeline_flash_attn_f32_f16_D256[k->type][f32acc][small_rows][0]; break;
  4640. default:
  4641. assert(!"unsupported D value");
  4642. return;
  4643. }
  4644. assert(pipelines);
  4645. const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
  4646. const uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
  4647. const uint32_t v_stride = (uint32_t)(nbv1 / ggml_type_size(v->type));
  4648. bool aligned = (KV % pipelines[1]->align) == 0 &&
  4649. // the "aligned" shader variant will forcibly align strides, for performance
  4650. (q_stride & 7) == 0 && (k_stride & 7) == 0 && (v_stride & 7) == 0;
  4651. vk_pipeline pipeline = pipelines[aligned];
  4652. assert(pipeline);
  4653. if (dryrun) {
  4654. // Request descriptor sets
  4655. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  4656. return;
  4657. }
  4658. float scale = 1.0f;
  4659. float max_bias = 0.0f;
  4660. float logit_softcap = 0.0f;
  4661. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  4662. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  4663. memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
  4664. if (logit_softcap != 0) {
  4665. scale /= logit_softcap;
  4666. }
  4667. const uint32_t n_head_kv = neq2;
  4668. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  4669. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  4670. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  4671. ggml_vk_sync_buffers(subctx);
  4672. vk_buffer d_Q = nullptr, d_K = nullptr, d_V = nullptr, d_D = nullptr, d_M = nullptr;
  4673. size_t q_buf_offset = 0, k_buf_offset = 0, v_buf_offset = 0, d_buf_offset = 0, m_buf_offset = 0;
  4674. bool Q_uma = false, K_uma = false, V_uma = false, D_uma = false, M_uma = false;
  4675. if (ctx->device->uma) {
  4676. ggml_vk_host_get(ctx->device, q->data, d_Q, q_buf_offset);
  4677. ggml_vk_host_get(ctx->device, k->data, d_K, k_buf_offset);
  4678. ggml_vk_host_get(ctx->device, v->data, d_V, v_buf_offset);
  4679. ggml_vk_host_get(ctx->device, dst->data, d_D, d_buf_offset);
  4680. Q_uma = d_Q != nullptr;
  4681. K_uma = d_K != nullptr;
  4682. V_uma = d_V != nullptr;
  4683. D_uma = d_D != nullptr;
  4684. if (mask) {
  4685. ggml_vk_host_get(ctx->device, mask->data, d_M, m_buf_offset);
  4686. M_uma = d_M != nullptr;
  4687. }
  4688. }
  4689. ggml_backend_vk_buffer_context * d_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  4690. ggml_backend_vk_buffer_context * q_buf_ctx = (ggml_backend_vk_buffer_context *)q->buffer->context;
  4691. ggml_backend_vk_buffer_context * k_buf_ctx = (ggml_backend_vk_buffer_context *)k->buffer->context;
  4692. ggml_backend_vk_buffer_context * v_buf_ctx = (ggml_backend_vk_buffer_context *)v->buffer->context;
  4693. if (!Q_uma) {
  4694. d_Q = q_buf_ctx->dev_buffer;
  4695. q_buf_offset = vk_tensor_offset(q) + q->view_offs;
  4696. }
  4697. if (!K_uma) {
  4698. d_K = k_buf_ctx->dev_buffer;
  4699. k_buf_offset = vk_tensor_offset(k) + k->view_offs;
  4700. }
  4701. if (!V_uma) {
  4702. d_V = v_buf_ctx->dev_buffer;
  4703. v_buf_offset = vk_tensor_offset(v) + v->view_offs;
  4704. }
  4705. if (!D_uma) {
  4706. d_D = d_buf_ctx->dev_buffer;
  4707. d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  4708. }
  4709. if (!M_uma) {
  4710. d_M = d_Q;
  4711. m_buf_offset = q_buf_offset;
  4712. if (mask) {
  4713. ggml_backend_vk_buffer_context * m_buf_ctx = (ggml_backend_vk_buffer_context*)mask->buffer->context;
  4714. d_M = m_buf_ctx->dev_buffer;
  4715. m_buf_offset = vk_tensor_offset(mask) + mask->view_offs;
  4716. }
  4717. }
  4718. const vk_flash_attn_push_constants pc = { N, KV,
  4719. (uint32_t)ne1, (uint32_t)ne2, (uint32_t)ne3,
  4720. (uint32_t)neq2, (uint32_t)neq3,
  4721. (uint32_t)nek2, (uint32_t)nek3,
  4722. (uint32_t)nev2, (uint32_t)nev3,
  4723. nem1,
  4724. q_stride, (uint32_t)nbq2, (uint32_t)nbq3,
  4725. k_stride, (uint32_t)nbk2, (uint32_t)nbk3,
  4726. v_stride, (uint32_t)nbv2, (uint32_t)nbv3,
  4727. nbm1,
  4728. scale, max_bias, logit_softcap,
  4729. mask != nullptr, n_head_log2, m0, m1 };
  4730. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
  4731. {
  4732. vk_subbuffer{d_Q, q_buf_offset, VK_WHOLE_SIZE},
  4733. vk_subbuffer{d_K, k_buf_offset, VK_WHOLE_SIZE},
  4734. vk_subbuffer{d_V, v_buf_offset, VK_WHOLE_SIZE},
  4735. vk_subbuffer{d_M, m_buf_offset, VK_WHOLE_SIZE},
  4736. vk_subbuffer{d_D, d_buf_offset, VK_WHOLE_SIZE},
  4737. },
  4738. sizeof(vk_flash_attn_push_constants), &pc, { (uint32_t)neq1, (uint32_t)neq2, (uint32_t)neq3 });
  4739. }
  4740. 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) {
  4741. switch (op) {
  4742. case GGML_OP_GET_ROWS:
  4743. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4744. if (dst->type == GGML_TYPE_F16) {
  4745. return ctx->device->pipeline_get_rows[src0->type];
  4746. }
  4747. if (dst->type == GGML_TYPE_F32) {
  4748. return ctx->device->pipeline_get_rows_f32[src0->type];
  4749. }
  4750. return nullptr;
  4751. case GGML_OP_ACC:
  4752. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4753. return ctx->device->pipeline_acc_f32;
  4754. }
  4755. return nullptr;
  4756. case GGML_OP_ADD:
  4757. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4758. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f32_norepeat : ctx->device->pipeline_add_f32;
  4759. }
  4760. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4761. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_add_f16_f32_f16_norepeat : ctx->device->pipeline_add_f16_f32_f16;
  4762. }
  4763. return nullptr;
  4764. case GGML_OP_SUB:
  4765. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4766. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_sub_f32_norepeat : ctx->device->pipeline_sub_f32;
  4767. }
  4768. return nullptr;
  4769. case GGML_OP_MUL:
  4770. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4771. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_mul_f32_norepeat : ctx->device->pipeline_mul_f32;
  4772. }
  4773. return nullptr;
  4774. case GGML_OP_DIV:
  4775. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4776. return ggml_are_same_shape(src0, src1) ? ctx->device->pipeline_div_f32_norepeat : ctx->device->pipeline_div_f32;
  4777. }
  4778. return nullptr;
  4779. case GGML_OP_CONCAT:
  4780. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4781. return ctx->device->pipeline_concat_f32;
  4782. }
  4783. if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4784. return ctx->device->pipeline_concat_f16;
  4785. }
  4786. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  4787. return ctx->device->pipeline_concat_i32;
  4788. }
  4789. return nullptr;
  4790. case GGML_OP_UPSCALE:
  4791. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4792. return ctx->device->pipeline_upscale_f32;
  4793. }
  4794. return nullptr;
  4795. case GGML_OP_SCALE:
  4796. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4797. return ctx->device->pipeline_scale_f32;
  4798. }
  4799. return nullptr;
  4800. case GGML_OP_SQR:
  4801. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4802. return ctx->device->pipeline_sqr_f32;
  4803. }
  4804. return nullptr;
  4805. case GGML_OP_SIN:
  4806. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4807. return ctx->device->pipeline_sin_f32;
  4808. }
  4809. return nullptr;
  4810. case GGML_OP_COS:
  4811. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4812. return ctx->device->pipeline_cos_f32;
  4813. }
  4814. return nullptr;
  4815. case GGML_OP_CLAMP:
  4816. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4817. return ctx->device->pipeline_clamp_f32;
  4818. }
  4819. return nullptr;
  4820. case GGML_OP_PAD:
  4821. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4822. return ctx->device->pipeline_pad_f32;
  4823. }
  4824. return nullptr;
  4825. case GGML_OP_REPEAT:
  4826. if (ggml_type_size(src0->type) == sizeof(float) && ggml_type_size(dst->type) == sizeof(float)) {
  4827. return ctx->device->pipeline_repeat_f32;
  4828. }
  4829. return nullptr;
  4830. case GGML_OP_REPEAT_BACK:
  4831. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4832. return ctx->device->pipeline_repeat_back_f32;
  4833. }
  4834. return nullptr;
  4835. case GGML_OP_CPY:
  4836. case GGML_OP_CONT:
  4837. case GGML_OP_DUP:
  4838. return ggml_vk_get_cpy_pipeline(ctx, src0, dst, dst->type);
  4839. case GGML_OP_SILU_BACK:
  4840. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4841. return ctx->device->pipeline_silu_back_f32;
  4842. }
  4843. return nullptr;
  4844. case GGML_OP_NORM:
  4845. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4846. return ctx->device->pipeline_norm_f32;
  4847. }
  4848. return nullptr;
  4849. case GGML_OP_GROUP_NORM:
  4850. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4851. return ctx->device->pipeline_group_norm_f32;
  4852. }
  4853. return nullptr;
  4854. case GGML_OP_RMS_NORM:
  4855. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4856. return ctx->device->pipeline_rms_norm_f32;
  4857. }
  4858. return nullptr;
  4859. case GGML_OP_RMS_NORM_BACK:
  4860. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4861. return ctx->device->pipeline_rms_norm_back_f32;
  4862. }
  4863. return nullptr;
  4864. case GGML_OP_L2_NORM:
  4865. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4866. return ctx->device->pipeline_l2_norm_f32;
  4867. }
  4868. return nullptr;
  4869. case GGML_OP_UNARY:
  4870. switch (ggml_get_unary_op(dst)) {
  4871. case GGML_UNARY_OP_SILU:
  4872. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4873. return ctx->device->pipeline_silu_f32;
  4874. }
  4875. break;
  4876. case GGML_UNARY_OP_GELU:
  4877. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4878. return ctx->device->pipeline_gelu_f32;
  4879. }
  4880. break;
  4881. case GGML_UNARY_OP_GELU_QUICK:
  4882. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4883. return ctx->device->pipeline_gelu_quick_f32;
  4884. }
  4885. break;
  4886. case GGML_UNARY_OP_RELU:
  4887. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4888. return ctx->device->pipeline_relu_f32;
  4889. }
  4890. break;
  4891. case GGML_UNARY_OP_TANH:
  4892. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4893. return ctx->device->pipeline_tanh_f32;
  4894. }
  4895. break;
  4896. case GGML_UNARY_OP_SIGMOID:
  4897. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4898. return ctx->device->pipeline_sigmoid_f32;
  4899. }
  4900. break;
  4901. default:
  4902. break;
  4903. }
  4904. return nullptr;
  4905. case GGML_OP_DIAG_MASK_INF:
  4906. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4907. return ctx->device->pipeline_diag_mask_inf_f32;
  4908. }
  4909. return nullptr;
  4910. case GGML_OP_SOFT_MAX:
  4911. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16);
  4912. if (src0->type == GGML_TYPE_F32 && (src1 == nullptr || src1->type == GGML_TYPE_F32) && dst->type == GGML_TYPE_F32) {
  4913. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_wg512 : ctx->device->pipeline_soft_max_f32;
  4914. }
  4915. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  4916. return src0->ne[0] > 1024 ? ctx->device->pipeline_soft_max_f32_f16_wg512 : ctx->device->pipeline_soft_max_f32_f16;
  4917. }
  4918. return nullptr;
  4919. case GGML_OP_SOFT_MAX_BACK:
  4920. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4921. return ctx->device->pipeline_soft_max_back_f32;
  4922. }
  4923. return nullptr;
  4924. case GGML_OP_ROPE:
  4925. case GGML_OP_ROPE_BACK:
  4926. {
  4927. const int mode = ((const int32_t *) dst->op_params)[2];
  4928. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  4929. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  4930. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  4931. if (is_neox) {
  4932. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4933. return ctx->device->pipeline_rope_neox_f32;
  4934. }
  4935. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4936. return ctx->device->pipeline_rope_neox_f16;
  4937. }
  4938. } else if (is_mrope && !is_vision) {
  4939. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4940. return ctx->device->pipeline_rope_multi_f32;
  4941. }
  4942. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4943. return ctx->device->pipeline_rope_multi_f16;
  4944. }
  4945. } else if (is_vision) {
  4946. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4947. return ctx->device->pipeline_rope_vision_f32;
  4948. }
  4949. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4950. return ctx->device->pipeline_rope_vision_f16;
  4951. }
  4952. } else {
  4953. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4954. return ctx->device->pipeline_rope_norm_f32;
  4955. }
  4956. if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4957. return ctx->device->pipeline_rope_norm_f16;
  4958. }
  4959. }
  4960. return nullptr;
  4961. }
  4962. case GGML_OP_ARGSORT:
  4963. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  4964. return ctx->device->pipeline_argsort_f32;
  4965. }
  4966. return nullptr;
  4967. case GGML_OP_SUM:
  4968. case GGML_OP_SUM_ROWS:
  4969. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4970. return ctx->device->pipeline_sum_rows_f32;
  4971. }
  4972. return nullptr;
  4973. case GGML_OP_ARGMAX:
  4974. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_I32) {
  4975. return ctx->device->pipeline_argmax_f32;
  4976. }
  4977. return nullptr;
  4978. case GGML_OP_COUNT_EQUAL:
  4979. if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I64) {
  4980. return ctx->device->pipeline_count_equal_i32;
  4981. }
  4982. return nullptr;
  4983. case GGML_OP_IM2COL:
  4984. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4985. return ctx->device->pipeline_im2col_f32;
  4986. }
  4987. if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16) {
  4988. return ctx->device->pipeline_im2col_f32_f16;
  4989. }
  4990. return nullptr;
  4991. case GGML_OP_TIMESTEP_EMBEDDING:
  4992. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4993. return ctx->device->pipeline_timestep_embedding_f32;
  4994. }
  4995. return nullptr;
  4996. case GGML_OP_POOL_2D:
  4997. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4998. return ctx->device->pipeline_pool2d_f32;
  4999. }
  5000. return nullptr;
  5001. case GGML_OP_RWKV_WKV6:
  5002. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5003. return ctx->device->pipeline_rwkv_wkv6_f32;
  5004. }
  5005. return nullptr;
  5006. case GGML_OP_RWKV_WKV7:
  5007. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5008. return ctx->device->pipeline_rwkv_wkv7_f32;
  5009. }
  5010. return nullptr;
  5011. case GGML_OP_OPT_STEP_ADAMW:
  5012. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5013. return ctx->device->pipeline_opt_step_adamw_f32;
  5014. }
  5015. return nullptr;
  5016. case GGML_OP_LEAKY_RELU:
  5017. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5018. return ctx->device->pipeline_leaky_relu_f32;
  5019. }
  5020. return nullptr;
  5021. default:
  5022. return nullptr;
  5023. }
  5024. GGML_UNUSED(src2);
  5025. }
  5026. static bool ggml_vk_op_supports_incontiguous(ggml_op op) {
  5027. switch (op) {
  5028. case GGML_OP_CPY:
  5029. case GGML_OP_GET_ROWS:
  5030. case GGML_OP_ADD:
  5031. case GGML_OP_SUB:
  5032. case GGML_OP_MUL:
  5033. case GGML_OP_DIV:
  5034. case GGML_OP_CONCAT:
  5035. case GGML_OP_UPSCALE:
  5036. case GGML_OP_SQR:
  5037. case GGML_OP_SIN:
  5038. case GGML_OP_COS:
  5039. case GGML_OP_CLAMP:
  5040. case GGML_OP_PAD:
  5041. case GGML_OP_REPEAT:
  5042. case GGML_OP_REPEAT_BACK:
  5043. case GGML_OP_ROPE:
  5044. return true;
  5045. default:
  5046. return false;
  5047. }
  5048. }
  5049. static uint32_t get_misalign_bytes(ggml_backend_vk_context * ctx, const ggml_tensor * t)
  5050. {
  5051. return ((vk_tensor_offset(t) + t->view_offs) & (ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1));;
  5052. }
  5053. template <typename T> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, T &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5054. GGML_UNUSED(p);
  5055. GGML_UNUSED(src0);
  5056. GGML_UNUSED(src1);
  5057. GGML_UNUSED(src2);
  5058. GGML_UNUSED(dst);
  5059. static_assert(!std::is_const<T>::value, "unexpected type");
  5060. GGML_ASSERT(!src0 || get_misalign_bytes(ctx, src0) == 0);
  5061. GGML_ASSERT(!src1 || get_misalign_bytes(ctx, src1) == 0);
  5062. GGML_ASSERT(!src2 || get_misalign_bytes(ctx, src2) == 0);
  5063. GGML_ASSERT(!dst || get_misalign_bytes(ctx, dst) == 0);
  5064. }
  5065. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_unary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5066. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5067. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5068. p.misalign_offsets = (a_offset << 16) | d_offset;
  5069. GGML_UNUSED(src1);
  5070. GGML_UNUSED(src2);
  5071. }
  5072. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_binary_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5073. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5074. const uint32_t b_offset = get_misalign_bytes(ctx, src1) / ggml_type_size(src1->type);
  5075. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5076. GGML_ASSERT(dst->op != GGML_OP_GET_ROWS || (a_offset == 0 && b_offset == 0 && d_offset == 0));
  5077. p.misalign_offsets = (a_offset << 16) | (b_offset << 8) | d_offset;
  5078. GGML_UNUSED(src2);
  5079. }
  5080. template <> void init_pushconst_tensor_offsets(ggml_backend_vk_context * ctx, vk_op_upscale_push_constants &p, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst) {
  5081. const uint32_t a_offset = get_misalign_bytes(ctx, src0) / ggml_type_size(src0->type);
  5082. const uint32_t d_offset = get_misalign_bytes(ctx, dst) / ggml_type_size(dst->type);
  5083. p.a_offset = a_offset;
  5084. p.d_offset = d_offset;
  5085. GGML_UNUSED(src1);
  5086. GGML_UNUSED(src2);
  5087. }
  5088. template<typename PC>
  5089. static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, ggml_op op, PC&& pc, bool dryrun = false) {
  5090. VK_LOG_DEBUG("ggml_vk_op_f32((" << src0 << ", name=" << src0->name << ", type=" << src0->type << ", ne0=" << src0->ne[0] << ", ne1=" << src0->ne[1] << ", ne2=" << src0->ne[2] << ", ne3=" << src0->ne[3] << ", nb0=" << src0->nb[0] << ", nb1=" << src0->nb[1] << ", nb2=" << src0->nb[2] << ", nb3=" << src0->nb[3];
  5091. if (src1 != nullptr) {
  5092. std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
  5093. }
  5094. if (src2 != nullptr) {
  5095. std::cerr << "), (" << src2 << ", name=" << src2->name << ", type=" << src2->type << ", ne0=" << src2->ne[0] << ", ne1=" << src2->ne[1] << ", ne2=" << src2->ne[2] << ", ne3=" << src2->ne[3] << ", nb0=" << src2->nb[0] << ", nb1=" << src2->nb[1] << ", nb2=" << src2->nb[2] << ", nb3=" << src2->nb[3];
  5096. }
  5097. std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
  5098. std::cerr << "), " << ggml_op_name(op) << ", " << (dryrun ? "dryrun" : "") << ")");
  5099. GGML_ASSERT(op == GGML_OP_GET_ROWS || op == GGML_OP_CPY || (!ggml_is_quantized(src0->type) && (src1 == nullptr || !ggml_is_quantized(src1->type)))); // NOLINT
  5100. GGML_ASSERT(ggml_vk_op_supports_incontiguous(op) || ggml_vk_dim01_contiguous(src0)); // NOLINT
  5101. GGML_ASSERT(dst->buffer != nullptr);
  5102. const uint64_t ne00 = src0->ne[0];
  5103. const uint64_t ne01 = src0->ne[1];
  5104. const uint64_t ne02 = src0->ne[2];
  5105. const uint64_t ne03 = src0->ne[3];
  5106. const uint64_t ne0 = ne00 * ne01;
  5107. const bool use_src1 = src1 != nullptr;
  5108. const uint64_t ne10 = use_src1 ? src1->ne[0] : 0;
  5109. const uint64_t ne11 = use_src1 ? src1->ne[1] : 0;
  5110. const uint64_t ne12 = use_src1 ? src1->ne[2] : 0;
  5111. const uint64_t ne13 = use_src1 ? src1->ne[3] : 0;
  5112. const uint64_t ne1 = ne10 * ne11;
  5113. // const uint64_t nb10 = use_src1 ? src1->nb[0] : 0;
  5114. const bool use_src2 = src2 != nullptr;
  5115. const uint64_t ne20 = use_src2 ? src2->ne[0] : 0;
  5116. const uint64_t ne21 = use_src2 ? src2->ne[1] : 0;
  5117. const uint64_t ne22 = use_src2 ? src2->ne[2] : 0;
  5118. const uint64_t ne23 = use_src2 ? src2->ne[3] : 0;
  5119. const uint64_t ne2 = ne20 * ne21;
  5120. const uint64_t ned0 = dst->ne[0];
  5121. const uint64_t ned1 = dst->ne[1];
  5122. const uint64_t ned2 = dst->ne[2];
  5123. const uint64_t ned3 = dst->ne[3];
  5124. const uint64_t ned = ned0 * ned1;
  5125. init_pushconst_fastdiv(pc);
  5126. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, op);
  5127. if (pipeline == nullptr) {
  5128. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(op) << " for " << ggml_type_name(src0->type);
  5129. if (src1 != nullptr) {
  5130. std::cerr << " and " << ggml_type_name(src1->type);
  5131. }
  5132. std::cerr << " to " << ggml_type_name(dst->type) << std::endl;
  5133. GGML_ABORT("fatal error");
  5134. }
  5135. if (dryrun) {
  5136. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5137. return;
  5138. }
  5139. const bool op_supports_incontiguous = ggml_vk_op_supports_incontiguous(op);
  5140. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5141. ggml_backend_vk_buffer_context * src0_buf_ctx = (ggml_backend_vk_buffer_context *)src0->buffer->context;
  5142. ggml_backend_vk_buffer_context * src1_buf_ctx = use_src1 ? (ggml_backend_vk_buffer_context *)src1->buffer->context : nullptr;
  5143. ggml_backend_vk_buffer_context * src2_buf_ctx = use_src2 ? (ggml_backend_vk_buffer_context *)src2->buffer->context : nullptr;
  5144. vk_buffer d_X = nullptr;
  5145. size_t x_buf_offset = 0;
  5146. vk_buffer d_Y = nullptr;
  5147. size_t y_buf_offset = 0;
  5148. vk_buffer d_Z = nullptr;
  5149. size_t z_buf_offset = 0;
  5150. bool src0_uma = false;
  5151. bool src1_uma = false;
  5152. bool src2_uma = false;
  5153. if (ctx->device->uma) {
  5154. ggml_vk_host_get(ctx->device, src0->data, d_X, x_buf_offset);
  5155. src0_uma = d_X != nullptr;
  5156. if (use_src1) {
  5157. ggml_vk_host_get(ctx->device, src1->data, d_Y, y_buf_offset);
  5158. src1_uma = d_Y != nullptr;
  5159. }
  5160. if (use_src2) {
  5161. ggml_vk_host_get(ctx->device, src2->data, d_Z, z_buf_offset);
  5162. src2_uma = d_Z != nullptr;
  5163. }
  5164. }
  5165. uint64_t x_sz = ggml_type_size(src0->type)/ggml_blck_size(src0->type) * ne0;
  5166. uint64_t y_sz = use_src1 ? ggml_type_size(src1->type) * ne1 : 0;
  5167. uint64_t z_sz = use_src2 ? ggml_type_size(src2->type) * ne2 : 0;
  5168. uint64_t d_sz = ggml_type_size(dst->type) * ned;
  5169. vk_buffer d_D = dst_buf_ctx->dev_buffer;
  5170. // Workaround for tiny tensor inputs on ROPE
  5171. if (op == GGML_OP_ROPE && use_src1 && y_sz > d_D->size) {
  5172. y_sz = VK_WHOLE_SIZE;
  5173. }
  5174. GGML_ASSERT(d_D != nullptr);
  5175. uint64_t d_buf_offset = vk_tensor_offset(dst) + dst->view_offs;
  5176. if(!src0_uma) {
  5177. d_X = src0_buf_ctx->dev_buffer;
  5178. x_buf_offset = vk_tensor_offset(src0) + src0->view_offs;
  5179. GGML_ASSERT(d_X != nullptr);
  5180. }
  5181. if (use_src1 && !src1_uma) {
  5182. d_Y = src1_buf_ctx->dev_buffer;
  5183. y_buf_offset = vk_tensor_offset(src1) + src1->view_offs;
  5184. GGML_ASSERT(d_Y != nullptr);
  5185. }
  5186. if (use_src2 && !src2_uma) {
  5187. d_Z = src2_buf_ctx->dev_buffer;
  5188. z_buf_offset = vk_tensor_offset(src2) + src2->view_offs;
  5189. GGML_ASSERT(d_Z != nullptr);
  5190. }
  5191. // Compute misalignment offset for descriptors and store it in in push constants, then align the descriptor offsets.
  5192. init_pushconst_tensor_offsets(ctx, pc, src0, src1, src2, dst);
  5193. x_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5194. y_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5195. z_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5196. d_buf_offset &= ~(ctx->device->properties.limits.minStorageBufferOffsetAlignment - 1);
  5197. if (op_supports_incontiguous) {
  5198. x_sz = ggml_nbytes(src0);
  5199. y_sz = use_src1 ? ggml_nbytes(src1) : 0;
  5200. z_sz = use_src2 ? ggml_nbytes(src2) : 0;
  5201. d_sz = ggml_nbytes(dst);
  5202. if (x_buf_offset + x_sz >= d_X->size) {
  5203. x_sz = VK_WHOLE_SIZE;
  5204. }
  5205. if (use_src1 && y_buf_offset + y_sz >= d_Y->size) {
  5206. y_sz = VK_WHOLE_SIZE;
  5207. }
  5208. if (use_src2 && z_buf_offset + z_sz >= d_Z->size) {
  5209. z_sz = VK_WHOLE_SIZE;
  5210. }
  5211. if (d_buf_offset + d_sz >= d_D->size) {
  5212. d_sz = VK_WHOLE_SIZE;
  5213. }
  5214. }
  5215. std::array<uint32_t, 3> elements;
  5216. // Single call if dimension 2 is contiguous
  5217. GGML_ASSERT(op_supports_incontiguous || (ggml_is_contiguous(src0) && (src1 == nullptr || ggml_is_contiguous(src1))));
  5218. switch (op) {
  5219. case GGML_OP_NORM:
  5220. case GGML_OP_RMS_NORM:
  5221. case GGML_OP_RMS_NORM_BACK:
  5222. case GGML_OP_L2_NORM:
  5223. case GGML_OP_SOFT_MAX:
  5224. case GGML_OP_SOFT_MAX_BACK:
  5225. case GGML_OP_SUM_ROWS:
  5226. case GGML_OP_ARGMAX:
  5227. {
  5228. const uint32_t nr = ggml_nrows(src0);
  5229. if (nr > 262144) {
  5230. elements = { 512, 512, CEIL_DIV(nr, 262144) };
  5231. } else if (nr > 512) {
  5232. elements = { 512, CEIL_DIV(nr, 512), 1 };
  5233. } else {
  5234. elements = { nr, 1, 1 };
  5235. }
  5236. } break;
  5237. case GGML_OP_SUM:
  5238. // We use GGML_OP_SUM_ROWS with 1 row.
  5239. elements = { 1, 1, 1 };
  5240. break;
  5241. case GGML_OP_GROUP_NORM:
  5242. {
  5243. const uint32_t num_groups = dst->op_params[0];
  5244. elements = { num_groups * (uint32_t)src0->ne[3], 1, 1 };
  5245. } break;
  5246. case GGML_OP_DIAG_MASK_INF:
  5247. case GGML_OP_ROPE:
  5248. case GGML_OP_ROPE_BACK:
  5249. elements = { (uint32_t)ggml_nrows(src0), (uint32_t)ne00, 1 };
  5250. break;
  5251. case GGML_OP_GET_ROWS:
  5252. elements = { (uint32_t)ne00, (uint32_t)ne10, (uint32_t)(ne11 * ne12) };
  5253. break;
  5254. case GGML_OP_ARGSORT:
  5255. elements = { (uint32_t)ne00, (uint32_t)ggml_nrows(src0), 1 };
  5256. break;
  5257. case GGML_OP_IM2COL:
  5258. {
  5259. const bool is_2D = dst->op_params[6] == 1;
  5260. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5261. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5262. const uint32_t KW = src0->ne[0];
  5263. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5264. const uint32_t OW = dst->ne[1];
  5265. const uint32_t batch = src1->ne[is_2D ? 3 : 2];
  5266. elements = { OW * KW * KH, OH, batch * IC };
  5267. } break;
  5268. case GGML_OP_TIMESTEP_EMBEDDING:
  5269. {
  5270. const uint32_t dim = dst->op_params[0];
  5271. uint32_t half_ceil = (dim + 1) / 2;
  5272. elements = { half_ceil, (uint32_t)src0->ne[0], 1 };
  5273. } break;
  5274. case GGML_OP_POOL_2D:
  5275. {
  5276. const uint32_t N = dst->ne[3];
  5277. const uint32_t OC = dst->ne[2];
  5278. const uint32_t OH = dst->ne[1];
  5279. const uint32_t OW = dst->ne[0];
  5280. elements = { N * OC * OH * OW, 1, 1};
  5281. } break;
  5282. case GGML_OP_ADD:
  5283. case GGML_OP_SUB:
  5284. case GGML_OP_DIV:
  5285. case GGML_OP_MUL:
  5286. case GGML_OP_SCALE:
  5287. case GGML_OP_SQR:
  5288. case GGML_OP_SIN:
  5289. case GGML_OP_COS:
  5290. case GGML_OP_CLAMP:
  5291. case GGML_OP_PAD:
  5292. case GGML_OP_REPEAT:
  5293. case GGML_OP_REPEAT_BACK:
  5294. case GGML_OP_CPY:
  5295. case GGML_OP_CONCAT:
  5296. case GGML_OP_UPSCALE:
  5297. case GGML_OP_UNARY:
  5298. {
  5299. const uint32_t ne = ggml_nelements(dst);
  5300. if (ne > 262144) {
  5301. elements = { 512, 512, CEIL_DIV(ne, 262144) };
  5302. } else if (ne > 512) {
  5303. elements = { 512, CEIL_DIV(ne, 512), 1 };
  5304. } else {
  5305. elements = { ne, 1, 1 };
  5306. }
  5307. } break;
  5308. default:
  5309. elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
  5310. break;
  5311. }
  5312. if (!op_supports_incontiguous) {
  5313. if (x_sz != VK_WHOLE_SIZE) {
  5314. x_sz *= ne02 * ne03;
  5315. }
  5316. if (use_src1 && y_sz != VK_WHOLE_SIZE) {
  5317. y_sz *= ne12 * ne13;
  5318. }
  5319. if (use_src2 && z_sz != VK_WHOLE_SIZE) {
  5320. z_sz *= ne22 * ne23;
  5321. }
  5322. if (d_sz != VK_WHOLE_SIZE) {
  5323. d_sz *= ned2 * ned3;
  5324. }
  5325. }
  5326. if (op == GGML_OP_SOFT_MAX) {
  5327. // Empty src1 is possible in soft_max, but the shader needs a buffer
  5328. vk_subbuffer subbuf_y;
  5329. if (use_src1) {
  5330. subbuf_y = { d_Y, y_buf_offset, y_sz };
  5331. } else {
  5332. subbuf_y = { d_X, 0, x_sz };
  5333. }
  5334. ggml_vk_sync_buffers(subctx);
  5335. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, subbuf_y, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5336. } else if (op == GGML_OP_ROPE || op == GGML_OP_ROPE_BACK) {
  5337. // Empty src2 is possible in rope, but the shader needs a buffer
  5338. vk_subbuffer subbuf_z;
  5339. if (use_src2) {
  5340. subbuf_z = { d_Z, z_buf_offset, z_sz };
  5341. } else {
  5342. subbuf_z = { d_X, 0, x_sz };
  5343. }
  5344. ggml_vk_sync_buffers(subctx);
  5345. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, subbuf_z, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5346. } else if (op == GGML_OP_IM2COL) {
  5347. // im2col uses only src1 and dst buffers
  5348. ggml_vk_sync_buffers(subctx);
  5349. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5350. } else if (op == GGML_OP_COUNT_EQUAL) {
  5351. ggml_vk_sync_buffers(subctx);
  5352. // count_equal assumes that destination buffer is initialized with zeroes
  5353. ggml_vk_buffer_memset_async(subctx, d_D, d_buf_offset, 0, d_sz);
  5354. ggml_vk_sync_buffers(subctx);
  5355. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5356. } else if (use_src2) {
  5357. ggml_vk_sync_buffers(subctx);
  5358. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_Z, z_buf_offset, z_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5359. } else if (use_src1) {
  5360. ggml_vk_sync_buffers(subctx);
  5361. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_Y, y_buf_offset, y_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5362. } else {
  5363. ggml_vk_sync_buffers(subctx);
  5364. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { vk_subbuffer{ d_X, x_buf_offset, x_sz }, vk_subbuffer{ d_D, d_buf_offset, d_sz } }, sizeof(PC), &pc, elements);
  5365. }
  5366. }
  5367. static void ggml_vk_get_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5368. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5369. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5370. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5371. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_GET_ROWS, {
  5372. (uint32_t)ggml_nelements(src0),
  5373. (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,
  5374. (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,
  5375. (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,
  5376. 0,
  5377. 0.0f, 0.0f, 0,
  5378. }, dryrun);
  5379. }
  5380. static void ggml_vk_acc(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5381. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5382. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5383. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5384. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5385. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5386. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5387. int offset = dst->op_params[3] / 4; // offset in bytes
  5388. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ACC, {
  5389. (uint32_t)ggml_nelements(src0),
  5390. (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)nb1, (uint32_t)nb2, (uint32_t)src0->nb[3] / src0_type_size,
  5391. (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,
  5392. (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] / dst_type_size, (uint32_t)nb1, (uint32_t)nb2, (uint32_t) dst->nb[3] / dst_type_size,
  5393. 0,
  5394. 0.0f, 0.0f, offset,
  5395. }, dryrun);
  5396. }
  5397. static void ggml_vk_add(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5398. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5399. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5400. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5401. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_ADD, {
  5402. (uint32_t)ggml_nelements(src0),
  5403. (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,
  5404. (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,
  5405. (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,
  5406. 0,
  5407. 0.0f, 0.0f, 0,
  5408. }, dryrun);
  5409. }
  5410. static void ggml_vk_sub(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5411. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5412. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5413. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5414. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SUB, {
  5415. (uint32_t)ggml_nelements(src0),
  5416. (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,
  5417. (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,
  5418. (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,
  5419. 0,
  5420. 0.0f, 0.0f, 0,
  5421. }, dryrun);
  5422. }
  5423. static void ggml_vk_mul(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5424. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5425. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5426. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5427. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_MUL, {
  5428. (uint32_t)ggml_nelements(src0),
  5429. (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,
  5430. (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,
  5431. (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,
  5432. 0,
  5433. 0.0f, 0.0f, 0,
  5434. }, dryrun);
  5435. }
  5436. static void ggml_vk_div(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5437. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5438. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5439. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5440. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_DIV, {
  5441. (uint32_t)ggml_nelements(src0),
  5442. (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,
  5443. (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,
  5444. (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,
  5445. 0,
  5446. 0.0f, 0.0f, 0,
  5447. }, dryrun);
  5448. }
  5449. static void ggml_vk_op_f32_wkv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_rwkv_wkv6_push_constants&& pc, int version, bool dryrun = false) {
  5450. GGML_ASSERT(version == 6 || version == 7);
  5451. int num_srcs = version == 6 ? 6 : 7;
  5452. for (int i = 0; i < num_srcs; i++) {
  5453. GGML_ASSERT(!ggml_is_quantized(dst->src[i]->type));
  5454. }
  5455. GGML_ASSERT(dst->buffer != nullptr);
  5456. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op);
  5457. GGML_ASSERT(pipeline != nullptr);
  5458. if (dryrun) {
  5459. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5460. return;
  5461. }
  5462. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  5463. ggml_backend_vk_buffer_context * src_buf_ctxs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5464. for (int i = 0; i < num_srcs; i++) {
  5465. src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
  5466. }
  5467. ggml_vk_sync_buffers(subctx);
  5468. vk_buffer d_D = nullptr, d_srcs[7] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  5469. size_t dst_offset = 0, src_offsets[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5470. bool dst_uma = false, srcs_uma[7] = { false, false, false, false, false, false, false };
  5471. if (ctx->device->uma) {
  5472. for (int i = 0; i < num_srcs; i++) {
  5473. ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
  5474. srcs_uma[i] = d_srcs[i] != nullptr;
  5475. }
  5476. ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
  5477. dst_uma = d_D != nullptr;
  5478. }
  5479. uint64_t src_sizes[7] = { 0, 0, 0, 0, 0, 0, 0 };
  5480. for (int i = 0; i < num_srcs; i++) {
  5481. src_sizes[i] = ggml_nbytes(dst->src[i]);
  5482. if (!srcs_uma[i]) {
  5483. d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
  5484. src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
  5485. }
  5486. }
  5487. const uint64_t dst_size = ggml_nbytes(dst);
  5488. if (!dst_uma) {
  5489. d_D = dst_buf_ctx->dev_buffer;
  5490. dst_offset = vk_tensor_offset(dst) + dst->view_offs;
  5491. }
  5492. std::array<uint32_t, 3> elements = {
  5493. (uint32_t)(pc.B * pc.H),
  5494. 1,
  5495. 1
  5496. };
  5497. if (version == 6) {
  5498. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5499. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5500. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5501. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5502. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5503. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5504. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5505. vk_subbuffer{ d_D, dst_offset, dst_size }
  5506. }, sizeof(vk_op_rwkv_wkv6_push_constants), &pc, elements);
  5507. } else if (version == 7) {
  5508. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5509. vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
  5510. vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
  5511. vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
  5512. vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
  5513. vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
  5514. vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
  5515. vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
  5516. vk_subbuffer{ d_D, dst_offset, dst_size }
  5517. }, sizeof(vk_op_rwkv_wkv7_push_constants), &pc, elements);
  5518. } else {
  5519. // shouldn't happen
  5520. GGML_ASSERT(false);
  5521. }
  5522. }
  5523. static void ggml_vk_rwkv_wkv6(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5524. const size_t seq_length = dst->src[0]->ne[2];
  5525. const size_t n_embed = dst->ne[0];
  5526. const size_t n_heads = dst->src[0]->ne[1];
  5527. const size_t n_seqs = dst->src[5]->ne[1];
  5528. ggml_vk_op_f32_wkv(
  5529. ctx, subctx, dst,
  5530. {
  5531. (uint32_t)n_seqs,
  5532. (uint32_t)seq_length,
  5533. (uint32_t)n_embed,
  5534. (uint32_t)n_heads,
  5535. },
  5536. 6,
  5537. dryrun
  5538. );
  5539. }
  5540. static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5541. const size_t seq_length = dst->src[0]->ne[2];
  5542. const size_t n_embed = dst->ne[0];
  5543. const size_t n_heads = dst->src[0]->ne[1];
  5544. const size_t n_seqs = dst->src[6]->ne[1];
  5545. ggml_vk_op_f32_wkv(
  5546. ctx, subctx, dst,
  5547. {
  5548. (uint32_t)n_seqs,
  5549. (uint32_t)seq_length,
  5550. (uint32_t)n_embed,
  5551. (uint32_t)n_heads,
  5552. },
  5553. 7,
  5554. dryrun
  5555. );
  5556. }
  5557. static void ggml_vk_op_f32_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_push_constants&& pc, bool dryrun = false) {
  5558. const ggml_tensor * x = dst->src[0];
  5559. const ggml_tensor * g = dst->src[1];
  5560. const ggml_tensor * gm = dst->src[2];
  5561. const ggml_tensor * gv = dst->src[3];
  5562. const ggml_tensor * p = dst->src[4];
  5563. GGML_ASSERT(x->type == GGML_TYPE_F32);
  5564. GGML_ASSERT(g->type == GGML_TYPE_F32);
  5565. GGML_ASSERT(gm->type == GGML_TYPE_F32);
  5566. GGML_ASSERT(gv->type == GGML_TYPE_F32);
  5567. GGML_ASSERT(p->type == GGML_TYPE_F32);
  5568. GGML_ASSERT(dst->buffer != nullptr);
  5569. GGML_ASSERT(ggml_is_contiguous(x));
  5570. GGML_ASSERT(ggml_is_contiguous(g));
  5571. GGML_ASSERT(ggml_is_contiguous(gm));
  5572. GGML_ASSERT(ggml_is_contiguous(gv));
  5573. GGML_ASSERT(ggml_is_contiguous(p));
  5574. GGML_ASSERT(ggml_are_same_shape(x, g));
  5575. GGML_ASSERT(ggml_are_same_shape(x, gm));
  5576. GGML_ASSERT(ggml_are_same_shape(x, gv));
  5577. GGML_ASSERT(ggml_nelements(p) == 7);
  5578. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, g, gm, gv, dst, GGML_OP_OPT_STEP_ADAMW);
  5579. GGML_ASSERT(pipeline != nullptr);
  5580. if (dryrun) {
  5581. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  5582. return;
  5583. }
  5584. ggml_backend_vk_buffer_context * x_buf_ctx = (ggml_backend_vk_buffer_context *)x->buffer->context;
  5585. ggml_backend_vk_buffer_context * g_buf_ctx = (ggml_backend_vk_buffer_context *)g->buffer->context;
  5586. ggml_backend_vk_buffer_context * gm_buf_ctx = (ggml_backend_vk_buffer_context *)gm->buffer->context;
  5587. ggml_backend_vk_buffer_context * gv_buf_ctx = (ggml_backend_vk_buffer_context *)gv->buffer->context;
  5588. ggml_backend_vk_buffer_context * p_buf_ctx = (ggml_backend_vk_buffer_context *)p->buffer->context;
  5589. ggml_vk_sync_buffers(subctx);
  5590. vk_buffer d_X = nullptr, d_G = nullptr, d_GM = nullptr, d_GV = nullptr, d_P = nullptr;
  5591. size_t x_offset = 0, g_offset = 0, gm_offset = 0, gv_offset = 0, p_offset = 0;
  5592. bool X_uma = false, G_uma = false, GM_uma = false, GV_uma = false, P_uma = false;
  5593. if (ctx->device->uma) {
  5594. ggml_vk_host_get(ctx->device, x->data, d_X, x_offset);
  5595. ggml_vk_host_get(ctx->device, g->data, d_G, g_offset);
  5596. ggml_vk_host_get(ctx->device, gm->data, d_GM, gm_offset);
  5597. ggml_vk_host_get(ctx->device, gv->data, d_GV, gv_offset);
  5598. ggml_vk_host_get(ctx->device, p->data, d_P, p_offset);
  5599. X_uma = d_X != nullptr;
  5600. G_uma = d_G != nullptr;
  5601. GM_uma = d_GM != nullptr;
  5602. GV_uma = d_GV != nullptr;
  5603. P_uma = d_P != nullptr;
  5604. }
  5605. if (!X_uma) {
  5606. d_X = x_buf_ctx->dev_buffer;
  5607. x_offset = vk_tensor_offset(x) + x->view_offs;
  5608. }
  5609. if (!G_uma) {
  5610. d_G = g_buf_ctx->dev_buffer;
  5611. g_offset = vk_tensor_offset(g) + g->view_offs;
  5612. }
  5613. if (!GM_uma) {
  5614. d_GM = gm_buf_ctx->dev_buffer;
  5615. gm_offset = vk_tensor_offset(gm) + gm->view_offs;
  5616. }
  5617. if (!GV_uma) {
  5618. d_GV = gv_buf_ctx->dev_buffer;
  5619. gv_offset = vk_tensor_offset(gv) + gv->view_offs;
  5620. }
  5621. if (!P_uma) {
  5622. d_P = p_buf_ctx->dev_buffer;
  5623. p_offset = vk_tensor_offset(p) + p->view_offs;
  5624. }
  5625. const uint64_t x_size = ggml_nbytes(x);
  5626. const uint64_t g_size = ggml_nbytes(g);
  5627. const uint64_t gm_size = ggml_nbytes(gm);
  5628. const uint64_t gv_size = ggml_nbytes(gv);
  5629. const uint64_t p_size = ggml_nbytes(p);
  5630. std::array<uint32_t, 3> elements = { (uint32_t)ggml_nelements(x), 1, 1 };
  5631. ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
  5632. vk_subbuffer{ d_X, x_offset, x_size },
  5633. vk_subbuffer{ d_G, g_offset, g_size },
  5634. vk_subbuffer{ d_GM, gm_offset, gm_size },
  5635. vk_subbuffer{ d_GV, gv_offset, gv_size },
  5636. vk_subbuffer{ d_P, p_offset, p_size },
  5637. }, sizeof(vk_op_push_constants), &pc, elements);
  5638. }
  5639. static void ggml_vk_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
  5640. const size_t n = ggml_nelements(dst->src[0]);
  5641. ggml_vk_op_f32_opt_step_adamw(
  5642. ctx, subctx, dst,
  5643. { (uint32_t)n, 0, 0.0f, 0.0f },
  5644. dryrun
  5645. );
  5646. }
  5647. static void ggml_vk_concat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5648. int * op_params = (int *)dst->op_params;
  5649. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5650. const uint32_t src1_type_size = ggml_type_size(src1->type);
  5651. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5652. ggml_vk_op_f32<vk_op_binary_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_CONCAT, {
  5653. (uint32_t)ggml_nelements(dst),
  5654. (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,
  5655. (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,
  5656. (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,
  5657. 0,
  5658. 0.0f, 0.0f, op_params[0],
  5659. }, dryrun);
  5660. }
  5661. static void ggml_vk_upscale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5662. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5663. const float sf0 = (float)dst->ne[0] / src0->ne[0];
  5664. const float sf1 = (float)dst->ne[1] / src0->ne[1];
  5665. const float sf2 = (float)dst->ne[2] / src0->ne[2];
  5666. const float sf3 = (float)dst->ne[3] / src0->ne[3];
  5667. ggml_vk_op_f32<vk_op_upscale_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UPSCALE, {
  5668. (uint32_t)ggml_nelements(dst), 0, 0,
  5669. (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,
  5670. (uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2],(uint32_t)dst->ne[3],
  5671. sf0, sf1, sf2, sf3,
  5672. }, dryrun);
  5673. }
  5674. static void ggml_vk_scale(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5675. float * op_params = (float *)dst->op_params;
  5676. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5677. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5678. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SCALE, {
  5679. (uint32_t)ggml_nelements(src0),
  5680. (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,
  5681. (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,
  5682. 0,
  5683. op_params[0], 0.0f,
  5684. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5685. }, dryrun);
  5686. }
  5687. static void ggml_vk_sqr(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5688. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5689. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5690. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SQR, {
  5691. (uint32_t)ggml_nelements(src0),
  5692. (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,
  5693. (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,
  5694. 0,
  5695. 0.0f, 0.0f,
  5696. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5697. }, dryrun);
  5698. }
  5699. static void ggml_vk_sin(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5700. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5701. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5702. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SIN, {
  5703. (uint32_t)ggml_nelements(src0),
  5704. (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,
  5705. (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,
  5706. 0,
  5707. 0.0f, 0.0f,
  5708. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5709. }, dryrun);
  5710. }
  5711. static void ggml_vk_cos(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5712. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5713. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5714. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_COS, {
  5715. (uint32_t)ggml_nelements(src0),
  5716. (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,
  5717. (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,
  5718. 0,
  5719. 0.0f, 0.0f,
  5720. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5721. }, dryrun);
  5722. }
  5723. static void ggml_vk_clamp(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5724. float * op_params = (float *)dst->op_params;
  5725. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5726. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5727. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CLAMP, {
  5728. (uint32_t)ggml_nelements(src0),
  5729. (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,
  5730. (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,
  5731. 0,
  5732. op_params[0], op_params[1],
  5733. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5734. }, dryrun);
  5735. }
  5736. static void ggml_vk_pad(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5737. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5738. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5739. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_PAD, {
  5740. (uint32_t)ggml_nelements(dst),
  5741. (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,
  5742. (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,
  5743. 0,
  5744. 0.0f, 0.0f,
  5745. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5746. }, dryrun);
  5747. }
  5748. static void ggml_vk_repeat(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5749. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5750. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5751. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT, {
  5752. (uint32_t)ggml_nelements(dst),
  5753. (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,
  5754. (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,
  5755. 0,
  5756. 0.0f, 0.0f,
  5757. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5758. }, dryrun);
  5759. }
  5760. static void ggml_vk_repeat_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5761. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5762. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5763. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_REPEAT_BACK, {
  5764. (uint32_t)ggml_nelements(dst),
  5765. (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,
  5766. (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,
  5767. 0,
  5768. 0.0f, 0.0f,
  5769. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5770. }, dryrun);
  5771. }
  5772. static void ggml_vk_cpy(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5773. const uint32_t src0_type_size = ggml_type_size(src0->type);
  5774. const uint32_t dst_type_size = ggml_type_size(dst->type);
  5775. ggml_vk_op_f32<vk_op_unary_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_CPY, {
  5776. (uint32_t)ggml_nelements(src0),
  5777. (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,
  5778. (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,
  5779. 0,
  5780. 0.0f, 0.0f,
  5781. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5782. }, dryrun);
  5783. }
  5784. static void ggml_vk_silu_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5785. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SILU_BACK, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5786. }
  5787. static void ggml_vk_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5788. float * op_params = (float *)dst->op_params;
  5789. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5790. }
  5791. static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5792. const int * int_op_params = (const int *)dst->op_params;
  5793. const float * float_op_params = (const float *)dst->op_params;
  5794. const uint32_t num_groups = int_op_params[0];
  5795. const float eps = float_op_params[1];
  5796. const uint32_t group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5797. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_GROUP_NORM, { group_size, 0, eps, 0.0f }, dryrun);
  5798. }
  5799. static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5800. float * op_params = (float *)dst->op_params;
  5801. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_RMS_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5802. }
  5803. static void ggml_vk_rms_norm_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5804. float * op_params = (float *)dst->op_params;
  5805. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_RMS_NORM_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5806. }
  5807. static void ggml_vk_l2_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5808. float * op_params = (float *)dst->op_params;
  5809. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_L2_NORM, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], 0.0f }, dryrun);
  5810. }
  5811. static void ggml_vk_unary(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5812. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_UNARY, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5813. }
  5814. static void ggml_vk_diag_mask_inf(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5815. int32_t * op_params = (int32_t *)dst->op_params;
  5816. ggml_vk_op_f32<vk_op_diag_mask_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_DIAG_MASK_INF, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0] }, dryrun);
  5817. }
  5818. static void ggml_vk_soft_max(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5819. float * op_params = (float *)dst->op_params;
  5820. float scale = op_params[0];
  5821. float max_bias = op_params[1];
  5822. const uint32_t ncols = (uint32_t)src0->ne[0];
  5823. const uint32_t nrows_x = (uint32_t)ggml_nrows(src0);
  5824. const uint32_t nrows_y = (uint32_t)src0->ne[1];
  5825. const uint32_t n_head_kv = nrows_x/nrows_y;
  5826. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  5827. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  5828. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  5829. ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
  5830. ncols,
  5831. src1 != nullptr ? nrows_y : (uint32_t)0,
  5832. scale, max_bias,
  5833. m0, m1,
  5834. n_head_log2,
  5835. nrows_x,
  5836. }, dryrun);
  5837. }
  5838. static void ggml_vk_soft_max_back(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5839. float * op_params = (float *)dst->op_params;
  5840. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX_BACK, { (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], op_params[0], op_params[1] }, dryrun);
  5841. }
  5842. static void ggml_vk_rope(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * src2, ggml_tensor * dst, bool backprop, bool dryrun = false) {
  5843. const int n_dims = ((int32_t *) dst->op_params)[1];
  5844. const int mode = ((int32_t *) dst->op_params)[2];
  5845. // const int n_ctx = ((int32_t *) dst->op_params)[3];
  5846. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  5847. const float freq_base = ((float *) dst->op_params)[5];
  5848. const float freq_scale = ((float *) dst->op_params)[6];
  5849. const float ext_factor = ((float *) dst->op_params)[7];
  5850. const float attn_factor = ((float *) dst->op_params)[8];
  5851. const float beta_fast = ((float *) dst->op_params)[9];
  5852. const float beta_slow = ((float *) dst->op_params)[10];
  5853. int sections[4] {};
  5854. if (mode & GGML_ROPE_TYPE_MROPE) {
  5855. memcpy(sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  5856. }
  5857. float corr_dims[2];
  5858. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  5859. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5860. uint32_t s1 = src0->nb[1] / ggml_type_size(src0->type);
  5861. uint32_t s2 = src0->nb[2] / ggml_type_size(src0->type);
  5862. ggml_vk_op_f32<vk_op_rope_push_constants>(ctx, subctx, src0, src1, src2, dst, GGML_OP_ROPE, {
  5863. (uint32_t)src0->ne[0], (uint32_t)n_dims, freq_scale, (uint32_t)src0->ne[1],
  5864. freq_base, ext_factor, attn_factor, {corr_dims[0], corr_dims[1]}, theta_scale,
  5865. src2 != nullptr, (uint32_t)src0->ne[2], s1, s2,
  5866. sections[0], sections[1], sections[2], sections[3], backprop
  5867. }, dryrun);
  5868. }
  5869. static void ggml_vk_argsort(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5870. int32_t * op_params = (int32_t *)dst->op_params;
  5871. uint32_t ncols = src0->ne[0];
  5872. uint32_t ncols_pad = 1;
  5873. while (ncols_pad < ncols) {
  5874. ncols_pad *= 2;
  5875. }
  5876. GGML_ASSERT(ncols_pad <= 1024);
  5877. ggml_vk_op_f32<vk_op_argsort_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGSORT, {
  5878. ncols,
  5879. ncols_pad,
  5880. op_params[0],
  5881. }, dryrun);
  5882. }
  5883. static void ggml_vk_sum(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5884. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5885. }
  5886. static void ggml_vk_sum_rows(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5887. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_SUM_ROWS, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  5888. }
  5889. static void ggml_vk_argmax(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5890. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_ARGMAX, { (uint32_t)src0->ne[0], 0, 0.0f, 0.0f }, dryrun);
  5891. }
  5892. static void ggml_vk_count_equal(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5893. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_COUNT_EQUAL, { (uint32_t)ggml_nelements(src0), 0, 0.0f, 0.0f }, dryrun);
  5894. }
  5895. static void ggml_vk_im2col(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, bool dryrun = false) {
  5896. const int32_t s0 = dst->op_params[0];
  5897. const int32_t s1 = dst->op_params[1];
  5898. const int32_t p0 = dst->op_params[2];
  5899. const int32_t p1 = dst->op_params[3];
  5900. const int32_t d0 = dst->op_params[4];
  5901. const int32_t d1 = dst->op_params[5];
  5902. const bool is_2D = dst->op_params[6] == 1;
  5903. const uint32_t IC = src1->ne[is_2D ? 2 : 1];
  5904. const uint32_t IH = is_2D ? src1->ne[1] : 1;
  5905. const uint32_t IW = src1->ne[0];
  5906. const uint32_t KH = is_2D ? src0->ne[1] : 1;
  5907. const uint32_t KW = src0->ne[0];
  5908. const uint32_t OH = is_2D ? dst->ne[2] : 1;
  5909. const uint32_t OW = dst->ne[1];
  5910. const uint32_t offset_delta = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  5911. const uint32_t batch_offset = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
  5912. const uint32_t pelements = OW * KW * KH;
  5913. ggml_vk_op_f32<vk_op_im2col_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_IM2COL, {
  5914. batch_offset, offset_delta,
  5915. IC, IW, IH, OW, OH, KW, KH,
  5916. pelements,
  5917. IC * KH * KW,
  5918. s0, s1, p0, p1, d0, d1,
  5919. }, dryrun);
  5920. }
  5921. static void ggml_vk_timestep_embedding(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5922. const uint32_t dim = dst->op_params[0];
  5923. const uint32_t max_period = dst->op_params[1];
  5924. const uint32_t nb1 = dst->nb[1] / ggml_type_size(dst->type);
  5925. ggml_vk_op_f32<vk_op_timestep_embedding_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_TIMESTEP_EMBEDDING, {
  5926. nb1, dim, max_period,
  5927. }, dryrun);
  5928. }
  5929. static void ggml_vk_pool_2d(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5930. uint32_t op = static_cast<uint32_t>(dst->op_params[0]);
  5931. const int32_t k1 = dst->op_params[1];
  5932. const int32_t k0 = dst->op_params[2];
  5933. const int32_t s1 = dst->op_params[3];
  5934. const int32_t s0 = dst->op_params[4];
  5935. const int32_t p1 = dst->op_params[5];
  5936. const int32_t p0 = dst->op_params[6];
  5937. const uint32_t IH = src0->ne[1];
  5938. const uint32_t IW = src0->ne[0];
  5939. const uint32_t N = dst->ne[3];
  5940. const uint32_t OC = dst->ne[2];
  5941. const uint32_t OH = dst->ne[1];
  5942. const uint32_t OW = dst->ne[0];
  5943. const uint32_t parallel_elements = N * OC * OH * OW;
  5944. ggml_vk_op_f32<vk_op_pool2d_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_POOL_2D, {
  5945. IW, IH, OW, OH, OC,
  5946. parallel_elements,
  5947. op,
  5948. k0, k1, s0, s1, p0, p1,
  5949. }, dryrun);
  5950. }
  5951. static void ggml_vk_leaky_relu(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
  5952. const float * op_params = (const float *)dst->op_params;
  5953. ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_LEAKY_RELU, { (uint32_t)ggml_nelements(src0), 0, op_params[0], 0.0f }, dryrun);
  5954. }
  5955. #ifdef GGML_VULKAN_RUN_TESTS
  5956. static void ggml_vk_print_matrix_area(const void * data, ggml_type type, int ne0, int ne1, int i0, int i1, int i2) {
  5957. if (type != GGML_TYPE_F32 && type != GGML_TYPE_F16) {
  5958. return;
  5959. }
  5960. i0 = std::max(i0, 5);
  5961. i1 = std::max(i1, 5);
  5962. i2 = std::max(i2, 0);
  5963. fprintf(stderr, " ");
  5964. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5965. fprintf(stderr, "%7d ", idx1);
  5966. }
  5967. fprintf(stderr, "\n");
  5968. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  5969. fprintf(stderr, "%7d: ", idx0);
  5970. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  5971. if (idx0 >= 0 && idx0 < ne0 && idx1 >= 0 && idx1 < ne1) {
  5972. float val;
  5973. if (type == GGML_TYPE_F32) {
  5974. val = *((const float *) data + i2*ne1*ne0 + idx1*ne0 + idx0);
  5975. } else if (type == GGML_TYPE_F16) {
  5976. val = ggml_fp16_to_fp32(*((const ggml_fp16_t *) data + i2*ne1*ne0 + idx1*ne0 + idx0));
  5977. } else {
  5978. GGML_ABORT("fatal error");
  5979. }
  5980. fprintf(stderr, "% 7.2f ", val);
  5981. } else {
  5982. fprintf(stderr, " ");
  5983. }
  5984. }
  5985. fprintf(stderr, "\n");
  5986. }
  5987. }
  5988. template <typename X_TYPE, typename Y_TYPE>
  5989. 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) {
  5990. VK_LOG_DEBUG("ggml_vk_test_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << shader_size << ")");
  5991. const size_t x_ne = m * k * batch;
  5992. const size_t y_ne = k * n * batch;
  5993. const size_t d_ne = m * n * batch;
  5994. vk_pipeline p;
  5995. std::string shname;
  5996. if (shader_size == 0) {
  5997. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  5998. p = ctx->device->pipeline_matmul_f32->a_s;
  5999. shname = "F32_ALIGNED_S";
  6000. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6001. p = ctx->device->pipeline_matmul_f32_f16->a_s;
  6002. shname = "F32_F16_ALIGNED_S";
  6003. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6004. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_s;
  6005. shname = "F16_F32_ALIGNED_S";
  6006. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6007. p = ctx->device->pipeline_matmul_f16.f32acc->a_s;
  6008. shname = "F16_ALIGNED_S";
  6009. } else {
  6010. GGML_ABORT("fatal error");
  6011. }
  6012. } else if (shader_size == 1) {
  6013. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6014. p = ctx->device->pipeline_matmul_f32->a_m;
  6015. shname = "F32_ALIGNED_M";
  6016. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6017. p = ctx->device->pipeline_matmul_f32_f16->a_m;
  6018. shname = "F32_F16_ALIGNED_M";
  6019. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6020. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_m;
  6021. shname = "F16_F32_ALIGNED_M";
  6022. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6023. p = ctx->device->pipeline_matmul_f16.f32acc->a_m;
  6024. shname = "F16_ALIGNED_M";
  6025. } else {
  6026. GGML_ABORT("fatal error");
  6027. }
  6028. } else if (shader_size == 2) {
  6029. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6030. p = ctx->device->pipeline_matmul_f32->a_l;
  6031. shname = "F32_ALIGNED_L";
  6032. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6033. p = ctx->device->pipeline_matmul_f32_f16->a_l;
  6034. shname = "F32_F16_ALIGNED_L";
  6035. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6036. p = ctx->device->pipeline_matmul_f16_f32.f32acc->a_l;
  6037. shname = "F16_F32_ALIGNED_L";
  6038. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6039. p = ctx->device->pipeline_matmul_f16.f32acc->a_l;
  6040. shname = "F16_ALIGNED_L";
  6041. } else {
  6042. GGML_ABORT("fatal error");
  6043. }
  6044. } else {
  6045. GGML_ASSERT(0);
  6046. }
  6047. const size_t kpad = ggml_vk_align_size(k, p->align);
  6048. if (k != kpad) {
  6049. if (shader_size == 0) {
  6050. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6051. p = ctx->device->pipeline_matmul_f32->s;
  6052. shname = "F32_S";
  6053. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6054. p = ctx->device->pipeline_matmul_f32_f16->s;
  6055. shname = "F32_F16_S";
  6056. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6057. p = ctx->device->pipeline_matmul_f16_f32.f32acc->s;
  6058. shname = "F16_F32_S";
  6059. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6060. p = ctx->device->pipeline_matmul_f16.f32acc->s;
  6061. shname = "F16_S";
  6062. }
  6063. } else if (shader_size == 1) {
  6064. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6065. p = ctx->device->pipeline_matmul_f32->m;
  6066. shname = "F32_M";
  6067. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6068. p = ctx->device->pipeline_matmul_f32_f16->m;
  6069. shname = "F32_F16_M";
  6070. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6071. p = ctx->device->pipeline_matmul_f16_f32.f32acc->m;
  6072. shname = "F16_F32_M";
  6073. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6074. p = ctx->device->pipeline_matmul_f16.f32acc->m;
  6075. shname = "F16_M";
  6076. }
  6077. } else if (shader_size == 2) {
  6078. if (std::is_same<float, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6079. p = ctx->device->pipeline_matmul_f32->l;
  6080. shname = "F32_L";
  6081. } else if (std::is_same<float, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6082. p = ctx->device->pipeline_matmul_f32_f16->l;
  6083. shname = "F32_F16_L";
  6084. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<float, Y_TYPE>()) {
  6085. p = ctx->device->pipeline_matmul_f16_f32.f32acc->l;
  6086. shname = "F16_F32_L";
  6087. } else if (std::is_same<ggml_fp16_t, X_TYPE>() && std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6088. p = ctx->device->pipeline_matmul_f16.f32acc->l;
  6089. shname = "F16_L";
  6090. }
  6091. }
  6092. }
  6093. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  6094. if (split_k > 1) {
  6095. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6096. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6097. // Resize buffer
  6098. if (ctx->prealloc_split_k != nullptr) {
  6099. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6100. }
  6101. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6102. }
  6103. }
  6104. if (ctx->device->need_compiles) {
  6105. ggml_vk_load_shaders(ctx->device);
  6106. }
  6107. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6108. vk_buffer d_X = ggml_vk_create_buffer_check(ctx->device, sizeof(X_TYPE) * x_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6109. vk_buffer d_Y = ggml_vk_create_buffer_check(ctx->device, sizeof(Y_TYPE) * y_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6110. vk_buffer d_D = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6111. X_TYPE* x = (X_TYPE *) malloc(sizeof(X_TYPE) * x_ne);
  6112. Y_TYPE* y = (Y_TYPE *) malloc(sizeof(Y_TYPE) * y_ne);
  6113. float* d = (float *) malloc(sizeof(float) * d_ne);
  6114. for (size_t i = 0; i < x_ne; i++) {
  6115. if (std::is_same<float, X_TYPE>()) {
  6116. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6117. // x[i] = 1.0f;
  6118. // x[i] = i + 1;
  6119. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6120. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  6121. x[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  6122. // x[i] = ggml_fp32_to_fp16(1.0f);
  6123. // x[i] = ggml_fp32_to_fp16(i + 1);
  6124. // x[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  6125. } else {
  6126. GGML_ABORT("fatal error");
  6127. }
  6128. }
  6129. for (size_t i = 0; i < y_ne; i++) {
  6130. if (std::is_same<float, Y_TYPE>()) {
  6131. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6132. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6133. // y[i] = i + 1;
  6134. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6135. y[i] = ggml_fp32_to_fp16((rand() / (float)RAND_MAX) * 2.0f - 1.0f);
  6136. // y[i] = ggml_fp32_to_fp16((i % k == i / k) ? 1.0f : 0.0f);
  6137. // y[i] = ggml_fp32_to_fp16(i + 1);
  6138. } else {
  6139. GGML_ABORT("fatal error");
  6140. }
  6141. }
  6142. ggml_vk_buffer_write(d_X, 0, x, sizeof(X_TYPE) * k * m * batch);
  6143. ggml_vk_buffer_write(d_Y, 0, y, sizeof(Y_TYPE) * k * n * batch);
  6144. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6145. ggml_vk_ctx_begin(ctx->device, subctx);
  6146. for (size_t i = 0; i < num_it; i++) {
  6147. ggml_vk_matmul(
  6148. 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),
  6149. m, n, k,
  6150. k, k, m, k*m, k*n, m*n,
  6151. split_k, batch, batch, batch, 1, 1, n
  6152. );
  6153. }
  6154. ggml_vk_ctx_end(subctx);
  6155. auto begin = std::chrono::high_resolution_clock::now();
  6156. ggml_vk_submit(subctx, ctx->fence);
  6157. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_matmul waitForFences");
  6158. ctx->device->device.resetFences({ ctx->fence });
  6159. auto end = std::chrono::high_resolution_clock::now();
  6160. double time = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6161. // copy dst to host
  6162. ggml_vk_buffer_read(d_D, 0, d, sizeof(float) * d_ne);
  6163. float * d_chk = (float *) malloc(sizeof(float) * d_ne);
  6164. ggml_init_params iparams = {
  6165. /*.mem_size =*/ 1024*1024*1024,
  6166. /*.mem_buffer =*/ NULL,
  6167. /*.no_alloc =*/ true,
  6168. };
  6169. ggml_context * ggml_ctx = ggml_init(iparams);
  6170. ggml_type src0_type;
  6171. ggml_type src1_type;
  6172. if (std::is_same<float, X_TYPE>()) {
  6173. src0_type = GGML_TYPE_F32;
  6174. } else if (std::is_same<ggml_fp16_t, X_TYPE>()) {
  6175. src0_type = GGML_TYPE_F16;
  6176. } else {
  6177. GGML_ABORT("fatal error");
  6178. }
  6179. if (std::is_same<float, Y_TYPE>()) {
  6180. src1_type = GGML_TYPE_F32;
  6181. } else if (std::is_same<ggml_fp16_t, Y_TYPE>()) {
  6182. src1_type = GGML_TYPE_F16;
  6183. } else {
  6184. GGML_ABORT("fatal error");
  6185. }
  6186. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, src0_type, k, m, batch);
  6187. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, src1_type, k, n, batch);
  6188. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6189. src0_ggml->data = x;
  6190. src1_ggml->data = y;
  6191. tensor_ggml->data = d_chk;
  6192. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6193. ggml_build_forward_expand(cgraph, tensor_ggml);
  6194. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6195. ggml_free(ggml_ctx);
  6196. double avg_err = 0.0;
  6197. int first_err_n = -1;
  6198. int first_err_m = -1;
  6199. int first_err_b = -1;
  6200. for (size_t i = 0; i < m*n*batch; i++) {
  6201. double err = std::fabs(d[i] - d_chk[i]);
  6202. avg_err += err;
  6203. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6204. first_err_b = i / (m * n);
  6205. first_err_n = (i % (m * n)) / m;
  6206. first_err_m = (i % (m * n)) % m;
  6207. }
  6208. }
  6209. avg_err /= m * n;
  6210. double tflops = 2.0*m*n*k*batch*num_it / (time / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6211. std::cerr << "TEST " << shname << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  6212. if (avg_err > 0.1 || std::isnan(avg_err)) {
  6213. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6214. std::cerr << "Actual result: " << std::endl << std::endl;
  6215. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6216. std::cerr << "Expected result: " << std::endl << std::endl;
  6217. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6218. if (split_k > 1) {
  6219. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6220. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6221. std::cerr << "d_buf0: " << std::endl << std::endl;
  6222. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6223. std::cerr << "d_buf1: " << std::endl << std::endl;
  6224. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6225. std::cerr << "d_buf2: " << std::endl << std::endl;
  6226. 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);
  6227. std::cerr << "d_buf3: " << std::endl << std::endl;
  6228. 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);
  6229. free(split_k_buf);
  6230. }
  6231. }
  6232. free(d_chk);
  6233. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  6234. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  6235. ggml_vk_destroy_buffer(d_X);
  6236. ggml_vk_destroy_buffer(d_Y);
  6237. ggml_vk_destroy_buffer(d_D);
  6238. ggml_pipeline_cleanup(p);
  6239. ggml_pipeline_cleanup(ctx->device->pipeline_matmul_split_k_reduce);
  6240. free(x);
  6241. free(y);
  6242. free(d);
  6243. }
  6244. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  6245. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16) {
  6246. return;
  6247. }
  6248. i0 = std::max(i0, 5);
  6249. i1 = std::max(i1, 5);
  6250. i2 = std::max(i2, 0);
  6251. i3 = std::max(i3, 0);
  6252. fprintf(stderr, " ");
  6253. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6254. fprintf(stderr, "%7d ", idx1);
  6255. }
  6256. fprintf(stderr, "\n");
  6257. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  6258. fprintf(stderr, "%7d: ", idx0);
  6259. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  6260. 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]) {
  6261. float val;
  6262. if (tensor->type == GGML_TYPE_F32) {
  6263. val = *(float *) ((char *) tensor->data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  6264. } else if (tensor->type == GGML_TYPE_F16) {
  6265. 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]));
  6266. } else {
  6267. GGML_ABORT("fatal error");
  6268. }
  6269. fprintf(stderr, "% 7.2f ", val);
  6270. } else {
  6271. fprintf(stderr, " ");
  6272. }
  6273. }
  6274. fprintf(stderr, "\n");
  6275. }
  6276. }
  6277. static void ggml_vk_quantize_data(const float * from, void * to, size_t ne, ggml_type quant) {
  6278. ggml_quantize_chunk(quant, from, to, 0, 1, ne, nullptr);
  6279. }
  6280. static void ggml_vk_dequantize_data(const void * from, float * to, size_t ne, ggml_type quant) {
  6281. if (quant == GGML_TYPE_F32) {
  6282. memcpy(to, from, sizeof(float) * ne);
  6283. return;
  6284. }
  6285. const auto * tt = ggml_get_type_traits(quant);
  6286. ggml_to_float_t dequant_fn = tt->to_float;
  6287. dequant_fn(from, to, ne);
  6288. }
  6289. static void ggml_vk_test_dequant(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  6290. VK_LOG_DEBUG("ggml_vk_test_dequant(" << ne << ")");
  6291. const size_t x_sz = sizeof(float) * ne;
  6292. const size_t x_sz_f16 = sizeof(ggml_fp16_t) * ne;
  6293. const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6294. float * x = (float *) malloc(x_sz);
  6295. void * qx = malloc(qx_sz);
  6296. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6297. vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz_f16, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6298. float * x_ref = (float *) malloc(x_sz);
  6299. ggml_fp16_t * x_chk = (ggml_fp16_t *) malloc(x_sz_f16);
  6300. for (size_t i = 0; i < ne; i++) {
  6301. x[i] = rand() / (float)RAND_MAX;
  6302. }
  6303. vk_pipeline p = ggml_vk_get_to_fp16(ctx, quant);
  6304. ggml_vk_quantize_data(x, qx, ne, quant);
  6305. ggml_vk_dequantize_data(qx, x_ref, ne, quant);
  6306. ggml_pipeline_request_descriptor_sets(ctx->device, p, 1);
  6307. if (ctx->device->need_compiles) {
  6308. ggml_vk_load_shaders(ctx->device);
  6309. }
  6310. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6311. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6312. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6313. ggml_vk_ctx_begin(ctx->device, subctx);
  6314. const std::vector<uint32_t> pc = { 1, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne, (uint32_t)ne };
  6315. ggml_vk_dispatch_pipeline(ctx, subctx, p, { vk_subbuffer{ qx_buf, 0, qx_sz }, vk_subbuffer{ x_buf, 0, x_sz_f16 } }, pc.size() * sizeof(int), pc.data(), { (uint32_t)ne, 1, 1});
  6316. ggml_vk_ctx_end(subctx);
  6317. auto begin = std::chrono::high_resolution_clock::now();
  6318. ggml_vk_submit(subctx, ctx->fence);
  6319. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6320. ctx->device->device.resetFences({ ctx->fence });
  6321. auto end = std::chrono::high_resolution_clock::now();
  6322. double ms_dequant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6323. ggml_vk_buffer_read(x_buf, 0, x_chk, x_sz_f16);
  6324. int first_err = -1;
  6325. double avg_err = 0.0;
  6326. for (size_t i = 0; i < ne; i++) {
  6327. double error = std::fabs(x_ref[i] - ggml_fp16_to_fp32(x_chk[i]));
  6328. avg_err += error;
  6329. if (first_err < 0 && error > 0.05) {
  6330. first_err = i;
  6331. }
  6332. }
  6333. avg_err /= ne;
  6334. std::cerr << "TEST DEQUANT " << ggml_type_name(quant) << " time=" << ms_dequant << "ms avg_err=" << avg_err << std::endl;
  6335. if (avg_err > 0.1) {
  6336. std::cerr << "first_error = " << first_err << std::endl;
  6337. std::cerr << "Actual result: " << std::endl << std::endl;
  6338. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6339. std::cerr << ggml_fp16_to_fp32(x_chk[i]) << ", ";
  6340. }
  6341. std::cerr << std::endl << "Expected result: " << std::endl << std::endl;
  6342. for (int i = std::max(0, first_err - 5); i < std::min((int)ne, first_err + 5); i++) {
  6343. std::cerr << x_ref[i] << ", ";
  6344. }
  6345. std::cerr << std::endl;
  6346. }
  6347. ggml_vk_destroy_buffer(x_buf);
  6348. ggml_vk_destroy_buffer(qx_buf);
  6349. free(x);
  6350. free(qx);
  6351. free(x_ref);
  6352. free(x_chk);
  6353. }
  6354. // This does not work without ggml q8_1 quantization support
  6355. //
  6356. // typedef uint16_t ggml_half;
  6357. // typedef uint32_t ggml_half2;
  6358. //
  6359. // #define QK8_1 32
  6360. // typedef struct {
  6361. // union {
  6362. // struct {
  6363. // ggml_half d; // delta
  6364. // ggml_half s; // d * sum(qs[i])
  6365. // } GGML_COMMON_AGGR_S;
  6366. // ggml_half2 ds;
  6367. // } GGML_COMMON_AGGR_U;
  6368. // int8_t qs[QK8_1]; // quants
  6369. // } block_q8_1;
  6370. //
  6371. // static void ggml_vk_test_quantize(ggml_backend_vk_context * ctx, size_t ne, ggml_type quant) {
  6372. // VK_LOG_DEBUG("ggml_vk_test_quantize(" << ne << ")");
  6373. // GGML_ASSERT(quant == GGML_TYPE_Q8_1);
  6374. //
  6375. // const size_t x_sz = sizeof(float) * ne;
  6376. // const size_t qx_sz = ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6377. // float * x = (float *) malloc(x_sz);
  6378. // block_q8_1 * qx = (block_q8_1 *)malloc(qx_sz);
  6379. // block_q8_1 * qx_res = (block_q8_1 *)malloc(qx_sz);
  6380. // vk_buffer x_buf = ggml_vk_create_buffer_check(ctx->device, x_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6381. // vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6382. //
  6383. // for (size_t i = 0; i < ne; i++) {
  6384. // x[i] = rand() / (float)RAND_MAX;
  6385. // }
  6386. //
  6387. // vk_pipeline p = ggml_vk_get_quantize_pipeline(ctx, quant);
  6388. //
  6389. // ggml_pipeline_request_descriptor_sets(ctx->device, p, 1);
  6390. //
  6391. // if (ctx->device->need_compiles) {
  6392. // ggml_vk_load_shaders(ctx->device);
  6393. // }
  6394. //
  6395. // ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6396. //
  6397. // ggml_vk_buffer_write(x_buf, 0, x, x_sz);
  6398. //
  6399. // vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6400. // ggml_vk_ctx_begin(ctx->device, subctx);
  6401. // ggml_vk_quantize_q8_1(ctx, subctx, ggml_vk_subbuffer(x_buf), ggml_vk_subbuffer(qx_buf), ne);
  6402. // ggml_vk_ctx_end(subctx);
  6403. //
  6404. // auto begin = std::chrono::high_resolution_clock::now();
  6405. //
  6406. // ggml_vk_submit(subctx, ctx->fence);
  6407. // VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_quantize waitForFences");
  6408. // ctx->device->device.resetFences({ ctx->fence });
  6409. //
  6410. // auto end = std::chrono::high_resolution_clock::now();
  6411. //
  6412. // double ms_quant = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6413. // ggml_vk_buffer_read(qx_buf, 0, qx, qx_sz);
  6414. //
  6415. // ggml_vk_quantize_data(x, qx_res, ne, quant);
  6416. //
  6417. // int first_err = -1;
  6418. //
  6419. // for (size_t i = 0; i < ne / 32; i++) {
  6420. // double error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d));
  6421. //
  6422. // if (first_err < 0 && error > 0.1) {
  6423. // first_err = i;
  6424. // }
  6425. //
  6426. // error = std::fabs(ggml_fp16_to_fp32(qx_res[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) - ggml_fp16_to_fp32(qx[i].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s));
  6427. //
  6428. // if (first_err < 0 && error > 0.1) {
  6429. // first_err = i;
  6430. // }
  6431. //
  6432. // for (size_t j = 0; j < 32; j++) {
  6433. // uint64_t error = std::abs(qx_res[i].qs[j] - qx[i].qs[j]);
  6434. //
  6435. // if (first_err < 0 && error > 1) {
  6436. // first_err = i;
  6437. // }
  6438. // }
  6439. // }
  6440. //
  6441. // std::cerr << "TEST QUANTIZE " << ggml_type_name(quant) << " time=" << ms_quant << "ms " << (first_err == -1 ? "CORRECT" : "INCORRECT") << std::endl;
  6442. //
  6443. // if (first_err != -1) {
  6444. // std::cerr << "first_error = " << first_err << std::endl;
  6445. // std::cerr << "Actual result: " << std::endl << std::endl;
  6446. // std::cout << "d=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  6447. // for (size_t j = 0; j < 32; j++) {
  6448. // std::cout << " qs" << j << "=" << (uint32_t)qx[first_err].qs[j] << " ";
  6449. // }
  6450. // std::cerr << std::endl << std::endl << "Expected result: " << std::endl << std::endl;
  6451. // std::cout << "d=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.d) << " s=" << ggml_fp16_to_fp32(qx_res[first_err].GGML_COMMON_AGGR_U.GGML_COMMON_AGGR_S.s) << " ";
  6452. // for (size_t j = 0; j < 32; j++) {
  6453. // std::cout << " qs" << j << "=" << (uint32_t)qx_res[first_err].qs[j] << " ";
  6454. // }
  6455. // std::cerr << std::endl;
  6456. // }
  6457. //
  6458. // ggml_vk_destroy_buffer(x_buf);
  6459. // ggml_vk_destroy_buffer(qx_buf);
  6460. //
  6461. // free(x);
  6462. // free(qx);
  6463. // free(qx_res);
  6464. // }
  6465. static void ggml_vk_test_dequant_matmul(ggml_backend_vk_context * ctx, size_t m, size_t n, size_t k, size_t batch, size_t num_it, size_t split_k, size_t shader_size, ggml_type quant, bool mmq = false) {
  6466. VK_LOG_DEBUG("ggml_vk_test_dequant_matmul(" << m << ", " << n << ", " << k << ", " << batch << ", " << num_it << ", " << split_k << ", " << ggml_type_name(quant) << ")");
  6467. const size_t x_ne = m * k * batch;
  6468. const size_t y_ne = k * n * batch;
  6469. const size_t d_ne = m * n * batch;
  6470. vk_matmul_pipeline2 * pipelines;
  6471. if (mmq) {
  6472. pipelines = ctx->device->pipeline_dequant_mul_mat_mat_q8_1;
  6473. } else {
  6474. pipelines = ctx->device->pipeline_dequant_mul_mat_mat;
  6475. }
  6476. const bool fp16acc = ctx->device->fp16;
  6477. vk_pipeline p;
  6478. std::string shname;
  6479. if (shader_size == 0) {
  6480. p = fp16acc ? pipelines[quant].f16acc->a_s : pipelines[quant].f32acc->a_s;
  6481. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_S";
  6482. } else if (shader_size == 1) {
  6483. p = fp16acc ? pipelines[quant].f16acc->a_m : pipelines[quant].f32acc->a_m;
  6484. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_M";
  6485. } else if (shader_size == 2) {
  6486. p = fp16acc ? pipelines[quant].f16acc->a_l : pipelines[quant].f32acc->a_l;
  6487. shname = std::string(ggml_type_name(quant)) + "_ALIGNED_L";
  6488. } else {
  6489. GGML_ASSERT(0);
  6490. }
  6491. const size_t kpad = mmq ? 0 : ggml_vk_align_size(k, p->align);
  6492. if (mmq || k != kpad) {
  6493. if (shader_size == 0) {
  6494. p = fp16acc ? pipelines[quant].f16acc->s : pipelines[quant].f32acc->s;
  6495. shname = std::string(ggml_type_name(quant)) + "_S";
  6496. } else if (shader_size == 1) {
  6497. p = fp16acc ? pipelines[quant].f16acc->m : pipelines[quant].f32acc->m;
  6498. shname = std::string(ggml_type_name(quant)) + "_M";
  6499. } else if (shader_size == 2) {
  6500. p = fp16acc ? pipelines[quant].f16acc->l : pipelines[quant].f32acc->l;
  6501. shname = std::string(ggml_type_name(quant)) + "_L";
  6502. } else {
  6503. GGML_ASSERT(0);
  6504. }
  6505. }
  6506. if (p == nullptr) {
  6507. std::cerr << "error: no pipeline for ggml_vk_test_dequant_matmul " << ggml_type_name(quant) << std::endl;
  6508. return;
  6509. }
  6510. const size_t x_sz = sizeof(float) * x_ne;
  6511. const size_t y_sz = sizeof(float) * y_ne;
  6512. const size_t qx_sz = x_ne * ggml_type_size(quant)/ggml_blck_size(quant);
  6513. const size_t qy_sz = mmq ? y_ne * ggml_type_size(GGML_TYPE_Q8_1)/ggml_blck_size(GGML_TYPE_Q8_1) : y_sz;
  6514. const size_t d_sz = sizeof(float) * d_ne;
  6515. float * x = (float *) malloc(x_sz);
  6516. float * y = (float *) malloc(y_sz);
  6517. void * qx = malloc(qx_sz);
  6518. vk_buffer qx_buf = ggml_vk_create_buffer_check(ctx->device, qx_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6519. vk_buffer y_buf = ggml_vk_create_buffer_check(ctx->device, y_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6520. vk_buffer qy_buf = ggml_vk_create_buffer_check(ctx->device, qy_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6521. vk_buffer d_buf = ggml_vk_create_buffer_check(ctx->device, d_sz, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6522. float * d = (float *) malloc(d_sz);
  6523. float * d_chk = (float *) malloc(d_sz);
  6524. for (size_t i = 0; i < x_ne; i++) {
  6525. x[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6526. // x[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6527. // x[i] = i % k;
  6528. }
  6529. ggml_vk_quantize_data(x, qx, x_ne, quant);
  6530. for (size_t i = 0; i < y_ne; i++) {
  6531. y[i] = (rand() / (float)RAND_MAX) * 2.0f - 1.0f;
  6532. // y[i] = (i % k == i / k) ? 1.0f : 0.0f;
  6533. // y[i] = i % k;
  6534. }
  6535. ggml_pipeline_request_descriptor_sets(ctx->device, p, num_it);
  6536. if (split_k > 1) {
  6537. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_matmul_split_k_reduce, num_it);
  6538. if (ctx->prealloc_split_k == nullptr || ctx->prealloc_split_k->size < sizeof(float) * d_ne * split_k) {
  6539. // Resize buffer
  6540. if (ctx->prealloc_split_k != nullptr) {
  6541. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6542. }
  6543. ctx->prealloc_split_k = ggml_vk_create_buffer_check(ctx->device, sizeof(float) * d_ne * split_k, vk::MemoryPropertyFlagBits::eDeviceLocal);
  6544. }
  6545. }
  6546. if (mmq) {
  6547. ggml_pipeline_request_descriptor_sets(ctx->device, ctx->device->pipeline_quantize_q8_1, num_it);
  6548. }
  6549. if (ctx->device->need_compiles) {
  6550. ggml_vk_load_shaders(ctx->device);
  6551. }
  6552. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  6553. ggml_vk_buffer_write(qx_buf, 0, qx, qx_sz);
  6554. ggml_vk_buffer_write(y_buf, 0, y, y_sz);
  6555. vk_context subctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6556. ggml_vk_ctx_begin(ctx->device, subctx);
  6557. if (mmq) {
  6558. for (size_t i = 0; i < num_it; i++) {
  6559. ggml_vk_quantize_q8_1(ctx, subctx, { y_buf, 0, y_sz }, { qy_buf, 0, qy_sz }, y_ne);
  6560. ggml_vk_matmul(
  6561. ctx, subctx, p, { qx_buf, 0, qx_sz }, { qy_buf, 0, qy_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  6562. m, n, k,
  6563. k, k, m, k*m, k*n, m*n,
  6564. split_k, batch, batch, batch, 1, 1, n
  6565. );
  6566. }
  6567. } else {
  6568. for (size_t i = 0; i < num_it; i++) {
  6569. ggml_vk_matmul(
  6570. ctx, subctx, p, { qx_buf, 0, qx_sz }, { y_buf, 0, y_sz }, { d_buf, 0, d_sz }, { ctx->prealloc_split_k, 0, ctx->prealloc_size_split_k },
  6571. m, n, k,
  6572. k, k, m, k*m, k*n, m*n,
  6573. split_k, batch, batch, batch, 1, 1, n
  6574. );
  6575. }
  6576. }
  6577. ggml_vk_ctx_end(subctx);
  6578. auto begin = std::chrono::high_resolution_clock::now();
  6579. ggml_vk_submit(subctx, ctx->fence);
  6580. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_test_dequant waitForFences");
  6581. ctx->device->device.resetFences({ ctx->fence });
  6582. auto end = std::chrono::high_resolution_clock::now();
  6583. double time_ms = std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count() / 1000.0;
  6584. ggml_vk_buffer_read(d_buf, 0, d, d_sz);
  6585. ggml_init_params iparams = {
  6586. /*.mem_size =*/ 1024*1024*1024,
  6587. /*.mem_buffer =*/ NULL,
  6588. /*.no_alloc =*/ true,
  6589. };
  6590. ggml_context * ggml_ctx = ggml_init(iparams);
  6591. ggml_tensor * src0_ggml = ggml_new_tensor_3d(ggml_ctx, quant, k, m, batch);
  6592. ggml_tensor * src1_ggml = ggml_new_tensor_3d(ggml_ctx, GGML_TYPE_F32, k, n, batch);
  6593. ggml_tensor * tensor_ggml = ggml_mul_mat(ggml_ctx, src0_ggml, src1_ggml);
  6594. src0_ggml->data = qx;
  6595. src1_ggml->data = y;
  6596. tensor_ggml->data = d_chk;
  6597. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  6598. ggml_build_forward_expand(cgraph, tensor_ggml);
  6599. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 1);
  6600. ggml_free(ggml_ctx);
  6601. double avg_err = 0.0;
  6602. int first_err_n = -1;
  6603. int first_err_m = -1;
  6604. int first_err_b = -1;
  6605. for (size_t i = 0; i < m*n*batch; i++) {
  6606. double err = std::fabs(d[i] - d_chk[i]);
  6607. avg_err += err;
  6608. if ((err > 0.05f || std::isnan(err)) && first_err_n == -1) {
  6609. first_err_b = i / (m * n);
  6610. first_err_n = (i % (m * n)) / m;
  6611. first_err_m = (i % (m * n)) % m;
  6612. }
  6613. }
  6614. avg_err /= m * n;
  6615. double tflops = 2.0*m*n*k*batch*num_it / (time_ms / 1000.0) / (1000.0*1000.0*1000.0*1000.0);
  6616. std::cerr << "TEST dequant matmul " << shname;
  6617. if (mmq) {
  6618. std::cerr << " mmq";
  6619. }
  6620. std::cerr << " m=" << m << " n=" << n << " k=" << k << " batch=" << batch << " split_k=" << split_k << " matmul " << time_ms / num_it << "ms " << tflops << " TFLOPS avg_err=" << avg_err << std::endl;
  6621. if (avg_err > 0.01 || std::isnan(avg_err)) {
  6622. std::cerr << "m = " << first_err_m << " n = " << first_err_n << " b = " << first_err_b << std::endl;
  6623. std::cerr << "Actual result: " << std::endl << std::endl;
  6624. ggml_vk_print_matrix_area(d, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6625. std::cerr << std::endl;
  6626. std::cerr << "Expected result: " << std::endl << std::endl;
  6627. ggml_vk_print_matrix_area(d_chk, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6628. std::cerr << "src0: " << std::endl << std::endl;
  6629. ggml_vk_print_matrix_area(x, GGML_TYPE_F32, k, m, first_err_m, first_err_n, first_err_b);
  6630. std::cerr << std::endl;
  6631. std::cerr << "src1: " << std::endl << std::endl;
  6632. ggml_vk_print_matrix_area(y, GGML_TYPE_F32, k, n, first_err_m, first_err_n, first_err_b);
  6633. if (split_k > 1) {
  6634. float * split_k_buf = (float *) malloc(sizeof(float) * d_ne * split_k);
  6635. ggml_vk_buffer_read(ctx->prealloc_split_k, 0, split_k_buf, sizeof(float) * d_ne * split_k);
  6636. std::cerr << "d_buf0: " << std::endl << std::endl;
  6637. ggml_vk_print_matrix_area(split_k_buf, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6638. std::cerr << "d_buf1: " << std::endl << std::endl;
  6639. ggml_vk_print_matrix_area(split_k_buf + d_ne, GGML_TYPE_F32, m, n, first_err_m, first_err_n, first_err_b);
  6640. std::cerr << "d_buf2: " << std::endl << std::endl;
  6641. 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);
  6642. std::cerr << "d_buf3: " << std::endl << std::endl;
  6643. 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);
  6644. free(split_k_buf);
  6645. }
  6646. }
  6647. ggml_vk_destroy_buffer(qx_buf);
  6648. ggml_vk_destroy_buffer(y_buf);
  6649. ggml_vk_destroy_buffer(qy_buf);
  6650. ggml_vk_destroy_buffer(d_buf);
  6651. free(x);
  6652. free(qx);
  6653. free(y);
  6654. free(d);
  6655. free(d_chk);
  6656. }
  6657. #endif
  6658. static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx) {
  6659. #if defined(GGML_VULKAN_RUN_TESTS)
  6660. const std::vector<size_t> vals {
  6661. 512, 512, 128,
  6662. 128, 512, 512,
  6663. 4096, 512, 4096,
  6664. 11008, 512, 4096,
  6665. 4096, 512, 11008,
  6666. 32000, 512, 4096,
  6667. 8, 8, 8,
  6668. 100, 46, 576,
  6669. 623, 111, 128,
  6670. 100, 46, 558,
  6671. 512, 1, 256,
  6672. 128, 110, 622,
  6673. 511, 511, 127,
  6674. 511, 511, 7,
  6675. 511, 511, 17,
  6676. 49, 49, 128,
  6677. 128, 49, 49,
  6678. 4096, 49, 4096,
  6679. };
  6680. const size_t num_it = 1;
  6681. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  6682. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  6683. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  6684. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q4_0, true);
  6685. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q4_0, true);
  6686. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q4_0, true);
  6687. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0);
  6688. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0);
  6689. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0);
  6690. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 0, GGML_TYPE_Q8_0, true);
  6691. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 1, GGML_TYPE_Q8_0, true);
  6692. ggml_vk_test_dequant_matmul(ctx, 4096, 512, 4096, 2, num_it, 1, 2, GGML_TYPE_Q8_0, true);
  6693. abort();
  6694. for (size_t i = 0; i < vals.size(); i += 3) {
  6695. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0);
  6696. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1);
  6697. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2);
  6698. std::cerr << '\n';
  6699. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0);
  6700. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1);
  6701. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2);
  6702. std::cerr << '\n';
  6703. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0);
  6704. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1);
  6705. ggml_vk_test_matmul<ggml_fp16_t, float>(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2);
  6706. std::cerr << '\n' << std::endl;
  6707. if (vals[i + 2] % 32 == 0) {
  6708. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_0);
  6709. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_0);
  6710. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_0);
  6711. std::cerr << '\n';
  6712. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_0);
  6713. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_0);
  6714. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_0);
  6715. std::cerr << '\n';
  6716. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_0);
  6717. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_0);
  6718. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_0);
  6719. std::cerr << '\n' << std::endl;
  6720. }
  6721. if (vals[i + 2] % 256 == 0) {
  6722. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 0, GGML_TYPE_Q4_K);
  6723. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 1, GGML_TYPE_Q4_K);
  6724. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 1, 2, GGML_TYPE_Q4_K);
  6725. std::cerr << '\n';
  6726. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 0, GGML_TYPE_Q4_K);
  6727. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 1, GGML_TYPE_Q4_K);
  6728. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 2, 2, GGML_TYPE_Q4_K);
  6729. std::cerr << '\n';
  6730. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 0, GGML_TYPE_Q4_K);
  6731. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 1, GGML_TYPE_Q4_K);
  6732. ggml_vk_test_dequant_matmul(ctx, vals[i], vals[i + 1], vals[i + 2], 2, num_it, 4, 2, GGML_TYPE_Q4_K);
  6733. std::cerr << '\n' << std::endl;
  6734. }
  6735. }
  6736. GGML_ABORT("fatal error");
  6737. #endif
  6738. if (ctx->prealloc_x == nullptr || (ctx->prealloc_size_x > 0 && ctx->prealloc_x->size < ctx->prealloc_size_x)) {
  6739. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(x_size: " << ctx->prealloc_size_x << ")");
  6740. // Resize buffer
  6741. if (ctx->prealloc_x != nullptr) {
  6742. ggml_vk_destroy_buffer(ctx->prealloc_x);
  6743. }
  6744. ctx->prealloc_x = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_x);
  6745. }
  6746. if (ctx->prealloc_y == nullptr || (ctx->prealloc_size_y > 0 && ctx->prealloc_y->size < ctx->prealloc_size_y)) {
  6747. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(y_size: " << ctx->prealloc_size_y << ")");
  6748. // Resize buffer
  6749. if (ctx->prealloc_y != nullptr) {
  6750. ggml_vk_destroy_buffer(ctx->prealloc_y);
  6751. }
  6752. ctx->prealloc_y = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_y);
  6753. }
  6754. if (ctx->prealloc_split_k == nullptr || (ctx->prealloc_size_split_k > 0 && ctx->prealloc_split_k->size < ctx->prealloc_size_split_k)) {
  6755. VK_LOG_MEMORY("ggml_vk_preallocate_buffers(split_k_size: " << ctx->prealloc_size_split_k << ")");
  6756. // Resize buffer
  6757. if (ctx->prealloc_split_k != nullptr) {
  6758. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  6759. }
  6760. ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k);
  6761. }
  6762. }
  6763. static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* tensor, int tensor_idx, bool use_fence);
  6764. // Returns true if node has enqueued work into the queue, false otherwise
  6765. // If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
  6766. static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool submit){
  6767. if (ggml_is_empty(node) || !node->buffer) {
  6768. return false;
  6769. }
  6770. VK_LOG_DEBUG("ggml_vk_build_graph(" << node << ", " << ggml_op_name(node->op) << ")");
  6771. ctx->semaphore_idx = 0;
  6772. const ggml_tensor * src0 = node->src[0];
  6773. const ggml_tensor * src1 = node->src[1];
  6774. const ggml_tensor * src2 = node->src[2];
  6775. const ggml_tensor * src3 = node->src[3];
  6776. switch (node->op) {
  6777. // Return on empty ops to avoid generating a compute_ctx and setting exit_tensor
  6778. case GGML_OP_RESHAPE:
  6779. case GGML_OP_VIEW:
  6780. case GGML_OP_PERMUTE:
  6781. case GGML_OP_TRANSPOSE:
  6782. case GGML_OP_NONE:
  6783. return false;
  6784. case GGML_OP_UNARY:
  6785. switch (ggml_get_unary_op(node)) {
  6786. case GGML_UNARY_OP_SILU:
  6787. case GGML_UNARY_OP_GELU:
  6788. case GGML_UNARY_OP_GELU_QUICK:
  6789. case GGML_UNARY_OP_RELU:
  6790. case GGML_UNARY_OP_TANH:
  6791. case GGML_UNARY_OP_SIGMOID:
  6792. break;
  6793. default:
  6794. return false;
  6795. }
  6796. break;
  6797. case GGML_OP_REPEAT:
  6798. case GGML_OP_REPEAT_BACK:
  6799. case GGML_OP_GET_ROWS:
  6800. case GGML_OP_ADD:
  6801. case GGML_OP_ACC:
  6802. case GGML_OP_SUB:
  6803. case GGML_OP_MUL:
  6804. case GGML_OP_DIV:
  6805. case GGML_OP_CONCAT:
  6806. case GGML_OP_UPSCALE:
  6807. case GGML_OP_SCALE:
  6808. case GGML_OP_SQR:
  6809. case GGML_OP_SIN:
  6810. case GGML_OP_COS:
  6811. case GGML_OP_CLAMP:
  6812. case GGML_OP_PAD:
  6813. case GGML_OP_CPY:
  6814. case GGML_OP_CONT:
  6815. case GGML_OP_DUP:
  6816. case GGML_OP_SILU_BACK:
  6817. case GGML_OP_NORM:
  6818. case GGML_OP_GROUP_NORM:
  6819. case GGML_OP_RMS_NORM:
  6820. case GGML_OP_RMS_NORM_BACK:
  6821. case GGML_OP_L2_NORM:
  6822. case GGML_OP_DIAG_MASK_INF:
  6823. case GGML_OP_SOFT_MAX:
  6824. case GGML_OP_SOFT_MAX_BACK:
  6825. case GGML_OP_ROPE:
  6826. case GGML_OP_ROPE_BACK:
  6827. case GGML_OP_MUL_MAT:
  6828. case GGML_OP_MUL_MAT_ID:
  6829. case GGML_OP_ARGSORT:
  6830. case GGML_OP_SUM:
  6831. case GGML_OP_SUM_ROWS:
  6832. case GGML_OP_ARGMAX:
  6833. case GGML_OP_COUNT_EQUAL:
  6834. case GGML_OP_IM2COL:
  6835. case GGML_OP_TIMESTEP_EMBEDDING:
  6836. case GGML_OP_POOL_2D:
  6837. case GGML_OP_RWKV_WKV6:
  6838. case GGML_OP_RWKV_WKV7:
  6839. case GGML_OP_LEAKY_RELU:
  6840. case GGML_OP_FLASH_ATTN_EXT:
  6841. case GGML_OP_OPT_STEP_ADAMW:
  6842. break;
  6843. default:
  6844. std::cerr << "ggml_vulkan: Error: Missing op: " << ggml_op_name(node->op) << std::endl;
  6845. GGML_ABORT("fatal error");
  6846. return false;
  6847. }
  6848. vk_context compute_ctx;
  6849. if (!dryrun) {
  6850. if (ctx->compute_ctx.expired()) {
  6851. compute_ctx = ggml_vk_create_context(ctx, ctx->device->compute_queue);
  6852. ctx->compute_ctx = compute_ctx;
  6853. ggml_vk_ctx_begin(ctx->device, compute_ctx);
  6854. } else {
  6855. compute_ctx = ctx->compute_ctx.lock();
  6856. }
  6857. } else {
  6858. switch (node->op) {
  6859. case GGML_OP_REPEAT:
  6860. case GGML_OP_REPEAT_BACK:
  6861. case GGML_OP_ACC:
  6862. case GGML_OP_GET_ROWS:
  6863. case GGML_OP_ADD:
  6864. case GGML_OP_SUB:
  6865. case GGML_OP_MUL:
  6866. case GGML_OP_DIV:
  6867. case GGML_OP_CONCAT:
  6868. case GGML_OP_UPSCALE:
  6869. case GGML_OP_SCALE:
  6870. case GGML_OP_SQR:
  6871. case GGML_OP_SIN:
  6872. case GGML_OP_COS:
  6873. case GGML_OP_CLAMP:
  6874. case GGML_OP_PAD:
  6875. case GGML_OP_CPY:
  6876. case GGML_OP_CONT:
  6877. case GGML_OP_DUP:
  6878. case GGML_OP_SILU_BACK:
  6879. case GGML_OP_NORM:
  6880. case GGML_OP_GROUP_NORM:
  6881. case GGML_OP_RMS_NORM:
  6882. case GGML_OP_RMS_NORM_BACK:
  6883. case GGML_OP_L2_NORM:
  6884. case GGML_OP_UNARY:
  6885. case GGML_OP_DIAG_MASK_INF:
  6886. case GGML_OP_SOFT_MAX:
  6887. case GGML_OP_SOFT_MAX_BACK:
  6888. case GGML_OP_ROPE:
  6889. case GGML_OP_ROPE_BACK:
  6890. case GGML_OP_ARGSORT:
  6891. case GGML_OP_SUM:
  6892. case GGML_OP_SUM_ROWS:
  6893. case GGML_OP_ARGMAX:
  6894. case GGML_OP_COUNT_EQUAL:
  6895. case GGML_OP_IM2COL:
  6896. case GGML_OP_TIMESTEP_EMBEDDING:
  6897. case GGML_OP_POOL_2D:
  6898. case GGML_OP_LEAKY_RELU:
  6899. {
  6900. // These operations all go through ggml_vk_op_f32, so short-circuit and
  6901. // do the only thing needed for the dryrun.
  6902. vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, node, node->op);
  6903. ggml_pipeline_request_descriptor_sets(ctx->device, pipeline, 1);
  6904. return false;
  6905. }
  6906. default:
  6907. break;
  6908. }
  6909. }
  6910. switch (node->op) {
  6911. case GGML_OP_REPEAT:
  6912. ggml_vk_repeat(ctx, compute_ctx, src0, node, dryrun);
  6913. break;
  6914. case GGML_OP_REPEAT_BACK:
  6915. ggml_vk_repeat_back(ctx, compute_ctx, src0, node, dryrun);
  6916. break;
  6917. case GGML_OP_ACC:
  6918. ggml_vk_acc(ctx, compute_ctx, src0, src1, node, dryrun);
  6919. break;
  6920. case GGML_OP_GET_ROWS:
  6921. ggml_vk_get_rows(ctx, compute_ctx, src0, src1, node, dryrun);
  6922. break;
  6923. case GGML_OP_ADD:
  6924. ggml_vk_add(ctx, compute_ctx, src0, src1, node, dryrun);
  6925. break;
  6926. case GGML_OP_SUB:
  6927. ggml_vk_sub(ctx, compute_ctx, src0, src1, node, dryrun);
  6928. break;
  6929. case GGML_OP_MUL:
  6930. ggml_vk_mul(ctx, compute_ctx, src0, src1, node, dryrun);
  6931. break;
  6932. case GGML_OP_DIV:
  6933. ggml_vk_div(ctx, compute_ctx, src0, src1, node, dryrun);
  6934. break;
  6935. case GGML_OP_CONCAT:
  6936. ggml_vk_concat(ctx, compute_ctx, src0, src1, node, dryrun);
  6937. break;
  6938. case GGML_OP_UPSCALE:
  6939. ggml_vk_upscale(ctx, compute_ctx, src0, node, dryrun);
  6940. break;
  6941. case GGML_OP_SCALE:
  6942. ggml_vk_scale(ctx, compute_ctx, src0, node, dryrun);
  6943. break;
  6944. case GGML_OP_SQR:
  6945. ggml_vk_sqr(ctx, compute_ctx, src0, node, dryrun);
  6946. break;
  6947. case GGML_OP_SIN:
  6948. ggml_vk_sin(ctx, compute_ctx, src0, node, dryrun);
  6949. break;
  6950. case GGML_OP_COS:
  6951. ggml_vk_cos(ctx, compute_ctx, src0, node, dryrun);
  6952. break;
  6953. case GGML_OP_CLAMP:
  6954. ggml_vk_clamp(ctx, compute_ctx, src0, node, dryrun);
  6955. break;
  6956. case GGML_OP_PAD:
  6957. ggml_vk_pad(ctx, compute_ctx, src0, node, dryrun);
  6958. break;
  6959. case GGML_OP_CPY:
  6960. case GGML_OP_CONT:
  6961. case GGML_OP_DUP:
  6962. ggml_vk_cpy(ctx, compute_ctx, src0, node, dryrun);
  6963. break;
  6964. case GGML_OP_SILU_BACK:
  6965. ggml_vk_silu_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6966. break;
  6967. case GGML_OP_NORM:
  6968. ggml_vk_norm(ctx, compute_ctx, src0, node, dryrun);
  6969. break;
  6970. case GGML_OP_GROUP_NORM:
  6971. ggml_vk_group_norm(ctx, compute_ctx, src0, node, dryrun);
  6972. break;
  6973. case GGML_OP_RMS_NORM:
  6974. ggml_vk_rms_norm(ctx, compute_ctx, src0, node, dryrun);
  6975. break;
  6976. case GGML_OP_RMS_NORM_BACK:
  6977. ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
  6978. break;
  6979. case GGML_OP_L2_NORM:
  6980. ggml_vk_l2_norm(ctx, compute_ctx, src0, node, dryrun);
  6981. break;
  6982. case GGML_OP_UNARY:
  6983. switch (ggml_get_unary_op(node)) {
  6984. case GGML_UNARY_OP_SILU:
  6985. case GGML_UNARY_OP_GELU:
  6986. case GGML_UNARY_OP_GELU_QUICK:
  6987. case GGML_UNARY_OP_RELU:
  6988. case GGML_UNARY_OP_TANH:
  6989. case GGML_UNARY_OP_SIGMOID:
  6990. ggml_vk_unary(ctx, compute_ctx, src0, node, dryrun);
  6991. break;
  6992. default:
  6993. return false;
  6994. }
  6995. break;
  6996. case GGML_OP_DIAG_MASK_INF:
  6997. ggml_vk_diag_mask_inf(ctx, compute_ctx, src0, node, dryrun);
  6998. break;
  6999. case GGML_OP_SOFT_MAX:
  7000. ggml_vk_soft_max(ctx, compute_ctx, src0, src1, node, dryrun);
  7001. break;
  7002. case GGML_OP_SOFT_MAX_BACK:
  7003. ggml_vk_soft_max_back(ctx, compute_ctx, src0, src1, node, dryrun);
  7004. break;
  7005. case GGML_OP_ROPE:
  7006. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, false, dryrun);
  7007. break;
  7008. case GGML_OP_ROPE_BACK:
  7009. ggml_vk_rope(ctx, compute_ctx, src0, src1, src2, node, true, dryrun);
  7010. break;
  7011. case GGML_OP_ARGSORT:
  7012. ggml_vk_argsort(ctx, compute_ctx, src0, node, dryrun);
  7013. break;
  7014. case GGML_OP_SUM:
  7015. ggml_vk_sum(ctx, compute_ctx, src0, node, dryrun);
  7016. break;
  7017. case GGML_OP_SUM_ROWS:
  7018. ggml_vk_sum_rows(ctx, compute_ctx, src0, node, dryrun);
  7019. break;
  7020. case GGML_OP_ARGMAX:
  7021. ggml_vk_argmax(ctx, compute_ctx, src0, node, dryrun);
  7022. break;
  7023. case GGML_OP_COUNT_EQUAL:
  7024. ggml_vk_count_equal(ctx, compute_ctx, src0, src1, node, dryrun);
  7025. break;
  7026. case GGML_OP_IM2COL:
  7027. ggml_vk_im2col(ctx, compute_ctx, src0, src1, node, dryrun);
  7028. break;
  7029. case GGML_OP_TIMESTEP_EMBEDDING:
  7030. ggml_vk_timestep_embedding(ctx, compute_ctx, src0, node, dryrun);
  7031. break;
  7032. case GGML_OP_POOL_2D:
  7033. ggml_vk_pool_2d(ctx, compute_ctx, src0, node, dryrun);
  7034. break;
  7035. case GGML_OP_LEAKY_RELU:
  7036. ggml_vk_leaky_relu(ctx, compute_ctx, src0, node, dryrun);
  7037. break;
  7038. case GGML_OP_MUL_MAT:
  7039. ggml_vk_mul_mat(ctx, compute_ctx, src0, src1, node, dryrun);
  7040. break;
  7041. case GGML_OP_MUL_MAT_ID:
  7042. ggml_vk_mul_mat_id(ctx, compute_ctx, src0, src1, src2, node, dryrun);
  7043. break;
  7044. case GGML_OP_FLASH_ATTN_EXT:
  7045. ggml_vk_flash_attn(ctx, compute_ctx, src0, src1, src2, src3, node, dryrun);
  7046. break;
  7047. case GGML_OP_RWKV_WKV6:
  7048. ggml_vk_rwkv_wkv6(ctx, compute_ctx, node, dryrun);
  7049. break;
  7050. case GGML_OP_RWKV_WKV7:
  7051. ggml_vk_rwkv_wkv7(ctx, compute_ctx, node, dryrun);
  7052. break;
  7053. case GGML_OP_OPT_STEP_ADAMW:
  7054. ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
  7055. break;
  7056. default:
  7057. return false;
  7058. }
  7059. if (dryrun) {
  7060. return false;
  7061. }
  7062. ctx->tensor_ctxs[node_idx] = compute_ctx;
  7063. #if defined(GGML_VULKAN_CHECK_RESULTS) || defined(GGML_VULKAN_PERF)
  7064. // Force context reset on each node so that each tensor ends up in its own context
  7065. // and can be run and compared to its CPU equivalent separately
  7066. last_node = true;
  7067. #endif
  7068. if (submit || last_node) {
  7069. ggml_vk_ctx_end(compute_ctx);
  7070. // TODO probably it'd be better to pass a exit_node flag to ggml_vk_compute_forward
  7071. if (last_node) {
  7072. compute_ctx->exit_tensor_idx = node_idx_begin;
  7073. }
  7074. else {
  7075. compute_ctx->exit_tensor_idx = -1;
  7076. }
  7077. ctx->compute_ctx.reset();
  7078. bool ok = ggml_vk_compute_forward(ctx, node_begin, node_idx_begin, false);
  7079. if (!ok) {
  7080. if (node->op == GGML_OP_UNARY) {
  7081. std::cerr << __func__ << ": error: op not supported UNARY " << node->name << " (" << ggml_unary_op_name(static_cast<ggml_unary_op>(node->op_params[0])) << ")" << std::endl;
  7082. }
  7083. else {
  7084. std::cerr << __func__ << ": error: op not supported " << node->name << " (" << ggml_op_name(node->op) << ")" << std::endl;
  7085. }
  7086. }
  7087. }
  7088. return true;
  7089. }
  7090. static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_tensor * tensor, int tensor_idx, bool use_fence = true){
  7091. ggml_backend_buffer * buf = nullptr;
  7092. switch (tensor->op) {
  7093. case GGML_OP_ADD:
  7094. case GGML_OP_ACC:
  7095. case GGML_OP_GET_ROWS:
  7096. case GGML_OP_SUB:
  7097. case GGML_OP_MUL:
  7098. case GGML_OP_DIV:
  7099. case GGML_OP_CONCAT:
  7100. case GGML_OP_UPSCALE:
  7101. case GGML_OP_SCALE:
  7102. case GGML_OP_SQR:
  7103. case GGML_OP_SIN:
  7104. case GGML_OP_COS:
  7105. case GGML_OP_CLAMP:
  7106. case GGML_OP_PAD:
  7107. case GGML_OP_CPY:
  7108. case GGML_OP_CONT:
  7109. case GGML_OP_DUP:
  7110. case GGML_OP_SILU_BACK:
  7111. case GGML_OP_NORM:
  7112. case GGML_OP_GROUP_NORM:
  7113. case GGML_OP_RMS_NORM:
  7114. case GGML_OP_RMS_NORM_BACK:
  7115. case GGML_OP_L2_NORM:
  7116. case GGML_OP_DIAG_MASK_INF:
  7117. case GGML_OP_SOFT_MAX:
  7118. case GGML_OP_SOFT_MAX_BACK:
  7119. case GGML_OP_ROPE:
  7120. case GGML_OP_ROPE_BACK:
  7121. case GGML_OP_RESHAPE:
  7122. case GGML_OP_VIEW:
  7123. case GGML_OP_PERMUTE:
  7124. case GGML_OP_TRANSPOSE:
  7125. case GGML_OP_NONE:
  7126. case GGML_OP_ARGSORT:
  7127. case GGML_OP_SUM:
  7128. case GGML_OP_SUM_ROWS:
  7129. case GGML_OP_ARGMAX:
  7130. case GGML_OP_COUNT_EQUAL:
  7131. case GGML_OP_IM2COL:
  7132. case GGML_OP_TIMESTEP_EMBEDDING:
  7133. case GGML_OP_POOL_2D:
  7134. case GGML_OP_RWKV_WKV6:
  7135. case GGML_OP_RWKV_WKV7:
  7136. case GGML_OP_LEAKY_RELU:
  7137. case GGML_OP_REPEAT:
  7138. case GGML_OP_REPEAT_BACK:
  7139. case GGML_OP_OPT_STEP_ADAMW:
  7140. buf = tensor->buffer;
  7141. break;
  7142. case GGML_OP_UNARY:
  7143. switch (ggml_get_unary_op(tensor)) {
  7144. case GGML_UNARY_OP_SILU:
  7145. case GGML_UNARY_OP_GELU:
  7146. case GGML_UNARY_OP_GELU_QUICK:
  7147. case GGML_UNARY_OP_RELU:
  7148. case GGML_UNARY_OP_TANH:
  7149. case GGML_UNARY_OP_SIGMOID:
  7150. buf = tensor->buffer;
  7151. break;
  7152. default:
  7153. return false;
  7154. }
  7155. break;
  7156. case GGML_OP_MUL_MAT:
  7157. case GGML_OP_MUL_MAT_ID:
  7158. case GGML_OP_FLASH_ATTN_EXT:
  7159. buf = tensor->buffer;
  7160. break;
  7161. default:
  7162. return false;
  7163. }
  7164. if (buf == nullptr) {
  7165. return false;
  7166. }
  7167. VK_LOG_DEBUG("ggml_vk_compute_forward(" << tensor << ", name=" << tensor->name << ", op=" << ggml_op_name(tensor->op) << ", type=" << tensor->type << ", ne0=" << tensor->ne[0] << ", ne1=" << tensor->ne[1] << ", ne2=" << tensor->ne[2] << ", ne3=" << tensor->ne[3] << ", nb0=" << tensor->nb[0] << ", nb1=" << tensor->nb[1] << ", nb2=" << tensor->nb[2] << ", nb3=" << tensor->nb[3] << ", view_src=" << tensor->view_src << ", view_offs=" << tensor->view_offs << ")");
  7168. vk_context subctx = ctx->tensor_ctxs[tensor_idx].lock();
  7169. // always wait for the GPU work to be done for the last submit
  7170. if (tensor_idx == subctx->exit_tensor_idx) {
  7171. use_fence = true;
  7172. }
  7173. // Only run if ctx hasn't been submitted yet
  7174. if (!subctx->seqs.empty()) {
  7175. #ifdef GGML_VULKAN_CHECK_RESULTS
  7176. ggml_vk_check_results_0(tensor);
  7177. use_fence = true;
  7178. #endif
  7179. // Do staging buffer copies
  7180. for (auto& cpy : subctx->in_memcpys) {
  7181. memcpy(cpy.dst, cpy.src, cpy.n);
  7182. }
  7183. ggml_vk_submit(subctx, use_fence ? ctx->fence : vk::Fence{});
  7184. if (use_fence) {
  7185. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_vk_compute_forward waitForFences");
  7186. ctx->device->device.resetFences({ ctx->fence });
  7187. }
  7188. #ifdef GGML_VULKAN_CHECK_RESULTS
  7189. ggml_vk_check_results_1(tensor);
  7190. #endif
  7191. }
  7192. if (tensor_idx == subctx->exit_tensor_idx) {
  7193. // Do staging buffer copies
  7194. for (auto& cpy : subctx->out_memcpys) {
  7195. memcpy(cpy.dst, cpy.src, cpy.n);
  7196. }
  7197. subctx->in_memcpys.clear();
  7198. subctx->out_memcpys.clear();
  7199. }
  7200. return true;
  7201. }
  7202. // Clean up after graph processing is done
  7203. static void ggml_vk_graph_cleanup(ggml_backend_vk_context * ctx) {
  7204. VK_LOG_DEBUG("ggml_vk_graph_cleanup()");
  7205. for (auto& buffer : ctx->gc.temp_buffers) {
  7206. ggml_vk_pool_free(ctx, buffer);
  7207. }
  7208. ctx->gc.temp_buffers.clear();
  7209. for (auto& dsr : ctx->device->pipeline_descriptor_set_requirements) {
  7210. vk_pipeline_ref plr = ctx->device->pipelines[dsr.first];
  7211. if (plr.expired()) {
  7212. continue;
  7213. }
  7214. vk_pipeline pl = plr.lock();
  7215. ggml_pipeline_cleanup(pl);
  7216. }
  7217. ggml_vk_queue_cleanup(ctx->device, ctx->device->compute_queue);
  7218. ggml_vk_queue_cleanup(ctx->device, ctx->device->transfer_queue);
  7219. for (size_t i = 0; i < ctx->gc.semaphores.size(); i++) {
  7220. ctx->device->device.destroySemaphore({ ctx->gc.semaphores[i].s });
  7221. }
  7222. ctx->gc.semaphores.clear();
  7223. for (size_t i = 0; i < ctx->gc.tl_semaphores.size(); i++) {
  7224. ctx->device->device.destroySemaphore({ ctx->gc.tl_semaphores[i].s });
  7225. }
  7226. ctx->gc.tl_semaphores.clear();
  7227. ctx->semaphore_idx = 0;
  7228. ctx->event_idx = 0;
  7229. for (auto& event : ctx->gc.events) {
  7230. ctx->device->device.resetEvent(event);
  7231. }
  7232. ctx->tensor_ctxs.clear();
  7233. ctx->gc.contexts.clear();
  7234. ctx->device->pipeline_descriptor_set_requirements.clear();
  7235. }
  7236. // Clean up on backend free
  7237. static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) {
  7238. VK_LOG_DEBUG("ggml_vk_cleanup(" << ctx->name << ")");
  7239. ggml_vk_graph_cleanup(ctx);
  7240. ggml_vk_destroy_buffer(ctx->prealloc_x);
  7241. ggml_vk_destroy_buffer(ctx->prealloc_y);
  7242. ggml_vk_destroy_buffer(ctx->prealloc_split_k);
  7243. for (auto& buffer : ctx->buffer_pool) {
  7244. ggml_vk_destroy_buffer(buffer);
  7245. }
  7246. ctx->prealloc_size_x = 0;
  7247. ctx->prealloc_size_y = 0;
  7248. ctx->prealloc_size_split_k = 0;
  7249. for (auto& event : ctx->gc.events) {
  7250. ctx->device->device.destroyEvent(event);
  7251. }
  7252. ctx->gc.events.clear();
  7253. ctx->device->device.destroyFence(ctx->fence);
  7254. }
  7255. static int ggml_vk_get_device_count() {
  7256. ggml_vk_instance_init();
  7257. return vk_instance.device_indices.size();
  7258. }
  7259. static void ggml_vk_get_device_description(int device, char * description, size_t description_size) {
  7260. ggml_vk_instance_init();
  7261. std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
  7262. vk::PhysicalDeviceProperties props;
  7263. devices[device].getProperties(&props);
  7264. snprintf(description, description_size, "%s", props.deviceName.data());
  7265. }
  7266. // backend interface
  7267. #define UNUSED GGML_UNUSED
  7268. // device backend
  7269. static bool ggml_backend_buffer_is_vk(ggml_backend_buffer_t buffer) {
  7270. return buffer->buft->iface.get_name == ggml_backend_vk_buffer_type_name;
  7271. }
  7272. static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7273. VK_LOG_MEMORY("ggml_backend_vk_buffer_free_buffer()");
  7274. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7275. ggml_vk_destroy_buffer(ctx->dev_buffer);
  7276. delete ctx;
  7277. }
  7278. static void * ggml_backend_vk_buffer_get_base(ggml_backend_buffer_t buffer) {
  7279. return vk_ptr_base;
  7280. UNUSED(buffer);
  7281. }
  7282. static enum ggml_status ggml_backend_vk_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7283. VK_LOG_DEBUG("ggml_backend_vk_buffer_init_tensor(" << buffer << " (" << buffer->context << "), " << tensor << ")");
  7284. if (tensor->view_src != nullptr) {
  7285. GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
  7286. }
  7287. return GGML_STATUS_SUCCESS;
  7288. }
  7289. static void ggml_backend_vk_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
  7290. VK_LOG_DEBUG("ggml_backend_vk_buffer_memset_tensor(" << buffer << ", " << tensor << ", " << value << ", " << offset << ", " << size << ")");
  7291. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7292. vk_buffer buf = buf_ctx->dev_buffer;
  7293. uint32_t val32 = (uint32_t)value * 0x01010101;
  7294. ggml_vk_buffer_memset(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, val32, size);
  7295. }
  7296. 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) {
  7297. VK_LOG_DEBUG("ggml_backend_vk_buffer_set_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  7298. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7299. vk_buffer buf = buf_ctx->dev_buffer;
  7300. ggml_vk_buffer_write(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7301. }
  7302. 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) {
  7303. VK_LOG_DEBUG("ggml_backend_vk_buffer_get_tensor(" << buffer << ", " << tensor << ", " << data << ", " << offset << ", " << size << ")");
  7304. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7305. vk_buffer buf = buf_ctx->dev_buffer;
  7306. ggml_vk_buffer_read(buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7307. }
  7308. static bool ggml_backend_vk_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  7309. if (ggml_backend_buffer_is_vk(src->buffer)) {
  7310. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  7311. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7312. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  7313. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  7314. ggml_vk_buffer_copy(dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  7315. return true;
  7316. }
  7317. return false;
  7318. UNUSED(buffer);
  7319. }
  7320. static void ggml_backend_vk_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7321. ggml_backend_vk_buffer_context * ctx = (ggml_backend_vk_buffer_context *)buffer->context;
  7322. ggml_vk_buffer_memset(ctx->dev_buffer, 0, value, buffer->size);
  7323. }
  7324. static ggml_backend_buffer_i ggml_backend_vk_buffer_interface = {
  7325. /* .free_buffer = */ ggml_backend_vk_buffer_free_buffer,
  7326. /* .get_base = */ ggml_backend_vk_buffer_get_base,
  7327. /* .init_tensor = */ ggml_backend_vk_buffer_init_tensor,
  7328. /* .memset_tensor = */ ggml_backend_vk_buffer_memset_tensor,
  7329. /* .set_tensor = */ ggml_backend_vk_buffer_set_tensor,
  7330. /* .get_tensor = */ ggml_backend_vk_buffer_get_tensor,
  7331. /* .cpy_tensor = */ ggml_backend_vk_buffer_cpy_tensor,
  7332. /* .clear = */ ggml_backend_vk_buffer_clear,
  7333. /* .reset = */ NULL,
  7334. };
  7335. // vk buffer type
  7336. static const char * ggml_backend_vk_buffer_type_name(ggml_backend_buffer_type_t buft) {
  7337. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  7338. return ctx->name.c_str();
  7339. }
  7340. static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7341. VK_LOG_MEMORY("ggml_backend_vk_buffer_type_alloc_buffer(" << size << ")");
  7342. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7343. vk_buffer dev_buffer = nullptr;
  7344. try {
  7345. dev_buffer = ggml_vk_create_buffer_device(ctx->device, size);
  7346. } catch (const vk::SystemError& e) {
  7347. return nullptr;
  7348. }
  7349. ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(ctx->device, std::move(dev_buffer), ctx->name);
  7350. return ggml_backend_buffer_init(buft, ggml_backend_vk_buffer_interface, bufctx, size);
  7351. }
  7352. static size_t ggml_backend_vk_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7353. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7354. return ctx->device->properties.limits.minStorageBufferOffsetAlignment;
  7355. }
  7356. static size_t ggml_backend_vk_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  7357. ggml_backend_vk_buffer_type_context * ctx = (ggml_backend_vk_buffer_type_context *) buft->context;
  7358. return ctx->device->suballocation_block_size;
  7359. }
  7360. static size_t ggml_backend_vk_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  7361. return ggml_nbytes(tensor);
  7362. UNUSED(buft);
  7363. }
  7364. ggml_backend_buffer_type_t ggml_backend_vk_buffer_type(size_t dev_num) {
  7365. ggml_vk_instance_init();
  7366. VK_LOG_DEBUG("ggml_backend_vk_buffer_type(" << dev_num << ")");
  7367. vk_device dev = ggml_vk_get_device(dev_num);
  7368. return &dev->buffer_type;
  7369. }
  7370. // host buffer type
  7371. static const char * ggml_backend_vk_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  7372. return GGML_VK_NAME "_Host";
  7373. UNUSED(buft);
  7374. }
  7375. static const char * ggml_backend_vk_host_buffer_name(ggml_backend_buffer_t buffer) {
  7376. return GGML_VK_NAME "_Host";
  7377. UNUSED(buffer);
  7378. }
  7379. static void ggml_backend_vk_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7380. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_free_buffer()");
  7381. ggml_vk_host_free(vk_instance.devices[0], buffer->context);
  7382. }
  7383. static ggml_backend_buffer_t ggml_backend_vk_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7384. VK_LOG_MEMORY("ggml_backend_vk_host_buffer_type_alloc_buffer(" << size << ")");
  7385. size += 32; // Behave like the CPU buffer type
  7386. void * ptr = nullptr;
  7387. try {
  7388. ptr = ggml_vk_host_malloc(vk_instance.devices[0], size);
  7389. } catch (vk::SystemError& e) {
  7390. std::cerr << "ggml_vulkan: Failed to allocate pinned memory." << std::endl;
  7391. std::cerr << "ggml_vulkan: " << e.what() << std::endl;
  7392. // fallback to cpu buffer
  7393. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7394. }
  7395. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7396. buffer->buft = buft;
  7397. buffer->iface.free_buffer = ggml_backend_vk_host_buffer_free_buffer;
  7398. return buffer;
  7399. UNUSED(buft);
  7400. }
  7401. static size_t ggml_backend_vk_host_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7402. return vk_instance.devices[0]->properties.limits.minMemoryMapAlignment;
  7403. UNUSED(buft);
  7404. }
  7405. // Should be changed to return device-specific host buffer type
  7406. // but that probably requires changes in llama.cpp
  7407. ggml_backend_buffer_type_t ggml_backend_vk_host_buffer_type() {
  7408. static struct ggml_backend_buffer_type ggml_backend_vk_buffer_type_host = {
  7409. /* .iface = */ {
  7410. /* .get_name = */ ggml_backend_vk_host_buffer_type_name,
  7411. /* .alloc_buffer = */ ggml_backend_vk_host_buffer_type_alloc_buffer,
  7412. /* .get_alignment = */ ggml_backend_vk_host_buffer_type_get_alignment,
  7413. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  7414. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7415. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7416. },
  7417. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), 0),
  7418. /* .context = */ nullptr,
  7419. };
  7420. // Make sure device 0 is initialized
  7421. ggml_vk_instance_init();
  7422. ggml_vk_get_device(0);
  7423. return &ggml_backend_vk_buffer_type_host;
  7424. }
  7425. // backend
  7426. static const char * ggml_backend_vk_name(ggml_backend_t backend) {
  7427. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7428. return ctx->name.c_str();
  7429. }
  7430. static void ggml_backend_vk_free(ggml_backend_t backend) {
  7431. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7432. VK_LOG_DEBUG("ggml_backend_vk_free(" << ctx->name << ")");
  7433. ggml_vk_cleanup(ctx);
  7434. delete ctx;
  7435. delete backend;
  7436. }
  7437. static ggml_backend_buffer_type_t ggml_backend_vk_get_default_buffer_type(ggml_backend_t backend) {
  7438. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7439. return &ctx->device->buffer_type;
  7440. }
  7441. static void ggml_backend_vk_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7442. VK_LOG_DEBUG("ggml_backend_vk_set_tensor_async(" << size << ")");
  7443. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7444. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  7445. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7446. vk_context transfer_ctx;
  7447. if (ctx->transfer_ctx.expired()) {
  7448. // Initialize new transfer context
  7449. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7450. ctx->transfer_ctx = transfer_ctx;
  7451. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7452. } else {
  7453. transfer_ctx = ctx->transfer_ctx.lock();
  7454. }
  7455. vk_buffer buf = buf_ctx->dev_buffer;
  7456. ggml_vk_buffer_write_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7457. }
  7458. static void ggml_backend_vk_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7459. VK_LOG_DEBUG("ggml_backend_vk_get_tensor_async(" << size << ")");
  7460. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7461. GGML_ASSERT((tensor->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || tensor->buffer->buft == ggml_backend_vk_host_buffer_type()) && "unsupported buffer type");
  7462. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  7463. vk_context transfer_ctx;
  7464. if (ctx->transfer_ctx.expired()) {
  7465. // Initialize new transfer context
  7466. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7467. ctx->transfer_ctx = transfer_ctx;
  7468. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7469. } else {
  7470. transfer_ctx = ctx->transfer_ctx.lock();
  7471. }
  7472. vk_buffer buf = buf_ctx->dev_buffer;
  7473. ggml_vk_buffer_read_async(transfer_ctx, buf, vk_tensor_offset(tensor) + tensor->view_offs + offset, data, size);
  7474. }
  7475. static bool ggml_backend_vk_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  7476. VK_LOG_DEBUG("ggml_backend_vk_cpy_tensor_async()");
  7477. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7478. if ((dst->buffer->buft == ggml_backend_vk_get_default_buffer_type(backend) || dst->buffer->buft == ggml_backend_vk_host_buffer_type()) && ggml_backend_buffer_is_vk(src->buffer)) {
  7479. ggml_backend_vk_buffer_context * src_buf_ctx = (ggml_backend_vk_buffer_context *)src->buffer->context;
  7480. ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
  7481. vk_context transfer_ctx;
  7482. if (ctx->transfer_ctx.expired()) {
  7483. // Initialize new transfer context
  7484. transfer_ctx = ggml_vk_create_context(ctx, ctx->device->transfer_queue);
  7485. ctx->transfer_ctx = transfer_ctx;
  7486. ggml_vk_ctx_begin(ctx->device, transfer_ctx);
  7487. } else {
  7488. transfer_ctx = ctx->transfer_ctx.lock();
  7489. }
  7490. vk_buffer src_buf = src_buf_ctx->dev_buffer;
  7491. vk_buffer dst_buf = dst_buf_ctx->dev_buffer;
  7492. ggml_vk_buffer_copy_async(transfer_ctx, dst_buf, vk_tensor_offset(dst) + dst->view_offs, src_buf, vk_tensor_offset(src) + src->view_offs, ggml_nbytes(src));
  7493. return true;
  7494. }
  7495. return false;
  7496. }
  7497. static void ggml_backend_vk_synchronize(ggml_backend_t backend) {
  7498. VK_LOG_DEBUG("ggml_backend_vk_synchronize()");
  7499. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7500. if(ctx->transfer_ctx.expired()) {
  7501. return;
  7502. }
  7503. vk_context transfer_ctx = ctx->transfer_ctx.lock();
  7504. ggml_vk_ctx_end(transfer_ctx);
  7505. for (auto& cpy : transfer_ctx->in_memcpys) {
  7506. memcpy(cpy.dst, cpy.src, cpy.n);
  7507. }
  7508. ggml_vk_submit(transfer_ctx, ctx->fence);
  7509. VK_CHECK(ctx->device->device.waitForFences({ ctx->fence }, true, UINT64_MAX), "ggml_backend_vk_synchronize waitForFences");
  7510. ctx->device->device.resetFences({ ctx->fence });
  7511. for (auto& cpy : transfer_ctx->out_memcpys) {
  7512. memcpy(cpy.dst, cpy.src, cpy.n);
  7513. }
  7514. ctx->transfer_ctx.reset();
  7515. }
  7516. static bool ggml_vk_is_empty(ggml_tensor * node) {
  7517. return ggml_is_empty(node) || node->op == GGML_OP_NONE || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE;
  7518. }
  7519. static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7520. VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
  7521. ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
  7522. uint64_t total_mat_mul_bytes = 0;
  7523. for (int i = 0; i < cgraph->n_nodes; i++) {
  7524. ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false);
  7525. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  7526. total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  7527. }
  7528. }
  7529. if (ctx->device->need_compiles) {
  7530. ggml_vk_load_shaders(ctx->device);
  7531. }
  7532. ggml_vk_preallocate_buffers(ctx);
  7533. ggml_pipeline_allocate_descriptor_sets(ctx->device);
  7534. int last_node = cgraph->n_nodes - 1;
  7535. // If the last op in the cgraph isn't backend GPU, the command buffer doesn't get closed properly
  7536. while (last_node > 0 && ggml_vk_is_empty(cgraph->nodes[last_node])) {
  7537. last_node -= 1;
  7538. }
  7539. // Reserve tensor context space for all nodes
  7540. ctx->tensor_ctxs.resize(cgraph->n_nodes);
  7541. bool first_node_in_batch = true; // true if next node will be first node in a batch
  7542. int submit_node_idx = 0; // index to first node in a batch
  7543. // Submit after enough work has accumulated, to overlap CPU cmdbuffer generation with GPU execution.
  7544. // Estimate the amount of matmul work by looking at the weight matrix size, and submit every 100MB
  7545. // (and scaled down based on model size, so smaller models submit earlier).
  7546. // Also submit at least every 100 nodes, in case there are workloads without as much matmul.
  7547. int nodes_per_submit = 100;
  7548. int submitted_nodes = 0;
  7549. int submit_count = 0;
  7550. uint64_t mul_mat_bytes = 0;
  7551. uint64_t mul_mat_bytes_per_submit = std::min(uint64_t(100*1000*1000), total_mat_mul_bytes / 40u);
  7552. for (int i = 0; i < cgraph->n_nodes; i++) {
  7553. if (first_node_in_batch) {
  7554. submit_node_idx = i;
  7555. }
  7556. if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
  7557. mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
  7558. }
  7559. bool submit = (submitted_nodes >= nodes_per_submit) ||
  7560. (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
  7561. (i == last_node);
  7562. bool enqueued = ggml_vk_build_graph(ctx, cgraph->nodes[i], i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i == last_node, submit);
  7563. if (enqueued) {
  7564. ++submitted_nodes;
  7565. #ifndef GGML_VULKAN_CHECK_RESULTS
  7566. if (first_node_in_batch) {
  7567. first_node_in_batch = false;
  7568. }
  7569. #endif
  7570. }
  7571. if (submit) {
  7572. first_node_in_batch = true;
  7573. submitted_nodes = 0;
  7574. mul_mat_bytes = 0;
  7575. if (submit_count < 3) {
  7576. mul_mat_bytes_per_submit *= 2;
  7577. }
  7578. submit_count++;
  7579. }
  7580. }
  7581. #ifdef GGML_VULKAN_PERF
  7582. ctx->device->perf_logger->print_timings();
  7583. #endif
  7584. ggml_vk_graph_cleanup(ctx);
  7585. return GGML_STATUS_SUCCESS;
  7586. UNUSED(backend);
  7587. }
  7588. // TODO: enable async and synchronize
  7589. static ggml_backend_i ggml_backend_vk_interface = {
  7590. /* .get_name = */ ggml_backend_vk_name,
  7591. /* .free = */ ggml_backend_vk_free,
  7592. /* .set_tensor_async = */ NULL, // ggml_backend_vk_set_tensor_async,
  7593. /* .get_tensor_async = */ NULL, // ggml_backend_vk_get_tensor_async,
  7594. /* .cpy_tensor_async = */ NULL, // ggml_backend_vk_cpy_tensor_async,
  7595. /* .synchronize = */ NULL, // ggml_backend_vk_synchronize,
  7596. /* .graph_plan_create = */ NULL,
  7597. /* .graph_plan_free = */ NULL,
  7598. /* .graph_plan_update = */ NULL,
  7599. /* .graph_plan_compute = */ NULL,
  7600. /* .graph_compute = */ ggml_backend_vk_graph_compute,
  7601. /* .event_record = */ NULL,
  7602. /* .event_wait = */ NULL,
  7603. };
  7604. static ggml_guid_t ggml_backend_vk_guid() {
  7605. static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
  7606. return &guid;
  7607. }
  7608. ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
  7609. VK_LOG_DEBUG("ggml_backend_vk_init(" << dev_num << ")");
  7610. ggml_backend_vk_context * ctx = new ggml_backend_vk_context;
  7611. ggml_vk_init(ctx, dev_num);
  7612. ggml_backend_t vk_backend = new ggml_backend {
  7613. /* .guid = */ ggml_backend_vk_guid(),
  7614. /* .interface = */ ggml_backend_vk_interface,
  7615. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_vk_reg(), dev_num),
  7616. /* .context = */ ctx,
  7617. };
  7618. return vk_backend;
  7619. }
  7620. bool ggml_backend_is_vk(ggml_backend_t backend) {
  7621. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
  7622. }
  7623. int ggml_backend_vk_get_device_count() {
  7624. return ggml_vk_get_device_count();
  7625. }
  7626. void ggml_backend_vk_get_device_description(int device, char * description, size_t description_size) {
  7627. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7628. int dev_idx = vk_instance.device_indices[device];
  7629. ggml_vk_get_device_description(dev_idx, description, description_size);
  7630. }
  7631. void ggml_backend_vk_get_device_memory(int device, size_t * free, size_t * total) {
  7632. GGML_ASSERT(device < (int) vk_instance.device_indices.size());
  7633. vk::PhysicalDevice vkdev = vk_instance.instance.enumeratePhysicalDevices()[vk_instance.device_indices[device]];
  7634. vk::PhysicalDeviceMemoryProperties memprops = vkdev.getMemoryProperties();
  7635. for (const vk::MemoryHeap& heap : memprops.memoryHeaps) {
  7636. if (heap.flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
  7637. *total = heap.size;
  7638. *free = heap.size;
  7639. break;
  7640. }
  7641. }
  7642. }
  7643. //////////////////////////
  7644. struct ggml_backend_vk_device_context {
  7645. size_t device;
  7646. std::string name;
  7647. std::string description;
  7648. };
  7649. static const char * ggml_backend_vk_device_get_name(ggml_backend_dev_t dev) {
  7650. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7651. return ctx->name.c_str();
  7652. }
  7653. static const char * ggml_backend_vk_device_get_description(ggml_backend_dev_t dev) {
  7654. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7655. return ctx->description.c_str();
  7656. }
  7657. static void ggml_backend_vk_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
  7658. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)device->context;
  7659. ggml_backend_vk_get_device_memory(ctx->device, free, total);
  7660. }
  7661. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_buffer_type(ggml_backend_dev_t dev) {
  7662. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7663. return ggml_backend_vk_buffer_type(ctx->device);
  7664. }
  7665. static ggml_backend_buffer_type_t ggml_backend_vk_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  7666. UNUSED(dev);
  7667. return ggml_backend_vk_host_buffer_type();
  7668. }
  7669. static enum ggml_backend_dev_type ggml_backend_vk_device_get_type(ggml_backend_dev_t dev) {
  7670. UNUSED(dev);
  7671. return GGML_BACKEND_DEVICE_TYPE_GPU;
  7672. }
  7673. static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  7674. props->name = ggml_backend_vk_device_get_name(dev);
  7675. props->description = ggml_backend_vk_device_get_description(dev);
  7676. props->type = ggml_backend_vk_device_get_type(dev);
  7677. ggml_backend_vk_device_get_memory(dev, &props->memory_free, &props->memory_total);
  7678. props->caps = {
  7679. /* .async = */ false,
  7680. /* .host_buffer = */ true,
  7681. /* .buffer_from_host_ptr = */ false,
  7682. /* .events = */ false,
  7683. };
  7684. }
  7685. static ggml_backend_t ggml_backend_vk_device_init(ggml_backend_dev_t dev, const char * params) {
  7686. UNUSED(params);
  7687. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7688. return ggml_backend_vk_init(ctx->device);
  7689. }
  7690. static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7691. switch (op->op) {
  7692. case GGML_OP_UNARY:
  7693. switch (ggml_get_unary_op(op)) {
  7694. case GGML_UNARY_OP_GELU:
  7695. case GGML_UNARY_OP_GELU_QUICK:
  7696. case GGML_UNARY_OP_SILU:
  7697. case GGML_UNARY_OP_RELU:
  7698. case GGML_UNARY_OP_TANH:
  7699. case GGML_UNARY_OP_SIGMOID:
  7700. return ggml_is_contiguous(op->src[0]) && op->src[0]->type == GGML_TYPE_F32;
  7701. default:
  7702. return false;
  7703. }
  7704. break;
  7705. case GGML_OP_MUL_MAT:
  7706. case GGML_OP_MUL_MAT_ID:
  7707. {
  7708. ggml_type src0_type = op->src[0]->type;
  7709. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7710. const vk_device& device = ggml_vk_get_device(ctx->device);
  7711. if (op->op == GGML_OP_MUL_MAT_ID && !device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
  7712. // If there's not enough shared memory for row_ids and the result tile, fallback to CPU
  7713. return false;
  7714. }
  7715. switch (src0_type) {
  7716. case GGML_TYPE_F32:
  7717. case GGML_TYPE_F16:
  7718. case GGML_TYPE_Q4_0:
  7719. case GGML_TYPE_Q4_1:
  7720. case GGML_TYPE_Q5_0:
  7721. case GGML_TYPE_Q5_1:
  7722. case GGML_TYPE_Q8_0:
  7723. case GGML_TYPE_Q2_K:
  7724. case GGML_TYPE_Q3_K:
  7725. case GGML_TYPE_Q4_K:
  7726. case GGML_TYPE_Q5_K:
  7727. case GGML_TYPE_Q6_K:
  7728. case GGML_TYPE_IQ1_S:
  7729. case GGML_TYPE_IQ1_M:
  7730. case GGML_TYPE_IQ2_XXS:
  7731. case GGML_TYPE_IQ2_XS:
  7732. case GGML_TYPE_IQ2_S:
  7733. case GGML_TYPE_IQ3_XXS:
  7734. case GGML_TYPE_IQ3_S:
  7735. case GGML_TYPE_IQ4_XS:
  7736. case GGML_TYPE_IQ4_NL:
  7737. break;
  7738. default:
  7739. return false;
  7740. }
  7741. struct ggml_tensor * a;
  7742. struct ggml_tensor * b;
  7743. if (op->op == GGML_OP_MUL_MAT) {
  7744. a = op->src[0];
  7745. b = op->src[1];
  7746. } else {
  7747. a = op->src[2];
  7748. b = op->src[1];
  7749. }
  7750. if (a->ne[3] != b->ne[3]) {
  7751. return false;
  7752. }
  7753. if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16) ||
  7754. !(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
  7755. return false;
  7756. }
  7757. return true;
  7758. } break;
  7759. case GGML_OP_FLASH_ATTN_EXT:
  7760. {
  7761. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7762. if (!ggml_vk_get_device(ctx->device)->coopmat2) {
  7763. return false;
  7764. }
  7765. switch (op->src[0]->ne[0]) {
  7766. case 64:
  7767. case 80:
  7768. case 96:
  7769. case 112:
  7770. case 128:
  7771. case 256:
  7772. break;
  7773. default:
  7774. return false;
  7775. }
  7776. if (op->src[1]->ne[0] != op->src[2]->ne[0]) {
  7777. // different head sizes of K and V are not supported yet
  7778. return false;
  7779. }
  7780. if (op->src[0]->type != GGML_TYPE_F32) {
  7781. return false;
  7782. }
  7783. if (op->type != GGML_TYPE_F32) {
  7784. return false;
  7785. }
  7786. if (op->src[3] && op->src[3]->type != GGML_TYPE_F16) {
  7787. return false;
  7788. }
  7789. // It's straightforward to support different K/V dequant, but would
  7790. // significantly increase the number of pipelines
  7791. if (op->src[1]->type != op->src[2]->type) {
  7792. return false;
  7793. }
  7794. switch (op->src[1]->type) {
  7795. case GGML_TYPE_F16:
  7796. case GGML_TYPE_Q4_0:
  7797. case GGML_TYPE_Q4_1:
  7798. case GGML_TYPE_Q5_0:
  7799. case GGML_TYPE_Q5_1:
  7800. case GGML_TYPE_Q8_0:
  7801. // K dequants currently disabled because D dimension is rounded up to 256 and runs inefficiently
  7802. //case GGML_TYPE_Q2_K:
  7803. //case GGML_TYPE_Q3_K:
  7804. //case GGML_TYPE_Q4_K:
  7805. //case GGML_TYPE_Q5_K:
  7806. //case GGML_TYPE_Q6_K:
  7807. //case GGML_TYPE_IQ1_S:
  7808. //case GGML_TYPE_IQ1_M:
  7809. //case GGML_TYPE_IQ2_XXS:
  7810. //case GGML_TYPE_IQ2_XS:
  7811. //case GGML_TYPE_IQ2_S:
  7812. //case GGML_TYPE_IQ3_XXS:
  7813. //case GGML_TYPE_IQ3_S:
  7814. //case GGML_TYPE_IQ4_XS:
  7815. case GGML_TYPE_IQ4_NL:
  7816. break;
  7817. default:
  7818. return false;
  7819. }
  7820. return true;
  7821. }
  7822. case GGML_OP_GET_ROWS:
  7823. {
  7824. switch (op->src[0]->type) {
  7825. case GGML_TYPE_F32:
  7826. case GGML_TYPE_F16:
  7827. case GGML_TYPE_Q4_0:
  7828. case GGML_TYPE_Q4_1:
  7829. case GGML_TYPE_Q5_0:
  7830. case GGML_TYPE_Q5_1:
  7831. case GGML_TYPE_Q8_0:
  7832. case GGML_TYPE_IQ1_S:
  7833. case GGML_TYPE_IQ1_M:
  7834. case GGML_TYPE_IQ2_XXS:
  7835. case GGML_TYPE_IQ2_XS:
  7836. case GGML_TYPE_IQ2_S:
  7837. case GGML_TYPE_IQ3_XXS:
  7838. case GGML_TYPE_IQ3_S:
  7839. case GGML_TYPE_IQ4_XS:
  7840. case GGML_TYPE_IQ4_NL:
  7841. return true;
  7842. default:
  7843. return false;
  7844. }
  7845. } break;
  7846. case GGML_OP_CONT:
  7847. case GGML_OP_CPY:
  7848. case GGML_OP_DUP:
  7849. {
  7850. ggml_type src0_type = op->src[0]->type;
  7851. ggml_type src1_type = op->src[1] != nullptr ? op->src[1]->type : src0_type;
  7852. if (src0_type == GGML_TYPE_F32) {
  7853. switch (src1_type) {
  7854. case GGML_TYPE_F32:
  7855. case GGML_TYPE_F16:
  7856. case GGML_TYPE_Q4_0:
  7857. case GGML_TYPE_Q4_1:
  7858. case GGML_TYPE_Q5_0:
  7859. case GGML_TYPE_Q5_1:
  7860. case GGML_TYPE_Q8_0:
  7861. case GGML_TYPE_IQ4_NL:
  7862. return true;
  7863. default:
  7864. break;
  7865. }
  7866. }
  7867. if (src1_type == GGML_TYPE_F32) {
  7868. switch (src0_type) {
  7869. case GGML_TYPE_Q4_0:
  7870. case GGML_TYPE_Q4_1:
  7871. case GGML_TYPE_Q5_0:
  7872. case GGML_TYPE_Q5_1:
  7873. case GGML_TYPE_Q8_0:
  7874. case GGML_TYPE_IQ4_NL:
  7875. return true;
  7876. default:
  7877. break;
  7878. }
  7879. }
  7880. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  7881. return true;
  7882. }
  7883. return false;
  7884. } break;
  7885. case GGML_OP_REPEAT:
  7886. return ggml_type_size(op->type) == sizeof(float) && ggml_type_size(op->src[0]->type) == sizeof(float);
  7887. case GGML_OP_REPEAT_BACK:
  7888. return op->type == GGML_TYPE_F32 && op->src[0]->type == GGML_TYPE_F32;
  7889. case GGML_OP_ROPE:
  7890. case GGML_OP_ROPE_BACK:
  7891. case GGML_OP_NONE:
  7892. case GGML_OP_RESHAPE:
  7893. case GGML_OP_VIEW:
  7894. case GGML_OP_PERMUTE:
  7895. case GGML_OP_TRANSPOSE:
  7896. return true;
  7897. case GGML_OP_NORM:
  7898. case GGML_OP_GROUP_NORM:
  7899. case GGML_OP_RMS_NORM:
  7900. case GGML_OP_L2_NORM:
  7901. return ggml_is_contiguous(op->src[0]);
  7902. case GGML_OP_ADD:
  7903. case GGML_OP_SUB:
  7904. case GGML_OP_MUL:
  7905. case GGML_OP_DIV:
  7906. case GGML_OP_SILU_BACK:
  7907. case GGML_OP_RMS_NORM_BACK:
  7908. case GGML_OP_SQR:
  7909. case GGML_OP_SIN:
  7910. case GGML_OP_COS:
  7911. case GGML_OP_CLAMP:
  7912. return op->src[0]->type == GGML_TYPE_F32;
  7913. case GGML_OP_ACC:
  7914. case GGML_OP_CONCAT:
  7915. case GGML_OP_UPSCALE:
  7916. case GGML_OP_SCALE:
  7917. case GGML_OP_PAD:
  7918. case GGML_OP_DIAG_MASK_INF:
  7919. case GGML_OP_SOFT_MAX:
  7920. case GGML_OP_SOFT_MAX_BACK:
  7921. case GGML_OP_ARGSORT:
  7922. case GGML_OP_SUM:
  7923. case GGML_OP_SUM_ROWS:
  7924. case GGML_OP_ARGMAX:
  7925. case GGML_OP_COUNT_EQUAL:
  7926. case GGML_OP_IM2COL:
  7927. case GGML_OP_TIMESTEP_EMBEDDING:
  7928. case GGML_OP_POOL_2D:
  7929. case GGML_OP_RWKV_WKV6:
  7930. case GGML_OP_RWKV_WKV7:
  7931. case GGML_OP_LEAKY_RELU:
  7932. case GGML_OP_OPT_STEP_ADAMW:
  7933. return true;
  7934. default:
  7935. return false;
  7936. }
  7937. UNUSED(dev);
  7938. }
  7939. static bool ggml_backend_vk_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  7940. if (buft->iface.get_name != ggml_backend_vk_buffer_type_name) {
  7941. return false;
  7942. }
  7943. ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
  7944. ggml_backend_vk_buffer_type_context * buft_ctx = (ggml_backend_vk_buffer_type_context *)buft->context;
  7945. return buft_ctx->device->idx == ctx->device;
  7946. }
  7947. static bool ggml_backend_vk_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  7948. const int min_batch_size = 32;
  7949. return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
  7950. (op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
  7951. UNUSED(dev);
  7952. }
  7953. static const struct ggml_backend_device_i ggml_backend_vk_device_i = {
  7954. /* .get_name = */ ggml_backend_vk_device_get_name,
  7955. /* .get_description = */ ggml_backend_vk_device_get_description,
  7956. /* .get_memory = */ ggml_backend_vk_device_get_memory,
  7957. /* .get_type = */ ggml_backend_vk_device_get_type,
  7958. /* .get_props = */ ggml_backend_vk_device_get_props,
  7959. /* .init_backend = */ ggml_backend_vk_device_init,
  7960. /* .get_buffer_type = */ ggml_backend_vk_device_get_buffer_type,
  7961. /* .get_host_buffer_type = */ ggml_backend_vk_device_get_host_buffer_type,
  7962. /* .buffer_from_host_ptr = */ NULL,
  7963. /* .supports_op = */ ggml_backend_vk_device_supports_op,
  7964. /* .supports_buft = */ ggml_backend_vk_device_supports_buft,
  7965. /* .offload_op = */ ggml_backend_vk_device_offload_op,
  7966. /* .event_new = */ NULL,
  7967. /* .event_free = */ NULL,
  7968. /* .event_synchronize = */ NULL,
  7969. };
  7970. static const char * ggml_backend_vk_reg_get_name(ggml_backend_reg_t reg) {
  7971. UNUSED(reg);
  7972. return GGML_VK_NAME;
  7973. }
  7974. static size_t ggml_backend_vk_reg_get_device_count(ggml_backend_reg_t reg) {
  7975. UNUSED(reg);
  7976. return ggml_backend_vk_get_device_count();
  7977. }
  7978. static ggml_backend_dev_t ggml_backend_vk_reg_get_device(ggml_backend_reg_t reg, size_t device) {
  7979. static std::vector<ggml_backend_dev_t> devices;
  7980. static bool initialized = false;
  7981. {
  7982. static std::mutex mutex;
  7983. std::lock_guard<std::mutex> lock(mutex);
  7984. if (!initialized) {
  7985. for (int i = 0; i < ggml_backend_vk_get_device_count(); i++) {
  7986. ggml_backend_vk_device_context * ctx = new ggml_backend_vk_device_context;
  7987. char desc[256];
  7988. ggml_backend_vk_get_device_description(i, desc, sizeof(desc));
  7989. ctx->device = i;
  7990. ctx->name = GGML_VK_NAME + std::to_string(i);
  7991. ctx->description = desc;
  7992. devices.push_back(new ggml_backend_device {
  7993. /* .iface = */ ggml_backend_vk_device_i,
  7994. /* .reg = */ reg,
  7995. /* .context = */ ctx,
  7996. });
  7997. }
  7998. initialized = true;
  7999. }
  8000. }
  8001. GGML_ASSERT(device < devices.size());
  8002. return devices[device];
  8003. }
  8004. static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
  8005. /* .get_name = */ ggml_backend_vk_reg_get_name,
  8006. /* .get_device_count = */ ggml_backend_vk_reg_get_device_count,
  8007. /* .get_device = */ ggml_backend_vk_reg_get_device,
  8008. /* .get_proc_address = */ NULL,
  8009. };
  8010. ggml_backend_reg_t ggml_backend_vk_reg() {
  8011. static ggml_backend_reg reg = {
  8012. /* .api_version = */ GGML_BACKEND_API_VERSION,
  8013. /* .iface = */ ggml_backend_vk_reg_i,
  8014. /* .context = */ nullptr,
  8015. };
  8016. try {
  8017. ggml_vk_instance_init();
  8018. return &reg;
  8019. } catch (const vk::SystemError& e) {
  8020. VK_LOG_DEBUG("ggml_backend_vk_reg() -> Error: System error: " << e.what());
  8021. return nullptr;
  8022. }
  8023. }
  8024. // Extension availability
  8025. static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  8026. #ifdef GGML_VULKAN_VALIDATE
  8027. bool portability_enumeration_ext = false;
  8028. // Check for portability enumeration extension for MoltenVK support
  8029. for (const auto& properties : instance_extensions) {
  8030. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  8031. return true;
  8032. }
  8033. }
  8034. if (!portability_enumeration_ext) {
  8035. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  8036. }
  8037. #endif
  8038. return false;
  8039. UNUSED(instance_extensions);
  8040. }
  8041. static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions) {
  8042. #ifdef __APPLE__
  8043. bool portability_enumeration_ext = false;
  8044. // Check for portability enumeration extension for MoltenVK support
  8045. for (const auto& properties : instance_extensions) {
  8046. if (strcmp("VK_KHR_portability_enumeration", properties.extensionName) == 0) {
  8047. return true;
  8048. }
  8049. }
  8050. if (!portability_enumeration_ext) {
  8051. std::cerr << "ggml_vulkan: WARNING: Instance extension VK_KHR_portability_enumeration not found." << std::endl;
  8052. }
  8053. #endif
  8054. return false;
  8055. UNUSED(instance_extensions);
  8056. }
  8057. static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
  8058. switch (props.vendorID) {
  8059. case VK_VENDOR_ID_INTEL:
  8060. // Intel drivers don't support coopmat properly yet
  8061. return false;
  8062. case VK_VENDOR_ID_AMD:
  8063. if (driver_props.driverID == vk::DriverId::eAmdProprietary || driver_props.driverID == vk::DriverId::eAmdOpenSource) {
  8064. // Workaround for AMD proprietary driver reporting support on all GPUs
  8065. return arch == vk_device_architecture::AMD_RDNA3;
  8066. }
  8067. return true;
  8068. default:
  8069. return true;
  8070. }
  8071. }
  8072. // checks
  8073. #ifdef GGML_VULKAN_CHECK_RESULTS
  8074. static void ggml_vk_print_graph_origin(const ggml_tensor * tensor, std::vector<const ggml_tensor *>& done, int level = 0) {
  8075. if (std::find(done.begin(), done.end(), tensor) != done.end() || level > 10) {
  8076. return;
  8077. }
  8078. for (int j = 0; j < level; j++) {
  8079. std::cerr << " ";
  8080. }
  8081. std::cerr << ggml_op_name(tensor->op) << " gpu=" << (tensor->extra != nullptr) << std::endl;
  8082. done.push_back(tensor);
  8083. for (int i = 0; i < GGML_MAX_SRC; i++) {
  8084. if (tensor->src[i] != nullptr) {
  8085. ggml_vk_print_graph_origin(tensor->src[i], done, level + 1);
  8086. }
  8087. }
  8088. }
  8089. static void ggml_vk_print_tensor_area(const ggml_tensor * tensor, const void * data, int i0, int i1, int i2, int i3) {
  8090. if (tensor->type != GGML_TYPE_F32 && tensor->type != GGML_TYPE_F16 && tensor->type != GGML_TYPE_I32) {
  8091. return;
  8092. }
  8093. i0 = std::max(i0, 5);
  8094. i1 = std::max(i1, 5);
  8095. i2 = std::max(i2, 0);
  8096. i3 = std::max(i3, 0);
  8097. fprintf(stderr, " ");
  8098. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8099. fprintf(stderr, "%7d ", idx1);
  8100. }
  8101. fprintf(stderr, "\n");
  8102. for (int idx0 = i0 - 5; idx0 < i0 + 5; idx0++) {
  8103. fprintf(stderr, "%7d: ", idx0);
  8104. for (int idx1 = i1 - 5; idx1 < i1 + 5; idx1++) {
  8105. 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]) {
  8106. float val;
  8107. if (tensor->type == GGML_TYPE_F32) {
  8108. val = *(const float *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  8109. } else if (tensor->type == GGML_TYPE_F16) {
  8110. 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]));
  8111. } else if (tensor->type == GGML_TYPE_I32) {
  8112. val = *(const int32_t *) ((const char *) data + i3*tensor->nb[3] + i2*tensor->nb[2] + idx1*tensor->nb[1] + idx0*tensor->nb[0]);
  8113. } else {
  8114. GGML_ABORT("fatal error");
  8115. }
  8116. fprintf(stderr, "% 7.2f ", val);
  8117. } else {
  8118. fprintf(stderr, " ");
  8119. }
  8120. }
  8121. fprintf(stderr, "\n");
  8122. }
  8123. }
  8124. static void ggml_vk_print_tensor(const ggml_tensor * tensor, const char * name) {
  8125. void * tensor_data = tensor->data;
  8126. const bool is_gpu = tensor->buffer != nullptr && ggml_backend_buffer_is_vk(tensor->buffer);
  8127. if (is_gpu) {
  8128. const size_t tensor_size = ggml_nbytes(tensor);
  8129. tensor_data = malloc(tensor_size);
  8130. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8131. vk_buffer buffer_gpu = buf_ctx->dev_buffer;
  8132. ggml_vk_buffer_read(buffer_gpu, vk_tensor_offset(tensor) + tensor->view_offs, tensor_data, tensor_size);
  8133. }
  8134. std::cerr << "TENSOR CHECK " << name << " (" << tensor->name << "): " << ggml_op_name(tensor->op) << std::endl;
  8135. std::cerr << "tensor=" << tensor << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << std::endl;
  8136. if (tensor->src[0] != nullptr) {
  8137. std::cerr << "tensor->src[0]=" << tensor->src[0] << " name=" << tensor->src[0]->name << " op=" << ggml_op_name(tensor->src[0]->op) << " type=" << ggml_type_name(tensor->src[0]->type) << " ne0=" << tensor->src[0]->ne[0] << " nb0=" << tensor->src[0]->nb[0] << " ne1=" << tensor->src[0]->ne[1] << " nb1=" << tensor->src[0]->nb[1] << " ne2=" << tensor->src[0]->ne[2] << " nb2=" << tensor->src[0]->nb[2] << " ne3=" << tensor->src[0]->ne[3] << " nb3=" << tensor->src[0]->nb[3] << std::endl;
  8138. }
  8139. if (tensor->src[1] != nullptr) {
  8140. std::cerr << "tensor->src[1]=" << tensor->src[1] << " name=" << tensor->src[1]->name << " op=" << ggml_op_name(tensor->src[1]->op) << " type=" << ggml_type_name(tensor->src[1]->type) << " ne0=" << tensor->src[1]->ne[0] << " nb0=" << tensor->src[1]->nb[0] << " ne1=" << tensor->src[1]->ne[1] << " nb1=" << tensor->src[1]->nb[1] << " ne2=" << tensor->src[1]->ne[2] << " nb2=" << tensor->src[1]->nb[2] << " ne3=" << tensor->src[1]->ne[3] << " nb3=" << tensor->src[1]->nb[3] << std::endl;
  8141. }
  8142. std::cerr << std::endl << "Result:" << std::endl;
  8143. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  8144. std::cerr << std::endl;
  8145. std::vector<const ggml_tensor *> done;
  8146. ggml_vk_print_graph_origin(tensor, done);
  8147. if (is_gpu) {
  8148. free(tensor_data);
  8149. }
  8150. }
  8151. void * comp_result;
  8152. size_t comp_size;
  8153. size_t comp_nb[GGML_MAX_DIMS];
  8154. size_t check_counter = 0;
  8155. static void ggml_vk_check_results_0(ggml_tensor * tensor) {
  8156. if (tensor->op == GGML_OP_TRANSPOSE) {
  8157. return;
  8158. }
  8159. check_counter++;
  8160. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  8161. return;
  8162. }
  8163. VK_LOG_DEBUG("ggml_vk_check_results_0(" << tensor->name << ")");
  8164. ggml_tensor * src0 = tensor->src[0];
  8165. ggml_tensor * src1 = tensor->src[1];
  8166. struct ggml_init_params iparams = {
  8167. /*.mem_size =*/ 2ul*1024ul*1024ul*1024ul,
  8168. /*.mem_buffer =*/ NULL,
  8169. /*.no_alloc =*/ false,
  8170. };
  8171. struct ggml_context * ggml_ctx = ggml_init(iparams);
  8172. std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  8173. std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
  8174. std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
  8175. const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
  8176. struct ggml_tensor * tensor_clone = nullptr;
  8177. for (int i = 0; i < 6; i++) {
  8178. ggml_tensor * srci = tensor->src[i];
  8179. if (srci == nullptr) {
  8180. continue;
  8181. }
  8182. ggml_tensor * srci_clone = ggml_dup_tensor(ggml_ctx, srci);
  8183. size_t srci_size = ggml_nbytes(srci);
  8184. src_clone[i] = srci_clone;
  8185. src_size[i] = ggml_nbytes(srci);
  8186. src_buffer[i] = malloc(srci_size);
  8187. srci_clone->data = src_buffer[i];
  8188. if (ggml_backend_buffer_is_host(srci->buffer)) {
  8189. memcpy(srci_clone->data, srci->data, srci_size);
  8190. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8191. } else if (ggml_backend_buffer_is_vk(srci->buffer)) {
  8192. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)srci->buffer->context;
  8193. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  8194. uint64_t offset = vk_tensor_offset(srci) + srci->view_offs;
  8195. if (!ggml_is_contiguous(srci) && ggml_vk_dim01_contiguous(srci)) {
  8196. for (int i3 = 0; i3 < srci->ne[3]; i3++) {
  8197. for (int i2 = 0; i2 < srci->ne[2]; i2++) {
  8198. const int idx = i3*srci->ne[2] + i2;
  8199. ggml_vk_buffer_read(buffer_gpu, offset + idx * srci->nb[2], ((char *)srci_clone->data + idx * srci_clone->nb[2]), srci->ne[1] * srci->nb[1]);
  8200. }
  8201. }
  8202. srci_clone->nb[0] = srci->nb[0];
  8203. srci_clone->nb[1] = srci->nb[1];
  8204. for (int i = 2; i < GGML_MAX_DIMS; i++) {
  8205. srci_clone->nb[i] = srci_clone->nb[i - 1]*srci_clone->ne[i - 1];
  8206. }
  8207. } else {
  8208. if (offset + srci_size >= buffer_gpu->size) {
  8209. srci_size = buffer_gpu->size - offset;
  8210. }
  8211. ggml_vk_buffer_read(buffer_gpu, offset, srci_clone->data, srci_size);
  8212. memcpy(srci_clone->nb, srci->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8213. }
  8214. } else {
  8215. GGML_ABORT("fatal error");
  8216. }
  8217. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8218. ggml_vk_print_tensor(srci, srci_name[i]);
  8219. }
  8220. }
  8221. if (tensor->op == GGML_OP_FLASH_ATTN_EXT) {
  8222. const float * params = (const float *)tensor->op_params;
  8223. tensor_clone = ggml_flash_attn_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], params[0], params[1], params[2]);
  8224. } else if (tensor->op == GGML_OP_MUL_MAT) {
  8225. tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
  8226. } else if (tensor->op == GGML_OP_MUL_MAT_ID) {
  8227. tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
  8228. } else if (tensor->op == GGML_OP_SUB) {
  8229. tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
  8230. } else if (tensor->op == GGML_OP_MUL) {
  8231. tensor_clone = ggml_mul(ggml_ctx, src_clone[0], src_clone[1]);
  8232. } else if (tensor->op == GGML_OP_DIV) {
  8233. tensor_clone = ggml_div(ggml_ctx, src_clone[0], src_clone[1]);
  8234. } else if (tensor->op == GGML_OP_CONCAT) {
  8235. tensor_clone = ggml_concat(ggml_ctx, src_clone[0], src_clone[1], *(int *)tensor->op_params);
  8236. } else if (tensor->op == GGML_OP_UPSCALE) {
  8237. tensor_clone = ggml_upscale_ext(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  8238. } else if (tensor->op == GGML_OP_SCALE) {
  8239. const float * params = (const float *)tensor->op_params;
  8240. tensor_clone = ggml_scale(ggml_ctx, src_clone[0], params[0]);
  8241. } else if (tensor->op == GGML_OP_SQR) {
  8242. tensor_clone = ggml_sqr(ggml_ctx, src_clone[0]);
  8243. } else if (tensor->op == GGML_OP_SIN) {
  8244. tensor_clone = ggml_sin(ggml_ctx, src_clone[0]);
  8245. } else if (tensor->op == GGML_OP_COS) {
  8246. tensor_clone = ggml_cos(ggml_ctx, src_clone[0]);
  8247. } else if (tensor->op == GGML_OP_CLAMP) {
  8248. const float * params = (const float *)tensor->op_params;
  8249. tensor_clone = ggml_clamp(ggml_ctx, src_clone[0], params[0], params[1]);
  8250. } else if (tensor->op == GGML_OP_PAD) {
  8251. tensor_clone = ggml_pad(ggml_ctx, src_clone[0], tensor->ne[0] - src_clone[0]->ne[0], tensor->ne[1] - src_clone[0]->ne[1], tensor->ne[2] - src_clone[0]->ne[2], tensor->ne[3] - src_clone[0]->ne[3]);
  8252. } else if (tensor->op == GGML_OP_REPEAT) {
  8253. tensor_clone = ggml_repeat(ggml_ctx, src_clone[0], tensor);
  8254. } else if (tensor->op == GGML_OP_REPEAT_BACK) {
  8255. tensor_clone = ggml_repeat_back(ggml_ctx, src_clone[0], tensor);
  8256. } else if (tensor->op == GGML_OP_ADD) {
  8257. tensor_clone = ggml_add(ggml_ctx, src_clone[0], src_clone[1]);
  8258. } else if (tensor->op == GGML_OP_ACC) {
  8259. tensor_clone = ggml_acc(ggml_ctx, src_clone[0], src_clone[1], tensor->op_params[0], tensor->op_params[1], tensor->op_params[2], tensor->op_params[3]);
  8260. } else if (tensor->op == GGML_OP_NORM) {
  8261. tensor_clone = ggml_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  8262. } else if (tensor->op == GGML_OP_GROUP_NORM) {
  8263. const float * float_params = (const float *)tensor->op_params;
  8264. tensor_clone = ggml_group_norm(ggml_ctx, src_clone[0], tensor->op_params[0], float_params[1]);
  8265. } else if (tensor->op == GGML_OP_RMS_NORM) {
  8266. tensor_clone = ggml_rms_norm(ggml_ctx, src_clone[0], *(float *)tensor->op_params);
  8267. } else if (tensor->op == GGML_OP_RMS_NORM_BACK) {
  8268. const float eps = ((float *) tensor->op_params)[0];
  8269. tensor_clone = ggml_rms_norm_back(ggml_ctx, src_clone[0], src_clone[1], eps);
  8270. } else if (tensor->op == GGML_OP_SILU_BACK) {
  8271. tensor_clone = ggml_silu_back(ggml_ctx, src_clone[0], src_clone[1]);
  8272. } else if (tensor->op == GGML_OP_L2_NORM) {
  8273. const float eps = ((float *) tensor->op_params)[0];
  8274. tensor_clone = ggml_l2_norm(ggml_ctx, src_clone[0], eps);
  8275. } else if (tensor->op == GGML_OP_SOFT_MAX) {
  8276. if (src1 != nullptr) {
  8277. const float * params = (const float *)tensor->op_params;
  8278. tensor_clone = ggml_soft_max_ext(ggml_ctx, src_clone[0], src_clone[1], params[0], params[1]);
  8279. } else {
  8280. tensor_clone = ggml_soft_max(ggml_ctx, src_clone[0]);
  8281. }
  8282. } else if (tensor->op == GGML_OP_SOFT_MAX_BACK) {
  8283. tensor_clone = ggml_soft_max_ext_back(ggml_ctx, src_clone[0], src_clone[1], ((float *)tensor->op_params)[0], ((float *)tensor->op_params)[1]);
  8284. } else if (tensor->op == GGML_OP_DIAG_MASK_INF) {
  8285. tensor_clone = ggml_diag_mask_inf(ggml_ctx, src_clone[0], tensor->op_params[0]);
  8286. } else if (tensor->op == GGML_OP_ROPE || tensor->op == GGML_OP_ROPE_BACK) {
  8287. const int n_dims = ((int32_t *) tensor->op_params)[1];
  8288. const int mode = ((int32_t *) tensor->op_params)[2];
  8289. //const int n_ctx_ggml = ((int32_t *) tensor->op_params)[3];
  8290. const int n_ctx_orig_ggml = ((int32_t *) tensor->op_params)[4];
  8291. const float freq_base = ((float *) tensor->op_params)[5];
  8292. const float freq_scale = ((float *) tensor->op_params)[6];
  8293. const float ext_factor = ((float *) tensor->op_params)[7];
  8294. const float attn_factor = ((float *) tensor->op_params)[8];
  8295. const float beta_fast = ((float *) tensor->op_params)[9];
  8296. const float beta_slow = ((float *) tensor->op_params)[10];
  8297. if (mode & GGML_ROPE_TYPE_MROPE) {
  8298. int32_t *sections = ((int32_t *) tensor->op_params) + 11;
  8299. if (tensor->op == GGML_OP_ROPE) {
  8300. tensor_clone = ggml_rope_multi(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  8301. } else {
  8302. tensor_clone = ggml_rope_multi_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, sections, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  8303. }
  8304. } else {
  8305. if (tensor->op == GGML_OP_ROPE) {
  8306. tensor_clone = ggml_rope_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  8307. } else {
  8308. tensor_clone = ggml_rope_ext_back(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], n_dims, mode, n_ctx_orig_ggml, freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
  8309. }
  8310. }
  8311. } else if (tensor->op == GGML_OP_UNARY) {
  8312. switch (ggml_get_unary_op(tensor)) {
  8313. case GGML_UNARY_OP_SILU:
  8314. tensor_clone = ggml_silu(ggml_ctx, src_clone[0]);
  8315. break;
  8316. case GGML_UNARY_OP_GELU:
  8317. tensor_clone = ggml_gelu(ggml_ctx, src_clone[0]);
  8318. break;
  8319. case GGML_UNARY_OP_GELU_QUICK:
  8320. tensor_clone = ggml_gelu_quick(ggml_ctx, src_clone[0]);
  8321. break;
  8322. case GGML_UNARY_OP_RELU:
  8323. tensor_clone = ggml_relu(ggml_ctx, src_clone[0]);
  8324. break;
  8325. case GGML_UNARY_OP_TANH:
  8326. tensor_clone = ggml_tanh(ggml_ctx, src_clone[0]);
  8327. break;
  8328. case GGML_UNARY_OP_SIGMOID:
  8329. tensor_clone = ggml_sigmoid(ggml_ctx, src_clone[0]);
  8330. break;
  8331. default:
  8332. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  8333. GGML_ABORT("fatal error");
  8334. }
  8335. } else if (tensor->op == GGML_OP_CPY || tensor->op == GGML_OP_DUP) {
  8336. if (src1 == nullptr) {
  8337. tensor_clone = ggml_dup(ggml_ctx, src_clone[0]);
  8338. tensor_clone->type = tensor->type;
  8339. } else {
  8340. tensor_clone = ggml_cpy(ggml_ctx, src_clone[0], src_clone[1]);
  8341. }
  8342. } else if (tensor->op == GGML_OP_CONT) {
  8343. tensor_clone = ggml_cont_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  8344. } else if (tensor->op == GGML_OP_RESHAPE) {
  8345. tensor_clone = ggml_reshape_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
  8346. } else if (tensor->op == GGML_OP_VIEW) {
  8347. tensor_clone = ggml_view_4d(ggml_ctx, src_clone[0], tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3], tensor->nb[1], tensor->nb[2], tensor->nb[3], ((int32_t *) tensor->op_params)[0]);
  8348. } else if (tensor->op == GGML_OP_PERMUTE) {
  8349. int32_t * params = (int32_t *)tensor->op_params;
  8350. tensor_clone = ggml_permute(ggml_ctx, src_clone[0], params[0], params[1], params[2], params[3]);
  8351. } else if (tensor->op == GGML_OP_TRANSPOSE) {
  8352. tensor_clone = ggml_transpose(ggml_ctx, src_clone[0]);
  8353. } else if (tensor->op == GGML_OP_GET_ROWS) {
  8354. tensor_clone = ggml_get_rows(ggml_ctx, src_clone[0], src_clone[1]);
  8355. } else if (tensor->op == GGML_OP_ARGSORT) {
  8356. tensor_clone = ggml_argsort(ggml_ctx, src_clone[0], (ggml_sort_order) *(int *)tensor->op_params);
  8357. } else if (tensor->op == GGML_OP_SUM) {
  8358. tensor_clone = ggml_sum(ggml_ctx, src_clone[0]);
  8359. } else if (tensor->op == GGML_OP_SUM_ROWS) {
  8360. tensor_clone = ggml_sum_rows(ggml_ctx, src_clone[0]);
  8361. } else if (tensor->op == GGML_OP_ARGMAX) {
  8362. tensor_clone = ggml_argmax(ggml_ctx, src_clone[0]);
  8363. } else if (tensor->op == GGML_OP_COUNT_EQUAL) {
  8364. tensor_clone = ggml_count_equal(ggml_ctx, src_clone[0], src_clone[1]);
  8365. } else if (tensor->op == GGML_OP_IM2COL) {
  8366. const int32_t s0 = tensor->op_params[0];
  8367. const int32_t s1 = tensor->op_params[1];
  8368. const int32_t p0 = tensor->op_params[2];
  8369. const int32_t p1 = tensor->op_params[3];
  8370. const int32_t d0 = tensor->op_params[4];
  8371. const int32_t d1 = tensor->op_params[5];
  8372. const bool is_2D = tensor->op_params[6] == 1;
  8373. tensor_clone = ggml_im2col(ggml_ctx, src_clone[0], src_clone[1], s0, s1, p0, p1, d0, d1, is_2D, tensor->type);
  8374. } else if (tensor->op == GGML_OP_TIMESTEP_EMBEDDING) {
  8375. const int32_t dim = tensor->op_params[0];
  8376. const int32_t max_period = tensor->op_params[1];
  8377. tensor_clone = ggml_timestep_embedding(ggml_ctx, src_clone[0], dim, max_period);
  8378. } else if (tensor->op == GGML_OP_POOL_2D) {
  8379. enum ggml_op_pool op = static_cast<ggml_op_pool>(tensor->op_params[0]);
  8380. const int32_t k0 = tensor->op_params[1];
  8381. const int32_t k1 = tensor->op_params[2];
  8382. const int32_t s0 = tensor->op_params[3];
  8383. const int32_t s1 = tensor->op_params[4];
  8384. const int32_t p0 = tensor->op_params[5];
  8385. const int32_t p1 = tensor->op_params[6];
  8386. tensor_clone = ggml_pool_2d(ggml_ctx, src_clone[0], op, k0, k1, s0, s1, p0, p1);
  8387. } else if (tensor->op == GGML_OP_LEAKY_RELU) {
  8388. const float * op_params = (const float *)tensor->op_params;
  8389. tensor_clone = ggml_leaky_relu(ggml_ctx, src_clone[0], op_params[0], false);
  8390. } else if (tensor->op == GGML_OP_RWKV_WKV6) {
  8391. tensor_clone = ggml_rwkv_wkv6(ggml_ctx, src_clone[0], src_clone[1],
  8392. src_clone[2], src_clone[3], src_clone[4], src_clone[5]);
  8393. } else if (tensor->op == GGML_OP_RWKV_WKV7) {
  8394. tensor_clone = ggml_rwkv_wkv7(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3],
  8395. src_clone[4], src_clone[5], src_clone[6]);
  8396. } else if (tensor->op == GGML_OP_OPT_STEP_ADAMW) {
  8397. src_clone[0]->flags = src0->flags;
  8398. tensor_clone = ggml_opt_step_adamw(ggml_ctx, src_clone[0], src_clone[1],
  8399. src_clone[2], src_clone[3], src_clone[4]);
  8400. }
  8401. else {
  8402. std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
  8403. GGML_ABORT("fatal error");
  8404. }
  8405. ggml_cgraph * cgraph = ggml_new_graph(ggml_ctx);
  8406. ggml_build_forward_expand(cgraph, tensor_clone);
  8407. ggml_graph_compute_with_ctx(ggml_ctx, cgraph, 8);
  8408. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8409. ggml_vk_print_tensor(tensor_clone, "tensor_clone");
  8410. }
  8411. comp_size = ggml_nbytes(tensor_clone);
  8412. comp_result = malloc(comp_size);
  8413. memcpy(comp_result, tensor_clone->data, comp_size);
  8414. memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
  8415. for (int i = 0; i < 6; i++) {
  8416. if (src_buffer[i] != nullptr) {
  8417. free(src_buffer[i]);
  8418. }
  8419. }
  8420. ggml_free(ggml_ctx);
  8421. VK_LOG_DEBUG("END ggml_vk_check_results_0(" << tensor->name << ")");
  8422. }
  8423. static void ggml_vk_check_results_1(ggml_tensor * tensor) {
  8424. if (tensor->op == GGML_OP_TRANSPOSE) {
  8425. return;
  8426. }
  8427. if (!(vk_output_tensor > 0 && vk_output_tensor == check_counter) && check_counter <= vk_skip_checks) {
  8428. return;
  8429. }
  8430. VK_LOG_DEBUG("ggml_vk_check_results_1(" << tensor->name << ")");
  8431. ggml_tensor * src0 = tensor->src[0];
  8432. ggml_tensor * src1 = tensor->src[1];
  8433. ggml_tensor * src2 = tensor->src[2];
  8434. ggml_tensor * src3 = tensor->src[3];
  8435. void * tensor_data = tensor->data;
  8436. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8437. size_t tensor_size = ggml_nbytes(tensor);
  8438. tensor_data = malloc(tensor_size);
  8439. ggml_backend_vk_buffer_context * buf_ctx = (ggml_backend_vk_buffer_context *)tensor->buffer->context;
  8440. vk_buffer& buffer_gpu = buf_ctx->dev_buffer;
  8441. uint64_t offset = vk_tensor_offset(tensor) + tensor->view_offs;
  8442. if (offset + tensor_size >= buffer_gpu->size) {
  8443. tensor_size = buffer_gpu->size - offset;
  8444. }
  8445. ggml_vk_buffer_read(buffer_gpu, offset, tensor_data, tensor_size);
  8446. }
  8447. float first_error_result = -1.0f;
  8448. float first_error_correct = -1.0f;
  8449. std::array<int, 4> first_error = { -1, -1, -1, -1 };
  8450. double avg_err = 0.0;
  8451. size_t counter = 0;
  8452. for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
  8453. for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
  8454. for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
  8455. for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
  8456. const bool buffer_size_fit = i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0] < comp_size;
  8457. float correct = 0.0f;
  8458. float result = 0.0f;
  8459. if (buffer_size_fit) {
  8460. if (tensor->type == GGML_TYPE_F32) {
  8461. correct = *(float *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8462. result = *(float *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8463. } else if (tensor->type == GGML_TYPE_F16) {
  8464. 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]));
  8465. 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]));
  8466. } else if (tensor->type == GGML_TYPE_I32) {
  8467. correct = *(int32_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8468. result = *(int32_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8469. } else if (tensor->type == GGML_TYPE_I64) {
  8470. correct = *(int64_t *) ((char *) comp_result + i3*comp_nb[3] + i2*comp_nb[2] + i1*comp_nb[1] + i0*comp_nb[0]);
  8471. result = *(int64_t *) ((char *) tensor_data + i3*tensor->nb[3] + i2*tensor->nb[2] + i1*tensor->nb[1] + i0*tensor->nb[0]);
  8472. } else {
  8473. std::cerr << "Results check not implemented for type " << ggml_type_name(tensor->type) << std::endl;
  8474. }
  8475. } else {
  8476. std::cerr << "Missing debug code for type " << ggml_type_name(tensor->type) << std::endl;
  8477. GGML_ABORT("fatal error");
  8478. }
  8479. if ((std::isnan(correct) != std::isnan(result)) || (std::isinf(correct) != std::isinf(result)) || !buffer_size_fit) {
  8480. 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;
  8481. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  8482. if (src0 != nullptr) {
  8483. std::cerr << "src0=" << src0 << " src0->name=" << src0->name << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  8484. }
  8485. if (src1 != nullptr) {
  8486. std::cerr << "src1=" << src1 << " src1->name=" << src1->name << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  8487. }
  8488. if (src2 != nullptr) {
  8489. std::cerr << "src2=" << src2 << " src2->name=" << src2->name << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  8490. }
  8491. if (src3 != nullptr) {
  8492. std::cerr << "src3=" << src3 << " src3->name=" << src3->name << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  8493. }
  8494. 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;
  8495. std::cerr << std::endl << "Result:" << std::endl;
  8496. ggml_vk_print_tensor_area(tensor, tensor_data, i0, i1, i2, i3);
  8497. std::cerr << std::endl << "Correct:" << std::endl;
  8498. ggml_vk_print_tensor_area(tensor, comp_result, i0, i1, i2, i3);
  8499. std::cerr << std::endl;
  8500. std::vector<const ggml_tensor *> done;
  8501. ggml_vk_print_graph_origin(tensor, done);
  8502. GGML_ABORT("fatal error");
  8503. }
  8504. if (first_error[0] == -1 && std::fabs(correct - result) > 0.1f) {
  8505. first_error[0] = i0;
  8506. first_error[1] = i1;
  8507. first_error[2] = i2;
  8508. first_error[3] = i3;
  8509. first_error_result = result;
  8510. first_error_correct = correct;
  8511. }
  8512. // Special case, value is infinite, avoid NaN result in avg_err
  8513. // NaN also appears in results, if both are nan error is 0
  8514. if (!std::isinf(correct) && !std::isinf(result) && !std::isnan(correct) && !std::isnan(result)) {
  8515. avg_err += std::fabs(correct - result);
  8516. }
  8517. counter++;
  8518. }
  8519. }
  8520. }
  8521. }
  8522. avg_err /= counter;
  8523. if (vk_output_tensor > 0 && vk_output_tensor == check_counter) {
  8524. std::cerr << "TENSOR CHECK: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8525. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  8526. if (src0 != nullptr) {
  8527. std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  8528. }
  8529. if (src1 != nullptr) {
  8530. std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  8531. }
  8532. if (src2 != nullptr) {
  8533. std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  8534. }
  8535. if (src3 != nullptr) {
  8536. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  8537. }
  8538. 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;
  8539. std::cerr << std::endl << "Result:" << std::endl;
  8540. ggml_vk_print_tensor_area(tensor, tensor_data, 5, 5, 0, 0);
  8541. std::cerr << std::endl << "Correct:" << std::endl;
  8542. ggml_vk_print_tensor_area(tensor, comp_result, 5, 5, 0, 0);
  8543. std::cerr << std::endl;
  8544. std::vector<const ggml_tensor *> done;
  8545. ggml_vk_print_graph_origin(tensor, done);
  8546. }
  8547. if (avg_err > 0.05 || std::isnan(avg_err)) {
  8548. std::cerr << "ERROR: avg_err=" << avg_err << " in " << ggml_op_name(tensor->op) << " (check " << check_counter << ")" << std::endl;
  8549. std::cerr << "tensor=" << tensor << " tensor->name=" << tensor->name << " tensor->type: " << ggml_type_name(tensor->type) << " ne0=" << tensor->ne[0] << " nb0=" << tensor->nb[0] << " ne1=" << tensor->ne[1] << " nb1=" << tensor->nb[1] << " ne2=" << tensor->ne[2] << " nb2=" << tensor->nb[2] << " ne3=" << tensor->ne[3] << " nb3=" << tensor->nb[3] << " offset=" << tensor->view_offs << std::endl;
  8550. if (src0 != nullptr) {
  8551. std::cerr << "src0=" << src0 << " op=" << ggml_op_name(src0->op) << " type=" << ggml_type_name(src0->type) << " ne0=" << src0->ne[0] << " nb0=" << src0->nb[0] << " ne1=" << src0->ne[1] << " nb1=" << src0->nb[1] << " ne2=" << src0->ne[2] << " nb2=" << src0->nb[2] << " ne3=" << src0->ne[3] << " nb3=" << src0->nb[3] << " offset=" << src0->view_offs << std::endl;
  8552. }
  8553. if (src1 != nullptr) {
  8554. std::cerr << "src1=" << src1 << " op=" << ggml_op_name(src1->op) << " type=" << ggml_type_name(src1->type) << " ne0=" << src1->ne[0] << " nb0=" << src1->nb[0] << " ne1=" << src1->ne[1] << " nb1=" << src1->nb[1] << " ne2=" << src1->ne[2] << " nb2=" << src1->nb[2] << " ne3=" << src1->ne[3] << " nb3=" << src1->nb[3] << " offset=" << src1->view_offs << std::endl;
  8555. }
  8556. if (src2 != nullptr) {
  8557. std::cerr << "src2=" << src2 << " op=" << ggml_op_name(src2->op) << " type=" << ggml_type_name(src2->type) << " ne0=" << src2->ne[0] << " nb0=" << src2->nb[0] << " ne1=" << src2->ne[1] << " nb1=" << src2->nb[1] << " ne2=" << src2->ne[2] << " nb2=" << src2->nb[2] << " ne3=" << src2->ne[3] << " nb3=" << src2->nb[3] << " offset=" << src2->view_offs << std::endl;
  8558. }
  8559. if (src3 != nullptr) {
  8560. std::cerr << "src3=" << src3 << " op=" << ggml_op_name(src3->op) << " type=" << ggml_type_name(src3->type) << " ne0=" << src3->ne[0] << " nb0=" << src3->nb[0] << " ne1=" << src3->ne[1] << " nb1=" << src3->nb[1] << " ne2=" << src3->ne[2] << " nb2=" << src3->nb[2] << " ne3=" << src3->ne[3] << " nb3=" << src3->nb[3] << " offset=" << src3->view_offs << std::endl;
  8561. }
  8562. 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;
  8563. std::cerr << std::endl << "Result:" << std::endl;
  8564. ggml_vk_print_tensor_area(tensor, tensor_data, first_error[0], first_error[1], first_error[2], first_error[3]);
  8565. std::cerr << std::endl << "Correct:" << std::endl;
  8566. ggml_vk_print_tensor_area(tensor, comp_result, first_error[0], first_error[1], first_error[2], first_error[3]);
  8567. std::cerr << std::endl;
  8568. std::vector<const ggml_tensor *> done;
  8569. ggml_vk_print_graph_origin(tensor, done);
  8570. GGML_ABORT("fatal error");
  8571. } else {
  8572. std::cerr << check_counter << " " << tensor->name << " op=" << ggml_op_name(tensor->op) << " avg_err=" << avg_err << std::endl;
  8573. }
  8574. free(comp_result);
  8575. comp_result = nullptr;
  8576. comp_size = 0;
  8577. if (ggml_backend_buffer_is_vk(tensor->buffer)) {
  8578. free(tensor_data);
  8579. }
  8580. VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
  8581. }
  8582. #endif
  8583. GGML_BACKEND_DL_IMPL(ggml_backend_vk_reg)