ggml-cuda.cu 369 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133
  1. #include <algorithm>
  2. #include <assert.h>
  3. #include <atomic>
  4. #include <cinttypes>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <float.h>
  8. #include <limits>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <vector>
  12. #if defined(GGML_USE_HIPBLAS)
  13. #include <hip/hip_runtime.h>
  14. #include <hipblas/hipblas.h>
  15. #include <hip/hip_fp16.h>
  16. #ifdef __HIP_PLATFORM_AMD__
  17. // for rocblas_initialize()
  18. #include "rocblas/rocblas.h"
  19. #endif // __HIP_PLATFORM_AMD__
  20. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  21. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  22. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  23. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  24. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  25. #define CUBLAS_OP_N HIPBLAS_OP_N
  26. #define CUBLAS_OP_T HIPBLAS_OP_T
  27. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  28. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  29. #define CUDA_R_16F HIPBLAS_R_16F
  30. #define CUDA_R_32F HIPBLAS_R_32F
  31. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  32. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  33. #define cublasCreate hipblasCreate
  34. #define cublasGemmEx hipblasGemmEx
  35. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  36. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  37. #define cublasHandle_t hipblasHandle_t
  38. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  39. #define cublasSetStream hipblasSetStream
  40. #define cublasSgemm hipblasSgemm
  41. #define cublasStatus_t hipblasStatus_t
  42. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  43. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  44. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  45. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  46. #define cudaDeviceProp hipDeviceProp_t
  47. #define cudaDeviceSynchronize hipDeviceSynchronize
  48. #define cudaError_t hipError_t
  49. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  50. #define cudaEventDisableTiming hipEventDisableTiming
  51. #define cudaEventRecord hipEventRecord
  52. #define cudaEvent_t hipEvent_t
  53. #define cudaEventDestroy hipEventDestroy
  54. #define cudaFree hipFree
  55. #define cudaFreeHost hipHostFree
  56. #define cudaGetDevice hipGetDevice
  57. #define cudaGetDeviceCount hipGetDeviceCount
  58. #define cudaGetDeviceProperties hipGetDeviceProperties
  59. #define cudaGetErrorString hipGetErrorString
  60. #define cudaGetLastError hipGetLastError
  61. #ifdef GGML_HIP_UMA
  62. #define cudaMalloc hipMallocManaged
  63. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  64. #else
  65. #define cudaMalloc hipMalloc
  66. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  67. #endif
  68. #define cudaMemcpy hipMemcpy
  69. #define cudaMemcpyAsync hipMemcpyAsync
  70. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  71. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  72. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  73. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  74. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  75. #define cudaMemcpyKind hipMemcpyKind
  76. #define cudaMemset hipMemset
  77. #define cudaMemsetAsync hipMemsetAsync
  78. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  79. #define cudaSetDevice hipSetDevice
  80. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  81. #define cudaStreamFireAndForget hipStreamFireAndForget
  82. #define cudaStreamNonBlocking hipStreamNonBlocking
  83. #define cudaStreamSynchronize hipStreamSynchronize
  84. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  85. #define cudaStream_t hipStream_t
  86. #define cudaSuccess hipSuccess
  87. #define __trap abort
  88. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  89. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  90. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  91. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  92. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  93. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  94. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  95. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  96. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  97. #else
  98. #include <cuda_runtime.h>
  99. #include <cuda.h>
  100. #include <cublas_v2.h>
  101. #include <cuda_fp16.h>
  102. #if CUDART_VERSION < 11020
  103. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  104. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  105. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  106. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  107. #define cublasComputeType_t cudaDataType_t
  108. #endif // CUDART_VERSION < 11020
  109. #endif // defined(GGML_USE_HIPBLAS)
  110. #include "ggml-cuda.h"
  111. #include "ggml.h"
  112. #include "ggml-backend-impl.h"
  113. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  114. #define CC_VOLTA 700
  115. #define CC_OFFSET_AMD 1000000
  116. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  117. #define GGML_CUDA_MAX_NODES 8192
  118. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  119. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  120. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  121. // - 7B quantum model: +100-200 MB
  122. // - 13B quantum model: +200-400 MB
  123. //
  124. //#define GGML_CUDA_FORCE_MMQ
  125. // TODO: improve this to be correct for more hardware
  126. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  127. // probably other such cases, and not sure what happens on AMD hardware
  128. #if !defined(GGML_CUDA_FORCE_MMQ)
  129. #define CUDA_USE_TENSOR_CORES
  130. #endif
  131. // max batch size to use MMQ kernels when tensor cores are available
  132. #define MMQ_MAX_BATCH_SIZE 32
  133. #if defined(GGML_USE_HIPBLAS)
  134. #define __CUDA_ARCH__ 1300
  135. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  136. defined(__gfx1150__) || defined(__gfx1151__)
  137. #define RDNA3
  138. #endif
  139. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  140. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  141. #define RDNA2
  142. #endif
  143. #ifndef __has_builtin
  144. #define __has_builtin(x) 0
  145. #endif
  146. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  147. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  148. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  149. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  150. #if __has_builtin(__builtin_elementwise_sub_sat)
  151. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  152. return reinterpret_cast<const int &>(c);
  153. #else
  154. int8x4_t c;
  155. int16_t tmp;
  156. #pragma unroll
  157. for (int i = 0; i < 4; i++) {
  158. tmp = va[i] - vb[i];
  159. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  160. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  161. c[i] = tmp;
  162. }
  163. return reinterpret_cast<int &>(c);
  164. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  165. }
  166. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  167. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  168. c = __builtin_amdgcn_sdot4(a, b, c, false);
  169. #elif defined(__gfx1100__)
  170. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  171. #elif defined(__gfx1010__) || defined(__gfx900__)
  172. int tmp1;
  173. int tmp2;
  174. asm("\n \
  175. v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0 src1_sel:BYTE_0 \n \
  176. v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:BYTE_1 \n \
  177. v_add3_u32 %0, %1, %2, %0 \n \
  178. v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2 src1_sel:BYTE_2 \n \
  179. v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_3 src1_sel:BYTE_3 \n \
  180. v_add3_u32 %0, %1, %2, %0 \n \
  181. "
  182. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  183. : "v"(a), "v"(b)
  184. );
  185. #else
  186. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  187. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  188. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  189. #endif
  190. return c;
  191. }
  192. #endif // defined(GGML_USE_HIPBLAS)
  193. #if defined(_MSC_VER)
  194. #pragma warning(disable: 4244 4267) // possible loss of data
  195. #endif
  196. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  197. [[noreturn]]
  198. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  199. int id = -1; // in case cudaGetDevice fails
  200. cudaGetDevice(&id);
  201. fprintf(stderr, "CUDA error: %s\n", msg);
  202. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  203. fprintf(stderr, " %s\n", stmt);
  204. // abort with GGML_ASSERT to get a stack trace
  205. GGML_ASSERT(!"CUDA error");
  206. }
  207. #define CUDA_CHECK_GEN(err, success, error_fn) \
  208. do { \
  209. auto err_ = (err); \
  210. if (err_ != (success)) { \
  211. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  212. } \
  213. } while (0)
  214. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  215. #if CUDART_VERSION >= 12000
  216. static const char * cublas_get_error_str(const cublasStatus_t err) {
  217. return cublasGetStatusString(err);
  218. }
  219. #else
  220. static const char * cublas_get_error_str(const cublasStatus_t err) {
  221. switch (err) {
  222. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  223. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  224. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  225. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  226. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  227. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  228. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  229. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  230. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  231. default: return "unknown error";
  232. }
  233. }
  234. #endif // CUDART_VERSION >= 12000
  235. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  236. #if !defined(GGML_USE_HIPBLAS)
  237. static const char * cu_get_error_str(CUresult err) {
  238. const char * err_str;
  239. cuGetErrorString(err, &err_str);
  240. return err_str;
  241. }
  242. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  243. #endif
  244. #if CUDART_VERSION >= 11100
  245. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  246. #else
  247. #define GGML_CUDA_ASSUME(x)
  248. #endif // CUDART_VERSION >= 11100
  249. #ifdef GGML_CUDA_F16
  250. typedef half dfloat; // dequantize float
  251. typedef half2 dfloat2;
  252. #else
  253. typedef float dfloat; // dequantize float
  254. typedef float2 dfloat2;
  255. #endif //GGML_CUDA_F16
  256. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  257. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  258. int x32 = 0;
  259. x32 |= x16[0] << 0;
  260. x32 |= x16[1] << 16;
  261. return x32;
  262. }
  263. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  264. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  265. int x32 = 0;
  266. x32 |= x16[0] << 0;
  267. x32 |= x16[1] << 16;
  268. return x32;
  269. }
  270. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  271. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  272. }
  273. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  274. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  275. }
  276. template<typename T>
  277. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  278. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  279. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  280. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  281. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  282. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  283. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  284. typedef void (*ggml_cuda_op_mul_mat_t)(
  285. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  286. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  287. const int64_t src1_padded_row_size, cudaStream_t stream);
  288. typedef void (*ggml_cuda_op_flatten_t)(
  289. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  290. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  291. // QK = number of values after dequantization
  292. // QR = QK / number of values before dequantization
  293. // QI = number of 32 bit integers before dequantization
  294. #define QK4_0 32
  295. #define QR4_0 2
  296. #define QI4_0 (QK4_0 / (4 * QR4_0))
  297. typedef struct {
  298. half d; // delta
  299. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  300. } block_q4_0;
  301. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  302. #define QK4_1 32
  303. #define QR4_1 2
  304. #define QI4_1 (QK4_1 / (4 * QR4_1))
  305. typedef struct {
  306. half2 dm; // dm.x = delta, dm.y = min
  307. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  308. } block_q4_1;
  309. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  310. #define QK5_0 32
  311. #define QR5_0 2
  312. #define QI5_0 (QK5_0 / (4 * QR5_0))
  313. typedef struct {
  314. half d; // delta
  315. uint8_t qh[4]; // 5-th bit of quants
  316. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  317. } block_q5_0;
  318. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  319. #define QK5_1 32
  320. #define QR5_1 2
  321. #define QI5_1 (QK5_1 / (4 * QR5_1))
  322. typedef struct {
  323. half2 dm; // dm.x = delta, dm.y = min
  324. uint8_t qh[4]; // 5-th bit of quants
  325. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  326. } block_q5_1;
  327. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  328. #define QK8_0 32
  329. #define QR8_0 1
  330. #define QI8_0 (QK8_0 / (4 * QR8_0))
  331. typedef struct {
  332. half d; // delta
  333. int8_t qs[QK8_0]; // quants
  334. } block_q8_0;
  335. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  336. #define QK8_1 32
  337. #define QR8_1 1
  338. #define QI8_1 (QK8_1 / (4 * QR8_1))
  339. typedef struct {
  340. half2 ds; // ds.x = delta, ds.y = sum
  341. int8_t qs[QK8_0]; // quants
  342. } block_q8_1;
  343. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  344. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  345. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  346. typedef void (*load_tiles_cuda_t)(
  347. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  348. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  349. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  350. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  351. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  352. //================================= k-quants
  353. #ifdef GGML_QKK_64
  354. #define QK_K 64
  355. #define K_SCALE_SIZE 4
  356. #else
  357. #define QK_K 256
  358. #define K_SCALE_SIZE 12
  359. #endif
  360. #define QR2_K 4
  361. #define QI2_K (QK_K / (4*QR2_K))
  362. typedef struct {
  363. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  364. uint8_t qs[QK_K/4]; // quants
  365. half2 dm; // super-block scale for quantized scales/mins
  366. } block_q2_K;
  367. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  368. #define QR3_K 4
  369. #define QI3_K (QK_K / (4*QR3_K))
  370. typedef struct {
  371. uint8_t hmask[QK_K/8]; // quants - high bit
  372. uint8_t qs[QK_K/4]; // quants - low 2 bits
  373. #ifdef GGML_QKK_64
  374. uint8_t scales[2]; // scales, quantized with 8 bits
  375. #else
  376. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  377. #endif
  378. half d; // super-block scale
  379. } block_q3_K;
  380. //static_assert(sizeof(block_q3_K) == sizeof(ggml_fp16_t) + QK_K / 4 + QK_K / 8 + K_SCALE_SIZE, "wrong q3_K block size/padding");
  381. #define QR4_K 2
  382. #define QI4_K (QK_K / (4*QR4_K))
  383. #ifdef GGML_QKK_64
  384. typedef struct {
  385. half dm[2]; // super-block scales/mins
  386. uint8_t scales[2]; // 4-bit block scales/mins
  387. uint8_t qs[QK_K/2]; // 4--bit quants
  388. } block_q4_K;
  389. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  390. #else
  391. typedef struct {
  392. half2 dm; // super-block scale for quantized scales/mins
  393. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  394. uint8_t qs[QK_K/2]; // 4--bit quants
  395. } block_q4_K;
  396. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  397. #endif
  398. #define QR5_K 2
  399. #define QI5_K (QK_K / (4*QR5_K))
  400. #ifdef GGML_QKK_64
  401. typedef struct {
  402. half d; // super-block scale
  403. int8_t scales[QK_K/16]; // block scales
  404. uint8_t qh[QK_K/8]; // quants, high bit
  405. uint8_t qs[QK_K/2]; // quants, low 4 bits
  406. } block_q5_K;
  407. static_assert(sizeof(block_q5_K) == sizeof(ggml_fp16_t) + QK_K/2 + QK_K/8 + QK_K/16, "wrong q5_K block size/padding");
  408. #else
  409. typedef struct {
  410. half2 dm; // super-block scale for quantized scales/mins
  411. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  412. uint8_t qh[QK_K/8]; // quants, high bit
  413. uint8_t qs[QK_K/2]; // quants, low 4 bits
  414. } block_q5_K;
  415. static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_fp16_t) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");
  416. #endif
  417. #define QR6_K 2
  418. #define QI6_K (QK_K / (4*QR6_K))
  419. typedef struct {
  420. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  421. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  422. int8_t scales[QK_K/16]; // scales
  423. half d; // delta
  424. } block_q6_K;
  425. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  426. #define WARP_SIZE 32
  427. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  428. #define CUDA_GELU_BLOCK_SIZE 256
  429. #define CUDA_SILU_BLOCK_SIZE 256
  430. #define CUDA_TANH_BLOCK_SIZE 256
  431. #define CUDA_RELU_BLOCK_SIZE 256
  432. #define CUDA_SQR_BLOCK_SIZE 256
  433. #define CUDA_CPY_BLOCK_SIZE 32
  434. #define CUDA_SCALE_BLOCK_SIZE 256
  435. #define CUDA_CLAMP_BLOCK_SIZE 256
  436. #define CUDA_ROPE_BLOCK_SIZE 256
  437. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  438. #define CUDA_ALIBI_BLOCK_SIZE 32
  439. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  440. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  441. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  442. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  443. #define CUDA_UPSCALE_BLOCK_SIZE 256
  444. #define CUDA_CONCAT_BLOCK_SIZE 256
  445. #define CUDA_PAD_BLOCK_SIZE 256
  446. #define CUDA_ACC_BLOCK_SIZE 256
  447. #define CUDA_IM2COL_BLOCK_SIZE 256
  448. // dmmv = dequantize_mul_mat_vec
  449. #ifndef GGML_CUDA_DMMV_X
  450. #define GGML_CUDA_DMMV_X 32
  451. #endif
  452. #ifndef GGML_CUDA_MMV_Y
  453. #define GGML_CUDA_MMV_Y 1
  454. #endif
  455. #ifndef K_QUANTS_PER_ITERATION
  456. #define K_QUANTS_PER_ITERATION 2
  457. #else
  458. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  459. #endif
  460. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  461. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  462. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  463. #define MUL_MAT_SRC1_COL_STRIDE 128
  464. #define MAX_STREAMS 8
  465. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  466. struct ggml_tensor_extra_gpu {
  467. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  468. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  469. };
  470. // this is faster on Windows
  471. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  472. static void ggml_cuda_set_device(const int device) {
  473. int current_device;
  474. CUDA_CHECK(cudaGetDevice(&current_device));
  475. if (device == current_device) {
  476. return;
  477. }
  478. CUDA_CHECK(cudaSetDevice(device));
  479. }
  480. static int g_device_count = -1;
  481. static int g_main_device = 0;
  482. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  483. struct cuda_device_capabilities {
  484. int cc; // compute capability
  485. bool vmm; // virtual memory support
  486. size_t vmm_granularity; // granularity of virtual memory
  487. };
  488. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, false, 0} };
  489. static void * g_scratch_buffer = nullptr;
  490. static size_t g_scratch_size = 0; // disabled by default
  491. static size_t g_scratch_offset = 0;
  492. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  493. [[noreturn]]
  494. static __device__ void bad_arch() {
  495. printf("ERROR: ggml-cuda was compiled without support for the current GPU architecture.\n");
  496. __trap();
  497. (void) bad_arch; // suppress unused function warning
  498. }
  499. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  500. #pragma unroll
  501. for (int mask = 16; mask > 0; mask >>= 1) {
  502. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  503. }
  504. return x;
  505. }
  506. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  507. #pragma unroll
  508. for (int mask = 16; mask > 0; mask >>= 1) {
  509. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  510. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  511. }
  512. return a;
  513. }
  514. static __device__ __forceinline__ float warp_reduce_max(float x) {
  515. #pragma unroll
  516. for (int mask = 16; mask > 0; mask >>= 1) {
  517. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  518. }
  519. return x;
  520. }
  521. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  522. return b;
  523. GGML_UNUSED(a);
  524. }
  525. static __device__ __forceinline__ float op_add(const float a, const float b) {
  526. return a + b;
  527. }
  528. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  529. return a * b;
  530. }
  531. static __device__ __forceinline__ float op_div(const float a, const float b) {
  532. return a / b;
  533. }
  534. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  535. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  536. int ne0, int ne1, int ne2, int ne3,
  537. int ne10, int ne11, int ne12, int ne13,
  538. /*int s0, */ int s1, int s2, int s3,
  539. /*int s10,*/ int s11, int s12, int s13) {
  540. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  541. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  542. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  543. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  544. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  545. return;
  546. }
  547. const int i11 = i1 % ne11;
  548. const int i12 = i2 % ne12;
  549. const int i13 = i3 % ne13;
  550. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  551. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  552. const size_t i_dst = i_src0;
  553. const src0_t * src0_row = src0 + i_src0;
  554. const src1_t * src1_row = src1 + i_src1;
  555. dst_t * dst_row = dst + i_dst;
  556. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  557. const int i10 = i0 % ne10;
  558. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  559. }
  560. }
  561. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  562. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  563. int ne0, int ne1, int ne2, int ne3,
  564. int ne10, int ne11, int ne12, int ne13,
  565. /*int s0, */ int s1, int s2, int s3,
  566. /*int s10,*/ int s11, int s12, int s13) {
  567. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  568. const int i3 = i/(ne2*ne1*ne0);
  569. const int i2 = (i/(ne1*ne0)) % ne2;
  570. const int i1 = (i/ne0) % ne1;
  571. const int i0 = i % ne0;
  572. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  573. return;
  574. }
  575. const int i11 = i1 % ne11;
  576. const int i12 = i2 % ne12;
  577. const int i13 = i3 % ne13;
  578. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  579. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  580. const size_t i_dst = i_src0;
  581. const src0_t * src0_row = src0 + i_src0;
  582. const src1_t * src1_row = src1 + i_src1;
  583. dst_t * dst_row = dst + i_dst;
  584. const int i10 = i0 % ne10;
  585. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  586. }
  587. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  588. const int ne10, const int ne11, const int ne12,
  589. const int nb1, const int nb2, int offset) {
  590. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  591. if (i >= ne) {
  592. return;
  593. }
  594. int src1_idx = i - offset;
  595. int oz = src1_idx / nb2;
  596. int oy = (src1_idx - (oz * nb2)) / nb1;
  597. int ox = src1_idx % nb1;
  598. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  599. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  600. } else {
  601. dst[i] = x[i];
  602. }
  603. }
  604. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  605. const float GELU_COEF_A = 0.044715f;
  606. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  607. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  608. if (i >= k) {
  609. return;
  610. }
  611. float xi = x[i];
  612. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  613. }
  614. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  615. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  616. if (i >= k) {
  617. return;
  618. }
  619. dst[i] = x[i] / (1.0f + expf(-x[i]));
  620. }
  621. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  622. const float GELU_QUICK_COEF = -1.702f;
  623. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  624. if (i >= k) {
  625. return;
  626. }
  627. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  628. }
  629. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  630. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  631. if (i >= k) {
  632. return;
  633. }
  634. dst[i] = tanhf(x[i]);
  635. }
  636. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  637. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  638. if (i >= k) {
  639. return;
  640. }
  641. dst[i] = fmaxf(x[i], 0);
  642. }
  643. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  644. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  645. if (i >= k) {
  646. return;
  647. }
  648. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  649. }
  650. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  651. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  652. if (i >= k) {
  653. return;
  654. }
  655. dst[i] = x[i] * x[i];
  656. }
  657. template <int block_size>
  658. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  659. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  660. const int tid = threadIdx.x;
  661. float2 mean_var = make_float2(0.f, 0.f);
  662. for (int col = tid; col < ncols; col += block_size) {
  663. const float xi = x[row*ncols + col];
  664. mean_var.x += xi;
  665. mean_var.y += xi * xi;
  666. }
  667. // sum up partial sums
  668. mean_var = warp_reduce_sum(mean_var);
  669. if (block_size > WARP_SIZE) {
  670. __shared__ float2 s_sum[32];
  671. int warp_id = threadIdx.x / WARP_SIZE;
  672. int lane_id = threadIdx.x % WARP_SIZE;
  673. if (lane_id == 0) {
  674. s_sum[warp_id] = mean_var;
  675. }
  676. __syncthreads();
  677. mean_var = s_sum[lane_id];
  678. mean_var = warp_reduce_sum(mean_var);
  679. }
  680. const float mean = mean_var.x / ncols;
  681. const float var = mean_var.y / ncols - mean * mean;
  682. const float inv_std = rsqrtf(var + eps);
  683. for (int col = tid; col < ncols; col += block_size) {
  684. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  685. }
  686. }
  687. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  688. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  689. if (nidx >= ne0) {
  690. return;
  691. }
  692. // operation
  693. int offset_dst =
  694. nidx +
  695. blockIdx.y * ne0 +
  696. blockIdx.z * ne0 * gridDim.y;
  697. if (blockIdx.z < ne02) { // src0
  698. int offset_src =
  699. nidx +
  700. blockIdx.y * ne0 +
  701. blockIdx.z * ne0 * gridDim.y;
  702. dst[offset_dst] = x[offset_src];
  703. } else {
  704. int offset_src =
  705. nidx +
  706. blockIdx.y * ne0 +
  707. (blockIdx.z - ne02) * ne0 * gridDim.y;
  708. dst[offset_dst] = y[offset_src];
  709. }
  710. }
  711. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  712. int ne0 = ne00 * scale_factor;
  713. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  714. if (nidx >= ne0) {
  715. return;
  716. }
  717. // operation
  718. int i00 = nidx / scale_factor;
  719. int i01 = blockIdx.y / scale_factor;
  720. int offset_src =
  721. i00 +
  722. i01 * ne00 +
  723. blockIdx.z * nb02;
  724. int offset_dst =
  725. nidx +
  726. blockIdx.y * ne0 +
  727. blockIdx.z * ne0 * gridDim.y;
  728. dst[offset_dst] = x[offset_src];
  729. }
  730. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  731. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  732. if (nidx >= ne0) {
  733. return;
  734. }
  735. // operation
  736. int offset_dst =
  737. nidx +
  738. blockIdx.y * ne0 +
  739. blockIdx.z * ne0 * gridDim.y;
  740. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  741. int offset_src =
  742. nidx +
  743. blockIdx.y * ne00 +
  744. blockIdx.z * ne00 * ne01;
  745. dst[offset_dst] = x[offset_src];
  746. } else {
  747. dst[offset_dst] = 0.0f;
  748. }
  749. }
  750. template <int block_size>
  751. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  752. int start = blockIdx.x * group_size;
  753. int end = start + group_size;
  754. start += threadIdx.x;
  755. if (end >= ne_elements) {
  756. end = ne_elements;
  757. }
  758. float tmp = 0.0f; // partial sum for thread in warp
  759. for (int j = start; j < end; j += block_size) {
  760. tmp += x[j];
  761. }
  762. tmp = warp_reduce_sum(tmp);
  763. if (block_size > WARP_SIZE) {
  764. __shared__ float s_sum[32];
  765. int warp_id = threadIdx.x / WARP_SIZE;
  766. int lane_id = threadIdx.x % WARP_SIZE;
  767. if (lane_id == 0) {
  768. s_sum[warp_id] = tmp;
  769. }
  770. __syncthreads();
  771. tmp = s_sum[lane_id];
  772. tmp = warp_reduce_sum(tmp);
  773. }
  774. float mean = tmp / group_size;
  775. tmp = 0.0f;
  776. for (int j = start; j < end; j += block_size) {
  777. float xi = x[j] - mean;
  778. dst[j] = xi;
  779. tmp += xi * xi;
  780. }
  781. tmp = warp_reduce_sum(tmp);
  782. if (block_size > WARP_SIZE) {
  783. __shared__ float s_sum[32];
  784. int warp_id = threadIdx.x / WARP_SIZE;
  785. int lane_id = threadIdx.x % WARP_SIZE;
  786. if (lane_id == 0) {
  787. s_sum[warp_id] = tmp;
  788. }
  789. __syncthreads();
  790. tmp = s_sum[lane_id];
  791. tmp = warp_reduce_sum(tmp);
  792. }
  793. float variance = tmp / group_size;
  794. float scale = rsqrtf(variance + eps);
  795. for (int j = start; j < end; j += block_size) {
  796. dst[j] *= scale;
  797. }
  798. }
  799. template <int block_size>
  800. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  801. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  802. const int tid = threadIdx.x;
  803. float tmp = 0.0f; // partial sum for thread in warp
  804. for (int col = tid; col < ncols; col += block_size) {
  805. const float xi = x[row*ncols + col];
  806. tmp += xi * xi;
  807. }
  808. // sum up partial sums
  809. tmp = warp_reduce_sum(tmp);
  810. if (block_size > WARP_SIZE) {
  811. __shared__ float s_sum[32];
  812. int warp_id = threadIdx.x / WARP_SIZE;
  813. int lane_id = threadIdx.x % WARP_SIZE;
  814. if (lane_id == 0) {
  815. s_sum[warp_id] = tmp;
  816. }
  817. __syncthreads();
  818. tmp = s_sum[lane_id];
  819. tmp = warp_reduce_sum(tmp);
  820. }
  821. const float mean = tmp / ncols;
  822. const float scale = rsqrtf(mean + eps);
  823. for (int col = tid; col < ncols; col += block_size) {
  824. dst[row*ncols + col] = scale * x[row*ncols + col];
  825. }
  826. }
  827. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  828. const block_q4_0 * x = (const block_q4_0 *) vx;
  829. const dfloat d = x[ib].d;
  830. const int vui = x[ib].qs[iqs];
  831. v.x = vui & 0xF;
  832. v.y = vui >> 4;
  833. #ifdef GGML_CUDA_F16
  834. v = __hsub2(v, {8.0f, 8.0f});
  835. v = __hmul2(v, {d, d});
  836. #else
  837. v.x = (v.x - 8.0f) * d;
  838. v.y = (v.y - 8.0f) * d;
  839. #endif // GGML_CUDA_F16
  840. }
  841. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  842. const block_q4_1 * x = (const block_q4_1 *) vx;
  843. const dfloat d = __low2half(x[ib].dm);
  844. const dfloat m = __high2half(x[ib].dm);
  845. const int vui = x[ib].qs[iqs];
  846. v.x = vui & 0xF;
  847. v.y = vui >> 4;
  848. #ifdef GGML_CUDA_F16
  849. v = __hmul2(v, {d, d});
  850. v = __hadd2(v, {m, m});
  851. #else
  852. v.x = (v.x * d) + m;
  853. v.y = (v.y * d) + m;
  854. #endif // GGML_CUDA_F16
  855. }
  856. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  857. const block_q5_0 * x = (const block_q5_0 *) vx;
  858. const dfloat d = x[ib].d;
  859. uint32_t qh;
  860. memcpy(&qh, x[ib].qh, sizeof(qh));
  861. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  862. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  863. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  864. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  865. #ifdef GGML_CUDA_F16
  866. v = __hsub2(v, {16.0f, 16.0f});
  867. v = __hmul2(v, {d, d});
  868. #else
  869. v.x = (v.x - 16.0f) * d;
  870. v.y = (v.y - 16.0f) * d;
  871. #endif // GGML_CUDA_F16
  872. }
  873. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  874. const block_q5_1 * x = (const block_q5_1 *) vx;
  875. const dfloat d = __low2half(x[ib].dm);
  876. const dfloat m = __high2half(x[ib].dm);
  877. uint32_t qh;
  878. memcpy(&qh, x[ib].qh, sizeof(qh));
  879. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  880. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  881. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  882. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  883. #ifdef GGML_CUDA_F16
  884. v = __hmul2(v, {d, d});
  885. v = __hadd2(v, {m, m});
  886. #else
  887. v.x = (v.x * d) + m;
  888. v.y = (v.y * d) + m;
  889. #endif // GGML_CUDA_F16
  890. }
  891. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  892. const block_q8_0 * x = (const block_q8_0 *) vx;
  893. const dfloat d = x[ib].d;
  894. v.x = x[ib].qs[iqs + 0];
  895. v.y = x[ib].qs[iqs + 1];
  896. #ifdef GGML_CUDA_F16
  897. v = __hmul2(v, {d, d});
  898. #else
  899. v.x *= d;
  900. v.y *= d;
  901. #endif // GGML_CUDA_F16
  902. }
  903. //================================== k-quants
  904. template<typename dst_t>
  905. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  906. const int i = blockIdx.x;
  907. const block_q2_K * x = (const block_q2_K *) vx;
  908. const int tid = threadIdx.x;
  909. #if QK_K == 256
  910. const int n = tid/32;
  911. const int l = tid - 32*n;
  912. const int is = 8*n + l/16;
  913. const uint8_t q = x[i].qs[32*n + l];
  914. dst_t * y = yy + i*QK_K + 128*n;
  915. float dall = __low2half(x[i].dm);
  916. float dmin = __high2half(x[i].dm);
  917. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  918. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  919. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  920. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  921. #else
  922. const int is = tid/16; // 0 or 1
  923. const int il = tid%16; // 0...15
  924. const uint8_t q = x[i].qs[il] >> (2*is);
  925. dst_t * y = yy + i*QK_K + 16*is + il;
  926. float dall = __low2half(x[i].dm);
  927. float dmin = __high2half(x[i].dm);
  928. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  929. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  930. #endif
  931. }
  932. template<typename dst_t>
  933. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  934. const int i = blockIdx.x;
  935. const block_q3_K * x = (const block_q3_K *) vx;
  936. #if QK_K == 256
  937. const int r = threadIdx.x/4;
  938. const int tid = r/2;
  939. const int is0 = r%2;
  940. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  941. const int n = tid / 4;
  942. const int j = tid - 4*n;
  943. uint8_t m = 1 << (4*n + j);
  944. int is = 8*n + 2*j + is0;
  945. int shift = 2*j;
  946. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  947. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  948. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  949. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  950. float d_all = x[i].d;
  951. float dl = d_all * (us - 32);
  952. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  953. const uint8_t * q = x[i].qs + 32*n;
  954. const uint8_t * hm = x[i].hmask;
  955. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  956. #else
  957. const int tid = threadIdx.x;
  958. const int is = tid/16; // 0 or 1
  959. const int il = tid%16; // 0...15
  960. const int im = il/8; // 0...1
  961. const int in = il%8; // 0...7
  962. dst_t * y = yy + i*QK_K + 16*is + il;
  963. const uint8_t q = x[i].qs[il] >> (2*is);
  964. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  965. const float d = (float)x[i].d;
  966. if (is == 0) {
  967. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  968. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  969. } else {
  970. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  971. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  972. }
  973. #endif
  974. }
  975. #if QK_K == 256
  976. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  977. if (j < 4) {
  978. d = q[j] & 63; m = q[j + 4] & 63;
  979. } else {
  980. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  981. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  982. }
  983. }
  984. #endif
  985. template<typename dst_t>
  986. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  987. const block_q4_K * x = (const block_q4_K *) vx;
  988. const int i = blockIdx.x;
  989. #if QK_K == 256
  990. // assume 32 threads
  991. const int tid = threadIdx.x;
  992. const int il = tid/8;
  993. const int ir = tid%8;
  994. const int is = 2*il;
  995. const int n = 4;
  996. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  997. const float dall = __low2half(x[i].dm);
  998. const float dmin = __high2half(x[i].dm);
  999. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1000. uint8_t sc, m;
  1001. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1002. const float d1 = dall * sc; const float m1 = dmin * m;
  1003. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1004. const float d2 = dall * sc; const float m2 = dmin * m;
  1005. for (int l = 0; l < n; ++l) {
  1006. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1007. y[l +32] = d2 * (q[l] >> 4) - m2;
  1008. }
  1009. #else
  1010. const int tid = threadIdx.x;
  1011. const uint8_t * q = x[i].qs;
  1012. dst_t * y = yy + i*QK_K;
  1013. const float d = (float)x[i].dm[0];
  1014. const float m = (float)x[i].dm[1];
  1015. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1016. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1017. #endif
  1018. }
  1019. template<typename dst_t>
  1020. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1021. const block_q5_K * x = (const block_q5_K *) vx;
  1022. const int i = blockIdx.x;
  1023. #if QK_K == 256
  1024. // assume 64 threads - this is very slightly better than the one below
  1025. const int tid = threadIdx.x;
  1026. const int il = tid/16; // il is in 0...3
  1027. const int ir = tid%16; // ir is in 0...15
  1028. const int is = 2*il; // is is in 0...6
  1029. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1030. const float dall = __low2half(x[i].dm);
  1031. const float dmin = __high2half(x[i].dm);
  1032. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1033. const uint8_t * qh = x[i].qh + 2*ir;
  1034. uint8_t sc, m;
  1035. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1036. const float d1 = dall * sc; const float m1 = dmin * m;
  1037. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1038. const float d2 = dall * sc; const float m2 = dmin * m;
  1039. uint8_t hm = 1 << (2*il);
  1040. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1041. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1042. hm <<= 1;
  1043. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1044. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1045. #else
  1046. const int tid = threadIdx.x;
  1047. const uint8_t q = x[i].qs[tid];
  1048. const int im = tid/8; // 0...3
  1049. const int in = tid%8; // 0...7
  1050. const int is = tid/16; // 0 or 1
  1051. const uint8_t h = x[i].qh[in] >> im;
  1052. const float d = x[i].d;
  1053. dst_t * y = yy + i*QK_K + tid;
  1054. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1055. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1056. #endif
  1057. }
  1058. template<typename dst_t>
  1059. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1060. const block_q6_K * x = (const block_q6_K *) vx;
  1061. const int i = blockIdx.x;
  1062. #if QK_K == 256
  1063. // assume 64 threads - this is very slightly better than the one below
  1064. const int tid = threadIdx.x;
  1065. const int ip = tid/32; // ip is 0 or 1
  1066. const int il = tid - 32*ip; // 0...32
  1067. const int is = 8*ip + il/16;
  1068. dst_t * y = yy + i*QK_K + 128*ip + il;
  1069. const float d = x[i].d;
  1070. const uint8_t * ql = x[i].ql + 64*ip + il;
  1071. const uint8_t qh = x[i].qh[32*ip + il];
  1072. const int8_t * sc = x[i].scales + is;
  1073. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1074. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1075. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1076. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1077. #else
  1078. // assume 32 threads
  1079. const int tid = threadIdx.x;
  1080. const int ip = tid/16; // 0 or 1
  1081. const int il = tid - 16*ip; // 0...15
  1082. dst_t * y = yy + i*QK_K + 16*ip + il;
  1083. const float d = x[i].d;
  1084. const uint8_t ql = x[i].ql[16*ip + il];
  1085. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1086. const int8_t * sc = x[i].scales;
  1087. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1088. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1089. #endif
  1090. }
  1091. static __global__ void dequantize_mul_mat_vec_q2_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  1092. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1093. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1094. if (row > nrows) return;
  1095. const int num_blocks_per_row = ncols / QK_K;
  1096. const int ib0 = row*num_blocks_per_row;
  1097. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1098. float tmp = 0; // partial sum for thread in warp
  1099. #if QK_K == 256
  1100. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1101. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1102. const int step = 16/K_QUANTS_PER_ITERATION;
  1103. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1104. const int in = tid - step*im; // 0...15 or 0...7
  1105. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1106. const int q_offset = 32*im + l0;
  1107. const int s_offset = 8*im;
  1108. const int y_offset = 128*im + l0;
  1109. uint32_t aux[4];
  1110. const uint8_t * d = (const uint8_t *)aux;
  1111. const uint8_t * m = (const uint8_t *)(aux + 2);
  1112. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1113. const float * y = yy + i * QK_K + y_offset;
  1114. const uint8_t * q = x[i].qs + q_offset;
  1115. const float dall = __low2half(x[i].dm);
  1116. const float dmin = __high2half(x[i].dm);
  1117. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1118. aux[0] = a[0] & 0x0f0f0f0f;
  1119. aux[1] = a[1] & 0x0f0f0f0f;
  1120. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1121. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1122. float sum1 = 0, sum2 = 0;
  1123. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1124. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1125. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1126. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1127. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1128. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1129. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1130. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1131. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1132. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1133. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1134. }
  1135. tmp += dall * sum1 - dmin * sum2;
  1136. }
  1137. #else
  1138. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1139. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1140. const int offset = tid * K_QUANTS_PER_ITERATION;
  1141. uint32_t uaux[2];
  1142. const uint8_t * d = (const uint8_t *)uaux;
  1143. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1144. const float * y = yy + i * QK_K + offset;
  1145. const uint8_t * q = x[i].qs + offset;
  1146. const uint32_t * s = (const uint32_t *)x[i].scales;
  1147. uaux[0] = s[0] & 0x0f0f0f0f;
  1148. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1149. const float2 dall = __half22float2(x[i].dm);
  1150. float sum1 = 0, sum2 = 0;
  1151. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1152. const uint8_t ql = q[l];
  1153. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1154. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1155. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1156. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1157. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1158. }
  1159. tmp += dall.x * sum1 - dall.y * sum2;
  1160. }
  1161. #endif
  1162. // sum up partial sums and write back result
  1163. #pragma unroll
  1164. for (int mask = 16; mask > 0; mask >>= 1) {
  1165. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1166. }
  1167. if (threadIdx.x == 0) {
  1168. dst[row] = tmp;
  1169. }
  1170. }
  1171. static __global__ void dequantize_mul_mat_vec_q3_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  1172. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1173. if (row > nrows) return;
  1174. const int num_blocks_per_row = ncols / QK_K;
  1175. const int ib0 = row*num_blocks_per_row;
  1176. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1177. float tmp = 0; // partial sum for thread in warp
  1178. #if QK_K == 256
  1179. const uint16_t kmask1 = 0x0303;
  1180. const uint16_t kmask2 = 0x0f0f;
  1181. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1182. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1183. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1184. const int step = 16/K_QUANTS_PER_ITERATION;
  1185. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1186. const int in = tid - step*im; // 0....15 or 0...7
  1187. const uint8_t m = 1 << (4*im);
  1188. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1189. const int q_offset = 32*im + l0;
  1190. const int y_offset = 128*im + l0;
  1191. uint16_t utmp[4];
  1192. const int8_t * s = (const int8_t *)utmp;
  1193. const uint16_t s_shift = 4*im;
  1194. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1195. const float * y = yy + i * QK_K + y_offset;
  1196. const uint8_t * q = x[i].qs + q_offset;
  1197. const uint8_t * h = x[i].hmask + l0;
  1198. const uint16_t * a = (const uint16_t *)x[i].scales;
  1199. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1200. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1201. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1202. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1203. const float d = x[i].d;
  1204. float sum = 0;
  1205. for (int l = 0; l < n; ++l) {
  1206. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1207. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1208. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1209. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1210. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1211. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1212. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1213. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1214. }
  1215. tmp += d * sum;
  1216. }
  1217. #else
  1218. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1219. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1220. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1221. const int in = offset/8; // 0 or 1
  1222. const int im = offset%8; // 0...7
  1223. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1224. const float * y = yy + i * QK_K + offset;
  1225. const uint8_t * q = x[i].qs + offset;
  1226. const uint8_t * s = x[i].scales;
  1227. const float dall = (float)x[i].d;
  1228. float sum = 0;
  1229. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1230. const uint8_t hl = x[i].hmask[im+l] >> in;
  1231. const uint8_t ql = q[l];
  1232. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1233. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1234. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1235. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1236. }
  1237. tmp += sum;
  1238. }
  1239. #endif
  1240. // sum up partial sums and write back result
  1241. #pragma unroll
  1242. for (int mask = 16; mask > 0; mask >>= 1) {
  1243. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1244. }
  1245. if (threadIdx.x == 0) {
  1246. dst[row] = tmp;
  1247. }
  1248. }
  1249. static __global__ void dequantize_mul_mat_vec_q4_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  1250. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1251. if (row > nrows) return;
  1252. const int num_blocks_per_row = ncols / QK_K;
  1253. const int ib0 = row*num_blocks_per_row;
  1254. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1255. #if QK_K == 256
  1256. const uint16_t kmask1 = 0x3f3f;
  1257. const uint16_t kmask2 = 0x0f0f;
  1258. const uint16_t kmask3 = 0xc0c0;
  1259. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1260. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1261. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1262. const int il = tid/step; // 0...3
  1263. const int ir = tid - step*il; // 0...7 or 0...3
  1264. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1265. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1266. const int in = il%2;
  1267. const int l0 = n*(2*ir + in);
  1268. const int q_offset = 32*im + l0;
  1269. const int y_offset = 64*im + l0;
  1270. uint16_t aux[4];
  1271. const uint8_t * sc = (const uint8_t *)aux;
  1272. #if K_QUANTS_PER_ITERATION == 2
  1273. uint32_t q32[4];
  1274. const uint8_t * q4 = (const uint8_t *)q32;
  1275. #else
  1276. uint16_t q16[4];
  1277. const uint8_t * q4 = (const uint8_t *)q16;
  1278. #endif
  1279. float tmp = 0; // partial sum for thread in warp
  1280. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1281. const float * y1 = yy + i*QK_K + y_offset;
  1282. const float * y2 = y1 + 128;
  1283. const float dall = __low2half(x[i].dm);
  1284. const float dmin = __high2half(x[i].dm);
  1285. const uint16_t * a = (const uint16_t *)x[i].scales;
  1286. aux[0] = a[im+0] & kmask1;
  1287. aux[1] = a[im+2] & kmask1;
  1288. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1289. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1290. #if K_QUANTS_PER_ITERATION == 2
  1291. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1292. const uint32_t * q2 = q1 + 16;
  1293. q32[0] = q1[0] & 0x0f0f0f0f;
  1294. q32[1] = q1[0] & 0xf0f0f0f0;
  1295. q32[2] = q2[0] & 0x0f0f0f0f;
  1296. q32[3] = q2[0] & 0xf0f0f0f0;
  1297. float4 s = {0.f, 0.f, 0.f, 0.f};
  1298. float smin = 0;
  1299. for (int l = 0; l < 4; ++l) {
  1300. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1301. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1302. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1303. }
  1304. tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
  1305. #else
  1306. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1307. const uint16_t * q2 = q1 + 32;
  1308. q16[0] = q1[0] & 0x0f0f;
  1309. q16[1] = q1[0] & 0xf0f0;
  1310. q16[2] = q2[0] & 0x0f0f;
  1311. q16[3] = q2[0] & 0xf0f0;
  1312. float4 s = {0.f, 0.f, 0.f, 0.f};
  1313. float smin = 0;
  1314. for (int l = 0; l < 2; ++l) {
  1315. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1316. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1317. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1318. }
  1319. tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
  1320. #endif
  1321. }
  1322. #else
  1323. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1324. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1325. const int step = tid * K_QUANTS_PER_ITERATION;
  1326. uint16_t aux16[2];
  1327. const uint8_t * s = (const uint8_t *)aux16;
  1328. float tmp = 0;
  1329. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1330. const uint8_t * q = x[i].qs + step;
  1331. const float * y = yy + i*QK_K + step;
  1332. const uint16_t * a = (const uint16_t *)x[i].scales;
  1333. aux16[0] = a[0] & 0x0f0f;
  1334. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1335. const float d = (float)x[i].dm[0];
  1336. const float m = (float)x[i].dm[1];
  1337. float sum = 0.f;
  1338. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1339. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1340. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1341. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1342. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1343. }
  1344. tmp += sum;
  1345. }
  1346. #endif
  1347. // sum up partial sums and write back result
  1348. #pragma unroll
  1349. for (int mask = 16; mask > 0; mask >>= 1) {
  1350. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1351. }
  1352. if (tid == 0) {
  1353. dst[row] = tmp;
  1354. }
  1355. }
  1356. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1357. const int row = blockIdx.x;
  1358. const int num_blocks_per_row = ncols / QK_K;
  1359. const int ib0 = row*num_blocks_per_row;
  1360. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1361. float tmp = 0; // partial sum for thread in warp
  1362. #if QK_K == 256
  1363. const uint16_t kmask1 = 0x3f3f;
  1364. const uint16_t kmask2 = 0x0f0f;
  1365. const uint16_t kmask3 = 0xc0c0;
  1366. const int tid = threadIdx.x/2; // 0...15
  1367. const int ix = threadIdx.x%2;
  1368. const int il = tid/4; // 0...3
  1369. const int ir = tid - 4*il;// 0...3
  1370. const int n = 2;
  1371. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1372. const int in = il%2;
  1373. const int l0 = n*(2*ir + in);
  1374. const int q_offset = 32*im + l0;
  1375. const int y_offset = 64*im + l0;
  1376. const uint8_t hm1 = 1 << (2*im);
  1377. const uint8_t hm2 = hm1 << 4;
  1378. uint16_t aux[4];
  1379. const uint8_t * sc = (const uint8_t *)aux;
  1380. uint16_t q16[8];
  1381. const uint8_t * q4 = (const uint8_t *)q16;
  1382. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1383. const uint8_t * ql1 = x[i].qs + q_offset;
  1384. const uint8_t * qh = x[i].qh + l0;
  1385. const float * y1 = yy + i*QK_K + y_offset;
  1386. const float * y2 = y1 + 128;
  1387. const float dall = __low2half(x[i].dm);
  1388. const float dmin = __high2half(x[i].dm);
  1389. const uint16_t * a = (const uint16_t *)x[i].scales;
  1390. aux[0] = a[im+0] & kmask1;
  1391. aux[1] = a[im+2] & kmask1;
  1392. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1393. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1394. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1395. float smin = 0;
  1396. const uint16_t * q1 = (const uint16_t *)ql1;
  1397. const uint16_t * q2 = q1 + 32;
  1398. q16[0] = q1[0] & 0x0f0f;
  1399. q16[1] = q1[8] & 0x0f0f;
  1400. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1401. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1402. q16[4] = q2[0] & 0x0f0f;
  1403. q16[5] = q2[8] & 0x0f0f;
  1404. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1405. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1406. for (int l = 0; l < n; ++l) {
  1407. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1408. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1409. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1410. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1411. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1412. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1413. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1414. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1415. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1416. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1417. }
  1418. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1419. }
  1420. #else
  1421. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1422. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1423. const int step = tid * K_QUANTS_PER_ITERATION;
  1424. const int im = step/8;
  1425. const int in = step%8;
  1426. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1427. const uint8_t * q = x[i].qs + step;
  1428. const int8_t * s = x[i].scales;
  1429. const float * y = yy + i*QK_K + step;
  1430. const float d = x[i].d;
  1431. float sum = 0.f;
  1432. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1433. const uint8_t h = x[i].qh[in+j] >> im;
  1434. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1435. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1436. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1437. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1438. }
  1439. tmp += sum;
  1440. }
  1441. #endif
  1442. // sum up partial sums and write back result
  1443. #pragma unroll
  1444. for (int mask = 16; mask > 0; mask >>= 1) {
  1445. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1446. }
  1447. if (threadIdx.x == 0) {
  1448. dst[row] = tmp;
  1449. }
  1450. }
  1451. static __global__ void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
  1452. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1453. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1454. if (row > nrows) return;
  1455. const int num_blocks_per_row = ncols / QK_K;
  1456. const int ib0 = row*num_blocks_per_row;
  1457. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1458. #if QK_K == 256
  1459. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1460. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1461. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1462. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1463. const int in = tid - step*im; // 0...15 or 0...7
  1464. #if K_QUANTS_PER_ITERATION == 1
  1465. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1466. const int is = 0;
  1467. #else
  1468. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1469. const int is = in / 4;
  1470. #endif
  1471. const int ql_offset = 64*im + l0;
  1472. const int qh_offset = 32*im + l0;
  1473. const int s_offset = 8*im + is;
  1474. const int y_offset = 128*im + l0;
  1475. float tmp = 0; // partial sum for thread in warp
  1476. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1477. const float * y = yy + i * QK_K + y_offset;
  1478. const uint8_t * ql = x[i].ql + ql_offset;
  1479. const uint8_t * qh = x[i].qh + qh_offset;
  1480. const int8_t * s = x[i].scales + s_offset;
  1481. const float d = x[i].d;
  1482. #if K_QUANTS_PER_ITERATION == 1
  1483. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1484. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1485. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1486. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1487. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1488. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1489. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1490. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1491. tmp += sum;
  1492. #else
  1493. float sum = 0;
  1494. for (int l = 0; l < 4; ++l) {
  1495. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1496. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1497. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1498. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1499. }
  1500. tmp += sum;
  1501. #endif
  1502. }
  1503. #else
  1504. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1505. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1506. const int step = tid * K_QUANTS_PER_ITERATION;
  1507. float tmp = 0; // partial sum for thread in warp
  1508. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1509. const float * y = yy + i * QK_K + step;
  1510. const uint8_t * ql = x[i].ql + step;
  1511. const uint8_t * qh = x[i].qh + step;
  1512. const int8_t * s = x[i].scales;
  1513. const float d = x[i+0].d;
  1514. float sum = 0;
  1515. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1516. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1517. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1518. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1519. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1520. }
  1521. tmp += sum;
  1522. }
  1523. #endif
  1524. // sum up partial sums and write back result
  1525. #pragma unroll
  1526. for (int mask = 16; mask > 0; mask >>= 1) {
  1527. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1528. }
  1529. if (tid == 0) {
  1530. dst[row] = tmp;
  1531. }
  1532. }
  1533. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1534. const half * x = (const half *) vx;
  1535. // automatic half -> float type cast if dfloat == float
  1536. v.x = x[ib + iqs + 0];
  1537. v.y = x[ib + iqs + 1];
  1538. }
  1539. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1540. const float * x = (const float *) vx;
  1541. // automatic half -> float type cast if dfloat == float
  1542. v.x = x[ib + iqs + 0];
  1543. v.y = x[ib + iqs + 1];
  1544. }
  1545. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1546. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1547. if (ix >= kx_padded) {
  1548. return;
  1549. }
  1550. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1551. const int i_padded = iy*kx_padded + ix;
  1552. block_q8_1 * y = (block_q8_1 *) vy;
  1553. const int ib = i_padded / QK8_1; // block index
  1554. const int iqs = i_padded % QK8_1; // quant index
  1555. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1556. float amax = fabsf(xi);
  1557. float sum = xi;
  1558. #pragma unroll
  1559. for (int mask = 16; mask > 0; mask >>= 1) {
  1560. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1561. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1562. }
  1563. const float d = amax / 127;
  1564. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1565. y[ib].qs[iqs] = q;
  1566. if (iqs > 0) {
  1567. return;
  1568. }
  1569. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1570. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1571. }
  1572. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1573. static __global__ void k_get_rows(
  1574. const void * src0, const int32_t * src1, dst_t * dst,
  1575. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1576. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1577. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1578. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1579. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1580. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1581. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1582. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1583. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1584. if (i00 >= ne00) {
  1585. return;
  1586. }
  1587. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1588. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1589. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1590. const int ib = i00/qk; // block index
  1591. const int iqs = (i00%qk)/qr; // quant index
  1592. const int iybs = i00 - i00%qk; // dst block start index
  1593. const int y_offset = qr == 1 ? 1 : qk/2;
  1594. // dequantize
  1595. dfloat2 v;
  1596. dequantize_kernel(src0_row, ib, iqs, v);
  1597. dst_row[iybs + iqs + 0] = v.x;
  1598. dst_row[iybs + iqs + y_offset] = v.y;
  1599. }
  1600. template<typename src0_t, typename dst_t>
  1601. static __global__ void k_get_rows_float(
  1602. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1603. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1604. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1605. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1606. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1607. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1608. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1609. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1610. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1611. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1612. if (i00 >= ne00) {
  1613. return;
  1614. }
  1615. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1616. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1617. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1618. dst_row[i00] = src0_row[i00];
  1619. }
  1620. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1621. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1622. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1623. if (i >= k) {
  1624. return;
  1625. }
  1626. const int ib = i/qk; // block index
  1627. const int iqs = (i%qk)/qr; // quant index
  1628. const int iybs = i - i%qk; // y block start index
  1629. const int y_offset = qr == 1 ? 1 : qk/2;
  1630. // dequantize
  1631. dfloat2 v;
  1632. dequantize_kernel(vx, ib, iqs, v);
  1633. y[iybs + iqs + 0] = v.x;
  1634. y[iybs + iqs + y_offset] = v.y;
  1635. }
  1636. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1637. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1638. #define VDR_Q4_0_Q8_1_MMVQ 2
  1639. #define VDR_Q4_0_Q8_1_MMQ 4
  1640. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1641. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1642. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1643. int sumi = 0;
  1644. #pragma unroll
  1645. for (int i = 0; i < vdr; ++i) {
  1646. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1647. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1648. // SIMD dot product of quantized values
  1649. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1650. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1651. }
  1652. const float2 ds8f = __half22float2(ds8);
  1653. // second part effectively subtracts 8 from each quant value
  1654. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1655. #else
  1656. bad_arch();
  1657. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1658. }
  1659. #define VDR_Q4_1_Q8_1_MMVQ 2
  1660. #define VDR_Q4_1_Q8_1_MMQ 4
  1661. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1662. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1663. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1664. int sumi = 0;
  1665. #pragma unroll
  1666. for (int i = 0; i < vdr; ++i) {
  1667. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1668. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1669. // SIMD dot product of quantized values
  1670. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1671. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1672. }
  1673. #ifdef GGML_CUDA_F16
  1674. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1675. const float d4d8 = tmp.x;
  1676. const float m4s8 = tmp.y;
  1677. #else
  1678. const float2 dm4f = __half22float2(dm4);
  1679. const float2 ds8f = __half22float2(ds8);
  1680. const float d4d8 = dm4f.x * ds8f.x;
  1681. const float m4s8 = dm4f.y * ds8f.y;
  1682. #endif // GGML_CUDA_F16
  1683. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1684. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1685. #else
  1686. bad_arch();
  1687. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1688. }
  1689. #define VDR_Q5_0_Q8_1_MMVQ 2
  1690. #define VDR_Q5_0_Q8_1_MMQ 4
  1691. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1692. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1693. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1694. int sumi = 0;
  1695. #pragma unroll
  1696. for (int i = 0; i < vdr; ++i) {
  1697. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1698. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1699. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1700. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1701. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1702. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1703. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1704. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1705. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1706. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1707. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1708. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1709. }
  1710. const float2 ds8f = __half22float2(ds8);
  1711. // second part effectively subtracts 16 from each quant value
  1712. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1713. #else
  1714. bad_arch();
  1715. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1716. }
  1717. #define VDR_Q5_1_Q8_1_MMVQ 2
  1718. #define VDR_Q5_1_Q8_1_MMQ 4
  1719. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1720. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1721. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1722. int sumi = 0;
  1723. #pragma unroll
  1724. for (int i = 0; i < vdr; ++i) {
  1725. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1726. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1727. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1728. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1729. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1730. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1731. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1732. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1733. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1734. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1735. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1736. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1737. }
  1738. #ifdef GGML_CUDA_F16
  1739. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1740. const float d5d8 = tmp.x;
  1741. const float m5s8 = tmp.y;
  1742. #else
  1743. const float2 dm5f = __half22float2(dm5);
  1744. const float2 ds8f = __half22float2(ds8);
  1745. const float d5d8 = dm5f.x * ds8f.x;
  1746. const float m5s8 = dm5f.y * ds8f.y;
  1747. #endif // GGML_CUDA_F16
  1748. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1749. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1750. #else
  1751. bad_arch();
  1752. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1753. }
  1754. #define VDR_Q8_0_Q8_1_MMVQ 2
  1755. #define VDR_Q8_0_Q8_1_MMQ 8
  1756. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1757. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1758. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1759. int sumi = 0;
  1760. #pragma unroll
  1761. for (int i = 0; i < vdr; ++i) {
  1762. // SIMD dot product of quantized values
  1763. sumi = __dp4a(v[i], u[i], sumi);
  1764. }
  1765. return d8_0*d8_1 * sumi;
  1766. #else
  1767. bad_arch();
  1768. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1769. }
  1770. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1771. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1772. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1773. int sumi = 0;
  1774. #pragma unroll
  1775. for (int i = 0; i < vdr; ++i) {
  1776. // SIMD dot product of quantized values
  1777. sumi = __dp4a(v[i], u[i], sumi);
  1778. }
  1779. #ifdef GGML_CUDA_F16
  1780. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1781. const float d8d8 = tmp.x;
  1782. const float m8s8 = tmp.y;
  1783. #else
  1784. const float2 dm8f = __half22float2(dm8);
  1785. const float2 ds8f = __half22float2(ds8);
  1786. const float d8d8 = dm8f.x * ds8f.x;
  1787. const float m8s8 = dm8f.y * ds8f.y;
  1788. #endif // GGML_CUDA_F16
  1789. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1790. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1791. #else
  1792. bad_arch();
  1793. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1794. }
  1795. #define VDR_Q2_K_Q8_1_MMVQ 1
  1796. #define VDR_Q2_K_Q8_1_MMQ 2
  1797. // contiguous v/x values
  1798. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1799. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1800. const half2 & dm2, const float * __restrict__ d8) {
  1801. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1802. float sumf_d = 0.0f;
  1803. float sumf_m = 0.0f;
  1804. #pragma unroll
  1805. for (int i = 0; i < QR2_K; ++i) {
  1806. const int sc = scales[2*i];
  1807. const int vi = (v >> (2*i)) & 0x03030303;
  1808. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1809. // fill int with 4x m
  1810. int m = sc >> 4;
  1811. m |= m << 8;
  1812. m |= m << 16;
  1813. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1814. }
  1815. const float2 dm2f = __half22float2(dm2);
  1816. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1817. #else
  1818. bad_arch();
  1819. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1820. }
  1821. // contiguous u/y values
  1822. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1823. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1824. const half2 & dm2, const float & d8) {
  1825. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1826. int sumi_d = 0;
  1827. int sumi_m = 0;
  1828. #pragma unroll
  1829. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1830. int sumi_d_sc = 0;
  1831. const int sc = scales[i0 / (QI8_1/2)];
  1832. // fill int with 4x m
  1833. int m = sc >> 4;
  1834. m |= m << 8;
  1835. m |= m << 16;
  1836. #pragma unroll
  1837. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1838. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1839. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1840. }
  1841. sumi_d += sumi_d_sc * (sc & 0xF);
  1842. }
  1843. const float2 dm2f = __half22float2(dm2);
  1844. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1845. #else
  1846. bad_arch();
  1847. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1848. }
  1849. #define VDR_Q3_K_Q8_1_MMVQ 1
  1850. #define VDR_Q3_K_Q8_1_MMQ 2
  1851. // contiguous v/x values
  1852. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1853. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1854. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1855. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1856. float sumf = 0.0f;
  1857. #pragma unroll
  1858. for (int i = 0; i < QR3_K; ++i) {
  1859. const int isc = scale_offset + 2*i;
  1860. const int isc_low = isc % (QK_K/32);
  1861. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1862. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1863. const int isc_high = isc % (QK_K/64);
  1864. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1865. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1866. const int sc = (sc_low | sc_high) - 32;
  1867. const int vil = (vl >> (2*i)) & 0x03030303;
  1868. const int vih = ((vh >> i) << 2) & 0x04040404;
  1869. const int vi = __vsubss4(vil, vih);
  1870. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1871. }
  1872. return d3 * sumf;
  1873. #else
  1874. bad_arch();
  1875. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1876. }
  1877. // contiguous u/y values
  1878. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1879. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1880. const float & d3, const float & d8) {
  1881. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1882. int sumi = 0;
  1883. #pragma unroll
  1884. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1885. int sumi_sc = 0;
  1886. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1887. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1888. }
  1889. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1890. }
  1891. return d3*d8 * sumi;
  1892. #else
  1893. bad_arch();
  1894. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1895. }
  1896. #define VDR_Q4_K_Q8_1_MMVQ 2
  1897. #define VDR_Q4_K_Q8_1_MMQ 8
  1898. // contiguous v/x values
  1899. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1900. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1901. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1902. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1903. float sumf_d = 0.0f;
  1904. float sumf_m = 0.0f;
  1905. #pragma unroll
  1906. for (int i = 0; i < QR4_K; ++i) {
  1907. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1908. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1909. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1910. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1911. sumf_d += d8[i] * (dot1 * sc[i]);
  1912. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1913. }
  1914. const float2 dm4f = __half22float2(dm4);
  1915. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1916. #else
  1917. bad_arch();
  1918. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1919. }
  1920. // contiguous u/y values
  1921. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1922. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1923. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1924. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1925. float sumf_d = 0.0f;
  1926. float sumf_m = 0.0f;
  1927. #pragma unroll
  1928. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1929. int sumi_d = 0;
  1930. #pragma unroll
  1931. for (int j = 0; j < QI8_1; ++j) {
  1932. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1933. }
  1934. const float2 ds8f = __half22float2(ds8[i]);
  1935. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1936. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1937. }
  1938. const float2 dm4f = __half22float2(dm4);
  1939. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1940. #else
  1941. bad_arch();
  1942. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1943. }
  1944. #define VDR_Q5_K_Q8_1_MMVQ 2
  1945. #define VDR_Q5_K_Q8_1_MMQ 8
  1946. // contiguous v/x values
  1947. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1948. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1949. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1950. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1951. float sumf_d = 0.0f;
  1952. float sumf_m = 0.0f;
  1953. #pragma unroll
  1954. for (int i = 0; i < QR5_K; ++i) {
  1955. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1956. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1957. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1958. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1959. const int v0i = vl0i | vh0i;
  1960. const int v1i = vl1i | vh1i;
  1961. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1962. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1963. sumf_d += d8[i] * (dot1 * sc[i]);
  1964. sumf_m += d8[i] * (dot2 * m[i]);
  1965. }
  1966. const float2 dm5f = __half22float2(dm5);
  1967. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1968. #else
  1969. bad_arch();
  1970. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1971. }
  1972. // contiguous u/y values
  1973. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1974. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1975. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1976. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1977. float sumf_d = 0.0f;
  1978. float sumf_m = 0.0f;
  1979. #pragma unroll
  1980. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1981. int sumi_d = 0;
  1982. #pragma unroll
  1983. for (int j = 0; j < QI8_1; ++j) {
  1984. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1985. }
  1986. const float2 ds8f = __half22float2(ds8[i]);
  1987. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1988. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1989. }
  1990. const float2 dm4f = __half22float2(dm4);
  1991. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1992. #else
  1993. bad_arch();
  1994. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1995. }
  1996. #define VDR_Q6_K_Q8_1_MMVQ 1
  1997. #define VDR_Q6_K_Q8_1_MMQ 8
  1998. // contiguous v/x values
  1999. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2000. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2001. const float & d, const float * __restrict__ d8) {
  2002. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2003. float sumf = 0.0f;
  2004. #pragma unroll
  2005. for (int i = 0; i < QR6_K; ++i) {
  2006. const int sc = scales[4*i];
  2007. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2008. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2009. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2010. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2011. }
  2012. return d*sumf;
  2013. #else
  2014. bad_arch();
  2015. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2016. }
  2017. // contiguous u/y values
  2018. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2019. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2020. const float & d6, const float * __restrict__ d8) {
  2021. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2022. float sumf_d = 0.0f;
  2023. #pragma unroll
  2024. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2025. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2026. #pragma unroll
  2027. for (int i = i0; i < i0 + 2; ++i) {
  2028. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2029. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2030. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2031. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2032. }
  2033. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2034. }
  2035. return d6 * sumf_d;
  2036. #else
  2037. bad_arch();
  2038. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2039. }
  2040. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2041. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2042. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2043. int v[VDR_Q4_0_Q8_1_MMVQ];
  2044. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2045. #pragma unroll
  2046. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2047. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2048. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2049. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2050. }
  2051. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2052. }
  2053. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2054. (void)x_qh; (void)x_sc;
  2055. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2056. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2057. *x_ql = tile_x_qs;
  2058. *x_dm = (half2 *) tile_x_d;
  2059. }
  2060. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2061. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2062. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2063. (void)x_qh; (void)x_sc;
  2064. GGML_CUDA_ASSUME(i_offset >= 0);
  2065. GGML_CUDA_ASSUME(i_offset < nwarps);
  2066. GGML_CUDA_ASSUME(k >= 0);
  2067. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2068. const int kbx = k / QI4_0;
  2069. const int kqsx = k % QI4_0;
  2070. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2071. float * x_dmf = (float *) x_dm;
  2072. #pragma unroll
  2073. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2074. int i = i0 + i_offset;
  2075. if (need_check) {
  2076. i = min(i, i_max);
  2077. }
  2078. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2079. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2080. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2081. }
  2082. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2083. const int kbxd = k % blocks_per_tile_x_row;
  2084. #pragma unroll
  2085. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2086. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2087. if (need_check) {
  2088. i = min(i, i_max);
  2089. }
  2090. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2091. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2092. }
  2093. }
  2094. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2095. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2096. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2097. (void)x_qh; (void)x_sc;
  2098. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2099. const float * x_dmf = (const float *) x_dm;
  2100. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2101. #pragma unroll
  2102. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2103. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2104. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2105. }
  2106. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2107. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2108. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2109. }
  2110. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2111. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2112. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2113. int v[VDR_Q4_1_Q8_1_MMVQ];
  2114. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2115. #pragma unroll
  2116. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2117. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2118. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2119. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2120. }
  2121. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2122. }
  2123. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2124. (void)x_qh; (void)x_sc;
  2125. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2126. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2127. *x_ql = tile_x_qs;
  2128. *x_dm = tile_x_dm;
  2129. }
  2130. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2131. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2132. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2133. (void)x_qh; (void)x_sc;
  2134. GGML_CUDA_ASSUME(i_offset >= 0);
  2135. GGML_CUDA_ASSUME(i_offset < nwarps);
  2136. GGML_CUDA_ASSUME(k >= 0);
  2137. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2138. const int kbx = k / QI4_1;
  2139. const int kqsx = k % QI4_1;
  2140. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2141. #pragma unroll
  2142. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2143. int i = i0 + i_offset;
  2144. if (need_check) {
  2145. i = min(i, i_max);
  2146. }
  2147. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2148. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2149. }
  2150. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2151. const int kbxd = k % blocks_per_tile_x_row;
  2152. #pragma unroll
  2153. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2154. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2155. if (need_check) {
  2156. i = min(i, i_max);
  2157. }
  2158. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2159. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2160. }
  2161. }
  2162. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2163. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2164. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2165. (void)x_qh; (void)x_sc;
  2166. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2167. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2168. #pragma unroll
  2169. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2170. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2171. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2172. }
  2173. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2174. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2175. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2176. }
  2177. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2178. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2179. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2180. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2181. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2182. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2183. #pragma unroll
  2184. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2185. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2186. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2187. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2188. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2189. }
  2190. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2191. }
  2192. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2193. (void)x_qh; (void)x_sc;
  2194. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2195. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2196. *x_ql = tile_x_ql;
  2197. *x_dm = (half2 *) tile_x_d;
  2198. }
  2199. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2200. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2201. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2202. (void)x_qh; (void)x_sc;
  2203. GGML_CUDA_ASSUME(i_offset >= 0);
  2204. GGML_CUDA_ASSUME(i_offset < nwarps);
  2205. GGML_CUDA_ASSUME(k >= 0);
  2206. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2207. const int kbx = k / QI5_0;
  2208. const int kqsx = k % QI5_0;
  2209. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2210. #pragma unroll
  2211. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2212. int i = i0 + i_offset;
  2213. if (need_check) {
  2214. i = min(i, i_max);
  2215. }
  2216. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2217. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2218. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2219. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2220. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2221. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2222. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2223. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2224. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2225. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2226. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2227. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2228. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2229. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2230. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2231. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2232. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2233. }
  2234. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2235. const int kbxd = k % blocks_per_tile_x_row;
  2236. float * x_dmf = (float *) x_dm;
  2237. #pragma unroll
  2238. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2239. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2240. if (need_check) {
  2241. i = min(i, i_max);
  2242. }
  2243. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2244. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2245. }
  2246. }
  2247. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2248. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2249. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2250. (void)x_qh; (void)x_sc;
  2251. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2252. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2253. const float * x_dmf = (const float *) x_dm;
  2254. const float * y_df = (const float *) y_ds;
  2255. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2256. #pragma unroll
  2257. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2258. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2259. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2260. }
  2261. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2262. (&x_ql[i * (2*WARP_SIZE + 1) + 2 * k], u, x_dmf[index_bx], y_df[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2263. }
  2264. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2265. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2266. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2267. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2268. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2269. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2270. #pragma unroll
  2271. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2272. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2273. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2274. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2275. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2276. }
  2277. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2278. }
  2279. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2280. (void)x_qh; (void)x_sc;
  2281. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2282. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2283. *x_ql = tile_x_ql;
  2284. *x_dm = tile_x_dm;
  2285. }
  2286. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2287. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2288. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2289. (void)x_qh; (void)x_sc;
  2290. GGML_CUDA_ASSUME(i_offset >= 0);
  2291. GGML_CUDA_ASSUME(i_offset < nwarps);
  2292. GGML_CUDA_ASSUME(k >= 0);
  2293. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2294. const int kbx = k / QI5_1;
  2295. const int kqsx = k % QI5_1;
  2296. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2297. #pragma unroll
  2298. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2299. int i = i0 + i_offset;
  2300. if (need_check) {
  2301. i = min(i, i_max);
  2302. }
  2303. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2304. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2305. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2306. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2307. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2308. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2309. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2310. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2311. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2312. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2313. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2314. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2315. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2316. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2317. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2318. }
  2319. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2320. const int kbxd = k % blocks_per_tile_x_row;
  2321. #pragma unroll
  2322. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2323. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2324. if (need_check) {
  2325. i = min(i, i_max);
  2326. }
  2327. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2328. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2329. }
  2330. }
  2331. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2332. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2333. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2334. (void)x_qh; (void)x_sc;
  2335. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2336. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2337. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2338. #pragma unroll
  2339. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2340. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2341. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2342. }
  2343. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2344. (&x_ql[i * (2*WARP_SIZE + 1) + 2 * k], u, x_dm[index_bx], y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2345. }
  2346. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2347. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2348. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2349. int v[VDR_Q8_0_Q8_1_MMVQ];
  2350. int u[VDR_Q8_0_Q8_1_MMVQ];
  2351. #pragma unroll
  2352. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2353. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2354. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2355. }
  2356. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2357. }
  2358. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2359. (void)x_qh; (void)x_sc;
  2360. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2361. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2362. *x_ql = tile_x_qs;
  2363. *x_dm = (half2 *) tile_x_d;
  2364. }
  2365. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2366. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2367. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2368. (void)x_qh; (void)x_sc;
  2369. GGML_CUDA_ASSUME(i_offset >= 0);
  2370. GGML_CUDA_ASSUME(i_offset < nwarps);
  2371. GGML_CUDA_ASSUME(k >= 0);
  2372. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2373. const int kbx = k / QI8_0;
  2374. const int kqsx = k % QI8_0;
  2375. float * x_dmf = (float *) x_dm;
  2376. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2377. #pragma unroll
  2378. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2379. int i = i0 + i_offset;
  2380. if (need_check) {
  2381. i = min(i, i_max);
  2382. }
  2383. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2384. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2385. }
  2386. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2387. const int kbxd = k % blocks_per_tile_x_row;
  2388. #pragma unroll
  2389. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2390. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2391. if (need_check) {
  2392. i = min(i, i_max);
  2393. }
  2394. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2395. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2396. }
  2397. }
  2398. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2399. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2400. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2401. (void)x_qh; (void)x_sc;
  2402. const float * x_dmf = (const float *) x_dm;
  2403. const float * y_df = (const float *) y_ds;
  2404. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2405. (&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[j * WARP_SIZE + k], x_dmf[i * (WARP_SIZE/QI8_0) + i/QI8_0 + k/QI8_0],
  2406. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2407. }
  2408. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2409. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2410. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2411. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2412. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2413. const uint8_t * scales = bq2_K->scales + scale_offset;
  2414. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2415. int u[QR2_K];
  2416. float d8[QR2_K];
  2417. #pragma unroll
  2418. for (int i = 0; i < QR2_K; ++ i) {
  2419. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2420. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2421. }
  2422. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2423. }
  2424. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2425. (void)x_qh;
  2426. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2427. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2428. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2429. *x_ql = tile_x_ql;
  2430. *x_dm = tile_x_dm;
  2431. *x_sc = tile_x_sc;
  2432. }
  2433. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2434. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2435. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2436. (void)x_qh;
  2437. GGML_CUDA_ASSUME(i_offset >= 0);
  2438. GGML_CUDA_ASSUME(i_offset < nwarps);
  2439. GGML_CUDA_ASSUME(k >= 0);
  2440. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2441. const int kbx = k / QI2_K;
  2442. const int kqsx = k % QI2_K;
  2443. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2444. #pragma unroll
  2445. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2446. int i = i0 + i_offset;
  2447. if (need_check) {
  2448. i = min(i, i_max);
  2449. }
  2450. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2451. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2452. }
  2453. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2454. const int kbxd = k % blocks_per_tile_x_row;
  2455. #pragma unroll
  2456. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2457. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2458. if (need_check) {
  2459. i = min(i, i_max);
  2460. }
  2461. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2462. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2463. }
  2464. #pragma unroll
  2465. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2466. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2467. if (need_check) {
  2468. i = min(i, i_max);
  2469. }
  2470. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2471. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2472. }
  2473. }
  2474. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2475. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2476. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2477. (void)x_qh;
  2478. const int kbx = k / QI2_K;
  2479. const int ky = (k % QI2_K) * QR2_K;
  2480. const float * y_df = (const float *) y_ds;
  2481. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2482. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2483. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2484. #pragma unroll
  2485. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2486. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2487. }
  2488. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2489. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2490. return vec_dot_q2_K_q8_1_impl_mmq(v, &y_qs[index_y], scales, x_dm[i * (WARP_SIZE/QI2_K) + i/QI2_K + kbx], y_df[index_y/QI8_1]);
  2491. }
  2492. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2493. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2494. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2495. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2496. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2497. const float d = bq3_K->d;
  2498. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2499. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2500. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2501. int u[QR3_K];
  2502. float d8[QR3_K];
  2503. #pragma unroll
  2504. for (int i = 0; i < QR3_K; ++i) {
  2505. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2506. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2507. }
  2508. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2509. }
  2510. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2511. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2512. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2513. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2514. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2515. *x_ql = tile_x_ql;
  2516. *x_dm = tile_x_dm;
  2517. *x_qh = tile_x_qh;
  2518. *x_sc = tile_x_sc;
  2519. }
  2520. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2521. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2522. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2523. GGML_CUDA_ASSUME(i_offset >= 0);
  2524. GGML_CUDA_ASSUME(i_offset < nwarps);
  2525. GGML_CUDA_ASSUME(k >= 0);
  2526. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2527. const int kbx = k / QI3_K;
  2528. const int kqsx = k % QI3_K;
  2529. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2530. #pragma unroll
  2531. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2532. int i = i0 + i_offset;
  2533. if (need_check) {
  2534. i = min(i, i_max);
  2535. }
  2536. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2537. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2538. }
  2539. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2540. const int kbxd = k % blocks_per_tile_x_row;
  2541. float * x_dmf = (float *) x_dm;
  2542. #pragma unroll
  2543. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2544. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2545. if (need_check) {
  2546. i = min(i, i_max);
  2547. }
  2548. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2549. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2550. }
  2551. #pragma unroll
  2552. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2553. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2554. if (need_check) {
  2555. i = min(i, i_max);
  2556. }
  2557. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2558. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2559. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2560. }
  2561. #pragma unroll
  2562. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2563. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2564. if (need_check) {
  2565. i = min(i, i_max);
  2566. }
  2567. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2568. const int ksc = k % (QI3_K/4);
  2569. const int ksc_low = ksc % (QI3_K/8);
  2570. const int shift_low = 4 * (ksc / (QI3_K/8));
  2571. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2572. const int ksc_high = QI3_K/8;
  2573. const int shift_high = 2 * ksc;
  2574. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2575. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2576. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2577. }
  2578. }
  2579. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2580. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2581. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2582. const int kbx = k / QI3_K;
  2583. const int ky = (k % QI3_K) * QR3_K;
  2584. const float * x_dmf = (const float *) x_dm;
  2585. const float * y_df = (const float *) y_ds;
  2586. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2587. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2588. #pragma unroll
  2589. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2590. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2591. const int shift = 2 * ((ky % 32) / 8);
  2592. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2593. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2594. const int vlh = (vh << 2) & 0x04040404;
  2595. v[l] = __vsubss4(vll, vlh);
  2596. }
  2597. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2598. return vec_dot_q3_K_q8_1_impl_mmq(v, &y_qs[index_y], scales, x_dmf[i * (WARP_SIZE/QI3_K) + i/QI3_K + kbx], y_df[index_y/QI8_1]);
  2599. }
  2600. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2601. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2602. #ifndef GGML_QKK_64
  2603. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2604. int v[2];
  2605. int u[2*QR4_K];
  2606. float d8[QR4_K];
  2607. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2608. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2609. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2610. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2611. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2612. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2613. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2614. v[0] = q4[0];
  2615. v[1] = q4[4];
  2616. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2617. uint16_t aux[2];
  2618. const int j = bq8_offset/2;
  2619. if (j < 2) {
  2620. aux[0] = scales[j+0] & 0x3f3f;
  2621. aux[1] = scales[j+2] & 0x3f3f;
  2622. } else {
  2623. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2624. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2625. }
  2626. const uint8_t * sc = (const uint8_t *)aux;
  2627. const uint8_t * m = sc + 2;
  2628. for (int i = 0; i < QR4_K; ++i) {
  2629. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2630. d8[i] = __low2half(bq8i->ds);
  2631. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2632. u[2*i+0] = q8[0];
  2633. u[2*i+1] = q8[4];
  2634. }
  2635. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2636. #else
  2637. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2638. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2639. float sumf_d = 0.0f;
  2640. float sumf_m = 0.0f;
  2641. uint16_t aux16[2];
  2642. const uint8_t * s = (const uint8_t *)aux16;
  2643. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2644. aux16[0] = a[0] & 0x0f0f;
  2645. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2646. const float dall = bq4_K->dm[0];
  2647. const float dmin = bq4_K->dm[1];
  2648. const float d8_1 = __low2float(bq8_1[0].ds);
  2649. const float d8_2 = __low2float(bq8_1[1].ds);
  2650. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2651. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2652. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2653. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2654. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2655. const int v1 = q4[0];
  2656. const int v2 = q4[4];
  2657. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2658. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2659. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2660. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2661. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2662. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2663. return dall * sumf_d - dmin * sumf_m;
  2664. #else
  2665. bad_arch();
  2666. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2667. #endif
  2668. }
  2669. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2670. (void)x_qh;
  2671. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2672. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2673. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2674. *x_ql = tile_x_ql;
  2675. *x_dm = tile_x_dm;
  2676. *x_sc = tile_x_sc;
  2677. }
  2678. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2679. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2680. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2681. (void)x_qh;
  2682. GGML_CUDA_ASSUME(i_offset >= 0);
  2683. GGML_CUDA_ASSUME(i_offset < nwarps);
  2684. GGML_CUDA_ASSUME(k >= 0);
  2685. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2686. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2687. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2688. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2689. #pragma unroll
  2690. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2691. int i = i0 + i_offset;
  2692. if (need_check) {
  2693. i = min(i, i_max);
  2694. }
  2695. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2696. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2697. }
  2698. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2699. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2700. #pragma unroll
  2701. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2702. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2703. if (need_check) {
  2704. i = min(i, i_max);
  2705. }
  2706. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2707. #if QK_K == 256
  2708. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2709. #else
  2710. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2711. #endif
  2712. }
  2713. #pragma unroll
  2714. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2715. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2716. if (need_check) {
  2717. i = min(i, i_max);
  2718. }
  2719. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2720. const int * scales = (const int *) bxi->scales;
  2721. const int ksc = k % (WARP_SIZE/8);
  2722. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2723. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2724. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2725. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2726. }
  2727. }
  2728. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2729. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2730. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2731. (void)x_qh;
  2732. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2733. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2734. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2735. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2736. }
  2737. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2738. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2739. #ifndef GGML_QKK_64
  2740. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2741. int vl[2];
  2742. int vh[2];
  2743. int u[2*QR5_K];
  2744. float d8[QR5_K];
  2745. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2746. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2747. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2748. vl[0] = ql[0];
  2749. vl[1] = ql[4];
  2750. vh[0] = qh[0] >> bq8_offset;
  2751. vh[1] = qh[4] >> bq8_offset;
  2752. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2753. uint16_t aux[2];
  2754. const int j = bq8_offset/2;
  2755. if (j < 2) {
  2756. aux[0] = scales[j+0] & 0x3f3f;
  2757. aux[1] = scales[j+2] & 0x3f3f;
  2758. } else {
  2759. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2760. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2761. }
  2762. const uint8_t * sc = (const uint8_t *)aux;
  2763. const uint8_t * m = sc + 2;
  2764. #pragma unroll
  2765. for (int i = 0; i < QR5_K; ++i) {
  2766. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2767. d8[i] = __low2float(bq8i->ds);
  2768. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2769. u[2*i+0] = q8[0];
  2770. u[2*i+1] = q8[4];
  2771. }
  2772. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2773. #else
  2774. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2775. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2776. const int8_t * s = bq5_K->scales;
  2777. const float d = bq5_K->d;
  2778. const float d8_1 = __low2half(bq8_1[0].ds);
  2779. const float d8_2 = __low2half(bq8_1[1].ds);
  2780. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2781. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2782. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2783. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2784. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2785. const int vl1 = ql[0];
  2786. const int vl2 = ql[4];
  2787. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2788. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2789. const int in = step%8; // 0, 4, 0, 4
  2790. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2791. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2792. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2793. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2794. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2795. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2796. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2797. return d * sumf_d;
  2798. #else
  2799. bad_arch();
  2800. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2801. #endif
  2802. }
  2803. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2804. (void)x_qh;
  2805. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2806. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2807. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2808. *x_ql = tile_x_ql;
  2809. *x_dm = tile_x_dm;
  2810. *x_sc = tile_x_sc;
  2811. }
  2812. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2813. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2814. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2815. (void)x_qh;
  2816. GGML_CUDA_ASSUME(i_offset >= 0);
  2817. GGML_CUDA_ASSUME(i_offset < nwarps);
  2818. GGML_CUDA_ASSUME(k >= 0);
  2819. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2820. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2821. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2822. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2823. #pragma unroll
  2824. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2825. int i = i0 + i_offset;
  2826. if (need_check) {
  2827. i = min(i, i_max);
  2828. }
  2829. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2830. const int ky = QR5_K*kqsx;
  2831. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2832. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2833. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2834. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2835. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2836. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2837. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2838. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2839. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2840. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2841. }
  2842. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2843. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2844. #pragma unroll
  2845. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2846. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2847. if (need_check) {
  2848. i = min(i, i_max);
  2849. }
  2850. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2851. #if QK_K == 256
  2852. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2853. #endif
  2854. }
  2855. #pragma unroll
  2856. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2857. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2858. if (need_check) {
  2859. i = min(i, i_max);
  2860. }
  2861. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2862. const int * scales = (const int *) bxi->scales;
  2863. const int ksc = k % (WARP_SIZE/8);
  2864. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2865. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2866. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2867. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2868. }
  2869. }
  2870. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2871. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2872. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2873. (void)x_qh;
  2874. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2875. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2876. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2877. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2878. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2879. }
  2880. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2881. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2882. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2883. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2884. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2885. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2886. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2887. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2888. const int8_t * scales = bq6_K->scales + scale_offset;
  2889. int u[QR6_K];
  2890. float d8[QR6_K];
  2891. #pragma unroll
  2892. for (int i = 0; i < QR6_K; ++i) {
  2893. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2894. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2895. }
  2896. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2897. }
  2898. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2899. (void)x_qh;
  2900. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2901. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2902. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2903. *x_ql = tile_x_ql;
  2904. *x_dm = tile_x_dm;
  2905. *x_sc = tile_x_sc;
  2906. }
  2907. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2908. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2909. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2910. (void)x_qh;
  2911. GGML_CUDA_ASSUME(i_offset >= 0);
  2912. GGML_CUDA_ASSUME(i_offset < nwarps);
  2913. GGML_CUDA_ASSUME(k >= 0);
  2914. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2915. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2916. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2917. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2918. #pragma unroll
  2919. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2920. int i = i0 + i_offset;
  2921. if (need_check) {
  2922. i = min(i, i_max);
  2923. }
  2924. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2925. const int ky = QR6_K*kqsx;
  2926. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2927. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2928. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2929. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2930. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2931. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2932. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2933. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2934. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2935. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2936. }
  2937. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2938. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2939. float * x_dmf = (float *) x_dm;
  2940. #pragma unroll
  2941. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2942. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2943. if (need_check) {
  2944. i = min(i, i_max);
  2945. }
  2946. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2947. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2948. }
  2949. #pragma unroll
  2950. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2951. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2952. if (need_check) {
  2953. i = min(i, i_max);
  2954. }
  2955. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2956. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2957. }
  2958. }
  2959. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2960. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2961. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2962. (void)x_qh;
  2963. const float * x_dmf = (const float *) x_dm;
  2964. const float * y_df = (const float *) y_ds;
  2965. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2966. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2967. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2968. return vec_dot_q6_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, x_dmf[i * (WARP_SIZE/QI6_K) + i/QI6_K], &y_df[index_y/QI8_1]);
  2969. }
  2970. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2971. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2972. static __device__ __forceinline__ void mul_mat_q(
  2973. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2974. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2975. const block_q_t * x = (const block_q_t *) vx;
  2976. const block_q8_1 * y = (const block_q8_1 *) vy;
  2977. const int blocks_per_row_x = ncols_x / qk;
  2978. const int blocks_per_col_y = nrows_y / QK8_1;
  2979. const int blocks_per_warp = WARP_SIZE / qi;
  2980. const int & ncols_dst = ncols_y;
  2981. const int row_dst_0 = blockIdx.x*mmq_y;
  2982. const int & row_x_0 = row_dst_0;
  2983. const int col_dst_0 = blockIdx.y*mmq_x;
  2984. const int & col_y_0 = col_dst_0;
  2985. int * tile_x_ql = nullptr;
  2986. half2 * tile_x_dm = nullptr;
  2987. int * tile_x_qh = nullptr;
  2988. int * tile_x_sc = nullptr;
  2989. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2990. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2991. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2992. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2993. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2994. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2995. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2996. #pragma unroll
  2997. for (int ir = 0; ir < qr; ++ir) {
  2998. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2999. const int kbxd = kqs / QI8_1;
  3000. #pragma unroll
  3001. for (int i = 0; i < mmq_x; i += nwarps) {
  3002. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3003. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3004. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3005. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3006. }
  3007. #pragma unroll
  3008. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3009. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3010. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3011. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3012. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3013. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3014. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3015. if (need_sum) {
  3016. *dsi_dst = *dsi_src;
  3017. } else {
  3018. float * dfi_dst = (float *) dsi_dst;
  3019. *dfi_dst = __low2half(*dsi_src);
  3020. }
  3021. }
  3022. __syncthreads();
  3023. // #pragma unroll // unrolling this loop causes too much register pressure
  3024. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3025. #pragma unroll
  3026. for (int j = 0; j < mmq_x; j += nwarps) {
  3027. #pragma unroll
  3028. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3029. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3030. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3031. threadIdx.x + i, threadIdx.y + j, k);
  3032. }
  3033. }
  3034. }
  3035. __syncthreads();
  3036. }
  3037. }
  3038. #pragma unroll
  3039. for (int j = 0; j < mmq_x; j += nwarps) {
  3040. const int col_dst = col_dst_0 + j + threadIdx.y;
  3041. if (col_dst >= ncols_dst) {
  3042. return;
  3043. }
  3044. #pragma unroll
  3045. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3046. const int row_dst = row_dst_0 + threadIdx.x + i;
  3047. if (row_dst >= nrows_dst) {
  3048. continue;
  3049. }
  3050. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3051. }
  3052. }
  3053. }
  3054. #define MMQ_X_Q4_0_RDNA2 64
  3055. #define MMQ_Y_Q4_0_RDNA2 128
  3056. #define NWARPS_Q4_0_RDNA2 8
  3057. #define MMQ_X_Q4_0_RDNA1 64
  3058. #define MMQ_Y_Q4_0_RDNA1 64
  3059. #define NWARPS_Q4_0_RDNA1 8
  3060. #if defined(CUDA_USE_TENSOR_CORES)
  3061. #define MMQ_X_Q4_0_AMPERE 4
  3062. #define MMQ_Y_Q4_0_AMPERE 32
  3063. #define NWARPS_Q4_0_AMPERE 4
  3064. #else
  3065. #define MMQ_X_Q4_0_AMPERE 64
  3066. #define MMQ_Y_Q4_0_AMPERE 128
  3067. #define NWARPS_Q4_0_AMPERE 4
  3068. #endif
  3069. #define MMQ_X_Q4_0_PASCAL 64
  3070. #define MMQ_Y_Q4_0_PASCAL 64
  3071. #define NWARPS_Q4_0_PASCAL 8
  3072. template <bool need_check> static __global__ void
  3073. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3074. #if defined(RDNA3) || defined(RDNA2)
  3075. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3076. #endif // defined(RDNA3) || defined(RDNA2)
  3077. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3078. mul_mat_q4_0(
  3079. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3080. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3081. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3082. #if defined(RDNA3) || defined(RDNA2)
  3083. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3084. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3085. const int nwarps = NWARPS_Q4_0_RDNA2;
  3086. #else
  3087. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3088. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3089. const int nwarps = NWARPS_Q4_0_RDNA1;
  3090. #endif // defined(RDNA3) || defined(RDNA2)
  3091. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3092. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3093. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3094. #elif __CUDA_ARCH__ >= CC_VOLTA
  3095. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3096. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3097. const int nwarps = NWARPS_Q4_0_AMPERE;
  3098. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3099. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3100. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3101. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3102. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3103. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3104. const int nwarps = NWARPS_Q4_0_PASCAL;
  3105. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3106. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3107. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3108. #else
  3109. (void) vec_dot_q4_0_q8_1_mul_mat;
  3110. bad_arch();
  3111. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3112. }
  3113. #define MMQ_X_Q4_1_RDNA2 64
  3114. #define MMQ_Y_Q4_1_RDNA2 128
  3115. #define NWARPS_Q4_1_RDNA2 8
  3116. #define MMQ_X_Q4_1_RDNA1 64
  3117. #define MMQ_Y_Q4_1_RDNA1 64
  3118. #define NWARPS_Q4_1_RDNA1 8
  3119. #if defined(CUDA_USE_TENSOR_CORES)
  3120. #define MMQ_X_Q4_1_AMPERE 4
  3121. #define MMQ_Y_Q4_1_AMPERE 32
  3122. #define NWARPS_Q4_1_AMPERE 4
  3123. #else
  3124. #define MMQ_X_Q4_1_AMPERE 64
  3125. #define MMQ_Y_Q4_1_AMPERE 128
  3126. #define NWARPS_Q4_1_AMPERE 4
  3127. #endif
  3128. #define MMQ_X_Q4_1_PASCAL 64
  3129. #define MMQ_Y_Q4_1_PASCAL 64
  3130. #define NWARPS_Q4_1_PASCAL 8
  3131. template <bool need_check> static __global__ void
  3132. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3133. #if defined(RDNA3) || defined(RDNA2)
  3134. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3135. #endif // defined(RDNA3) || defined(RDNA2)
  3136. #elif __CUDA_ARCH__ < CC_VOLTA
  3137. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3138. #endif // __CUDA_ARCH__ < CC_VOLTA
  3139. mul_mat_q4_1(
  3140. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3141. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3142. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3143. #if defined(RDNA3) || defined(RDNA2)
  3144. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3145. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3146. const int nwarps = NWARPS_Q4_1_RDNA2;
  3147. #else
  3148. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3149. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3150. const int nwarps = NWARPS_Q4_1_RDNA1;
  3151. #endif // defined(RDNA3) || defined(RDNA2)
  3152. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3153. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3154. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3155. #elif __CUDA_ARCH__ >= CC_VOLTA
  3156. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3157. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3158. const int nwarps = NWARPS_Q4_1_AMPERE;
  3159. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3160. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3161. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3162. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3163. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3164. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3165. const int nwarps = NWARPS_Q4_1_PASCAL;
  3166. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3167. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3168. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3169. #else
  3170. (void) vec_dot_q4_1_q8_1_mul_mat;
  3171. bad_arch();
  3172. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3173. }
  3174. #define MMQ_X_Q5_0_RDNA2 64
  3175. #define MMQ_Y_Q5_0_RDNA2 128
  3176. #define NWARPS_Q5_0_RDNA2 8
  3177. #define MMQ_X_Q5_0_RDNA1 64
  3178. #define MMQ_Y_Q5_0_RDNA1 64
  3179. #define NWARPS_Q5_0_RDNA1 8
  3180. #if defined(CUDA_USE_TENSOR_CORES)
  3181. #define MMQ_X_Q5_0_AMPERE 4
  3182. #define MMQ_Y_Q5_0_AMPERE 32
  3183. #define NWARPS_Q5_0_AMPERE 4
  3184. #else
  3185. #define MMQ_X_Q5_0_AMPERE 128
  3186. #define MMQ_Y_Q5_0_AMPERE 64
  3187. #define NWARPS_Q5_0_AMPERE 4
  3188. #endif
  3189. #define MMQ_X_Q5_0_PASCAL 64
  3190. #define MMQ_Y_Q5_0_PASCAL 64
  3191. #define NWARPS_Q5_0_PASCAL 8
  3192. template <bool need_check> static __global__ void
  3193. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3194. #if defined(RDNA3) || defined(RDNA2)
  3195. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3196. #endif // defined(RDNA3) || defined(RDNA2)
  3197. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3198. mul_mat_q5_0(
  3199. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3200. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3201. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3202. #if defined(RDNA3) || defined(RDNA2)
  3203. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3204. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3205. const int nwarps = NWARPS_Q5_0_RDNA2;
  3206. #else
  3207. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3208. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3209. const int nwarps = NWARPS_Q5_0_RDNA1;
  3210. #endif // defined(RDNA3) || defined(RDNA2)
  3211. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3212. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3213. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3214. #elif __CUDA_ARCH__ >= CC_VOLTA
  3215. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3216. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3217. const int nwarps = NWARPS_Q5_0_AMPERE;
  3218. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3219. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3220. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3221. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3222. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3223. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3224. const int nwarps = NWARPS_Q5_0_PASCAL;
  3225. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3226. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3227. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3228. #else
  3229. (void) vec_dot_q5_0_q8_1_mul_mat;
  3230. bad_arch();
  3231. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3232. }
  3233. #define MMQ_X_Q5_1_RDNA2 64
  3234. #define MMQ_Y_Q5_1_RDNA2 128
  3235. #define NWARPS_Q5_1_RDNA2 8
  3236. #define MMQ_X_Q5_1_RDNA1 64
  3237. #define MMQ_Y_Q5_1_RDNA1 64
  3238. #define NWARPS_Q5_1_RDNA1 8
  3239. #if defined(CUDA_USE_TENSOR_CORES)
  3240. #define MMQ_X_Q5_1_AMPERE 4
  3241. #define MMQ_Y_Q5_1_AMPERE 32
  3242. #define NWARPS_Q5_1_AMPERE 4
  3243. #else
  3244. #define MMQ_X_Q5_1_AMPERE 128
  3245. #define MMQ_Y_Q5_1_AMPERE 64
  3246. #define NWARPS_Q5_1_AMPERE 4
  3247. #endif
  3248. #define MMQ_X_Q5_1_PASCAL 64
  3249. #define MMQ_Y_Q5_1_PASCAL 64
  3250. #define NWARPS_Q5_1_PASCAL 8
  3251. template <bool need_check> static __global__ void
  3252. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3253. #if defined(RDNA3) || defined(RDNA2)
  3254. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3255. #endif // defined(RDNA3) || defined(RDNA2)
  3256. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3257. mul_mat_q5_1(
  3258. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3259. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3260. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3261. #if defined(RDNA3) || defined(RDNA2)
  3262. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3263. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3264. const int nwarps = NWARPS_Q5_1_RDNA2;
  3265. #else
  3266. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3267. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3268. const int nwarps = NWARPS_Q5_1_RDNA1;
  3269. #endif // defined(RDNA3) || defined(RDNA2)
  3270. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3271. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3272. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3273. #elif __CUDA_ARCH__ >= CC_VOLTA
  3274. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3275. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3276. const int nwarps = NWARPS_Q5_1_AMPERE;
  3277. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3278. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3279. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3280. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3281. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3282. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3283. const int nwarps = NWARPS_Q5_1_PASCAL;
  3284. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3285. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3286. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3287. #else
  3288. (void) vec_dot_q5_1_q8_1_mul_mat;
  3289. bad_arch();
  3290. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3291. }
  3292. #define MMQ_X_Q8_0_RDNA2 64
  3293. #define MMQ_Y_Q8_0_RDNA2 128
  3294. #define NWARPS_Q8_0_RDNA2 8
  3295. #define MMQ_X_Q8_0_RDNA1 64
  3296. #define MMQ_Y_Q8_0_RDNA1 64
  3297. #define NWARPS_Q8_0_RDNA1 8
  3298. #if defined(CUDA_USE_TENSOR_CORES)
  3299. #define MMQ_X_Q8_0_AMPERE 4
  3300. #define MMQ_Y_Q8_0_AMPERE 32
  3301. #define NWARPS_Q8_0_AMPERE 4
  3302. #else
  3303. #define MMQ_X_Q8_0_AMPERE 128
  3304. #define MMQ_Y_Q8_0_AMPERE 64
  3305. #define NWARPS_Q8_0_AMPERE 4
  3306. #endif
  3307. #define MMQ_X_Q8_0_PASCAL 64
  3308. #define MMQ_Y_Q8_0_PASCAL 64
  3309. #define NWARPS_Q8_0_PASCAL 8
  3310. template <bool need_check> static __global__ void
  3311. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3312. #if defined(RDNA3) || defined(RDNA2)
  3313. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3314. #endif // defined(RDNA3) || defined(RDNA2)
  3315. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3316. mul_mat_q8_0(
  3317. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3318. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3319. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3320. #if defined(RDNA3) || defined(RDNA2)
  3321. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3322. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3323. const int nwarps = NWARPS_Q8_0_RDNA2;
  3324. #else
  3325. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3326. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3327. const int nwarps = NWARPS_Q8_0_RDNA1;
  3328. #endif // defined(RDNA3) || defined(RDNA2)
  3329. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3330. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3331. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3332. #elif __CUDA_ARCH__ >= CC_VOLTA
  3333. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3334. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3335. const int nwarps = NWARPS_Q8_0_AMPERE;
  3336. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3337. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3338. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3339. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3340. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3341. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3342. const int nwarps = NWARPS_Q8_0_PASCAL;
  3343. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3344. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3345. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3346. #else
  3347. (void) vec_dot_q8_0_q8_1_mul_mat;
  3348. bad_arch();
  3349. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3350. }
  3351. #define MMQ_X_Q2_K_RDNA2 64
  3352. #define MMQ_Y_Q2_K_RDNA2 128
  3353. #define NWARPS_Q2_K_RDNA2 8
  3354. #define MMQ_X_Q2_K_RDNA1 128
  3355. #define MMQ_Y_Q2_K_RDNA1 32
  3356. #define NWARPS_Q2_K_RDNA1 8
  3357. #if defined(CUDA_USE_TENSOR_CORES)
  3358. #define MMQ_X_Q2_K_AMPERE 4
  3359. #define MMQ_Y_Q2_K_AMPERE 32
  3360. #define NWARPS_Q2_K_AMPERE 4
  3361. #else
  3362. #define MMQ_X_Q2_K_AMPERE 64
  3363. #define MMQ_Y_Q2_K_AMPERE 128
  3364. #define NWARPS_Q2_K_AMPERE 4
  3365. #endif
  3366. #define MMQ_X_Q2_K_PASCAL 64
  3367. #define MMQ_Y_Q2_K_PASCAL 64
  3368. #define NWARPS_Q2_K_PASCAL 8
  3369. template <bool need_check> static __global__ void
  3370. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3371. #if defined(RDNA3) || defined(RDNA2)
  3372. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3373. #endif // defined(RDNA3) || defined(RDNA2)
  3374. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3375. mul_mat_q2_K(
  3376. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3377. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3378. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3379. #if defined(RDNA3) || defined(RDNA2)
  3380. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3381. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3382. const int nwarps = NWARPS_Q2_K_RDNA2;
  3383. #else
  3384. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3385. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3386. const int nwarps = NWARPS_Q2_K_RDNA1;
  3387. #endif // defined(RDNA3) || defined(RDNA2)
  3388. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3389. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3390. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3391. #elif __CUDA_ARCH__ >= CC_VOLTA
  3392. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3393. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3394. const int nwarps = NWARPS_Q2_K_AMPERE;
  3395. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3396. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3397. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3398. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3399. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3400. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3401. const int nwarps = NWARPS_Q2_K_PASCAL;
  3402. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3403. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3404. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3405. #else
  3406. (void) vec_dot_q2_K_q8_1_mul_mat;
  3407. bad_arch();
  3408. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3409. }
  3410. #define MMQ_X_Q3_K_RDNA2 128
  3411. #define MMQ_Y_Q3_K_RDNA2 64
  3412. #define NWARPS_Q3_K_RDNA2 8
  3413. #define MMQ_X_Q3_K_RDNA1 32
  3414. #define MMQ_Y_Q3_K_RDNA1 128
  3415. #define NWARPS_Q3_K_RDNA1 8
  3416. #if defined(CUDA_USE_TENSOR_CORES)
  3417. #define MMQ_X_Q3_K_AMPERE 4
  3418. #define MMQ_Y_Q3_K_AMPERE 32
  3419. #define NWARPS_Q3_K_AMPERE 4
  3420. #else
  3421. #define MMQ_X_Q3_K_AMPERE 128
  3422. #define MMQ_Y_Q3_K_AMPERE 128
  3423. #define NWARPS_Q3_K_AMPERE 4
  3424. #endif
  3425. #define MMQ_X_Q3_K_PASCAL 64
  3426. #define MMQ_Y_Q3_K_PASCAL 64
  3427. #define NWARPS_Q3_K_PASCAL 8
  3428. template <bool need_check> static __global__ void
  3429. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3430. #if defined(RDNA3) || defined(RDNA2)
  3431. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3432. #endif // defined(RDNA3) || defined(RDNA2)
  3433. #elif __CUDA_ARCH__ < CC_VOLTA
  3434. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3435. #endif // __CUDA_ARCH__ < CC_VOLTA
  3436. mul_mat_q3_K(
  3437. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3438. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3439. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3440. #if defined(RDNA3) || defined(RDNA2)
  3441. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3442. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3443. const int nwarps = NWARPS_Q3_K_RDNA2;
  3444. #else
  3445. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3446. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3447. const int nwarps = NWARPS_Q3_K_RDNA1;
  3448. #endif // defined(RDNA3) || defined(RDNA2)
  3449. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3450. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3451. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3452. #elif __CUDA_ARCH__ >= CC_VOLTA
  3453. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3454. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3455. const int nwarps = NWARPS_Q3_K_AMPERE;
  3456. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3457. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3458. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3459. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3460. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3461. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3462. const int nwarps = NWARPS_Q3_K_PASCAL;
  3463. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3464. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3465. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3466. #else
  3467. (void) vec_dot_q3_K_q8_1_mul_mat;
  3468. bad_arch();
  3469. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3470. }
  3471. #define MMQ_X_Q4_K_RDNA2 64
  3472. #define MMQ_Y_Q4_K_RDNA2 128
  3473. #define NWARPS_Q4_K_RDNA2 8
  3474. #define MMQ_X_Q4_K_RDNA1 32
  3475. #define MMQ_Y_Q4_K_RDNA1 64
  3476. #define NWARPS_Q4_K_RDNA1 8
  3477. #if defined(CUDA_USE_TENSOR_CORES)
  3478. #define MMQ_X_Q4_K_AMPERE 4
  3479. #define MMQ_Y_Q4_K_AMPERE 32
  3480. #define NWARPS_Q4_K_AMPERE 4
  3481. #else
  3482. #define MMQ_X_Q4_K_AMPERE 64
  3483. #define MMQ_Y_Q4_K_AMPERE 128
  3484. #define NWARPS_Q4_K_AMPERE 4
  3485. #endif
  3486. #define MMQ_X_Q4_K_PASCAL 64
  3487. #define MMQ_Y_Q4_K_PASCAL 64
  3488. #define NWARPS_Q4_K_PASCAL 8
  3489. template <bool need_check> static __global__ void
  3490. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3491. #if defined(RDNA3) || defined(RDNA2)
  3492. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3493. #endif // defined(RDNA3) || defined(RDNA2)
  3494. #elif __CUDA_ARCH__ < CC_VOLTA
  3495. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3496. #endif // __CUDA_ARCH__ < CC_VOLTA
  3497. mul_mat_q4_K(
  3498. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3499. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3500. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3501. #if defined(RDNA3) || defined(RDNA2)
  3502. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3503. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3504. const int nwarps = NWARPS_Q4_K_RDNA2;
  3505. #else
  3506. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3507. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3508. const int nwarps = NWARPS_Q4_K_RDNA1;
  3509. #endif // defined(RDNA3) || defined(RDNA2)
  3510. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3511. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3512. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3513. #elif __CUDA_ARCH__ >= CC_VOLTA
  3514. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3515. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3516. const int nwarps = NWARPS_Q4_K_AMPERE;
  3517. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3518. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3519. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3520. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3521. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3522. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3523. const int nwarps = NWARPS_Q4_K_PASCAL;
  3524. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3525. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3526. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3527. #else
  3528. (void) vec_dot_q4_K_q8_1_mul_mat;
  3529. bad_arch();
  3530. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3531. }
  3532. #define MMQ_X_Q5_K_RDNA2 64
  3533. #define MMQ_Y_Q5_K_RDNA2 128
  3534. #define NWARPS_Q5_K_RDNA2 8
  3535. #define MMQ_X_Q5_K_RDNA1 32
  3536. #define MMQ_Y_Q5_K_RDNA1 64
  3537. #define NWARPS_Q5_K_RDNA1 8
  3538. #if defined(CUDA_USE_TENSOR_CORES)
  3539. #define MMQ_X_Q5_K_AMPERE 4
  3540. #define MMQ_Y_Q5_K_AMPERE 32
  3541. #define NWARPS_Q5_K_AMPERE 4
  3542. #else
  3543. #define MMQ_X_Q5_K_AMPERE 64
  3544. #define MMQ_Y_Q5_K_AMPERE 128
  3545. #define NWARPS_Q5_K_AMPERE 4
  3546. #endif
  3547. #define MMQ_X_Q5_K_PASCAL 64
  3548. #define MMQ_Y_Q5_K_PASCAL 64
  3549. #define NWARPS_Q5_K_PASCAL 8
  3550. template <bool need_check> static __global__ void
  3551. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3552. #if defined(RDNA3) || defined(RDNA2)
  3553. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3554. #endif // defined(RDNA3) || defined(RDNA2)
  3555. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3556. mul_mat_q5_K(
  3557. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3558. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3559. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3560. #if defined(RDNA3) || defined(RDNA2)
  3561. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3562. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3563. const int nwarps = NWARPS_Q5_K_RDNA2;
  3564. #else
  3565. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3566. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3567. const int nwarps = NWARPS_Q5_K_RDNA1;
  3568. #endif // defined(RDNA3) || defined(RDNA2)
  3569. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3570. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3571. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3572. #elif __CUDA_ARCH__ >= CC_VOLTA
  3573. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3574. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3575. const int nwarps = NWARPS_Q5_K_AMPERE;
  3576. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3577. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3578. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3579. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3580. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3581. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3582. const int nwarps = NWARPS_Q5_K_PASCAL;
  3583. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3584. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3585. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3586. #else
  3587. (void) vec_dot_q5_K_q8_1_mul_mat;
  3588. bad_arch();
  3589. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3590. }
  3591. #define MMQ_X_Q6_K_RDNA2 64
  3592. #define MMQ_Y_Q6_K_RDNA2 128
  3593. #define NWARPS_Q6_K_RDNA2 8
  3594. #define MMQ_X_Q6_K_RDNA1 32
  3595. #define MMQ_Y_Q6_K_RDNA1 64
  3596. #define NWARPS_Q6_K_RDNA1 8
  3597. #if defined(CUDA_USE_TENSOR_CORES)
  3598. #define MMQ_X_Q6_K_AMPERE 4
  3599. #define MMQ_Y_Q6_K_AMPERE 32
  3600. #define NWARPS_Q6_K_AMPERE 4
  3601. #else
  3602. #define MMQ_X_Q6_K_AMPERE 64
  3603. #define MMQ_Y_Q6_K_AMPERE 64
  3604. #define NWARPS_Q6_K_AMPERE 4
  3605. #endif
  3606. #define MMQ_X_Q6_K_PASCAL 64
  3607. #define MMQ_Y_Q6_K_PASCAL 64
  3608. #define NWARPS_Q6_K_PASCAL 8
  3609. template <bool need_check> static __global__ void
  3610. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3611. #if defined(RDNA3) || defined(RDNA2)
  3612. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3613. #endif // defined(RDNA3) || defined(RDNA2)
  3614. #elif __CUDA_ARCH__ < CC_VOLTA
  3615. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3616. #endif // __CUDA_ARCH__ < CC_VOLTA
  3617. mul_mat_q6_K(
  3618. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3619. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3620. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3621. #if defined(RDNA3) || defined(RDNA2)
  3622. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3623. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3624. const int nwarps = NWARPS_Q6_K_RDNA2;
  3625. #else
  3626. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3627. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3628. const int nwarps = NWARPS_Q6_K_RDNA1;
  3629. #endif // defined(RDNA3) || defined(RDNA2)
  3630. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3631. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3632. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3633. #elif __CUDA_ARCH__ >= CC_VOLTA
  3634. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3635. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3636. const int nwarps = NWARPS_Q6_K_AMPERE;
  3637. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3638. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3639. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3640. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3641. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3642. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3643. const int nwarps = NWARPS_Q6_K_PASCAL;
  3644. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3645. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3646. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3647. #else
  3648. (void) vec_dot_q6_K_q8_1_mul_mat;
  3649. bad_arch();
  3650. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3651. }
  3652. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3653. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3654. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3655. if (row >= nrows) {
  3656. return;
  3657. }
  3658. const int blocks_per_row = ncols / qk;
  3659. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3660. // partial sum for each thread
  3661. float tmp = 0.0f;
  3662. const block_q_t * x = (const block_q_t *) vx;
  3663. const block_q8_1 * y = (const block_q8_1 *) vy;
  3664. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3665. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3666. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3667. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3668. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3669. }
  3670. // sum up partial sums and write back result
  3671. #pragma unroll
  3672. for (int mask = 16; mask > 0; mask >>= 1) {
  3673. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3674. }
  3675. if (threadIdx.x == 0) {
  3676. dst[row] = tmp;
  3677. }
  3678. }
  3679. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3680. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3681. // qk = quantized weights per x block
  3682. // qr = number of quantized weights per data value in x block
  3683. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3684. if (row >= nrows) {
  3685. return;
  3686. }
  3687. const int tid = threadIdx.x;
  3688. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3689. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3690. const int y_offset = qr == 1 ? 1 : qk/2;
  3691. // partial sum for each thread
  3692. #ifdef GGML_CUDA_F16
  3693. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3694. #else
  3695. float tmp = 0.0f;
  3696. #endif // GGML_CUDA_F16
  3697. for (int i = 0; i < ncols; i += iter_stride) {
  3698. const int col = i + vals_per_iter*tid;
  3699. const int ib = (row*ncols + col)/qk; // x block index
  3700. const int iqs = (col%qk)/qr; // x quant index
  3701. const int iybs = col - col%qk; // y block start index
  3702. // processing >2 values per i iter is faster for fast GPUs
  3703. #pragma unroll
  3704. for (int j = 0; j < vals_per_iter; j += 2) {
  3705. // process 2 vals per j iter
  3706. // dequantize
  3707. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3708. dfloat2 v;
  3709. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3710. // matrix multiplication
  3711. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3712. #ifdef GGML_CUDA_F16
  3713. tmp += __hmul2(v, {
  3714. y[iybs + iqs + j/qr + 0],
  3715. y[iybs + iqs + j/qr + y_offset]
  3716. });
  3717. #else
  3718. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3719. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3720. #endif // GGML_CUDA_F16
  3721. }
  3722. }
  3723. // sum up partial sums and write back result
  3724. #pragma unroll
  3725. for (int mask = 16; mask > 0; mask >>= 1) {
  3726. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3727. }
  3728. if (tid == 0) {
  3729. #ifdef GGML_CUDA_F16
  3730. dst[row] = tmp.x + tmp.y;
  3731. #else
  3732. dst[row] = tmp;
  3733. #endif // GGML_CUDA_F16
  3734. }
  3735. }
  3736. static __global__ void mul_mat_p021_f16_f32(
  3737. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3738. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3739. const half * x = (const half *) vx;
  3740. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3741. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3742. const int channel_x = channel / (nchannels_y / nchannels_x);
  3743. const int nrows_y = ncols_x;
  3744. const int nrows_dst = nrows_x;
  3745. const int row_dst = row_x;
  3746. float tmp = 0.0f;
  3747. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3748. const int col_x = col_x0 + threadIdx.x;
  3749. if (col_x >= ncols_x) {
  3750. break;
  3751. }
  3752. // x is transposed and permuted
  3753. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3754. const float xi = __half2float(x[ix]);
  3755. const int row_y = col_x;
  3756. // y is not transposed but permuted
  3757. const int iy = channel*nrows_y + row_y;
  3758. tmp += xi * y[iy];
  3759. }
  3760. // dst is not transposed and not permuted
  3761. const int idst = channel*nrows_dst + row_dst;
  3762. // sum up partial sums and write back result
  3763. #pragma unroll
  3764. for (int mask = 16; mask > 0; mask >>= 1) {
  3765. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3766. }
  3767. if (threadIdx.x == 0) {
  3768. dst[idst] = tmp;
  3769. }
  3770. }
  3771. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3772. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3773. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3774. const half * x = (const half *) vx;
  3775. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3776. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3777. const int channel_x = channel / channel_x_divisor;
  3778. const int nrows_y = ncols_x;
  3779. const int nrows_dst = nrows_x;
  3780. const int row_dst = row_x;
  3781. const int idst = channel*nrows_dst + row_dst;
  3782. float tmp = 0.0f;
  3783. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3784. const int col_x = col_x0 + threadIdx.x;
  3785. if (col_x >= ncols_x) {
  3786. break;
  3787. }
  3788. const int row_y = col_x;
  3789. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3790. const int iy = channel*nrows_y + row_y;
  3791. const float xi = __half2float(x[ix]);
  3792. tmp += xi * y[iy];
  3793. }
  3794. // sum up partial sums and write back result
  3795. #pragma unroll
  3796. for (int mask = 16; mask > 0; mask >>= 1) {
  3797. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3798. }
  3799. if (threadIdx.x == 0) {
  3800. dst[idst] = tmp;
  3801. }
  3802. }
  3803. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3804. const float * xi = (const float *) cxi;
  3805. float * dsti = (float *) cdsti;
  3806. *dsti = *xi;
  3807. }
  3808. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3809. const float * xi = (const float *) cxi;
  3810. half * dsti = (half *) cdsti;
  3811. *dsti = __float2half(*xi);
  3812. }
  3813. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3814. const half * xi = (const half *) cxi;
  3815. half * dsti = (half *) cdsti;
  3816. *dsti = *xi;
  3817. }
  3818. template <cpy_kernel_t cpy_1>
  3819. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3820. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3821. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3822. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3823. if (i >= ne) {
  3824. return;
  3825. }
  3826. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3827. // then combine those indices with the corresponding byte offsets to get the total offsets
  3828. const int i02 = i / (ne00*ne01);
  3829. const int i01 = (i - i02*ne01*ne00) / ne00;
  3830. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3831. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3832. const int i12 = i / (ne10*ne11);
  3833. const int i11 = (i - i12*ne10*ne11) / ne10;
  3834. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3835. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3836. cpy_1(cx + x_offset, cdst + dst_offset);
  3837. }
  3838. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3839. const float * xi = (const float *) cxi;
  3840. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3841. float amax = 0.0f; // absolute max
  3842. for (int j = 0; j < QK8_0; j++) {
  3843. const float v = xi[j];
  3844. amax = fmaxf(amax, fabsf(v));
  3845. }
  3846. const float d = amax / ((1 << 7) - 1);
  3847. const float id = d ? 1.0f/d : 0.0f;
  3848. dsti->d = d;
  3849. for (int j = 0; j < QK8_0; ++j) {
  3850. const float x0 = xi[j]*id;
  3851. dsti->qs[j] = roundf(x0);
  3852. }
  3853. }
  3854. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3855. const float * xi = (const float *) cxi;
  3856. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3857. float amax = 0.0f;
  3858. float vmax = 0.0f;
  3859. for (int j = 0; j < QK4_0; ++j) {
  3860. const float v = xi[j];
  3861. if (amax < fabsf(v)) {
  3862. amax = fabsf(v);
  3863. vmax = v;
  3864. }
  3865. }
  3866. const float d = vmax / -8;
  3867. const float id = d ? 1.0f/d : 0.0f;
  3868. dsti->d = d;
  3869. for (int j = 0; j < QK4_0/2; ++j) {
  3870. const float x0 = xi[0 + j]*id;
  3871. const float x1 = xi[QK4_0/2 + j]*id;
  3872. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3873. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3874. dsti->qs[j] = xi0;
  3875. dsti->qs[j] |= xi1 << 4;
  3876. }
  3877. }
  3878. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3879. const float * xi = (const float *) cxi;
  3880. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3881. float vmin = FLT_MAX;
  3882. float vmax = -FLT_MAX;
  3883. for (int j = 0; j < QK4_1; ++j) {
  3884. const float v = xi[j];
  3885. if (v < vmin) vmin = v;
  3886. if (v > vmax) vmax = v;
  3887. }
  3888. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3889. const float id = d ? 1.0f/d : 0.0f;
  3890. dsti->dm.x = d;
  3891. dsti->dm.y = vmin;
  3892. for (int j = 0; j < QK4_1/2; ++j) {
  3893. const float x0 = (xi[0 + j] - vmin)*id;
  3894. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3895. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3896. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3897. dsti->qs[j] = xi0;
  3898. dsti->qs[j] |= xi1 << 4;
  3899. }
  3900. }
  3901. template <cpy_kernel_t cpy_blck, int qk>
  3902. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3903. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3904. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3905. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3906. if (i >= ne) {
  3907. return;
  3908. }
  3909. const int i02 = i / (ne00*ne01);
  3910. const int i01 = (i - i02*ne01*ne00) / ne00;
  3911. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3912. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3913. const int i12 = i / (ne10*ne11);
  3914. const int i11 = (i - i12*ne10*ne11) / ne10;
  3915. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3916. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3917. cpy_blck(cx + x_offset, cdst + dst_offset);
  3918. }
  3919. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3920. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3921. return 1.0f - min(1.0f, max(0.0f, y));
  3922. }
  3923. struct rope_corr_dims {
  3924. float v[4];
  3925. };
  3926. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3927. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3928. static __device__ void rope_yarn(
  3929. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3930. float * cos_theta, float * sin_theta
  3931. ) {
  3932. // Get n-d rotational scaling corrected for extrapolation
  3933. float theta_interp = freq_scale * theta_extrap;
  3934. float theta = theta_interp;
  3935. if (ext_factor != 0.0f) {
  3936. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3937. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3938. // Get n-d magnitude scaling corrected for interpolation
  3939. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3940. }
  3941. *cos_theta = cosf(theta) * mscale;
  3942. *sin_theta = sinf(theta) * mscale;
  3943. }
  3944. // rope == RoPE == rotary positional embedding
  3945. template<typename T, bool has_pos>
  3946. static __global__ void rope(
  3947. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3948. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3949. ) {
  3950. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3951. if (col >= ncols) {
  3952. return;
  3953. }
  3954. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3955. const int i = row*ncols + col;
  3956. const int i2 = row/p_delta_rows;
  3957. const int p = has_pos ? pos[i2] : 0;
  3958. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3959. float cos_theta, sin_theta;
  3960. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3961. const float x0 = x[i + 0];
  3962. const float x1 = x[i + 1];
  3963. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3964. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3965. }
  3966. template<typename T, bool has_pos>
  3967. static __global__ void rope_neox(
  3968. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3969. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3970. ) {
  3971. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3972. if (col >= ncols) {
  3973. return;
  3974. }
  3975. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3976. const int ib = col / n_dims;
  3977. const int ic = col % n_dims;
  3978. if (ib > 0) {
  3979. const int i = row*ncols + ib*n_dims + ic;
  3980. dst[i + 0] = x[i + 0];
  3981. dst[i + 1] = x[i + 1];
  3982. return;
  3983. }
  3984. const int i = row*ncols + ib*n_dims + ic/2;
  3985. const int i2 = row/p_delta_rows;
  3986. float cur_rot = inv_ndims * ic - ib;
  3987. const int p = has_pos ? pos[i2] : 0;
  3988. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3989. float cos_theta, sin_theta;
  3990. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3991. const float x0 = x[i + 0];
  3992. const float x1 = x[i + n_dims/2];
  3993. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3994. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3995. }
  3996. static __global__ void rope_glm_f32(
  3997. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3998. int n_ctx
  3999. ) {
  4000. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4001. const int half_n_dims = ncols/4;
  4002. if (col >= half_n_dims) {
  4003. return;
  4004. }
  4005. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4006. const int i = row*ncols + col;
  4007. const int i2 = row/p_delta_rows;
  4008. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4009. // FIXME: this is likely wrong
  4010. const int p = pos != nullptr ? pos[i2] : 0;
  4011. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4012. const float sin_theta = sinf(theta);
  4013. const float cos_theta = cosf(theta);
  4014. const float x0 = x[i + 0];
  4015. const float x1 = x[i + half_n_dims];
  4016. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4017. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4018. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4019. const float sin_block_theta = sinf(block_theta);
  4020. const float cos_block_theta = cosf(block_theta);
  4021. const float x2 = x[i + half_n_dims * 2];
  4022. const float x3 = x[i + half_n_dims * 3];
  4023. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4024. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4025. }
  4026. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4027. const int n_heads_log2_floor, const float m0, const float m1) {
  4028. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4029. if (col >= ncols) {
  4030. return;
  4031. }
  4032. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4033. const int i = row*ncols + col;
  4034. const int k = row/k_rows;
  4035. float m_k;
  4036. if (k < n_heads_log2_floor) {
  4037. m_k = powf(m0, k + 1);
  4038. } else {
  4039. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4040. }
  4041. dst[i] = col * m_k + x[i];
  4042. }
  4043. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4044. const int row = blockIdx.y;
  4045. const int col = threadIdx.x;
  4046. float sum = 0.0f;
  4047. for (int i = col; i < ncols; i += blockDim.x) {
  4048. sum += x[row * ncols + i];
  4049. }
  4050. sum = warp_reduce_sum(sum);
  4051. if (col == 0) {
  4052. dst[row] = sum;
  4053. }
  4054. }
  4055. template<typename T>
  4056. static inline __device__ void swap(T & a, T & b) {
  4057. T tmp = a;
  4058. a = b;
  4059. b = tmp;
  4060. }
  4061. template<ggml_sort_order order>
  4062. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4063. // bitonic sort
  4064. int col = threadIdx.x;
  4065. int row = blockIdx.y;
  4066. if (col >= ncols) return;
  4067. const float * x_row = x + row * ncols;
  4068. int * dst_row = dst + row * ncols;
  4069. // initialize indices
  4070. if (col < ncols) {
  4071. dst_row[col] = col;
  4072. }
  4073. __syncthreads();
  4074. for (int k = 2; k <= ncols; k *= 2) {
  4075. for (int j = k / 2; j > 0; j /= 2) {
  4076. int ixj = col ^ j;
  4077. if (ixj > col) {
  4078. if ((col & k) == 0) {
  4079. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  4080. swap(dst_row[col], dst_row[ixj]);
  4081. }
  4082. } else {
  4083. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  4084. swap(dst_row[col], dst_row[ixj]);
  4085. }
  4086. }
  4087. }
  4088. __syncthreads();
  4089. }
  4090. }
  4091. }
  4092. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4093. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4094. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4095. if (col >= ncols) {
  4096. return;
  4097. }
  4098. const int i = row*ncols + col;
  4099. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4100. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4101. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4102. }
  4103. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4104. const int tid = threadIdx.x;
  4105. const int rowx = blockIdx.x;
  4106. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4107. const int block_size = blockDim.x;
  4108. const int warp_id = threadIdx.x / WARP_SIZE;
  4109. const int lane_id = threadIdx.x % WARP_SIZE;
  4110. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4111. float max_val = -INFINITY;
  4112. for (int col = tid; col < ncols; col += block_size) {
  4113. const int ix = rowx*ncols + col;
  4114. const int iy = rowy*ncols + col;
  4115. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4116. }
  4117. // find the max value in the block
  4118. max_val = warp_reduce_max(max_val);
  4119. if (block_size > WARP_SIZE) {
  4120. if (warp_id == 0) {
  4121. buf[lane_id] = -INFINITY;
  4122. }
  4123. __syncthreads();
  4124. if (lane_id == 0) {
  4125. buf[warp_id] = max_val;
  4126. }
  4127. __syncthreads();
  4128. max_val = buf[lane_id];
  4129. max_val = warp_reduce_max(max_val);
  4130. }
  4131. float tmp = 0.f;
  4132. for (int col = tid; col < ncols; col += block_size) {
  4133. const int ix = rowx*ncols + col;
  4134. const int iy = rowy*ncols + col;
  4135. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4136. tmp += val;
  4137. dst[ix] = val;
  4138. }
  4139. // find the sum of exps in the block
  4140. tmp = warp_reduce_sum(tmp);
  4141. if (block_size > WARP_SIZE) {
  4142. if (warp_id == 0) {
  4143. buf[lane_id] = 0.f;
  4144. }
  4145. __syncthreads();
  4146. if (lane_id == 0) {
  4147. buf[warp_id] = tmp;
  4148. }
  4149. __syncthreads();
  4150. tmp = buf[lane_id];
  4151. tmp = warp_reduce_sum(tmp);
  4152. }
  4153. const float inv_tmp = 1.f / tmp;
  4154. for (int col = tid; col < ncols; col += block_size) {
  4155. const int i = rowx*ncols + col;
  4156. dst[i] *= inv_tmp;
  4157. }
  4158. }
  4159. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4160. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4161. if (i >= k) {
  4162. return;
  4163. }
  4164. dst[i] = scale * x[i];
  4165. }
  4166. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4167. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4168. if (i >= k) {
  4169. return;
  4170. }
  4171. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4172. }
  4173. static __global__ void im2col_f32_f16(
  4174. const float * x, half * dst,
  4175. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4176. int s0, int s1, int p0, int p1, int d0, int d1) {
  4177. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4178. if (i >= pelements) {
  4179. return;
  4180. }
  4181. const int ksize = OW * (KH > 1 ? KW : 1);
  4182. const int kx = i / ksize;
  4183. const int kd = kx * ksize;
  4184. const int ky = (i - kd) / OW;
  4185. const int ix = i % OW;
  4186. const int64_t iiw = ix * s0 + kx * d0 - p0;
  4187. const int64_t iih = blockIdx.y * s1 + ky * d1 - p1;
  4188. const int64_t offset_dst =
  4189. (blockIdx.y * OW + ix) * CHW +
  4190. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4191. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4192. dst[offset_dst] = __float2half(0.0f);
  4193. } else {
  4194. const int64_t offset_src = blockIdx.z * offset_delta;
  4195. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4196. }
  4197. }
  4198. template<int qk, int qr, dequantize_kernel_t dq>
  4199. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4200. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4201. GGML_TENSOR_BINARY_OP_LOCALS
  4202. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4203. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4204. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4205. // strides in elements
  4206. //const size_t s0 = nb0 / ggml_element_size(dst);
  4207. const size_t s1 = nb1 / ggml_element_size(dst);
  4208. const size_t s2 = nb2 / ggml_element_size(dst);
  4209. const size_t s3 = nb3 / ggml_element_size(dst);
  4210. const size_t s10 = nb10 / ggml_element_size(src1);
  4211. const size_t s11 = nb11 / ggml_element_size(src1);
  4212. const size_t s12 = nb12 / ggml_element_size(src1);
  4213. //const size_t s13 = nb13 / ggml_element_size(src1);
  4214. GGML_ASSERT(ne00 % 2 == 0);
  4215. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4216. src0_dd, src1_dd, dst_dd,
  4217. ne00, /*ne01, ne02, ne03,*/
  4218. /*ne10, ne11,*/ ne12, /*ne13,*/
  4219. /* s0,*/ s1, s2, s3,
  4220. /* nb00,*/ nb01, nb02, nb03,
  4221. s10, s11, s12/*, s13*/);
  4222. (void) dst;
  4223. }
  4224. template<typename src0_t>
  4225. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4226. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4227. GGML_TENSOR_BINARY_OP_LOCALS
  4228. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4229. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4230. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4231. // strides in elements
  4232. //const size_t s0 = nb0 / ggml_element_size(dst);
  4233. const size_t s1 = nb1 / ggml_element_size(dst);
  4234. const size_t s2 = nb2 / ggml_element_size(dst);
  4235. const size_t s3 = nb3 / ggml_element_size(dst);
  4236. const size_t s10 = nb10 / ggml_element_size(src1);
  4237. const size_t s11 = nb11 / ggml_element_size(src1);
  4238. const size_t s12 = nb12 / ggml_element_size(src1);
  4239. //const size_t s13 = nb13 / ggml_element_size(src1);
  4240. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4241. src0_dd, src1_dd, dst_dd,
  4242. ne00, /*ne01, ne02, ne03,*/
  4243. /*ne10, ne11,*/ ne12, /*ne13,*/
  4244. /* s0,*/ s1, s2, s3,
  4245. /* nb00,*/ nb01, nb02, nb03,
  4246. s10, s11, s12/*, s13*/);
  4247. (void) dst;
  4248. }
  4249. template<float (*bin_op)(const float, const float)>
  4250. struct bin_bcast_cuda {
  4251. template<typename src0_t, typename src1_t, typename dst_t>
  4252. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4253. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4254. cudaStream_t stream) {
  4255. GGML_TENSOR_BINARY_OP_LOCALS
  4256. int nr0 = ne10/ne0;
  4257. int nr1 = ne11/ne1;
  4258. int nr2 = ne12/ne2;
  4259. int nr3 = ne13/ne3;
  4260. int nr[4] = { nr0, nr1, nr2, nr3 };
  4261. // collapse dimensions until first broadcast dimension
  4262. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4263. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4264. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4265. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4266. auto collapse = [](int64_t cne[]) {
  4267. cne[0] *= cne[1];
  4268. cne[1] = cne[2];
  4269. cne[2] = cne[3];
  4270. cne[3] = 1;
  4271. };
  4272. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  4273. cnb[1] *= cne[1];
  4274. cnb[2] *= cne[2];
  4275. cnb[3] *= cne[3];
  4276. };
  4277. for (int i = 0; i < 4; i++) {
  4278. if (nr[i] != 1) {
  4279. break;
  4280. }
  4281. if (i > 0) {
  4282. collapse_nb(cnb0, cne0);
  4283. collapse_nb(cnb1, cne1);
  4284. collapse(cne0);
  4285. collapse(cne1);
  4286. }
  4287. }
  4288. {
  4289. int64_t ne0 = cne0[0];
  4290. int64_t ne1 = cne0[1];
  4291. int64_t ne2 = cne0[2];
  4292. int64_t ne3 = cne0[3];
  4293. int64_t ne10 = cne1[0];
  4294. int64_t ne11 = cne1[1];
  4295. int64_t ne12 = cne1[2];
  4296. int64_t ne13 = cne1[3];
  4297. size_t nb0 = cnb0[0];
  4298. size_t nb1 = cnb0[1];
  4299. size_t nb2 = cnb0[2];
  4300. size_t nb3 = cnb0[3];
  4301. size_t nb10 = cnb1[0];
  4302. size_t nb11 = cnb1[1];
  4303. size_t nb12 = cnb1[2];
  4304. size_t nb13 = cnb1[3];
  4305. size_t s0 = nb0 / sizeof(dst_t);
  4306. size_t s1 = nb1 / sizeof(dst_t);
  4307. size_t s2 = nb2 / sizeof(dst_t);
  4308. size_t s3 = nb3 / sizeof(dst_t);
  4309. size_t s10 = nb10 / sizeof(src1_t);
  4310. size_t s11 = nb11 / sizeof(src1_t);
  4311. size_t s12 = nb12 / sizeof(src1_t);
  4312. size_t s13 = nb13 / sizeof(src1_t);
  4313. GGML_ASSERT(s0 == 1);
  4314. GGML_ASSERT(s10 == 1);
  4315. const int block_size = 128;
  4316. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4317. dim3 block_dims;
  4318. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4319. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4320. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4321. dim3 block_nums(
  4322. (hne0 + block_dims.x - 1) / block_dims.x,
  4323. (ne1 + block_dims.y - 1) / block_dims.y,
  4324. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4325. );
  4326. if (block_nums.z > 65535) {
  4327. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4328. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4329. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4330. src0_dd, src1_dd, dst_dd,
  4331. ne0, ne1, ne2, ne3,
  4332. ne10, ne11, ne12, ne13,
  4333. /* s0, */ s1, s2, s3,
  4334. /* s10, */ s11, s12, s13);
  4335. } else {
  4336. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4337. src0_dd, src1_dd, dst_dd,
  4338. ne0, ne1, ne2, ne3,
  4339. ne10, ne11, ne12, ne13,
  4340. /* s0, */ s1, s2, s3,
  4341. /* s10, */ s11, s12, s13);
  4342. }
  4343. }
  4344. }
  4345. };
  4346. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4347. const int ne10, const int ne11, const int ne12,
  4348. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4349. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4350. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4351. }
  4352. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4353. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4354. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4355. }
  4356. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4357. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4358. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4359. }
  4360. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4361. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4362. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4363. }
  4364. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4365. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4366. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4367. }
  4368. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4369. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4370. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4371. }
  4372. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4373. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4374. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4375. }
  4376. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4377. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4378. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4379. }
  4380. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4381. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4382. if (ncols < 1024) {
  4383. const dim3 block_dims(WARP_SIZE, 1, 1);
  4384. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4385. } else {
  4386. const dim3 block_dims(1024, 1, 1);
  4387. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4388. }
  4389. }
  4390. static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
  4391. static const float eps = 1e-6f;
  4392. if (group_size < 1024) {
  4393. const dim3 block_dims(WARP_SIZE, 1, 1);
  4394. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4395. } else {
  4396. const dim3 block_dims(1024, 1, 1);
  4397. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4398. }
  4399. }
  4400. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  4401. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4402. dim3 gridDim(num_blocks, ne1, ne2);
  4403. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4404. }
  4405. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  4406. int ne0 = (ne00 * scale_factor);
  4407. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4408. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4409. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4410. }
  4411. static void pad_f32_cuda(const float * x, float * dst,
  4412. const int ne00, const int ne01, const int ne02,
  4413. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4414. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4415. dim3 gridDim(num_blocks, ne1, ne2);
  4416. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4417. }
  4418. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4419. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4420. if (ncols < 1024) {
  4421. const dim3 block_dims(WARP_SIZE, 1, 1);
  4422. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4423. } else {
  4424. const dim3 block_dims(1024, 1, 1);
  4425. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4426. }
  4427. }
  4428. static void quantize_row_q8_1_cuda(const float * x, void * vy, const int kx, const int ky, const int kx_padded, cudaStream_t stream) {
  4429. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4430. const dim3 num_blocks(block_num_x, ky, 1);
  4431. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4432. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4433. }
  4434. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4435. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4436. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4437. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4438. }
  4439. template<typename dst_t>
  4440. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4441. const int nb = k / QK_K;
  4442. #if QK_K == 256
  4443. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4444. #else
  4445. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4446. #endif
  4447. }
  4448. template<typename dst_t>
  4449. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4450. const int nb = k / QK_K;
  4451. #if QK_K == 256
  4452. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4453. #else
  4454. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4455. #endif
  4456. }
  4457. template<typename dst_t>
  4458. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4459. const int nb = k / QK_K;
  4460. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4461. }
  4462. template<typename dst_t>
  4463. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4464. const int nb = k / QK_K;
  4465. #if QK_K == 256
  4466. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4467. #else
  4468. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4469. #endif
  4470. }
  4471. template<typename dst_t>
  4472. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4473. const int nb = k / QK_K;
  4474. #if QK_K == 256
  4475. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4476. #else
  4477. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4478. #endif
  4479. }
  4480. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4481. switch (type) {
  4482. case GGML_TYPE_Q4_0:
  4483. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4484. case GGML_TYPE_Q4_1:
  4485. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4486. case GGML_TYPE_Q5_0:
  4487. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4488. case GGML_TYPE_Q5_1:
  4489. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4490. case GGML_TYPE_Q8_0:
  4491. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4492. case GGML_TYPE_Q2_K:
  4493. return dequantize_row_q2_K_cuda;
  4494. case GGML_TYPE_Q3_K:
  4495. return dequantize_row_q3_K_cuda;
  4496. case GGML_TYPE_Q4_K:
  4497. return dequantize_row_q4_K_cuda;
  4498. case GGML_TYPE_Q5_K:
  4499. return dequantize_row_q5_K_cuda;
  4500. case GGML_TYPE_Q6_K:
  4501. return dequantize_row_q6_K_cuda;
  4502. case GGML_TYPE_F32:
  4503. return dequantize_block_cuda<1, 1, convert_f32>;
  4504. default:
  4505. return nullptr;
  4506. }
  4507. }
  4508. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4509. switch (type) {
  4510. case GGML_TYPE_Q4_0:
  4511. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4512. case GGML_TYPE_Q4_1:
  4513. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4514. case GGML_TYPE_Q5_0:
  4515. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4516. case GGML_TYPE_Q5_1:
  4517. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4518. case GGML_TYPE_Q8_0:
  4519. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4520. case GGML_TYPE_Q2_K:
  4521. return dequantize_row_q2_K_cuda;
  4522. case GGML_TYPE_Q3_K:
  4523. return dequantize_row_q3_K_cuda;
  4524. case GGML_TYPE_Q4_K:
  4525. return dequantize_row_q4_K_cuda;
  4526. case GGML_TYPE_Q5_K:
  4527. return dequantize_row_q5_K_cuda;
  4528. case GGML_TYPE_Q6_K:
  4529. return dequantize_row_q6_K_cuda;
  4530. case GGML_TYPE_F16:
  4531. return dequantize_block_cuda<1, 1, convert_f16>;
  4532. default:
  4533. return nullptr;
  4534. }
  4535. }
  4536. static void dequantize_mul_mat_vec_q4_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4537. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4538. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4539. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4540. const dim3 block_nums(block_num_y, 1, 1);
  4541. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4542. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4543. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4544. }
  4545. static void dequantize_mul_mat_vec_q4_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4546. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4547. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4548. const dim3 block_nums(block_num_y, 1, 1);
  4549. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4550. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4551. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4552. }
  4553. static void dequantize_mul_mat_vec_q5_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4554. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4555. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4556. const dim3 block_nums(block_num_y, 1, 1);
  4557. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4558. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4559. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4560. }
  4561. static void dequantize_mul_mat_vec_q5_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4562. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4563. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4564. const dim3 block_nums(block_num_y, 1, 1);
  4565. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4566. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4567. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4568. }
  4569. static void dequantize_mul_mat_vec_q8_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4570. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4571. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4572. const dim3 block_nums(block_num_y, 1, 1);
  4573. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4574. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4575. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4576. }
  4577. static void dequantize_mul_mat_vec_q2_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4578. GGML_ASSERT(ncols % QK_K == 0);
  4579. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4580. const int block_num_y = (nrows + ny - 1) / ny;
  4581. const dim3 block_nums(block_num_y, 1, 1);
  4582. const dim3 block_dims(32, ny, 1);
  4583. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4584. }
  4585. static void dequantize_mul_mat_vec_q3_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4586. GGML_ASSERT(ncols % QK_K == 0);
  4587. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4588. const int block_num_y = (nrows + ny - 1) / ny;
  4589. const dim3 block_nums(block_num_y, 1, 1);
  4590. const dim3 block_dims(32, ny, 1);
  4591. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4592. }
  4593. static void dequantize_mul_mat_vec_q4_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4594. GGML_ASSERT(ncols % QK_K == 0);
  4595. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4596. const int block_num_y = (nrows + ny - 1) / ny;
  4597. const dim3 block_nums(block_num_y, 1, 1);
  4598. const dim3 block_dims(32, ny, 1);
  4599. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4600. }
  4601. static void dequantize_mul_mat_vec_q5_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4602. GGML_ASSERT(ncols % QK_K == 0);
  4603. const dim3 block_dims(32, 1, 1);
  4604. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4605. }
  4606. static void dequantize_mul_mat_vec_q6_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4607. GGML_ASSERT(ncols % QK_K == 0);
  4608. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4609. const int block_num_y = (nrows + ny - 1) / ny;
  4610. const dim3 block_nums(block_num_y, 1, 1);
  4611. const dim3 block_dims(32, ny, 1);
  4612. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4613. }
  4614. static void convert_mul_mat_vec_f16_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4615. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4616. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4617. const dim3 block_nums(block_num_y, 1, 1);
  4618. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4619. dequantize_mul_mat_vec<1, 1, convert_f16>
  4620. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4621. }
  4622. static void mul_mat_vec_q4_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4623. GGML_ASSERT(ncols % QK4_0 == 0);
  4624. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4625. const dim3 block_nums(block_num_y, 1, 1);
  4626. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4627. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4628. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4629. }
  4630. static void mul_mat_vec_q4_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4631. GGML_ASSERT(ncols % QK4_1 == 0);
  4632. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4633. const dim3 block_nums(block_num_y, 1, 1);
  4634. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4635. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4636. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4637. }
  4638. static void mul_mat_vec_q5_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4639. GGML_ASSERT(ncols % QK5_0 == 0);
  4640. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4641. const dim3 block_nums(block_num_y, 1, 1);
  4642. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4643. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4644. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4645. }
  4646. static void mul_mat_vec_q5_1_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4647. GGML_ASSERT(ncols % QK5_1 == 0);
  4648. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4649. const dim3 block_nums(block_num_y, 1, 1);
  4650. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4651. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4652. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4653. }
  4654. static void mul_mat_vec_q8_0_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4655. GGML_ASSERT(ncols % QK8_0 == 0);
  4656. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4657. const dim3 block_nums(block_num_y, 1, 1);
  4658. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4659. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4660. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4661. }
  4662. static void mul_mat_vec_q2_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4663. GGML_ASSERT(ncols % QK_K == 0);
  4664. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4665. const dim3 block_nums(block_num_y, 1, 1);
  4666. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4667. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4668. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4669. }
  4670. static void mul_mat_vec_q3_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4671. GGML_ASSERT(ncols % QK_K == 0);
  4672. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4673. const dim3 block_nums(block_num_y, 1, 1);
  4674. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4675. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4676. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4677. }
  4678. static void mul_mat_vec_q4_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4679. GGML_ASSERT(ncols % QK_K == 0);
  4680. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4681. const dim3 block_nums(block_num_y, 1, 1);
  4682. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4683. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4684. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4685. }
  4686. static void mul_mat_vec_q5_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4687. GGML_ASSERT(ncols % QK_K == 0);
  4688. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4689. const dim3 block_nums(block_num_y, 1, 1);
  4690. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4691. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4692. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4693. }
  4694. static void mul_mat_vec_q6_K_q8_1_cuda(const void * vx, const void * vy, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  4695. GGML_ASSERT(ncols % QK_K == 0);
  4696. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4697. const dim3 block_nums(block_num_y, 1, 1);
  4698. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4699. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4700. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4701. }
  4702. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4703. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4704. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4705. int id;
  4706. CUDA_CHECK(cudaGetDevice(&id));
  4707. const int compute_capability = g_device_caps[id].cc;
  4708. int mmq_x, mmq_y, nwarps;
  4709. if (compute_capability >= CC_RDNA2) {
  4710. mmq_x = MMQ_X_Q4_0_RDNA2;
  4711. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4712. nwarps = NWARPS_Q4_0_RDNA2;
  4713. } else if (compute_capability >= CC_OFFSET_AMD) {
  4714. mmq_x = MMQ_X_Q4_0_RDNA1;
  4715. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4716. nwarps = NWARPS_Q4_0_RDNA1;
  4717. } else if (compute_capability >= CC_VOLTA) {
  4718. mmq_x = MMQ_X_Q4_0_AMPERE;
  4719. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4720. nwarps = NWARPS_Q4_0_AMPERE;
  4721. } else if (compute_capability >= MIN_CC_DP4A) {
  4722. mmq_x = MMQ_X_Q4_0_PASCAL;
  4723. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4724. nwarps = NWARPS_Q4_0_PASCAL;
  4725. } else {
  4726. GGML_ASSERT(false);
  4727. }
  4728. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4729. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4730. const dim3 block_nums(block_num_x, block_num_y, 1);
  4731. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4732. if (nrows_x % mmq_y == 0) {
  4733. const bool need_check = false;
  4734. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4735. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4736. } else {
  4737. const bool need_check = true;
  4738. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4739. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4740. }
  4741. }
  4742. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4743. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4744. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4745. int id;
  4746. CUDA_CHECK(cudaGetDevice(&id));
  4747. const int compute_capability = g_device_caps[id].cc;
  4748. int mmq_x, mmq_y, nwarps;
  4749. if (compute_capability >= CC_RDNA2) {
  4750. mmq_x = MMQ_X_Q4_1_RDNA2;
  4751. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4752. nwarps = NWARPS_Q4_1_RDNA2;
  4753. } else if (compute_capability >= CC_OFFSET_AMD) {
  4754. mmq_x = MMQ_X_Q4_1_RDNA1;
  4755. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4756. nwarps = NWARPS_Q4_1_RDNA1;
  4757. } else if (compute_capability >= CC_VOLTA) {
  4758. mmq_x = MMQ_X_Q4_1_AMPERE;
  4759. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4760. nwarps = NWARPS_Q4_1_AMPERE;
  4761. } else if (compute_capability >= MIN_CC_DP4A) {
  4762. mmq_x = MMQ_X_Q4_1_PASCAL;
  4763. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4764. nwarps = NWARPS_Q4_1_PASCAL;
  4765. } else {
  4766. GGML_ASSERT(false);
  4767. }
  4768. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4769. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4770. const dim3 block_nums(block_num_x, block_num_y, 1);
  4771. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4772. if (nrows_x % mmq_y == 0) {
  4773. const bool need_check = false;
  4774. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4775. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4776. } else {
  4777. const bool need_check = true;
  4778. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4779. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4780. }
  4781. }
  4782. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4783. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4784. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4785. int id;
  4786. CUDA_CHECK(cudaGetDevice(&id));
  4787. const int compute_capability = g_device_caps[id].cc;
  4788. int mmq_x, mmq_y, nwarps;
  4789. if (compute_capability >= CC_RDNA2) {
  4790. mmq_x = MMQ_X_Q5_0_RDNA2;
  4791. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4792. nwarps = NWARPS_Q5_0_RDNA2;
  4793. } else if (compute_capability >= CC_OFFSET_AMD) {
  4794. mmq_x = MMQ_X_Q5_0_RDNA1;
  4795. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4796. nwarps = NWARPS_Q5_0_RDNA1;
  4797. } else if (compute_capability >= CC_VOLTA) {
  4798. mmq_x = MMQ_X_Q5_0_AMPERE;
  4799. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4800. nwarps = NWARPS_Q5_0_AMPERE;
  4801. } else if (compute_capability >= MIN_CC_DP4A) {
  4802. mmq_x = MMQ_X_Q5_0_PASCAL;
  4803. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4804. nwarps = NWARPS_Q5_0_PASCAL;
  4805. } else {
  4806. GGML_ASSERT(false);
  4807. }
  4808. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4809. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4810. const dim3 block_nums(block_num_x, block_num_y, 1);
  4811. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4812. if (nrows_x % mmq_y == 0) {
  4813. const bool need_check = false;
  4814. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4815. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4816. } else {
  4817. const bool need_check = true;
  4818. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4819. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4820. }
  4821. }
  4822. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4823. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4824. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4825. int id;
  4826. CUDA_CHECK(cudaGetDevice(&id));
  4827. const int compute_capability = g_device_caps[id].cc;
  4828. int mmq_x, mmq_y, nwarps;
  4829. if (compute_capability >= CC_RDNA2) {
  4830. mmq_x = MMQ_X_Q5_1_RDNA2;
  4831. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4832. nwarps = NWARPS_Q5_1_RDNA2;
  4833. } else if (compute_capability >= CC_OFFSET_AMD) {
  4834. mmq_x = MMQ_X_Q5_1_RDNA1;
  4835. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4836. nwarps = NWARPS_Q5_1_RDNA1;
  4837. } else if (compute_capability >= CC_VOLTA) {
  4838. mmq_x = MMQ_X_Q5_1_AMPERE;
  4839. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4840. nwarps = NWARPS_Q5_1_AMPERE;
  4841. } else if (compute_capability >= MIN_CC_DP4A) {
  4842. mmq_x = MMQ_X_Q5_1_PASCAL;
  4843. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4844. nwarps = NWARPS_Q5_1_PASCAL;
  4845. } else {
  4846. GGML_ASSERT(false);
  4847. }
  4848. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4849. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4850. const dim3 block_nums(block_num_x, block_num_y, 1);
  4851. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4852. if (nrows_x % mmq_y == 0) {
  4853. const bool need_check = false;
  4854. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4855. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4856. } else {
  4857. const bool need_check = true;
  4858. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4859. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4860. }
  4861. }
  4862. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4863. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4864. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4865. int id;
  4866. CUDA_CHECK(cudaGetDevice(&id));
  4867. const int compute_capability = g_device_caps[id].cc;
  4868. int mmq_x, mmq_y, nwarps;
  4869. if (compute_capability >= CC_RDNA2) {
  4870. mmq_x = MMQ_X_Q8_0_RDNA2;
  4871. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4872. nwarps = NWARPS_Q8_0_RDNA2;
  4873. } else if (compute_capability >= CC_OFFSET_AMD) {
  4874. mmq_x = MMQ_X_Q8_0_RDNA1;
  4875. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4876. nwarps = NWARPS_Q8_0_RDNA1;
  4877. } else if (compute_capability >= CC_VOLTA) {
  4878. mmq_x = MMQ_X_Q8_0_AMPERE;
  4879. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4880. nwarps = NWARPS_Q8_0_AMPERE;
  4881. } else if (compute_capability >= MIN_CC_DP4A) {
  4882. mmq_x = MMQ_X_Q8_0_PASCAL;
  4883. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4884. nwarps = NWARPS_Q8_0_PASCAL;
  4885. } else {
  4886. GGML_ASSERT(false);
  4887. }
  4888. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4889. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4890. const dim3 block_nums(block_num_x, block_num_y, 1);
  4891. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4892. if (nrows_x % mmq_y == 0) {
  4893. const bool need_check = false;
  4894. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4895. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4896. } else {
  4897. const bool need_check = true;
  4898. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4899. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4900. }
  4901. }
  4902. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4903. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4904. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4905. int id;
  4906. CUDA_CHECK(cudaGetDevice(&id));
  4907. const int compute_capability = g_device_caps[id].cc;
  4908. int mmq_x, mmq_y, nwarps;
  4909. if (compute_capability >= CC_RDNA2) {
  4910. mmq_x = MMQ_X_Q2_K_RDNA2;
  4911. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4912. nwarps = NWARPS_Q2_K_RDNA2;
  4913. } else if (compute_capability >= CC_OFFSET_AMD) {
  4914. mmq_x = MMQ_X_Q2_K_RDNA1;
  4915. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4916. nwarps = NWARPS_Q2_K_RDNA1;
  4917. } else if (compute_capability >= CC_VOLTA) {
  4918. mmq_x = MMQ_X_Q2_K_AMPERE;
  4919. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4920. nwarps = NWARPS_Q2_K_AMPERE;
  4921. } else if (compute_capability >= MIN_CC_DP4A) {
  4922. mmq_x = MMQ_X_Q2_K_PASCAL;
  4923. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4924. nwarps = NWARPS_Q2_K_PASCAL;
  4925. } else {
  4926. GGML_ASSERT(false);
  4927. }
  4928. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4929. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4930. const dim3 block_nums(block_num_x, block_num_y, 1);
  4931. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4932. if (nrows_x % mmq_y == 0) {
  4933. const bool need_check = false;
  4934. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4935. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4936. } else {
  4937. const bool need_check = true;
  4938. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4939. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4940. }
  4941. }
  4942. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4943. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4944. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4945. #if QK_K == 256
  4946. int id;
  4947. CUDA_CHECK(cudaGetDevice(&id));
  4948. const int compute_capability = g_device_caps[id].cc;
  4949. int mmq_x, mmq_y, nwarps;
  4950. if (compute_capability >= CC_RDNA2) {
  4951. mmq_x = MMQ_X_Q3_K_RDNA2;
  4952. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4953. nwarps = NWARPS_Q3_K_RDNA2;
  4954. } else if (compute_capability >= CC_OFFSET_AMD) {
  4955. mmq_x = MMQ_X_Q3_K_RDNA1;
  4956. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4957. nwarps = NWARPS_Q3_K_RDNA1;
  4958. } else if (compute_capability >= CC_VOLTA) {
  4959. mmq_x = MMQ_X_Q3_K_AMPERE;
  4960. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4961. nwarps = NWARPS_Q3_K_AMPERE;
  4962. } else if (compute_capability >= MIN_CC_DP4A) {
  4963. mmq_x = MMQ_X_Q3_K_PASCAL;
  4964. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4965. nwarps = NWARPS_Q3_K_PASCAL;
  4966. } else {
  4967. GGML_ASSERT(false);
  4968. }
  4969. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4970. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4971. const dim3 block_nums(block_num_x, block_num_y, 1);
  4972. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4973. if (nrows_x % mmq_y == 0) {
  4974. const bool need_check = false;
  4975. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4976. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4977. } else {
  4978. const bool need_check = true;
  4979. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4980. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4981. }
  4982. #endif
  4983. }
  4984. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4985. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4986. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4987. int id;
  4988. CUDA_CHECK(cudaGetDevice(&id));
  4989. const int compute_capability = g_device_caps[id].cc;
  4990. int mmq_x, mmq_y, nwarps;
  4991. if (compute_capability >= CC_RDNA2) {
  4992. mmq_x = MMQ_X_Q4_K_RDNA2;
  4993. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4994. nwarps = NWARPS_Q4_K_RDNA2;
  4995. } else if (compute_capability >= CC_OFFSET_AMD) {
  4996. mmq_x = MMQ_X_Q4_K_RDNA1;
  4997. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4998. nwarps = NWARPS_Q4_K_RDNA1;
  4999. } else if (compute_capability >= CC_VOLTA) {
  5000. mmq_x = MMQ_X_Q4_K_AMPERE;
  5001. mmq_y = MMQ_Y_Q4_K_AMPERE;
  5002. nwarps = NWARPS_Q4_K_AMPERE;
  5003. } else if (compute_capability >= MIN_CC_DP4A) {
  5004. mmq_x = MMQ_X_Q4_K_PASCAL;
  5005. mmq_y = MMQ_Y_Q4_K_PASCAL;
  5006. nwarps = NWARPS_Q4_K_PASCAL;
  5007. } else {
  5008. GGML_ASSERT(false);
  5009. }
  5010. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5011. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5012. const dim3 block_nums(block_num_x, block_num_y, 1);
  5013. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5014. if (nrows_x % mmq_y == 0) {
  5015. const bool need_check = false;
  5016. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5017. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5018. } else {
  5019. const bool need_check = true;
  5020. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5021. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5022. }
  5023. }
  5024. static void ggml_mul_mat_q5_K_q8_1_cuda(
  5025. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5026. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5027. int id;
  5028. CUDA_CHECK(cudaGetDevice(&id));
  5029. const int compute_capability = g_device_caps[id].cc;
  5030. int mmq_x, mmq_y, nwarps;
  5031. if (compute_capability >= CC_RDNA2) {
  5032. mmq_x = MMQ_X_Q5_K_RDNA2;
  5033. mmq_y = MMQ_Y_Q5_K_RDNA2;
  5034. nwarps = NWARPS_Q5_K_RDNA2;
  5035. } else if (compute_capability >= CC_OFFSET_AMD) {
  5036. mmq_x = MMQ_X_Q5_K_RDNA1;
  5037. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5038. nwarps = NWARPS_Q5_K_RDNA1;
  5039. } else if (compute_capability >= CC_VOLTA) {
  5040. mmq_x = MMQ_X_Q5_K_AMPERE;
  5041. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5042. nwarps = NWARPS_Q5_K_AMPERE;
  5043. } else if (compute_capability >= MIN_CC_DP4A) {
  5044. mmq_x = MMQ_X_Q5_K_PASCAL;
  5045. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5046. nwarps = NWARPS_Q5_K_PASCAL;
  5047. } else {
  5048. GGML_ASSERT(false);
  5049. }
  5050. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5051. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5052. const dim3 block_nums(block_num_x, block_num_y, 1);
  5053. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5054. if (nrows_x % mmq_y == 0) {
  5055. const bool need_check = false;
  5056. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5057. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5058. } else {
  5059. const bool need_check = true;
  5060. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5061. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5062. }
  5063. }
  5064. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5065. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5066. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5067. int id;
  5068. CUDA_CHECK(cudaGetDevice(&id));
  5069. const int compute_capability = g_device_caps[id].cc;
  5070. int mmq_x, mmq_y, nwarps;
  5071. if (compute_capability >= CC_RDNA2) {
  5072. mmq_x = MMQ_X_Q6_K_RDNA2;
  5073. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5074. nwarps = NWARPS_Q6_K_RDNA2;
  5075. } else if (compute_capability >= CC_OFFSET_AMD) {
  5076. mmq_x = MMQ_X_Q6_K_RDNA1;
  5077. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5078. nwarps = NWARPS_Q6_K_RDNA1;
  5079. } else if (compute_capability >= CC_VOLTA) {
  5080. mmq_x = MMQ_X_Q6_K_AMPERE;
  5081. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5082. nwarps = NWARPS_Q6_K_AMPERE;
  5083. } else if (compute_capability >= MIN_CC_DP4A) {
  5084. mmq_x = MMQ_X_Q6_K_PASCAL;
  5085. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5086. nwarps = NWARPS_Q6_K_PASCAL;
  5087. } else {
  5088. GGML_ASSERT(false);
  5089. }
  5090. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5091. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5092. const dim3 block_nums(block_num_x, block_num_y, 1);
  5093. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5094. if (nrows_x % mmq_y == 0) {
  5095. const bool need_check = false;
  5096. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5097. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5098. } else {
  5099. const bool need_check = true;
  5100. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5101. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5102. }
  5103. }
  5104. static void ggml_mul_mat_p021_f16_f32_cuda(
  5105. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5106. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5107. const dim3 block_nums(1, nrows_x, nchannels_y);
  5108. const dim3 block_dims(WARP_SIZE, 1, 1);
  5109. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5110. }
  5111. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5112. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5113. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5114. const dim3 block_nums(1, nrows_x, nchannels_y);
  5115. const dim3 block_dims(WARP_SIZE, 1, 1);
  5116. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5117. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5118. }
  5119. static void ggml_cpy_f32_f32_cuda(
  5120. const char * cx, char * cdst, const int ne,
  5121. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5122. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5123. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5124. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5125. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5126. }
  5127. static void ggml_cpy_f32_f16_cuda(
  5128. const char * cx, char * cdst, const int ne,
  5129. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5130. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5131. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5132. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5133. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5134. }
  5135. static void ggml_cpy_f32_q8_0_cuda(
  5136. const char * cx, char * cdst, const int ne,
  5137. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5138. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5139. GGML_ASSERT(ne % QK8_0 == 0);
  5140. const int num_blocks = ne / QK8_0;
  5141. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5142. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5143. }
  5144. static void ggml_cpy_f32_q4_0_cuda(
  5145. const char * cx, char * cdst, const int ne,
  5146. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5147. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5148. GGML_ASSERT(ne % QK4_0 == 0);
  5149. const int num_blocks = ne / QK4_0;
  5150. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5151. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5152. }
  5153. static void ggml_cpy_f32_q4_1_cuda(
  5154. const char * cx, char * cdst, const int ne,
  5155. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5156. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5157. GGML_ASSERT(ne % QK4_1 == 0);
  5158. const int num_blocks = ne / QK4_1;
  5159. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5160. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5161. }
  5162. static void ggml_cpy_f16_f16_cuda(
  5163. const char * cx, char * cdst, const int ne,
  5164. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5165. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5166. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5167. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5168. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5169. }
  5170. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5171. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5172. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5173. }
  5174. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5175. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5176. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5177. }
  5178. template<typename T>
  5179. static void rope_cuda(
  5180. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5181. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5182. ) {
  5183. GGML_ASSERT(ncols % 2 == 0);
  5184. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5185. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5186. const dim3 block_nums(nrows, num_blocks_x, 1);
  5187. if (pos == nullptr) {
  5188. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5189. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5190. );
  5191. } else {
  5192. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5193. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5194. );
  5195. }
  5196. }
  5197. template<typename T>
  5198. static void rope_neox_cuda(
  5199. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5200. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5201. ) {
  5202. GGML_ASSERT(ncols % 2 == 0);
  5203. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5204. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5205. const dim3 block_nums(nrows, num_blocks_x, 1);
  5206. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5207. const float inv_ndims = -1.0f / n_dims;
  5208. if (pos == nullptr) {
  5209. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5210. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5211. theta_scale, inv_ndims
  5212. );
  5213. } else {
  5214. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5215. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5216. theta_scale, inv_ndims
  5217. );
  5218. }
  5219. }
  5220. static void rope_glm_f32_cuda(
  5221. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5222. float freq_base, int n_ctx, cudaStream_t stream
  5223. ) {
  5224. GGML_ASSERT(ncols % 4 == 0);
  5225. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5226. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5227. const dim3 block_nums(num_blocks_x, nrows, 1);
  5228. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5229. }
  5230. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5231. const int k_rows, const int n_heads_log2_floor, const float m0,
  5232. const float m1, cudaStream_t stream) {
  5233. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5234. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5235. const dim3 block_nums(num_blocks_x, nrows, 1);
  5236. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5237. }
  5238. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5239. const dim3 block_dims(WARP_SIZE, 1, 1);
  5240. const dim3 block_nums(1, nrows, 1);
  5241. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5242. }
  5243. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5244. // bitonic sort requires ncols to be power of 2
  5245. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5246. const dim3 block_dims(ncols, 1, 1);
  5247. const dim3 block_nums(1, nrows, 1);
  5248. if (order == GGML_SORT_ASC) {
  5249. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5250. } else if (order == GGML_SORT_DESC) {
  5251. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5252. } else {
  5253. GGML_ASSERT(false);
  5254. }
  5255. }
  5256. static void diag_mask_inf_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, const int rows_per_channel, const int n_past, cudaStream_t stream) {
  5257. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5258. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5259. const dim3 block_nums(nrows_x, block_num_x, 1);
  5260. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5261. }
  5262. static void soft_max_f32_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {
  5263. int nth = WARP_SIZE;
  5264. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5265. const dim3 block_dims(nth, 1, 1);
  5266. const dim3 block_nums(nrows_x, 1, 1);
  5267. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5268. }
  5269. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5270. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5271. int offset_delta,
  5272. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5273. const int parallel_elements = OW * KW * KH;
  5274. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5275. dim3 block_nums(num_blocks, OH, IC);
  5276. im2col_f32_f16<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, offset_delta, IW, IH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  5277. }
  5278. // buffer pool for cuda
  5279. #define MAX_CUDA_BUFFERS 256
  5280. struct scoped_spin_lock {
  5281. std::atomic_flag& lock;
  5282. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5283. while (lock.test_and_set(std::memory_order_acquire)) {
  5284. ; // spin
  5285. }
  5286. }
  5287. ~scoped_spin_lock() {
  5288. lock.clear(std::memory_order_release);
  5289. }
  5290. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5291. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5292. };
  5293. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5294. // #define DEBUG_CUDA_MALLOC
  5295. struct ggml_cuda_buffer {
  5296. void * ptr = nullptr;
  5297. size_t size = 0;
  5298. };
  5299. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5300. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  5301. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  5302. scoped_spin_lock lock(g_cuda_pool_lock);
  5303. #ifdef DEBUG_CUDA_MALLOC
  5304. int nnz = 0;
  5305. size_t max_size = 0;
  5306. #endif
  5307. size_t best_diff = 1ull << 36;
  5308. int ibest = -1;
  5309. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5310. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5311. if (b.ptr != nullptr) {
  5312. #ifdef DEBUG_CUDA_MALLOC
  5313. ++nnz;
  5314. if (b.size > max_size) max_size = b.size;
  5315. #endif
  5316. if (b.size >= size) {
  5317. size_t diff = b.size - size;
  5318. if (diff < best_diff) {
  5319. best_diff = diff;
  5320. ibest = i;
  5321. if (!best_diff) {
  5322. void * ptr = b.ptr;
  5323. *actual_size = b.size;
  5324. b.ptr = nullptr;
  5325. b.size = 0;
  5326. return ptr;
  5327. }
  5328. }
  5329. }
  5330. }
  5331. }
  5332. if (ibest >= 0) {
  5333. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  5334. void * ptr = b.ptr;
  5335. *actual_size = b.size;
  5336. b.ptr = nullptr;
  5337. b.size = 0;
  5338. return ptr;
  5339. }
  5340. void * ptr;
  5341. size_t look_ahead_size = (size_t) (1.05 * size);
  5342. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5343. ggml_cuda_set_device(device);
  5344. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5345. *actual_size = look_ahead_size;
  5346. g_cuda_pool_size[device] += look_ahead_size;
  5347. #ifdef DEBUG_CUDA_MALLOC
  5348. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  5349. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  5350. #endif
  5351. return ptr;
  5352. }
  5353. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  5354. scoped_spin_lock lock(g_cuda_pool_lock);
  5355. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5356. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  5357. if (b.ptr == nullptr) {
  5358. b.ptr = ptr;
  5359. b.size = size;
  5360. return;
  5361. }
  5362. }
  5363. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5364. ggml_cuda_set_device(device);
  5365. CUDA_CHECK(cudaFree(ptr));
  5366. g_cuda_pool_size[device] -= size;
  5367. }
  5368. #if !defined(GGML_USE_HIPBLAS)
  5369. // pool with virtual memory
  5370. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  5371. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  5372. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 36; // 64 GB
  5373. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  5374. scoped_spin_lock lock(g_cuda_pool_lock);
  5375. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  5376. const size_t alignment = 128;
  5377. size = alignment * ((size + alignment - 1) / alignment);
  5378. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  5379. if (size > avail) {
  5380. // round up to the next multiple of the granularity
  5381. size_t reserve_size = size - avail;
  5382. const size_t granularity = g_device_caps[device].vmm_granularity;
  5383. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  5384. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  5385. // allocate more physical memory
  5386. CUmemAllocationProp prop = {};
  5387. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5388. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5389. prop.location.id = device;
  5390. CUmemGenericAllocationHandle handle;
  5391. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  5392. // reserve virtual address space (if not already reserved)
  5393. if (g_cuda_pool_addr[device] == 0) {
  5394. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  5395. }
  5396. // map at the end of the pool
  5397. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  5398. // the memory allocation handle is no longer needed after mapping
  5399. CU_CHECK(cuMemRelease(handle));
  5400. // set access
  5401. CUmemAccessDesc access = {};
  5402. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5403. access.location.id = device;
  5404. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  5405. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  5406. // add to the pool
  5407. g_cuda_pool_size[device] += reserve_size;
  5408. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  5409. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  5410. // (unsigned long long) (reserve_size/1024/1024));
  5411. }
  5412. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  5413. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  5414. *actual_size = size;
  5415. g_cuda_pool_used[device] += size;
  5416. #ifdef DEBUG_CUDA_MALLOC
  5417. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  5418. #endif
  5419. return ptr;
  5420. }
  5421. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  5422. scoped_spin_lock lock(g_cuda_pool_lock);
  5423. #ifdef DEBUG_CUDA_MALLOC
  5424. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  5425. #endif
  5426. g_cuda_pool_used[device] -= size;
  5427. // all deallocations must be in reverse order of the allocations
  5428. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  5429. }
  5430. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  5431. if (g_device_caps[device].vmm) {
  5432. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  5433. } else {
  5434. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  5435. }
  5436. }
  5437. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  5438. if (g_device_caps[device].vmm) {
  5439. ggml_cuda_pool_free_vmm(device, ptr, size);
  5440. } else {
  5441. ggml_cuda_pool_free_leg(device, ptr, size);
  5442. }
  5443. }
  5444. #else
  5445. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  5446. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  5447. #endif // !defined(GGML_USE_HIPBLAS)
  5448. template<typename T>
  5449. struct cuda_pool_alloc {
  5450. int device = -1;
  5451. T * ptr = nullptr;
  5452. size_t actual_size = 0;
  5453. // size is in number of elements
  5454. T * alloc(size_t size) {
  5455. GGML_ASSERT(ptr == nullptr);
  5456. CUDA_CHECK(cudaGetDevice(&device));
  5457. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  5458. return ptr;
  5459. }
  5460. cuda_pool_alloc(size_t size) {
  5461. alloc(size);
  5462. }
  5463. ~cuda_pool_alloc() {
  5464. if (ptr != nullptr) {
  5465. ggml_cuda_pool_free(device, ptr, actual_size);
  5466. }
  5467. }
  5468. T * get() {
  5469. return ptr;
  5470. }
  5471. cuda_pool_alloc() = default;
  5472. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  5473. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  5474. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  5475. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  5476. };
  5477. static bool g_cublas_loaded = false;
  5478. bool ggml_cublas_loaded(void) {
  5479. return g_cublas_loaded;
  5480. }
  5481. void ggml_init_cublas() {
  5482. static bool initialized = false;
  5483. if (!initialized) {
  5484. #ifdef __HIP_PLATFORM_AMD__
  5485. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5486. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5487. rocblas_initialize();
  5488. CUDA_CHECK(cudaDeviceSynchronize());
  5489. #endif
  5490. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5491. initialized = true;
  5492. g_cublas_loaded = false;
  5493. return;
  5494. }
  5495. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5496. int64_t total_vram = 0;
  5497. #if defined(GGML_CUDA_FORCE_MMQ)
  5498. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5499. #else
  5500. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5501. #endif
  5502. #if defined(CUDA_USE_TENSOR_CORES)
  5503. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5504. #else
  5505. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5506. #endif
  5507. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5508. for (int id = 0; id < g_device_count; ++id) {
  5509. int device_vmm = 0;
  5510. #if !defined(GGML_USE_HIPBLAS)
  5511. CUdevice device;
  5512. CU_CHECK(cuDeviceGet(&device, id));
  5513. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  5514. if (device_vmm) {
  5515. CUmemAllocationProp alloc_prop = {};
  5516. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  5517. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  5518. alloc_prop.location.id = id;
  5519. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  5520. }
  5521. #endif // !defined(GGML_USE_HIPBLAS)
  5522. g_device_caps[id].vmm = !!device_vmm;
  5523. cudaDeviceProp prop;
  5524. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5525. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  5526. g_tensor_split[id] = total_vram;
  5527. total_vram += prop.totalGlobalMem;
  5528. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5529. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5530. #else
  5531. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  5532. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5533. }
  5534. for (int id = 0; id < g_device_count; ++id) {
  5535. g_tensor_split[id] /= total_vram;
  5536. }
  5537. for (int id = 0; id < g_device_count; ++id) {
  5538. ggml_cuda_set_device(id);
  5539. // create cuda streams
  5540. for (int is = 0; is < MAX_STREAMS; ++is) {
  5541. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5542. }
  5543. // create cublas handle
  5544. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5545. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5546. }
  5547. // configure logging to stdout
  5548. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5549. initialized = true;
  5550. g_cublas_loaded = true;
  5551. }
  5552. }
  5553. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5554. if (tensor_split == nullptr) {
  5555. return;
  5556. }
  5557. bool all_zero = true;
  5558. for (int i = 0; i < g_device_count; ++i) {
  5559. if (tensor_split[i] != 0.0f) {
  5560. all_zero = false;
  5561. break;
  5562. }
  5563. }
  5564. if (all_zero) {
  5565. return;
  5566. }
  5567. float split_sum = 0.0f;
  5568. for (int i = 0; i < g_device_count; ++i) {
  5569. g_tensor_split[i] = split_sum;
  5570. split_sum += tensor_split[i];
  5571. }
  5572. for (int i = 0; i < g_device_count; ++i) {
  5573. g_tensor_split[i] /= split_sum;
  5574. }
  5575. }
  5576. void * ggml_cuda_host_malloc(size_t size) {
  5577. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5578. return nullptr;
  5579. }
  5580. void * ptr = nullptr;
  5581. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5582. if (err != cudaSuccess) {
  5583. // clear the error
  5584. cudaGetLastError();
  5585. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5586. size/1024.0/1024.0, cudaGetErrorString(err));
  5587. return nullptr;
  5588. }
  5589. return ptr;
  5590. }
  5591. void ggml_cuda_host_free(void * ptr) {
  5592. CUDA_CHECK(cudaFreeHost(ptr));
  5593. }
  5594. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5595. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5596. cudaMemcpyKind kind;
  5597. char * src_ptr;
  5598. if (src->backend == GGML_BACKEND_CPU) {
  5599. kind = cudaMemcpyHostToDevice;
  5600. src_ptr = (char *) src->data;
  5601. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5602. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5603. kind = cudaMemcpyDeviceToDevice;
  5604. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5605. int id;
  5606. CUDA_CHECK(cudaGetDevice(&id));
  5607. src_ptr = (char *) extra->data_device[id];
  5608. } else {
  5609. GGML_ASSERT(false);
  5610. }
  5611. char * dst_ptr = (char *) dst;
  5612. const int64_t ne0 = src->ne[0];
  5613. const int64_t nb0 = src->nb[0];
  5614. const int64_t nb1 = src->nb[1];
  5615. const int64_t nb2 = src->nb[2];
  5616. const int64_t nb3 = src->nb[3];
  5617. const enum ggml_type type = src->type;
  5618. const int64_t ts = ggml_type_size(type);
  5619. const int64_t bs = ggml_blck_size(type);
  5620. int64_t i1_diff = i1_high - i1_low;
  5621. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5622. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5623. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5624. } else if (nb0 == ts) {
  5625. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5626. } else {
  5627. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5628. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5629. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5630. // pretend the row is a matrix with cols=1
  5631. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5632. if (r != cudaSuccess) return r;
  5633. }
  5634. return cudaSuccess;
  5635. }
  5636. }
  5637. static void ggml_cuda_op_get_rows(
  5638. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5639. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  5640. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5641. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5642. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5643. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5644. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5645. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5646. switch (src0->type) {
  5647. case GGML_TYPE_F16:
  5648. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5649. break;
  5650. case GGML_TYPE_F32:
  5651. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5652. break;
  5653. case GGML_TYPE_Q4_0:
  5654. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5655. break;
  5656. case GGML_TYPE_Q4_1:
  5657. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5658. break;
  5659. case GGML_TYPE_Q5_0:
  5660. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5661. break;
  5662. case GGML_TYPE_Q5_1:
  5663. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5664. break;
  5665. case GGML_TYPE_Q8_0:
  5666. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5667. break;
  5668. default:
  5669. // TODO: k-quants
  5670. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  5671. GGML_ASSERT(false);
  5672. break;
  5673. }
  5674. }
  5675. template<class op>
  5676. static void ggml_cuda_op_bin_bcast(
  5677. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5678. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5679. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5680. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5681. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5682. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5683. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5684. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5685. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5686. } else {
  5687. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5688. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5689. GGML_ASSERT(false);
  5690. }
  5691. }
  5692. static void ggml_cuda_op_repeat(
  5693. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5694. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  5695. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5696. (void) src1;
  5697. (void) src1_d;
  5698. }
  5699. static void ggml_cuda_op_add(
  5700. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5701. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5702. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5703. }
  5704. static void ggml_cuda_op_acc(
  5705. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5706. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5707. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5708. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5709. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5710. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5711. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5712. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5713. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5714. int offset = dst->op_params[3] / 4; // offset in bytes
  5715. acc_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  5716. (void) dst;
  5717. }
  5718. static void ggml_cuda_op_mul(
  5719. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5720. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5721. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5722. }
  5723. static void ggml_cuda_op_div(
  5724. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5725. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5726. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5727. }
  5728. static void ggml_cuda_op_gelu(
  5729. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5730. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5731. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5732. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5733. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5734. (void) src1;
  5735. (void) dst;
  5736. (void) src1_dd;
  5737. }
  5738. static void ggml_cuda_op_silu(
  5739. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5740. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5741. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5742. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5743. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5744. (void) src1;
  5745. (void) dst;
  5746. (void) src1_dd;
  5747. }
  5748. static void ggml_cuda_op_gelu_quick(
  5749. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5750. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5751. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5752. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5753. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5754. (void) src1;
  5755. (void) dst;
  5756. (void) src1_dd;
  5757. }
  5758. static void ggml_cuda_op_tanh(
  5759. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5760. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5761. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5762. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5763. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5764. (void) src1;
  5765. (void) dst;
  5766. (void) src1_dd;
  5767. }
  5768. static void ggml_cuda_op_relu(
  5769. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5770. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5771. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5772. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5773. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5774. (void) src1;
  5775. (void) dst;
  5776. (void) src1_dd;
  5777. }
  5778. static void ggml_cuda_op_leaky_relu(
  5779. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5780. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5781. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5782. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5783. float negative_slope;
  5784. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5785. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5786. (void) src1;
  5787. (void) dst;
  5788. (void) src1_dd;
  5789. }
  5790. static void ggml_cuda_op_sqr(
  5791. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5792. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5793. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5794. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5795. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5796. (void) src1;
  5797. (void) dst;
  5798. (void) src1_dd;
  5799. }
  5800. static void ggml_cuda_op_norm(
  5801. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5802. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5803. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5804. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5805. const int64_t ne00 = src0->ne[0];
  5806. const int64_t nrows = ggml_nrows(src0);
  5807. float eps;
  5808. memcpy(&eps, dst->op_params, sizeof(float));
  5809. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5810. (void) src1;
  5811. (void) dst;
  5812. (void) src1_dd;
  5813. }
  5814. static void ggml_cuda_op_group_norm(
  5815. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5816. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5817. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5818. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5819. int num_groups = dst->op_params[0];
  5820. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5821. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5822. (void) src1;
  5823. (void) dst;
  5824. (void) src1_dd;
  5825. }
  5826. static void ggml_cuda_op_concat(
  5827. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5828. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5829. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5830. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5831. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5832. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5833. concat_f32_cuda(src0_dd + i3 * (src0->nb[3] / 4), src1_dd + i3 * (src1->nb[3] / 4), dst_dd + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], main_stream);
  5834. }
  5835. (void) src1;
  5836. (void) dst;
  5837. }
  5838. static void ggml_cuda_op_upscale(
  5839. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5840. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5841. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5842. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5843. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5844. const int scale_factor = dst->op_params[0];
  5845. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5846. (void) src1;
  5847. (void) dst;
  5848. (void) src1_dd;
  5849. }
  5850. static void ggml_cuda_op_pad(
  5851. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5852. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5853. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5854. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5855. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5856. pad_f32_cuda(src0_dd, dst_dd,
  5857. src0->ne[0], src0->ne[1], src0->ne[2],
  5858. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5859. (void) src1;
  5860. (void) dst;
  5861. (void) src1_dd;
  5862. }
  5863. static void ggml_cuda_op_rms_norm(
  5864. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5865. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  5866. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5867. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5868. const int64_t ne00 = src0->ne[0];
  5869. const int64_t nrows = ggml_nrows(src0);
  5870. float eps;
  5871. memcpy(&eps, dst->op_params, sizeof(float));
  5872. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5873. (void) src1;
  5874. (void) dst;
  5875. (void) src1_dd;
  5876. }
  5877. static void ggml_cuda_op_mul_mat_q(
  5878. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5879. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5880. const int64_t src1_padded_row_size, cudaStream_t stream) {
  5881. const int64_t ne00 = src0->ne[0];
  5882. const int64_t ne10 = src1->ne[0];
  5883. GGML_ASSERT(ne10 % QK8_1 == 0);
  5884. const int64_t ne0 = dst->ne[0];
  5885. const int64_t row_diff = row_high - row_low;
  5886. int id;
  5887. CUDA_CHECK(cudaGetDevice(&id));
  5888. // the main device has a larger memory buffer to hold the results from all GPUs
  5889. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5890. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5891. switch (src0->type) {
  5892. case GGML_TYPE_Q4_0:
  5893. ggml_mul_mat_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5894. break;
  5895. case GGML_TYPE_Q4_1:
  5896. ggml_mul_mat_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5897. break;
  5898. case GGML_TYPE_Q5_0:
  5899. ggml_mul_mat_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5900. break;
  5901. case GGML_TYPE_Q5_1:
  5902. ggml_mul_mat_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5903. break;
  5904. case GGML_TYPE_Q8_0:
  5905. ggml_mul_mat_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5906. break;
  5907. case GGML_TYPE_Q2_K:
  5908. ggml_mul_mat_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5909. break;
  5910. case GGML_TYPE_Q3_K:
  5911. ggml_mul_mat_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5912. break;
  5913. case GGML_TYPE_Q4_K:
  5914. ggml_mul_mat_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5915. break;
  5916. case GGML_TYPE_Q5_K:
  5917. ggml_mul_mat_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5918. break;
  5919. case GGML_TYPE_Q6_K:
  5920. ggml_mul_mat_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  5921. break;
  5922. default:
  5923. GGML_ASSERT(false);
  5924. break;
  5925. }
  5926. (void) src1;
  5927. (void) dst;
  5928. (void) src1_ddf_i;
  5929. }
  5930. static int64_t get_row_rounding(ggml_type type) {
  5931. int64_t min_compute_capability = INT_MAX;
  5932. int64_t max_compute_capability = INT_MIN;
  5933. for (int id = 0; id < g_device_count; ++id) {
  5934. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5935. if (min_compute_capability > g_device_caps[id].cc) {
  5936. min_compute_capability = g_device_caps[id].cc;
  5937. }
  5938. if (max_compute_capability < g_device_caps[id].cc) {
  5939. max_compute_capability = g_device_caps[id].cc;
  5940. }
  5941. }
  5942. }
  5943. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5944. switch(type) {
  5945. case GGML_TYPE_Q4_0:
  5946. case GGML_TYPE_Q4_1:
  5947. case GGML_TYPE_Q5_0:
  5948. case GGML_TYPE_Q5_1:
  5949. case GGML_TYPE_Q8_0:
  5950. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5951. case GGML_TYPE_F16:
  5952. case GGML_TYPE_F32:
  5953. return 1;
  5954. case GGML_TYPE_Q2_K:
  5955. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5956. case GGML_TYPE_Q3_K:
  5957. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5958. case GGML_TYPE_Q4_K:
  5959. case GGML_TYPE_Q5_K:
  5960. case GGML_TYPE_Q6_K:
  5961. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5962. default:
  5963. GGML_ASSERT(false);
  5964. }
  5965. #else
  5966. switch(type) {
  5967. case GGML_TYPE_Q4_0:
  5968. case GGML_TYPE_Q4_1:
  5969. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5970. case GGML_TYPE_Q5_0:
  5971. case GGML_TYPE_Q5_1:
  5972. case GGML_TYPE_Q8_0:
  5973. return 64;
  5974. case GGML_TYPE_F16:
  5975. case GGML_TYPE_F32:
  5976. return 1;
  5977. case GGML_TYPE_Q2_K:
  5978. case GGML_TYPE_Q3_K:
  5979. case GGML_TYPE_Q4_K:
  5980. case GGML_TYPE_Q5_K:
  5981. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5982. case GGML_TYPE_Q6_K:
  5983. return 64;
  5984. default:
  5985. GGML_ASSERT(false);
  5986. }
  5987. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5988. }
  5989. static void ggml_cuda_op_mul_mat_vec_q(
  5990. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5991. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5992. const int64_t src1_padded_row_size, cudaStream_t stream) {
  5993. GGML_ASSERT(ggml_nrows(src1) == 1);
  5994. const int64_t ne00 = src0->ne[0];
  5995. const int64_t row_diff = row_high - row_low;
  5996. switch (src0->type) {
  5997. case GGML_TYPE_Q4_0:
  5998. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5999. break;
  6000. case GGML_TYPE_Q4_1:
  6001. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6002. break;
  6003. case GGML_TYPE_Q5_0:
  6004. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6005. break;
  6006. case GGML_TYPE_Q5_1:
  6007. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6008. break;
  6009. case GGML_TYPE_Q8_0:
  6010. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6011. break;
  6012. case GGML_TYPE_Q2_K:
  6013. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6014. break;
  6015. case GGML_TYPE_Q3_K:
  6016. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6017. break;
  6018. case GGML_TYPE_Q4_K:
  6019. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6020. break;
  6021. case GGML_TYPE_Q5_K:
  6022. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6023. break;
  6024. case GGML_TYPE_Q6_K:
  6025. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  6026. break;
  6027. default:
  6028. GGML_ASSERT(false);
  6029. break;
  6030. }
  6031. (void) src1;
  6032. (void) dst;
  6033. (void) src1_ddf_i;
  6034. (void) src1_ncols;
  6035. (void) src1_padded_row_size;
  6036. }
  6037. static void ggml_cuda_op_dequantize_mul_mat_vec(
  6038. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6039. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6040. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6041. const int64_t ne00 = src0->ne[0];
  6042. const int64_t row_diff = row_high - row_low;
  6043. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6044. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  6045. #ifdef GGML_CUDA_F16
  6046. cuda_pool_alloc<half> src1_dfloat_a;
  6047. half * src1_dfloat = nullptr; // dfloat == half
  6048. bool src1_convert_f16 =
  6049. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  6050. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  6051. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  6052. if (src1_convert_f16) {
  6053. src1_dfloat = src1_dfloat_a.alloc(ne00);
  6054. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  6055. ne00, 1, sizeof(float), 0, 0,
  6056. ne00, 1, sizeof(half), 0, 0, stream);
  6057. }
  6058. #else
  6059. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  6060. #endif // GGML_CUDA_F16
  6061. switch (src0->type) {
  6062. case GGML_TYPE_Q4_0:
  6063. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6064. break;
  6065. case GGML_TYPE_Q4_1:
  6066. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6067. break;
  6068. case GGML_TYPE_Q5_0:
  6069. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6070. break;
  6071. case GGML_TYPE_Q5_1:
  6072. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6073. break;
  6074. case GGML_TYPE_Q8_0:
  6075. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6076. break;
  6077. case GGML_TYPE_Q2_K:
  6078. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6079. break;
  6080. case GGML_TYPE_Q3_K:
  6081. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6082. break;
  6083. case GGML_TYPE_Q4_K:
  6084. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6085. break;
  6086. case GGML_TYPE_Q5_K:
  6087. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6088. break;
  6089. case GGML_TYPE_Q6_K:
  6090. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  6091. break;
  6092. case GGML_TYPE_F16:
  6093. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  6094. break;
  6095. default:
  6096. GGML_ASSERT(false);
  6097. break;
  6098. }
  6099. (void) src1;
  6100. (void) dst;
  6101. (void) src1_ddq_i;
  6102. (void) src1_ncols;
  6103. (void) src1_padded_row_size;
  6104. }
  6105. static void ggml_cuda_op_mul_mat_cublas(
  6106. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6107. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6108. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6109. GGML_ASSERT(src0_dd_i != nullptr);
  6110. GGML_ASSERT(src1_ddf_i != nullptr);
  6111. GGML_ASSERT(dst_dd_i != nullptr);
  6112. const int64_t ne00 = src0->ne[0];
  6113. const int64_t ne10 = src1->ne[0];
  6114. const int64_t ne0 = dst->ne[0];
  6115. const int64_t row_diff = row_high - row_low;
  6116. int id;
  6117. CUDA_CHECK(cudaGetDevice(&id));
  6118. // the main device has a larger memory buffer to hold the results from all GPUs
  6119. // ldc == nrows of the matrix that cuBLAS writes into
  6120. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6121. const int compute_capability = g_device_caps[id].cc;
  6122. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  6123. //printf("this branch\n");
  6124. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  6125. cuda_pool_alloc<half> src0_as_f16;
  6126. if (src0->type != GGML_TYPE_F16) {
  6127. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  6128. GGML_ASSERT(to_fp16_cuda != nullptr);
  6129. size_t ne = row_diff*ne00;
  6130. src0_as_f16.alloc(ne);
  6131. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  6132. }
  6133. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  6134. cuda_pool_alloc<half> src1_as_f16;
  6135. if (src1->type != GGML_TYPE_F16) {
  6136. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6137. GGML_ASSERT(to_fp16_cuda != nullptr);
  6138. size_t ne = src1_ncols*ne10;
  6139. src1_as_f16.alloc(ne);
  6140. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  6141. }
  6142. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  6143. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  6144. const half alpha_f16 = 1.0f;
  6145. const half beta_f16 = 0.0f;
  6146. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6147. CUBLAS_CHECK(
  6148. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6149. row_diff, src1_ncols, ne10,
  6150. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  6151. src1_ptr, CUDA_R_16F, ne10,
  6152. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  6153. CUBLAS_COMPUTE_16F,
  6154. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6155. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6156. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  6157. } else {
  6158. cuda_pool_alloc<float> src0_ddq_as_f32;
  6159. cuda_pool_alloc<float> src1_ddq_as_f32;
  6160. if (src0->type != GGML_TYPE_F32) {
  6161. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6162. GGML_ASSERT(to_fp32_cuda != nullptr);
  6163. src0_ddq_as_f32.alloc(row_diff*ne00);
  6164. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  6165. }
  6166. if (src1->type != GGML_TYPE_F32) {
  6167. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  6168. GGML_ASSERT(to_fp32_cuda != nullptr);
  6169. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  6170. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  6171. }
  6172. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  6173. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  6174. const float alpha = 1.0f;
  6175. const float beta = 0.0f;
  6176. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6177. CUBLAS_CHECK(
  6178. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6179. row_diff, src1_ncols, ne10,
  6180. &alpha, src0_ddf_i, ne00,
  6181. src1_ddf1_i, ne10,
  6182. &beta, dst_dd_i, ldc));
  6183. }
  6184. (void) dst;
  6185. (void) src1_ddq_i;
  6186. (void) src1_padded_row_size;
  6187. }
  6188. static void ggml_cuda_op_rope(
  6189. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6190. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6191. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6192. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6193. GGML_ASSERT(src0->type == dst->type);
  6194. const int64_t ne00 = src0->ne[0];
  6195. const int64_t ne01 = src0->ne[1];
  6196. const int64_t ne2 = dst->ne[2];
  6197. const int64_t nrows = ggml_nrows(src0);
  6198. //const int n_past = ((int32_t *) dst->op_params)[0];
  6199. const int n_dims = ((int32_t *) dst->op_params)[1];
  6200. const int mode = ((int32_t *) dst->op_params)[2];
  6201. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6202. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6203. // RoPE alteration for extended context
  6204. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6205. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6206. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6207. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6208. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6209. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6210. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6211. const int32_t * pos = nullptr;
  6212. if ((mode & 1) == 0) {
  6213. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6214. GGML_ASSERT(src1->ne[0] == ne2);
  6215. pos = (const int32_t *) src1_dd;
  6216. }
  6217. const bool is_neox = mode & 2;
  6218. const bool is_glm = mode & 4;
  6219. rope_corr_dims corr_dims;
  6220. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6221. // compute
  6222. if (is_glm) {
  6223. GGML_ASSERT(false);
  6224. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6225. } else if (is_neox) {
  6226. if (src0->type == GGML_TYPE_F32) {
  6227. rope_neox_cuda(
  6228. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6229. attn_factor, corr_dims, main_stream
  6230. );
  6231. } else if (src0->type == GGML_TYPE_F16) {
  6232. rope_neox_cuda(
  6233. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6234. attn_factor, corr_dims, main_stream
  6235. );
  6236. } else {
  6237. GGML_ASSERT(false);
  6238. }
  6239. } else {
  6240. if (src0->type == GGML_TYPE_F32) {
  6241. rope_cuda(
  6242. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6243. attn_factor, corr_dims, main_stream
  6244. );
  6245. } else if (src0->type == GGML_TYPE_F16) {
  6246. rope_cuda(
  6247. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6248. attn_factor, corr_dims, main_stream
  6249. );
  6250. } else {
  6251. GGML_ASSERT(false);
  6252. }
  6253. }
  6254. (void) src1;
  6255. (void) dst;
  6256. (void) src1_dd;
  6257. }
  6258. static void ggml_cuda_op_alibi(
  6259. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6260. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6261. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6262. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6263. const int64_t ne00 = src0->ne[0];
  6264. const int64_t ne01 = src0->ne[1];
  6265. const int64_t ne02 = src0->ne[2];
  6266. const int64_t nrows = ggml_nrows(src0);
  6267. //const int n_past = ((int32_t *) dst->op_params)[0];
  6268. const int n_head = ((int32_t *) dst->op_params)[1];
  6269. float max_bias;
  6270. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6271. //GGML_ASSERT(ne01 + n_past == ne00);
  6272. GGML_ASSERT(n_head == ne02);
  6273. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6274. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6275. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6276. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6277. (void) src1;
  6278. (void) src1_dd;
  6279. }
  6280. static void ggml_cuda_op_im2col(
  6281. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6282. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6283. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6284. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6285. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6286. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6287. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6288. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6289. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6290. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6291. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6292. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6293. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6294. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6295. const int64_t IW = src1->ne[0];
  6296. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6297. const int64_t KW = src0->ne[0];
  6298. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6299. const int64_t OW = dst->ne[1];
  6300. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6301. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  6302. (void) src0;
  6303. (void) src0_dd;
  6304. }
  6305. static void ggml_cuda_op_sum_rows(
  6306. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6307. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6308. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6309. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6310. const int64_t ncols = src0->ne[0];
  6311. const int64_t nrows = ggml_nrows(src0);
  6312. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6313. (void) src1;
  6314. (void) dst;
  6315. (void) src1_dd;
  6316. }
  6317. static void ggml_cuda_op_argsort(
  6318. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6319. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6320. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6321. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6322. const int64_t ncols = src0->ne[0];
  6323. const int64_t nrows = ggml_nrows(src0);
  6324. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6325. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6326. (void) src1;
  6327. (void) dst;
  6328. (void) src1_dd;
  6329. }
  6330. static void ggml_cuda_op_diag_mask_inf(
  6331. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6332. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6333. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6334. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6335. const int64_t ne00 = src0->ne[0];
  6336. const int64_t ne01 = src0->ne[1];
  6337. const int nrows0 = ggml_nrows(src0);
  6338. const int n_past = ((int32_t *) dst->op_params)[0];
  6339. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6340. (void) src1;
  6341. (void) dst;
  6342. (void) src1_dd;
  6343. }
  6344. static void ggml_cuda_op_soft_max(
  6345. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6346. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6347. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6348. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6349. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6350. const int64_t ne00 = src0->ne[0];
  6351. const int64_t nrows_x = ggml_nrows(src0);
  6352. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6353. float scale = 1.0f;
  6354. memcpy(&scale, dst->op_params, sizeof(float));
  6355. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6356. (void) dst;
  6357. }
  6358. static void ggml_cuda_op_scale(
  6359. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6360. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6361. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6362. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6363. float scale;
  6364. memcpy(&scale, dst->op_params, sizeof(float));
  6365. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6366. CUDA_CHECK(cudaGetLastError());
  6367. (void) src1;
  6368. (void) dst;
  6369. (void) src1_dd;
  6370. }
  6371. static void ggml_cuda_op_clamp(
  6372. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6373. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6374. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6375. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6376. float min;
  6377. float max;
  6378. memcpy(&min, dst->op_params, sizeof(float));
  6379. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6380. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6381. CUDA_CHECK(cudaGetLastError());
  6382. (void) src1;
  6383. (void) dst;
  6384. (void) src1_dd;
  6385. }
  6386. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6387. const int64_t nrows0 = ggml_nrows(src0);
  6388. const bool use_src1 = src1 != nullptr;
  6389. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6390. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6391. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6392. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6393. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6394. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6395. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6396. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6397. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6398. // dd = data device
  6399. float * src0_ddf = nullptr;
  6400. float * src1_ddf = nullptr;
  6401. float * dst_ddf = nullptr;
  6402. cuda_pool_alloc<float> src0_f;
  6403. cuda_pool_alloc<float> src1_f;
  6404. cuda_pool_alloc<float> dst_f;
  6405. ggml_cuda_set_device(g_main_device);
  6406. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6407. if (src0_on_device) {
  6408. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6409. } else {
  6410. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  6411. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6412. }
  6413. if (use_src1) {
  6414. if (src1_on_device) {
  6415. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6416. } else {
  6417. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  6418. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6419. }
  6420. }
  6421. if (dst_on_device) {
  6422. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6423. } else {
  6424. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  6425. }
  6426. // do the computation
  6427. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6428. CUDA_CHECK(cudaGetLastError());
  6429. // copy dst to host if necessary
  6430. if (!dst_on_device) {
  6431. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6432. }
  6433. if (dst->backend == GGML_BACKEND_CPU) {
  6434. CUDA_CHECK(cudaDeviceSynchronize());
  6435. }
  6436. }
  6437. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6438. static bool peer_access_enabled = false;
  6439. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6440. if (peer_access_enabled == enable_peer_access) {
  6441. return;
  6442. }
  6443. #ifdef NDEBUG
  6444. for (int id = 0; id < g_device_count; ++id) {
  6445. ggml_cuda_set_device(id);
  6446. CUDA_CHECK(cudaDeviceSynchronize());
  6447. }
  6448. for (int id = 0; id < g_device_count; ++id) {
  6449. ggml_cuda_set_device(id);
  6450. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6451. if (id == id_other) {
  6452. continue;
  6453. }
  6454. if (id != g_main_device && id_other != g_main_device) {
  6455. continue;
  6456. }
  6457. int can_access_peer;
  6458. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6459. if (can_access_peer) {
  6460. if (enable_peer_access) {
  6461. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6462. } else {
  6463. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6464. }
  6465. }
  6466. }
  6467. }
  6468. #endif // NDEBUG
  6469. peer_access_enabled = enable_peer_access;
  6470. }
  6471. static void ggml_cuda_op_mul_mat(
  6472. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6473. const bool convert_src1_to_q8_1) {
  6474. const int64_t ne00 = src0->ne[0];
  6475. const int64_t ne01 = src0->ne[1];
  6476. const int64_t ne02 = src0->ne[2];
  6477. const int64_t ne03 = src0->ne[3];
  6478. const int64_t ne10 = src1->ne[0];
  6479. const int64_t ne11 = src1->ne[1];
  6480. const int64_t ne12 = src1->ne[2];
  6481. const int64_t ne13 = src1->ne[3];
  6482. const int64_t nrows1 = ggml_nrows(src1);
  6483. GGML_ASSERT(ne03 == ne13);
  6484. const int64_t ne0 = dst->ne[0];
  6485. const int64_t ne1 = dst->ne[1];
  6486. const int nb2 = dst->nb[2];
  6487. const int nb3 = dst->nb[3];
  6488. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6489. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6490. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  6491. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6492. const int64_t i02_divisor = ne12 / ne02;
  6493. const size_t src0_ts = ggml_type_size(src0->type);
  6494. const size_t src0_bs = ggml_blck_size(src0->type);
  6495. const size_t q8_1_ts = sizeof(block_q8_1);
  6496. const size_t q8_1_bs = QK8_1;
  6497. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6498. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6499. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6500. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6501. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6502. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6503. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6504. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6505. GGML_ASSERT(!(split && ne02 > 1));
  6506. GGML_ASSERT(!(split && ne03 > 1));
  6507. GGML_ASSERT(!(split && ne02 < ne12));
  6508. struct dev_data {
  6509. cuda_pool_alloc<char> src0_dd_alloc;
  6510. cuda_pool_alloc<float> src1_ddf_alloc;
  6511. cuda_pool_alloc<char> src1_ddq_alloc;
  6512. cuda_pool_alloc<float> dst_dd_alloc;
  6513. char * src0_dd = nullptr;
  6514. float * src1_ddf = nullptr; // float
  6515. char * src1_ddq = nullptr; // q8_1
  6516. float * dst_dd = nullptr;
  6517. int64_t row_low;
  6518. int64_t row_high;
  6519. };
  6520. dev_data dev[GGML_CUDA_MAX_DEVICES];
  6521. int used_devices = 0;
  6522. for (int id = 0; id < g_device_count; ++id) {
  6523. // by default, use all rows
  6524. dev[id].row_low = 0;
  6525. dev[id].row_high = ne01;
  6526. // for multi GPU, get the row boundaries from tensor split
  6527. // and round to mul_mat_q tile sizes
  6528. if (split) {
  6529. const int64_t rounding = get_row_rounding(src0->type);
  6530. if (id != 0) {
  6531. dev[id].row_low = ne01*g_tensor_split[id];
  6532. if (dev[id].row_low < ne01) {
  6533. dev[id].row_low -= dev[id].row_low % rounding;
  6534. }
  6535. }
  6536. if (id != g_device_count - 1) {
  6537. dev[id].row_high = ne01*g_tensor_split[id + 1];
  6538. if (dev[id].row_high < ne01) {
  6539. dev[id].row_high -= dev[id].row_high % rounding;
  6540. }
  6541. }
  6542. }
  6543. }
  6544. for (int id = 0; id < g_device_count; ++id) {
  6545. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  6546. continue;
  6547. }
  6548. used_devices++;
  6549. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6550. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6551. ggml_cuda_set_device(id);
  6552. cudaStream_t stream = g_cudaStreams[id][0];
  6553. if (src0_on_device && src0_is_contiguous) {
  6554. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  6555. } else {
  6556. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  6557. }
  6558. if (src1_on_device && src1_is_contiguous) {
  6559. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  6560. } else {
  6561. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  6562. }
  6563. if (convert_src1_to_q8_1) {
  6564. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  6565. if (src1_on_device && src1_is_contiguous) {
  6566. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  6567. CUDA_CHECK(cudaGetLastError());
  6568. }
  6569. }
  6570. if (dst_on_device) {
  6571. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  6572. } else {
  6573. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  6574. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  6575. }
  6576. }
  6577. // if multiple devices are used they need to wait for the main device
  6578. // here an event is recorded that signals that the main device has finished calculating the input data
  6579. if (split && used_devices > 1) {
  6580. ggml_cuda_set_device(g_main_device);
  6581. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6582. }
  6583. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6584. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6585. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6586. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6587. for (int id = 0; id < g_device_count; ++id) {
  6588. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  6589. continue;
  6590. }
  6591. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6592. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6593. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  6594. ggml_cuda_set_device(id);
  6595. cudaStream_t stream = g_cudaStreams[id][is];
  6596. // wait for main GPU data if necessary
  6597. if (split && (id != g_main_device || is != 0)) {
  6598. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6599. }
  6600. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6601. const int64_t i03 = i0 / ne12;
  6602. const int64_t i02 = i0 % ne12;
  6603. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6604. // for split tensors the data begins at i0 == i0_offset_low
  6605. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6606. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  6607. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  6608. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6609. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6610. // in that case an offset on dst_ddf_i is needed
  6611. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6612. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  6613. }
  6614. // copy src0, src1 to device if necessary
  6615. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6616. if (id != g_main_device) {
  6617. if (convert_src1_to_q8_1) {
  6618. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  6619. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  6620. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  6621. } else {
  6622. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6623. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6624. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  6625. src1_ncols*ne10*sizeof(float), stream));
  6626. }
  6627. }
  6628. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6629. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6630. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6631. } else {
  6632. GGML_ASSERT(false);
  6633. }
  6634. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6635. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6636. CUDA_CHECK(cudaGetLastError());
  6637. }
  6638. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6639. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  6640. }
  6641. // do the computation
  6642. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6643. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  6644. CUDA_CHECK(cudaGetLastError());
  6645. // copy dst to host or other device if necessary
  6646. if (!dst_on_device) {
  6647. void * dst_off_device;
  6648. cudaMemcpyKind kind;
  6649. if (dst->backend == GGML_BACKEND_CPU) {
  6650. dst_off_device = dst->data;
  6651. kind = cudaMemcpyDeviceToHost;
  6652. } else if (dst->backend == GGML_BACKEND_GPU) {
  6653. dst_off_device = dst_extra->data_device[g_main_device];
  6654. kind = cudaMemcpyDeviceToDevice;
  6655. } else {
  6656. GGML_ASSERT(false);
  6657. }
  6658. if (split) {
  6659. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6660. // dst is NOT transposed.
  6661. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6662. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6663. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6664. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6665. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6666. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  6667. #if !defined(GGML_USE_HIPBLAS)
  6668. if (kind == cudaMemcpyDeviceToDevice) {
  6669. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  6670. cudaMemcpy3DPeerParms p = {};
  6671. p.dstDevice = g_main_device;
  6672. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  6673. p.srcDevice = id;
  6674. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  6675. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  6676. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  6677. } else
  6678. #endif
  6679. {
  6680. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  6681. dst_dd_i, row_diff*sizeof(float),
  6682. row_diff*sizeof(float), src1_ncols,
  6683. kind, stream));
  6684. }
  6685. } else {
  6686. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6687. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6688. dhf_dst_i += src1_col_0*ne0;
  6689. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6690. }
  6691. }
  6692. // add event for the main device to wait on until other device is done
  6693. if (split && (id != g_main_device || is != 0)) {
  6694. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6695. }
  6696. }
  6697. }
  6698. }
  6699. // main device waits for all other devices to be finished
  6700. if (split && g_device_count > 1) {
  6701. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6702. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6703. ggml_cuda_set_device(g_main_device);
  6704. for (int id = 0; id < g_device_count; ++id) {
  6705. if (dev[id].row_low == dev[id].row_high) {
  6706. continue;
  6707. }
  6708. for (int64_t is = 0; is < is_max; ++is) {
  6709. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6710. }
  6711. }
  6712. }
  6713. if (dst->backend == GGML_BACKEND_CPU) {
  6714. ggml_cuda_set_device(g_main_device);
  6715. CUDA_CHECK(cudaDeviceSynchronize());
  6716. }
  6717. }
  6718. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6719. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6720. }
  6721. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6722. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6723. }
  6724. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6725. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6726. }
  6727. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6728. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6729. }
  6730. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6731. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6732. }
  6733. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6734. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6735. }
  6736. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6737. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6738. }
  6739. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6740. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6741. }
  6742. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6743. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6744. }
  6745. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6746. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6747. }
  6748. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6749. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6750. }
  6751. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6752. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6753. }
  6754. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6755. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6756. }
  6757. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6758. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6759. }
  6760. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6761. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6762. }
  6763. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6764. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6765. }
  6766. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6767. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6768. }
  6769. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6770. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6771. }
  6772. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6773. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6774. }
  6775. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6776. if (!g_cublas_loaded) return false;
  6777. const int64_t ne10 = src1->ne[0];
  6778. const int64_t ne0 = dst->ne[0];
  6779. const int64_t ne1 = dst->ne[1];
  6780. // TODO: find the optimal values for these
  6781. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6782. src1->type == GGML_TYPE_F32 &&
  6783. dst->type == GGML_TYPE_F32 &&
  6784. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6785. }
  6786. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6787. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6788. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6789. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6790. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6791. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6792. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6793. const int64_t ne00 = src0->ne[0];
  6794. const int64_t ne01 = src0->ne[1];
  6795. const int64_t ne02 = src0->ne[2];
  6796. const int64_t ne12 = src1->ne[2];
  6797. ggml_cuda_set_device(g_main_device);
  6798. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6799. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6800. void * src0_ddq = src0_extra->data_device[g_main_device];
  6801. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6802. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6803. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6804. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6805. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6806. }
  6807. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6808. GGML_ASSERT(!ggml_is_transposed(src0));
  6809. GGML_ASSERT(!ggml_is_transposed(src1));
  6810. GGML_ASSERT(!ggml_is_permuted(src0));
  6811. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6812. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6813. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6814. const int64_t ne00 = src0->ne[0];
  6815. const int64_t ne01 = src0->ne[1];
  6816. const int64_t ne02 = src0->ne[2];
  6817. const int64_t nb01 = src0->nb[1];
  6818. const int64_t nb02 = src0->nb[2];
  6819. const int64_t ne12 = src1->ne[2];
  6820. ggml_cuda_set_device(g_main_device);
  6821. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6822. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6823. void * src0_ddq = src0_extra->data_device[g_main_device];
  6824. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6825. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6826. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6827. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6828. const int64_t row_stride_x = nb01 / sizeof(half);
  6829. const int64_t channel_stride_x = nb02 / sizeof(half);
  6830. ggml_mul_mat_vec_nc_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, ne12, channel_stride_x, main_stream);
  6831. }
  6832. static __global__ void k_compute_batched_ptrs(
  6833. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  6834. const void ** ptrs_src, void ** ptrs_dst,
  6835. int64_t ne12, int64_t ne13,
  6836. int64_t ne23,
  6837. size_t nb02, size_t nb03,
  6838. size_t nb12, size_t nb13,
  6839. size_t nbd2, size_t nbd3,
  6840. int64_t r2, int64_t r3) {
  6841. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6842. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6843. if (i13 >= ne13 || i12 >= ne12) {
  6844. return;
  6845. }
  6846. int64_t i03 = i13 / r3;
  6847. int64_t i02 = i12 / r2;
  6848. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6849. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  6850. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  6851. }
  6852. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6853. GGML_ASSERT(!ggml_is_transposed(src0));
  6854. GGML_ASSERT(!ggml_is_transposed(src1));
  6855. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6856. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6857. GGML_TENSOR_BINARY_OP_LOCALS
  6858. const int64_t ne_dst = ggml_nelements(dst);
  6859. ggml_cuda_set_device(g_main_device);
  6860. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6861. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6862. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6863. void * src0_ddq = src0_extra->data_device[g_main_device];
  6864. half * src0_f16 = (half *) src0_ddq;
  6865. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6866. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6867. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6868. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6869. // convert src1 to fp16
  6870. cuda_pool_alloc<half> src1_f16_alloc;
  6871. if (src1->type != GGML_TYPE_F16) {
  6872. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6873. const int64_t ne_src1 = ggml_nelements(src1);
  6874. src1_f16_alloc.alloc(ne_src1);
  6875. GGML_ASSERT(to_fp16_cuda != nullptr);
  6876. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  6877. }
  6878. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  6879. cuda_pool_alloc<half> dst_f16;
  6880. char * dst_t;
  6881. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  6882. cudaDataType_t cu_data_type = CUDA_R_16F;
  6883. // dst strides
  6884. size_t nbd2 = dst->nb[2];
  6885. size_t nbd3 = dst->nb[3];
  6886. const half alpha_f16 = 1.0f;
  6887. const half beta_f16 = 0.0f;
  6888. const float alpha_f32 = 1.0f;
  6889. const float beta_f32 = 0.0f;
  6890. const void * alpha = &alpha_f16;
  6891. const void * beta = &beta_f16;
  6892. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6893. dst_t = (char *) dst_f16.alloc(ne_dst);
  6894. nbd2 /= sizeof(float) / sizeof(half);
  6895. nbd3 /= sizeof(float) / sizeof(half);
  6896. } else {
  6897. dst_t = (char *) dst_ddf;
  6898. cu_compute_type = CUBLAS_COMPUTE_32F;
  6899. cu_data_type = CUDA_R_32F;
  6900. alpha = &alpha_f32;
  6901. beta = &beta_f32;
  6902. }
  6903. GGML_ASSERT(ne12 % ne02 == 0);
  6904. GGML_ASSERT(ne13 % ne03 == 0);
  6905. // broadcast factors
  6906. const int64_t r2 = ne12/ne02;
  6907. const int64_t r3 = ne13/ne03;
  6908. #if 0
  6909. // use cublasGemmEx
  6910. {
  6911. for (int i13 = 0; i13 < ne13; ++i13) {
  6912. for (int i12 = 0; i12 < ne12; ++i12) {
  6913. int i03 = i13 / r3;
  6914. int i02 = i12 / r2;
  6915. CUBLAS_CHECK(
  6916. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6917. ne01, ne11, ne10,
  6918. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6919. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6920. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  6921. cu_compute_type,
  6922. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6923. }
  6924. }
  6925. }
  6926. #else
  6927. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6928. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6929. // use cublasGemmStridedBatchedEx
  6930. CUBLAS_CHECK(
  6931. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6932. ne01, ne11, ne10,
  6933. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  6934. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  6935. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  6936. ne12*ne13,
  6937. cu_compute_type,
  6938. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6939. } else {
  6940. // use cublasGemmBatchedEx
  6941. const int ne23 = ne12*ne13;
  6942. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  6943. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  6944. dim3 block_dims(ne13, ne12);
  6945. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6946. src0_f16, src1_f16, dst_t,
  6947. ptrs_src.get(), ptrs_dst.get(),
  6948. ne12, ne13,
  6949. ne23,
  6950. nb02, nb03,
  6951. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  6952. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  6953. nbd2, nbd3,
  6954. r2, r3);
  6955. CUDA_CHECK(cudaGetLastError());
  6956. CUBLAS_CHECK(
  6957. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6958. ne01, ne11, ne10,
  6959. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  6960. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  6961. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  6962. ne23,
  6963. cu_compute_type,
  6964. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6965. }
  6966. #endif
  6967. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  6968. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6969. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  6970. }
  6971. }
  6972. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6973. const bool all_on_device =
  6974. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6975. (src1->backend == GGML_BACKEND_GPU) &&
  6976. ( dst->backend == GGML_BACKEND_GPU);
  6977. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6978. int64_t min_compute_capability = INT_MAX;
  6979. for (int id = 0; id < g_device_count; ++id) {
  6980. if (min_compute_capability > g_device_caps[id].cc && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6981. min_compute_capability = g_device_caps[id].cc;
  6982. }
  6983. }
  6984. #ifdef CUDA_USE_TENSOR_CORES
  6985. const bool use_tensor_cores = true;
  6986. #else
  6987. const bool use_tensor_cores = false;
  6988. #endif
  6989. // debug helpers
  6990. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6991. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6992. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6993. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6994. //printf("src0 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src0), ggml_is_transposed(src0), ggml_type_name(src0->type), src0->name);
  6995. //printf("src1 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src1), ggml_is_transposed(src1), ggml_type_name(src1->type), src1->name);
  6996. if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  6997. // KQ single-batch
  6998. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6999. } else if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  7000. // KQV single-batch
  7001. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  7002. } else if (!split && all_on_device && use_tensor_cores && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  7003. // KQ + KQV multi-batch
  7004. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  7005. } else if (src0->type == GGML_TYPE_F32) {
  7006. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7007. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  7008. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->type == GGML_TYPE_F32) {
  7009. #ifdef GGML_CUDA_FORCE_DMMV
  7010. const bool use_mul_mat_vec_q = false;
  7011. #else
  7012. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  7013. #endif // GGML_CUDA_FORCE_DMMV
  7014. if (use_mul_mat_vec_q) {
  7015. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  7016. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  7017. } else {
  7018. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  7019. }
  7020. } else {
  7021. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  7022. // when tensor cores are available, use them for large batch size
  7023. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  7024. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  7025. use_mul_mat_q = false;
  7026. }
  7027. if (use_mul_mat_q) {
  7028. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  7029. } else {
  7030. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  7031. }
  7032. }
  7033. } else {
  7034. GGML_ASSERT(false);
  7035. }
  7036. }
  7037. #if 0
  7038. template<typename ... Srcs>
  7039. static __global__ void k_compute_batched_ptrs_id(
  7040. const void ** ptrs_src, void ** ptrs_dst,
  7041. int ne12, int ne13,
  7042. int ne23,
  7043. int nb02, int nb03,
  7044. int nb12, int nb13,
  7045. int nb2, int nb3,
  7046. int r2, int r3,
  7047. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  7048. const half * src1_f16, half * dst_f16,
  7049. const int32_t * ids, const int id,
  7050. Srcs... src0s) {
  7051. int i = ids[id];
  7052. half * src0_f16;
  7053. const void * srcs_ar[] = { (const half *) src0s... };
  7054. if (src0_type == GGML_TYPE_F16) {
  7055. src0_f16 = (half *) srcs_ar[i];
  7056. } else {
  7057. src0_f16 = src0_as_f16;
  7058. if (threadIdx.x == 0 && threadIdx.y == 0) {
  7059. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  7060. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  7061. }
  7062. }
  7063. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  7064. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  7065. if (i13 >= ne13 || i12 >= ne12) {
  7066. return;
  7067. }
  7068. int i03 = i13 / r3;
  7069. int i02 = i12 / r2;
  7070. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  7071. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  7072. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  7073. }
  7074. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  7075. const struct ggml_tensor * ids = dst->src[0];
  7076. const struct ggml_tensor * src1 = dst->src[1];
  7077. const struct ggml_tensor * src00 = dst->src[2];
  7078. const int id = dst->op_params[0];
  7079. GGML_ASSERT(!ggml_is_transposed(src00));
  7080. GGML_ASSERT(!ggml_is_transposed(src1));
  7081. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  7082. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7083. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  7084. const int64_t ne01 = src00->ne[1];
  7085. const int64_t ne02 = src00->ne[2];
  7086. const int64_t ne03 = src00->ne[3];
  7087. //const int64_t nb01 = src00->nb[1];
  7088. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  7089. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  7090. const int64_t ne10 = src1->ne[0];
  7091. const int64_t ne11 = src1->ne[1];
  7092. const int64_t ne12 = src1->ne[2];
  7093. const int64_t ne13 = src1->ne[3];
  7094. //const int64_t nb11 = src1->nb[1];
  7095. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  7096. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  7097. const int64_t ne1 = ggml_nelements(src1);
  7098. const int64_t ne = ggml_nelements(dst);
  7099. ggml_cuda_set_device(g_main_device);
  7100. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7101. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  7102. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7103. //void * src0_ddq = src0_extra->data_device[g_main_device];
  7104. //half * src0_as_f16 = (half *) src0_ddq;
  7105. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7106. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7107. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7108. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7109. // convert src1 to fp16
  7110. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7111. GGML_ASSERT(to_fp16_cuda != nullptr);
  7112. size_t src1_as = 0;
  7113. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  7114. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  7115. size_t dst_as = 0;
  7116. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  7117. GGML_ASSERT(ne12 % ne02 == 0);
  7118. GGML_ASSERT(ne13 % ne03 == 0);
  7119. // broadcast factors
  7120. const int64_t r2 = ne12/ne02;
  7121. const int64_t r3 = ne13/ne03;
  7122. const half alpha_f16 = 1.0f;
  7123. const half beta_f16 = 0.0f;
  7124. // use cublasGemmBatchedEx
  7125. const int ne23 = ne12*ne13;
  7126. const void ** ptrs_src = nullptr;
  7127. void ** ptrs_dst = nullptr;
  7128. size_t ptrs_src_s = 0;
  7129. size_t ptrs_dst_s = 0;
  7130. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  7131. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  7132. int64_t src0_ne = ggml_nelements(src00);
  7133. half * src0_as_f16 = nullptr;
  7134. size_t src0_as = 0;
  7135. if (src00->type != GGML_TYPE_F16) {
  7136. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7137. }
  7138. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7139. dim3 block_dims(ne13, ne12);
  7140. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7141. ptrs_src, ptrs_dst,
  7142. ne12, ne13,
  7143. ne23,
  7144. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7145. nb12, nb13,
  7146. dst->nb[2], dst->nb[3],
  7147. r2, r3,
  7148. src00->type, src0_as_f16, src0_ne,
  7149. src1_as_f16, dst_f16,
  7150. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7151. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7152. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7153. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7154. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7155. );
  7156. CUDA_CHECK(cudaGetLastError());
  7157. CUBLAS_CHECK(
  7158. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7159. ne01, ne11, ne10,
  7160. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7161. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7162. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7163. ne23,
  7164. CUBLAS_COMPUTE_16F,
  7165. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7166. if (src0_as != 0) {
  7167. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7168. }
  7169. if (ptrs_src_s != 0) {
  7170. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7171. }
  7172. if (ptrs_dst_s != 0) {
  7173. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7174. }
  7175. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7176. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7177. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7178. ggml_cuda_pool_free(dst_f16, dst_as);
  7179. }
  7180. #endif
  7181. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7182. #if 0
  7183. ggml_cuda_mul_mat_id_cublas(dst);
  7184. // TODO: mmq/mmv support
  7185. #endif
  7186. const int64_t nb11 = src1->nb[1];
  7187. const int64_t nb1 = dst->nb[1];
  7188. const struct ggml_tensor * ids = src0;
  7189. const int32_t id = ((int32_t *) dst->op_params)[0];
  7190. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7191. std::vector<char> ids_host(ggml_nbytes(ids));
  7192. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  7193. if (ids->backend == GGML_BACKEND_GPU) {
  7194. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7195. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  7196. CUDA_CHECK(cudaStreamSynchronize(stream));
  7197. } else {
  7198. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7199. }
  7200. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7201. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7202. ggml_tensor_extra_gpu src1_row_extra;
  7203. ggml_tensor_extra_gpu dst_row_extra;
  7204. ggml_tensor src1_row = *src1;
  7205. ggml_tensor dst_row = *dst;
  7206. src1_row.backend = GGML_BACKEND_GPU;
  7207. dst_row.backend = GGML_BACKEND_GPU;
  7208. src1_row.extra = &src1_row_extra;
  7209. dst_row.extra = &dst_row_extra;
  7210. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  7211. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  7212. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  7213. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  7214. if (src1->ne[1] == 1) {
  7215. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7216. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7217. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7218. //int32_t row_id;
  7219. //CUDA_CHECK(cudaMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  7220. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7221. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7222. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7223. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7224. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  7225. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  7226. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  7227. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  7228. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7229. }
  7230. } else {
  7231. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  7232. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  7233. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  7234. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  7235. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  7236. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  7237. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  7238. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  7239. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  7240. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7241. int64_t num_src1_rows = 0;
  7242. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7243. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7244. if (row_id_i != row_id) {
  7245. continue;
  7246. }
  7247. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7248. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  7249. nb11, src1_kind, stream));
  7250. num_src1_rows++;
  7251. }
  7252. if (num_src1_rows == 0) {
  7253. continue;
  7254. }
  7255. src1_row.ne[1] = num_src1_rows;
  7256. dst_row.ne[1] = num_src1_rows;
  7257. src1_row.nb[1] = nb11;
  7258. src1_row.nb[2] = num_src1_rows*nb11;
  7259. src1_row.nb[3] = num_src1_rows*nb11;
  7260. dst_row.nb[1] = nb1;
  7261. dst_row.nb[2] = num_src1_rows*nb1;
  7262. dst_row.nb[3] = num_src1_rows*nb1;
  7263. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7264. num_src1_rows = 0;
  7265. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7266. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7267. if (row_id_i != row_id) {
  7268. continue;
  7269. }
  7270. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7271. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  7272. nb1, dst_kind, stream));
  7273. num_src1_rows++;
  7274. }
  7275. }
  7276. }
  7277. if (dst->backend == GGML_BACKEND_CPU) {
  7278. CUDA_CHECK(cudaStreamSynchronize(stream));
  7279. }
  7280. }
  7281. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7282. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7283. }
  7284. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7285. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7286. }
  7287. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7288. const int64_t ne = ggml_nelements(src0);
  7289. GGML_ASSERT(ne == ggml_nelements(src1));
  7290. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7291. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7292. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7293. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7294. const int64_t ne00 = src0->ne[0];
  7295. const int64_t ne01 = src0->ne[1];
  7296. GGML_ASSERT(src0->ne[3] == 1);
  7297. const int64_t nb00 = src0->nb[0];
  7298. const int64_t nb01 = src0->nb[1];
  7299. const int64_t nb02 = src0->nb[2];
  7300. const int64_t ne10 = src1->ne[0];
  7301. const int64_t ne11 = src1->ne[1];
  7302. GGML_ASSERT(src1->ne[3] == 1);
  7303. const int64_t nb10 = src1->nb[0];
  7304. const int64_t nb11 = src1->nb[1];
  7305. const int64_t nb12 = src1->nb[2];
  7306. ggml_cuda_set_device(g_main_device);
  7307. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7308. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7309. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7310. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7311. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7312. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7313. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7314. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7315. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7316. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7317. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7318. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7319. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7320. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7321. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7322. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7323. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7324. } else {
  7325. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7326. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7327. GGML_ASSERT(false);
  7328. }
  7329. (void) dst;
  7330. }
  7331. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7332. // TODO: why do we pass dst as src1 here?
  7333. ggml_cuda_cpy(src0, dst, nullptr);
  7334. (void) src1;
  7335. }
  7336. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7337. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7338. }
  7339. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7340. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7341. }
  7342. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7343. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7344. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7345. }
  7346. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7347. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7348. }
  7349. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7350. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7351. }
  7352. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7353. GGML_ASSERT(ggml_is_contiguous(src0));
  7354. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7355. }
  7356. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7357. GGML_ASSERT(ggml_is_contiguous(src0));
  7358. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7359. }
  7360. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7361. (void) src0;
  7362. (void) src1;
  7363. (void) dst;
  7364. }
  7365. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7366. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7367. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7368. }
  7369. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7370. const int64_t nrows = ggml_nrows(tensor);
  7371. const int64_t ne0 = tensor->ne[0];
  7372. const size_t nb1 = tensor->nb[1];
  7373. ggml_backend_type backend = tensor->backend;
  7374. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7375. memset(extra, 0, sizeof(*extra));
  7376. for (int id = 0; id < g_device_count; ++id) {
  7377. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7378. continue;
  7379. }
  7380. ggml_cuda_set_device(id);
  7381. int64_t row_low, row_high;
  7382. if (backend == GGML_BACKEND_GPU) {
  7383. row_low = 0;
  7384. row_high = nrows;
  7385. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7386. const int64_t rounding = get_row_rounding(tensor->type);
  7387. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7388. row_low -= row_low % rounding;
  7389. if (id == g_device_count - 1) {
  7390. row_high = nrows;
  7391. } else {
  7392. row_high = nrows*g_tensor_split[id + 1];
  7393. row_high -= row_high % rounding;
  7394. }
  7395. } else {
  7396. GGML_ASSERT(false);
  7397. }
  7398. if (row_low == row_high) {
  7399. continue;
  7400. }
  7401. int64_t nrows_split = row_high - row_low;
  7402. const size_t offset_split = row_low*nb1;
  7403. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7404. const size_t original_size = size;
  7405. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7406. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7407. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7408. }
  7409. char * buf;
  7410. CUDA_CHECK(cudaMalloc(&buf, size));
  7411. char * buf_host = (char *)data + offset_split;
  7412. // set padding to 0 to avoid possible NaN values
  7413. if (size > original_size) {
  7414. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7415. }
  7416. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7417. extra->data_device[id] = buf;
  7418. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7419. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7420. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7421. }
  7422. }
  7423. }
  7424. tensor->extra = extra;
  7425. }
  7426. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7427. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7428. return;
  7429. }
  7430. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7431. for (int id = 0; id < g_device_count; ++id) {
  7432. ggml_cuda_set_device(id);
  7433. if (extra->data_device[id] != nullptr) {
  7434. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7435. }
  7436. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7437. if (extra->events[id][is] != nullptr) {
  7438. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7439. }
  7440. }
  7441. }
  7442. delete extra;
  7443. }
  7444. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7445. static size_t g_temp_tensor_extra_index = 0;
  7446. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7447. if (g_temp_tensor_extras == nullptr) {
  7448. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7449. }
  7450. size_t alloc_index = g_temp_tensor_extra_index;
  7451. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7452. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7453. memset(extra, 0, sizeof(*extra));
  7454. return extra;
  7455. }
  7456. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7457. if (scratch && g_scratch_size == 0) {
  7458. return;
  7459. }
  7460. tensor->backend = GGML_BACKEND_GPU;
  7461. // recursively assign CUDA buffers until a compute tensor is found
  7462. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7463. const ggml_op src0_op = tensor->src[0]->op;
  7464. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7465. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7466. }
  7467. }
  7468. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7469. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7470. }
  7471. if (scratch && no_alloc) {
  7472. return;
  7473. }
  7474. ggml_tensor_extra_gpu * extra;
  7475. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7476. tensor->op == GGML_OP_VIEW ||
  7477. force_inplace;
  7478. const size_t size = ggml_nbytes(tensor);
  7479. ggml_cuda_set_device(g_main_device);
  7480. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7481. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7482. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7483. size_t offset = 0;
  7484. if (tensor->op == GGML_OP_VIEW) {
  7485. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7486. }
  7487. extra = ggml_cuda_alloc_temp_tensor_extra();
  7488. extra->data_device[g_main_device] = src0_ddc + offset;
  7489. } else if (tensor->op == GGML_OP_CPY) {
  7490. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7491. void * src1_ddv = src1_extra->data_device[g_main_device];
  7492. extra = ggml_cuda_alloc_temp_tensor_extra();
  7493. extra->data_device[g_main_device] = src1_ddv;
  7494. } else if (scratch) {
  7495. GGML_ASSERT(size <= g_scratch_size);
  7496. if (g_scratch_offset + size > g_scratch_size) {
  7497. g_scratch_offset = 0;
  7498. }
  7499. char * data = (char *) g_scratch_buffer;
  7500. if (data == nullptr) {
  7501. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7502. g_scratch_buffer = data;
  7503. }
  7504. extra = ggml_cuda_alloc_temp_tensor_extra();
  7505. extra->data_device[g_main_device] = data + g_scratch_offset;
  7506. g_scratch_offset += size;
  7507. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7508. } else { // allocate new buffers outside of scratch
  7509. void * data;
  7510. CUDA_CHECK(cudaMalloc(&data, size));
  7511. CUDA_CHECK(cudaMemset(data, 0, size));
  7512. extra = new ggml_tensor_extra_gpu;
  7513. memset(extra, 0, sizeof(*extra));
  7514. extra->data_device[g_main_device] = data;
  7515. }
  7516. tensor->extra = extra;
  7517. }
  7518. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7519. if (g_scratch_size == 0) {
  7520. return;
  7521. }
  7522. if (g_scratch_buffer == nullptr) {
  7523. ggml_cuda_set_device(g_main_device);
  7524. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7525. }
  7526. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7527. const bool inplace = tensor->view_src != nullptr;
  7528. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  7529. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  7530. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7531. size_t view_offset = 0;
  7532. if (tensor->op == GGML_OP_VIEW) {
  7533. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7534. }
  7535. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7536. } else {
  7537. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7538. }
  7539. tensor->extra = extra;
  7540. }
  7541. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7542. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7543. GGML_ASSERT(ggml_is_contiguous(tensor));
  7544. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7545. ggml_cuda_set_device(g_main_device);
  7546. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7547. }
  7548. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7549. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7550. }
  7551. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7552. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7553. }
  7554. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7555. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7556. }
  7557. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7558. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7559. }
  7560. void ggml_cuda_set_main_device(const int main_device) {
  7561. if (main_device >= g_device_count) {
  7562. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7563. main_device, g_device_count, g_main_device);
  7564. return;
  7565. }
  7566. if (g_main_device != main_device && g_device_count > 1) {
  7567. g_main_device = main_device;
  7568. cudaDeviceProp prop;
  7569. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7570. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7571. }
  7572. }
  7573. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7574. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7575. // it still won't always work as expected, but it's better than nothing
  7576. if (scratch_size > g_scratch_size) {
  7577. ggml_cuda_free_scratch();
  7578. }
  7579. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7580. }
  7581. void ggml_cuda_free_scratch() {
  7582. if (g_scratch_buffer == nullptr) {
  7583. return;
  7584. }
  7585. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7586. g_scratch_buffer = nullptr;
  7587. }
  7588. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7589. if (!g_cublas_loaded) return false;
  7590. ggml_cuda_func_t func;
  7591. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7592. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7593. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7594. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  7595. return false;
  7596. }
  7597. if (tensor->op == GGML_OP_MUL_MAT) {
  7598. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7599. #ifndef NDEBUG
  7600. fprintf(stderr, "%s: cannot compute %s: src0->ne[3] = %" PRId64 ", src1->ne[3] = %" PRId64 " - fallback to CPU\n", __func__, tensor->name, tensor->src[0]->ne[3], tensor->src[1]->ne[3]);
  7601. #endif
  7602. return false;
  7603. }
  7604. }
  7605. switch (tensor->op) {
  7606. case GGML_OP_REPEAT:
  7607. func = ggml_cuda_repeat;
  7608. break;
  7609. case GGML_OP_GET_ROWS:
  7610. func = ggml_cuda_get_rows;
  7611. break;
  7612. case GGML_OP_DUP:
  7613. func = ggml_cuda_dup;
  7614. break;
  7615. case GGML_OP_ADD:
  7616. func = ggml_cuda_add;
  7617. break;
  7618. case GGML_OP_ACC:
  7619. func = ggml_cuda_acc;
  7620. break;
  7621. case GGML_OP_MUL:
  7622. func = ggml_cuda_mul;
  7623. break;
  7624. case GGML_OP_DIV:
  7625. func = ggml_cuda_div;
  7626. break;
  7627. case GGML_OP_UNARY:
  7628. switch (ggml_get_unary_op(tensor)) {
  7629. case GGML_UNARY_OP_GELU:
  7630. func = ggml_cuda_gelu;
  7631. break;
  7632. case GGML_UNARY_OP_SILU:
  7633. func = ggml_cuda_silu;
  7634. break;
  7635. case GGML_UNARY_OP_GELU_QUICK:
  7636. func = ggml_cuda_gelu_quick;
  7637. break;
  7638. case GGML_UNARY_OP_TANH:
  7639. func = ggml_cuda_tanh;
  7640. break;
  7641. case GGML_UNARY_OP_RELU:
  7642. func = ggml_cuda_relu;
  7643. break;
  7644. default:
  7645. return false;
  7646. }
  7647. break;
  7648. case GGML_OP_NORM:
  7649. func = ggml_cuda_norm;
  7650. break;
  7651. case GGML_OP_GROUP_NORM:
  7652. func = ggml_cuda_group_norm;
  7653. break;
  7654. case GGML_OP_CONCAT:
  7655. func = ggml_cuda_concat;
  7656. break;
  7657. case GGML_OP_UPSCALE:
  7658. func = ggml_cuda_upscale;
  7659. break;
  7660. case GGML_OP_PAD:
  7661. func = ggml_cuda_pad;
  7662. break;
  7663. case GGML_OP_LEAKY_RELU:
  7664. func = ggml_cuda_leaky_relu;
  7665. break;
  7666. case GGML_OP_RMS_NORM:
  7667. func = ggml_cuda_rms_norm;
  7668. break;
  7669. case GGML_OP_MUL_MAT:
  7670. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7671. return false;
  7672. }
  7673. func = ggml_cuda_mul_mat;
  7674. break;
  7675. case GGML_OP_MUL_MAT_ID:
  7676. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7677. return false;
  7678. }
  7679. func = ggml_cuda_mul_mat_id;
  7680. break;
  7681. case GGML_OP_SCALE:
  7682. func = ggml_cuda_scale;
  7683. break;
  7684. case GGML_OP_SQR:
  7685. func = ggml_cuda_sqr;
  7686. break;
  7687. case GGML_OP_CLAMP:
  7688. func = ggml_cuda_clamp;
  7689. break;
  7690. case GGML_OP_CPY:
  7691. func = ggml_cuda_cpy;
  7692. break;
  7693. case GGML_OP_CONT:
  7694. func = ggml_cuda_dup;
  7695. break;
  7696. case GGML_OP_NONE:
  7697. case GGML_OP_RESHAPE:
  7698. case GGML_OP_VIEW:
  7699. case GGML_OP_PERMUTE:
  7700. case GGML_OP_TRANSPOSE:
  7701. func = ggml_cuda_nop;
  7702. break;
  7703. case GGML_OP_DIAG_MASK_INF:
  7704. func = ggml_cuda_diag_mask_inf;
  7705. break;
  7706. case GGML_OP_SOFT_MAX:
  7707. func = ggml_cuda_soft_max;
  7708. break;
  7709. case GGML_OP_ROPE:
  7710. func = ggml_cuda_rope;
  7711. break;
  7712. case GGML_OP_ALIBI:
  7713. func = ggml_cuda_alibi;
  7714. break;
  7715. case GGML_OP_IM2COL:
  7716. func = ggml_cuda_im2col;
  7717. break;
  7718. case GGML_OP_SUM_ROWS:
  7719. func = ggml_cuda_sum_rows;
  7720. break;
  7721. case GGML_OP_ARGSORT:
  7722. func = ggml_cuda_argsort;
  7723. break;
  7724. default:
  7725. return false;
  7726. }
  7727. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  7728. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  7729. }
  7730. if (params->ith != 0) {
  7731. return true;
  7732. }
  7733. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7734. return true;
  7735. }
  7736. func(tensor->src[0], tensor->src[1], tensor);
  7737. return true;
  7738. }
  7739. int ggml_cuda_get_device_count() {
  7740. int device_count;
  7741. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7742. return 0;
  7743. }
  7744. return device_count;
  7745. }
  7746. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7747. cudaDeviceProp prop;
  7748. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7749. snprintf(description, description_size, "%s", prop.name);
  7750. }
  7751. ////////////////////////////////////////////////////////////////////////////////
  7752. // backend interface
  7753. #define UNUSED GGML_UNUSED
  7754. // cuda buffer
  7755. struct ggml_backend_buffer_context_cuda {
  7756. int device;
  7757. void * dev_ptr = nullptr;
  7758. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7759. size_t temp_tensor_extra_index = 0;
  7760. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7761. ~ggml_backend_buffer_context_cuda() {
  7762. delete[] temp_tensor_extras;
  7763. }
  7764. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7765. if (temp_tensor_extras == nullptr) {
  7766. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7767. }
  7768. size_t alloc_index = temp_tensor_extra_index;
  7769. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7770. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7771. memset(extra, 0, sizeof(*extra));
  7772. return extra;
  7773. }
  7774. };
  7775. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7776. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7777. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7778. delete ctx;
  7779. }
  7780. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7781. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7782. return ctx->dev_ptr;
  7783. }
  7784. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7785. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7786. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7787. assert(tensor->view_src->buffer->buft == buffer->buft);
  7788. tensor->backend = tensor->view_src->backend;
  7789. tensor->extra = tensor->view_src->extra;
  7790. return;
  7791. }
  7792. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7793. extra->data_device[ctx->device] = tensor->data;
  7794. tensor->backend = GGML_BACKEND_GPU;
  7795. tensor->extra = extra;
  7796. if (ggml_is_quantized(tensor->type)) {
  7797. // initialize padding to 0 to avoid possible NaN values
  7798. int64_t row_low = 0;
  7799. int64_t row_high = ggml_nrows(tensor);
  7800. int64_t nrows_split = row_high - row_low;
  7801. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7802. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7803. if (padded_size > original_size && tensor->view_src == nullptr) {
  7804. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7805. }
  7806. }
  7807. UNUSED(buffer);
  7808. }
  7809. static void ggml_backend_cuda_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7810. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7811. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7812. ggml_cuda_set_device(ctx->device);
  7813. CUDA_CHECK(cudaDeviceSynchronize());
  7814. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7815. }
  7816. static void ggml_backend_cuda_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7817. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7818. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7819. ggml_cuda_set_device(ctx->device);
  7820. CUDA_CHECK(cudaDeviceSynchronize());
  7821. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7822. }
  7823. static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  7824. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7825. ggml_cuda_set_device(ctx->device);
  7826. CUDA_CHECK(cudaDeviceSynchronize());
  7827. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  7828. }
  7829. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7830. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7831. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7832. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7833. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7834. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7835. /* .cpy_tensor_from = */ NULL,
  7836. /* .cpy_tensor_to = */ NULL,
  7837. /* .clear = */ ggml_backend_cuda_buffer_clear,
  7838. };
  7839. // cuda buffer type
  7840. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7841. int device = (int) (intptr_t) buft->context;
  7842. ggml_cuda_set_device(device);
  7843. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7844. void * dev_ptr;
  7845. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7846. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7847. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7848. }
  7849. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7850. return 128;
  7851. UNUSED(buft);
  7852. }
  7853. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7854. int64_t row_low = 0;
  7855. int64_t row_high = ggml_nrows(tensor);
  7856. int64_t nrows_split = row_high - row_low;
  7857. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7858. int64_t ne0 = tensor->ne[0];
  7859. if (ggml_is_quantized(tensor->type)) {
  7860. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7861. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7862. }
  7863. }
  7864. return size;
  7865. UNUSED(buft);
  7866. }
  7867. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7868. return ggml_backend_is_cuda(backend);
  7869. UNUSED(buft);
  7870. }
  7871. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  7872. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7873. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7874. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7875. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7876. /* .is_host = */ nullptr,
  7877. };
  7878. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7879. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  7880. static bool ggml_backend_cuda_buffer_type_initialized = false;
  7881. if (!ggml_backend_cuda_buffer_type_initialized) {
  7882. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7883. ggml_backend_cuda_buffer_types[i] = {
  7884. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  7885. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7886. };
  7887. }
  7888. ggml_backend_cuda_buffer_type_initialized = true;
  7889. }
  7890. return &ggml_backend_cuda_buffer_types[device];
  7891. }
  7892. // host buffer type
  7893. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7894. ggml_cuda_host_free(buffer->context);
  7895. }
  7896. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7897. void * ptr = ggml_cuda_host_malloc(size);
  7898. if (ptr == nullptr) {
  7899. // fallback to cpu buffer
  7900. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  7901. }
  7902. // FIXME: this is a hack to avoid having to implement a new buffer type
  7903. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7904. buffer->buft = buft;
  7905. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7906. return buffer;
  7907. }
  7908. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7909. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  7910. /* .iface = */ {
  7911. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7912. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7913. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7914. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7915. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  7916. },
  7917. /* .context = */ nullptr,
  7918. };
  7919. return &ggml_backend_cuda_buffer_type_host;
  7920. }
  7921. // backend
  7922. struct ggml_backend_context_cuda {
  7923. int device;
  7924. };
  7925. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7926. return GGML_CUDA_NAME;
  7927. UNUSED(backend);
  7928. }
  7929. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7930. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7931. delete cuda_ctx;
  7932. delete backend;
  7933. }
  7934. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7935. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7936. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7937. }
  7938. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7939. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7940. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7941. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7942. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7943. }
  7944. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7945. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7946. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7947. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7948. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7949. }
  7950. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7951. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7952. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7953. UNUSED(backend);
  7954. }
  7955. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7956. GGML_ASSERT(!"not implemented");
  7957. return nullptr;
  7958. UNUSED(backend);
  7959. UNUSED(cgraph);
  7960. }
  7961. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7962. GGML_ASSERT(!"not implemented");
  7963. UNUSED(backend);
  7964. UNUSED(plan);
  7965. }
  7966. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7967. GGML_ASSERT(!"not implemented");
  7968. UNUSED(backend);
  7969. UNUSED(plan);
  7970. }
  7971. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7972. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7973. ggml_cuda_set_main_device(cuda_ctx->device);
  7974. ggml_compute_params params = {};
  7975. params.type = GGML_TASK_COMPUTE;
  7976. params.ith = 0;
  7977. for (int i = 0; i < cgraph->n_nodes; i++) {
  7978. ggml_tensor * node = cgraph->nodes[i];
  7979. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7980. continue;
  7981. assert(node->backend == GGML_BACKEND_GPU);
  7982. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7983. assert(node->extra != nullptr);
  7984. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7985. if (node->src[j] != nullptr) {
  7986. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7987. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7988. assert(node->src[j]->extra != nullptr);
  7989. }
  7990. }
  7991. bool ok = ggml_cuda_compute_forward(&params, node);
  7992. if (!ok) {
  7993. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7994. }
  7995. GGML_ASSERT(ok);
  7996. #if 0
  7997. if (node->type == GGML_TYPE_F32) {
  7998. cudaDeviceSynchronize();
  7999. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  8000. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  8001. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  8002. ggml_type_name(node->src[0]->type),
  8003. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  8004. node->src[0]->name,
  8005. node->src[1] ? node->src[1]->name : "none");
  8006. double sum = 0.0;
  8007. double sq_sum = 0.0;
  8008. for (int i = 0; i < ggml_nelements(node); i++) {
  8009. printf("%f ", tmp[i]);
  8010. sum += tmp[i];
  8011. sq_sum += tmp[i]*tmp[i];
  8012. }
  8013. printf("\n");
  8014. printf("sum: %f, ", sum);
  8015. printf("sq_sum: %f\n", sq_sum);
  8016. }
  8017. #endif
  8018. }
  8019. UNUSED(backend);
  8020. }
  8021. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  8022. switch (op->op) {
  8023. case GGML_OP_UNARY:
  8024. switch (ggml_get_unary_op(op)) {
  8025. case GGML_UNARY_OP_GELU:
  8026. case GGML_UNARY_OP_SILU:
  8027. case GGML_UNARY_OP_RELU:
  8028. case GGML_UNARY_OP_GELU_QUICK:
  8029. case GGML_UNARY_OP_TANH:
  8030. return true;
  8031. default:
  8032. return false;
  8033. }
  8034. break;
  8035. case GGML_OP_MUL_MAT:
  8036. case GGML_OP_MUL_MAT_ID:
  8037. {
  8038. struct ggml_tensor * a;
  8039. struct ggml_tensor * b;
  8040. if (op->op == GGML_OP_MUL_MAT) {
  8041. a = op->src[0];
  8042. b = op->src[1];
  8043. } else {
  8044. a = op->src[2];
  8045. b = op->src[1];
  8046. }
  8047. if (a->ne[3] != b->ne[3]) {
  8048. return false;
  8049. }
  8050. return true;
  8051. } break;
  8052. case GGML_OP_GET_ROWS:
  8053. {
  8054. switch (op->src[0]->type) {
  8055. case GGML_TYPE_F16:
  8056. case GGML_TYPE_F32:
  8057. case GGML_TYPE_Q4_0:
  8058. case GGML_TYPE_Q4_1:
  8059. case GGML_TYPE_Q5_0:
  8060. case GGML_TYPE_Q5_1:
  8061. case GGML_TYPE_Q8_0:
  8062. return true;
  8063. default:
  8064. return false;
  8065. }
  8066. } break;
  8067. case GGML_OP_CPY:
  8068. {
  8069. ggml_type src0_type = op->src[0]->type;
  8070. ggml_type src1_type = op->src[1]->type;
  8071. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  8072. return true;
  8073. }
  8074. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  8075. return true;
  8076. }
  8077. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  8078. return true;
  8079. }
  8080. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  8081. return true;
  8082. }
  8083. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  8084. return true;
  8085. }
  8086. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  8087. return true;
  8088. }
  8089. return false;
  8090. } break;
  8091. case GGML_OP_NONE:
  8092. case GGML_OP_RESHAPE:
  8093. case GGML_OP_VIEW:
  8094. case GGML_OP_PERMUTE:
  8095. case GGML_OP_TRANSPOSE:
  8096. case GGML_OP_NORM:
  8097. case GGML_OP_REPEAT:
  8098. case GGML_OP_DUP:
  8099. case GGML_OP_ADD:
  8100. case GGML_OP_MUL:
  8101. case GGML_OP_DIV:
  8102. case GGML_OP_RMS_NORM:
  8103. case GGML_OP_SCALE:
  8104. case GGML_OP_SQR:
  8105. case GGML_OP_CLAMP:
  8106. case GGML_OP_CONT:
  8107. case GGML_OP_DIAG_MASK_INF:
  8108. case GGML_OP_SOFT_MAX:
  8109. case GGML_OP_ROPE:
  8110. case GGML_OP_ALIBI:
  8111. case GGML_OP_IM2COL:
  8112. case GGML_OP_SUM_ROWS:
  8113. case GGML_OP_ARGSORT:
  8114. case GGML_OP_ACC:
  8115. case GGML_OP_CONCAT:
  8116. case GGML_OP_GROUP_NORM:
  8117. case GGML_OP_UPSCALE:
  8118. case GGML_OP_PAD:
  8119. case GGML_OP_LEAKY_RELU:
  8120. return true;
  8121. default:
  8122. return false;
  8123. }
  8124. UNUSED(backend);
  8125. }
  8126. static ggml_backend_i cuda_backend_i = {
  8127. /* .get_name = */ ggml_backend_cuda_name,
  8128. /* .free = */ ggml_backend_cuda_free,
  8129. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  8130. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  8131. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  8132. /* .cpy_tensor_from_async = */ NULL,
  8133. /* .cpy_tensor_to_async = */ NULL,
  8134. /* .synchronize = */ ggml_backend_cuda_synchronize,
  8135. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  8136. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  8137. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  8138. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  8139. /* .supports_op = */ ggml_backend_cuda_supports_op,
  8140. };
  8141. ggml_backend_t ggml_backend_cuda_init(int device) {
  8142. ggml_init_cublas(); // TODO: remove from ggml.c
  8143. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  8144. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  8145. return nullptr;
  8146. }
  8147. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  8148. ggml_cuda_set_main_device(device);
  8149. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  8150. /* .device = */ device
  8151. };
  8152. ggml_backend_t cuda_backend = new ggml_backend {
  8153. /* .interface = */ cuda_backend_i,
  8154. /* .context = */ ctx
  8155. };
  8156. return cuda_backend;
  8157. }
  8158. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  8159. return backend->iface.get_name == ggml_backend_cuda_name;
  8160. }
  8161. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  8162. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  8163. return cuda_backend;
  8164. UNUSED(params);
  8165. }
  8166. extern "C" int ggml_backend_cuda_reg_devices();
  8167. int ggml_backend_cuda_reg_devices() {
  8168. int device_count = ggml_cuda_get_device_count();
  8169. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  8170. for (int i = 0; i < device_count; i++) {
  8171. char name[128];
  8172. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  8173. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  8174. }
  8175. return device_count;
  8176. }