ggml-cuda.cu 274 KB

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