ggml-cuda.cu 267 KB

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