ggml-cuda.cu 305 KB

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