ggml-vulkan.cpp 310 KB

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