ggml-cuda.cu 251 KB

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