ggml-cuda.cu 269 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297
  1. #include <cstddef>
  2. #include <cstdint>
  3. #include <limits>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <atomic>
  7. #include <assert.h>
  8. #if defined(GGML_USE_HIPBLAS)
  9. #include <hip/hip_runtime.h>
  10. #include <hipblas/hipblas.h>
  11. #include <hip/hip_fp16.h>
  12. #ifdef __HIP_PLATFORM_AMD__
  13. // for rocblas_initialize()
  14. #include "rocblas/rocblas.h"
  15. #endif // __HIP_PLATFORM_AMD__
  16. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  17. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  18. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  19. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  20. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  21. #define CUBLAS_OP_N HIPBLAS_OP_N
  22. #define CUBLAS_OP_T HIPBLAS_OP_T
  23. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  24. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  25. #define CUDA_R_16F HIPBLAS_R_16F
  26. #define CUDA_R_32F HIPBLAS_R_32F
  27. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  28. #define cublasCreate hipblasCreate
  29. #define cublasGemmEx hipblasGemmEx
  30. #define cublasHandle_t hipblasHandle_t
  31. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  32. #define cublasSetStream hipblasSetStream
  33. #define cublasSgemm hipblasSgemm
  34. #define cublasStatus_t hipblasStatus_t
  35. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  36. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  37. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  38. #define cudaDeviceProp hipDeviceProp_t
  39. #define cudaDeviceSynchronize hipDeviceSynchronize
  40. #define cudaError_t hipError_t
  41. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  42. #define cudaEventDisableTiming hipEventDisableTiming
  43. #define cudaEventRecord hipEventRecord
  44. #define cudaEvent_t hipEvent_t
  45. #define cudaEventDestroy hipEventDestroy
  46. #define cudaFree hipFree
  47. #define cudaFreeHost hipHostFree
  48. #define cudaGetDevice hipGetDevice
  49. #define cudaGetDeviceCount hipGetDeviceCount
  50. #define cudaGetDeviceProperties hipGetDeviceProperties
  51. #define cudaGetErrorString hipGetErrorString
  52. #define cudaGetLastError hipGetLastError
  53. #define cudaMalloc hipMalloc
  54. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  55. #define cudaMemcpy hipMemcpy
  56. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  57. #define cudaMemcpyAsync hipMemcpyAsync
  58. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  59. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  60. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  61. #define cudaMemcpyKind hipMemcpyKind
  62. #define cudaMemset hipMemset
  63. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  64. #define cudaSetDevice hipSetDevice
  65. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  66. #define cudaStreamNonBlocking hipStreamNonBlocking
  67. #define cudaStreamSynchronize hipStreamSynchronize
  68. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  69. #define cudaStream_t hipStream_t
  70. #define cudaSuccess hipSuccess
  71. #else
  72. #include <cuda_runtime.h>
  73. #include <cublas_v2.h>
  74. #include <cuda_fp16.h>
  75. #endif // defined(GGML_USE_HIPBLAS)
  76. #include "ggml-cuda.h"
  77. #include "ggml.h"
  78. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  79. #define CC_TURING 700
  80. #define CC_OFFSET_AMD 1000000
  81. #define CC_RDNA2 CC_OFFSET_AMD + 1030
  82. #if defined(GGML_USE_HIPBLAS)
  83. #define __CUDA_ARCH__ 1300
  84. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  85. defined(__gfx1150__) || defined(__gfx1151__)
  86. #define RDNA3
  87. #endif
  88. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  89. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  90. #define RDNA2
  91. #endif
  92. #ifndef __has_builtin
  93. #define __has_builtin(x) 0
  94. #endif
  95. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  96. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  97. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  98. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  99. #if __has_builtin(__builtin_elementwise_sub_sat)
  100. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  101. return reinterpret_cast<const int&>(c);
  102. #else
  103. int8x4_t c;
  104. int16_t tmp;
  105. #pragma unroll
  106. for (int i = 0; i < 4; i++) {
  107. tmp = va[i] - vb[i];
  108. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  109. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  110. c[i] = tmp;
  111. }
  112. return reinterpret_cast<int&>(c);
  113. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  114. }
  115. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  116. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  117. c = __builtin_amdgcn_sdot4(a, b, c, false);
  118. #elif defined(__gfx1100__)
  119. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  120. #elif defined(__gfx1010__) || defined(__gfx900__)
  121. int tmp1;
  122. int tmp2;
  123. asm("\n \
  124. 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 \
  125. 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 \
  126. v_add3_u32 %0, %1, %2, %0 \n \
  127. 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 \
  128. 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 \
  129. v_add3_u32 %0, %1, %2, %0 \n \
  130. "
  131. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  132. : "v"(a), "v"(b)
  133. );
  134. #else
  135. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  136. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  137. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  138. #endif
  139. return c;
  140. }
  141. #endif // defined(GGML_USE_HIPBLAS)
  142. #if defined(_MSC_VER)
  143. #pragma warning(disable: 4244 4267) // possible loss of data
  144. #endif
  145. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  146. #define CUDA_CHECK(err) \
  147. do { \
  148. cudaError_t err_ = (err); \
  149. if (err_ != cudaSuccess) { \
  150. int id; \
  151. cudaGetDevice(&id); \
  152. fprintf(stderr, "\nCUDA error %d at %s:%d: %s\n", err_, __FILE__, __LINE__, \
  153. cudaGetErrorString(err_)); \
  154. fprintf(stderr, "current device: %d\n", id); \
  155. exit(1); \
  156. } \
  157. } while (0)
  158. #if CUDART_VERSION >= 12000
  159. #define CUBLAS_CHECK(err) \
  160. do { \
  161. cublasStatus_t err_ = (err); \
  162. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  163. int id; \
  164. cudaGetDevice(&id); \
  165. fprintf(stderr, "\ncuBLAS error %d at %s:%d: %s\n", \
  166. err_, __FILE__, __LINE__, cublasGetStatusString(err_)); \
  167. fprintf(stderr, "current device: %d\n", id); \
  168. exit(1); \
  169. } \
  170. } while (0)
  171. #else
  172. #define CUBLAS_CHECK(err) \
  173. do { \
  174. cublasStatus_t err_ = (err); \
  175. if (err_ != CUBLAS_STATUS_SUCCESS) { \
  176. int id; \
  177. cudaGetDevice(&id); \
  178. fprintf(stderr, "\ncuBLAS error %d at %s:%d\n", err_, __FILE__, __LINE__); \
  179. fprintf(stderr, "current device: %d\n", id); \
  180. exit(1); \
  181. } \
  182. } while (0)
  183. #endif // CUDART_VERSION >= 11
  184. #if CUDART_VERSION >= 11100
  185. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  186. #else
  187. #define GGML_CUDA_ASSUME(x)
  188. #endif // CUDART_VERSION >= 11100
  189. #ifdef GGML_CUDA_F16
  190. typedef half dfloat; // dequantize float
  191. typedef half2 dfloat2;
  192. #else
  193. typedef float dfloat; // dequantize float
  194. typedef float2 dfloat2;
  195. #endif //GGML_CUDA_F16
  196. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  197. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  198. int x32 = 0;
  199. x32 |= x16[0] << 0;
  200. x32 |= x16[1] << 16;
  201. return x32;
  202. }
  203. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  204. const uint16_t * x16 = (uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  205. int x32 = 0;
  206. x32 |= x16[0] << 0;
  207. x32 |= x16[1] << 16;
  208. return x32;
  209. }
  210. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  211. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  212. }
  213. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  214. return *((int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  215. }
  216. template<typename T>
  217. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  218. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  219. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  220. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  221. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  222. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  223. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  224. typedef void (*ggml_cuda_op_mul_mat_t)(
  225. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  226. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  227. const int64_t src1_padded_row_size, const cudaStream_t & stream);
  228. typedef void (*ggml_cuda_op_flatten_t)(
  229. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  230. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream);
  231. // QK = number of values after dequantization
  232. // QR = QK / number of values before dequantization
  233. // QI = number of 32 bit integers before dequantization
  234. #define QK4_0 32
  235. #define QR4_0 2
  236. #define QI4_0 (QK4_0 / (4 * QR4_0))
  237. typedef struct {
  238. half d; // delta
  239. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  240. } block_q4_0;
  241. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  242. #define QK4_1 32
  243. #define QR4_1 2
  244. #define QI4_1 (QK4_1 / (4 * QR4_1))
  245. typedef struct {
  246. half2 dm; // dm.x = delta, dm.y = min
  247. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  248. } block_q4_1;
  249. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  250. #define QK5_0 32
  251. #define QR5_0 2
  252. #define QI5_0 (QK5_0 / (4 * QR5_0))
  253. typedef struct {
  254. half d; // delta
  255. uint8_t qh[4]; // 5-th bit of quants
  256. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  257. } block_q5_0;
  258. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  259. #define QK5_1 32
  260. #define QR5_1 2
  261. #define QI5_1 (QK5_1 / (4 * QR5_1))
  262. typedef struct {
  263. half2 dm; // dm.x = delta, dm.y = min
  264. uint8_t qh[4]; // 5-th bit of quants
  265. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  266. } block_q5_1;
  267. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  268. #define QK8_0 32
  269. #define QR8_0 1
  270. #define QI8_0 (QK8_0 / (4 * QR8_0))
  271. typedef struct {
  272. half d; // delta
  273. int8_t qs[QK8_0]; // quants
  274. } block_q8_0;
  275. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  276. #define QK8_1 32
  277. #define QR8_1 1
  278. #define QI8_1 (QK8_1 / (4 * QR8_1))
  279. typedef struct {
  280. half2 ds; // ds.x = delta, ds.y = sum
  281. int8_t qs[QK8_0]; // quants
  282. } block_q8_1;
  283. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  284. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  285. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  286. typedef void (*load_tiles_cuda_t)(
  287. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  288. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  289. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  290. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  291. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  292. //================================= k-quants
  293. #ifdef GGML_QKK_64
  294. #define QK_K 64
  295. #define K_SCALE_SIZE 4
  296. #else
  297. #define QK_K 256
  298. #define K_SCALE_SIZE 12
  299. #endif
  300. #define QR2_K 4
  301. #define QI2_K (QK_K / (4*QR2_K))
  302. typedef struct {
  303. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  304. uint8_t qs[QK_K/4]; // quants
  305. half2 dm; // super-block scale for quantized scales/mins
  306. } block_q2_K;
  307. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  308. #define QR3_K 4
  309. #define QI3_K (QK_K / (4*QR3_K))
  310. typedef struct {
  311. uint8_t hmask[QK_K/8]; // quants - high bit
  312. uint8_t qs[QK_K/4]; // quants - low 2 bits
  313. #ifdef GGML_QKK_64
  314. uint8_t scales[2]; // scales, quantized with 8 bits
  315. #else
  316. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  317. #endif
  318. half d; // super-block scale
  319. } block_q3_K;
  320. //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");
  321. #define QR4_K 2
  322. #define QI4_K (QK_K / (4*QR4_K))
  323. #ifdef GGML_QKK_64
  324. typedef struct {
  325. half dm[2]; // super-block scales/mins
  326. uint8_t scales[2]; // 4-bit block scales/mins
  327. uint8_t qs[QK_K/2]; // 4--bit quants
  328. } block_q4_K;
  329. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  330. #else
  331. typedef struct {
  332. half2 dm; // super-block scale for quantized scales/mins
  333. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  334. uint8_t qs[QK_K/2]; // 4--bit quants
  335. } block_q4_K;
  336. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  337. #endif
  338. #define QR5_K 2
  339. #define QI5_K (QK_K / (4*QR5_K))
  340. #ifdef GGML_QKK_64
  341. typedef struct {
  342. half d; // super-block scale
  343. int8_t scales[QK_K/16]; // block scales
  344. uint8_t qh[QK_K/8]; // quants, high bit
  345. uint8_t qs[QK_K/2]; // quants, low 4 bits
  346. } block_q5_K;
  347. 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");
  348. #else
  349. typedef struct {
  350. half2 dm; // super-block scale for quantized scales/mins
  351. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  352. uint8_t qh[QK_K/8]; // quants, high bit
  353. uint8_t qs[QK_K/2]; // quants, low 4 bits
  354. } block_q5_K;
  355. 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");
  356. #endif
  357. #define QR6_K 2
  358. #define QI6_K (QK_K / (4*QR6_K))
  359. typedef struct {
  360. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  361. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  362. int8_t scales[QK_K/16]; // scales
  363. half d; // delta
  364. } block_q6_K;
  365. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  366. #define WARP_SIZE 32
  367. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  368. #define CUDA_ADD_BLOCK_SIZE 256
  369. #define CUDA_MUL_BLOCK_SIZE 256
  370. #define CUDA_GELU_BLOCK_SIZE 256
  371. #define CUDA_SILU_BLOCK_SIZE 256
  372. #define CUDA_CPY_BLOCK_SIZE 32
  373. #define CUDA_SCALE_BLOCK_SIZE 256
  374. #define CUDA_ROPE_BLOCK_SIZE 256
  375. #define CUDA_ALIBI_BLOCK_SIZE 32
  376. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  377. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  378. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  379. // dmmv = dequantize_mul_mat_vec
  380. #ifndef GGML_CUDA_DMMV_X
  381. #define GGML_CUDA_DMMV_X 32
  382. #endif
  383. #ifndef GGML_CUDA_MMV_Y
  384. #define GGML_CUDA_MMV_Y 1
  385. #endif
  386. #ifndef K_QUANTS_PER_ITERATION
  387. #define K_QUANTS_PER_ITERATION 2
  388. #else
  389. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  390. #endif
  391. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  392. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  393. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  394. #define MUL_MAT_SRC1_COL_STRIDE 128
  395. #define MAX_STREAMS 8
  396. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { nullptr };
  397. struct ggml_tensor_extra_gpu {
  398. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  399. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  400. };
  401. // this is faster on Windows
  402. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  403. inline cudaError_t ggml_cuda_set_device(const int device) {
  404. int current_device;
  405. CUDA_CHECK(cudaGetDevice(&current_device));
  406. if (device == current_device) {
  407. return cudaSuccess;
  408. }
  409. return cudaSetDevice(device);
  410. }
  411. static int g_device_count = -1;
  412. static int g_main_device = 0;
  413. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  414. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  415. static bool g_mul_mat_q = true;
  416. static void * g_scratch_buffer = nullptr;
  417. static size_t g_scratch_size = 1024*1024*1024; // 1 GB by default
  418. static size_t g_scratch_offset = 0;
  419. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  420. static __global__ void add_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  421. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  422. if (i >= kx) {
  423. return;
  424. }
  425. dst[i] = x[i] + y[i%ky];
  426. }
  427. static __global__ void add_f16_f32_f16(const half * x, const float * y, half * dst, const int k) {
  428. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  429. if (i >= k) {
  430. return;
  431. }
  432. dst[i] = __hadd(x[i], __float2half(y[i]));
  433. }
  434. static __global__ void mul_f32(const float * x, const float * y, float * dst, const int kx, const int ky) {
  435. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  436. if (i >= kx) {
  437. return;
  438. }
  439. dst[i] = x[i] * y[i%ky];
  440. }
  441. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  442. const float GELU_COEF_A = 0.044715f;
  443. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  444. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  445. if (i >= k) {
  446. return;
  447. }
  448. float xi = x[i];
  449. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  450. }
  451. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  452. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  453. if (i >= k) {
  454. return;
  455. }
  456. dst[i] = x[i] / (1.0f + expf(-x[i]));
  457. }
  458. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  459. #pragma unroll
  460. for (int mask = 16; mask > 0; mask >>= 1) {
  461. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  462. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  463. }
  464. return a;
  465. }
  466. template <int block_size>
  467. static __global__ void norm_f32(const float * x, float * dst, const int ncols) {
  468. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  469. const int tid = threadIdx.x;
  470. const float eps = 1e-5f;
  471. float2 mean_var = make_float2(0.f, 0.f);
  472. for (int col = tid; col < ncols; col += block_size) {
  473. const float xi = x[row*ncols + col];
  474. mean_var.x += xi;
  475. mean_var.y += xi * xi;
  476. }
  477. // sum up partial sums
  478. mean_var = warp_reduce_sum(mean_var);
  479. if (block_size > WARP_SIZE) {
  480. __shared__ float2 s_sum[32];
  481. int warp_id = threadIdx.x / WARP_SIZE;
  482. int lane_id = threadIdx.x % WARP_SIZE;
  483. if (lane_id == 0) {
  484. s_sum[warp_id] = mean_var;
  485. }
  486. __syncthreads();
  487. mean_var = s_sum[lane_id];
  488. mean_var = warp_reduce_sum(mean_var);
  489. }
  490. const float mean = mean_var.x / ncols;
  491. const float var = mean_var.y / ncols - mean * mean;
  492. const float inv_std = rsqrtf(var + eps);
  493. for (int col = tid; col < ncols; col += block_size) {
  494. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  495. }
  496. }
  497. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  498. #pragma unroll
  499. for (int mask = 16; mask > 0; mask >>= 1) {
  500. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  501. }
  502. return x;
  503. }
  504. template <int block_size>
  505. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  506. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  507. const int tid = threadIdx.x;
  508. float tmp = 0.0f; // partial sum for thread in warp
  509. for (int col = tid; col < ncols; col += block_size) {
  510. const float xi = x[row*ncols + col];
  511. tmp += xi * xi;
  512. }
  513. // sum up partial sums
  514. tmp = warp_reduce_sum(tmp);
  515. if (block_size > WARP_SIZE) {
  516. __shared__ float s_sum[32];
  517. int warp_id = threadIdx.x / WARP_SIZE;
  518. int lane_id = threadIdx.x % WARP_SIZE;
  519. if (lane_id == 0) {
  520. s_sum[warp_id] = tmp;
  521. }
  522. __syncthreads();
  523. tmp = s_sum[lane_id];
  524. tmp = warp_reduce_sum(tmp);
  525. }
  526. const float mean = tmp / ncols;
  527. const float scale = rsqrtf(mean + eps);
  528. for (int col = tid; col < ncols; col += block_size) {
  529. dst[row*ncols + col] = scale * x[row*ncols + col];
  530. }
  531. }
  532. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  533. const block_q4_0 * x = (const block_q4_0 *) vx;
  534. const dfloat d = x[ib].d;
  535. const int vui = x[ib].qs[iqs];
  536. v.x = vui & 0xF;
  537. v.y = vui >> 4;
  538. #ifdef GGML_CUDA_F16
  539. v = __hsub2(v, {8.0f, 8.0f});
  540. v = __hmul2(v, {d, d});
  541. #else
  542. v.x = (v.x - 8.0f) * d;
  543. v.y = (v.y - 8.0f) * d;
  544. #endif // GGML_CUDA_F16
  545. }
  546. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  547. const block_q4_1 * x = (const block_q4_1 *) vx;
  548. const dfloat d = __low2half(x[ib].dm);
  549. const dfloat m = __high2half(x[ib].dm);
  550. const int vui = x[ib].qs[iqs];
  551. v.x = vui & 0xF;
  552. v.y = vui >> 4;
  553. #ifdef GGML_CUDA_F16
  554. v = __hmul2(v, {d, d});
  555. v = __hadd2(v, {m, m});
  556. #else
  557. v.x = (v.x * d) + m;
  558. v.y = (v.y * d) + m;
  559. #endif // GGML_CUDA_F16
  560. }
  561. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  562. const block_q5_0 * x = (const block_q5_0 *) vx;
  563. const dfloat d = x[ib].d;
  564. uint32_t qh;
  565. memcpy(&qh, x[ib].qh, sizeof(qh));
  566. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  567. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  568. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  569. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  570. #ifdef GGML_CUDA_F16
  571. v = __hsub2(v, {16.0f, 16.0f});
  572. v = __hmul2(v, {d, d});
  573. #else
  574. v.x = (v.x - 16.0f) * d;
  575. v.y = (v.y - 16.0f) * d;
  576. #endif // GGML_CUDA_F16
  577. }
  578. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  579. const block_q5_1 * x = (const block_q5_1 *) vx;
  580. const dfloat d = __low2half(x[ib].dm);
  581. const dfloat m = __high2half(x[ib].dm);
  582. uint32_t qh;
  583. memcpy(&qh, x[ib].qh, sizeof(qh));
  584. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  585. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  586. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  587. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  588. #ifdef GGML_CUDA_F16
  589. v = __hmul2(v, {d, d});
  590. v = __hadd2(v, {m, m});
  591. #else
  592. v.x = (v.x * d) + m;
  593. v.y = (v.y * d) + m;
  594. #endif // GGML_CUDA_F16
  595. }
  596. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  597. const block_q8_0 * x = (const block_q8_0 *) vx;
  598. const dfloat d = x[ib].d;
  599. v.x = x[ib].qs[iqs + 0];
  600. v.y = x[ib].qs[iqs + 1];
  601. #ifdef GGML_CUDA_F16
  602. v = __hmul2(v, {d, d});
  603. #else
  604. v.x *= d;
  605. v.y *= d;
  606. #endif // GGML_CUDA_F16
  607. }
  608. //================================== k-quants
  609. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, float * __restrict__ yy) {
  610. const int i = blockIdx.x;
  611. const block_q2_K * x = (const block_q2_K *) vx;
  612. const int tid = threadIdx.x;
  613. #if QK_K == 256
  614. const int n = tid/32;
  615. const int l = tid - 32*n;
  616. const int is = 8*n + l/16;
  617. const uint8_t q = x[i].qs[32*n + l];
  618. float * y = yy + i*QK_K + 128*n;
  619. float dall = __low2half(x[i].dm);
  620. float dmin = __high2half(x[i].dm);
  621. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  622. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  623. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  624. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  625. #else
  626. const int is = tid/16; // 0 or 1
  627. const int il = tid%16; // 0...15
  628. const uint8_t q = x[i].qs[il] >> (2*is);
  629. float * y = yy + i*QK_K + 16*is + il;
  630. float dall = __low2half(x[i].dm);
  631. float dmin = __high2half(x[i].dm);
  632. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  633. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  634. #endif
  635. }
  636. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, float * __restrict__ yy) {
  637. const int i = blockIdx.x;
  638. const block_q3_K * x = (const block_q3_K *) vx;
  639. #if QK_K == 256
  640. const int r = threadIdx.x/4;
  641. const int tid = r/2;
  642. const int is0 = r%2;
  643. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  644. const int n = tid / 4;
  645. const int j = tid - 4*n;
  646. uint8_t m = 1 << (4*n + j);
  647. int is = 8*n + 2*j + is0;
  648. int shift = 2*j;
  649. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  650. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  651. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  652. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  653. float d_all = x[i].d;
  654. float dl = d_all * (us - 32);
  655. float * y = yy + i*QK_K + 128*n + 32*j;
  656. const uint8_t * q = x[i].qs + 32*n;
  657. const uint8_t * hm = x[i].hmask;
  658. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  659. #else
  660. const int tid = threadIdx.x;
  661. const int is = tid/16; // 0 or 1
  662. const int il = tid%16; // 0...15
  663. const int im = il/8; // 0...1
  664. const int in = il%8; // 0...7
  665. float * y = yy + i*QK_K + 16*is + il;
  666. const uint8_t q = x[i].qs[il] >> (2*is);
  667. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  668. const float d = (float)x[i].d;
  669. if (is == 0) {
  670. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  671. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  672. } else {
  673. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  674. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  675. }
  676. #endif
  677. }
  678. #if QK_K == 256
  679. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  680. if (j < 4) {
  681. d = q[j] & 63; m = q[j + 4] & 63;
  682. } else {
  683. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  684. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  685. }
  686. }
  687. #endif
  688. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, float * __restrict__ yy) {
  689. const block_q4_K * x = (const block_q4_K *) vx;
  690. const int i = blockIdx.x;
  691. #if QK_K == 256
  692. // assume 32 threads
  693. const int tid = threadIdx.x;
  694. const int il = tid/8;
  695. const int ir = tid%8;
  696. const int is = 2*il;
  697. const int n = 4;
  698. float * y = yy + i*QK_K + 64*il + n*ir;
  699. const float dall = __low2half(x[i].dm);
  700. const float dmin = __high2half(x[i].dm);
  701. const uint8_t * q = x[i].qs + 32*il + n*ir;
  702. uint8_t sc, m;
  703. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  704. const float d1 = dall * sc; const float m1 = dmin * m;
  705. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  706. const float d2 = dall * sc; const float m2 = dmin * m;
  707. for (int l = 0; l < n; ++l) {
  708. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  709. y[l +32] = d2 * (q[l] >> 4) - m2;
  710. }
  711. #else
  712. const int tid = threadIdx.x;
  713. const uint8_t * q = x[i].qs;
  714. float * y = yy + i*QK_K;
  715. const float d = (float)x[i].dm[0];
  716. const float m = (float)x[i].dm[1];
  717. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  718. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  719. #endif
  720. }
  721. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, float * __restrict__ yy) {
  722. const block_q5_K * x = (const block_q5_K *) vx;
  723. const int i = blockIdx.x;
  724. #if QK_K == 256
  725. // assume 64 threads - this is very slightly better than the one below
  726. const int tid = threadIdx.x;
  727. const int il = tid/16; // il is in 0...3
  728. const int ir = tid%16; // ir is in 0...15
  729. const int is = 2*il; // is is in 0...6
  730. float * y = yy + i*QK_K + 64*il + 2*ir;
  731. const float dall = __low2half(x[i].dm);
  732. const float dmin = __high2half(x[i].dm);
  733. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  734. const uint8_t * qh = x[i].qh + 2*ir;
  735. uint8_t sc, m;
  736. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  737. const float d1 = dall * sc; const float m1 = dmin * m;
  738. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  739. const float d2 = dall * sc; const float m2 = dmin * m;
  740. uint8_t hm = 1 << (2*il);
  741. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  742. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  743. hm <<= 1;
  744. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  745. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  746. #else
  747. const int tid = threadIdx.x;
  748. const uint8_t q = x[i].qs[tid];
  749. const int im = tid/8; // 0...3
  750. const int in = tid%8; // 0...7
  751. const int is = tid/16; // 0 or 1
  752. const uint8_t h = x[i].qh[in] >> im;
  753. const float d = x[i].d;
  754. float * y = yy + i*QK_K + tid;
  755. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  756. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  757. #endif
  758. }
  759. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, float * __restrict__ yy) {
  760. const block_q6_K * x = (const block_q6_K *) vx;
  761. const int i = blockIdx.x;
  762. #if QK_K == 256
  763. // assume 64 threads - this is very slightly better than the one below
  764. const int tid = threadIdx.x;
  765. const int ip = tid/32; // ip is 0 or 1
  766. const int il = tid - 32*ip; // 0...32
  767. const int is = 8*ip + il/16;
  768. float * y = yy + i*QK_K + 128*ip + il;
  769. const float d = x[i].d;
  770. const uint8_t * ql = x[i].ql + 64*ip + il;
  771. const uint8_t qh = x[i].qh[32*ip + il];
  772. const int8_t * sc = x[i].scales + is;
  773. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  774. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  775. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  776. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  777. #else
  778. // assume 32 threads
  779. const int tid = threadIdx.x;
  780. const int ip = tid/16; // 0 or 1
  781. const int il = tid - 16*ip; // 0...15
  782. float * y = yy + i*QK_K + 16*ip + il;
  783. const float d = x[i].d;
  784. const uint8_t ql = x[i].ql[16*ip + il];
  785. const uint8_t qh = x[i].qh[il] >> (2*ip);
  786. const int8_t * sc = x[i].scales;
  787. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  788. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  789. #endif
  790. }
  791. 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) {
  792. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  793. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  794. if (row > nrows) return;
  795. const int num_blocks_per_row = ncols / QK_K;
  796. const int ib0 = row*num_blocks_per_row;
  797. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  798. float tmp = 0; // partial sum for thread in warp
  799. #if QK_K == 256
  800. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  801. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  802. const int step = 16/K_QUANTS_PER_ITERATION;
  803. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  804. const int in = tid - step*im; // 0...15 or 0...7
  805. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  806. const int q_offset = 32*im + l0;
  807. const int s_offset = 8*im;
  808. const int y_offset = 128*im + l0;
  809. uint32_t aux[4];
  810. const uint8_t * d = (const uint8_t *)aux;
  811. const uint8_t * m = (const uint8_t *)(aux + 2);
  812. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  813. const float * y = yy + i * QK_K + y_offset;
  814. const uint8_t * q = x[i].qs + q_offset;
  815. const float dall = __low2half(x[i].dm);
  816. const float dmin = __high2half(x[i].dm);
  817. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  818. aux[0] = a[0] & 0x0f0f0f0f;
  819. aux[1] = a[1] & 0x0f0f0f0f;
  820. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  821. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  822. float sum1 = 0, sum2 = 0;
  823. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  824. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  825. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  826. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  827. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  828. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  829. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  830. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  831. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  832. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  833. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  834. }
  835. tmp += dall * sum1 - dmin * sum2;
  836. }
  837. #else
  838. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  839. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  840. const int offset = tid * K_QUANTS_PER_ITERATION;
  841. uint32_t uaux[2];
  842. const uint8_t * d = (const uint8_t *)uaux;
  843. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  844. const float * y = yy + i * QK_K + offset;
  845. const uint8_t * q = x[i].qs + offset;
  846. const uint32_t * s = (const uint32_t *)x[i].scales;
  847. uaux[0] = s[0] & 0x0f0f0f0f;
  848. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  849. const float2 dall = __half22float2(x[i].dm);
  850. float sum1 = 0, sum2 = 0;
  851. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  852. const uint8_t ql = q[l];
  853. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  854. + y[l+16] * d[1] * ((ql >> 2) & 3)
  855. + y[l+32] * d[2] * ((ql >> 4) & 3)
  856. + y[l+48] * d[3] * ((ql >> 6) & 3);
  857. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  858. }
  859. tmp += dall.x * sum1 - dall.y * sum2;
  860. }
  861. #endif
  862. // sum up partial sums and write back result
  863. #pragma unroll
  864. for (int mask = 16; mask > 0; mask >>= 1) {
  865. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  866. }
  867. if (threadIdx.x == 0) {
  868. dst[row] = tmp;
  869. }
  870. }
  871. 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) {
  872. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  873. if (row > nrows) return;
  874. const int num_blocks_per_row = ncols / QK_K;
  875. const int ib0 = row*num_blocks_per_row;
  876. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  877. float tmp = 0; // partial sum for thread in warp
  878. #if QK_K == 256
  879. const uint16_t kmask1 = 0x0303;
  880. const uint16_t kmask2 = 0x0f0f;
  881. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  882. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  883. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  884. const int step = 16/K_QUANTS_PER_ITERATION;
  885. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  886. const int in = tid - step*im; // 0....15 or 0...7
  887. const uint8_t m = 1 << (4*im);
  888. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  889. const int q_offset = 32*im + l0;
  890. const int y_offset = 128*im + l0;
  891. uint16_t utmp[4];
  892. const int8_t * s = (const int8_t *)utmp;
  893. const uint16_t s_shift = 4*im;
  894. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  895. const float * y = yy + i * QK_K + y_offset;
  896. const uint8_t * q = x[i].qs + q_offset;
  897. const uint8_t * h = x[i].hmask + l0;
  898. const uint16_t * a = (const uint16_t *)x[i].scales;
  899. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  900. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  901. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  902. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  903. const float d = x[i].d;
  904. float sum = 0;
  905. for (int l = 0; l < n; ++l) {
  906. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  907. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  908. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  909. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  910. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  911. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  912. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  913. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  914. }
  915. tmp += d * sum;
  916. }
  917. #else
  918. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  919. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  920. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  921. const int in = offset/8; // 0 or 1
  922. const int im = offset%8; // 0...7
  923. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  924. const float * y = yy + i * QK_K + offset;
  925. const uint8_t * q = x[i].qs + offset;
  926. const uint8_t * s = x[i].scales;
  927. const float dall = (float)x[i].d;
  928. float sum = 0;
  929. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  930. const uint8_t hl = x[i].hmask[im+l] >> in;
  931. const uint8_t ql = q[l];
  932. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  933. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  934. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  935. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  936. }
  937. tmp += sum;
  938. }
  939. #endif
  940. // sum up partial sums and write back result
  941. #pragma unroll
  942. for (int mask = 16; mask > 0; mask >>= 1) {
  943. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  944. }
  945. if (threadIdx.x == 0) {
  946. dst[row] = tmp;
  947. }
  948. }
  949. 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) {
  950. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  951. if (row > nrows) return;
  952. const int num_blocks_per_row = ncols / QK_K;
  953. const int ib0 = row*num_blocks_per_row;
  954. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  955. #if QK_K == 256
  956. const uint16_t kmask1 = 0x3f3f;
  957. const uint16_t kmask2 = 0x0f0f;
  958. const uint16_t kmask3 = 0xc0c0;
  959. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  960. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  961. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  962. const int il = tid/step; // 0...3
  963. const int ir = tid - step*il; // 0...7 or 0...3
  964. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  965. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  966. const int in = il%2;
  967. const int l0 = n*(2*ir + in);
  968. const int q_offset = 32*im + l0;
  969. const int y_offset = 64*im + l0;
  970. uint16_t aux[4];
  971. const uint8_t * sc = (const uint8_t *)aux;
  972. #if K_QUANTS_PER_ITERATION == 2
  973. uint32_t q32[4];
  974. const uint8_t * q4 = (const uint8_t *)q32;
  975. #else
  976. uint16_t q16[4];
  977. const uint8_t * q4 = (const uint8_t *)q16;
  978. #endif
  979. float tmp = 0; // partial sum for thread in warp
  980. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  981. const float * y1 = yy + i*QK_K + y_offset;
  982. const float * y2 = y1 + 128;
  983. const float dall = __low2half(x[i].dm);
  984. const float dmin = __high2half(x[i].dm);
  985. const uint16_t * a = (const uint16_t *)x[i].scales;
  986. aux[0] = a[im+0] & kmask1;
  987. aux[1] = a[im+2] & kmask1;
  988. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  989. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  990. #if K_QUANTS_PER_ITERATION == 2
  991. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  992. const uint32_t * q2 = q1 + 16;
  993. q32[0] = q1[0] & 0x0f0f0f0f;
  994. q32[1] = q1[0] & 0xf0f0f0f0;
  995. q32[2] = q2[0] & 0x0f0f0f0f;
  996. q32[3] = q2[0] & 0xf0f0f0f0;
  997. float4 s = {0.f, 0.f, 0.f, 0.f};
  998. float smin = 0;
  999. for (int l = 0; l < 4; ++l) {
  1000. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1001. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1002. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1003. }
  1004. 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;
  1005. #else
  1006. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1007. const uint16_t * q2 = q1 + 32;
  1008. q16[0] = q1[0] & 0x0f0f;
  1009. q16[1] = q1[0] & 0xf0f0;
  1010. q16[2] = q2[0] & 0x0f0f;
  1011. q16[3] = q2[0] & 0xf0f0;
  1012. float4 s = {0.f, 0.f, 0.f, 0.f};
  1013. float smin = 0;
  1014. for (int l = 0; l < 2; ++l) {
  1015. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1016. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1017. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1018. }
  1019. 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;
  1020. #endif
  1021. }
  1022. #else
  1023. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1024. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1025. const int step = tid * K_QUANTS_PER_ITERATION;
  1026. uint16_t aux16[2];
  1027. const uint8_t * s = (const uint8_t *)aux16;
  1028. float tmp = 0;
  1029. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1030. const uint8_t * q = x[i].qs + step;
  1031. const float * y = yy + i*QK_K + step;
  1032. const uint16_t * a = (const uint16_t *)x[i].scales;
  1033. aux16[0] = a[0] & 0x0f0f;
  1034. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1035. const float d = (float)x[i].dm[0];
  1036. const float m = (float)x[i].dm[1];
  1037. float sum = 0.f;
  1038. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1039. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1040. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1041. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1042. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1043. }
  1044. tmp += sum;
  1045. }
  1046. #endif
  1047. // sum up partial sums and write back result
  1048. #pragma unroll
  1049. for (int mask = 16; mask > 0; mask >>= 1) {
  1050. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1051. }
  1052. if (tid == 0) {
  1053. dst[row] = tmp;
  1054. }
  1055. }
  1056. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1057. const int row = blockIdx.x;
  1058. const int num_blocks_per_row = ncols / QK_K;
  1059. const int ib0 = row*num_blocks_per_row;
  1060. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1061. float tmp = 0; // partial sum for thread in warp
  1062. #if QK_K == 256
  1063. const uint16_t kmask1 = 0x3f3f;
  1064. const uint16_t kmask2 = 0x0f0f;
  1065. const uint16_t kmask3 = 0xc0c0;
  1066. const int tid = threadIdx.x/2; // 0...15
  1067. const int ix = threadIdx.x%2;
  1068. const int il = tid/4; // 0...3
  1069. const int ir = tid - 4*il;// 0...3
  1070. const int n = 2;
  1071. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1072. const int in = il%2;
  1073. const int l0 = n*(2*ir + in);
  1074. const int q_offset = 32*im + l0;
  1075. const int y_offset = 64*im + l0;
  1076. const uint8_t hm1 = 1 << (2*im);
  1077. const uint8_t hm2 = hm1 << 4;
  1078. uint16_t aux[4];
  1079. const uint8_t * sc = (const uint8_t *)aux;
  1080. uint16_t q16[8];
  1081. const uint8_t * q4 = (const uint8_t *)q16;
  1082. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1083. const uint8_t * ql1 = x[i].qs + q_offset;
  1084. const uint8_t * qh = x[i].qh + l0;
  1085. const float * y1 = yy + i*QK_K + y_offset;
  1086. const float * y2 = y1 + 128;
  1087. const float dall = __low2half(x[i].dm);
  1088. const float dmin = __high2half(x[i].dm);
  1089. const uint16_t * a = (const uint16_t *)x[i].scales;
  1090. aux[0] = a[im+0] & kmask1;
  1091. aux[1] = a[im+2] & kmask1;
  1092. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1093. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1094. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1095. float smin = 0;
  1096. const uint16_t * q1 = (const uint16_t *)ql1;
  1097. const uint16_t * q2 = q1 + 32;
  1098. q16[0] = q1[0] & 0x0f0f;
  1099. q16[1] = q1[8] & 0x0f0f;
  1100. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1101. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1102. q16[4] = q2[0] & 0x0f0f;
  1103. q16[5] = q2[8] & 0x0f0f;
  1104. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1105. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1106. for (int l = 0; l < n; ++l) {
  1107. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1108. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1109. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1110. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1111. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1112. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1113. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1114. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1115. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1116. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1117. }
  1118. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1119. }
  1120. #else
  1121. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1122. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1123. const int step = tid * K_QUANTS_PER_ITERATION;
  1124. const int im = step/8;
  1125. const int in = step%8;
  1126. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1127. const uint8_t * q = x[i].qs + step;
  1128. const int8_t * s = x[i].scales;
  1129. const float * y = yy + i*QK_K + step;
  1130. const float d = x[i].d;
  1131. float sum = 0.f;
  1132. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1133. const uint8_t h = x[i].qh[in+j] >> im;
  1134. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1135. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1136. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1137. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1138. }
  1139. tmp += sum;
  1140. }
  1141. #endif
  1142. // sum up partial sums and write back result
  1143. #pragma unroll
  1144. for (int mask = 16; mask > 0; mask >>= 1) {
  1145. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1146. }
  1147. if (threadIdx.x == 0) {
  1148. dst[row] = tmp;
  1149. }
  1150. }
  1151. 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) {
  1152. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1153. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  1154. if (row > nrows) return;
  1155. const int num_blocks_per_row = ncols / QK_K;
  1156. const int ib0 = row*num_blocks_per_row;
  1157. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1158. #if QK_K == 256
  1159. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1160. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1161. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1162. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1163. const int in = tid - step*im; // 0...15 or 0...7
  1164. #if K_QUANTS_PER_ITERATION == 1
  1165. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1166. const int is = 0;
  1167. #else
  1168. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1169. const int is = in / 4;
  1170. #endif
  1171. const int ql_offset = 64*im + l0;
  1172. const int qh_offset = 32*im + l0;
  1173. const int s_offset = 8*im + is;
  1174. const int y_offset = 128*im + l0;
  1175. float tmp = 0; // partial sum for thread in warp
  1176. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1177. const float * y = yy + i * QK_K + y_offset;
  1178. const uint8_t * ql = x[i].ql + ql_offset;
  1179. const uint8_t * qh = x[i].qh + qh_offset;
  1180. const int8_t * s = x[i].scales + s_offset;
  1181. const float d = x[i].d;
  1182. #if K_QUANTS_PER_ITERATION == 1
  1183. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1184. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1185. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1186. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1187. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1188. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1189. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1190. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1191. tmp += sum;
  1192. #else
  1193. float sum = 0;
  1194. for (int l = 0; l < 4; ++l) {
  1195. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1196. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1197. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1198. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1199. }
  1200. tmp += sum;
  1201. #endif
  1202. }
  1203. #else
  1204. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1205. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1206. const int step = tid * K_QUANTS_PER_ITERATION;
  1207. float tmp = 0; // partial sum for thread in warp
  1208. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1209. const float * y = yy + i * QK_K + step;
  1210. const uint8_t * ql = x[i].ql + step;
  1211. const uint8_t * qh = x[i].qh + step;
  1212. const int8_t * s = x[i].scales;
  1213. const float d = x[i+0].d;
  1214. float sum = 0;
  1215. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1216. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1217. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1218. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1219. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1220. }
  1221. tmp += sum;
  1222. }
  1223. #endif
  1224. // sum up partial sums and write back result
  1225. #pragma unroll
  1226. for (int mask = 16; mask > 0; mask >>= 1) {
  1227. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1228. }
  1229. if (tid == 0) {
  1230. dst[row] = tmp;
  1231. }
  1232. }
  1233. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1234. const half * x = (const half *) vx;
  1235. // automatic half -> float type cast if dfloat == float
  1236. v.x = x[ib + iqs + 0];
  1237. v.y = x[ib + iqs + 1];
  1238. }
  1239. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1240. const float * x = (const float *) vx;
  1241. // automatic half -> float type cast if dfloat == float
  1242. v.x = x[ib + iqs + 0];
  1243. v.y = x[ib + iqs + 1];
  1244. }
  1245. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1246. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1247. if (ix >= kx_padded) {
  1248. return;
  1249. }
  1250. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1251. const int i_padded = iy*kx_padded + ix;
  1252. block_q8_1 * y = (block_q8_1 *) vy;
  1253. const int ib = i_padded / QK8_1; // block index
  1254. const int iqs = i_padded % QK8_1; // quant index
  1255. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1256. float amax = fabsf(xi);
  1257. float sum = xi;
  1258. #pragma unroll
  1259. for (int mask = 16; mask > 0; mask >>= 1) {
  1260. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1261. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1262. }
  1263. const float d = amax / 127;
  1264. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1265. y[ib].qs[iqs] = q;
  1266. if (iqs > 0) {
  1267. return;
  1268. }
  1269. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1270. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1271. }
  1272. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1273. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1274. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1275. if (i >= k) {
  1276. return;
  1277. }
  1278. const int ib = i/qk; // block index
  1279. const int iqs = (i%qk)/qr; // quant index
  1280. const int iybs = i - i%qk; // y block start index
  1281. const int y_offset = qr == 1 ? 1 : qk/2;
  1282. // dequantize
  1283. dfloat2 v;
  1284. dequantize_kernel(vx, ib, iqs, v);
  1285. y[iybs + iqs + 0] = v.x;
  1286. y[iybs + iqs + y_offset] = v.y;
  1287. }
  1288. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1289. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1290. #define VDR_Q4_0_Q8_1_MMVQ 2
  1291. #define VDR_Q4_0_Q8_1_MMQ 4
  1292. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1293. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1294. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1295. int sumi = 0;
  1296. #pragma unroll
  1297. for (int i = 0; i < vdr; ++i) {
  1298. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1299. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1300. // SIMD dot product of quantized values
  1301. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1302. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1303. }
  1304. const float2 ds8f = __half22float2(ds8);
  1305. // second part effectively subtracts 8 from each quant value
  1306. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1307. #else
  1308. assert(false);
  1309. return 0.0f; // only to satisfy the compiler
  1310. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1311. }
  1312. #define VDR_Q4_1_Q8_1_MMVQ 2
  1313. #define VDR_Q4_1_Q8_1_MMQ 4
  1314. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1315. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1316. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1317. int sumi = 0;
  1318. #pragma unroll
  1319. for (int i = 0; i < vdr; ++i) {
  1320. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1321. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1322. // SIMD dot product of quantized values
  1323. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1324. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1325. }
  1326. #ifdef GGML_CUDA_F16
  1327. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1328. const float d4d8 = tmp.x;
  1329. const float m4s8 = tmp.y;
  1330. #else
  1331. const float2 dm4f = __half22float2(dm4);
  1332. const float2 ds8f = __half22float2(ds8);
  1333. const float d4d8 = dm4f.x * ds8f.x;
  1334. const float m4s8 = dm4f.y * ds8f.y;
  1335. #endif // GGML_CUDA_F16
  1336. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1337. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1338. #else
  1339. assert(false);
  1340. return 0.0f; // only to satisfy the compiler
  1341. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1342. }
  1343. #define VDR_Q5_0_Q8_1_MMVQ 2
  1344. #define VDR_Q5_0_Q8_1_MMQ 4
  1345. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1346. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1347. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1348. int sumi = 0;
  1349. #pragma unroll
  1350. for (int i = 0; i < vdr; ++i) {
  1351. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1352. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1353. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1354. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1355. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1356. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1357. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1358. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1359. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1360. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1361. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1362. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1363. }
  1364. const float2 ds8f = __half22float2(ds8);
  1365. // second part effectively subtracts 16 from each quant value
  1366. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1367. #else
  1368. assert(false);
  1369. return 0.0f; // only to satisfy the compiler
  1370. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1371. }
  1372. #define VDR_Q5_1_Q8_1_MMVQ 2
  1373. #define VDR_Q5_1_Q8_1_MMQ 4
  1374. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1375. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1376. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1377. int sumi = 0;
  1378. #pragma unroll
  1379. for (int i = 0; i < vdr; ++i) {
  1380. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1381. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1382. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1383. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1384. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1385. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1386. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1387. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1388. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1389. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1390. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1391. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1392. }
  1393. #ifdef GGML_CUDA_F16
  1394. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1395. const float d5d8 = tmp.x;
  1396. const float m5s8 = tmp.y;
  1397. #else
  1398. const float2 dm5f = __half22float2(dm5);
  1399. const float2 ds8f = __half22float2(ds8);
  1400. const float d5d8 = dm5f.x * ds8f.x;
  1401. const float m5s8 = dm5f.y * ds8f.y;
  1402. #endif // GGML_CUDA_F16
  1403. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1404. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1405. #else
  1406. assert(false);
  1407. return 0.0f; // only to satisfy the compiler
  1408. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1409. }
  1410. #define VDR_Q8_0_Q8_1_MMVQ 2
  1411. #define VDR_Q8_0_Q8_1_MMQ 8
  1412. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1413. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1414. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1415. int sumi = 0;
  1416. #pragma unroll
  1417. for (int i = 0; i < vdr; ++i) {
  1418. // SIMD dot product of quantized values
  1419. sumi = __dp4a(v[i], u[i], sumi);
  1420. }
  1421. return d8_0*d8_1 * sumi;
  1422. #else
  1423. assert(false);
  1424. return 0.0f; // only to satisfy the compiler
  1425. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1426. }
  1427. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1428. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1429. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1430. int sumi = 0;
  1431. #pragma unroll
  1432. for (int i = 0; i < vdr; ++i) {
  1433. // SIMD dot product of quantized values
  1434. sumi = __dp4a(v[i], u[i], sumi);
  1435. }
  1436. #ifdef GGML_CUDA_F16
  1437. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1438. const float d8d8 = tmp.x;
  1439. const float m8s8 = tmp.y;
  1440. #else
  1441. const float2 dm8f = __half22float2(dm8);
  1442. const float2 ds8f = __half22float2(ds8);
  1443. const float d8d8 = dm8f.x * ds8f.x;
  1444. const float m8s8 = dm8f.y * ds8f.y;
  1445. #endif // GGML_CUDA_F16
  1446. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1447. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1448. #else
  1449. assert(false);
  1450. return 0.0f; // only to satisfy the compiler
  1451. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1452. }
  1453. #define VDR_Q2_K_Q8_1_MMVQ 1
  1454. #define VDR_Q2_K_Q8_1_MMQ 2
  1455. // contiguous v/x values
  1456. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1457. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1458. const half2 & dm2, const float * __restrict__ d8) {
  1459. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1460. float sumf_d = 0.0f;
  1461. float sumf_m = 0.0f;
  1462. #pragma unroll
  1463. for (int i = 0; i < QR2_K; ++i) {
  1464. const int sc = scales[2*i];
  1465. const int vi = (v >> (2*i)) & 0x03030303;
  1466. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1467. // fill int with 4x m
  1468. int m = sc >> 4;
  1469. m |= m << 8;
  1470. m |= m << 16;
  1471. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1472. }
  1473. const float2 dm2f = __half22float2(dm2);
  1474. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1475. #else
  1476. assert(false);
  1477. return 0.0f; // only to satisfy the compiler
  1478. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1479. }
  1480. // contiguous u/y values
  1481. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1482. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1483. const half2 & dm2, const float & d8) {
  1484. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1485. int sumi_d = 0;
  1486. int sumi_m = 0;
  1487. #pragma unroll
  1488. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1489. int sumi_d_sc = 0;
  1490. const int sc = scales[i0 / (QI8_1/2)];
  1491. // fill int with 4x m
  1492. int m = sc >> 4;
  1493. m |= m << 8;
  1494. m |= m << 16;
  1495. #pragma unroll
  1496. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1497. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1498. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1499. }
  1500. sumi_d += sumi_d_sc * (sc & 0xF);
  1501. }
  1502. const float2 dm2f = __half22float2(dm2);
  1503. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1504. #else
  1505. assert(false);
  1506. return 0.0f; // only to satisfy the compiler
  1507. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1508. }
  1509. #define VDR_Q3_K_Q8_1_MMVQ 1
  1510. #define VDR_Q3_K_Q8_1_MMQ 2
  1511. // contiguous v/x values
  1512. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1513. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1514. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1515. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1516. float sumf = 0.0f;
  1517. #pragma unroll
  1518. for (int i = 0; i < QR3_K; ++i) {
  1519. const int isc = scale_offset + 2*i;
  1520. const int isc_low = isc % (QK_K/32);
  1521. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1522. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1523. const int isc_high = isc % (QK_K/64);
  1524. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1525. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1526. const int sc = (sc_low | sc_high) - 32;
  1527. const int vil = (vl >> (2*i)) & 0x03030303;
  1528. const int vih = ((vh >> i) << 2) & 0x04040404;
  1529. const int vi = __vsubss4(vil, vih);
  1530. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1531. }
  1532. return d3 * sumf;
  1533. #else
  1534. assert(false);
  1535. return 0.0f; // only to satisfy the compiler
  1536. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1537. }
  1538. // contiguous u/y values
  1539. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1540. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1541. const float & d3, const float & d8) {
  1542. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1543. int sumi = 0;
  1544. #pragma unroll
  1545. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1546. int sumi_sc = 0;
  1547. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1548. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1549. }
  1550. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1551. }
  1552. return d3*d8 * sumi;
  1553. #else
  1554. assert(false);
  1555. return 0.0f; // only to satisfy the compiler
  1556. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1557. }
  1558. #define VDR_Q4_K_Q8_1_MMVQ 2
  1559. #define VDR_Q4_K_Q8_1_MMQ 8
  1560. // contiguous v/x values
  1561. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1562. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1563. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1564. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1565. float sumf_d = 0.0f;
  1566. float sumf_m = 0.0f;
  1567. #pragma unroll
  1568. for (int i = 0; i < QR4_K; ++i) {
  1569. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1570. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1571. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1572. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1573. sumf_d += d8[i] * (dot1 * sc[i]);
  1574. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1575. }
  1576. const float2 dm4f = __half22float2(dm4);
  1577. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1578. #else
  1579. assert(false);
  1580. return 0.0f; // only to satisfy the compiler
  1581. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1582. }
  1583. // contiguous u/y values
  1584. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1585. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1586. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1587. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1588. float sumf_d = 0.0f;
  1589. float sumf_m = 0.0f;
  1590. #pragma unroll
  1591. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1592. int sumi_d = 0;
  1593. #pragma unroll
  1594. for (int j = 0; j < QI8_1; ++j) {
  1595. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1596. }
  1597. const float2 ds8f = __half22float2(ds8[i]);
  1598. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1599. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1600. }
  1601. const float2 dm4f = __half22float2(dm4);
  1602. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1603. #else
  1604. assert(false);
  1605. return 0.0f; // only to satisfy the compiler
  1606. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1607. }
  1608. #define VDR_Q5_K_Q8_1_MMVQ 2
  1609. #define VDR_Q5_K_Q8_1_MMQ 8
  1610. // contiguous v/x values
  1611. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1612. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1613. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1614. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1615. float sumf_d = 0.0f;
  1616. float sumf_m = 0.0f;
  1617. #pragma unroll
  1618. for (int i = 0; i < QR5_K; ++i) {
  1619. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1620. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1621. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1622. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1623. const int v0i = vl0i | vh0i;
  1624. const int v1i = vl1i | vh1i;
  1625. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1626. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1627. sumf_d += d8[i] * (dot1 * sc[i]);
  1628. sumf_m += d8[i] * (dot2 * m[i]);
  1629. }
  1630. const float2 dm5f = __half22float2(dm5);
  1631. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1632. #else
  1633. assert(false);
  1634. return 0.0f; // only to satisfy the compiler
  1635. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1636. }
  1637. // contiguous u/y values
  1638. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1639. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1640. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1641. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1642. float sumf_d = 0.0f;
  1643. float sumf_m = 0.0f;
  1644. #pragma unroll
  1645. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1646. int sumi_d = 0;
  1647. #pragma unroll
  1648. for (int j = 0; j < QI8_1; ++j) {
  1649. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1650. }
  1651. const float2 ds8f = __half22float2(ds8[i]);
  1652. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1653. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1654. }
  1655. const float2 dm4f = __half22float2(dm4);
  1656. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1657. #else
  1658. assert(false);
  1659. return 0.0f; // only to satisfy the compiler
  1660. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1661. }
  1662. #define VDR_Q6_K_Q8_1_MMVQ 1
  1663. #define VDR_Q6_K_Q8_1_MMQ 8
  1664. // contiguous v/x values
  1665. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1666. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1667. const float & d, const float * __restrict__ d8) {
  1668. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1669. float sumf = 0.0f;
  1670. #pragma unroll
  1671. for (int i = 0; i < QR6_K; ++i) {
  1672. const int sc = scales[4*i];
  1673. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1674. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1675. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1676. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1677. }
  1678. return d*sumf;
  1679. #else
  1680. assert(false);
  1681. return 0.0f; // only to satisfy the compiler
  1682. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1683. }
  1684. // contiguous u/y values
  1685. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1686. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1687. const float & d6, const float * __restrict__ d8) {
  1688. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1689. float sumf_d = 0.0f;
  1690. #pragma unroll
  1691. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1692. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  1693. #pragma unroll
  1694. for (int i = i0; i < i0 + 2; ++i) {
  1695. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  1696. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  1697. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  1698. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  1699. }
  1700. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  1701. }
  1702. return d6 * sumf_d;
  1703. #else
  1704. assert(false);
  1705. return 0.0f; // only to satisfy the compiler
  1706. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1707. }
  1708. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  1709. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1710. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  1711. int v[VDR_Q4_0_Q8_1_MMVQ];
  1712. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  1713. #pragma unroll
  1714. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  1715. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  1716. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1717. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  1718. }
  1719. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  1720. }
  1721. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1722. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  1723. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  1724. *x_ql = tile_x_qs;
  1725. *x_dm = (half2 *) tile_x_d;
  1726. }
  1727. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  1728. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1729. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1730. GGML_CUDA_ASSUME(i_offset >= 0);
  1731. GGML_CUDA_ASSUME(i_offset < nwarps);
  1732. GGML_CUDA_ASSUME(k >= 0);
  1733. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1734. const int kbx = k / QI4_0;
  1735. const int kqsx = k % QI4_0;
  1736. const block_q4_0 * bx0 = (block_q4_0 *) vx;
  1737. float * x_dmf = (float *) x_dm;
  1738. #pragma unroll
  1739. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1740. int i = i0 + i_offset;
  1741. if (need_check) {
  1742. i = min(i, i_max);
  1743. }
  1744. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1745. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  1746. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  1747. }
  1748. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  1749. const int kbxd = k % blocks_per_tile_x_row;
  1750. #pragma unroll
  1751. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  1752. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  1753. if (need_check) {
  1754. i = min(i, i_max);
  1755. }
  1756. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1757. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  1758. }
  1759. }
  1760. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  1761. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1762. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1763. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1764. const float * x_dmf = (float *) x_dm;
  1765. int u[2*VDR_Q4_0_Q8_1_MMQ];
  1766. #pragma unroll
  1767. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  1768. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1769. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  1770. }
  1771. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  1772. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  1773. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1774. }
  1775. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  1776. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1777. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  1778. int v[VDR_Q4_1_Q8_1_MMVQ];
  1779. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  1780. #pragma unroll
  1781. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  1782. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  1783. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1784. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  1785. }
  1786. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  1787. }
  1788. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1789. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  1790. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  1791. *x_ql = tile_x_qs;
  1792. *x_dm = tile_x_dm;
  1793. }
  1794. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  1795. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1796. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1797. GGML_CUDA_ASSUME(i_offset >= 0);
  1798. GGML_CUDA_ASSUME(i_offset < nwarps);
  1799. GGML_CUDA_ASSUME(k >= 0);
  1800. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1801. const int kbx = k / QI4_1;
  1802. const int kqsx = k % QI4_1;
  1803. const block_q4_1 * bx0 = (block_q4_1 *) vx;
  1804. #pragma unroll
  1805. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1806. int i = i0 + i_offset;
  1807. if (need_check) {
  1808. i = min(i, i_max);
  1809. }
  1810. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1811. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1812. }
  1813. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  1814. const int kbxd = k % blocks_per_tile_x_row;
  1815. #pragma unroll
  1816. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  1817. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  1818. if (need_check) {
  1819. i = min(i, i_max);
  1820. }
  1821. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1822. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  1823. }
  1824. }
  1825. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  1826. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1827. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1828. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1829. int u[2*VDR_Q4_1_Q8_1_MMQ];
  1830. #pragma unroll
  1831. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  1832. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1833. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  1834. }
  1835. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  1836. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  1837. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  1838. }
  1839. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  1840. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1841. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  1842. int vl[VDR_Q5_0_Q8_1_MMVQ];
  1843. int vh[VDR_Q5_0_Q8_1_MMVQ];
  1844. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  1845. #pragma unroll
  1846. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  1847. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  1848. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  1849. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1850. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  1851. }
  1852. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  1853. }
  1854. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1855. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1856. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  1857. *x_ql = tile_x_ql;
  1858. *x_dm = (half2 *) tile_x_d;
  1859. }
  1860. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  1861. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1862. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1863. GGML_CUDA_ASSUME(i_offset >= 0);
  1864. GGML_CUDA_ASSUME(i_offset < nwarps);
  1865. GGML_CUDA_ASSUME(k >= 0);
  1866. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1867. const int kbx = k / QI5_0;
  1868. const int kqsx = k % QI5_0;
  1869. const block_q5_0 * bx0 = (block_q5_0 *) vx;
  1870. #pragma unroll
  1871. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1872. int i = i0 + i_offset;
  1873. if (need_check) {
  1874. i = min(i, i_max);
  1875. }
  1876. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  1877. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  1878. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  1879. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1880. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1881. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1882. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1883. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1884. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  1885. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1886. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1887. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1888. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1889. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1890. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1891. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  1892. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1893. }
  1894. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  1895. const int kbxd = k % blocks_per_tile_x_row;
  1896. float * x_dmf = (float *) x_dm;
  1897. #pragma unroll
  1898. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  1899. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  1900. if (need_check) {
  1901. i = min(i, i_max);
  1902. }
  1903. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  1904. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  1905. }
  1906. }
  1907. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  1908. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1909. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1910. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1911. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  1912. const float * x_dmf = (const float *) x_dm;
  1913. const float * y_df = (const float *) y_ds;
  1914. int u[2*VDR_Q5_0_Q8_1_MMQ];
  1915. #pragma unroll
  1916. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  1917. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1918. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  1919. }
  1920. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  1921. (&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)]);
  1922. }
  1923. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  1924. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  1925. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  1926. int vl[VDR_Q5_1_Q8_1_MMVQ];
  1927. int vh[VDR_Q5_1_Q8_1_MMVQ];
  1928. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  1929. #pragma unroll
  1930. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  1931. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  1932. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  1933. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  1934. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  1935. }
  1936. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  1937. }
  1938. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  1939. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  1940. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  1941. *x_ql = tile_x_ql;
  1942. *x_dm = tile_x_dm;
  1943. }
  1944. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  1945. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  1946. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  1947. GGML_CUDA_ASSUME(i_offset >= 0);
  1948. GGML_CUDA_ASSUME(i_offset < nwarps);
  1949. GGML_CUDA_ASSUME(k >= 0);
  1950. GGML_CUDA_ASSUME(k < WARP_SIZE);
  1951. const int kbx = k / QI5_1;
  1952. const int kqsx = k % QI5_1;
  1953. const block_q5_1 * bx0 = (block_q5_1 *) vx;
  1954. #pragma unroll
  1955. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  1956. int i = i0 + i_offset;
  1957. if (need_check) {
  1958. i = min(i, i_max);
  1959. }
  1960. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  1961. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  1962. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  1963. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  1964. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  1965. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  1966. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  1967. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  1968. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  1969. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  1970. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  1971. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  1972. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  1973. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  1974. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  1975. }
  1976. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  1977. const int kbxd = k % blocks_per_tile_x_row;
  1978. #pragma unroll
  1979. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  1980. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  1981. if (need_check) {
  1982. i = min(i, i_max);
  1983. }
  1984. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  1985. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  1986. }
  1987. }
  1988. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  1989. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  1990. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  1991. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  1992. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  1993. int u[2*VDR_Q5_1_Q8_1_MMQ];
  1994. #pragma unroll
  1995. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  1996. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  1997. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  1998. }
  1999. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2000. (&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)]);
  2001. }
  2002. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2003. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2004. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2005. int v[VDR_Q8_0_Q8_1_MMVQ];
  2006. int u[VDR_Q8_0_Q8_1_MMVQ];
  2007. #pragma unroll
  2008. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2009. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2010. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2011. }
  2012. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2013. }
  2014. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2015. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2016. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2017. *x_ql = tile_x_qs;
  2018. *x_dm = (half2 *) tile_x_d;
  2019. }
  2020. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2021. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2022. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2023. GGML_CUDA_ASSUME(i_offset >= 0);
  2024. GGML_CUDA_ASSUME(i_offset < nwarps);
  2025. GGML_CUDA_ASSUME(k >= 0);
  2026. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2027. const int kbx = k / QI8_0;
  2028. const int kqsx = k % QI8_0;
  2029. float * x_dmf = (float *) x_dm;
  2030. const block_q8_0 * bx0 = (block_q8_0 *) vx;
  2031. #pragma unroll
  2032. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2033. int i = i0 + i_offset;
  2034. if (need_check) {
  2035. i = min(i, i_max);
  2036. }
  2037. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2038. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2039. }
  2040. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2041. const int kbxd = k % blocks_per_tile_x_row;
  2042. #pragma unroll
  2043. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2044. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2045. if (need_check) {
  2046. i = min(i, i_max);
  2047. }
  2048. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2049. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2050. }
  2051. }
  2052. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2053. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2054. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2055. const float * x_dmf = (const float *) x_dm;
  2056. const float * y_df = (const float *) y_ds;
  2057. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2058. (&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],
  2059. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2060. }
  2061. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2062. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2063. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2064. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2065. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2066. const uint8_t * scales = bq2_K->scales + scale_offset;
  2067. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2068. int u[QR2_K];
  2069. float d8[QR2_K];
  2070. #pragma unroll
  2071. for (int i = 0; i < QR2_K; ++ i) {
  2072. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2073. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2074. }
  2075. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2076. }
  2077. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2078. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2079. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2080. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2081. *x_ql = tile_x_ql;
  2082. *x_dm = tile_x_dm;
  2083. *x_sc = tile_x_sc;
  2084. }
  2085. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2086. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2087. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2088. GGML_CUDA_ASSUME(i_offset >= 0);
  2089. GGML_CUDA_ASSUME(i_offset < nwarps);
  2090. GGML_CUDA_ASSUME(k >= 0);
  2091. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2092. const int kbx = k / QI2_K;
  2093. const int kqsx = k % QI2_K;
  2094. const block_q2_K * bx0 = (block_q2_K *) vx;
  2095. #pragma unroll
  2096. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2097. int i = i0 + i_offset;
  2098. if (need_check) {
  2099. i = min(i, i_max);
  2100. }
  2101. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2102. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2103. }
  2104. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2105. const int kbxd = k % blocks_per_tile_x_row;
  2106. #pragma unroll
  2107. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2108. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2109. if (need_check) {
  2110. i = min(i, i_max);
  2111. }
  2112. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2113. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2114. }
  2115. #pragma unroll
  2116. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2117. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2118. if (need_check) {
  2119. i = min(i, i_max);
  2120. }
  2121. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2122. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2123. }
  2124. }
  2125. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2126. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2127. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2128. const int kbx = k / QI2_K;
  2129. const int ky = (k % QI2_K) * QR2_K;
  2130. const float * y_df = (const float *) y_ds;
  2131. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2132. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2133. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2134. #pragma unroll
  2135. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2136. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2137. }
  2138. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2139. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2140. 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]);
  2141. }
  2142. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2143. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2144. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2145. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2146. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2147. const float d = bq3_K->d;
  2148. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2149. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2150. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2151. int u[QR3_K];
  2152. float d8[QR3_K];
  2153. #pragma unroll
  2154. for (int i = 0; i < QR3_K; ++i) {
  2155. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2156. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2157. }
  2158. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2159. }
  2160. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2161. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2162. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2163. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2164. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2165. *x_ql = tile_x_ql;
  2166. *x_dm = tile_x_dm;
  2167. *x_qh = tile_x_qh;
  2168. *x_sc = tile_x_sc;
  2169. }
  2170. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2171. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2172. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2173. GGML_CUDA_ASSUME(i_offset >= 0);
  2174. GGML_CUDA_ASSUME(i_offset < nwarps);
  2175. GGML_CUDA_ASSUME(k >= 0);
  2176. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2177. const int kbx = k / QI3_K;
  2178. const int kqsx = k % QI3_K;
  2179. const block_q3_K * bx0 = (block_q3_K *) vx;
  2180. #pragma unroll
  2181. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2182. int i = i0 + i_offset;
  2183. if (need_check) {
  2184. i = min(i, i_max);
  2185. }
  2186. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2187. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2188. }
  2189. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2190. const int kbxd = k % blocks_per_tile_x_row;
  2191. float * x_dmf = (float *) x_dm;
  2192. #pragma unroll
  2193. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2194. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2195. if (need_check) {
  2196. i = min(i, i_max);
  2197. }
  2198. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2199. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2200. }
  2201. #pragma unroll
  2202. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2203. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2204. if (need_check) {
  2205. i = min(i, i_max);
  2206. }
  2207. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2208. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2209. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2210. }
  2211. #pragma unroll
  2212. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2213. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2214. if (need_check) {
  2215. i = min(i, i_max);
  2216. }
  2217. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2218. const int ksc = k % (QI3_K/4);
  2219. const int ksc_low = ksc % (QI3_K/8);
  2220. const int shift_low = 4 * (ksc / (QI3_K/8));
  2221. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2222. const int ksc_high = QI3_K/8;
  2223. const int shift_high = 2 * ksc;
  2224. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2225. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2226. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2227. }
  2228. }
  2229. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2230. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2231. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2232. const int kbx = k / QI3_K;
  2233. const int ky = (k % QI3_K) * QR3_K;
  2234. const float * x_dmf = (const float *) x_dm;
  2235. const float * y_df = (const float *) y_ds;
  2236. const int8_t * scales = ((int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2237. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2238. #pragma unroll
  2239. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2240. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2241. const int shift = 2 * ((ky % 32) / 8);
  2242. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2243. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2244. const int vlh = (vh << 2) & 0x04040404;
  2245. v[l] = __vsubss4(vll, vlh);
  2246. }
  2247. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2248. 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]);
  2249. }
  2250. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2251. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2252. #ifndef GGML_QKK_64
  2253. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2254. int v[2];
  2255. int u[2*QR4_K];
  2256. float d8[QR4_K];
  2257. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2258. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2259. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2260. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2261. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2262. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2263. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2264. v[0] = q4[0];
  2265. v[1] = q4[4];
  2266. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2267. uint16_t aux[2];
  2268. const int j = bq8_offset/2;
  2269. if (j < 2) {
  2270. aux[0] = scales[j+0] & 0x3f3f;
  2271. aux[1] = scales[j+2] & 0x3f3f;
  2272. } else {
  2273. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2274. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2275. }
  2276. const uint8_t * sc = (const uint8_t *)aux;
  2277. const uint8_t * m = sc + 2;
  2278. for (int i = 0; i < QR4_K; ++i) {
  2279. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2280. d8[i] = __low2half(bq8i->ds);
  2281. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2282. u[2*i+0] = q8[0];
  2283. u[2*i+1] = q8[4];
  2284. }
  2285. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2286. #else
  2287. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2288. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2289. float sumf_d = 0.0f;
  2290. float sumf_m = 0.0f;
  2291. uint16_t aux16[2];
  2292. const uint8_t * s = (const uint8_t *)aux16;
  2293. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2294. aux16[0] = a[0] & 0x0f0f;
  2295. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2296. const float dall = bq4_K->dm[0];
  2297. const float dmin = bq4_K->dm[1];
  2298. const float d8_1 = __low2float(bq8_1[0].ds);
  2299. const float d8_2 = __low2float(bq8_1[1].ds);
  2300. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2301. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2302. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2303. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2304. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2305. const int v1 = q4[0];
  2306. const int v2 = q4[4];
  2307. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2308. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2309. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2310. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2311. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2312. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2313. return dall * sumf_d - dmin * sumf_m;
  2314. #else
  2315. assert(false);
  2316. return 0.0f; // only to satisfy the compiler
  2317. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2318. #endif
  2319. }
  2320. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2321. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2322. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2323. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2324. *x_ql = tile_x_ql;
  2325. *x_dm = tile_x_dm;
  2326. *x_sc = tile_x_sc;
  2327. }
  2328. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2329. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2330. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2331. GGML_CUDA_ASSUME(i_offset >= 0);
  2332. GGML_CUDA_ASSUME(i_offset < nwarps);
  2333. GGML_CUDA_ASSUME(k >= 0);
  2334. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2335. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2336. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2337. const block_q4_K * bx0 = (block_q4_K *) vx;
  2338. #pragma unroll
  2339. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2340. int i = i0 + i_offset;
  2341. if (need_check) {
  2342. i = min(i, i_max);
  2343. }
  2344. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2345. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2346. }
  2347. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2348. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2349. #pragma unroll
  2350. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2351. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2352. if (need_check) {
  2353. i = min(i, i_max);
  2354. }
  2355. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2356. #if QK_K == 256
  2357. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2358. #else
  2359. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2360. #endif
  2361. }
  2362. #pragma unroll
  2363. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2364. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2365. if (need_check) {
  2366. i = min(i, i_max);
  2367. }
  2368. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2369. const int * scales = (int *) bxi->scales;
  2370. const int ksc = k % (WARP_SIZE/8);
  2371. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2372. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2373. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2374. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2375. }
  2376. }
  2377. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2378. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2379. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2380. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2381. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2382. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2383. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2384. }
  2385. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2386. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2387. #ifndef GGML_QKK_64
  2388. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2389. int vl[2];
  2390. int vh[2];
  2391. int u[2*QR5_K];
  2392. float d8[QR5_K];
  2393. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2394. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2395. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2396. vl[0] = ql[0];
  2397. vl[1] = ql[4];
  2398. vh[0] = qh[0] >> bq8_offset;
  2399. vh[1] = qh[4] >> bq8_offset;
  2400. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2401. uint16_t aux[2];
  2402. const int j = bq8_offset/2;
  2403. if (j < 2) {
  2404. aux[0] = scales[j+0] & 0x3f3f;
  2405. aux[1] = scales[j+2] & 0x3f3f;
  2406. } else {
  2407. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2408. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2409. }
  2410. const uint8_t * sc = (const uint8_t *)aux;
  2411. const uint8_t * m = sc + 2;
  2412. #pragma unroll
  2413. for (int i = 0; i < QR5_K; ++i) {
  2414. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2415. d8[i] = __low2float(bq8i->ds);
  2416. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2417. u[2*i+0] = q8[0];
  2418. u[2*i+1] = q8[4];
  2419. }
  2420. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2421. #else
  2422. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2423. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2424. const int8_t * s = bq5_K->scales;
  2425. const float d = bq5_K->d;
  2426. const float d8_1 = __low2half(bq8_1[0].ds);
  2427. const float d8_2 = __low2half(bq8_1[1].ds);
  2428. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2429. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2430. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2431. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2432. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2433. const int vl1 = ql[0];
  2434. const int vl2 = ql[4];
  2435. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2436. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2437. const int in = step%8; // 0, 4, 0, 4
  2438. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2439. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2440. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2441. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2442. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2443. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2444. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2445. return d * sumf_d;
  2446. #else
  2447. assert(false);
  2448. return 0.0f; // only to satisfy the compiler
  2449. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2450. #endif
  2451. }
  2452. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2453. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2454. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2455. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2456. *x_ql = tile_x_ql;
  2457. *x_dm = tile_x_dm;
  2458. *x_sc = tile_x_sc;
  2459. }
  2460. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2461. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2462. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2463. GGML_CUDA_ASSUME(i_offset >= 0);
  2464. GGML_CUDA_ASSUME(i_offset < nwarps);
  2465. GGML_CUDA_ASSUME(k >= 0);
  2466. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2467. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2468. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2469. const block_q5_K * bx0 = (block_q5_K *) vx;
  2470. #pragma unroll
  2471. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2472. int i = i0 + i_offset;
  2473. if (need_check) {
  2474. i = min(i, i_max);
  2475. }
  2476. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2477. const int ky = QR5_K*kqsx;
  2478. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2479. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2480. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2481. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2482. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2483. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2484. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2485. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2486. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2487. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2488. }
  2489. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2490. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2491. #pragma unroll
  2492. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2493. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2494. if (need_check) {
  2495. i = min(i, i_max);
  2496. }
  2497. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2498. #if QK_K == 256
  2499. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2500. #endif
  2501. }
  2502. #pragma unroll
  2503. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2504. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2505. if (need_check) {
  2506. i = min(i, i_max);
  2507. }
  2508. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2509. const int * scales = (int *) bxi->scales;
  2510. const int ksc = k % (WARP_SIZE/8);
  2511. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2512. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2513. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2514. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2515. }
  2516. }
  2517. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2518. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2519. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2520. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2521. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2522. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2523. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2524. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2525. }
  2526. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2527. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2528. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2529. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2530. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2531. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2532. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2533. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2534. const int8_t * scales = bq6_K->scales + scale_offset;
  2535. int u[QR6_K];
  2536. float d8[QR6_K];
  2537. #pragma unroll
  2538. for (int i = 0; i < QR6_K; ++i) {
  2539. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2540. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2541. }
  2542. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2543. }
  2544. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2545. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2546. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2547. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2548. *x_ql = tile_x_ql;
  2549. *x_dm = tile_x_dm;
  2550. *x_sc = tile_x_sc;
  2551. }
  2552. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2553. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2554. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2555. GGML_CUDA_ASSUME(i_offset >= 0);
  2556. GGML_CUDA_ASSUME(i_offset < nwarps);
  2557. GGML_CUDA_ASSUME(k >= 0);
  2558. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2559. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2560. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2561. const block_q6_K * bx0 = (block_q6_K *) vx;
  2562. #pragma unroll
  2563. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2564. int i = i0 + i_offset;
  2565. if (need_check) {
  2566. i = min(i, i_max);
  2567. }
  2568. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2569. const int ky = QR6_K*kqsx;
  2570. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2571. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2572. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2573. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2574. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2575. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2576. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2577. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2578. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2579. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2580. }
  2581. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2582. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2583. float * x_dmf = (float *) x_dm;
  2584. #pragma unroll
  2585. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2586. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2587. if (need_check) {
  2588. i = min(i, i_max);
  2589. }
  2590. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2591. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2592. }
  2593. #pragma unroll
  2594. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2595. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2596. if (need_check) {
  2597. i = min(i, i_max);
  2598. }
  2599. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2600. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2601. }
  2602. }
  2603. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2604. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2605. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2606. const float * x_dmf = (const float *) x_dm;
  2607. const float * y_df = (const float *) y_ds;
  2608. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2609. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2610. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2611. 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]);
  2612. }
  2613. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2614. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2615. static __device__ __forceinline__ void mul_mat_q(
  2616. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2617. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2618. const block_q_t * x = (const block_q_t *) vx;
  2619. const block_q8_1 * y = (const block_q8_1 *) vy;
  2620. const int blocks_per_row_x = ncols_x / qk;
  2621. const int blocks_per_col_y = nrows_y / QK8_1;
  2622. const int blocks_per_warp = WARP_SIZE / qi;
  2623. const int & ncols_dst = ncols_y;
  2624. const int row_dst_0 = blockIdx.x*mmq_y;
  2625. const int & row_x_0 = row_dst_0;
  2626. const int col_dst_0 = blockIdx.y*mmq_x;
  2627. const int & col_y_0 = col_dst_0;
  2628. int * tile_x_ql = nullptr;
  2629. half2 * tile_x_dm = nullptr;
  2630. int * tile_x_qh = nullptr;
  2631. int * tile_x_sc = nullptr;
  2632. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2633. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2634. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2635. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {0.0f};
  2636. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2637. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2638. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2639. #pragma unroll
  2640. for (int ir = 0; ir < qr; ++ir) {
  2641. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2642. const int kbxd = kqs / QI8_1;
  2643. #pragma unroll
  2644. for (int i = 0; i < mmq_x; i += nwarps) {
  2645. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2646. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2647. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2648. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2649. }
  2650. #pragma unroll
  2651. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2652. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2653. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2654. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2655. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2656. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2657. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2658. if (need_sum) {
  2659. *dsi_dst = *dsi_src;
  2660. } else {
  2661. float * dfi_dst = (float *) dsi_dst;
  2662. *dfi_dst = __low2half(*dsi_src);
  2663. }
  2664. }
  2665. __syncthreads();
  2666. // #pragma unroll // unrolling this loop causes too much register pressure
  2667. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2668. #pragma unroll
  2669. for (int j = 0; j < mmq_x; j += nwarps) {
  2670. #pragma unroll
  2671. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2672. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  2673. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  2674. threadIdx.x + i, threadIdx.y + j, k);
  2675. }
  2676. }
  2677. }
  2678. __syncthreads();
  2679. }
  2680. }
  2681. #pragma unroll
  2682. for (int j = 0; j < mmq_x; j += nwarps) {
  2683. const int col_dst = col_dst_0 + j + threadIdx.y;
  2684. if (col_dst >= ncols_dst) {
  2685. return;
  2686. }
  2687. #pragma unroll
  2688. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  2689. const int row_dst = row_dst_0 + threadIdx.x + i;
  2690. if (row_dst >= nrows_dst) {
  2691. continue;
  2692. }
  2693. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  2694. }
  2695. }
  2696. }
  2697. #define MMQ_X_Q4_0_RDNA2 64
  2698. #define MMQ_Y_Q4_0_RDNA2 128
  2699. #define NWARPS_Q4_0_RDNA2 8
  2700. #define MMQ_X_Q4_0_RDNA1 64
  2701. #define MMQ_Y_Q4_0_RDNA1 64
  2702. #define NWARPS_Q4_0_RDNA1 8
  2703. #define MMQ_X_Q4_0_AMPERE 64
  2704. #define MMQ_Y_Q4_0_AMPERE 128
  2705. #define NWARPS_Q4_0_AMPERE 4
  2706. #define MMQ_X_Q4_0_PASCAL 64
  2707. #define MMQ_Y_Q4_0_PASCAL 64
  2708. #define NWARPS_Q4_0_PASCAL 8
  2709. template <bool need_check> static __global__ void
  2710. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2711. #if defined(RDNA3) || defined(RDNA2)
  2712. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  2713. #endif // defined(RDNA3) || defined(RDNA2)
  2714. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2715. mul_mat_q4_0(
  2716. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2717. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2718. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2719. #if defined(RDNA3) || defined(RDNA2)
  2720. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  2721. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  2722. const int nwarps = NWARPS_Q4_0_RDNA2;
  2723. #else
  2724. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  2725. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  2726. const int nwarps = NWARPS_Q4_0_RDNA1;
  2727. #endif // defined(RDNA3) || defined(RDNA2)
  2728. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2729. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2730. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2731. #elif __CUDA_ARCH__ >= CC_TURING
  2732. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  2733. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  2734. const int nwarps = NWARPS_Q4_0_AMPERE;
  2735. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2736. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2737. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2738. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2739. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  2740. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  2741. const int nwarps = NWARPS_Q4_0_PASCAL;
  2742. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  2743. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  2744. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2745. #else
  2746. (void) vec_dot_q4_0_q8_1_mul_mat;
  2747. assert(false);
  2748. #endif // __CUDA_ARCH__ >= CC_TURING
  2749. }
  2750. #define MMQ_X_Q4_1_RDNA2 64
  2751. #define MMQ_Y_Q4_1_RDNA2 128
  2752. #define NWARPS_Q4_1_RDNA2 8
  2753. #define MMQ_X_Q4_1_RDNA1 64
  2754. #define MMQ_Y_Q4_1_RDNA1 64
  2755. #define NWARPS_Q4_1_RDNA1 8
  2756. #define MMQ_X_Q4_1_AMPERE 64
  2757. #define MMQ_Y_Q4_1_AMPERE 128
  2758. #define NWARPS_Q4_1_AMPERE 4
  2759. #define MMQ_X_Q4_1_PASCAL 64
  2760. #define MMQ_Y_Q4_1_PASCAL 64
  2761. #define NWARPS_Q4_1_PASCAL 8
  2762. template <bool need_check> static __global__ void
  2763. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2764. #if defined(RDNA3) || defined(RDNA2)
  2765. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  2766. #endif // defined(RDNA3) || defined(RDNA2)
  2767. #elif __CUDA_ARCH__ < CC_TURING
  2768. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  2769. #endif // __CUDA_ARCH__ < CC_TURING
  2770. mul_mat_q4_1(
  2771. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2772. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2773. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2774. #if defined(RDNA3) || defined(RDNA2)
  2775. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  2776. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  2777. const int nwarps = NWARPS_Q4_1_RDNA2;
  2778. #else
  2779. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  2780. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  2781. const int nwarps = NWARPS_Q4_1_RDNA1;
  2782. #endif // defined(RDNA3) || defined(RDNA2)
  2783. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2784. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2785. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2786. #elif __CUDA_ARCH__ >= CC_TURING
  2787. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  2788. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  2789. const int nwarps = NWARPS_Q4_1_AMPERE;
  2790. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2791. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2792. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2793. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2794. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  2795. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  2796. const int nwarps = NWARPS_Q4_1_PASCAL;
  2797. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  2798. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  2799. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2800. #else
  2801. (void) vec_dot_q4_1_q8_1_mul_mat;
  2802. assert(false);
  2803. #endif // __CUDA_ARCH__ >= CC_TURING
  2804. }
  2805. #define MMQ_X_Q5_0_RDNA2 64
  2806. #define MMQ_Y_Q5_0_RDNA2 128
  2807. #define NWARPS_Q5_0_RDNA2 8
  2808. #define MMQ_X_Q5_0_RDNA1 64
  2809. #define MMQ_Y_Q5_0_RDNA1 64
  2810. #define NWARPS_Q5_0_RDNA1 8
  2811. #define MMQ_X_Q5_0_AMPERE 128
  2812. #define MMQ_Y_Q5_0_AMPERE 64
  2813. #define NWARPS_Q5_0_AMPERE 4
  2814. #define MMQ_X_Q5_0_PASCAL 64
  2815. #define MMQ_Y_Q5_0_PASCAL 64
  2816. #define NWARPS_Q5_0_PASCAL 8
  2817. template <bool need_check> static __global__ void
  2818. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2819. #if defined(RDNA3) || defined(RDNA2)
  2820. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  2821. #endif // defined(RDNA3) || defined(RDNA2)
  2822. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2823. mul_mat_q5_0(
  2824. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2825. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2826. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2827. #if defined(RDNA3) || defined(RDNA2)
  2828. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  2829. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  2830. const int nwarps = NWARPS_Q5_0_RDNA2;
  2831. #else
  2832. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  2833. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  2834. const int nwarps = NWARPS_Q5_0_RDNA1;
  2835. #endif // defined(RDNA3) || defined(RDNA2)
  2836. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2837. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2838. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2839. #elif __CUDA_ARCH__ >= CC_TURING
  2840. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  2841. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  2842. const int nwarps = NWARPS_Q5_0_AMPERE;
  2843. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2844. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2845. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2846. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2847. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  2848. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  2849. const int nwarps = NWARPS_Q5_0_PASCAL;
  2850. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  2851. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  2852. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2853. #else
  2854. (void) vec_dot_q5_0_q8_1_mul_mat;
  2855. assert(false);
  2856. #endif // __CUDA_ARCH__ >= CC_TURING
  2857. }
  2858. #define MMQ_X_Q5_1_RDNA2 64
  2859. #define MMQ_Y_Q5_1_RDNA2 128
  2860. #define NWARPS_Q5_1_RDNA2 8
  2861. #define MMQ_X_Q5_1_RDNA1 64
  2862. #define MMQ_Y_Q5_1_RDNA1 64
  2863. #define NWARPS_Q5_1_RDNA1 8
  2864. #define MMQ_X_Q5_1_AMPERE 128
  2865. #define MMQ_Y_Q5_1_AMPERE 64
  2866. #define NWARPS_Q5_1_AMPERE 4
  2867. #define MMQ_X_Q5_1_PASCAL 64
  2868. #define MMQ_Y_Q5_1_PASCAL 64
  2869. #define NWARPS_Q5_1_PASCAL 8
  2870. template <bool need_check> static __global__ void
  2871. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2872. #if defined(RDNA3) || defined(RDNA2)
  2873. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  2874. #endif // defined(RDNA3) || defined(RDNA2)
  2875. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2876. mul_mat_q5_1(
  2877. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2878. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2879. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2880. #if defined(RDNA3) || defined(RDNA2)
  2881. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  2882. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  2883. const int nwarps = NWARPS_Q5_1_RDNA2;
  2884. #else
  2885. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  2886. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  2887. const int nwarps = NWARPS_Q5_1_RDNA1;
  2888. #endif // defined(RDNA3) || defined(RDNA2)
  2889. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2890. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2891. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2892. #elif __CUDA_ARCH__ >= CC_TURING
  2893. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  2894. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  2895. const int nwarps = NWARPS_Q5_1_AMPERE;
  2896. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2897. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2898. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2899. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2900. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  2901. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  2902. const int nwarps = NWARPS_Q5_1_PASCAL;
  2903. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  2904. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  2905. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2906. #else
  2907. (void) vec_dot_q5_1_q8_1_mul_mat;
  2908. assert(false);
  2909. #endif // __CUDA_ARCH__ >= CC_TURING
  2910. }
  2911. #define MMQ_X_Q8_0_RDNA2 64
  2912. #define MMQ_Y_Q8_0_RDNA2 128
  2913. #define NWARPS_Q8_0_RDNA2 8
  2914. #define MMQ_X_Q8_0_RDNA1 64
  2915. #define MMQ_Y_Q8_0_RDNA1 64
  2916. #define NWARPS_Q8_0_RDNA1 8
  2917. #define MMQ_X_Q8_0_AMPERE 128
  2918. #define MMQ_Y_Q8_0_AMPERE 64
  2919. #define NWARPS_Q8_0_AMPERE 4
  2920. #define MMQ_X_Q8_0_PASCAL 64
  2921. #define MMQ_Y_Q8_0_PASCAL 64
  2922. #define NWARPS_Q8_0_PASCAL 8
  2923. template <bool need_check> static __global__ void
  2924. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2925. #if defined(RDNA3) || defined(RDNA2)
  2926. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  2927. #endif // defined(RDNA3) || defined(RDNA2)
  2928. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2929. mul_mat_q8_0(
  2930. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2931. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2932. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2933. #if defined(RDNA3) || defined(RDNA2)
  2934. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  2935. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  2936. const int nwarps = NWARPS_Q8_0_RDNA2;
  2937. #else
  2938. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  2939. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  2940. const int nwarps = NWARPS_Q8_0_RDNA1;
  2941. #endif // defined(RDNA3) || defined(RDNA2)
  2942. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  2943. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  2944. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2945. #elif __CUDA_ARCH__ >= CC_TURING
  2946. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  2947. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  2948. const int nwarps = NWARPS_Q8_0_AMPERE;
  2949. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  2950. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  2951. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2952. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  2953. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  2954. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  2955. const int nwarps = NWARPS_Q8_0_PASCAL;
  2956. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  2957. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  2958. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2959. #else
  2960. (void) vec_dot_q8_0_q8_1_mul_mat;
  2961. assert(false);
  2962. #endif // __CUDA_ARCH__ >= CC_TURING
  2963. }
  2964. #define MMQ_X_Q2_K_RDNA2 64
  2965. #define MMQ_Y_Q2_K_RDNA2 128
  2966. #define NWARPS_Q2_K_RDNA2 8
  2967. #define MMQ_X_Q2_K_RDNA1 128
  2968. #define MMQ_Y_Q2_K_RDNA1 32
  2969. #define NWARPS_Q2_K_RDNA1 8
  2970. #define MMQ_X_Q2_K_AMPERE 64
  2971. #define MMQ_Y_Q2_K_AMPERE 128
  2972. #define NWARPS_Q2_K_AMPERE 4
  2973. #define MMQ_X_Q2_K_PASCAL 64
  2974. #define MMQ_Y_Q2_K_PASCAL 64
  2975. #define NWARPS_Q2_K_PASCAL 8
  2976. template <bool need_check> static __global__ void
  2977. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2978. #if defined(RDNA3) || defined(RDNA2)
  2979. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  2980. #endif // defined(RDNA3) || defined(RDNA2)
  2981. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2982. mul_mat_q2_K(
  2983. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2984. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2985. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  2986. #if defined(RDNA3) || defined(RDNA2)
  2987. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  2988. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  2989. const int nwarps = NWARPS_Q2_K_RDNA2;
  2990. #else
  2991. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  2992. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  2993. const int nwarps = NWARPS_Q2_K_RDNA1;
  2994. #endif // defined(RDNA3) || defined(RDNA2)
  2995. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  2996. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  2997. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  2998. #elif __CUDA_ARCH__ >= CC_TURING
  2999. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3000. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3001. const int nwarps = NWARPS_Q2_K_AMPERE;
  3002. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3003. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3004. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3005. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3006. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3007. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3008. const int nwarps = NWARPS_Q2_K_PASCAL;
  3009. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3010. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3011. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3012. #else
  3013. (void) vec_dot_q2_K_q8_1_mul_mat;
  3014. assert(false);
  3015. #endif // __CUDA_ARCH__ >= CC_TURING
  3016. }
  3017. #define MMQ_X_Q3_K_RDNA2 128
  3018. #define MMQ_Y_Q3_K_RDNA2 64
  3019. #define NWARPS_Q3_K_RDNA2 8
  3020. #define MMQ_X_Q3_K_RDNA1 32
  3021. #define MMQ_Y_Q3_K_RDNA1 128
  3022. #define NWARPS_Q3_K_RDNA1 8
  3023. #define MMQ_X_Q3_K_AMPERE 128
  3024. #define MMQ_Y_Q3_K_AMPERE 128
  3025. #define NWARPS_Q3_K_AMPERE 4
  3026. #define MMQ_X_Q3_K_PASCAL 64
  3027. #define MMQ_Y_Q3_K_PASCAL 64
  3028. #define NWARPS_Q3_K_PASCAL 8
  3029. template <bool need_check> static __global__ void
  3030. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3031. #if defined(RDNA3) || defined(RDNA2)
  3032. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3033. #endif // defined(RDNA3) || defined(RDNA2)
  3034. #elif __CUDA_ARCH__ < CC_TURING
  3035. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3036. #endif // __CUDA_ARCH__ < CC_TURING
  3037. mul_mat_q3_K(
  3038. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3039. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3040. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3041. #if defined(RDNA3) || defined(RDNA2)
  3042. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3043. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3044. const int nwarps = NWARPS_Q3_K_RDNA2;
  3045. #else
  3046. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3047. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3048. const int nwarps = NWARPS_Q3_K_RDNA1;
  3049. #endif // defined(RDNA3) || defined(RDNA2)
  3050. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3051. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3052. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3053. #elif __CUDA_ARCH__ >= CC_TURING
  3054. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3055. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3056. const int nwarps = NWARPS_Q3_K_AMPERE;
  3057. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3058. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3059. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3060. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3061. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3062. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3063. const int nwarps = NWARPS_Q3_K_PASCAL;
  3064. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3065. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3066. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3067. #else
  3068. (void) vec_dot_q3_K_q8_1_mul_mat;
  3069. assert(false);
  3070. #endif // __CUDA_ARCH__ >= CC_TURING
  3071. }
  3072. #define MMQ_X_Q4_K_RDNA2 64
  3073. #define MMQ_Y_Q4_K_RDNA2 128
  3074. #define NWARPS_Q4_K_RDNA2 8
  3075. #define MMQ_X_Q4_K_RDNA1 32
  3076. #define MMQ_Y_Q4_K_RDNA1 64
  3077. #define NWARPS_Q4_K_RDNA1 8
  3078. #define MMQ_X_Q4_K_AMPERE 64
  3079. #define MMQ_Y_Q4_K_AMPERE 128
  3080. #define NWARPS_Q4_K_AMPERE 4
  3081. #define MMQ_X_Q4_K_PASCAL 64
  3082. #define MMQ_Y_Q4_K_PASCAL 64
  3083. #define NWARPS_Q4_K_PASCAL 8
  3084. template <bool need_check> static __global__ void
  3085. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3086. #if defined(RDNA3) || defined(RDNA2)
  3087. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3088. #endif // defined(RDNA3) || defined(RDNA2)
  3089. #elif __CUDA_ARCH__ < CC_TURING
  3090. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3091. #endif // __CUDA_ARCH__ < CC_TURING
  3092. mul_mat_q4_K(
  3093. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3094. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3095. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3096. #if defined(RDNA3) || defined(RDNA2)
  3097. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3098. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3099. const int nwarps = NWARPS_Q4_K_RDNA2;
  3100. #else
  3101. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3102. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3103. const int nwarps = NWARPS_Q4_K_RDNA1;
  3104. #endif // defined(RDNA3) || defined(RDNA2)
  3105. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3106. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3107. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3108. #elif __CUDA_ARCH__ >= CC_TURING
  3109. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3110. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3111. const int nwarps = NWARPS_Q4_K_AMPERE;
  3112. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3113. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3114. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3115. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3116. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3117. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3118. const int nwarps = NWARPS_Q4_K_PASCAL;
  3119. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3120. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3121. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3122. #else
  3123. (void) vec_dot_q4_K_q8_1_mul_mat;
  3124. assert(false);
  3125. #endif // __CUDA_ARCH__ >= CC_TURING
  3126. }
  3127. #define MMQ_X_Q5_K_RDNA2 64
  3128. #define MMQ_Y_Q5_K_RDNA2 128
  3129. #define NWARPS_Q5_K_RDNA2 8
  3130. #define MMQ_X_Q5_K_RDNA1 32
  3131. #define MMQ_Y_Q5_K_RDNA1 64
  3132. #define NWARPS_Q5_K_RDNA1 8
  3133. #define MMQ_X_Q5_K_AMPERE 64
  3134. #define MMQ_Y_Q5_K_AMPERE 128
  3135. #define NWARPS_Q5_K_AMPERE 4
  3136. #define MMQ_X_Q5_K_PASCAL 64
  3137. #define MMQ_Y_Q5_K_PASCAL 64
  3138. #define NWARPS_Q5_K_PASCAL 8
  3139. template <bool need_check> static __global__ void
  3140. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3141. #if defined(RDNA3) || defined(RDNA2)
  3142. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3143. #endif // defined(RDNA3) || defined(RDNA2)
  3144. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3145. mul_mat_q5_K(
  3146. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3147. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3148. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3149. #if defined(RDNA3) || defined(RDNA2)
  3150. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3151. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3152. const int nwarps = NWARPS_Q5_K_RDNA2;
  3153. #else
  3154. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3155. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3156. const int nwarps = NWARPS_Q5_K_RDNA1;
  3157. #endif // defined(RDNA3) || defined(RDNA2)
  3158. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3159. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3160. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3161. #elif __CUDA_ARCH__ >= CC_TURING
  3162. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3163. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3164. const int nwarps = NWARPS_Q5_K_AMPERE;
  3165. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3166. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3167. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3168. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3169. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3170. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3171. const int nwarps = NWARPS_Q5_K_PASCAL;
  3172. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3173. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3174. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3175. #else
  3176. (void) vec_dot_q5_K_q8_1_mul_mat;
  3177. assert(false);
  3178. #endif // __CUDA_ARCH__ >= CC_TURING
  3179. }
  3180. #define MMQ_X_Q6_K_RDNA2 64
  3181. #define MMQ_Y_Q6_K_RDNA2 128
  3182. #define NWARPS_Q6_K_RDNA2 8
  3183. #define MMQ_X_Q6_K_RDNA1 32
  3184. #define MMQ_Y_Q6_K_RDNA1 64
  3185. #define NWARPS_Q6_K_RDNA1 8
  3186. #define MMQ_X_Q6_K_AMPERE 64
  3187. #define MMQ_Y_Q6_K_AMPERE 64
  3188. #define NWARPS_Q6_K_AMPERE 4
  3189. #define MMQ_X_Q6_K_PASCAL 64
  3190. #define MMQ_Y_Q6_K_PASCAL 64
  3191. #define NWARPS_Q6_K_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_Q6_K_RDNA2, 2)
  3196. #endif // defined(RDNA3) || defined(RDNA2)
  3197. #elif __CUDA_ARCH__ < CC_TURING
  3198. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3199. #endif // __CUDA_ARCH__ < CC_TURING
  3200. mul_mat_q6_K(
  3201. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3202. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3203. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3204. #if defined(RDNA3) || defined(RDNA2)
  3205. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3206. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3207. const int nwarps = NWARPS_Q6_K_RDNA2;
  3208. #else
  3209. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3210. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3211. const int nwarps = NWARPS_Q6_K_RDNA1;
  3212. #endif // defined(RDNA3) || defined(RDNA2)
  3213. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3214. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3215. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3216. #elif __CUDA_ARCH__ >= CC_TURING
  3217. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3218. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3219. const int nwarps = NWARPS_Q6_K_AMPERE;
  3220. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3221. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3222. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3223. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3224. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3225. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3226. const int nwarps = NWARPS_Q6_K_PASCAL;
  3227. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3228. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3229. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3230. #else
  3231. (void) vec_dot_q6_K_q8_1_mul_mat;
  3232. assert(false);
  3233. #endif // __CUDA_ARCH__ >= CC_TURING
  3234. }
  3235. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3236. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3237. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  3238. if (row >= nrows) {
  3239. return;
  3240. }
  3241. const int blocks_per_row = ncols / qk;
  3242. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3243. // partial sum for each thread
  3244. float tmp = 0.0f;
  3245. const block_q_t * x = (const block_q_t *) vx;
  3246. const block_q8_1 * y = (const block_q8_1 *) vy;
  3247. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3248. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3249. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3250. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3251. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3252. }
  3253. // sum up partial sums and write back result
  3254. #pragma unroll
  3255. for (int mask = 16; mask > 0; mask >>= 1) {
  3256. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3257. }
  3258. if (threadIdx.x == 0) {
  3259. dst[row] = tmp;
  3260. }
  3261. }
  3262. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3263. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3264. // qk = quantized weights per x block
  3265. // qr = number of quantized weights per data value in x block
  3266. const int row = blockIdx.y*blockDim.y + threadIdx.y;
  3267. if (row >= nrows) {
  3268. return;
  3269. }
  3270. const int tid = threadIdx.x;
  3271. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3272. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3273. const int y_offset = qr == 1 ? 1 : qk/2;
  3274. // partial sum for each thread
  3275. #ifdef GGML_CUDA_F16
  3276. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3277. #else
  3278. float tmp = 0.0f;
  3279. #endif // GGML_CUDA_F16
  3280. for (int i = 0; i < ncols; i += iter_stride) {
  3281. const int col = i + vals_per_iter*tid;
  3282. const int ib = (row*ncols + col)/qk; // x block index
  3283. const int iqs = (col%qk)/qr; // x quant index
  3284. const int iybs = col - col%qk; // y block start index
  3285. // processing >2 values per i iter is faster for fast GPUs
  3286. #pragma unroll
  3287. for (int j = 0; j < vals_per_iter; j += 2) {
  3288. // process 2 vals per j iter
  3289. // dequantize
  3290. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3291. dfloat2 v;
  3292. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3293. // matrix multiplication
  3294. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3295. #ifdef GGML_CUDA_F16
  3296. tmp += __hmul2(v, {
  3297. y[iybs + iqs + j/qr + 0],
  3298. y[iybs + iqs + j/qr + y_offset]
  3299. });
  3300. #else
  3301. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3302. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3303. #endif // GGML_CUDA_F16
  3304. }
  3305. }
  3306. // sum up partial sums and write back result
  3307. #pragma unroll
  3308. for (int mask = 16; mask > 0; mask >>= 1) {
  3309. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3310. }
  3311. if (tid == 0) {
  3312. #ifdef GGML_CUDA_F16
  3313. dst[row] = tmp.x + tmp.y;
  3314. #else
  3315. dst[row] = tmp;
  3316. #endif // GGML_CUDA_F16
  3317. }
  3318. }
  3319. static __global__ void mul_mat_p021_f16_f32(
  3320. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3321. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3322. const half * x = (const half *) vx;
  3323. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3324. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3325. const int channel_x = channel / (nchannels_y / nchannels_x);
  3326. const int nrows_y = ncols_x;
  3327. const int nrows_dst = nrows_x;
  3328. const int row_dst = row_x;
  3329. float tmp = 0.0f;
  3330. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3331. const int col_x = col_x0 + threadIdx.x;
  3332. if (col_x >= ncols_x) {
  3333. break;
  3334. }
  3335. // x is transposed and permuted
  3336. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3337. const float xi = __half2float(x[ix]);
  3338. const int row_y = col_x;
  3339. // y is not transposed but permuted
  3340. const int iy = channel*nrows_y + row_y;
  3341. tmp += xi * y[iy];
  3342. }
  3343. // dst is not transposed and not permuted
  3344. const int idst = channel*nrows_dst + row_dst;
  3345. // sum up partial sums and write back result
  3346. #pragma unroll
  3347. for (int mask = 16; mask > 0; mask >>= 1) {
  3348. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3349. }
  3350. if (threadIdx.x == 0) {
  3351. dst[idst] = tmp;
  3352. }
  3353. }
  3354. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3355. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3356. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3357. const half * x = (const half *) vx;
  3358. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3359. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3360. const int channel_x = channel / channel_x_divisor;
  3361. const int nrows_y = ncols_x;
  3362. const int nrows_dst = nrows_x;
  3363. const int row_dst = row_x;
  3364. const int idst = channel*nrows_dst + row_dst;
  3365. float tmp = 0.0f;
  3366. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3367. const int col_x = col_x0 + threadIdx.x;
  3368. if (col_x >= ncols_x) {
  3369. break;
  3370. }
  3371. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3372. const float xi = __half2float(x[ix]);
  3373. const int row_y = col_x;
  3374. const int iy = channel*nrows_y + row_y;
  3375. tmp += xi * y[iy];
  3376. }
  3377. // sum up partial sums and write back result
  3378. #pragma unroll
  3379. for (int mask = 16; mask > 0; mask >>= 1) {
  3380. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3381. }
  3382. if (threadIdx.x == 0) {
  3383. dst[idst] = tmp;
  3384. }
  3385. }
  3386. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3387. const float * xi = (const float *) cxi;
  3388. float * dsti = (float *) cdsti;
  3389. *dsti = *xi;
  3390. }
  3391. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3392. const float * xi = (const float *) cxi;
  3393. half * dsti = (half *) cdsti;
  3394. *dsti = __float2half(*xi);
  3395. }
  3396. template <cpy_kernel_t cpy_1>
  3397. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3398. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3399. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3400. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3401. if (i >= ne) {
  3402. return;
  3403. }
  3404. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3405. // then combine those indices with the corresponding byte offsets to get the total offsets
  3406. const int i02 = i / (ne00*ne01);
  3407. const int i01 = (i - i02*ne01*ne00) / ne00;
  3408. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3409. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3410. const int i12 = i / (ne10*ne11);
  3411. const int i11 = (i - i12*ne10*ne11) / ne10;
  3412. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3413. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3414. cpy_1(cx + x_offset, cdst + dst_offset);
  3415. }
  3416. // rope == RoPE == rotary positional embedding
  3417. static __global__ void rope_f32(const float * x, float * dst, const int ncols, const float p0,
  3418. const float p_delta, const int p_delta_rows, const float theta_scale) {
  3419. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3420. if (col >= ncols) {
  3421. return;
  3422. }
  3423. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3424. const int i = row*ncols + col;
  3425. const float theta = (p0 + p_delta * (row/p_delta_rows))*powf(theta_scale, col/2);
  3426. const float sin_theta = sinf(theta);
  3427. const float cos_theta = cosf(theta);
  3428. const float x0 = x[i + 0];
  3429. const float x1 = x[i + 1];
  3430. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3431. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3432. }
  3433. static __global__ void rope_neox_f32(const float * x, float * dst, const int ncols, const float p0,
  3434. const float p_delta, const int p_delta_rows, const float theta_scale) {
  3435. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3436. if (col >= ncols) {
  3437. return;
  3438. }
  3439. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3440. const int i = row*ncols + col/2;
  3441. const float theta = (p0 + p_delta * (row/p_delta_rows))*powf(theta_scale, col/2);
  3442. const float sin_theta = sinf(theta);
  3443. const float cos_theta = cosf(theta);
  3444. const float x0 = x[i + 0];
  3445. const float x1 = x[i + ncols/2];
  3446. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3447. dst[i + ncols/2] = x0*sin_theta + x1*cos_theta;
  3448. }
  3449. static __global__ void rope_glm_f32(const float * x, float * dst, const int ncols, const float p0,
  3450. const float p_delta, const int p_delta_rows, const float theta_scale, const int n_ctx) {
  3451. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3452. const int half_n_dims = ncols/4;
  3453. if (col >= half_n_dims) {
  3454. return;
  3455. }
  3456. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3457. const int i = row*ncols + col;
  3458. const float col_theta_scale = powf(theta_scale, col);
  3459. const float p = p0 + p_delta*(row/p_delta_rows);
  3460. const float theta = min(p, p_delta*(n_ctx - 2))*col_theta_scale;
  3461. const float sin_theta = sinf(theta);
  3462. const float cos_theta = cosf(theta);
  3463. const float x0 = x[i + 0];
  3464. const float x1 = x[i + half_n_dims];
  3465. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3466. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  3467. const float block_theta = max(p - p_delta*(n_ctx - 2), 0.f)*col_theta_scale;
  3468. const float sin_block_theta = sinf(block_theta);
  3469. const float cos_block_theta = cosf(block_theta);
  3470. const float x2 = x[i + half_n_dims * 2];
  3471. const float x3 = x[i + half_n_dims * 3];
  3472. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  3473. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  3474. }
  3475. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  3476. const int n_heads_log2_floor, const float m0, const float m1) {
  3477. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3478. if (col >= ncols) {
  3479. return;
  3480. }
  3481. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3482. const int i = row*ncols + col;
  3483. const int k = row/k_rows;
  3484. float m_k;
  3485. if (k < n_heads_log2_floor) {
  3486. m_k = powf(m0, k + 1);
  3487. } else {
  3488. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  3489. }
  3490. dst[i] = col * m_k + x[i];
  3491. }
  3492. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  3493. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  3494. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3495. if (col >= ncols) {
  3496. return;
  3497. }
  3498. const int i = row*ncols + col;
  3499. // dst[i] = col > n_past + row ? -INFINITY : x[i];
  3500. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  3501. }
  3502. // the CUDA soft max implementation differs from the CPU implementation
  3503. // instead of doubles floats are used
  3504. static __global__ void soft_max_f32(const float * x, float * dst, const int ncols) {
  3505. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3506. const int block_size = blockDim.y;
  3507. const int tid = threadIdx.y;
  3508. float max_val = -INFINITY;
  3509. for (int col = tid; col < ncols; col += block_size) {
  3510. const int i = row*ncols + col;
  3511. max_val = max(max_val, x[i]);
  3512. }
  3513. // find the max value in the block
  3514. #pragma unroll
  3515. for (int mask = 16; mask > 0; mask >>= 1) {
  3516. max_val = max(max_val, __shfl_xor_sync(0xffffffff, max_val, mask, 32));
  3517. }
  3518. float tmp = 0.f;
  3519. for (int col = tid; col < ncols; col += block_size) {
  3520. const int i = row*ncols + col;
  3521. const float val = expf(x[i] - max_val);
  3522. tmp += val;
  3523. dst[i] = val;
  3524. }
  3525. // sum up partial sums
  3526. #pragma unroll
  3527. for (int mask = 16; mask > 0; mask >>= 1) {
  3528. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3529. }
  3530. const float inv_tmp = 1.f / tmp;
  3531. for (int col = tid; col < ncols; col += block_size) {
  3532. const int i = row*ncols + col;
  3533. dst[i] *= inv_tmp;
  3534. }
  3535. }
  3536. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  3537. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3538. if (i >= k) {
  3539. return;
  3540. }
  3541. dst[i] = scale * x[i];
  3542. }
  3543. static void add_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  3544. const int num_blocks = (kx + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3545. add_f32<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  3546. }
  3547. static void add_f16_f32_f16_cuda(const half * x, const float * y, half * dst, const int k, cudaStream_t stream) {
  3548. const int num_blocks = (k + CUDA_ADD_BLOCK_SIZE - 1) / CUDA_ADD_BLOCK_SIZE;
  3549. add_f16_f32_f16<<<num_blocks, CUDA_ADD_BLOCK_SIZE, 0, stream>>>(x, y, dst, k);
  3550. }
  3551. static void mul_f32_cuda(const float * x, const float * y, float * dst, const int kx, const int ky, cudaStream_t stream) {
  3552. const int num_blocks = (kx + CUDA_MUL_BLOCK_SIZE - 1) / CUDA_MUL_BLOCK_SIZE;
  3553. mul_f32<<<num_blocks, CUDA_MUL_BLOCK_SIZE, 0, stream>>>(x, y, dst, kx, ky);
  3554. }
  3555. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3556. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  3557. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3558. }
  3559. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  3560. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  3561. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  3562. }
  3563. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  3564. GGML_ASSERT(ncols % WARP_SIZE == 0);
  3565. if (ncols < 1024) {
  3566. const dim3 block_dims(WARP_SIZE, 1, 1);
  3567. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  3568. } else {
  3569. const dim3 block_dims(1024, 1, 1);
  3570. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols);
  3571. }
  3572. }
  3573. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  3574. GGML_ASSERT(ncols % WARP_SIZE == 0);
  3575. if (ncols < 1024) {
  3576. const dim3 block_dims(WARP_SIZE, 1, 1);
  3577. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  3578. } else {
  3579. const dim3 block_dims(1024, 1, 1);
  3580. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  3581. }
  3582. }
  3583. 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) {
  3584. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  3585. const dim3 num_blocks(block_num_x, ky, 1);
  3586. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  3587. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  3588. }
  3589. static void dequantize_row_q4_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3590. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3591. dequantize_block<QK4_0, QR4_0, dequantize_q4_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3592. }
  3593. static void dequantize_row_q4_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3594. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3595. dequantize_block<QK4_1, QR4_1, dequantize_q4_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3596. }
  3597. static void dequantize_row_q5_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3598. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3599. dequantize_block<QK5_0, QR5_0, dequantize_q5_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3600. }
  3601. static void dequantize_row_q5_1_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3602. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3603. dequantize_block<QK5_1, QR5_1, dequantize_q5_1><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3604. }
  3605. static void dequantize_row_q8_0_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3606. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3607. dequantize_block<QK8_0, QR8_0, dequantize_q8_0><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3608. }
  3609. static void dequantize_row_q2_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3610. const int nb = k / QK_K;
  3611. #if QK_K == 256
  3612. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  3613. #else
  3614. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  3615. #endif
  3616. }
  3617. static void dequantize_row_q3_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3618. const int nb = k / QK_K;
  3619. #if QK_K == 256
  3620. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  3621. #else
  3622. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  3623. #endif
  3624. }
  3625. static void dequantize_row_q4_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3626. const int nb = k / QK_K;
  3627. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  3628. }
  3629. static void dequantize_row_q5_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3630. const int nb = k / QK_K;
  3631. #if QK_K == 256
  3632. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  3633. #else
  3634. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  3635. #endif
  3636. }
  3637. static void dequantize_row_q6_K_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3638. const int nb = k / QK_K;
  3639. #if QK_K == 256
  3640. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  3641. #else
  3642. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  3643. #endif
  3644. }
  3645. 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) {
  3646. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3647. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3648. const dim3 block_nums(1, block_num_y, 1);
  3649. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3650. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  3651. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3652. }
  3653. 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) {
  3654. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3655. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3656. const dim3 block_nums(1, block_num_y, 1);
  3657. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3658. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  3659. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3660. }
  3661. 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) {
  3662. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3663. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3664. const dim3 block_nums(1, block_num_y, 1);
  3665. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3666. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  3667. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3668. }
  3669. 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) {
  3670. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3671. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3672. const dim3 block_nums(1, block_num_y, 1);
  3673. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3674. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  3675. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3676. }
  3677. 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) {
  3678. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3679. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3680. const dim3 block_nums(1, block_num_y, 1);
  3681. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3682. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  3683. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3684. }
  3685. 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) {
  3686. GGML_ASSERT(ncols % QK_K == 0);
  3687. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  3688. const int block_num_y = (nrows + ny - 1) / ny;
  3689. const dim3 block_nums(1, block_num_y, 1);
  3690. const dim3 block_dims(32, ny, 1);
  3691. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3692. }
  3693. 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) {
  3694. GGML_ASSERT(ncols % QK_K == 0);
  3695. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3696. const int block_num_y = (nrows + ny - 1) / ny;
  3697. const dim3 block_nums(1, block_num_y, 1);
  3698. const dim3 block_dims(32, ny, 1);
  3699. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3700. }
  3701. 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) {
  3702. GGML_ASSERT(ncols % QK_K == 0);
  3703. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3704. const int block_num_y = (nrows + ny - 1) / ny;
  3705. const dim3 block_nums(1, block_num_y, 1);
  3706. const dim3 block_dims(32, ny, 1);
  3707. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3708. }
  3709. 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) {
  3710. GGML_ASSERT(ncols % QK_K == 0);
  3711. const dim3 block_dims(32, 1, 1);
  3712. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  3713. }
  3714. 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) {
  3715. GGML_ASSERT(ncols % QK_K == 0);
  3716. const int ny = 2 / K_QUANTS_PER_ITERATION;
  3717. const int block_num_y = (nrows + ny - 1) / ny;
  3718. const dim3 block_nums(1, block_num_y, 1);
  3719. const dim3 block_dims(32, ny, 1);
  3720. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3721. }
  3722. 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) {
  3723. GGML_ASSERT(ncols % QK4_0 == 0);
  3724. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3725. const dim3 block_nums(1, block_num_y, 1);
  3726. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3727. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  3728. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3729. }
  3730. 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) {
  3731. GGML_ASSERT(ncols % QK4_1 == 0);
  3732. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3733. const dim3 block_nums(1, block_num_y, 1);
  3734. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3735. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  3736. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3737. }
  3738. 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) {
  3739. GGML_ASSERT(ncols % QK5_0 == 0);
  3740. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3741. const dim3 block_nums(1, block_num_y, 1);
  3742. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3743. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  3744. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3745. }
  3746. 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) {
  3747. GGML_ASSERT(ncols % QK5_1 == 0);
  3748. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3749. const dim3 block_nums(1, block_num_y, 1);
  3750. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3751. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  3752. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3753. }
  3754. 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) {
  3755. GGML_ASSERT(ncols % QK8_0 == 0);
  3756. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3757. const dim3 block_nums(1, block_num_y, 1);
  3758. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3759. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  3760. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3761. }
  3762. 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) {
  3763. GGML_ASSERT(ncols % QK_K == 0);
  3764. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3765. const dim3 block_nums(1, block_num_y, 1);
  3766. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3767. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  3768. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3769. }
  3770. 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) {
  3771. GGML_ASSERT(ncols % QK_K == 0);
  3772. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3773. const dim3 block_nums(1, block_num_y, 1);
  3774. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3775. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  3776. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3777. }
  3778. 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) {
  3779. GGML_ASSERT(ncols % QK_K == 0);
  3780. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3781. const dim3 block_nums(1, block_num_y, 1);
  3782. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3783. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  3784. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3785. }
  3786. 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) {
  3787. GGML_ASSERT(ncols % QK_K == 0);
  3788. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3789. const dim3 block_nums(1, block_num_y, 1);
  3790. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3791. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  3792. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3793. }
  3794. 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) {
  3795. GGML_ASSERT(ncols % QK_K == 0);
  3796. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3797. const dim3 block_nums(1, block_num_y, 1);
  3798. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3799. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  3800. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  3801. }
  3802. static void convert_fp16_to_fp32_cuda(const void * vx, float * y, const int k, cudaStream_t stream) {
  3803. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  3804. dequantize_block<1, 1, convert_f16><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3805. }
  3806. static void convert_fp32_to_fp16_cuda(const void * vx, half * y, const int k, cudaStream_t stream) {
  3807. const int num_blocks = (k + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  3808. dequantize_block<1, 1, convert_f32><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  3809. }
  3810. 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) {
  3811. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  3812. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  3813. const dim3 block_nums(1, block_num_y, 1);
  3814. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  3815. dequantize_mul_mat_vec<1, 1, convert_f16>
  3816. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  3817. }
  3818. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  3819. switch (type) {
  3820. case GGML_TYPE_F32:
  3821. return convert_fp32_to_fp16_cuda;
  3822. default:
  3823. return nullptr;
  3824. }
  3825. }
  3826. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  3827. switch (type) {
  3828. case GGML_TYPE_Q4_0:
  3829. return dequantize_row_q4_0_cuda;
  3830. case GGML_TYPE_Q4_1:
  3831. return dequantize_row_q4_1_cuda;
  3832. case GGML_TYPE_Q5_0:
  3833. return dequantize_row_q5_0_cuda;
  3834. case GGML_TYPE_Q5_1:
  3835. return dequantize_row_q5_1_cuda;
  3836. case GGML_TYPE_Q8_0:
  3837. return dequantize_row_q8_0_cuda;
  3838. case GGML_TYPE_Q2_K:
  3839. return dequantize_row_q2_K_cuda;
  3840. case GGML_TYPE_Q3_K:
  3841. return dequantize_row_q3_K_cuda;
  3842. case GGML_TYPE_Q4_K:
  3843. return dequantize_row_q4_K_cuda;
  3844. case GGML_TYPE_Q5_K:
  3845. return dequantize_row_q5_K_cuda;
  3846. case GGML_TYPE_Q6_K:
  3847. return dequantize_row_q6_K_cuda;
  3848. case GGML_TYPE_F16:
  3849. return convert_fp16_to_fp32_cuda;
  3850. default:
  3851. return nullptr;
  3852. }
  3853. }
  3854. static void ggml_mul_mat_q4_0_q8_1_cuda(
  3855. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3856. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3857. int id;
  3858. CUDA_CHECK(cudaGetDevice(&id));
  3859. const int compute_capability = g_compute_capabilities[id];
  3860. int mmq_x, mmq_y, nwarps;
  3861. if (compute_capability >= CC_RDNA2) {
  3862. mmq_x = MMQ_X_Q4_0_RDNA2;
  3863. mmq_y = MMQ_Y_Q4_0_RDNA2;
  3864. nwarps = NWARPS_Q4_0_RDNA2;
  3865. } else if (compute_capability >= CC_OFFSET_AMD) {
  3866. mmq_x = MMQ_X_Q4_0_RDNA1;
  3867. mmq_y = MMQ_Y_Q4_0_RDNA1;
  3868. nwarps = NWARPS_Q4_0_RDNA1;
  3869. } else if (compute_capability >= CC_TURING) {
  3870. mmq_x = MMQ_X_Q4_0_AMPERE;
  3871. mmq_y = MMQ_Y_Q4_0_AMPERE;
  3872. nwarps = NWARPS_Q4_0_AMPERE;
  3873. } else if (compute_capability >= MIN_CC_DP4A) {
  3874. mmq_x = MMQ_X_Q4_0_PASCAL;
  3875. mmq_y = MMQ_Y_Q4_0_PASCAL;
  3876. nwarps = NWARPS_Q4_0_PASCAL;
  3877. } else {
  3878. GGML_ASSERT(false);
  3879. }
  3880. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3881. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3882. const dim3 block_nums(block_num_x, block_num_y, 1);
  3883. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3884. if (nrows_x % mmq_y == 0) {
  3885. const bool need_check = false;
  3886. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3887. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3888. } else {
  3889. const bool need_check = true;
  3890. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3891. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3892. }
  3893. }
  3894. static void ggml_mul_mat_q4_1_q8_1_cuda(
  3895. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3896. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3897. int id;
  3898. CUDA_CHECK(cudaGetDevice(&id));
  3899. const int compute_capability = g_compute_capabilities[id];
  3900. int mmq_x, mmq_y, nwarps;
  3901. if (compute_capability >= CC_RDNA2) {
  3902. mmq_x = MMQ_X_Q4_1_RDNA2;
  3903. mmq_y = MMQ_Y_Q4_1_RDNA2;
  3904. nwarps = NWARPS_Q4_1_RDNA2;
  3905. } else if (compute_capability >= CC_OFFSET_AMD) {
  3906. mmq_x = MMQ_X_Q4_1_RDNA1;
  3907. mmq_y = MMQ_Y_Q4_1_RDNA1;
  3908. nwarps = NWARPS_Q4_1_RDNA1;
  3909. } else if (compute_capability >= CC_TURING) {
  3910. mmq_x = MMQ_X_Q4_1_AMPERE;
  3911. mmq_y = MMQ_Y_Q4_1_AMPERE;
  3912. nwarps = NWARPS_Q4_1_AMPERE;
  3913. } else if (compute_capability >= MIN_CC_DP4A) {
  3914. mmq_x = MMQ_X_Q4_1_PASCAL;
  3915. mmq_y = MMQ_Y_Q4_1_PASCAL;
  3916. nwarps = NWARPS_Q4_1_PASCAL;
  3917. } else {
  3918. GGML_ASSERT(false);
  3919. }
  3920. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3921. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3922. const dim3 block_nums(block_num_x, block_num_y, 1);
  3923. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3924. if (nrows_x % mmq_y == 0) {
  3925. const bool need_check = false;
  3926. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  3927. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3928. } else {
  3929. const bool need_check = true;
  3930. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  3931. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3932. }
  3933. }
  3934. static void ggml_mul_mat_q5_0_q8_1_cuda(
  3935. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3936. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3937. int id;
  3938. CUDA_CHECK(cudaGetDevice(&id));
  3939. const int compute_capability = g_compute_capabilities[id];
  3940. int mmq_x, mmq_y, nwarps;
  3941. if (compute_capability >= CC_RDNA2) {
  3942. mmq_x = MMQ_X_Q5_0_RDNA2;
  3943. mmq_y = MMQ_Y_Q5_0_RDNA2;
  3944. nwarps = NWARPS_Q5_0_RDNA2;
  3945. } else if (compute_capability >= CC_OFFSET_AMD) {
  3946. mmq_x = MMQ_X_Q5_0_RDNA1;
  3947. mmq_y = MMQ_Y_Q5_0_RDNA1;
  3948. nwarps = NWARPS_Q5_0_RDNA1;
  3949. } else if (compute_capability >= CC_TURING) {
  3950. mmq_x = MMQ_X_Q5_0_AMPERE;
  3951. mmq_y = MMQ_Y_Q5_0_AMPERE;
  3952. nwarps = NWARPS_Q5_0_AMPERE;
  3953. } else if (compute_capability >= MIN_CC_DP4A) {
  3954. mmq_x = MMQ_X_Q5_0_PASCAL;
  3955. mmq_y = MMQ_Y_Q5_0_PASCAL;
  3956. nwarps = NWARPS_Q5_0_PASCAL;
  3957. } else {
  3958. GGML_ASSERT(false);
  3959. }
  3960. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  3961. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  3962. const dim3 block_nums(block_num_x, block_num_y, 1);
  3963. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  3964. if (nrows_x % mmq_y == 0) {
  3965. const bool need_check = false;
  3966. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3967. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3968. } else {
  3969. const bool need_check = true;
  3970. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  3971. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3972. }
  3973. }
  3974. static void ggml_mul_mat_q5_1_q8_1_cuda(
  3975. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  3976. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  3977. int id;
  3978. CUDA_CHECK(cudaGetDevice(&id));
  3979. const int compute_capability = g_compute_capabilities[id];
  3980. int mmq_x, mmq_y, nwarps;
  3981. if (compute_capability >= CC_RDNA2) {
  3982. mmq_x = MMQ_X_Q5_1_RDNA2;
  3983. mmq_y = MMQ_Y_Q5_1_RDNA2;
  3984. nwarps = NWARPS_Q5_1_RDNA2;
  3985. } else if (compute_capability >= CC_OFFSET_AMD) {
  3986. mmq_x = MMQ_X_Q5_1_RDNA1;
  3987. mmq_y = MMQ_Y_Q5_1_RDNA1;
  3988. nwarps = NWARPS_Q5_1_RDNA1;
  3989. } else if (compute_capability >= CC_TURING) {
  3990. mmq_x = MMQ_X_Q5_1_AMPERE;
  3991. mmq_y = MMQ_Y_Q5_1_AMPERE;
  3992. nwarps = NWARPS_Q5_1_AMPERE;
  3993. } else if (compute_capability >= MIN_CC_DP4A) {
  3994. mmq_x = MMQ_X_Q5_1_PASCAL;
  3995. mmq_y = MMQ_Y_Q5_1_PASCAL;
  3996. nwarps = NWARPS_Q5_1_PASCAL;
  3997. } else {
  3998. GGML_ASSERT(false);
  3999. }
  4000. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4001. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4002. const dim3 block_nums(block_num_x, block_num_y, 1);
  4003. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4004. if (nrows_x % mmq_y == 0) {
  4005. const bool need_check = false;
  4006. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4007. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4008. } else {
  4009. const bool need_check = true;
  4010. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4011. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4012. }
  4013. }
  4014. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4015. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4016. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4017. int id;
  4018. CUDA_CHECK(cudaGetDevice(&id));
  4019. const int compute_capability = g_compute_capabilities[id];
  4020. int mmq_x, mmq_y, nwarps;
  4021. if (compute_capability >= CC_RDNA2) {
  4022. mmq_x = MMQ_X_Q8_0_RDNA2;
  4023. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4024. nwarps = NWARPS_Q8_0_RDNA2;
  4025. } else if (compute_capability >= CC_OFFSET_AMD) {
  4026. mmq_x = MMQ_X_Q8_0_RDNA1;
  4027. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4028. nwarps = NWARPS_Q8_0_RDNA1;
  4029. } else if (compute_capability >= CC_TURING) {
  4030. mmq_x = MMQ_X_Q8_0_AMPERE;
  4031. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4032. nwarps = NWARPS_Q8_0_AMPERE;
  4033. } else if (compute_capability >= MIN_CC_DP4A) {
  4034. mmq_x = MMQ_X_Q8_0_PASCAL;
  4035. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4036. nwarps = NWARPS_Q8_0_PASCAL;
  4037. } else {
  4038. GGML_ASSERT(false);
  4039. }
  4040. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4041. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4042. const dim3 block_nums(block_num_x, block_num_y, 1);
  4043. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4044. if (nrows_x % mmq_y == 0) {
  4045. const bool need_check = false;
  4046. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4047. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4048. } else {
  4049. const bool need_check = true;
  4050. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4051. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4052. }
  4053. }
  4054. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4055. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4056. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4057. int id;
  4058. CUDA_CHECK(cudaGetDevice(&id));
  4059. const int compute_capability = g_compute_capabilities[id];
  4060. int mmq_x, mmq_y, nwarps;
  4061. if (compute_capability >= CC_RDNA2) {
  4062. mmq_x = MMQ_X_Q2_K_RDNA2;
  4063. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4064. nwarps = NWARPS_Q2_K_RDNA2;
  4065. } else if (compute_capability >= CC_OFFSET_AMD) {
  4066. mmq_x = MMQ_X_Q2_K_RDNA1;
  4067. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4068. nwarps = NWARPS_Q2_K_RDNA1;
  4069. } else if (compute_capability >= CC_TURING) {
  4070. mmq_x = MMQ_X_Q2_K_AMPERE;
  4071. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4072. nwarps = NWARPS_Q2_K_AMPERE;
  4073. } else if (compute_capability >= MIN_CC_DP4A) {
  4074. mmq_x = MMQ_X_Q2_K_PASCAL;
  4075. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4076. nwarps = NWARPS_Q2_K_PASCAL;
  4077. } else {
  4078. GGML_ASSERT(false);
  4079. }
  4080. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4081. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4082. const dim3 block_nums(block_num_x, block_num_y, 1);
  4083. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4084. if (nrows_x % mmq_y == 0) {
  4085. const bool need_check = false;
  4086. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4087. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4088. } else {
  4089. const bool need_check = true;
  4090. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4091. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4092. }
  4093. }
  4094. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4095. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4096. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4097. #if QK_K == 256
  4098. int id;
  4099. CUDA_CHECK(cudaGetDevice(&id));
  4100. const int compute_capability = g_compute_capabilities[id];
  4101. int mmq_x, mmq_y, nwarps;
  4102. if (compute_capability >= CC_RDNA2) {
  4103. mmq_x = MMQ_X_Q3_K_RDNA2;
  4104. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4105. nwarps = NWARPS_Q3_K_RDNA2;
  4106. } else if (compute_capability >= CC_OFFSET_AMD) {
  4107. mmq_x = MMQ_X_Q3_K_RDNA1;
  4108. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4109. nwarps = NWARPS_Q3_K_RDNA1;
  4110. } else if (compute_capability >= CC_TURING) {
  4111. mmq_x = MMQ_X_Q3_K_AMPERE;
  4112. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4113. nwarps = NWARPS_Q3_K_AMPERE;
  4114. } else if (compute_capability >= MIN_CC_DP4A) {
  4115. mmq_x = MMQ_X_Q3_K_PASCAL;
  4116. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4117. nwarps = NWARPS_Q3_K_PASCAL;
  4118. } else {
  4119. GGML_ASSERT(false);
  4120. }
  4121. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4122. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4123. const dim3 block_nums(block_num_x, block_num_y, 1);
  4124. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4125. if (nrows_x % mmq_y == 0) {
  4126. const bool need_check = false;
  4127. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4128. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4129. } else {
  4130. const bool need_check = true;
  4131. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4132. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4133. }
  4134. #endif
  4135. }
  4136. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4137. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4138. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4139. int id;
  4140. CUDA_CHECK(cudaGetDevice(&id));
  4141. const int compute_capability = g_compute_capabilities[id];
  4142. int mmq_x, mmq_y, nwarps;
  4143. if (compute_capability >= CC_RDNA2) {
  4144. mmq_x = MMQ_X_Q4_K_RDNA2;
  4145. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4146. nwarps = NWARPS_Q4_K_RDNA2;
  4147. } else if (compute_capability >= CC_OFFSET_AMD) {
  4148. mmq_x = MMQ_X_Q4_K_RDNA1;
  4149. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4150. nwarps = NWARPS_Q4_K_RDNA1;
  4151. } else if (compute_capability >= CC_TURING) {
  4152. mmq_x = MMQ_X_Q4_K_AMPERE;
  4153. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4154. nwarps = NWARPS_Q4_K_AMPERE;
  4155. } else if (compute_capability >= MIN_CC_DP4A) {
  4156. mmq_x = MMQ_X_Q4_K_PASCAL;
  4157. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4158. nwarps = NWARPS_Q4_K_PASCAL;
  4159. } else {
  4160. GGML_ASSERT(false);
  4161. }
  4162. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4163. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4164. const dim3 block_nums(block_num_x, block_num_y, 1);
  4165. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4166. if (nrows_x % mmq_y == 0) {
  4167. const bool need_check = false;
  4168. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4169. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4170. } else {
  4171. const bool need_check = true;
  4172. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4173. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4174. }
  4175. }
  4176. static void ggml_mul_mat_q5_K_q8_1_cuda(
  4177. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4178. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4179. int id;
  4180. CUDA_CHECK(cudaGetDevice(&id));
  4181. const int compute_capability = g_compute_capabilities[id];
  4182. int mmq_x, mmq_y, nwarps;
  4183. if (compute_capability >= CC_RDNA2) {
  4184. mmq_x = MMQ_X_Q5_K_RDNA2;
  4185. mmq_y = MMQ_Y_Q5_K_RDNA2;
  4186. nwarps = NWARPS_Q5_K_RDNA2;
  4187. } else if (compute_capability >= CC_OFFSET_AMD) {
  4188. mmq_x = MMQ_X_Q5_K_RDNA1;
  4189. mmq_y = MMQ_Y_Q5_K_RDNA1;
  4190. nwarps = NWARPS_Q5_K_RDNA1;
  4191. } else if (compute_capability >= CC_TURING) {
  4192. mmq_x = MMQ_X_Q5_K_AMPERE;
  4193. mmq_y = MMQ_Y_Q5_K_AMPERE;
  4194. nwarps = NWARPS_Q5_K_AMPERE;
  4195. } else if (compute_capability >= MIN_CC_DP4A) {
  4196. mmq_x = MMQ_X_Q5_K_PASCAL;
  4197. mmq_y = MMQ_Y_Q5_K_PASCAL;
  4198. nwarps = NWARPS_Q5_K_PASCAL;
  4199. } else {
  4200. GGML_ASSERT(false);
  4201. }
  4202. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4203. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4204. const dim3 block_nums(block_num_x, block_num_y, 1);
  4205. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4206. if (nrows_x % mmq_y == 0) {
  4207. const bool need_check = false;
  4208. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4209. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4210. } else {
  4211. const bool need_check = true;
  4212. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4213. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4214. }
  4215. }
  4216. static void ggml_mul_mat_q6_K_q8_1_cuda(
  4217. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4218. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4219. int id;
  4220. CUDA_CHECK(cudaGetDevice(&id));
  4221. const int compute_capability = g_compute_capabilities[id];
  4222. int mmq_x, mmq_y, nwarps;
  4223. if (compute_capability >= CC_RDNA2) {
  4224. mmq_x = MMQ_X_Q6_K_RDNA2;
  4225. mmq_y = MMQ_Y_Q6_K_RDNA2;
  4226. nwarps = NWARPS_Q6_K_RDNA2;
  4227. } else if (compute_capability >= CC_OFFSET_AMD) {
  4228. mmq_x = MMQ_X_Q6_K_RDNA1;
  4229. mmq_y = MMQ_Y_Q6_K_RDNA1;
  4230. nwarps = NWARPS_Q6_K_RDNA1;
  4231. } else if (compute_capability >= CC_TURING) {
  4232. mmq_x = MMQ_X_Q6_K_AMPERE;
  4233. mmq_y = MMQ_Y_Q6_K_AMPERE;
  4234. nwarps = NWARPS_Q6_K_AMPERE;
  4235. } else if (compute_capability >= MIN_CC_DP4A) {
  4236. mmq_x = MMQ_X_Q6_K_PASCAL;
  4237. mmq_y = MMQ_Y_Q6_K_PASCAL;
  4238. nwarps = NWARPS_Q6_K_PASCAL;
  4239. } else {
  4240. GGML_ASSERT(false);
  4241. }
  4242. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4243. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4244. const dim3 block_nums(block_num_x, block_num_y, 1);
  4245. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4246. if (nrows_x % mmq_y == 0) {
  4247. const bool need_check = false;
  4248. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4249. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4250. } else {
  4251. const bool need_check = true;
  4252. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4253. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4254. }
  4255. }
  4256. static void ggml_mul_mat_p021_f16_f32_cuda(
  4257. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  4258. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  4259. const dim3 block_nums(1, nrows_x, nchannels_y);
  4260. const dim3 block_dims(WARP_SIZE, 1, 1);
  4261. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  4262. }
  4263. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  4264. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  4265. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  4266. const dim3 block_nums(1, nrows_x, nchannels_y);
  4267. const dim3 block_dims(WARP_SIZE, 1, 1);
  4268. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  4269. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  4270. }
  4271. static void ggml_cpy_f32_f32_cuda(
  4272. const char * cx, char * cdst, const int ne,
  4273. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4274. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  4275. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  4276. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  4277. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  4278. }
  4279. static void ggml_cpy_f32_f16_cuda(
  4280. const char * cx, char * cdst, const int ne,
  4281. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  4282. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  4283. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  4284. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  4285. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  4286. }
  4287. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  4288. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  4289. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  4290. }
  4291. static void rope_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p0,
  4292. const float p_delta, const int p_delta_rows, const float theta_scale, cudaStream_t stream) {
  4293. GGML_ASSERT(ncols % 2 == 0);
  4294. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  4295. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  4296. const dim3 block_nums(nrows, num_blocks_x, 1);
  4297. rope_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p0, p_delta, p_delta_rows, theta_scale);
  4298. }
  4299. static void rope_neox_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p0,
  4300. const float p_delta, const int p_delta_rows, const float theta_scale, cudaStream_t stream) {
  4301. GGML_ASSERT(ncols % 2 == 0);
  4302. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  4303. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  4304. const dim3 block_nums(nrows, num_blocks_x, 1);
  4305. rope_neox_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p0, p_delta, p_delta_rows, theta_scale);
  4306. }
  4307. static void rope_glm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float p0,
  4308. const float p_delta, const int p_delta_rows, const float theta_scale, const int n_ctx, cudaStream_t stream) {
  4309. GGML_ASSERT(ncols % 4 == 0);
  4310. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  4311. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  4312. const dim3 block_nums(num_blocks_x, nrows, 1);
  4313. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, p0, p_delta, p_delta_rows, theta_scale, n_ctx);
  4314. }
  4315. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  4316. const int k_rows, const int n_heads_log2_floor, const float m0,
  4317. const float m1, cudaStream_t stream) {
  4318. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  4319. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  4320. const dim3 block_nums(num_blocks_x, nrows, 1);
  4321. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  4322. }
  4323. 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) {
  4324. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  4325. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  4326. const dim3 block_nums(nrows_x, block_num_x, 1);
  4327. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  4328. }
  4329. static void soft_max_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, cudaStream_t stream) {
  4330. const dim3 block_dims(1, WARP_SIZE, 1);
  4331. const dim3 block_nums(nrows_x, 1, 1);
  4332. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x);
  4333. }
  4334. // buffer pool for cuda
  4335. #define MAX_CUDA_BUFFERS 256
  4336. struct scoped_spin_lock {
  4337. std::atomic_flag& lock;
  4338. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  4339. while (lock.test_and_set(std::memory_order_acquire)) {
  4340. ; // spin
  4341. }
  4342. }
  4343. ~scoped_spin_lock() {
  4344. lock.clear(std::memory_order_release);
  4345. }
  4346. scoped_spin_lock(const scoped_spin_lock&) = delete;
  4347. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  4348. };
  4349. struct cuda_buffer {
  4350. void * ptr = nullptr;
  4351. size_t size = 0;
  4352. };
  4353. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  4354. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  4355. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  4356. scoped_spin_lock lock(g_cuda_pool_lock);
  4357. int id;
  4358. CUDA_CHECK(cudaGetDevice(&id));
  4359. #ifdef DEBUG_CUDA_MALLOC
  4360. int nnz = 0;
  4361. size_t max_size = 0, tot_size = 0;
  4362. #endif
  4363. size_t best_diff = 1ull << 36;
  4364. int ibest = -1;
  4365. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  4366. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  4367. if (b.ptr != nullptr) {
  4368. #ifdef DEBUG_CUDA_MALLOC
  4369. ++nnz;
  4370. tot_size += b.size;
  4371. if (b.size > max_size) max_size = b.size;
  4372. #endif
  4373. if (b.size >= size) {
  4374. size_t diff = b.size - size;
  4375. if (diff < best_diff) {
  4376. best_diff = diff;
  4377. ibest = i;
  4378. if (!best_diff) {
  4379. void * ptr = b.ptr;
  4380. *actual_size = b.size;
  4381. b.ptr = nullptr;
  4382. b.size = 0;
  4383. return ptr;
  4384. }
  4385. }
  4386. }
  4387. }
  4388. }
  4389. if (ibest >= 0) {
  4390. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  4391. void * ptr = b.ptr;
  4392. *actual_size = b.size;
  4393. b.ptr = nullptr;
  4394. b.size = 0;
  4395. return ptr;
  4396. }
  4397. #ifdef DEBUG_CUDA_MALLOC
  4398. fprintf(stderr, "%s: %d buffers, max_size = %u MB, tot_size = %u MB, requested %u MB\n", __func__, nnz,
  4399. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  4400. #endif
  4401. void * ptr;
  4402. size_t look_ahead_size = (size_t) (1.05 * size);
  4403. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  4404. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  4405. *actual_size = look_ahead_size;
  4406. return ptr;
  4407. }
  4408. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  4409. scoped_spin_lock lock(g_cuda_pool_lock);
  4410. int id;
  4411. CUDA_CHECK(cudaGetDevice(&id));
  4412. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  4413. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  4414. if (b.ptr == nullptr) {
  4415. b.ptr = ptr;
  4416. b.size = size;
  4417. return;
  4418. }
  4419. }
  4420. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  4421. CUDA_CHECK(cudaFree(ptr));
  4422. }
  4423. void ggml_init_cublas() {
  4424. static bool initialized = false;
  4425. if (!initialized) {
  4426. #ifdef __HIP_PLATFORM_AMD__
  4427. // Workaround for a rocBLAS bug when using multiple graphics cards:
  4428. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  4429. rocblas_initialize();
  4430. CUDA_CHECK(cudaDeviceSynchronize());
  4431. #endif
  4432. CUDA_CHECK(cudaGetDeviceCount(&g_device_count));
  4433. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  4434. int64_t total_vram = 0;
  4435. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  4436. for (int64_t id = 0; id < g_device_count; ++id) {
  4437. cudaDeviceProp prop;
  4438. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  4439. fprintf(stderr, " Device %ld: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  4440. g_tensor_split[id] = total_vram;
  4441. total_vram += prop.totalGlobalMem;
  4442. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4443. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  4444. #else
  4445. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  4446. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4447. }
  4448. for (int64_t id = 0; id < g_device_count; ++id) {
  4449. g_tensor_split[id] /= total_vram;
  4450. }
  4451. for (int64_t id = 0; id < g_device_count; ++id) {
  4452. CUDA_CHECK(ggml_cuda_set_device(id));
  4453. // create cuda streams
  4454. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  4455. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  4456. }
  4457. // create cublas handle
  4458. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  4459. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  4460. }
  4461. // configure logging to stdout
  4462. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  4463. initialized = true;
  4464. }
  4465. }
  4466. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  4467. if (tensor_split == nullptr) {
  4468. return;
  4469. }
  4470. bool all_zero = true;
  4471. for (int i = 0; i < g_device_count; ++i) {
  4472. if (tensor_split[i] != 0.0f) {
  4473. all_zero = false;
  4474. break;
  4475. }
  4476. }
  4477. if (all_zero) {
  4478. return;
  4479. }
  4480. float split_sum = 0.0f;
  4481. for (int i = 0; i < g_device_count; ++i) {
  4482. g_tensor_split[i] = split_sum;
  4483. split_sum += tensor_split[i];
  4484. }
  4485. for (int i = 0; i < g_device_count; ++i) {
  4486. g_tensor_split[i] /= split_sum;
  4487. }
  4488. }
  4489. void * ggml_cuda_host_malloc(size_t size) {
  4490. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  4491. return nullptr;
  4492. }
  4493. void * ptr = nullptr;
  4494. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  4495. if (err != cudaSuccess) {
  4496. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  4497. // This can fixed the OOM error in WSL.
  4498. cudaGetLastError();
  4499. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  4500. size/1024.0/1024.0, cudaGetErrorString(err));
  4501. return nullptr;
  4502. }
  4503. return ptr;
  4504. }
  4505. void ggml_cuda_host_free(void * ptr) {
  4506. CUDA_CHECK(cudaFreeHost(ptr));
  4507. }
  4508. static cudaError_t ggml_cuda_cpy_tensor_2d(
  4509. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  4510. cudaMemcpyKind kind;
  4511. char * src_ptr;
  4512. if (src->backend == GGML_BACKEND_CPU) {
  4513. kind = cudaMemcpyHostToDevice;
  4514. src_ptr = (char *) src->data;
  4515. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  4516. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  4517. kind = cudaMemcpyDeviceToDevice;
  4518. struct ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  4519. int id;
  4520. CUDA_CHECK(cudaGetDevice(&id));
  4521. src_ptr = (char *) extra->data_device[id];
  4522. } else {
  4523. GGML_ASSERT(false);
  4524. }
  4525. char * dst_ptr = (char *) dst;
  4526. const int64_t ne0 = src->ne[0];
  4527. const int64_t nb0 = src->nb[0];
  4528. const int64_t nb1 = src->nb[1];
  4529. const int64_t nb2 = src->nb[2];
  4530. const int64_t nb3 = src->nb[3];
  4531. const enum ggml_type type = src->type;
  4532. const int64_t ts = ggml_type_size(type);
  4533. const int64_t bs = ggml_blck_size(type);
  4534. int64_t i1_diff = i1_high - i1_low;
  4535. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  4536. if (nb0 == ts && nb1 == ts*ne0/bs) {
  4537. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  4538. } else if (nb0 == ts) {
  4539. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  4540. } else {
  4541. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  4542. const void * rx = (const void *) ((const char *) x + i1*nb1);
  4543. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  4544. // pretend the row is a matrix with cols=1
  4545. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  4546. if (r != cudaSuccess) return r;
  4547. }
  4548. return cudaSuccess;
  4549. }
  4550. }
  4551. inline void ggml_cuda_op_add(
  4552. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4553. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4554. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4555. const int64_t ne10 = src1->ne[0];
  4556. const int64_t ne11 = src1->ne[1];
  4557. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  4558. add_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(src0), ne10*ne11, main_stream);
  4559. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  4560. add_f16_f32_f16_cuda((const half *) src0_dd, src1_dd, (half *) dst_dd, ggml_nelements(src0), main_stream);
  4561. } else {
  4562. GGML_ASSERT(false);
  4563. }
  4564. (void) src1;
  4565. (void) dst;
  4566. }
  4567. inline void ggml_cuda_op_mul(
  4568. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4569. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4570. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4571. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4572. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4573. const int64_t ne10 = src1->ne[0];
  4574. const int64_t ne11 = src1->ne[1];
  4575. mul_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(src0), ne10*ne11, main_stream);
  4576. (void) dst;
  4577. }
  4578. inline void ggml_cuda_op_gelu(
  4579. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4580. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4581. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4582. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4583. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  4584. (void) src1;
  4585. (void) dst;
  4586. (void) src1_dd;
  4587. }
  4588. inline void ggml_cuda_op_silu(
  4589. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4590. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4591. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4592. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4593. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  4594. (void) src1;
  4595. (void) dst;
  4596. (void) src1_dd;
  4597. }
  4598. inline void ggml_cuda_op_norm(
  4599. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4600. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4601. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4602. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4603. const int64_t ne00 = src0->ne[0];
  4604. const int64_t nrows = ggml_nrows(src0);
  4605. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, main_stream);
  4606. (void) src1;
  4607. (void) dst;
  4608. (void) src1_dd;
  4609. }
  4610. inline void ggml_cuda_op_rms_norm(
  4611. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4612. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4613. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4614. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4615. const int64_t ne00 = src0->ne[0];
  4616. const int64_t nrows = ggml_nrows(src0);
  4617. float eps;
  4618. memcpy(&eps, dst->op_params, sizeof(float));
  4619. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  4620. (void) src1;
  4621. (void) dst;
  4622. (void) src1_dd;
  4623. }
  4624. inline void ggml_cuda_op_mul_mat_q(
  4625. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  4626. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  4627. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  4628. const int64_t ne00 = src0->ne[0];
  4629. const int64_t ne10 = src1->ne[0];
  4630. GGML_ASSERT(ne10 % QK8_1 == 0);
  4631. const int64_t ne0 = dst->ne[0];
  4632. const int64_t row_diff = row_high - row_low;
  4633. int id;
  4634. CUDA_CHECK(cudaGetDevice(&id));
  4635. // the main device has a larger memory buffer to hold the results from all GPUs
  4636. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  4637. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  4638. switch (src0->type) {
  4639. case GGML_TYPE_Q4_0:
  4640. 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);
  4641. break;
  4642. case GGML_TYPE_Q4_1:
  4643. 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);
  4644. break;
  4645. case GGML_TYPE_Q5_0:
  4646. 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);
  4647. break;
  4648. case GGML_TYPE_Q5_1:
  4649. 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);
  4650. break;
  4651. case GGML_TYPE_Q8_0:
  4652. 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);
  4653. break;
  4654. case GGML_TYPE_Q2_K:
  4655. 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);
  4656. break;
  4657. case GGML_TYPE_Q3_K:
  4658. 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);
  4659. break;
  4660. case GGML_TYPE_Q4_K:
  4661. 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);
  4662. break;
  4663. case GGML_TYPE_Q5_K:
  4664. 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);
  4665. break;
  4666. case GGML_TYPE_Q6_K:
  4667. 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);
  4668. break;
  4669. default:
  4670. GGML_ASSERT(false);
  4671. break;
  4672. }
  4673. (void) src1;
  4674. (void) dst;
  4675. (void) src1_ddf_i;
  4676. }
  4677. static int64_t get_row_rounding(ggml_type type) {
  4678. int64_t min_compute_capability = INT_MAX;
  4679. int64_t max_compute_capability = INT_MIN;
  4680. for (int64_t id = 0; id < g_device_count; ++id) {
  4681. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  4682. if (min_compute_capability > g_compute_capabilities[id]) {
  4683. min_compute_capability = g_compute_capabilities[id];
  4684. }
  4685. if (max_compute_capability < g_compute_capabilities[id]) {
  4686. max_compute_capability = g_compute_capabilities[id];
  4687. }
  4688. }
  4689. }
  4690. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4691. switch(type) {
  4692. case GGML_TYPE_Q4_0:
  4693. case GGML_TYPE_Q4_1:
  4694. case GGML_TYPE_Q5_0:
  4695. case GGML_TYPE_Q5_1:
  4696. case GGML_TYPE_Q8_0:
  4697. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  4698. case GGML_TYPE_F16:
  4699. return 1;
  4700. case GGML_TYPE_Q2_K:
  4701. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  4702. case GGML_TYPE_Q3_K:
  4703. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  4704. case GGML_TYPE_Q4_K:
  4705. case GGML_TYPE_Q5_K:
  4706. case GGML_TYPE_Q6_K:
  4707. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  4708. default:
  4709. GGML_ASSERT(false);
  4710. }
  4711. #else
  4712. switch(type) {
  4713. case GGML_TYPE_Q4_0:
  4714. case GGML_TYPE_Q4_1:
  4715. return max_compute_capability >= CC_TURING ? 128 : 64;
  4716. case GGML_TYPE_Q5_0:
  4717. case GGML_TYPE_Q5_1:
  4718. case GGML_TYPE_Q8_0:
  4719. return 64;
  4720. case GGML_TYPE_F16:
  4721. return 1;
  4722. case GGML_TYPE_Q2_K:
  4723. case GGML_TYPE_Q3_K:
  4724. case GGML_TYPE_Q4_K:
  4725. case GGML_TYPE_Q5_K:
  4726. return max_compute_capability >= CC_TURING ? 128 : 64;
  4727. case GGML_TYPE_Q6_K:
  4728. return 64;
  4729. default:
  4730. GGML_ASSERT(false);
  4731. }
  4732. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4733. }
  4734. inline void ggml_cuda_op_mul_mat_vec_q(
  4735. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  4736. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  4737. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  4738. const int64_t ne00 = src0->ne[0];
  4739. const int64_t row_diff = row_high - row_low;
  4740. switch (src0->type) {
  4741. case GGML_TYPE_Q4_0:
  4742. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4743. break;
  4744. case GGML_TYPE_Q4_1:
  4745. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4746. break;
  4747. case GGML_TYPE_Q5_0:
  4748. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4749. break;
  4750. case GGML_TYPE_Q5_1:
  4751. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4752. break;
  4753. case GGML_TYPE_Q8_0:
  4754. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4755. break;
  4756. case GGML_TYPE_Q2_K:
  4757. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4758. break;
  4759. case GGML_TYPE_Q3_K:
  4760. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4761. break;
  4762. case GGML_TYPE_Q4_K:
  4763. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4764. break;
  4765. case GGML_TYPE_Q5_K:
  4766. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4767. break;
  4768. case GGML_TYPE_Q6_K:
  4769. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  4770. break;
  4771. default:
  4772. GGML_ASSERT(false);
  4773. break;
  4774. }
  4775. (void) src1;
  4776. (void) dst;
  4777. (void) src1_ddf_i;
  4778. (void) src1_ncols;
  4779. (void) src1_padded_row_size;
  4780. }
  4781. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  4782. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  4783. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  4784. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  4785. const int64_t ne00 = src0->ne[0];
  4786. const int64_t row_diff = row_high - row_low;
  4787. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  4788. #ifdef GGML_CUDA_F16
  4789. size_t ash;
  4790. dfloat * src1_dfloat = nullptr; // dfloat == half
  4791. bool src1_convert_f16 = src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  4792. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  4793. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  4794. if (src1_convert_f16) {
  4795. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  4796. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  4797. ne00, 1, sizeof(float), 0, 0,
  4798. ne00, 1, sizeof(half), 0, 0, stream);
  4799. }
  4800. #else
  4801. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  4802. #endif // GGML_CUDA_F16
  4803. switch (src0->type) {
  4804. case GGML_TYPE_Q4_0:
  4805. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  4806. break;
  4807. case GGML_TYPE_Q4_1:
  4808. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  4809. break;
  4810. case GGML_TYPE_Q5_0:
  4811. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  4812. break;
  4813. case GGML_TYPE_Q5_1:
  4814. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  4815. break;
  4816. case GGML_TYPE_Q8_0:
  4817. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  4818. break;
  4819. case GGML_TYPE_Q2_K:
  4820. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  4821. break;
  4822. case GGML_TYPE_Q3_K:
  4823. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  4824. break;
  4825. case GGML_TYPE_Q4_K:
  4826. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  4827. break;
  4828. case GGML_TYPE_Q5_K:
  4829. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  4830. break;
  4831. case GGML_TYPE_Q6_K:
  4832. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  4833. break;
  4834. case GGML_TYPE_F16:
  4835. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  4836. break;
  4837. default:
  4838. GGML_ASSERT(false);
  4839. break;
  4840. }
  4841. #ifdef GGML_CUDA_F16
  4842. if (src1_convert_f16) {
  4843. ggml_cuda_pool_free(src1_dfloat, ash);
  4844. }
  4845. #endif // GGML_CUDA_F16
  4846. (void) src1;
  4847. (void) dst;
  4848. (void) src1_ddq_i;
  4849. (void) src1_ncols;
  4850. (void) src1_padded_row_size;
  4851. }
  4852. inline void ggml_cuda_op_mul_mat_cublas(
  4853. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  4854. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  4855. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  4856. GGML_ASSERT(src0_dd_i != nullptr);
  4857. GGML_ASSERT(src1_ddf_i != nullptr);
  4858. GGML_ASSERT(dst_dd_i != nullptr);
  4859. const int64_t ne00 = src0->ne[0];
  4860. const int64_t ne10 = src1->ne[0];
  4861. const int64_t ne0 = dst->ne[0];
  4862. const int64_t row_diff = row_high - row_low;
  4863. int id;
  4864. CUDA_CHECK(cudaGetDevice(&id));
  4865. // the main device has a larger memory buffer to hold the results from all GPUs
  4866. // ldc == nrows of the matrix that cuBLAS writes into
  4867. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  4868. const int compute_capability = g_compute_capabilities[id];
  4869. if (compute_capability >= CC_TURING && src0->type == GGML_TYPE_F16 && ggml_is_contiguous(src0) && ldc == row_diff) {
  4870. // convert src1 to fp16, multiply as fp16, convert dst to fp32
  4871. half * src1_as_f16 = nullptr;
  4872. size_t src1_as = 0;
  4873. if (src1->type != GGML_TYPE_F16) {
  4874. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  4875. GGML_ASSERT(to_fp16_cuda != nullptr);
  4876. size_t ne = src1_ncols*ne10;
  4877. src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src1_as);
  4878. to_fp16_cuda(src1_ddf_i, src1_as_f16, ne, stream);
  4879. }
  4880. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddq_i : src1_as_f16;
  4881. size_t dst_as = 0;
  4882. half * dst_f16 = (half *) ggml_cuda_pool_malloc(row_diff*src1_ncols * sizeof(half), &dst_as);
  4883. const half alpha_f16 = 1.0f;
  4884. const half beta_f16 = 0.0f;
  4885. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  4886. CUBLAS_CHECK(
  4887. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  4888. row_diff, src1_ncols, ne10,
  4889. &alpha_f16, src0_dd_i, CUDA_R_16F, ne00,
  4890. src1_ptr, CUDA_R_16F, ne10,
  4891. &beta_f16, dst_f16, CUDA_R_16F, ldc,
  4892. CUBLAS_COMPUTE_16F,
  4893. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  4894. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  4895. to_fp32_cuda(dst_f16, dst_dd_i, row_diff*src1_ncols, stream);
  4896. ggml_cuda_pool_free(dst_f16, dst_as);
  4897. if (src1_as != 0) {
  4898. ggml_cuda_pool_free(src1_as_f16, src1_as);
  4899. }
  4900. }
  4901. else {
  4902. float * src0_ddq_as_f32 = nullptr;
  4903. size_t src0_as = 0;
  4904. if (src0->type != GGML_TYPE_F32) {
  4905. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  4906. GGML_ASSERT(to_fp32_cuda != nullptr);
  4907. src0_ddq_as_f32 = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_as); // NOLINT
  4908. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32, row_diff*ne00, stream);
  4909. }
  4910. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32;
  4911. const float alpha = 1.0f;
  4912. const float beta = 0.0f;
  4913. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  4914. CUBLAS_CHECK(
  4915. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  4916. row_diff, src1_ncols, ne10,
  4917. &alpha, src0_ddf_i, ne00,
  4918. src1_ddf_i, ne10,
  4919. &beta, dst_dd_i, ldc));
  4920. if (src0_as != 0) {
  4921. ggml_cuda_pool_free(src0_ddq_as_f32, src0_as);
  4922. }
  4923. }
  4924. (void) dst;
  4925. (void) src1_ddq_i;
  4926. (void) src1_padded_row_size;
  4927. }
  4928. inline void ggml_cuda_op_rope(
  4929. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4930. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4931. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4932. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4933. const int64_t ne00 = src0->ne[0];
  4934. const int64_t ne01 = src0->ne[1];
  4935. const int64_t nrows = ggml_nrows(src0);
  4936. const int n_past = ((int32_t *) dst->op_params)[0];
  4937. const int n_dims = ((int32_t *) dst->op_params)[1];
  4938. const int mode = ((int32_t *) dst->op_params)[2];
  4939. const int n_ctx = ((int32_t *) dst->op_params)[3];
  4940. // RoPE alteration for extended context
  4941. float freq_base, freq_scale;
  4942. memcpy(&freq_base, (int32_t *) dst->op_params + 4, sizeof(float));
  4943. memcpy(&freq_scale, (int32_t *) dst->op_params + 5, sizeof(float));
  4944. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  4945. const float p0 = (((mode & 1) == 0 ? n_past : 0)) * freq_scale;
  4946. const bool is_neox = mode & 2;
  4947. const bool is_glm = mode & 4;
  4948. // compute
  4949. if (is_glm) {
  4950. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, p0, freq_scale, ne01, theta_scale, n_ctx, main_stream);
  4951. } else if (is_neox) {
  4952. GGML_ASSERT(ne00 == n_dims && "ne00 != n_dims is not implemented for CUDA yet");
  4953. rope_neox_f32_cuda(src0_dd, dst_dd, ne00, nrows, p0, freq_scale, ne01, theta_scale, main_stream);
  4954. } else {
  4955. rope_f32_cuda(src0_dd, dst_dd, ne00, nrows, p0, freq_scale, ne01, theta_scale, main_stream);
  4956. }
  4957. (void) src1;
  4958. (void) dst;
  4959. (void) src1_dd;
  4960. }
  4961. inline void ggml_cuda_op_alibi(
  4962. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4963. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4964. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4965. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4966. const int64_t ne00 = src0->ne[0];
  4967. const int64_t ne01 = src0->ne[1];
  4968. const int64_t ne02 = src0->ne[2];
  4969. const int64_t nrows = ggml_nrows(src0);
  4970. const int n_past = ((int32_t *) dst->op_params)[0];
  4971. const int n_head = ((int32_t *) dst->op_params)[1];
  4972. float max_bias;
  4973. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  4974. GGML_ASSERT(ne01 + n_past == ne00);
  4975. GGML_ASSERT(n_head == ne02);
  4976. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  4977. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  4978. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  4979. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  4980. (void) src1;
  4981. (void) src1_dd;
  4982. }
  4983. inline void ggml_cuda_op_diag_mask_inf(
  4984. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4985. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  4986. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  4987. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  4988. const int64_t ne00 = src0->ne[0];
  4989. const int64_t ne01 = src0->ne[1];
  4990. const int nrows0 = ggml_nrows(src0);
  4991. const int n_past = ((int32_t *) dst->op_params)[0];
  4992. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  4993. (void) src1;
  4994. (void) dst;
  4995. (void) src1_dd;
  4996. }
  4997. inline void ggml_cuda_op_soft_max(
  4998. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4999. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5000. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5001. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5002. const int64_t ne00 = src0->ne[0];
  5003. const int64_t nrows = ggml_nrows(src0);
  5004. soft_max_f32_cuda(src0_dd, dst_dd, ne00, nrows, main_stream);
  5005. (void) src1;
  5006. (void) dst;
  5007. (void) src1_dd;
  5008. }
  5009. inline void ggml_cuda_op_scale(
  5010. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5011. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5012. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5013. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5014. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5015. const float scale = ((float *) src1->data)[0];
  5016. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  5017. CUDA_CHECK(cudaGetLastError());
  5018. (void) src1;
  5019. (void) dst;
  5020. (void) src1_dd;
  5021. }
  5022. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  5023. const int64_t nrows0 = ggml_nrows(src0);
  5024. const bool use_src1 = src1 != nullptr;
  5025. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  5026. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  5027. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  5028. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5029. struct ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  5030. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5031. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  5032. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  5033. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  5034. const bool src1_stays_on_host = use_src1 && dst->op == GGML_OP_SCALE;
  5035. // dd = data device
  5036. float * src0_ddf = nullptr;
  5037. float * src1_ddf = nullptr;
  5038. float * dst_ddf = nullptr;
  5039. // as = actual size
  5040. size_t src0_asf = 0;
  5041. size_t src1_asf = 0;
  5042. size_t dst_asf = 0;
  5043. ggml_cuda_set_device(g_main_device);
  5044. const cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5045. if (src0_on_device) {
  5046. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  5047. } else {
  5048. src0_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_asf);
  5049. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  5050. }
  5051. if (use_src1 && !src1_stays_on_host) {
  5052. if (src1_on_device) {
  5053. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  5054. } else {
  5055. src1_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf);
  5056. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  5057. }
  5058. }
  5059. if (dst_on_device) {
  5060. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  5061. } else {
  5062. dst_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(dst), &dst_asf);
  5063. }
  5064. // do the computation
  5065. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  5066. CUDA_CHECK(cudaGetLastError());
  5067. // copy dst to host if necessary
  5068. if (!dst_on_device) {
  5069. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  5070. }
  5071. if (src0_asf > 0) {
  5072. ggml_cuda_pool_free(src0_ddf, src0_asf);
  5073. }
  5074. if (src1_asf > 0) {
  5075. ggml_cuda_pool_free(src1_ddf, src1_asf);
  5076. }
  5077. if (dst_asf > 0) {
  5078. ggml_cuda_pool_free(dst_ddf, dst_asf);
  5079. }
  5080. if (dst->backend == GGML_BACKEND_CPU) {
  5081. CUDA_CHECK(cudaDeviceSynchronize());
  5082. }
  5083. }
  5084. void ggml_cuda_set_peer_access(const int n_tokens) {
  5085. static bool peer_access_enabled = false;
  5086. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  5087. if (peer_access_enabled == enable_peer_access) {
  5088. return;
  5089. }
  5090. #ifdef NDEBUG
  5091. for (int id = 0; id < g_device_count; ++id) {
  5092. CUDA_CHECK(ggml_cuda_set_device(id));
  5093. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  5094. if (id == id_other) {
  5095. continue;
  5096. }
  5097. if (id != g_main_device && id_other != g_main_device) {
  5098. continue;
  5099. }
  5100. int can_access_peer;
  5101. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  5102. if (can_access_peer) {
  5103. if (enable_peer_access) {
  5104. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  5105. } else {
  5106. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  5107. }
  5108. }
  5109. }
  5110. }
  5111. #endif // NDEBUG
  5112. peer_access_enabled = enable_peer_access;
  5113. }
  5114. static void ggml_cuda_op_mul_mat(
  5115. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  5116. const bool convert_src1_to_q8_1) {
  5117. const int64_t ne00 = src0->ne[0];
  5118. const int64_t ne01 = src0->ne[1];
  5119. const int64_t ne02 = src0->ne[2];
  5120. const int64_t ne03 = src0->ne[3];
  5121. const int64_t nrows0 = ggml_nrows(src0);
  5122. const int64_t ne10 = src1->ne[0];
  5123. const int64_t ne11 = src1->ne[1];
  5124. const int64_t ne12 = src1->ne[2];
  5125. const int64_t ne13 = src1->ne[3];
  5126. const int64_t nrows1 = ggml_nrows(src1);
  5127. GGML_ASSERT(ne03 == ne13);
  5128. const int64_t ne0 = dst->ne[0];
  5129. const int64_t ne1 = dst->ne[1];
  5130. const int nb2 = dst->nb[2];
  5131. const int nb3 = dst->nb[3];
  5132. ggml_cuda_set_peer_access(ne11);
  5133. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  5134. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  5135. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  5136. const int64_t i02_divisor = ne12 / ne02;
  5137. const size_t src0_ts = ggml_type_size(src0->type);
  5138. const size_t src0_bs = ggml_blck_size(src0->type);
  5139. const size_t q8_1_ts = sizeof(block_q8_1);
  5140. const size_t q8_1_bs = QK8_1;
  5141. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5142. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5143. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5144. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  5145. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  5146. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  5147. const int64_t src1_padded_col_size = ne10 % MATRIX_ROW_PADDING == 0 ?
  5148. ne10 : ne10 - ne10 % MATRIX_ROW_PADDING + MATRIX_ROW_PADDING;
  5149. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  5150. GGML_ASSERT(!(split && ne02 > 1));
  5151. GGML_ASSERT(!(split && ne03 > 1));
  5152. GGML_ASSERT(!(split && ne02 < ne12));
  5153. // dd = data device
  5154. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  5155. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  5156. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  5157. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  5158. // as = actual size
  5159. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  5160. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  5161. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  5162. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  5163. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  5164. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  5165. for (int64_t id = 0; id < g_device_count; ++id) {
  5166. // by default, use all rows
  5167. row_low[id] = 0;
  5168. row_high[id] = ne01;
  5169. // for multi GPU, get the row boundaries from tensor split
  5170. // and round to mul_mat_q tile sizes
  5171. if (split) {
  5172. const int64_t rounding = get_row_rounding(src0->type);
  5173. if (id != 0) {
  5174. row_low[id] = ne01*g_tensor_split[id];
  5175. row_low[id] -= row_low[id] % rounding;
  5176. }
  5177. if (id != g_device_count - 1) {
  5178. row_high[id] = ne01*g_tensor_split[id + 1];
  5179. row_high[id] -= row_high[id] % rounding;
  5180. }
  5181. }
  5182. }
  5183. for (int64_t id = 0; id < g_device_count; ++id) {
  5184. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  5185. continue;
  5186. }
  5187. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  5188. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  5189. ggml_cuda_set_device(id);
  5190. const cudaStream_t stream = g_cudaStreams[id][0];
  5191. if (src0_on_device && src0_is_contiguous) {
  5192. src0_dd[id] = (char *) src0_extra->data_device[id];
  5193. } else {
  5194. const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  5195. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  5196. }
  5197. if (src1_on_device && src1_is_contiguous) {
  5198. src1_ddf[id] = (float *) src1_extra->data_device[id];
  5199. } else {
  5200. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  5201. }
  5202. if (convert_src1_to_q8_1) {
  5203. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  5204. if (split && src1_on_device && src1_is_contiguous) {
  5205. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  5206. CUDA_CHECK(cudaGetLastError());
  5207. }
  5208. }
  5209. if (dst_on_device) {
  5210. dst_dd[id] = (float *) dst_extra->data_device[id];
  5211. } else {
  5212. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  5213. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  5214. }
  5215. }
  5216. // if multiple devices are used they need to wait for the main device
  5217. // here an event is recorded that signals that the main device has finished calculating the input data
  5218. if (split && g_device_count > 1) {
  5219. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5220. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  5221. }
  5222. const int64_t src1_col_stride = split && g_device_count > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  5223. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  5224. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  5225. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  5226. for (int64_t id = 0; id < g_device_count; ++id) {
  5227. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  5228. continue;
  5229. }
  5230. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  5231. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  5232. const int64_t row_diff = row_high[id] - row_low[id];
  5233. ggml_cuda_set_device(id);
  5234. const cudaStream_t stream = g_cudaStreams[id][is];
  5235. // wait for main GPU data if necessary
  5236. if (split && (id != g_main_device || is != 0)) {
  5237. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  5238. }
  5239. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  5240. const int64_t i03 = i0 / ne12;
  5241. const int64_t i02 = i0 % ne12;
  5242. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  5243. // for split tensors the data begins at i0 == i0_offset_low
  5244. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * ne01*ne00*src0_ts/src0_bs;
  5245. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  5246. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  5247. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  5248. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  5249. // in that case an offset on dst_ddf_i is needed
  5250. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  5251. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  5252. }
  5253. // copy src0, src1 to device if necessary
  5254. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  5255. if (id != g_main_device) {
  5256. if (convert_src1_to_q8_1) {
  5257. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  5258. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  5259. cudaMemcpyDeviceToDevice, stream));
  5260. } else {
  5261. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  5262. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  5263. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  5264. cudaMemcpyDeviceToDevice, stream));
  5265. }
  5266. }
  5267. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  5268. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  5269. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  5270. } else {
  5271. GGML_ASSERT(false);
  5272. }
  5273. if (convert_src1_to_q8_1 && src1->backend == GGML_BACKEND_CPU) {
  5274. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  5275. CUDA_CHECK(cudaGetLastError());
  5276. }
  5277. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  5278. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  5279. }
  5280. // do the computation
  5281. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  5282. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  5283. CUDA_CHECK(cudaGetLastError());
  5284. // copy dst to host or other device if necessary
  5285. if (!dst_on_device) {
  5286. void * dst_off_device;
  5287. cudaMemcpyKind kind;
  5288. if (dst->backend == GGML_BACKEND_CPU) {
  5289. dst_off_device = dst->data;
  5290. kind = cudaMemcpyDeviceToHost;
  5291. } else if (dst->backend == GGML_BACKEND_GPU) {
  5292. dst_off_device = dst_extra->data_device[g_main_device];
  5293. kind = cudaMemcpyDeviceToDevice;
  5294. } else {
  5295. GGML_ASSERT(false);
  5296. }
  5297. if (split) {
  5298. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  5299. // dst is NOT transposed.
  5300. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  5301. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  5302. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  5303. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  5304. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  5305. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  5306. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  5307. row_diff*sizeof(float), src1_ncols, kind, stream));
  5308. } else {
  5309. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  5310. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  5311. dhf_dst_i += src1_col_0*ne0;
  5312. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  5313. }
  5314. }
  5315. // add event for the main device to wait on until other device is done
  5316. if (split && (id != g_main_device || is != 0)) {
  5317. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  5318. }
  5319. }
  5320. }
  5321. }
  5322. for (int64_t id = 0; id < g_device_count; ++id) {
  5323. CUDA_CHECK(ggml_cuda_set_device(id));
  5324. // free buffers again when done
  5325. if (src0_as[id] > 0) {
  5326. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  5327. }
  5328. if (src1_asf[id] > 0) {
  5329. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  5330. }
  5331. if (src1_asq[id] > 0) {
  5332. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  5333. }
  5334. if (dst_as[id] > 0) {
  5335. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  5336. }
  5337. }
  5338. // main device waits for all other devices to be finished
  5339. if (split && g_device_count > 1) {
  5340. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  5341. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  5342. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5343. for (int64_t id = 0; id < g_device_count; ++id) {
  5344. for (int64_t is = 0; is < is_max; ++is) {
  5345. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  5346. }
  5347. }
  5348. }
  5349. if (dst->backend == GGML_BACKEND_CPU) {
  5350. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5351. CUDA_CHECK(cudaDeviceSynchronize());
  5352. }
  5353. }
  5354. void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5355. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  5356. }
  5357. void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5358. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  5359. }
  5360. void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5361. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  5362. }
  5363. void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5364. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  5365. }
  5366. void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5367. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  5368. }
  5369. void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5370. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  5371. }
  5372. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  5373. const int64_t ne10 = src1->ne[0];
  5374. const int64_t ne0 = dst->ne[0];
  5375. const int64_t ne1 = dst->ne[1];
  5376. // TODO: find the optimal values for these
  5377. if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  5378. src1->type == GGML_TYPE_F32 &&
  5379. dst->type == GGML_TYPE_F32 &&
  5380. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32)) {
  5381. return true;
  5382. }
  5383. return false;
  5384. }
  5385. void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  5386. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  5387. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  5388. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  5389. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  5390. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5391. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5392. const int64_t ne00 = src0->ne[0];
  5393. const int64_t ne01 = src0->ne[1];
  5394. const int64_t ne02 = src0->ne[2];
  5395. const int64_t ne12 = src1->ne[2];
  5396. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5397. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5398. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5399. void * src0_ddq = src0_extra->data_device[g_main_device];
  5400. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5401. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  5402. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5403. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  5404. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  5405. }
  5406. void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  5407. GGML_ASSERT(!ggml_is_contiguous(src0) && ggml_is_contiguous(src1));
  5408. GGML_ASSERT(!ggml_is_permuted(src0));
  5409. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  5410. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  5411. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5412. const int64_t ne00 = src0->ne[0];
  5413. const int64_t ne01 = src0->ne[1];
  5414. const int64_t ne02 = src0->ne[2];
  5415. const int64_t ne12 = src1->ne[2];
  5416. const int64_t nb01 = src0->nb[1];
  5417. const int64_t nb02 = src0->nb[2];
  5418. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5419. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5420. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5421. void * src0_ddq = src0_extra->data_device[g_main_device];
  5422. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5423. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  5424. struct ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  5425. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  5426. const int64_t row_stride_x = nb01 / sizeof(half);
  5427. const int64_t channel_stride_x = nb02 / sizeof(half);
  5428. 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);
  5429. }
  5430. void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5431. bool all_on_device = (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  5432. src1->backend == GGML_BACKEND_GPU && dst->backend == GGML_BACKEND_GPU;
  5433. int64_t min_compute_capability = INT_MAX;
  5434. for (int64_t id = 0; id < g_device_count; ++id) {
  5435. if (min_compute_capability > g_compute_capabilities[id]
  5436. && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5437. min_compute_capability = g_compute_capabilities[id];
  5438. }
  5439. }
  5440. if (all_on_device && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  5441. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  5442. } else if (all_on_device && !ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && src1->ne[1] == 1) {
  5443. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  5444. }else if (src0->type == GGML_TYPE_F32) {
  5445. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  5446. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  5447. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  5448. #ifdef GGML_CUDA_FORCE_DMMV
  5449. const bool use_mul_mat_vec_q = false;
  5450. #else
  5451. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  5452. #endif // GGML_CUDA_FORCE_DMMV
  5453. if (use_mul_mat_vec_q) {
  5454. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  5455. } else {
  5456. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  5457. }
  5458. } else {
  5459. if (g_mul_mat_q && ggml_is_quantized(src0->type) && min_compute_capability >= MIN_CC_DP4A) {
  5460. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  5461. } else {
  5462. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  5463. }
  5464. }
  5465. } else {
  5466. GGML_ASSERT(false);
  5467. }
  5468. }
  5469. void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5470. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  5471. }
  5472. void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5473. const int64_t ne = ggml_nelements(src0);
  5474. GGML_ASSERT(ne == ggml_nelements(src1));
  5475. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  5476. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  5477. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  5478. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  5479. const int64_t ne00 = src0->ne[0];
  5480. const int64_t ne01 = src0->ne[1];
  5481. GGML_ASSERT(src0->ne[3] == 1);
  5482. const int64_t nb00 = src0->nb[0];
  5483. const int64_t nb01 = src0->nb[1];
  5484. const int64_t nb02 = src0->nb[2];
  5485. const int64_t ne10 = src1->ne[0];
  5486. const int64_t ne11 = src1->ne[1];
  5487. GGML_ASSERT(src1->ne[3] == 1);
  5488. const int64_t nb10 = src1->nb[0];
  5489. const int64_t nb11 = src1->nb[1];
  5490. const int64_t nb12 = src1->nb[2];
  5491. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5492. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  5493. const struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  5494. const struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  5495. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  5496. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  5497. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  5498. ggml_cpy_f32_f32_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  5499. ne10, ne11, nb10, nb11, nb12, main_stream);
  5500. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  5501. ggml_cpy_f32_f16_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02,
  5502. ne10, ne11, nb10, nb11, nb12, main_stream);
  5503. } else {
  5504. GGML_ASSERT(false);
  5505. }
  5506. (void) dst;
  5507. }
  5508. void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5509. ggml_cuda_cpy(src0, dst, nullptr);
  5510. (void) src1;
  5511. }
  5512. void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5513. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  5514. }
  5515. void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5516. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  5517. }
  5518. void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5519. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  5520. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  5521. }
  5522. void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5523. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  5524. }
  5525. void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  5526. (void) src0;
  5527. (void) src1;
  5528. (void) dst;
  5529. }
  5530. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  5531. const int64_t nrows = ggml_nrows(tensor);
  5532. const int64_t ne0 = tensor->ne[0];
  5533. const size_t nb1 = tensor->nb[1];
  5534. ggml_backend backend = tensor->backend;
  5535. struct ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  5536. memset(extra, 0, sizeof(*extra));
  5537. for (int64_t id = 0; id < g_device_count; ++id) {
  5538. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  5539. continue;
  5540. }
  5541. ggml_cuda_set_device(id);
  5542. int64_t row_low, row_high;
  5543. if (backend == GGML_BACKEND_GPU) {
  5544. row_low = 0;
  5545. row_high = nrows;
  5546. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  5547. const int64_t rounding = get_row_rounding(tensor->type);
  5548. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  5549. row_low -= row_low % rounding;
  5550. if (id == g_device_count - 1) {
  5551. row_high = nrows;
  5552. } else {
  5553. row_high = nrows*g_tensor_split[id + 1];
  5554. row_high -= row_high % rounding;
  5555. }
  5556. } else {
  5557. GGML_ASSERT(false);
  5558. }
  5559. if (row_low == row_high) {
  5560. continue;
  5561. }
  5562. int64_t nrows_split = row_high - row_low;
  5563. const size_t offset_split = row_low*nb1;
  5564. size_t size = ggml_nbytes_split(tensor, nrows_split);
  5565. const size_t original_size = size;
  5566. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  5567. if (ne0 % MATRIX_ROW_PADDING != 0) {
  5568. size += (MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING)
  5569. * ggml_type_size(tensor->type)/ggml_blck_size(tensor->type);
  5570. }
  5571. char * buf;
  5572. CUDA_CHECK(cudaMalloc(&buf, size));
  5573. char * buf_host = (char*)data + offset_split;
  5574. // set padding to 0 to avoid possible NaN values
  5575. if (size > original_size) {
  5576. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  5577. }
  5578. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  5579. extra->data_device[id] = buf;
  5580. if (backend == GGML_BACKEND_GPU_SPLIT) {
  5581. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  5582. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  5583. }
  5584. }
  5585. }
  5586. tensor->extra = extra;
  5587. }
  5588. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  5589. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  5590. return;
  5591. }
  5592. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  5593. for (int64_t id = 0; id < g_device_count; ++id) {
  5594. if (extra->data_device[id] != nullptr) {
  5595. CUDA_CHECK(ggml_cuda_set_device(id));
  5596. CUDA_CHECK(cudaFree(extra->data_device[id]));
  5597. }
  5598. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  5599. if (extra->events[id][is] != nullptr) {
  5600. CUDA_CHECK(ggml_cuda_set_device(id));
  5601. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  5602. }
  5603. }
  5604. }
  5605. delete extra;
  5606. }
  5607. static struct ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  5608. static size_t g_temp_tensor_extra_index = 0;
  5609. static struct ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  5610. if (g_temp_tensor_extras == nullptr) {
  5611. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_MAX_NODES];
  5612. }
  5613. size_t alloc_index = g_temp_tensor_extra_index;
  5614. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_MAX_NODES;
  5615. struct ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  5616. memset(extra, 0, sizeof(*extra));
  5617. return extra;
  5618. }
  5619. void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  5620. if (scratch && g_scratch_size == 0) {
  5621. return;
  5622. }
  5623. // recursively assign CUDA buffers until a compute tensor is found
  5624. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  5625. const ggml_op src0_op = tensor->src[0]->op;
  5626. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  5627. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  5628. }
  5629. }
  5630. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  5631. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  5632. }
  5633. tensor->backend = GGML_BACKEND_GPU;
  5634. if (scratch && no_alloc) {
  5635. return;
  5636. }
  5637. struct ggml_tensor_extra_gpu * extra;
  5638. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  5639. tensor->op == GGML_OP_VIEW ||
  5640. force_inplace;
  5641. const size_t size = ggml_nbytes(tensor);
  5642. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  5643. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  5644. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  5645. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  5646. size_t offset = 0;
  5647. if (tensor->op == GGML_OP_VIEW) {
  5648. memcpy(&offset, tensor->op_params, sizeof(size_t));
  5649. }
  5650. extra = ggml_cuda_alloc_temp_tensor_extra();
  5651. extra->data_device[g_main_device] = src0_ddc + offset;
  5652. } else if (tensor->op == GGML_OP_CPY) {
  5653. struct ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  5654. void * src1_ddv = src1_extra->data_device[g_main_device];
  5655. extra = ggml_cuda_alloc_temp_tensor_extra();
  5656. extra->data_device[g_main_device] = src1_ddv;
  5657. } else if (scratch) {
  5658. GGML_ASSERT(size <= g_scratch_size);
  5659. if (g_scratch_offset + size > g_scratch_size) {
  5660. g_scratch_offset = 0;
  5661. }
  5662. char * data = (char *) g_scratch_buffer;
  5663. if (data == nullptr) {
  5664. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  5665. g_scratch_buffer = data;
  5666. }
  5667. extra = ggml_cuda_alloc_temp_tensor_extra();
  5668. extra->data_device[g_main_device] = data + g_scratch_offset;
  5669. g_scratch_offset += size;
  5670. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  5671. } else { // allocate new buffers outside of scratch
  5672. void * data;
  5673. CUDA_CHECK(cudaMalloc(&data, size));
  5674. CUDA_CHECK(cudaMemset(data, 0, size));
  5675. extra = new ggml_tensor_extra_gpu;
  5676. memset(extra, 0, sizeof(*extra));
  5677. extra->data_device[g_main_device] = data;
  5678. }
  5679. tensor->extra = extra;
  5680. }
  5681. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  5682. if (g_scratch_size == 0) {
  5683. return;
  5684. }
  5685. if (g_scratch_buffer == nullptr) {
  5686. ggml_cuda_set_device(g_main_device);
  5687. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  5688. }
  5689. struct ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  5690. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  5691. tensor->op == GGML_OP_VIEW;
  5692. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  5693. struct ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  5694. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  5695. size_t view_offset = 0;
  5696. if (tensor->op == GGML_OP_VIEW) {
  5697. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  5698. }
  5699. extra->data_device[g_main_device] = src0_ddc + view_offset;
  5700. } else {
  5701. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  5702. }
  5703. tensor->extra = extra;
  5704. }
  5705. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  5706. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  5707. }
  5708. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  5709. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  5710. }
  5711. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  5712. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  5713. }
  5714. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  5715. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  5716. }
  5717. void ggml_cuda_set_main_device(const int main_device) {
  5718. if (main_device >= g_device_count) {
  5719. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  5720. main_device, g_device_count, g_main_device);
  5721. return;
  5722. }
  5723. g_main_device = main_device;
  5724. if (g_device_count > 1) {
  5725. cudaDeviceProp prop;
  5726. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  5727. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  5728. }
  5729. }
  5730. void ggml_cuda_set_mul_mat_q(const bool mul_mat_q) {
  5731. g_mul_mat_q = mul_mat_q;
  5732. }
  5733. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  5734. g_scratch_size = scratch_size;
  5735. }
  5736. void ggml_cuda_free_scratch() {
  5737. if (g_scratch_buffer == nullptr) {
  5738. return;
  5739. }
  5740. CUDA_CHECK(cudaFree(g_scratch_buffer));
  5741. g_scratch_buffer = nullptr;
  5742. }
  5743. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor){
  5744. ggml_cuda_func_t func;
  5745. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  5746. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  5747. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  5748. switch (tensor->op) {
  5749. case GGML_OP_DUP:
  5750. if (!any_on_device) {
  5751. return false;
  5752. }
  5753. func = ggml_cuda_dup;
  5754. break;
  5755. case GGML_OP_ADD:
  5756. if (!any_on_device) {
  5757. return false;
  5758. }
  5759. func = ggml_cuda_add;
  5760. break;
  5761. case GGML_OP_MUL:
  5762. if (!any_on_device) {
  5763. return false;
  5764. }
  5765. func = ggml_cuda_mul;
  5766. break;
  5767. case GGML_OP_UNARY:
  5768. switch (ggml_get_unary_op(tensor)) {
  5769. case GGML_UNARY_OP_GELU:
  5770. if (!any_on_device) {
  5771. return false;
  5772. }
  5773. func = ggml_cuda_gelu;
  5774. break;
  5775. case GGML_UNARY_OP_SILU:
  5776. if (!any_on_device) {
  5777. return false;
  5778. }
  5779. func = ggml_cuda_silu;
  5780. break;
  5781. default:
  5782. return false;
  5783. } break;
  5784. case GGML_OP_NORM:
  5785. if (!any_on_device) {
  5786. return false;
  5787. }
  5788. func = ggml_cuda_norm;
  5789. break;
  5790. case GGML_OP_RMS_NORM:
  5791. if (!any_on_device) {
  5792. return false;
  5793. }
  5794. func = ggml_cuda_rms_norm;
  5795. break;
  5796. case GGML_OP_MUL_MAT:
  5797. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  5798. return false;
  5799. }
  5800. func = ggml_cuda_mul_mat;
  5801. break;
  5802. case GGML_OP_SCALE:
  5803. if (!any_on_device) {
  5804. return false;
  5805. }
  5806. func = ggml_cuda_scale;
  5807. break;
  5808. case GGML_OP_CPY:
  5809. if (!any_on_device) {
  5810. return false;
  5811. }
  5812. func = ggml_cuda_cpy;
  5813. break;
  5814. case GGML_OP_CONT:
  5815. if (!any_on_device) {
  5816. return false;
  5817. }
  5818. func = ggml_cuda_dup;
  5819. break;
  5820. case GGML_OP_RESHAPE:
  5821. case GGML_OP_VIEW:
  5822. case GGML_OP_PERMUTE:
  5823. case GGML_OP_TRANSPOSE:
  5824. if (!any_on_device) {
  5825. return false;
  5826. }
  5827. func = ggml_cuda_nop;
  5828. break;
  5829. case GGML_OP_DIAG_MASK_INF:
  5830. if (!any_on_device) {
  5831. return false;
  5832. }
  5833. func = ggml_cuda_diag_mask_inf;
  5834. break;
  5835. case GGML_OP_SOFT_MAX:
  5836. if (!any_on_device) {
  5837. return false;
  5838. }
  5839. func = ggml_cuda_soft_max;
  5840. break;
  5841. case GGML_OP_ROPE:
  5842. if (!any_on_device) {
  5843. return false;
  5844. }
  5845. func = ggml_cuda_rope;
  5846. break;
  5847. case GGML_OP_ALIBI:
  5848. if (!any_on_device) {
  5849. return false;
  5850. }
  5851. func = ggml_cuda_alibi;
  5852. break;
  5853. default:
  5854. return false;
  5855. }
  5856. if (params->ith != 0) {
  5857. return true;
  5858. }
  5859. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  5860. return true;
  5861. }
  5862. func(tensor->src[0], tensor->src[1], tensor);
  5863. return true;
  5864. }
  5865. int ggml_cuda_get_device_count() {
  5866. int device_count;
  5867. CUDA_CHECK(cudaGetDeviceCount(&device_count));
  5868. return device_count;
  5869. }
  5870. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  5871. cudaDeviceProp prop;
  5872. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  5873. snprintf(description, description_size, "%s", prop.name);
  5874. }