ggml-cuda.cu 361 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874
  1. #include <algorithm>
  2. #include <assert.h>
  3. #include <atomic>
  4. #include <cinttypes>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <float.h>
  8. #include <limits>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <vector>
  12. #if defined(GGML_USE_HIPBLAS)
  13. #include <hip/hip_runtime.h>
  14. #include <hipblas/hipblas.h>
  15. #include <hip/hip_fp16.h>
  16. #ifdef __HIP_PLATFORM_AMD__
  17. // for rocblas_initialize()
  18. #include "rocblas/rocblas.h"
  19. #endif // __HIP_PLATFORM_AMD__
  20. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  21. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  22. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  23. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  24. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  25. #define CUBLAS_OP_N HIPBLAS_OP_N
  26. #define CUBLAS_OP_T HIPBLAS_OP_T
  27. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  28. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  29. #define CUDA_R_16F HIPBLAS_R_16F
  30. #define CUDA_R_32F HIPBLAS_R_32F
  31. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  32. #define cublasCreate hipblasCreate
  33. #define cublasGemmEx hipblasGemmEx
  34. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  35. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  36. #define cublasHandle_t hipblasHandle_t
  37. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  38. #define cublasSetStream hipblasSetStream
  39. #define cublasSgemm hipblasSgemm
  40. #define cublasStatus_t hipblasStatus_t
  41. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  42. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  43. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  44. #define cudaDeviceProp hipDeviceProp_t
  45. #define cudaDeviceSynchronize hipDeviceSynchronize
  46. #define cudaError_t hipError_t
  47. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  48. #define cudaEventDisableTiming hipEventDisableTiming
  49. #define cudaEventRecord hipEventRecord
  50. #define cudaEvent_t hipEvent_t
  51. #define cudaEventDestroy hipEventDestroy
  52. #define cudaFree hipFree
  53. #define cudaFreeHost hipHostFree
  54. #define cudaGetDevice hipGetDevice
  55. #define cudaGetDeviceCount hipGetDeviceCount
  56. #define cudaGetDeviceProperties hipGetDeviceProperties
  57. #define cudaGetErrorString hipGetErrorString
  58. #define cudaGetLastError hipGetLastError
  59. #define cudaMalloc hipMalloc
  60. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  61. #define cudaMemcpy hipMemcpy
  62. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  63. #define cudaMemcpyAsync hipMemcpyAsync
  64. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  65. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  66. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  67. #define cudaMemcpyKind hipMemcpyKind
  68. #define cudaMemset hipMemset
  69. #define cudaMemsetAsync hipMemsetAsync
  70. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  71. #define cudaSetDevice hipSetDevice
  72. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  73. #define cudaStreamFireAndForget hipStreamFireAndForget
  74. #define cudaStreamNonBlocking hipStreamNonBlocking
  75. #define cudaStreamSynchronize hipStreamSynchronize
  76. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  77. #define cudaStream_t hipStream_t
  78. #define cudaSuccess hipSuccess
  79. #else
  80. #include <cuda_runtime.h>
  81. #include <cublas_v2.h>
  82. #include <cuda_fp16.h>
  83. #endif // defined(GGML_USE_HIPBLAS)
  84. #include "ggml-cuda.h"
  85. #include "ggml.h"
  86. #include "ggml-backend-impl.h"
  87. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  88. #define CC_VOLTA 700
  89. #define CC_OFFSET_AMD 1000000
  90. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  91. #define GGML_CUDA_MAX_NODES 8192
  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 id; \
  176. cudaGetDevice(&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", id); \
  180. GGML_ASSERT(!"CUDA error"); \
  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 id; \
  189. cudaGetDevice(&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", id); \
  193. GGML_ASSERT(!"cuBLAS error"); \
  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. GGML_ASSERT(!"cuBLAS error"); \
  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 = (const 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 = (const 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 *((const 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 *((const 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_GELU_BLOCK_SIZE 256
  394. #define CUDA_SILU_BLOCK_SIZE 256
  395. #define CUDA_TANH_BLOCK_SIZE 256
  396. #define CUDA_RELU_BLOCK_SIZE 256
  397. #define CUDA_SQR_BLOCK_SIZE 256
  398. #define CUDA_CPY_BLOCK_SIZE 32
  399. #define CUDA_SCALE_BLOCK_SIZE 256
  400. #define CUDA_CLAMP_BLOCK_SIZE 256
  401. #define CUDA_ROPE_BLOCK_SIZE 256
  402. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  403. #define CUDA_ALIBI_BLOCK_SIZE 32
  404. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  405. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  406. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  407. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  408. #define CUDA_UPSCALE_BLOCK_SIZE 256
  409. #define CUDA_CONCAT_BLOCK_SIZE 256
  410. #define CUDA_PAD_BLOCK_SIZE 256
  411. #define CUDA_ACC_BLOCK_SIZE 256
  412. #define CUDA_IM2COL_BLOCK_SIZE 256
  413. // dmmv = dequantize_mul_mat_vec
  414. #ifndef GGML_CUDA_DMMV_X
  415. #define GGML_CUDA_DMMV_X 32
  416. #endif
  417. #ifndef GGML_CUDA_MMV_Y
  418. #define GGML_CUDA_MMV_Y 1
  419. #endif
  420. #ifndef K_QUANTS_PER_ITERATION
  421. #define K_QUANTS_PER_ITERATION 2
  422. #else
  423. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  424. #endif
  425. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  426. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  427. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  428. #define MUL_MAT_SRC1_COL_STRIDE 128
  429. #define MAX_STREAMS 8
  430. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  431. struct ggml_tensor_extra_gpu {
  432. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  433. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  434. };
  435. // this is faster on Windows
  436. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  437. inline cudaError_t ggml_cuda_set_device(const int device) {
  438. int current_device;
  439. CUDA_CHECK(cudaGetDevice(&current_device));
  440. if (device == current_device) {
  441. return cudaSuccess;
  442. }
  443. return cudaSetDevice(device);
  444. }
  445. static int g_device_count = -1;
  446. static int g_main_device = 0;
  447. static int g_compute_capabilities[GGML_CUDA_MAX_DEVICES];
  448. static float g_tensor_split[GGML_CUDA_MAX_DEVICES] = {0};
  449. static void * g_scratch_buffer = nullptr;
  450. static size_t g_scratch_size = 0; // disabled by default
  451. static size_t g_scratch_offset = 0;
  452. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  453. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  454. #pragma unroll
  455. for (int mask = 16; mask > 0; mask >>= 1) {
  456. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  457. }
  458. return x;
  459. }
  460. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  461. #pragma unroll
  462. for (int mask = 16; mask > 0; mask >>= 1) {
  463. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  464. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  465. }
  466. return a;
  467. }
  468. static __device__ __forceinline__ float warp_reduce_max(float x) {
  469. #pragma unroll
  470. for (int mask = 16; mask > 0; mask >>= 1) {
  471. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  472. }
  473. return x;
  474. }
  475. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  476. return b;
  477. }
  478. static __device__ __forceinline__ float op_add(const float a, const float b) {
  479. return a + b;
  480. }
  481. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  482. return a * b;
  483. }
  484. static __device__ __forceinline__ float op_div(const float a, const float b) {
  485. return a / b;
  486. }
  487. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  488. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  489. int ne0, int ne1, int ne2, int ne3,
  490. int ne10, int ne11, int ne12, int ne13,
  491. /*int s0, */ int s1, int s2, int s3,
  492. /*int s10,*/ int s11, int s12, int s13) {
  493. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  494. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  495. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  496. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  497. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  498. return;
  499. }
  500. const int i11 = i1 % ne11;
  501. const int i12 = i2 % ne12;
  502. const int i13 = i3 % ne13;
  503. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  504. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  505. const size_t i_dst = i_src0;
  506. const src0_t * src0_row = src0 + i_src0;
  507. const src1_t * src1_row = src1 + i_src1;
  508. dst_t * dst_row = dst + i_dst;
  509. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  510. const int i10 = i0 % ne10;
  511. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  512. }
  513. }
  514. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  515. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  516. int ne0, int ne1, int ne2, int ne3,
  517. int ne10, int ne11, int ne12, int ne13,
  518. /*int s0, */ int s1, int s2, int s3,
  519. /*int s10,*/ int s11, int s12, int s13) {
  520. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  521. const int i3 = i/(ne2*ne1*ne0);
  522. const int i2 = (i/(ne1*ne0)) % ne2;
  523. const int i1 = (i/ne0) % ne1;
  524. const int i0 = i % ne0;
  525. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  526. return;
  527. }
  528. const int i11 = i1 % ne11;
  529. const int i12 = i2 % ne12;
  530. const int i13 = i3 % ne13;
  531. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  532. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  533. const size_t i_dst = i_src0;
  534. const src0_t * src0_row = src0 + i_src0;
  535. const src1_t * src1_row = src1 + i_src1;
  536. dst_t * dst_row = dst + i_dst;
  537. const int i10 = i0 % ne10;
  538. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  539. }
  540. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  541. const int ne10, const int ne11, const int ne12,
  542. const int nb1, const int nb2, int offset) {
  543. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  544. if (i >= ne) {
  545. return;
  546. }
  547. int src1_idx = i - offset;
  548. int oz = src1_idx / nb2;
  549. int oy = (src1_idx - (oz * nb2)) / nb1;
  550. int ox = src1_idx % nb1;
  551. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  552. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  553. } else {
  554. dst[i] = x[i];
  555. }
  556. }
  557. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  558. const float GELU_COEF_A = 0.044715f;
  559. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  560. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  561. if (i >= k) {
  562. return;
  563. }
  564. float xi = x[i];
  565. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  566. }
  567. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  568. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  569. if (i >= k) {
  570. return;
  571. }
  572. dst[i] = x[i] / (1.0f + expf(-x[i]));
  573. }
  574. static __global__ void gelu_quick_f32(const float *x, float *dst, int k) {
  575. const float GELU_QUICK_COEF = -1.702f;
  576. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  577. if (i >= k) {
  578. return;
  579. }
  580. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  581. }
  582. static __global__ void tanh_f32(const float *x, float *dst, int k) {
  583. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  584. if (i >= k) {
  585. return;
  586. }
  587. dst[i] = tanhf(x[i]);
  588. }
  589. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  590. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  591. if (i >= k) {
  592. return;
  593. }
  594. dst[i] = fmaxf(x[i], 0);
  595. }
  596. static __global__ void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope) {
  597. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  598. if (i >= k) {
  599. return;
  600. }
  601. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  602. }
  603. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  604. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  605. if (i >= k) {
  606. return;
  607. }
  608. dst[i] = x[i] * x[i];
  609. }
  610. template <int block_size>
  611. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  612. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  613. const int tid = threadIdx.x;
  614. float2 mean_var = make_float2(0.f, 0.f);
  615. for (int col = tid; col < ncols; col += block_size) {
  616. const float xi = x[row*ncols + col];
  617. mean_var.x += xi;
  618. mean_var.y += xi * xi;
  619. }
  620. // sum up partial sums
  621. mean_var = warp_reduce_sum(mean_var);
  622. if (block_size > WARP_SIZE) {
  623. __shared__ float2 s_sum[32];
  624. int warp_id = threadIdx.x / WARP_SIZE;
  625. int lane_id = threadIdx.x % WARP_SIZE;
  626. if (lane_id == 0) {
  627. s_sum[warp_id] = mean_var;
  628. }
  629. __syncthreads();
  630. mean_var = s_sum[lane_id];
  631. mean_var = warp_reduce_sum(mean_var);
  632. }
  633. const float mean = mean_var.x / ncols;
  634. const float var = mean_var.y / ncols - mean * mean;
  635. const float inv_std = rsqrtf(var + eps);
  636. for (int col = tid; col < ncols; col += block_size) {
  637. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  638. }
  639. }
  640. static __global__ void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02) {
  641. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  642. if (nidx >= ne0) {
  643. return;
  644. }
  645. // operation
  646. int offset_dst =
  647. nidx +
  648. blockIdx.y * ne0 +
  649. blockIdx.z * ne0 * gridDim.y;
  650. if (blockIdx.z < ne02) { // src0
  651. int offset_src =
  652. nidx +
  653. blockIdx.y * ne0 +
  654. blockIdx.z * ne0 * gridDim.y;
  655. dst[offset_dst] = x[offset_src];
  656. } else {
  657. int offset_src =
  658. nidx +
  659. blockIdx.y * ne0 +
  660. (blockIdx.z - ne02) * ne0 * gridDim.y;
  661. dst[offset_dst] = y[offset_src];
  662. }
  663. }
  664. static __global__ void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor) {
  665. int ne0 = ne00 * scale_factor;
  666. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  667. if (nidx >= ne0) {
  668. return;
  669. }
  670. // operation
  671. int i00 = nidx / scale_factor;
  672. int i01 = blockIdx.y / scale_factor;
  673. int offset_src =
  674. i00 +
  675. i01 * ne00 +
  676. blockIdx.z * nb02;
  677. int offset_dst =
  678. nidx +
  679. blockIdx.y * ne0 +
  680. blockIdx.z * ne0 * gridDim.y;
  681. dst[offset_dst] = x[offset_src];
  682. }
  683. static __global__ void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  684. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  685. if (nidx >= ne0) {
  686. return;
  687. }
  688. // operation
  689. int offset_dst =
  690. nidx +
  691. blockIdx.y * ne0 +
  692. blockIdx.z * ne0 * gridDim.y;
  693. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  694. int offset_src =
  695. nidx +
  696. blockIdx.y * ne00 +
  697. blockIdx.z * ne00 * ne01;
  698. dst[offset_dst] = x[offset_src];
  699. } else {
  700. dst[offset_dst] = 0.0f;
  701. }
  702. }
  703. template <int block_size>
  704. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  705. int start = blockIdx.x * group_size;
  706. int end = start + group_size;
  707. start += threadIdx.x;
  708. if (end >= ne_elements) {
  709. end = ne_elements;
  710. }
  711. float tmp = 0.0f; // partial sum for thread in warp
  712. for (int j = start; j < end; j += block_size) {
  713. tmp += x[j];
  714. }
  715. tmp = warp_reduce_sum(tmp);
  716. if (block_size > WARP_SIZE) {
  717. __shared__ float s_sum[32];
  718. int warp_id = threadIdx.x / WARP_SIZE;
  719. int lane_id = threadIdx.x % WARP_SIZE;
  720. if (lane_id == 0) {
  721. s_sum[warp_id] = tmp;
  722. }
  723. __syncthreads();
  724. tmp = s_sum[lane_id];
  725. tmp = warp_reduce_sum(tmp);
  726. }
  727. float mean = tmp / group_size;
  728. tmp = 0.0f;
  729. for (int j = start; j < end; j += block_size) {
  730. float xi = x[j] - mean;
  731. dst[j] = xi;
  732. tmp += xi * xi;
  733. }
  734. tmp = warp_reduce_sum(tmp);
  735. if (block_size > WARP_SIZE) {
  736. __shared__ float s_sum[32];
  737. int warp_id = threadIdx.x / WARP_SIZE;
  738. int lane_id = threadIdx.x % WARP_SIZE;
  739. if (lane_id == 0) {
  740. s_sum[warp_id] = tmp;
  741. }
  742. __syncthreads();
  743. tmp = s_sum[lane_id];
  744. tmp = warp_reduce_sum(tmp);
  745. }
  746. float variance = tmp / group_size;
  747. float scale = rsqrtf(variance + eps);
  748. for (int j = start; j < end; j += block_size) {
  749. dst[j] *= scale;
  750. }
  751. }
  752. template <int block_size>
  753. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  754. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  755. const int tid = threadIdx.x;
  756. float tmp = 0.0f; // partial sum for thread in warp
  757. for (int col = tid; col < ncols; col += block_size) {
  758. const float xi = x[row*ncols + col];
  759. tmp += xi * xi;
  760. }
  761. // sum up partial sums
  762. tmp = warp_reduce_sum(tmp);
  763. if (block_size > WARP_SIZE) {
  764. __shared__ float s_sum[32];
  765. int warp_id = threadIdx.x / WARP_SIZE;
  766. int lane_id = threadIdx.x % WARP_SIZE;
  767. if (lane_id == 0) {
  768. s_sum[warp_id] = tmp;
  769. }
  770. __syncthreads();
  771. tmp = s_sum[lane_id];
  772. tmp = warp_reduce_sum(tmp);
  773. }
  774. const float mean = tmp / ncols;
  775. const float scale = rsqrtf(mean + eps);
  776. for (int col = tid; col < ncols; col += block_size) {
  777. dst[row*ncols + col] = scale * x[row*ncols + col];
  778. }
  779. }
  780. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  781. const block_q4_0 * x = (const block_q4_0 *) vx;
  782. const dfloat d = x[ib].d;
  783. const int vui = x[ib].qs[iqs];
  784. v.x = vui & 0xF;
  785. v.y = vui >> 4;
  786. #ifdef GGML_CUDA_F16
  787. v = __hsub2(v, {8.0f, 8.0f});
  788. v = __hmul2(v, {d, d});
  789. #else
  790. v.x = (v.x - 8.0f) * d;
  791. v.y = (v.y - 8.0f) * d;
  792. #endif // GGML_CUDA_F16
  793. }
  794. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  795. const block_q4_1 * x = (const block_q4_1 *) vx;
  796. const dfloat d = __low2half(x[ib].dm);
  797. const dfloat m = __high2half(x[ib].dm);
  798. const int vui = x[ib].qs[iqs];
  799. v.x = vui & 0xF;
  800. v.y = vui >> 4;
  801. #ifdef GGML_CUDA_F16
  802. v = __hmul2(v, {d, d});
  803. v = __hadd2(v, {m, m});
  804. #else
  805. v.x = (v.x * d) + m;
  806. v.y = (v.y * d) + m;
  807. #endif // GGML_CUDA_F16
  808. }
  809. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  810. const block_q5_0 * x = (const block_q5_0 *) vx;
  811. const dfloat d = x[ib].d;
  812. uint32_t qh;
  813. memcpy(&qh, x[ib].qh, sizeof(qh));
  814. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  815. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  816. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  817. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  818. #ifdef GGML_CUDA_F16
  819. v = __hsub2(v, {16.0f, 16.0f});
  820. v = __hmul2(v, {d, d});
  821. #else
  822. v.x = (v.x - 16.0f) * d;
  823. v.y = (v.y - 16.0f) * d;
  824. #endif // GGML_CUDA_F16
  825. }
  826. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  827. const block_q5_1 * x = (const block_q5_1 *) vx;
  828. const dfloat d = __low2half(x[ib].dm);
  829. const dfloat m = __high2half(x[ib].dm);
  830. uint32_t qh;
  831. memcpy(&qh, x[ib].qh, sizeof(qh));
  832. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  833. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  834. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  835. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  836. #ifdef GGML_CUDA_F16
  837. v = __hmul2(v, {d, d});
  838. v = __hadd2(v, {m, m});
  839. #else
  840. v.x = (v.x * d) + m;
  841. v.y = (v.y * d) + m;
  842. #endif // GGML_CUDA_F16
  843. }
  844. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  845. const block_q8_0 * x = (const block_q8_0 *) vx;
  846. const dfloat d = x[ib].d;
  847. v.x = x[ib].qs[iqs + 0];
  848. v.y = x[ib].qs[iqs + 1];
  849. #ifdef GGML_CUDA_F16
  850. v = __hmul2(v, {d, d});
  851. #else
  852. v.x *= d;
  853. v.y *= d;
  854. #endif // GGML_CUDA_F16
  855. }
  856. //================================== k-quants
  857. template<typename dst_t>
  858. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  859. const int i = blockIdx.x;
  860. const block_q2_K * x = (const block_q2_K *) vx;
  861. const int tid = threadIdx.x;
  862. #if QK_K == 256
  863. const int n = tid/32;
  864. const int l = tid - 32*n;
  865. const int is = 8*n + l/16;
  866. const uint8_t q = x[i].qs[32*n + l];
  867. dst_t * y = yy + i*QK_K + 128*n;
  868. float dall = __low2half(x[i].dm);
  869. float dmin = __high2half(x[i].dm);
  870. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  871. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  872. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  873. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  874. #else
  875. const int is = tid/16; // 0 or 1
  876. const int il = tid%16; // 0...15
  877. const uint8_t q = x[i].qs[il] >> (2*is);
  878. dst_t * y = yy + i*QK_K + 16*is + il;
  879. float dall = __low2half(x[i].dm);
  880. float dmin = __high2half(x[i].dm);
  881. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  882. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  883. #endif
  884. }
  885. template<typename dst_t>
  886. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  887. const int i = blockIdx.x;
  888. const block_q3_K * x = (const block_q3_K *) vx;
  889. #if QK_K == 256
  890. const int r = threadIdx.x/4;
  891. const int tid = r/2;
  892. const int is0 = r%2;
  893. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  894. const int n = tid / 4;
  895. const int j = tid - 4*n;
  896. uint8_t m = 1 << (4*n + j);
  897. int is = 8*n + 2*j + is0;
  898. int shift = 2*j;
  899. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  900. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  901. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  902. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  903. float d_all = x[i].d;
  904. float dl = d_all * (us - 32);
  905. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  906. const uint8_t * q = x[i].qs + 32*n;
  907. const uint8_t * hm = x[i].hmask;
  908. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  909. #else
  910. const int tid = threadIdx.x;
  911. const int is = tid/16; // 0 or 1
  912. const int il = tid%16; // 0...15
  913. const int im = il/8; // 0...1
  914. const int in = il%8; // 0...7
  915. dst_t * y = yy + i*QK_K + 16*is + il;
  916. const uint8_t q = x[i].qs[il] >> (2*is);
  917. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  918. const float d = (float)x[i].d;
  919. if (is == 0) {
  920. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  921. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  922. } else {
  923. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  924. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  925. }
  926. #endif
  927. }
  928. #if QK_K == 256
  929. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  930. if (j < 4) {
  931. d = q[j] & 63; m = q[j + 4] & 63;
  932. } else {
  933. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  934. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  935. }
  936. }
  937. #endif
  938. template<typename dst_t>
  939. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  940. const block_q4_K * x = (const block_q4_K *) vx;
  941. const int i = blockIdx.x;
  942. #if QK_K == 256
  943. // assume 32 threads
  944. const int tid = threadIdx.x;
  945. const int il = tid/8;
  946. const int ir = tid%8;
  947. const int is = 2*il;
  948. const int n = 4;
  949. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  950. const float dall = __low2half(x[i].dm);
  951. const float dmin = __high2half(x[i].dm);
  952. const uint8_t * q = x[i].qs + 32*il + n*ir;
  953. uint8_t sc, m;
  954. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  955. const float d1 = dall * sc; const float m1 = dmin * m;
  956. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  957. const float d2 = dall * sc; const float m2 = dmin * m;
  958. for (int l = 0; l < n; ++l) {
  959. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  960. y[l +32] = d2 * (q[l] >> 4) - m2;
  961. }
  962. #else
  963. const int tid = threadIdx.x;
  964. const uint8_t * q = x[i].qs;
  965. dst_t * y = yy + i*QK_K;
  966. const float d = (float)x[i].dm[0];
  967. const float m = (float)x[i].dm[1];
  968. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  969. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  970. #endif
  971. }
  972. template<typename dst_t>
  973. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  974. const block_q5_K * x = (const block_q5_K *) vx;
  975. const int i = blockIdx.x;
  976. #if QK_K == 256
  977. // assume 64 threads - this is very slightly better than the one below
  978. const int tid = threadIdx.x;
  979. const int il = tid/16; // il is in 0...3
  980. const int ir = tid%16; // ir is in 0...15
  981. const int is = 2*il; // is is in 0...6
  982. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  983. const float dall = __low2half(x[i].dm);
  984. const float dmin = __high2half(x[i].dm);
  985. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  986. const uint8_t * qh = x[i].qh + 2*ir;
  987. uint8_t sc, m;
  988. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  989. const float d1 = dall * sc; const float m1 = dmin * m;
  990. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  991. const float d2 = dall * sc; const float m2 = dmin * m;
  992. uint8_t hm = 1 << (2*il);
  993. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  994. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  995. hm <<= 1;
  996. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  997. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  998. #else
  999. const int tid = threadIdx.x;
  1000. const uint8_t q = x[i].qs[tid];
  1001. const int im = tid/8; // 0...3
  1002. const int in = tid%8; // 0...7
  1003. const int is = tid/16; // 0 or 1
  1004. const uint8_t h = x[i].qh[in] >> im;
  1005. const float d = x[i].d;
  1006. dst_t * y = yy + i*QK_K + tid;
  1007. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1008. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1009. #endif
  1010. }
  1011. template<typename dst_t>
  1012. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1013. const block_q6_K * x = (const block_q6_K *) vx;
  1014. const int i = blockIdx.x;
  1015. #if QK_K == 256
  1016. // assume 64 threads - this is very slightly better than the one below
  1017. const int tid = threadIdx.x;
  1018. const int ip = tid/32; // ip is 0 or 1
  1019. const int il = tid - 32*ip; // 0...32
  1020. const int is = 8*ip + il/16;
  1021. dst_t * y = yy + i*QK_K + 128*ip + il;
  1022. const float d = x[i].d;
  1023. const uint8_t * ql = x[i].ql + 64*ip + il;
  1024. const uint8_t qh = x[i].qh[32*ip + il];
  1025. const int8_t * sc = x[i].scales + is;
  1026. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1027. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1028. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1029. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1030. #else
  1031. // assume 32 threads
  1032. const int tid = threadIdx.x;
  1033. const int ip = tid/16; // 0 or 1
  1034. const int il = tid - 16*ip; // 0...15
  1035. dst_t * y = yy + i*QK_K + 16*ip + il;
  1036. const float d = x[i].d;
  1037. const uint8_t ql = x[i].ql[16*ip + il];
  1038. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1039. const int8_t * sc = x[i].scales;
  1040. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1041. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1042. #endif
  1043. }
  1044. 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) {
  1045. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1046. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1047. if (row > nrows) return;
  1048. const int num_blocks_per_row = ncols / QK_K;
  1049. const int ib0 = row*num_blocks_per_row;
  1050. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1051. float tmp = 0; // partial sum for thread in warp
  1052. #if QK_K == 256
  1053. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1054. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1055. const int step = 16/K_QUANTS_PER_ITERATION;
  1056. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1057. const int in = tid - step*im; // 0...15 or 0...7
  1058. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1059. const int q_offset = 32*im + l0;
  1060. const int s_offset = 8*im;
  1061. const int y_offset = 128*im + l0;
  1062. uint32_t aux[4];
  1063. const uint8_t * d = (const uint8_t *)aux;
  1064. const uint8_t * m = (const uint8_t *)(aux + 2);
  1065. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1066. const float * y = yy + i * QK_K + y_offset;
  1067. const uint8_t * q = x[i].qs + q_offset;
  1068. const float dall = __low2half(x[i].dm);
  1069. const float dmin = __high2half(x[i].dm);
  1070. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1071. aux[0] = a[0] & 0x0f0f0f0f;
  1072. aux[1] = a[1] & 0x0f0f0f0f;
  1073. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1074. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1075. float sum1 = 0, sum2 = 0;
  1076. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1077. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1078. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1079. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1080. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1081. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1082. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1083. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1084. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1085. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1086. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1087. }
  1088. tmp += dall * sum1 - dmin * sum2;
  1089. }
  1090. #else
  1091. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1092. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1093. const int offset = tid * K_QUANTS_PER_ITERATION;
  1094. uint32_t uaux[2];
  1095. const uint8_t * d = (const uint8_t *)uaux;
  1096. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1097. const float * y = yy + i * QK_K + offset;
  1098. const uint8_t * q = x[i].qs + offset;
  1099. const uint32_t * s = (const uint32_t *)x[i].scales;
  1100. uaux[0] = s[0] & 0x0f0f0f0f;
  1101. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1102. const float2 dall = __half22float2(x[i].dm);
  1103. float sum1 = 0, sum2 = 0;
  1104. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1105. const uint8_t ql = q[l];
  1106. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1107. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1108. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1109. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1110. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1111. }
  1112. tmp += dall.x * sum1 - dall.y * sum2;
  1113. }
  1114. #endif
  1115. // sum up partial sums and write back result
  1116. #pragma unroll
  1117. for (int mask = 16; mask > 0; mask >>= 1) {
  1118. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1119. }
  1120. if (threadIdx.x == 0) {
  1121. dst[row] = tmp;
  1122. }
  1123. }
  1124. 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) {
  1125. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1126. if (row > nrows) return;
  1127. const int num_blocks_per_row = ncols / QK_K;
  1128. const int ib0 = row*num_blocks_per_row;
  1129. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1130. float tmp = 0; // partial sum for thread in warp
  1131. #if QK_K == 256
  1132. const uint16_t kmask1 = 0x0303;
  1133. const uint16_t kmask2 = 0x0f0f;
  1134. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1135. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1136. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1137. const int step = 16/K_QUANTS_PER_ITERATION;
  1138. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1139. const int in = tid - step*im; // 0....15 or 0...7
  1140. const uint8_t m = 1 << (4*im);
  1141. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1142. const int q_offset = 32*im + l0;
  1143. const int y_offset = 128*im + l0;
  1144. uint16_t utmp[4];
  1145. const int8_t * s = (const int8_t *)utmp;
  1146. const uint16_t s_shift = 4*im;
  1147. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1148. const float * y = yy + i * QK_K + y_offset;
  1149. const uint8_t * q = x[i].qs + q_offset;
  1150. const uint8_t * h = x[i].hmask + l0;
  1151. const uint16_t * a = (const uint16_t *)x[i].scales;
  1152. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1153. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1154. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1155. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1156. const float d = x[i].d;
  1157. float sum = 0;
  1158. for (int l = 0; l < n; ++l) {
  1159. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1160. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1161. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1162. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1163. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1164. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1165. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1166. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1167. }
  1168. tmp += d * sum;
  1169. }
  1170. #else
  1171. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1172. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1173. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1174. const int in = offset/8; // 0 or 1
  1175. const int im = offset%8; // 0...7
  1176. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1177. const float * y = yy + i * QK_K + offset;
  1178. const uint8_t * q = x[i].qs + offset;
  1179. const uint8_t * s = x[i].scales;
  1180. const float dall = (float)x[i].d;
  1181. float sum = 0;
  1182. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1183. const uint8_t hl = x[i].hmask[im+l] >> in;
  1184. const uint8_t ql = q[l];
  1185. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1186. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1187. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1188. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1189. }
  1190. tmp += sum;
  1191. }
  1192. #endif
  1193. // sum up partial sums and write back result
  1194. #pragma unroll
  1195. for (int mask = 16; mask > 0; mask >>= 1) {
  1196. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1197. }
  1198. if (threadIdx.x == 0) {
  1199. dst[row] = tmp;
  1200. }
  1201. }
  1202. 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) {
  1203. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1204. if (row > nrows) return;
  1205. const int num_blocks_per_row = ncols / QK_K;
  1206. const int ib0 = row*num_blocks_per_row;
  1207. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1208. #if QK_K == 256
  1209. const uint16_t kmask1 = 0x3f3f;
  1210. const uint16_t kmask2 = 0x0f0f;
  1211. const uint16_t kmask3 = 0xc0c0;
  1212. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1213. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1214. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1215. const int il = tid/step; // 0...3
  1216. const int ir = tid - step*il; // 0...7 or 0...3
  1217. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1218. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1219. const int in = il%2;
  1220. const int l0 = n*(2*ir + in);
  1221. const int q_offset = 32*im + l0;
  1222. const int y_offset = 64*im + l0;
  1223. uint16_t aux[4];
  1224. const uint8_t * sc = (const uint8_t *)aux;
  1225. #if K_QUANTS_PER_ITERATION == 2
  1226. uint32_t q32[4];
  1227. const uint8_t * q4 = (const uint8_t *)q32;
  1228. #else
  1229. uint16_t q16[4];
  1230. const uint8_t * q4 = (const uint8_t *)q16;
  1231. #endif
  1232. float tmp = 0; // partial sum for thread in warp
  1233. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1234. const float * y1 = yy + i*QK_K + y_offset;
  1235. const float * y2 = y1 + 128;
  1236. const float dall = __low2half(x[i].dm);
  1237. const float dmin = __high2half(x[i].dm);
  1238. const uint16_t * a = (const uint16_t *)x[i].scales;
  1239. aux[0] = a[im+0] & kmask1;
  1240. aux[1] = a[im+2] & kmask1;
  1241. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1242. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1243. #if K_QUANTS_PER_ITERATION == 2
  1244. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1245. const uint32_t * q2 = q1 + 16;
  1246. q32[0] = q1[0] & 0x0f0f0f0f;
  1247. q32[1] = q1[0] & 0xf0f0f0f0;
  1248. q32[2] = q2[0] & 0x0f0f0f0f;
  1249. q32[3] = q2[0] & 0xf0f0f0f0;
  1250. float4 s = {0.f, 0.f, 0.f, 0.f};
  1251. float smin = 0;
  1252. for (int l = 0; l < 4; ++l) {
  1253. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1254. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1255. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1256. }
  1257. 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;
  1258. #else
  1259. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1260. const uint16_t * q2 = q1 + 32;
  1261. q16[0] = q1[0] & 0x0f0f;
  1262. q16[1] = q1[0] & 0xf0f0;
  1263. q16[2] = q2[0] & 0x0f0f;
  1264. q16[3] = q2[0] & 0xf0f0;
  1265. float4 s = {0.f, 0.f, 0.f, 0.f};
  1266. float smin = 0;
  1267. for (int l = 0; l < 2; ++l) {
  1268. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1269. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1270. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1271. }
  1272. 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;
  1273. #endif
  1274. }
  1275. #else
  1276. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1277. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1278. const int step = tid * K_QUANTS_PER_ITERATION;
  1279. uint16_t aux16[2];
  1280. const uint8_t * s = (const uint8_t *)aux16;
  1281. float tmp = 0;
  1282. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1283. const uint8_t * q = x[i].qs + step;
  1284. const float * y = yy + i*QK_K + step;
  1285. const uint16_t * a = (const uint16_t *)x[i].scales;
  1286. aux16[0] = a[0] & 0x0f0f;
  1287. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1288. const float d = (float)x[i].dm[0];
  1289. const float m = (float)x[i].dm[1];
  1290. float sum = 0.f;
  1291. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1292. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1293. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1294. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1295. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1296. }
  1297. tmp += sum;
  1298. }
  1299. #endif
  1300. // sum up partial sums and write back result
  1301. #pragma unroll
  1302. for (int mask = 16; mask > 0; mask >>= 1) {
  1303. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1304. }
  1305. if (tid == 0) {
  1306. dst[row] = tmp;
  1307. }
  1308. }
  1309. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1310. const int row = blockIdx.x;
  1311. const int num_blocks_per_row = ncols / QK_K;
  1312. const int ib0 = row*num_blocks_per_row;
  1313. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  1314. float tmp = 0; // partial sum for thread in warp
  1315. #if QK_K == 256
  1316. const uint16_t kmask1 = 0x3f3f;
  1317. const uint16_t kmask2 = 0x0f0f;
  1318. const uint16_t kmask3 = 0xc0c0;
  1319. const int tid = threadIdx.x/2; // 0...15
  1320. const int ix = threadIdx.x%2;
  1321. const int il = tid/4; // 0...3
  1322. const int ir = tid - 4*il;// 0...3
  1323. const int n = 2;
  1324. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1325. const int in = il%2;
  1326. const int l0 = n*(2*ir + in);
  1327. const int q_offset = 32*im + l0;
  1328. const int y_offset = 64*im + l0;
  1329. const uint8_t hm1 = 1 << (2*im);
  1330. const uint8_t hm2 = hm1 << 4;
  1331. uint16_t aux[4];
  1332. const uint8_t * sc = (const uint8_t *)aux;
  1333. uint16_t q16[8];
  1334. const uint8_t * q4 = (const uint8_t *)q16;
  1335. for (int i = ix; i < num_blocks_per_row; i += 2) {
  1336. const uint8_t * ql1 = x[i].qs + q_offset;
  1337. const uint8_t * qh = x[i].qh + l0;
  1338. const float * y1 = yy + i*QK_K + y_offset;
  1339. const float * y2 = y1 + 128;
  1340. const float dall = __low2half(x[i].dm);
  1341. const float dmin = __high2half(x[i].dm);
  1342. const uint16_t * a = (const uint16_t *)x[i].scales;
  1343. aux[0] = a[im+0] & kmask1;
  1344. aux[1] = a[im+2] & kmask1;
  1345. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1346. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1347. float4 sum = {0.f, 0.f, 0.f, 0.f};
  1348. float smin = 0;
  1349. const uint16_t * q1 = (const uint16_t *)ql1;
  1350. const uint16_t * q2 = q1 + 32;
  1351. q16[0] = q1[0] & 0x0f0f;
  1352. q16[1] = q1[8] & 0x0f0f;
  1353. q16[2] = (q1[0] >> 4) & 0x0f0f;
  1354. q16[3] = (q1[8] >> 4) & 0x0f0f;
  1355. q16[4] = q2[0] & 0x0f0f;
  1356. q16[5] = q2[8] & 0x0f0f;
  1357. q16[6] = (q2[0] >> 4) & 0x0f0f;
  1358. q16[7] = (q2[8] >> 4) & 0x0f0f;
  1359. for (int l = 0; l < n; ++l) {
  1360. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  1361. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  1362. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  1363. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  1364. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  1365. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  1366. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  1367. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  1368. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  1369. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  1370. }
  1371. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  1372. }
  1373. #else
  1374. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1375. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1376. const int step = tid * K_QUANTS_PER_ITERATION;
  1377. const int im = step/8;
  1378. const int in = step%8;
  1379. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1380. const uint8_t * q = x[i].qs + step;
  1381. const int8_t * s = x[i].scales;
  1382. const float * y = yy + i*QK_K + step;
  1383. const float d = x[i].d;
  1384. float sum = 0.f;
  1385. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1386. const uint8_t h = x[i].qh[in+j] >> im;
  1387. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  1388. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  1389. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  1390. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  1391. }
  1392. tmp += sum;
  1393. }
  1394. #endif
  1395. // sum up partial sums and write back result
  1396. #pragma unroll
  1397. for (int mask = 16; mask > 0; mask >>= 1) {
  1398. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1399. }
  1400. if (threadIdx.x == 0) {
  1401. dst[row] = tmp;
  1402. }
  1403. }
  1404. 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) {
  1405. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1406. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1407. if (row > nrows) return;
  1408. const int num_blocks_per_row = ncols / QK_K;
  1409. const int ib0 = row*num_blocks_per_row;
  1410. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  1411. #if QK_K == 256
  1412. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1413. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  1414. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  1415. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1416. const int in = tid - step*im; // 0...15 or 0...7
  1417. #if K_QUANTS_PER_ITERATION == 1
  1418. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  1419. const int is = 0;
  1420. #else
  1421. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  1422. const int is = in / 4;
  1423. #endif
  1424. const int ql_offset = 64*im + l0;
  1425. const int qh_offset = 32*im + l0;
  1426. const int s_offset = 8*im + is;
  1427. const int y_offset = 128*im + l0;
  1428. float tmp = 0; // partial sum for thread in warp
  1429. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1430. const float * y = yy + i * QK_K + y_offset;
  1431. const uint8_t * ql = x[i].ql + ql_offset;
  1432. const uint8_t * qh = x[i].qh + qh_offset;
  1433. const int8_t * s = x[i].scales + s_offset;
  1434. const float d = x[i].d;
  1435. #if K_QUANTS_PER_ITERATION == 1
  1436. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  1437. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  1438. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  1439. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  1440. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  1441. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  1442. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  1443. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  1444. tmp += sum;
  1445. #else
  1446. float sum = 0;
  1447. for (int l = 0; l < 4; ++l) {
  1448. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  1449. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  1450. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  1451. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  1452. }
  1453. tmp += sum;
  1454. #endif
  1455. }
  1456. #else
  1457. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  1458. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  1459. const int step = tid * K_QUANTS_PER_ITERATION;
  1460. float tmp = 0; // partial sum for thread in warp
  1461. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1462. const float * y = yy + i * QK_K + step;
  1463. const uint8_t * ql = x[i].ql + step;
  1464. const uint8_t * qh = x[i].qh + step;
  1465. const int8_t * s = x[i].scales;
  1466. const float d = x[i+0].d;
  1467. float sum = 0;
  1468. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1469. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  1470. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  1471. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  1472. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  1473. }
  1474. tmp += sum;
  1475. }
  1476. #endif
  1477. // sum up partial sums and write back result
  1478. #pragma unroll
  1479. for (int mask = 16; mask > 0; mask >>= 1) {
  1480. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1481. }
  1482. if (tid == 0) {
  1483. dst[row] = tmp;
  1484. }
  1485. }
  1486. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1487. const half * x = (const half *) vx;
  1488. // automatic half -> float type cast if dfloat == float
  1489. v.x = x[ib + iqs + 0];
  1490. v.y = x[ib + iqs + 1];
  1491. }
  1492. static __device__ void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  1493. const float * x = (const float *) vx;
  1494. // automatic half -> float type cast if dfloat == float
  1495. v.x = x[ib + iqs + 0];
  1496. v.y = x[ib + iqs + 1];
  1497. }
  1498. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  1499. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  1500. if (ix >= kx_padded) {
  1501. return;
  1502. }
  1503. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  1504. const int i_padded = iy*kx_padded + ix;
  1505. block_q8_1 * y = (block_q8_1 *) vy;
  1506. const int ib = i_padded / QK8_1; // block index
  1507. const int iqs = i_padded % QK8_1; // quant index
  1508. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  1509. float amax = fabsf(xi);
  1510. float sum = xi;
  1511. #pragma unroll
  1512. for (int mask = 16; mask > 0; mask >>= 1) {
  1513. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  1514. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  1515. }
  1516. const float d = amax / 127;
  1517. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  1518. y[ib].qs[iqs] = q;
  1519. if (iqs > 0) {
  1520. return;
  1521. }
  1522. reinterpret_cast<half&>(y[ib].ds.x) = d;
  1523. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  1524. }
  1525. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1526. static __global__ void k_get_rows(
  1527. const void * src0, const int32_t * src1, dst_t * dst,
  1528. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1529. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1530. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1531. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1532. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1533. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  1534. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1535. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1536. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1537. if (i00 >= ne00) {
  1538. return;
  1539. }
  1540. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1541. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1542. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1543. const int ib = i00/qk; // block index
  1544. const int iqs = (i00%qk)/qr; // quant index
  1545. const int iybs = i00 - i00%qk; // dst block start index
  1546. const int y_offset = qr == 1 ? 1 : qk/2;
  1547. // dequantize
  1548. dfloat2 v;
  1549. dequantize_kernel(src0_row, ib, iqs, v);
  1550. dst_row[iybs + iqs + 0] = v.x;
  1551. dst_row[iybs + iqs + y_offset] = v.y;
  1552. }
  1553. template<typename src0_t, typename dst_t>
  1554. static __global__ void k_get_rows_float(
  1555. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1556. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1557. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1558. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1559. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1560. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  1561. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  1562. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  1563. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  1564. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  1565. if (i00 >= ne00) {
  1566. return;
  1567. }
  1568. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1569. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1570. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1571. dst_row[i00] = src0_row[i00];
  1572. }
  1573. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1574. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  1575. const int i = blockDim.x*blockIdx.x + 2*threadIdx.x;
  1576. if (i >= k) {
  1577. return;
  1578. }
  1579. const int ib = i/qk; // block index
  1580. const int iqs = (i%qk)/qr; // quant index
  1581. const int iybs = i - i%qk; // y block start index
  1582. const int y_offset = qr == 1 ? 1 : qk/2;
  1583. // dequantize
  1584. dfloat2 v;
  1585. dequantize_kernel(vx, ib, iqs, v);
  1586. y[iybs + iqs + 0] = v.x;
  1587. y[iybs + iqs + y_offset] = v.y;
  1588. }
  1589. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  1590. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  1591. #define VDR_Q4_0_Q8_1_MMVQ 2
  1592. #define VDR_Q4_0_Q8_1_MMQ 4
  1593. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  1594. const int * v, const int * u, const float & d4, const half2 & ds8) {
  1595. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1596. int sumi = 0;
  1597. #pragma unroll
  1598. for (int i = 0; i < vdr; ++i) {
  1599. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1600. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1601. // SIMD dot product of quantized values
  1602. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1603. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1604. }
  1605. const float2 ds8f = __half22float2(ds8);
  1606. // second part effectively subtracts 8 from each quant value
  1607. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  1608. #else
  1609. assert(false);
  1610. return 0.0f; // only to satisfy the compiler
  1611. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1612. }
  1613. #define VDR_Q4_1_Q8_1_MMVQ 2
  1614. #define VDR_Q4_1_Q8_1_MMQ 4
  1615. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  1616. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  1617. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1618. int sumi = 0;
  1619. #pragma unroll
  1620. for (int i = 0; i < vdr; ++i) {
  1621. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  1622. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  1623. // SIMD dot product of quantized values
  1624. sumi = __dp4a(vi0, u[2*i+0], sumi);
  1625. sumi = __dp4a(vi1, u[2*i+1], sumi);
  1626. }
  1627. #ifdef GGML_CUDA_F16
  1628. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  1629. const float d4d8 = tmp.x;
  1630. const float m4s8 = tmp.y;
  1631. #else
  1632. const float2 dm4f = __half22float2(dm4);
  1633. const float2 ds8f = __half22float2(ds8);
  1634. const float d4d8 = dm4f.x * ds8f.x;
  1635. const float m4s8 = dm4f.y * ds8f.y;
  1636. #endif // GGML_CUDA_F16
  1637. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  1638. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  1639. #else
  1640. assert(false);
  1641. return 0.0f; // only to satisfy the compiler
  1642. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1643. }
  1644. #define VDR_Q5_0_Q8_1_MMVQ 2
  1645. #define VDR_Q5_0_Q8_1_MMQ 4
  1646. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  1647. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  1648. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1649. int sumi = 0;
  1650. #pragma unroll
  1651. for (int i = 0; i < vdr; ++i) {
  1652. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1653. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1654. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1655. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1656. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1657. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1658. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1659. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1660. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1661. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1662. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1663. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1664. }
  1665. const float2 ds8f = __half22float2(ds8);
  1666. // second part effectively subtracts 16 from each quant value
  1667. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  1668. #else
  1669. assert(false);
  1670. return 0.0f; // only to satisfy the compiler
  1671. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1672. }
  1673. #define VDR_Q5_1_Q8_1_MMVQ 2
  1674. #define VDR_Q5_1_Q8_1_MMQ 4
  1675. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  1676. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  1677. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1678. int sumi = 0;
  1679. #pragma unroll
  1680. for (int i = 0; i < vdr; ++i) {
  1681. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  1682. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  1683. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  1684. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  1685. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  1686. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  1687. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  1688. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  1689. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  1690. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  1691. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  1692. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  1693. }
  1694. #ifdef GGML_CUDA_F16
  1695. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  1696. const float d5d8 = tmp.x;
  1697. const float m5s8 = tmp.y;
  1698. #else
  1699. const float2 dm5f = __half22float2(dm5);
  1700. const float2 ds8f = __half22float2(ds8);
  1701. const float d5d8 = dm5f.x * ds8f.x;
  1702. const float m5s8 = dm5f.y * ds8f.y;
  1703. #endif // GGML_CUDA_F16
  1704. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  1705. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  1706. #else
  1707. assert(false);
  1708. return 0.0f; // only to satisfy the compiler
  1709. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1710. }
  1711. #define VDR_Q8_0_Q8_1_MMVQ 2
  1712. #define VDR_Q8_0_Q8_1_MMQ 8
  1713. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  1714. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  1715. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1716. int sumi = 0;
  1717. #pragma unroll
  1718. for (int i = 0; i < vdr; ++i) {
  1719. // SIMD dot product of quantized values
  1720. sumi = __dp4a(v[i], u[i], sumi);
  1721. }
  1722. return d8_0*d8_1 * sumi;
  1723. #else
  1724. assert(false);
  1725. return 0.0f; // only to satisfy the compiler
  1726. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1727. }
  1728. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  1729. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  1730. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1731. int sumi = 0;
  1732. #pragma unroll
  1733. for (int i = 0; i < vdr; ++i) {
  1734. // SIMD dot product of quantized values
  1735. sumi = __dp4a(v[i], u[i], sumi);
  1736. }
  1737. #ifdef GGML_CUDA_F16
  1738. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  1739. const float d8d8 = tmp.x;
  1740. const float m8s8 = tmp.y;
  1741. #else
  1742. const float2 dm8f = __half22float2(dm8);
  1743. const float2 ds8f = __half22float2(ds8);
  1744. const float d8d8 = dm8f.x * ds8f.x;
  1745. const float m8s8 = dm8f.y * ds8f.y;
  1746. #endif // GGML_CUDA_F16
  1747. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  1748. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  1749. #else
  1750. assert(false);
  1751. return 0.0f; // only to satisfy the compiler
  1752. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1753. }
  1754. #define VDR_Q2_K_Q8_1_MMVQ 1
  1755. #define VDR_Q2_K_Q8_1_MMQ 2
  1756. // contiguous v/x values
  1757. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  1758. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1759. const half2 & dm2, const float * __restrict__ d8) {
  1760. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1761. float sumf_d = 0.0f;
  1762. float sumf_m = 0.0f;
  1763. #pragma unroll
  1764. for (int i = 0; i < QR2_K; ++i) {
  1765. const int sc = scales[2*i];
  1766. const int vi = (v >> (2*i)) & 0x03030303;
  1767. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  1768. // fill int with 4x m
  1769. int m = sc >> 4;
  1770. m |= m << 8;
  1771. m |= m << 16;
  1772. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  1773. }
  1774. const float2 dm2f = __half22float2(dm2);
  1775. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  1776. #else
  1777. assert(false);
  1778. return 0.0f; // only to satisfy the compiler
  1779. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1780. }
  1781. // contiguous u/y values
  1782. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  1783. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1784. const half2 & dm2, const float & d8) {
  1785. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1786. int sumi_d = 0;
  1787. int sumi_m = 0;
  1788. #pragma unroll
  1789. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  1790. int sumi_d_sc = 0;
  1791. const int sc = scales[i0 / (QI8_1/2)];
  1792. // fill int with 4x m
  1793. int m = sc >> 4;
  1794. m |= m << 8;
  1795. m |= m << 16;
  1796. #pragma unroll
  1797. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1798. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  1799. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  1800. }
  1801. sumi_d += sumi_d_sc * (sc & 0xF);
  1802. }
  1803. const float2 dm2f = __half22float2(dm2);
  1804. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  1805. #else
  1806. assert(false);
  1807. return 0.0f; // only to satisfy the compiler
  1808. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1809. }
  1810. #define VDR_Q3_K_Q8_1_MMVQ 1
  1811. #define VDR_Q3_K_Q8_1_MMQ 2
  1812. // contiguous v/x values
  1813. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  1814. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  1815. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  1816. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1817. float sumf = 0.0f;
  1818. #pragma unroll
  1819. for (int i = 0; i < QR3_K; ++i) {
  1820. const int isc = scale_offset + 2*i;
  1821. const int isc_low = isc % (QK_K/32);
  1822. const int sc_shift_low = 4 * (isc / (QK_K/32));
  1823. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  1824. const int isc_high = isc % (QK_K/64);
  1825. const int sc_shift_high = 2 * (isc / (QK_K/64));
  1826. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  1827. const int sc = (sc_low | sc_high) - 32;
  1828. const int vil = (vl >> (2*i)) & 0x03030303;
  1829. const int vih = ((vh >> i) << 2) & 0x04040404;
  1830. const int vi = __vsubss4(vil, vih);
  1831. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1832. }
  1833. return d3 * sumf;
  1834. #else
  1835. assert(false);
  1836. return 0.0f; // only to satisfy the compiler
  1837. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1838. }
  1839. // contiguous u/y values
  1840. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  1841. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1842. const float & d3, const float & d8) {
  1843. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1844. int sumi = 0;
  1845. #pragma unroll
  1846. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  1847. int sumi_sc = 0;
  1848. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  1849. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  1850. }
  1851. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  1852. }
  1853. return d3*d8 * sumi;
  1854. #else
  1855. assert(false);
  1856. return 0.0f; // only to satisfy the compiler
  1857. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1858. }
  1859. #define VDR_Q4_K_Q8_1_MMVQ 2
  1860. #define VDR_Q4_K_Q8_1_MMQ 8
  1861. // contiguous v/x values
  1862. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  1863. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1864. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  1865. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1866. float sumf_d = 0.0f;
  1867. float sumf_m = 0.0f;
  1868. #pragma unroll
  1869. for (int i = 0; i < QR4_K; ++i) {
  1870. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  1871. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  1872. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  1873. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  1874. sumf_d += d8[i] * (dot1 * sc[i]);
  1875. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  1876. }
  1877. const float2 dm4f = __half22float2(dm4);
  1878. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1879. #else
  1880. assert(false);
  1881. return 0.0f; // only to satisfy the compiler
  1882. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1883. }
  1884. // contiguous u/y values
  1885. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  1886. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1887. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1888. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1889. float sumf_d = 0.0f;
  1890. float sumf_m = 0.0f;
  1891. #pragma unroll
  1892. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  1893. int sumi_d = 0;
  1894. #pragma unroll
  1895. for (int j = 0; j < QI8_1; ++j) {
  1896. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1897. }
  1898. const float2 ds8f = __half22float2(ds8[i]);
  1899. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1900. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1901. }
  1902. const float2 dm4f = __half22float2(dm4);
  1903. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1904. #else
  1905. assert(false);
  1906. return 0.0f; // only to satisfy the compiler
  1907. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1908. }
  1909. #define VDR_Q5_K_Q8_1_MMVQ 2
  1910. #define VDR_Q5_K_Q8_1_MMQ 8
  1911. // contiguous v/x values
  1912. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  1913. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1914. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  1915. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1916. float sumf_d = 0.0f;
  1917. float sumf_m = 0.0f;
  1918. #pragma unroll
  1919. for (int i = 0; i < QR5_K; ++i) {
  1920. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  1921. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  1922. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  1923. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  1924. const int v0i = vl0i | vh0i;
  1925. const int v1i = vl1i | vh1i;
  1926. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  1927. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  1928. sumf_d += d8[i] * (dot1 * sc[i]);
  1929. sumf_m += d8[i] * (dot2 * m[i]);
  1930. }
  1931. const float2 dm5f = __half22float2(dm5);
  1932. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  1933. #else
  1934. assert(false);
  1935. return 0.0f; // only to satisfy the compiler
  1936. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1937. }
  1938. // contiguous u/y values
  1939. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  1940. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  1941. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  1942. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1943. float sumf_d = 0.0f;
  1944. float sumf_m = 0.0f;
  1945. #pragma unroll
  1946. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  1947. int sumi_d = 0;
  1948. #pragma unroll
  1949. for (int j = 0; j < QI8_1; ++j) {
  1950. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  1951. }
  1952. const float2 ds8f = __half22float2(ds8[i]);
  1953. sumf_d += ds8f.x * (sc[i] * sumi_d);
  1954. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  1955. }
  1956. const float2 dm4f = __half22float2(dm4);
  1957. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  1958. #else
  1959. assert(false);
  1960. return 0.0f; // only to satisfy the compiler
  1961. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1962. }
  1963. #define VDR_Q6_K_Q8_1_MMVQ 1
  1964. #define VDR_Q6_K_Q8_1_MMQ 8
  1965. // contiguous v/x values
  1966. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  1967. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  1968. const float & d, const float * __restrict__ d8) {
  1969. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1970. float sumf = 0.0f;
  1971. #pragma unroll
  1972. for (int i = 0; i < QR6_K; ++i) {
  1973. const int sc = scales[4*i];
  1974. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  1975. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  1976. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  1977. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  1978. }
  1979. return d*sumf;
  1980. #else
  1981. assert(false);
  1982. return 0.0f; // only to satisfy the compiler
  1983. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  1984. }
  1985. // contiguous u/y values
  1986. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  1987. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  1988. const float & d6, const float * __restrict__ d8) {
  1989. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1990. float sumf_d = 0.0f;
  1991. #pragma unroll
  1992. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  1993. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  1994. #pragma unroll
  1995. for (int i = i0; i < i0 + 2; ++i) {
  1996. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  1997. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  1998. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  1999. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2000. }
  2001. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2002. }
  2003. return d6 * sumf_d;
  2004. #else
  2005. assert(false);
  2006. return 0.0f; // only to satisfy the compiler
  2007. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2008. }
  2009. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2010. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2011. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2012. int v[VDR_Q4_0_Q8_1_MMVQ];
  2013. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2014. #pragma unroll
  2015. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2016. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2017. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2018. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2019. }
  2020. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2021. }
  2022. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2023. (void)x_qh; (void)x_sc;
  2024. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2025. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2026. *x_ql = tile_x_qs;
  2027. *x_dm = (half2 *) tile_x_d;
  2028. }
  2029. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2030. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2031. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2032. (void)x_qh; (void)x_sc;
  2033. GGML_CUDA_ASSUME(i_offset >= 0);
  2034. GGML_CUDA_ASSUME(i_offset < nwarps);
  2035. GGML_CUDA_ASSUME(k >= 0);
  2036. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2037. const int kbx = k / QI4_0;
  2038. const int kqsx = k % QI4_0;
  2039. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2040. float * x_dmf = (float *) x_dm;
  2041. #pragma unroll
  2042. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2043. int i = i0 + i_offset;
  2044. if (need_check) {
  2045. i = min(i, i_max);
  2046. }
  2047. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2048. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2049. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2050. }
  2051. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2052. const int kbxd = k % blocks_per_tile_x_row;
  2053. #pragma unroll
  2054. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2055. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2056. if (need_check) {
  2057. i = min(i, i_max);
  2058. }
  2059. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2060. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2061. }
  2062. }
  2063. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2064. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2065. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2066. (void)x_qh; (void)x_sc;
  2067. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2068. const float * x_dmf = (const float *) x_dm;
  2069. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2070. #pragma unroll
  2071. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2072. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2073. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2074. }
  2075. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2076. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2077. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2078. }
  2079. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2080. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2081. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2082. int v[VDR_Q4_1_Q8_1_MMVQ];
  2083. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2084. #pragma unroll
  2085. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2086. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2087. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2088. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2089. }
  2090. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2091. }
  2092. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2093. (void)x_qh; (void)x_sc;
  2094. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2095. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2096. *x_ql = tile_x_qs;
  2097. *x_dm = tile_x_dm;
  2098. }
  2099. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2100. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2101. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2102. (void)x_qh; (void)x_sc;
  2103. GGML_CUDA_ASSUME(i_offset >= 0);
  2104. GGML_CUDA_ASSUME(i_offset < nwarps);
  2105. GGML_CUDA_ASSUME(k >= 0);
  2106. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2107. const int kbx = k / QI4_1;
  2108. const int kqsx = k % QI4_1;
  2109. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2110. #pragma unroll
  2111. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2112. int i = i0 + i_offset;
  2113. if (need_check) {
  2114. i = min(i, i_max);
  2115. }
  2116. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2117. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2118. }
  2119. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2120. const int kbxd = k % blocks_per_tile_x_row;
  2121. #pragma unroll
  2122. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2123. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2124. if (need_check) {
  2125. i = min(i, i_max);
  2126. }
  2127. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2128. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2129. }
  2130. }
  2131. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2132. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2133. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2134. (void)x_qh; (void)x_sc;
  2135. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2136. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2137. #pragma unroll
  2138. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2139. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2140. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2141. }
  2142. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2143. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2144. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2145. }
  2146. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2147. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2148. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2149. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2150. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2151. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2152. #pragma unroll
  2153. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2154. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2155. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2156. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2157. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2158. }
  2159. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2160. }
  2161. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2162. (void)x_qh; (void)x_sc;
  2163. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2164. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2165. *x_ql = tile_x_ql;
  2166. *x_dm = (half2 *) tile_x_d;
  2167. }
  2168. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2169. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2170. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2171. (void)x_qh; (void)x_sc;
  2172. GGML_CUDA_ASSUME(i_offset >= 0);
  2173. GGML_CUDA_ASSUME(i_offset < nwarps);
  2174. GGML_CUDA_ASSUME(k >= 0);
  2175. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2176. const int kbx = k / QI5_0;
  2177. const int kqsx = k % QI5_0;
  2178. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2179. #pragma unroll
  2180. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2181. int i = i0 + i_offset;
  2182. if (need_check) {
  2183. i = min(i, i_max);
  2184. }
  2185. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2186. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2187. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2188. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2189. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2190. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2191. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2192. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2193. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2194. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2195. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2196. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2197. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2198. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2199. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2200. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2201. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2202. }
  2203. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2204. const int kbxd = k % blocks_per_tile_x_row;
  2205. float * x_dmf = (float *) x_dm;
  2206. #pragma unroll
  2207. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2208. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2209. if (need_check) {
  2210. i = min(i, i_max);
  2211. }
  2212. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2213. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2214. }
  2215. }
  2216. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2217. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2218. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2219. (void)x_qh; (void)x_sc;
  2220. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2221. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2222. const float * x_dmf = (const float *) x_dm;
  2223. const float * y_df = (const float *) y_ds;
  2224. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2225. #pragma unroll
  2226. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2227. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2228. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2229. }
  2230. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2231. (&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)]);
  2232. }
  2233. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2234. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2235. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2236. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2237. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2238. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2239. #pragma unroll
  2240. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2241. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2242. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2243. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2244. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2245. }
  2246. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2247. }
  2248. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2249. (void)x_qh; (void)x_sc;
  2250. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2251. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2252. *x_ql = tile_x_ql;
  2253. *x_dm = tile_x_dm;
  2254. }
  2255. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2256. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2257. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2258. (void)x_qh; (void)x_sc;
  2259. GGML_CUDA_ASSUME(i_offset >= 0);
  2260. GGML_CUDA_ASSUME(i_offset < nwarps);
  2261. GGML_CUDA_ASSUME(k >= 0);
  2262. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2263. const int kbx = k / QI5_1;
  2264. const int kqsx = k % QI5_1;
  2265. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2266. #pragma unroll
  2267. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2268. int i = i0 + i_offset;
  2269. if (need_check) {
  2270. i = min(i, i_max);
  2271. }
  2272. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2273. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2274. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2275. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2276. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2277. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2278. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2279. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2280. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2281. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2282. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2283. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2284. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2285. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2286. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2287. }
  2288. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2289. const int kbxd = k % blocks_per_tile_x_row;
  2290. #pragma unroll
  2291. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2292. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  2293. if (need_check) {
  2294. i = min(i, i_max);
  2295. }
  2296. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2297. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  2298. }
  2299. }
  2300. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  2301. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2302. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2303. (void)x_qh; (void)x_sc;
  2304. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2305. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  2306. int u[2*VDR_Q5_1_Q8_1_MMQ];
  2307. #pragma unroll
  2308. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  2309. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2310. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  2311. }
  2312. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  2313. (&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)]);
  2314. }
  2315. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  2316. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2317. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  2318. int v[VDR_Q8_0_Q8_1_MMVQ];
  2319. int u[VDR_Q8_0_Q8_1_MMVQ];
  2320. #pragma unroll
  2321. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  2322. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  2323. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2324. }
  2325. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  2326. }
  2327. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2328. (void)x_qh; (void)x_sc;
  2329. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2330. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  2331. *x_ql = tile_x_qs;
  2332. *x_dm = (half2 *) tile_x_d;
  2333. }
  2334. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  2335. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2336. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2337. (void)x_qh; (void)x_sc;
  2338. GGML_CUDA_ASSUME(i_offset >= 0);
  2339. GGML_CUDA_ASSUME(i_offset < nwarps);
  2340. GGML_CUDA_ASSUME(k >= 0);
  2341. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2342. const int kbx = k / QI8_0;
  2343. const int kqsx = k % QI8_0;
  2344. float * x_dmf = (float *) x_dm;
  2345. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  2346. #pragma unroll
  2347. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2348. int i = i0 + i_offset;
  2349. if (need_check) {
  2350. i = min(i, i_max);
  2351. }
  2352. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2353. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  2354. }
  2355. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  2356. const int kbxd = k % blocks_per_tile_x_row;
  2357. #pragma unroll
  2358. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  2359. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  2360. if (need_check) {
  2361. i = min(i, i_max);
  2362. }
  2363. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2364. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  2365. }
  2366. }
  2367. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  2368. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2369. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2370. (void)x_qh; (void)x_sc;
  2371. const float * x_dmf = (const float *) x_dm;
  2372. const float * y_df = (const float *) y_ds;
  2373. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  2374. (&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],
  2375. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  2376. }
  2377. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  2378. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2379. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  2380. const int bq8_offset = QR2_K * (iqs / QI8_1);
  2381. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2382. const uint8_t * scales = bq2_K->scales + scale_offset;
  2383. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  2384. int u[QR2_K];
  2385. float d8[QR2_K];
  2386. #pragma unroll
  2387. for (int i = 0; i < QR2_K; ++ i) {
  2388. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2389. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2390. }
  2391. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  2392. }
  2393. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2394. (void)x_qh;
  2395. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2396. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  2397. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2398. *x_ql = tile_x_ql;
  2399. *x_dm = tile_x_dm;
  2400. *x_sc = tile_x_sc;
  2401. }
  2402. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  2403. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2404. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2405. (void)x_qh;
  2406. GGML_CUDA_ASSUME(i_offset >= 0);
  2407. GGML_CUDA_ASSUME(i_offset < nwarps);
  2408. GGML_CUDA_ASSUME(k >= 0);
  2409. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2410. const int kbx = k / QI2_K;
  2411. const int kqsx = k % QI2_K;
  2412. const block_q2_K * bx0 = (const block_q2_K *) vx;
  2413. #pragma unroll
  2414. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2415. int i = i0 + i_offset;
  2416. if (need_check) {
  2417. i = min(i, i_max);
  2418. }
  2419. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  2420. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2421. }
  2422. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  2423. const int kbxd = k % blocks_per_tile_x_row;
  2424. #pragma unroll
  2425. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  2426. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  2427. if (need_check) {
  2428. i = min(i, i_max);
  2429. }
  2430. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2431. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  2432. }
  2433. #pragma unroll
  2434. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2435. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2436. if (need_check) {
  2437. i = min(i, i_max);
  2438. }
  2439. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  2440. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  2441. }
  2442. }
  2443. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  2444. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2445. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2446. (void)x_qh;
  2447. const int kbx = k / QI2_K;
  2448. const int ky = (k % QI2_K) * QR2_K;
  2449. const float * y_df = (const float *) y_ds;
  2450. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  2451. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  2452. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  2453. #pragma unroll
  2454. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  2455. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2456. }
  2457. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  2458. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  2459. 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]);
  2460. }
  2461. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  2462. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2463. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  2464. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  2465. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  2466. const float d = bq3_K->d;
  2467. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  2468. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2469. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  2470. int u[QR3_K];
  2471. float d8[QR3_K];
  2472. #pragma unroll
  2473. for (int i = 0; i < QR3_K; ++i) {
  2474. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  2475. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  2476. }
  2477. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  2478. }
  2479. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2480. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2481. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  2482. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  2483. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  2484. *x_ql = tile_x_ql;
  2485. *x_dm = tile_x_dm;
  2486. *x_qh = tile_x_qh;
  2487. *x_sc = tile_x_sc;
  2488. }
  2489. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  2490. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2491. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2492. GGML_CUDA_ASSUME(i_offset >= 0);
  2493. GGML_CUDA_ASSUME(i_offset < nwarps);
  2494. GGML_CUDA_ASSUME(k >= 0);
  2495. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2496. const int kbx = k / QI3_K;
  2497. const int kqsx = k % QI3_K;
  2498. const block_q3_K * bx0 = (const block_q3_K *) vx;
  2499. #pragma unroll
  2500. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2501. int i = i0 + i_offset;
  2502. if (need_check) {
  2503. i = min(i, i_max);
  2504. }
  2505. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  2506. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2507. }
  2508. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  2509. const int kbxd = k % blocks_per_tile_x_row;
  2510. float * x_dmf = (float *) x_dm;
  2511. #pragma unroll
  2512. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  2513. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  2514. if (need_check) {
  2515. i = min(i, i_max);
  2516. }
  2517. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2518. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  2519. }
  2520. #pragma unroll
  2521. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  2522. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  2523. if (need_check) {
  2524. i = min(i, i_max);
  2525. }
  2526. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  2527. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  2528. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  2529. }
  2530. #pragma unroll
  2531. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  2532. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  2533. if (need_check) {
  2534. i = min(i, i_max);
  2535. }
  2536. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  2537. const int ksc = k % (QI3_K/4);
  2538. const int ksc_low = ksc % (QI3_K/8);
  2539. const int shift_low = 4 * (ksc / (QI3_K/8));
  2540. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  2541. const int ksc_high = QI3_K/8;
  2542. const int shift_high = 2 * ksc;
  2543. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  2544. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  2545. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  2546. }
  2547. }
  2548. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  2549. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2550. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2551. const int kbx = k / QI3_K;
  2552. const int ky = (k % QI3_K) * QR3_K;
  2553. const float * x_dmf = (const float *) x_dm;
  2554. const float * y_df = (const float *) y_ds;
  2555. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  2556. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  2557. #pragma unroll
  2558. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  2559. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  2560. const int shift = 2 * ((ky % 32) / 8);
  2561. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  2562. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  2563. const int vlh = (vh << 2) & 0x04040404;
  2564. v[l] = __vsubss4(vll, vlh);
  2565. }
  2566. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  2567. 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]);
  2568. }
  2569. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  2570. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2571. #ifndef GGML_QKK_64
  2572. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2573. int v[2];
  2574. int u[2*QR4_K];
  2575. float d8[QR4_K];
  2576. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  2577. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  2578. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  2579. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  2580. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  2581. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  2582. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2583. v[0] = q4[0];
  2584. v[1] = q4[4];
  2585. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  2586. uint16_t aux[2];
  2587. const int j = bq8_offset/2;
  2588. if (j < 2) {
  2589. aux[0] = scales[j+0] & 0x3f3f;
  2590. aux[1] = scales[j+2] & 0x3f3f;
  2591. } else {
  2592. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2593. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2594. }
  2595. const uint8_t * sc = (const uint8_t *)aux;
  2596. const uint8_t * m = sc + 2;
  2597. for (int i = 0; i < QR4_K; ++i) {
  2598. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2599. d8[i] = __low2half(bq8i->ds);
  2600. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2601. u[2*i+0] = q8[0];
  2602. u[2*i+1] = q8[4];
  2603. }
  2604. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  2605. #else
  2606. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2607. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  2608. float sumf_d = 0.0f;
  2609. float sumf_m = 0.0f;
  2610. uint16_t aux16[2];
  2611. const uint8_t * s = (const uint8_t *)aux16;
  2612. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  2613. aux16[0] = a[0] & 0x0f0f;
  2614. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2615. const float dall = bq4_K->dm[0];
  2616. const float dmin = bq4_K->dm[1];
  2617. const float d8_1 = __low2float(bq8_1[0].ds);
  2618. const float d8_2 = __low2float(bq8_1[1].ds);
  2619. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2620. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2621. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2622. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2623. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  2624. const int v1 = q4[0];
  2625. const int v2 = q4[4];
  2626. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  2627. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  2628. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  2629. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  2630. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  2631. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  2632. return dall * sumf_d - dmin * sumf_m;
  2633. #else
  2634. assert(false);
  2635. return 0.0f; // only to satisfy the compiler
  2636. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2637. #endif
  2638. }
  2639. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2640. (void)x_qh;
  2641. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  2642. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  2643. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2644. *x_ql = tile_x_ql;
  2645. *x_dm = tile_x_dm;
  2646. *x_sc = tile_x_sc;
  2647. }
  2648. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  2649. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2650. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2651. (void)x_qh;
  2652. GGML_CUDA_ASSUME(i_offset >= 0);
  2653. GGML_CUDA_ASSUME(i_offset < nwarps);
  2654. GGML_CUDA_ASSUME(k >= 0);
  2655. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2656. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  2657. const int kqsx = k % QI4_K; // == k if QK_K == 256
  2658. const block_q4_K * bx0 = (const block_q4_K *) vx;
  2659. #pragma unroll
  2660. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2661. int i = i0 + i_offset;
  2662. if (need_check) {
  2663. i = min(i, i_max);
  2664. }
  2665. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  2666. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2667. }
  2668. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  2669. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2670. #pragma unroll
  2671. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  2672. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  2673. if (need_check) {
  2674. i = min(i, i_max);
  2675. }
  2676. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2677. #if QK_K == 256
  2678. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  2679. #else
  2680. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  2681. #endif
  2682. }
  2683. #pragma unroll
  2684. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2685. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2686. if (need_check) {
  2687. i = min(i, i_max);
  2688. }
  2689. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  2690. const int * scales = (const int *) bxi->scales;
  2691. const int ksc = k % (WARP_SIZE/8);
  2692. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2693. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2694. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2695. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2696. }
  2697. }
  2698. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  2699. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2700. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2701. (void)x_qh;
  2702. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  2703. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  2704. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  2705. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  2706. }
  2707. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  2708. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2709. #ifndef GGML_QKK_64
  2710. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2711. int vl[2];
  2712. int vh[2];
  2713. int u[2*QR5_K];
  2714. float d8[QR5_K];
  2715. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  2716. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  2717. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  2718. vl[0] = ql[0];
  2719. vl[1] = ql[4];
  2720. vh[0] = qh[0] >> bq8_offset;
  2721. vh[1] = qh[4] >> bq8_offset;
  2722. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  2723. uint16_t aux[2];
  2724. const int j = bq8_offset/2;
  2725. if (j < 2) {
  2726. aux[0] = scales[j+0] & 0x3f3f;
  2727. aux[1] = scales[j+2] & 0x3f3f;
  2728. } else {
  2729. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  2730. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  2731. }
  2732. const uint8_t * sc = (const uint8_t *)aux;
  2733. const uint8_t * m = sc + 2;
  2734. #pragma unroll
  2735. for (int i = 0; i < QR5_K; ++i) {
  2736. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  2737. d8[i] = __low2float(bq8i->ds);
  2738. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  2739. u[2*i+0] = q8[0];
  2740. u[2*i+1] = q8[4];
  2741. }
  2742. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  2743. #else
  2744. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2745. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  2746. const int8_t * s = bq5_K->scales;
  2747. const float d = bq5_K->d;
  2748. const float d8_1 = __low2half(bq8_1[0].ds);
  2749. const float d8_2 = __low2half(bq8_1[1].ds);
  2750. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  2751. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  2752. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  2753. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  2754. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  2755. const int vl1 = ql[0];
  2756. const int vl2 = ql[4];
  2757. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  2758. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  2759. const int in = step%8; // 0, 4, 0, 4
  2760. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  2761. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  2762. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  2763. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  2764. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  2765. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  2766. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  2767. return d * sumf_d;
  2768. #else
  2769. assert(false);
  2770. return 0.0f; // only to satisfy the compiler
  2771. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2772. #endif
  2773. }
  2774. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2775. (void)x_qh;
  2776. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2777. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  2778. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2779. *x_ql = tile_x_ql;
  2780. *x_dm = tile_x_dm;
  2781. *x_sc = tile_x_sc;
  2782. }
  2783. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  2784. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2785. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2786. (void)x_qh;
  2787. GGML_CUDA_ASSUME(i_offset >= 0);
  2788. GGML_CUDA_ASSUME(i_offset < nwarps);
  2789. GGML_CUDA_ASSUME(k >= 0);
  2790. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2791. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  2792. const int kqsx = k % QI5_K; // == k if QK_K == 256
  2793. const block_q5_K * bx0 = (const block_q5_K *) vx;
  2794. #pragma unroll
  2795. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2796. int i = i0 + i_offset;
  2797. if (need_check) {
  2798. i = min(i, i_max);
  2799. }
  2800. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  2801. const int ky = QR5_K*kqsx;
  2802. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2803. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2804. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2805. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  2806. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  2807. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  2808. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  2809. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  2810. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  2811. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  2812. }
  2813. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  2814. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2815. #pragma unroll
  2816. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  2817. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  2818. if (need_check) {
  2819. i = min(i, i_max);
  2820. }
  2821. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2822. #if QK_K == 256
  2823. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  2824. #endif
  2825. }
  2826. #pragma unroll
  2827. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2828. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2829. if (need_check) {
  2830. i = min(i, i_max);
  2831. }
  2832. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  2833. const int * scales = (const int *) bxi->scales;
  2834. const int ksc = k % (WARP_SIZE/8);
  2835. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  2836. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  2837. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  2838. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  2839. }
  2840. }
  2841. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  2842. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2843. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2844. (void)x_qh;
  2845. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  2846. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  2847. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  2848. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  2849. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  2850. }
  2851. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  2852. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2853. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  2854. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  2855. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  2856. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  2857. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  2858. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  2859. const int8_t * scales = bq6_K->scales + scale_offset;
  2860. int u[QR6_K];
  2861. float d8[QR6_K];
  2862. #pragma unroll
  2863. for (int i = 0; i < QR6_K; ++i) {
  2864. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  2865. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  2866. }
  2867. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  2868. }
  2869. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2870. (void)x_qh;
  2871. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2872. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  2873. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  2874. *x_ql = tile_x_ql;
  2875. *x_dm = tile_x_dm;
  2876. *x_sc = tile_x_sc;
  2877. }
  2878. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  2879. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2880. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2881. (void)x_qh;
  2882. GGML_CUDA_ASSUME(i_offset >= 0);
  2883. GGML_CUDA_ASSUME(i_offset < nwarps);
  2884. GGML_CUDA_ASSUME(k >= 0);
  2885. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2886. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  2887. const int kqsx = k % QI6_K; // == k if QK_K == 256
  2888. const block_q6_K * bx0 = (const block_q6_K *) vx;
  2889. #pragma unroll
  2890. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2891. int i = i0 + i_offset;
  2892. if (need_check) {
  2893. i = min(i, i_max);
  2894. }
  2895. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  2896. const int ky = QR6_K*kqsx;
  2897. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  2898. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  2899. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  2900. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  2901. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  2902. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  2903. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  2904. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  2905. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  2906. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  2907. }
  2908. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  2909. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  2910. float * x_dmf = (float *) x_dm;
  2911. #pragma unroll
  2912. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  2913. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  2914. if (need_check) {
  2915. i = min(i, i_max);
  2916. }
  2917. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  2918. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  2919. }
  2920. #pragma unroll
  2921. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  2922. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  2923. if (need_check) {
  2924. i = min(i, i_max);
  2925. }
  2926. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  2927. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  2928. }
  2929. }
  2930. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  2931. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2932. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2933. (void)x_qh;
  2934. const float * x_dmf = (const float *) x_dm;
  2935. const float * y_df = (const float *) y_ds;
  2936. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  2937. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  2938. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  2939. 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]);
  2940. }
  2941. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  2942. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  2943. static __device__ __forceinline__ void mul_mat_q(
  2944. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  2945. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  2946. const block_q_t * x = (const block_q_t *) vx;
  2947. const block_q8_1 * y = (const block_q8_1 *) vy;
  2948. const int blocks_per_row_x = ncols_x / qk;
  2949. const int blocks_per_col_y = nrows_y / QK8_1;
  2950. const int blocks_per_warp = WARP_SIZE / qi;
  2951. const int & ncols_dst = ncols_y;
  2952. const int row_dst_0 = blockIdx.x*mmq_y;
  2953. const int & row_x_0 = row_dst_0;
  2954. const int col_dst_0 = blockIdx.y*mmq_x;
  2955. const int & col_y_0 = col_dst_0;
  2956. int * tile_x_ql = nullptr;
  2957. half2 * tile_x_dm = nullptr;
  2958. int * tile_x_qh = nullptr;
  2959. int * tile_x_sc = nullptr;
  2960. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  2961. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  2962. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  2963. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  2964. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  2965. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  2966. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  2967. #pragma unroll
  2968. for (int ir = 0; ir < qr; ++ir) {
  2969. const int kqs = ir*WARP_SIZE + threadIdx.x;
  2970. const int kbxd = kqs / QI8_1;
  2971. #pragma unroll
  2972. for (int i = 0; i < mmq_x; i += nwarps) {
  2973. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  2974. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  2975. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  2976. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  2977. }
  2978. #pragma unroll
  2979. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  2980. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  2981. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  2982. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  2983. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  2984. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  2985. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  2986. if (need_sum) {
  2987. *dsi_dst = *dsi_src;
  2988. } else {
  2989. float * dfi_dst = (float *) dsi_dst;
  2990. *dfi_dst = __low2half(*dsi_src);
  2991. }
  2992. }
  2993. __syncthreads();
  2994. // #pragma unroll // unrolling this loop causes too much register pressure
  2995. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  2996. #pragma unroll
  2997. for (int j = 0; j < mmq_x; j += nwarps) {
  2998. #pragma unroll
  2999. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3000. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3001. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3002. threadIdx.x + i, threadIdx.y + j, k);
  3003. }
  3004. }
  3005. }
  3006. __syncthreads();
  3007. }
  3008. }
  3009. #pragma unroll
  3010. for (int j = 0; j < mmq_x; j += nwarps) {
  3011. const int col_dst = col_dst_0 + j + threadIdx.y;
  3012. if (col_dst >= ncols_dst) {
  3013. return;
  3014. }
  3015. #pragma unroll
  3016. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3017. const int row_dst = row_dst_0 + threadIdx.x + i;
  3018. if (row_dst >= nrows_dst) {
  3019. continue;
  3020. }
  3021. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3022. }
  3023. }
  3024. }
  3025. #define MMQ_X_Q4_0_RDNA2 64
  3026. #define MMQ_Y_Q4_0_RDNA2 128
  3027. #define NWARPS_Q4_0_RDNA2 8
  3028. #define MMQ_X_Q4_0_RDNA1 64
  3029. #define MMQ_Y_Q4_0_RDNA1 64
  3030. #define NWARPS_Q4_0_RDNA1 8
  3031. #if defined(CUDA_USE_TENSOR_CORES)
  3032. #define MMQ_X_Q4_0_AMPERE 4
  3033. #define MMQ_Y_Q4_0_AMPERE 32
  3034. #define NWARPS_Q4_0_AMPERE 4
  3035. #else
  3036. #define MMQ_X_Q4_0_AMPERE 64
  3037. #define MMQ_Y_Q4_0_AMPERE 128
  3038. #define NWARPS_Q4_0_AMPERE 4
  3039. #endif
  3040. #define MMQ_X_Q4_0_PASCAL 64
  3041. #define MMQ_Y_Q4_0_PASCAL 64
  3042. #define NWARPS_Q4_0_PASCAL 8
  3043. template <bool need_check> static __global__ void
  3044. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3045. #if defined(RDNA3) || defined(RDNA2)
  3046. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3047. #endif // defined(RDNA3) || defined(RDNA2)
  3048. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3049. mul_mat_q4_0(
  3050. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3051. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3052. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3053. #if defined(RDNA3) || defined(RDNA2)
  3054. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3055. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3056. const int nwarps = NWARPS_Q4_0_RDNA2;
  3057. #else
  3058. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3059. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3060. const int nwarps = NWARPS_Q4_0_RDNA1;
  3061. #endif // defined(RDNA3) || defined(RDNA2)
  3062. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3063. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3064. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3065. #elif __CUDA_ARCH__ >= CC_VOLTA
  3066. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3067. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3068. const int nwarps = NWARPS_Q4_0_AMPERE;
  3069. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3070. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3071. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3072. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3073. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3074. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3075. const int nwarps = NWARPS_Q4_0_PASCAL;
  3076. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3077. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3078. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3079. #else
  3080. (void) vec_dot_q4_0_q8_1_mul_mat;
  3081. assert(false);
  3082. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3083. }
  3084. #define MMQ_X_Q4_1_RDNA2 64
  3085. #define MMQ_Y_Q4_1_RDNA2 128
  3086. #define NWARPS_Q4_1_RDNA2 8
  3087. #define MMQ_X_Q4_1_RDNA1 64
  3088. #define MMQ_Y_Q4_1_RDNA1 64
  3089. #define NWARPS_Q4_1_RDNA1 8
  3090. #if defined(CUDA_USE_TENSOR_CORES)
  3091. #define MMQ_X_Q4_1_AMPERE 4
  3092. #define MMQ_Y_Q4_1_AMPERE 32
  3093. #define NWARPS_Q4_1_AMPERE 4
  3094. #else
  3095. #define MMQ_X_Q4_1_AMPERE 64
  3096. #define MMQ_Y_Q4_1_AMPERE 128
  3097. #define NWARPS_Q4_1_AMPERE 4
  3098. #endif
  3099. #define MMQ_X_Q4_1_PASCAL 64
  3100. #define MMQ_Y_Q4_1_PASCAL 64
  3101. #define NWARPS_Q4_1_PASCAL 8
  3102. template <bool need_check> static __global__ void
  3103. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3104. #if defined(RDNA3) || defined(RDNA2)
  3105. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3106. #endif // defined(RDNA3) || defined(RDNA2)
  3107. #elif __CUDA_ARCH__ < CC_VOLTA
  3108. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3109. #endif // __CUDA_ARCH__ < CC_VOLTA
  3110. mul_mat_q4_1(
  3111. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3112. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3113. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3114. #if defined(RDNA3) || defined(RDNA2)
  3115. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3116. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3117. const int nwarps = NWARPS_Q4_1_RDNA2;
  3118. #else
  3119. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3120. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3121. const int nwarps = NWARPS_Q4_1_RDNA1;
  3122. #endif // defined(RDNA3) || defined(RDNA2)
  3123. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3124. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3125. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3126. #elif __CUDA_ARCH__ >= CC_VOLTA
  3127. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3128. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3129. const int nwarps = NWARPS_Q4_1_AMPERE;
  3130. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3131. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3132. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3133. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3134. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  3135. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  3136. const int nwarps = NWARPS_Q4_1_PASCAL;
  3137. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3138. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3139. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3140. #else
  3141. (void) vec_dot_q4_1_q8_1_mul_mat;
  3142. assert(false);
  3143. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3144. }
  3145. #define MMQ_X_Q5_0_RDNA2 64
  3146. #define MMQ_Y_Q5_0_RDNA2 128
  3147. #define NWARPS_Q5_0_RDNA2 8
  3148. #define MMQ_X_Q5_0_RDNA1 64
  3149. #define MMQ_Y_Q5_0_RDNA1 64
  3150. #define NWARPS_Q5_0_RDNA1 8
  3151. #if defined(CUDA_USE_TENSOR_CORES)
  3152. #define MMQ_X_Q5_0_AMPERE 4
  3153. #define MMQ_Y_Q5_0_AMPERE 32
  3154. #define NWARPS_Q5_0_AMPERE 4
  3155. #else
  3156. #define MMQ_X_Q5_0_AMPERE 128
  3157. #define MMQ_Y_Q5_0_AMPERE 64
  3158. #define NWARPS_Q5_0_AMPERE 4
  3159. #endif
  3160. #define MMQ_X_Q5_0_PASCAL 64
  3161. #define MMQ_Y_Q5_0_PASCAL 64
  3162. #define NWARPS_Q5_0_PASCAL 8
  3163. template <bool need_check> static __global__ void
  3164. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3165. #if defined(RDNA3) || defined(RDNA2)
  3166. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  3167. #endif // defined(RDNA3) || defined(RDNA2)
  3168. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3169. mul_mat_q5_0(
  3170. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3171. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3172. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3173. #if defined(RDNA3) || defined(RDNA2)
  3174. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  3175. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  3176. const int nwarps = NWARPS_Q5_0_RDNA2;
  3177. #else
  3178. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  3179. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  3180. const int nwarps = NWARPS_Q5_0_RDNA1;
  3181. #endif // defined(RDNA3) || defined(RDNA2)
  3182. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3183. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3184. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3185. #elif __CUDA_ARCH__ >= CC_VOLTA
  3186. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  3187. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  3188. const int nwarps = NWARPS_Q5_0_AMPERE;
  3189. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3190. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3191. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3192. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3193. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  3194. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  3195. const int nwarps = NWARPS_Q5_0_PASCAL;
  3196. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  3197. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  3198. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3199. #else
  3200. (void) vec_dot_q5_0_q8_1_mul_mat;
  3201. assert(false);
  3202. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3203. }
  3204. #define MMQ_X_Q5_1_RDNA2 64
  3205. #define MMQ_Y_Q5_1_RDNA2 128
  3206. #define NWARPS_Q5_1_RDNA2 8
  3207. #define MMQ_X_Q5_1_RDNA1 64
  3208. #define MMQ_Y_Q5_1_RDNA1 64
  3209. #define NWARPS_Q5_1_RDNA1 8
  3210. #if defined(CUDA_USE_TENSOR_CORES)
  3211. #define MMQ_X_Q5_1_AMPERE 4
  3212. #define MMQ_Y_Q5_1_AMPERE 32
  3213. #define NWARPS_Q5_1_AMPERE 4
  3214. #else
  3215. #define MMQ_X_Q5_1_AMPERE 128
  3216. #define MMQ_Y_Q5_1_AMPERE 64
  3217. #define NWARPS_Q5_1_AMPERE 4
  3218. #endif
  3219. #define MMQ_X_Q5_1_PASCAL 64
  3220. #define MMQ_Y_Q5_1_PASCAL 64
  3221. #define NWARPS_Q5_1_PASCAL 8
  3222. template <bool need_check> static __global__ void
  3223. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3224. #if defined(RDNA3) || defined(RDNA2)
  3225. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  3226. #endif // defined(RDNA3) || defined(RDNA2)
  3227. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3228. mul_mat_q5_1(
  3229. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3230. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3231. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3232. #if defined(RDNA3) || defined(RDNA2)
  3233. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  3234. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  3235. const int nwarps = NWARPS_Q5_1_RDNA2;
  3236. #else
  3237. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  3238. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  3239. const int nwarps = NWARPS_Q5_1_RDNA1;
  3240. #endif // defined(RDNA3) || defined(RDNA2)
  3241. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3242. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3243. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3244. #elif __CUDA_ARCH__ >= CC_VOLTA
  3245. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  3246. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  3247. const int nwarps = NWARPS_Q5_1_AMPERE;
  3248. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3249. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3250. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3251. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3252. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  3253. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  3254. const int nwarps = NWARPS_Q5_1_PASCAL;
  3255. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  3256. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  3257. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3258. #else
  3259. (void) vec_dot_q5_1_q8_1_mul_mat;
  3260. assert(false);
  3261. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3262. }
  3263. #define MMQ_X_Q8_0_RDNA2 64
  3264. #define MMQ_Y_Q8_0_RDNA2 128
  3265. #define NWARPS_Q8_0_RDNA2 8
  3266. #define MMQ_X_Q8_0_RDNA1 64
  3267. #define MMQ_Y_Q8_0_RDNA1 64
  3268. #define NWARPS_Q8_0_RDNA1 8
  3269. #if defined(CUDA_USE_TENSOR_CORES)
  3270. #define MMQ_X_Q8_0_AMPERE 4
  3271. #define MMQ_Y_Q8_0_AMPERE 32
  3272. #define NWARPS_Q8_0_AMPERE 4
  3273. #else
  3274. #define MMQ_X_Q8_0_AMPERE 128
  3275. #define MMQ_Y_Q8_0_AMPERE 64
  3276. #define NWARPS_Q8_0_AMPERE 4
  3277. #endif
  3278. #define MMQ_X_Q8_0_PASCAL 64
  3279. #define MMQ_Y_Q8_0_PASCAL 64
  3280. #define NWARPS_Q8_0_PASCAL 8
  3281. template <bool need_check> static __global__ void
  3282. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3283. #if defined(RDNA3) || defined(RDNA2)
  3284. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  3285. #endif // defined(RDNA3) || defined(RDNA2)
  3286. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3287. mul_mat_q8_0(
  3288. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3289. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3290. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3291. #if defined(RDNA3) || defined(RDNA2)
  3292. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  3293. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  3294. const int nwarps = NWARPS_Q8_0_RDNA2;
  3295. #else
  3296. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  3297. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  3298. const int nwarps = NWARPS_Q8_0_RDNA1;
  3299. #endif // defined(RDNA3) || defined(RDNA2)
  3300. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3301. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3302. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3303. #elif __CUDA_ARCH__ >= CC_VOLTA
  3304. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  3305. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  3306. const int nwarps = NWARPS_Q8_0_AMPERE;
  3307. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3308. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3309. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3310. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3311. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  3312. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  3313. const int nwarps = NWARPS_Q8_0_PASCAL;
  3314. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  3315. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  3316. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3317. #else
  3318. (void) vec_dot_q8_0_q8_1_mul_mat;
  3319. assert(false);
  3320. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3321. }
  3322. #define MMQ_X_Q2_K_RDNA2 64
  3323. #define MMQ_Y_Q2_K_RDNA2 128
  3324. #define NWARPS_Q2_K_RDNA2 8
  3325. #define MMQ_X_Q2_K_RDNA1 128
  3326. #define MMQ_Y_Q2_K_RDNA1 32
  3327. #define NWARPS_Q2_K_RDNA1 8
  3328. #if defined(CUDA_USE_TENSOR_CORES)
  3329. #define MMQ_X_Q2_K_AMPERE 4
  3330. #define MMQ_Y_Q2_K_AMPERE 32
  3331. #define NWARPS_Q2_K_AMPERE 4
  3332. #else
  3333. #define MMQ_X_Q2_K_AMPERE 64
  3334. #define MMQ_Y_Q2_K_AMPERE 128
  3335. #define NWARPS_Q2_K_AMPERE 4
  3336. #endif
  3337. #define MMQ_X_Q2_K_PASCAL 64
  3338. #define MMQ_Y_Q2_K_PASCAL 64
  3339. #define NWARPS_Q2_K_PASCAL 8
  3340. template <bool need_check> static __global__ void
  3341. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3342. #if defined(RDNA3) || defined(RDNA2)
  3343. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  3344. #endif // defined(RDNA3) || defined(RDNA2)
  3345. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3346. mul_mat_q2_K(
  3347. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3348. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3349. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3350. #if defined(RDNA3) || defined(RDNA2)
  3351. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  3352. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  3353. const int nwarps = NWARPS_Q2_K_RDNA2;
  3354. #else
  3355. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  3356. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  3357. const int nwarps = NWARPS_Q2_K_RDNA1;
  3358. #endif // defined(RDNA3) || defined(RDNA2)
  3359. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3360. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3361. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3362. #elif __CUDA_ARCH__ >= CC_VOLTA
  3363. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  3364. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  3365. const int nwarps = NWARPS_Q2_K_AMPERE;
  3366. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3367. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3368. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3369. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3370. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  3371. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  3372. const int nwarps = NWARPS_Q2_K_PASCAL;
  3373. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  3374. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  3375. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3376. #else
  3377. (void) vec_dot_q2_K_q8_1_mul_mat;
  3378. assert(false);
  3379. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3380. }
  3381. #define MMQ_X_Q3_K_RDNA2 128
  3382. #define MMQ_Y_Q3_K_RDNA2 64
  3383. #define NWARPS_Q3_K_RDNA2 8
  3384. #define MMQ_X_Q3_K_RDNA1 32
  3385. #define MMQ_Y_Q3_K_RDNA1 128
  3386. #define NWARPS_Q3_K_RDNA1 8
  3387. #if defined(CUDA_USE_TENSOR_CORES)
  3388. #define MMQ_X_Q3_K_AMPERE 4
  3389. #define MMQ_Y_Q3_K_AMPERE 32
  3390. #define NWARPS_Q3_K_AMPERE 4
  3391. #else
  3392. #define MMQ_X_Q3_K_AMPERE 128
  3393. #define MMQ_Y_Q3_K_AMPERE 128
  3394. #define NWARPS_Q3_K_AMPERE 4
  3395. #endif
  3396. #define MMQ_X_Q3_K_PASCAL 64
  3397. #define MMQ_Y_Q3_K_PASCAL 64
  3398. #define NWARPS_Q3_K_PASCAL 8
  3399. template <bool need_check> static __global__ void
  3400. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3401. #if defined(RDNA3) || defined(RDNA2)
  3402. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  3403. #endif // defined(RDNA3) || defined(RDNA2)
  3404. #elif __CUDA_ARCH__ < CC_VOLTA
  3405. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  3406. #endif // __CUDA_ARCH__ < CC_VOLTA
  3407. mul_mat_q3_K(
  3408. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3409. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3410. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3411. #if defined(RDNA3) || defined(RDNA2)
  3412. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  3413. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  3414. const int nwarps = NWARPS_Q3_K_RDNA2;
  3415. #else
  3416. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  3417. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  3418. const int nwarps = NWARPS_Q3_K_RDNA1;
  3419. #endif // defined(RDNA3) || defined(RDNA2)
  3420. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3421. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3422. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3423. #elif __CUDA_ARCH__ >= CC_VOLTA
  3424. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  3425. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  3426. const int nwarps = NWARPS_Q3_K_AMPERE;
  3427. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3428. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3429. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3430. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3431. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  3432. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  3433. const int nwarps = NWARPS_Q3_K_PASCAL;
  3434. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  3435. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  3436. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3437. #else
  3438. (void) vec_dot_q3_K_q8_1_mul_mat;
  3439. assert(false);
  3440. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3441. }
  3442. #define MMQ_X_Q4_K_RDNA2 64
  3443. #define MMQ_Y_Q4_K_RDNA2 128
  3444. #define NWARPS_Q4_K_RDNA2 8
  3445. #define MMQ_X_Q4_K_RDNA1 32
  3446. #define MMQ_Y_Q4_K_RDNA1 64
  3447. #define NWARPS_Q4_K_RDNA1 8
  3448. #if defined(CUDA_USE_TENSOR_CORES)
  3449. #define MMQ_X_Q4_K_AMPERE 4
  3450. #define MMQ_Y_Q4_K_AMPERE 32
  3451. #define NWARPS_Q4_K_AMPERE 4
  3452. #else
  3453. #define MMQ_X_Q4_K_AMPERE 64
  3454. #define MMQ_Y_Q4_K_AMPERE 128
  3455. #define NWARPS_Q4_K_AMPERE 4
  3456. #endif
  3457. #define MMQ_X_Q4_K_PASCAL 64
  3458. #define MMQ_Y_Q4_K_PASCAL 64
  3459. #define NWARPS_Q4_K_PASCAL 8
  3460. template <bool need_check> static __global__ void
  3461. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3462. #if defined(RDNA3) || defined(RDNA2)
  3463. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  3464. #endif // defined(RDNA3) || defined(RDNA2)
  3465. #elif __CUDA_ARCH__ < CC_VOLTA
  3466. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  3467. #endif // __CUDA_ARCH__ < CC_VOLTA
  3468. mul_mat_q4_K(
  3469. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3470. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3471. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3472. #if defined(RDNA3) || defined(RDNA2)
  3473. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  3474. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  3475. const int nwarps = NWARPS_Q4_K_RDNA2;
  3476. #else
  3477. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  3478. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  3479. const int nwarps = NWARPS_Q4_K_RDNA1;
  3480. #endif // defined(RDNA3) || defined(RDNA2)
  3481. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3482. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3483. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3484. #elif __CUDA_ARCH__ >= CC_VOLTA
  3485. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  3486. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  3487. const int nwarps = NWARPS_Q4_K_AMPERE;
  3488. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3489. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3490. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3491. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3492. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  3493. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  3494. const int nwarps = NWARPS_Q4_K_PASCAL;
  3495. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  3496. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  3497. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3498. #else
  3499. (void) vec_dot_q4_K_q8_1_mul_mat;
  3500. assert(false);
  3501. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3502. }
  3503. #define MMQ_X_Q5_K_RDNA2 64
  3504. #define MMQ_Y_Q5_K_RDNA2 128
  3505. #define NWARPS_Q5_K_RDNA2 8
  3506. #define MMQ_X_Q5_K_RDNA1 32
  3507. #define MMQ_Y_Q5_K_RDNA1 64
  3508. #define NWARPS_Q5_K_RDNA1 8
  3509. #if defined(CUDA_USE_TENSOR_CORES)
  3510. #define MMQ_X_Q5_K_AMPERE 4
  3511. #define MMQ_Y_Q5_K_AMPERE 32
  3512. #define NWARPS_Q5_K_AMPERE 4
  3513. #else
  3514. #define MMQ_X_Q5_K_AMPERE 64
  3515. #define MMQ_Y_Q5_K_AMPERE 128
  3516. #define NWARPS_Q5_K_AMPERE 4
  3517. #endif
  3518. #define MMQ_X_Q5_K_PASCAL 64
  3519. #define MMQ_Y_Q5_K_PASCAL 64
  3520. #define NWARPS_Q5_K_PASCAL 8
  3521. template <bool need_check> static __global__ void
  3522. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3523. #if defined(RDNA3) || defined(RDNA2)
  3524. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  3525. #endif // defined(RDNA3) || defined(RDNA2)
  3526. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3527. mul_mat_q5_K(
  3528. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3529. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3530. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3531. #if defined(RDNA3) || defined(RDNA2)
  3532. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  3533. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  3534. const int nwarps = NWARPS_Q5_K_RDNA2;
  3535. #else
  3536. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  3537. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  3538. const int nwarps = NWARPS_Q5_K_RDNA1;
  3539. #endif // defined(RDNA3) || defined(RDNA2)
  3540. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3541. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3542. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3543. #elif __CUDA_ARCH__ >= CC_VOLTA
  3544. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  3545. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  3546. const int nwarps = NWARPS_Q5_K_AMPERE;
  3547. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3548. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3549. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3550. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3551. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  3552. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  3553. const int nwarps = NWARPS_Q5_K_PASCAL;
  3554. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  3555. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  3556. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3557. #else
  3558. (void) vec_dot_q5_K_q8_1_mul_mat;
  3559. assert(false);
  3560. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3561. }
  3562. #define MMQ_X_Q6_K_RDNA2 64
  3563. #define MMQ_Y_Q6_K_RDNA2 128
  3564. #define NWARPS_Q6_K_RDNA2 8
  3565. #define MMQ_X_Q6_K_RDNA1 32
  3566. #define MMQ_Y_Q6_K_RDNA1 64
  3567. #define NWARPS_Q6_K_RDNA1 8
  3568. #if defined(CUDA_USE_TENSOR_CORES)
  3569. #define MMQ_X_Q6_K_AMPERE 4
  3570. #define MMQ_Y_Q6_K_AMPERE 32
  3571. #define NWARPS_Q6_K_AMPERE 4
  3572. #else
  3573. #define MMQ_X_Q6_K_AMPERE 64
  3574. #define MMQ_Y_Q6_K_AMPERE 64
  3575. #define NWARPS_Q6_K_AMPERE 4
  3576. #endif
  3577. #define MMQ_X_Q6_K_PASCAL 64
  3578. #define MMQ_Y_Q6_K_PASCAL 64
  3579. #define NWARPS_Q6_K_PASCAL 8
  3580. template <bool need_check> static __global__ void
  3581. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3582. #if defined(RDNA3) || defined(RDNA2)
  3583. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  3584. #endif // defined(RDNA3) || defined(RDNA2)
  3585. #elif __CUDA_ARCH__ < CC_VOLTA
  3586. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  3587. #endif // __CUDA_ARCH__ < CC_VOLTA
  3588. mul_mat_q6_K(
  3589. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3590. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3591. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3592. #if defined(RDNA3) || defined(RDNA2)
  3593. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  3594. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  3595. const int nwarps = NWARPS_Q6_K_RDNA2;
  3596. #else
  3597. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  3598. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  3599. const int nwarps = NWARPS_Q6_K_RDNA1;
  3600. #endif // defined(RDNA3) || defined(RDNA2)
  3601. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3602. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3603. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3604. #elif __CUDA_ARCH__ >= CC_VOLTA
  3605. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  3606. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  3607. const int nwarps = NWARPS_Q6_K_AMPERE;
  3608. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3609. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3610. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3611. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3612. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  3613. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  3614. const int nwarps = NWARPS_Q6_K_PASCAL;
  3615. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  3616. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  3617. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3618. #else
  3619. (void) vec_dot_q6_K_q8_1_mul_mat;
  3620. assert(false);
  3621. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3622. }
  3623. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  3624. static __global__ void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows) {
  3625. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3626. if (row >= nrows) {
  3627. return;
  3628. }
  3629. const int blocks_per_row = ncols / qk;
  3630. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  3631. // partial sum for each thread
  3632. float tmp = 0.0f;
  3633. const block_q_t * x = (const block_q_t *) vx;
  3634. const block_q8_1 * y = (const block_q8_1 *) vy;
  3635. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  3636. const int ibx = row*blocks_per_row + i + threadIdx.x / (qi/vdr); // x block index
  3637. const int iby = (i + threadIdx.x / (qi/vdr)) * (qk/QK8_1); // y block index that aligns with ibx
  3638. const int iqs = vdr * (threadIdx.x % (qi/vdr)); // x block quant index when casting the quants to int
  3639. tmp += vec_dot_q_cuda(&x[ibx], &y[iby], iqs);
  3640. }
  3641. // sum up partial sums and write back result
  3642. #pragma unroll
  3643. for (int mask = 16; mask > 0; mask >>= 1) {
  3644. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3645. }
  3646. if (threadIdx.x == 0) {
  3647. dst[row] = tmp;
  3648. }
  3649. }
  3650. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  3651. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  3652. // qk = quantized weights per x block
  3653. // qr = number of quantized weights per data value in x block
  3654. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  3655. if (row >= nrows) {
  3656. return;
  3657. }
  3658. const int tid = threadIdx.x;
  3659. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  3660. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  3661. const int y_offset = qr == 1 ? 1 : qk/2;
  3662. // partial sum for each thread
  3663. #ifdef GGML_CUDA_F16
  3664. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  3665. #else
  3666. float tmp = 0.0f;
  3667. #endif // GGML_CUDA_F16
  3668. for (int i = 0; i < ncols; i += iter_stride) {
  3669. const int col = i + vals_per_iter*tid;
  3670. const int ib = (row*ncols + col)/qk; // x block index
  3671. const int iqs = (col%qk)/qr; // x quant index
  3672. const int iybs = col - col%qk; // y block start index
  3673. // processing >2 values per i iter is faster for fast GPUs
  3674. #pragma unroll
  3675. for (int j = 0; j < vals_per_iter; j += 2) {
  3676. // process 2 vals per j iter
  3677. // dequantize
  3678. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  3679. dfloat2 v;
  3680. dequantize_kernel(vx, ib, iqs + j/qr, v);
  3681. // matrix multiplication
  3682. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  3683. #ifdef GGML_CUDA_F16
  3684. tmp += __hmul2(v, {
  3685. y[iybs + iqs + j/qr + 0],
  3686. y[iybs + iqs + j/qr + y_offset]
  3687. });
  3688. #else
  3689. tmp += v.x * y[iybs + iqs + j/qr + 0];
  3690. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  3691. #endif // GGML_CUDA_F16
  3692. }
  3693. }
  3694. // sum up partial sums and write back result
  3695. #pragma unroll
  3696. for (int mask = 16; mask > 0; mask >>= 1) {
  3697. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3698. }
  3699. if (tid == 0) {
  3700. #ifdef GGML_CUDA_F16
  3701. dst[row] = tmp.x + tmp.y;
  3702. #else
  3703. dst[row] = tmp;
  3704. #endif // GGML_CUDA_F16
  3705. }
  3706. }
  3707. static __global__ void mul_mat_p021_f16_f32(
  3708. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  3709. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  3710. const half * x = (const half *) vx;
  3711. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3712. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3713. const int channel_x = channel / (nchannels_y / nchannels_x);
  3714. const int nrows_y = ncols_x;
  3715. const int nrows_dst = nrows_x;
  3716. const int row_dst = row_x;
  3717. float tmp = 0.0f;
  3718. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3719. const int col_x = col_x0 + threadIdx.x;
  3720. if (col_x >= ncols_x) {
  3721. break;
  3722. }
  3723. // x is transposed and permuted
  3724. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  3725. const float xi = __half2float(x[ix]);
  3726. const int row_y = col_x;
  3727. // y is not transposed but permuted
  3728. const int iy = channel*nrows_y + row_y;
  3729. tmp += xi * y[iy];
  3730. }
  3731. // dst is not transposed and not permuted
  3732. const int idst = channel*nrows_dst + row_dst;
  3733. // sum up partial sums and write back result
  3734. #pragma unroll
  3735. for (int mask = 16; mask > 0; mask >>= 1) {
  3736. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3737. }
  3738. if (threadIdx.x == 0) {
  3739. dst[idst] = tmp;
  3740. }
  3741. }
  3742. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  3743. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  3744. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  3745. const half * x = (const half *) vx;
  3746. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  3747. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  3748. const int channel_x = channel / channel_x_divisor;
  3749. const int nrows_y = ncols_x;
  3750. const int nrows_dst = nrows_x;
  3751. const int row_dst = row_x;
  3752. const int idst = channel*nrows_dst + row_dst;
  3753. float tmp = 0.0f;
  3754. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  3755. const int col_x = col_x0 + threadIdx.x;
  3756. if (col_x >= ncols_x) {
  3757. break;
  3758. }
  3759. const int row_y = col_x;
  3760. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  3761. const int iy = channel*nrows_y + row_y;
  3762. const float xi = __half2float(x[ix]);
  3763. tmp += xi * y[iy];
  3764. }
  3765. // sum up partial sums and write back result
  3766. #pragma unroll
  3767. for (int mask = 16; mask > 0; mask >>= 1) {
  3768. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  3769. }
  3770. if (threadIdx.x == 0) {
  3771. dst[idst] = tmp;
  3772. }
  3773. }
  3774. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  3775. const float * xi = (const float *) cxi;
  3776. float * dsti = (float *) cdsti;
  3777. *dsti = *xi;
  3778. }
  3779. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  3780. const float * xi = (const float *) cxi;
  3781. half * dsti = (half *) cdsti;
  3782. *dsti = __float2half(*xi);
  3783. }
  3784. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  3785. const half * xi = (const half *) cxi;
  3786. half * dsti = (half *) cdsti;
  3787. *dsti = *xi;
  3788. }
  3789. template <cpy_kernel_t cpy_1>
  3790. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  3791. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3792. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3793. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  3794. if (i >= ne) {
  3795. return;
  3796. }
  3797. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  3798. // then combine those indices with the corresponding byte offsets to get the total offsets
  3799. const int i02 = i / (ne00*ne01);
  3800. const int i01 = (i - i02*ne01*ne00) / ne00;
  3801. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  3802. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3803. const int i12 = i / (ne10*ne11);
  3804. const int i11 = (i - i12*ne10*ne11) / ne10;
  3805. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  3806. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3807. cpy_1(cx + x_offset, cdst + dst_offset);
  3808. }
  3809. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  3810. const float * xi = (const float *) cxi;
  3811. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  3812. float amax = 0.0f; // absolute max
  3813. for (int j = 0; j < QK8_0; j++) {
  3814. const float v = xi[j];
  3815. amax = fmaxf(amax, fabsf(v));
  3816. }
  3817. const float d = amax / ((1 << 7) - 1);
  3818. const float id = d ? 1.0f/d : 0.0f;
  3819. dsti->d = d;
  3820. for (int j = 0; j < QK8_0; ++j) {
  3821. const float x0 = xi[j]*id;
  3822. dsti->qs[j] = roundf(x0);
  3823. }
  3824. }
  3825. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  3826. const float * xi = (const float *) cxi;
  3827. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  3828. float amax = 0.0f;
  3829. float vmax = 0.0f;
  3830. for (int j = 0; j < QK4_0; ++j) {
  3831. const float v = xi[j];
  3832. if (amax < fabsf(v)) {
  3833. amax = fabsf(v);
  3834. vmax = v;
  3835. }
  3836. }
  3837. const float d = vmax / -8;
  3838. const float id = d ? 1.0f/d : 0.0f;
  3839. dsti->d = d;
  3840. for (int j = 0; j < QK4_0/2; ++j) {
  3841. const float x0 = xi[0 + j]*id;
  3842. const float x1 = xi[QK4_0/2 + j]*id;
  3843. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  3844. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  3845. dsti->qs[j] = xi0;
  3846. dsti->qs[j] |= xi1 << 4;
  3847. }
  3848. }
  3849. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  3850. const float * xi = (const float *) cxi;
  3851. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  3852. float vmin = FLT_MAX;
  3853. float vmax = -FLT_MAX;
  3854. for (int j = 0; j < QK4_1; ++j) {
  3855. const float v = xi[j];
  3856. if (v < vmin) vmin = v;
  3857. if (v > vmax) vmax = v;
  3858. }
  3859. const float d = (vmax - vmin) / ((1 << 4) - 1);
  3860. const float id = d ? 1.0f/d : 0.0f;
  3861. dsti->dm.x = d;
  3862. dsti->dm.y = vmin;
  3863. for (int j = 0; j < QK4_1/2; ++j) {
  3864. const float x0 = (xi[0 + j] - vmin)*id;
  3865. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  3866. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  3867. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  3868. dsti->qs[j] = xi0;
  3869. dsti->qs[j] |= xi1 << 4;
  3870. }
  3871. }
  3872. template <cpy_kernel_t cpy_blck, int qk>
  3873. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  3874. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  3875. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12) {
  3876. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  3877. if (i >= ne) {
  3878. return;
  3879. }
  3880. const int i02 = i / (ne00*ne01);
  3881. const int i01 = (i - i02*ne01*ne00) / ne00;
  3882. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  3883. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  3884. const int i12 = i / (ne10*ne11);
  3885. const int i11 = (i - i12*ne10*ne11) / ne10;
  3886. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  3887. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  3888. cpy_blck(cx + x_offset, cdst + dst_offset);
  3889. }
  3890. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  3891. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  3892. return 1.0f - min(1.0f, max(0.0f, y));
  3893. }
  3894. struct rope_corr_dims {
  3895. float v[4];
  3896. };
  3897. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  3898. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  3899. static __device__ void rope_yarn(
  3900. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  3901. float * cos_theta, float * sin_theta
  3902. ) {
  3903. // Get n-d rotational scaling corrected for extrapolation
  3904. float theta_interp = freq_scale * theta_extrap;
  3905. float theta = theta_interp;
  3906. if (ext_factor != 0.0f) {
  3907. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  3908. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  3909. // Get n-d magnitude scaling corrected for interpolation
  3910. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  3911. }
  3912. *cos_theta = cosf(theta) * mscale;
  3913. *sin_theta = sinf(theta) * mscale;
  3914. }
  3915. // rope == RoPE == rotary positional embedding
  3916. template<typename T, bool has_pos>
  3917. static __global__ void rope(
  3918. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3919. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  3920. ) {
  3921. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3922. if (col >= ncols) {
  3923. return;
  3924. }
  3925. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3926. const int i = row*ncols + col;
  3927. const int i2 = row/p_delta_rows;
  3928. const int p = has_pos ? pos[i2] : 0;
  3929. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  3930. float cos_theta, sin_theta;
  3931. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3932. const float x0 = x[i + 0];
  3933. const float x1 = x[i + 1];
  3934. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3935. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  3936. }
  3937. template<typename T, bool has_pos>
  3938. static __global__ void rope_neox(
  3939. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  3940. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  3941. ) {
  3942. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  3943. if (col >= ncols) {
  3944. return;
  3945. }
  3946. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  3947. const int ib = col / n_dims;
  3948. const int ic = col % n_dims;
  3949. const int i = row*ncols + ib*n_dims + ic/2;
  3950. const int i2 = row/p_delta_rows;
  3951. float cur_rot = inv_ndims * ic - ib;
  3952. const int p = has_pos ? pos[i2] : 0;
  3953. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  3954. float cos_theta, sin_theta;
  3955. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  3956. const float x0 = x[i + 0];
  3957. const float x1 = x[i + n_dims/2];
  3958. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3959. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  3960. }
  3961. static __global__ void rope_glm_f32(
  3962. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  3963. int n_ctx
  3964. ) {
  3965. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3966. const int half_n_dims = ncols/4;
  3967. if (col >= half_n_dims) {
  3968. return;
  3969. }
  3970. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3971. const int i = row*ncols + col;
  3972. const int i2 = row/p_delta_rows;
  3973. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  3974. // FIXME: this is likely wrong
  3975. const int p = pos != nullptr ? pos[i2] : 0;
  3976. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  3977. const float sin_theta = sinf(theta);
  3978. const float cos_theta = cosf(theta);
  3979. const float x0 = x[i + 0];
  3980. const float x1 = x[i + half_n_dims];
  3981. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  3982. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  3983. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  3984. const float sin_block_theta = sinf(block_theta);
  3985. const float cos_block_theta = cosf(block_theta);
  3986. const float x2 = x[i + half_n_dims * 2];
  3987. const float x3 = x[i + half_n_dims * 3];
  3988. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  3989. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  3990. }
  3991. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  3992. const int n_heads_log2_floor, const float m0, const float m1) {
  3993. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  3994. if (col >= ncols) {
  3995. return;
  3996. }
  3997. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  3998. const int i = row*ncols + col;
  3999. const int k = row/k_rows;
  4000. float m_k;
  4001. if (k < n_heads_log2_floor) {
  4002. m_k = powf(m0, k + 1);
  4003. } else {
  4004. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4005. }
  4006. dst[i] = col * m_k + x[i];
  4007. }
  4008. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4009. const int row = blockIdx.y;
  4010. const int col = threadIdx.x;
  4011. float sum = 0.0f;
  4012. for (int i = col; i < ncols; i += blockDim.x) {
  4013. sum += x[row * ncols + i];
  4014. }
  4015. sum = warp_reduce_sum(sum);
  4016. if (col == 0) {
  4017. dst[row] = sum;
  4018. }
  4019. }
  4020. template<typename T>
  4021. static inline __device__ void swap(T & a, T & b) {
  4022. T tmp = a;
  4023. a = b;
  4024. b = tmp;
  4025. }
  4026. template<ggml_sort_order order>
  4027. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4028. // bitonic sort
  4029. int col = threadIdx.x;
  4030. int row = blockIdx.y;
  4031. if (col >= ncols) return;
  4032. const float * x_row = x + row * ncols;
  4033. int * dst_row = dst + row * ncols;
  4034. // initialize indices
  4035. if (col < ncols) {
  4036. dst_row[col] = col;
  4037. }
  4038. __syncthreads();
  4039. for (int k = 2; k <= ncols; k *= 2) {
  4040. for (int j = k / 2; j > 0; j /= 2) {
  4041. int ixj = col ^ j;
  4042. if (ixj > col) {
  4043. if ((col & k) == 0) {
  4044. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  4045. swap(dst_row[col], dst_row[ixj]);
  4046. }
  4047. } else {
  4048. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  4049. swap(dst_row[col], dst_row[ixj]);
  4050. }
  4051. }
  4052. }
  4053. __syncthreads();
  4054. }
  4055. }
  4056. }
  4057. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4058. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4059. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4060. if (col >= ncols) {
  4061. return;
  4062. }
  4063. const int i = row*ncols + col;
  4064. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4065. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4066. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4067. }
  4068. static __global__ void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale) {
  4069. const int tid = threadIdx.x;
  4070. const int rowx = blockIdx.x;
  4071. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  4072. const int block_size = blockDim.x;
  4073. const int warp_id = threadIdx.x / WARP_SIZE;
  4074. const int lane_id = threadIdx.x % WARP_SIZE;
  4075. __shared__ float buf[CUDA_SOFT_MAX_BLOCK_SIZE/WARP_SIZE];
  4076. float max_val = -INFINITY;
  4077. for (int col = tid; col < ncols; col += block_size) {
  4078. const int ix = rowx*ncols + col;
  4079. const int iy = rowy*ncols + col;
  4080. max_val = max(max_val, x[ix]*scale + (y ? y[iy] : 0.0f));
  4081. }
  4082. // find the max value in the block
  4083. max_val = warp_reduce_max(max_val);
  4084. if (block_size > WARP_SIZE) {
  4085. if (warp_id == 0) {
  4086. buf[lane_id] = -INFINITY;
  4087. }
  4088. __syncthreads();
  4089. if (lane_id == 0) {
  4090. buf[warp_id] = max_val;
  4091. }
  4092. __syncthreads();
  4093. max_val = buf[lane_id];
  4094. max_val = warp_reduce_max(max_val);
  4095. }
  4096. float tmp = 0.f;
  4097. for (int col = tid; col < ncols; col += block_size) {
  4098. const int ix = rowx*ncols + col;
  4099. const int iy = rowy*ncols + col;
  4100. const float val = expf((x[ix]*scale + (y ? y[iy] : 0.0f)) - max_val);
  4101. tmp += val;
  4102. dst[ix] = val;
  4103. }
  4104. // find the sum of exps in the block
  4105. tmp = warp_reduce_sum(tmp);
  4106. if (block_size > WARP_SIZE) {
  4107. if (warp_id == 0) {
  4108. buf[lane_id] = 0.f;
  4109. }
  4110. __syncthreads();
  4111. if (lane_id == 0) {
  4112. buf[warp_id] = tmp;
  4113. }
  4114. __syncthreads();
  4115. tmp = buf[lane_id];
  4116. tmp = warp_reduce_sum(tmp);
  4117. }
  4118. const float inv_tmp = 1.f / tmp;
  4119. for (int col = tid; col < ncols; col += block_size) {
  4120. const int i = rowx*ncols + col;
  4121. dst[i] *= inv_tmp;
  4122. }
  4123. }
  4124. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  4125. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4126. if (i >= k) {
  4127. return;
  4128. }
  4129. dst[i] = scale * x[i];
  4130. }
  4131. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  4132. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4133. if (i >= k) {
  4134. return;
  4135. }
  4136. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  4137. }
  4138. static __global__ void im2col_f32_f16(
  4139. const float * x, half * dst,
  4140. int offset_delta, int IW, int IH, int OW, int KW, int KH, int pelements, int CHW,
  4141. int s0, int s1, int p0, int p1, int d0, int d1) {
  4142. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  4143. if (i >= pelements) {
  4144. return;
  4145. }
  4146. const int ksize = OW * (KH > 1 ? KW : 1);
  4147. const int kx = i / ksize;
  4148. const int kd = kx * ksize;
  4149. const int ky = (i - kd) / OW;
  4150. const int ix = i % OW;
  4151. const int iiw = ix * s0 + kx * d0 - p0;
  4152. const int iih = blockIdx.y * s1 + ky * d1 - p1;
  4153. const int offset_dst =
  4154. (blockIdx.y * OW + ix) * CHW +
  4155. (blockIdx.z * (KW * KH) + ky * KW + kx);
  4156. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  4157. dst[offset_dst] = __float2half(0.0f);
  4158. } else {
  4159. const int offset_src = blockIdx.z * offset_delta;
  4160. dst[offset_dst] = __float2half(x[offset_src + iih * IW + iiw]);
  4161. }
  4162. }
  4163. template<int qk, int qr, dequantize_kernel_t dq>
  4164. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4165. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4166. GGML_TENSOR_BINARY_OP_LOCALS
  4167. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4168. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  4169. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4170. // strides in elements
  4171. //const size_t s0 = nb0 / ggml_element_size(dst);
  4172. const size_t s1 = nb1 / ggml_element_size(dst);
  4173. const size_t s2 = nb2 / ggml_element_size(dst);
  4174. const size_t s3 = nb3 / ggml_element_size(dst);
  4175. const size_t s10 = nb10 / ggml_element_size(src1);
  4176. const size_t s11 = nb11 / ggml_element_size(src1);
  4177. const size_t s12 = nb12 / ggml_element_size(src1);
  4178. //const size_t s13 = nb13 / ggml_element_size(src1);
  4179. GGML_ASSERT(ne00 % 2 == 0);
  4180. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  4181. src0_dd, src1_dd, dst_dd,
  4182. ne00, /*ne01, ne02, ne03,*/
  4183. /*ne10, ne11,*/ ne12, /*ne13,*/
  4184. /* s0,*/ s1, s2, s3,
  4185. /* nb00,*/ nb01, nb02, nb03,
  4186. s10, s11, s12/*, s13*/);
  4187. (void) dst;
  4188. }
  4189. template<typename src0_t>
  4190. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  4191. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  4192. GGML_TENSOR_BINARY_OP_LOCALS
  4193. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  4194. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  4195. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  4196. // strides in elements
  4197. //const size_t s0 = nb0 / ggml_element_size(dst);
  4198. const size_t s1 = nb1 / ggml_element_size(dst);
  4199. const size_t s2 = nb2 / ggml_element_size(dst);
  4200. const size_t s3 = nb3 / ggml_element_size(dst);
  4201. const size_t s10 = nb10 / ggml_element_size(src1);
  4202. const size_t s11 = nb11 / ggml_element_size(src1);
  4203. const size_t s12 = nb12 / ggml_element_size(src1);
  4204. //const size_t s13 = nb13 / ggml_element_size(src1);
  4205. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  4206. src0_dd, src1_dd, dst_dd,
  4207. ne00, /*ne01, ne02, ne03,*/
  4208. /*ne10, ne11,*/ ne12, /*ne13,*/
  4209. /* s0,*/ s1, s2, s3,
  4210. /* nb00,*/ nb01, nb02, nb03,
  4211. s10, s11, s12/*, s13*/);
  4212. (void) dst;
  4213. }
  4214. template<float (*bin_op)(const float, const float)>
  4215. struct bin_bcast_cuda {
  4216. template<typename src0_t, typename src1_t, typename dst_t>
  4217. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  4218. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  4219. cudaStream_t stream) {
  4220. GGML_TENSOR_BINARY_OP_LOCALS
  4221. int nr0 = ne10/ne0;
  4222. int nr1 = ne11/ne1;
  4223. int nr2 = ne12/ne2;
  4224. int nr3 = ne13/ne3;
  4225. int nr[4] = { nr0, nr1, nr2, nr3 };
  4226. // collapse dimensions until first broadcast dimension
  4227. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  4228. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  4229. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  4230. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  4231. auto collapse = [](int64_t cne[]) {
  4232. cne[0] *= cne[1];
  4233. cne[1] = cne[2];
  4234. cne[2] = cne[3];
  4235. cne[3] = 1;
  4236. };
  4237. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  4238. cnb[1] *= cne[1];
  4239. cnb[2] *= cne[2];
  4240. cnb[3] *= cne[3];
  4241. };
  4242. for (int i = 0; i < 4; i++) {
  4243. if (nr[i] != 1) {
  4244. break;
  4245. }
  4246. if (i > 0) {
  4247. collapse_nb(cnb0, cne0);
  4248. collapse_nb(cnb1, cne1);
  4249. collapse(cne0);
  4250. collapse(cne1);
  4251. }
  4252. }
  4253. {
  4254. int64_t ne0 = cne0[0];
  4255. int64_t ne1 = cne0[1];
  4256. int64_t ne2 = cne0[2];
  4257. int64_t ne3 = cne0[3];
  4258. int64_t ne10 = cne1[0];
  4259. int64_t ne11 = cne1[1];
  4260. int64_t ne12 = cne1[2];
  4261. int64_t ne13 = cne1[3];
  4262. size_t nb0 = cnb0[0];
  4263. size_t nb1 = cnb0[1];
  4264. size_t nb2 = cnb0[2];
  4265. size_t nb3 = cnb0[3];
  4266. size_t nb10 = cnb1[0];
  4267. size_t nb11 = cnb1[1];
  4268. size_t nb12 = cnb1[2];
  4269. size_t nb13 = cnb1[3];
  4270. size_t s0 = nb0 / sizeof(dst_t);
  4271. size_t s1 = nb1 / sizeof(dst_t);
  4272. size_t s2 = nb2 / sizeof(dst_t);
  4273. size_t s3 = nb3 / sizeof(dst_t);
  4274. size_t s10 = nb10 / sizeof(src1_t);
  4275. size_t s11 = nb11 / sizeof(src1_t);
  4276. size_t s12 = nb12 / sizeof(src1_t);
  4277. size_t s13 = nb13 / sizeof(src1_t);
  4278. GGML_ASSERT(s0 == 1);
  4279. GGML_ASSERT(s10 == 1);
  4280. const int block_size = 128;
  4281. int64_t hne0 = std::max(ne0/2LL, 1LL);
  4282. dim3 block_dims;
  4283. block_dims.x = std::min<unsigned int>(hne0, block_size);
  4284. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  4285. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  4286. dim3 block_nums(
  4287. (hne0 + block_dims.x - 1) / block_dims.x,
  4288. (ne1 + block_dims.y - 1) / block_dims.y,
  4289. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  4290. );
  4291. if (block_nums.z > 65535) {
  4292. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  4293. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  4294. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  4295. src0_dd, src1_dd, dst_dd,
  4296. ne0, ne1, ne2, ne3,
  4297. ne10, ne11, ne12, ne13,
  4298. /* s0, */ s1, s2, s3,
  4299. /* s10, */ s11, s12, s13);
  4300. } else {
  4301. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  4302. src0_dd, src1_dd, dst_dd,
  4303. ne0, ne1, ne2, ne3,
  4304. ne10, ne11, ne12, ne13,
  4305. /* s0, */ s1, s2, s3,
  4306. /* s10, */ s11, s12, s13);
  4307. }
  4308. }
  4309. }
  4310. };
  4311. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  4312. const int ne10, const int ne11, const int ne12,
  4313. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  4314. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  4315. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  4316. }
  4317. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4318. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4319. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4320. }
  4321. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4322. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  4323. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4324. }
  4325. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4326. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  4327. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4328. }
  4329. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4330. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  4331. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4332. }
  4333. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4334. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4335. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4336. }
  4337. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  4338. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  4339. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  4340. }
  4341. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  4342. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  4343. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  4344. }
  4345. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4346. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4347. if (ncols < 1024) {
  4348. const dim3 block_dims(WARP_SIZE, 1, 1);
  4349. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4350. } else {
  4351. const dim3 block_dims(1024, 1, 1);
  4352. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4353. }
  4354. }
  4355. static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
  4356. static const float eps = 1e-6f;
  4357. if (group_size < 1024) {
  4358. const dim3 block_dims(WARP_SIZE, 1, 1);
  4359. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4360. } else {
  4361. const dim3 block_dims(1024, 1, 1);
  4362. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  4363. }
  4364. }
  4365. static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
  4366. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  4367. dim3 gridDim(num_blocks, ne1, ne2);
  4368. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  4369. }
  4370. static void upscale_f32_cuda(const float * x, float * dst, const int ne00, const int ne01, const int ne02, const int scale_factor, cudaStream_t stream) {
  4371. int ne0 = (ne00 * scale_factor);
  4372. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  4373. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  4374. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  4375. }
  4376. static void pad_f32_cuda(const float * x, float * dst,
  4377. const int ne00, const int ne01, const int ne02,
  4378. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  4379. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  4380. dim3 gridDim(num_blocks, ne1, ne2);
  4381. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  4382. }
  4383. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  4384. GGML_ASSERT(ncols % WARP_SIZE == 0);
  4385. if (ncols < 1024) {
  4386. const dim3 block_dims(WARP_SIZE, 1, 1);
  4387. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4388. } else {
  4389. const dim3 block_dims(1024, 1, 1);
  4390. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  4391. }
  4392. }
  4393. 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) {
  4394. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  4395. const dim3 num_blocks(block_num_x, ky, 1);
  4396. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  4397. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  4398. }
  4399. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4400. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  4401. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  4402. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  4403. }
  4404. template<typename dst_t>
  4405. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4406. const int nb = k / QK_K;
  4407. #if QK_K == 256
  4408. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  4409. #else
  4410. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  4411. #endif
  4412. }
  4413. template<typename dst_t>
  4414. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4415. const int nb = k / QK_K;
  4416. #if QK_K == 256
  4417. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  4418. #else
  4419. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  4420. #endif
  4421. }
  4422. template<typename dst_t>
  4423. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4424. const int nb = k / QK_K;
  4425. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  4426. }
  4427. template<typename dst_t>
  4428. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4429. const int nb = k / QK_K;
  4430. #if QK_K == 256
  4431. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  4432. #else
  4433. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  4434. #endif
  4435. }
  4436. template<typename dst_t>
  4437. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  4438. const int nb = k / QK_K;
  4439. #if QK_K == 256
  4440. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  4441. #else
  4442. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  4443. #endif
  4444. }
  4445. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  4446. switch (type) {
  4447. case GGML_TYPE_Q4_0:
  4448. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4449. case GGML_TYPE_Q4_1:
  4450. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4451. case GGML_TYPE_Q5_0:
  4452. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4453. case GGML_TYPE_Q5_1:
  4454. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4455. case GGML_TYPE_Q8_0:
  4456. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4457. case GGML_TYPE_Q2_K:
  4458. return dequantize_row_q2_K_cuda;
  4459. case GGML_TYPE_Q3_K:
  4460. return dequantize_row_q3_K_cuda;
  4461. case GGML_TYPE_Q4_K:
  4462. return dequantize_row_q4_K_cuda;
  4463. case GGML_TYPE_Q5_K:
  4464. return dequantize_row_q5_K_cuda;
  4465. case GGML_TYPE_Q6_K:
  4466. return dequantize_row_q6_K_cuda;
  4467. case GGML_TYPE_F32:
  4468. return dequantize_block_cuda<1, 1, convert_f32>;
  4469. default:
  4470. return nullptr;
  4471. }
  4472. }
  4473. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  4474. switch (type) {
  4475. case GGML_TYPE_Q4_0:
  4476. return dequantize_block_cuda<QK4_0, QR4_0, dequantize_q4_0>;
  4477. case GGML_TYPE_Q4_1:
  4478. return dequantize_block_cuda<QK4_1, QR4_1, dequantize_q4_1>;
  4479. case GGML_TYPE_Q5_0:
  4480. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  4481. case GGML_TYPE_Q5_1:
  4482. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  4483. case GGML_TYPE_Q8_0:
  4484. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  4485. case GGML_TYPE_Q2_K:
  4486. return dequantize_row_q2_K_cuda;
  4487. case GGML_TYPE_Q3_K:
  4488. return dequantize_row_q3_K_cuda;
  4489. case GGML_TYPE_Q4_K:
  4490. return dequantize_row_q4_K_cuda;
  4491. case GGML_TYPE_Q5_K:
  4492. return dequantize_row_q5_K_cuda;
  4493. case GGML_TYPE_Q6_K:
  4494. return dequantize_row_q6_K_cuda;
  4495. case GGML_TYPE_F16:
  4496. return dequantize_block_cuda<1, 1, convert_f16>;
  4497. default:
  4498. return nullptr;
  4499. }
  4500. }
  4501. 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) {
  4502. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4503. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4504. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  4505. const dim3 block_nums(block_num_y, 1, 1);
  4506. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4507. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  4508. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4509. }
  4510. 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) {
  4511. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4512. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4513. const dim3 block_nums(block_num_y, 1, 1);
  4514. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4515. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  4516. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4517. }
  4518. 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) {
  4519. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4520. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4521. const dim3 block_nums(block_num_y, 1, 1);
  4522. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4523. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  4524. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4525. }
  4526. 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) {
  4527. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4528. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4529. const dim3 block_nums(block_num_y, 1, 1);
  4530. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4531. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  4532. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4533. }
  4534. 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) {
  4535. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4536. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4537. const dim3 block_nums(block_num_y, 1, 1);
  4538. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4539. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  4540. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4541. }
  4542. 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) {
  4543. GGML_ASSERT(ncols % QK_K == 0);
  4544. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  4545. const int block_num_y = (nrows + ny - 1) / ny;
  4546. const dim3 block_nums(block_num_y, 1, 1);
  4547. const dim3 block_dims(32, ny, 1);
  4548. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4549. }
  4550. 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) {
  4551. GGML_ASSERT(ncols % QK_K == 0);
  4552. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4553. const int block_num_y = (nrows + ny - 1) / ny;
  4554. const dim3 block_nums(block_num_y, 1, 1);
  4555. const dim3 block_dims(32, ny, 1);
  4556. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4557. }
  4558. 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) {
  4559. GGML_ASSERT(ncols % QK_K == 0);
  4560. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4561. const int block_num_y = (nrows + ny - 1) / ny;
  4562. const dim3 block_nums(block_num_y, 1, 1);
  4563. const dim3 block_dims(32, ny, 1);
  4564. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4565. }
  4566. 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) {
  4567. GGML_ASSERT(ncols % QK_K == 0);
  4568. const dim3 block_dims(32, 1, 1);
  4569. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  4570. }
  4571. 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) {
  4572. GGML_ASSERT(ncols % QK_K == 0);
  4573. const int ny = 2 / K_QUANTS_PER_ITERATION;
  4574. const int block_num_y = (nrows + ny - 1) / ny;
  4575. const dim3 block_nums(block_num_y, 1, 1);
  4576. const dim3 block_dims(32, ny, 1);
  4577. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4578. }
  4579. 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) {
  4580. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  4581. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4582. const dim3 block_nums(block_num_y, 1, 1);
  4583. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4584. dequantize_mul_mat_vec<1, 1, convert_f16>
  4585. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  4586. }
  4587. 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) {
  4588. GGML_ASSERT(ncols % QK4_0 == 0);
  4589. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4590. const dim3 block_nums(block_num_y, 1, 1);
  4591. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4592. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  4593. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4594. }
  4595. 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) {
  4596. GGML_ASSERT(ncols % QK4_1 == 0);
  4597. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4598. const dim3 block_nums(block_num_y, 1, 1);
  4599. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4600. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  4601. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4602. }
  4603. 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) {
  4604. GGML_ASSERT(ncols % QK5_0 == 0);
  4605. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4606. const dim3 block_nums(block_num_y, 1, 1);
  4607. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4608. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  4609. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4610. }
  4611. 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) {
  4612. GGML_ASSERT(ncols % QK5_1 == 0);
  4613. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4614. const dim3 block_nums(block_num_y, 1, 1);
  4615. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4616. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  4617. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4618. }
  4619. 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) {
  4620. GGML_ASSERT(ncols % QK8_0 == 0);
  4621. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4622. const dim3 block_nums(block_num_y, 1, 1);
  4623. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4624. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  4625. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4626. }
  4627. 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) {
  4628. GGML_ASSERT(ncols % QK_K == 0);
  4629. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4630. const dim3 block_nums(block_num_y, 1, 1);
  4631. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4632. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  4633. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4634. }
  4635. 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) {
  4636. GGML_ASSERT(ncols % QK_K == 0);
  4637. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4638. const dim3 block_nums(block_num_y, 1, 1);
  4639. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4640. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  4641. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4642. }
  4643. 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) {
  4644. GGML_ASSERT(ncols % QK_K == 0);
  4645. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4646. const dim3 block_nums(block_num_y, 1, 1);
  4647. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4648. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  4649. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4650. }
  4651. 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) {
  4652. GGML_ASSERT(ncols % QK_K == 0);
  4653. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4654. const dim3 block_nums(block_num_y, 1, 1);
  4655. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4656. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  4657. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4658. }
  4659. 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) {
  4660. GGML_ASSERT(ncols % QK_K == 0);
  4661. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  4662. const dim3 block_nums(block_num_y, 1, 1);
  4663. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  4664. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  4665. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols, nrows);
  4666. }
  4667. static void ggml_mul_mat_q4_0_q8_1_cuda(
  4668. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4669. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4670. int id;
  4671. CUDA_CHECK(cudaGetDevice(&id));
  4672. const int compute_capability = g_compute_capabilities[id];
  4673. int mmq_x, mmq_y, nwarps;
  4674. if (compute_capability >= CC_RDNA2) {
  4675. mmq_x = MMQ_X_Q4_0_RDNA2;
  4676. mmq_y = MMQ_Y_Q4_0_RDNA2;
  4677. nwarps = NWARPS_Q4_0_RDNA2;
  4678. } else if (compute_capability >= CC_OFFSET_AMD) {
  4679. mmq_x = MMQ_X_Q4_0_RDNA1;
  4680. mmq_y = MMQ_Y_Q4_0_RDNA1;
  4681. nwarps = NWARPS_Q4_0_RDNA1;
  4682. } else if (compute_capability >= CC_VOLTA) {
  4683. mmq_x = MMQ_X_Q4_0_AMPERE;
  4684. mmq_y = MMQ_Y_Q4_0_AMPERE;
  4685. nwarps = NWARPS_Q4_0_AMPERE;
  4686. } else if (compute_capability >= MIN_CC_DP4A) {
  4687. mmq_x = MMQ_X_Q4_0_PASCAL;
  4688. mmq_y = MMQ_Y_Q4_0_PASCAL;
  4689. nwarps = NWARPS_Q4_0_PASCAL;
  4690. } else {
  4691. GGML_ASSERT(false);
  4692. }
  4693. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4694. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4695. const dim3 block_nums(block_num_x, block_num_y, 1);
  4696. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4697. if (nrows_x % mmq_y == 0) {
  4698. const bool need_check = false;
  4699. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4700. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4701. } else {
  4702. const bool need_check = true;
  4703. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4704. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4705. }
  4706. }
  4707. static void ggml_mul_mat_q4_1_q8_1_cuda(
  4708. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4709. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4710. int id;
  4711. CUDA_CHECK(cudaGetDevice(&id));
  4712. const int compute_capability = g_compute_capabilities[id];
  4713. int mmq_x, mmq_y, nwarps;
  4714. if (compute_capability >= CC_RDNA2) {
  4715. mmq_x = MMQ_X_Q4_1_RDNA2;
  4716. mmq_y = MMQ_Y_Q4_1_RDNA2;
  4717. nwarps = NWARPS_Q4_1_RDNA2;
  4718. } else if (compute_capability >= CC_OFFSET_AMD) {
  4719. mmq_x = MMQ_X_Q4_1_RDNA1;
  4720. mmq_y = MMQ_Y_Q4_1_RDNA1;
  4721. nwarps = NWARPS_Q4_1_RDNA1;
  4722. } else if (compute_capability >= CC_VOLTA) {
  4723. mmq_x = MMQ_X_Q4_1_AMPERE;
  4724. mmq_y = MMQ_Y_Q4_1_AMPERE;
  4725. nwarps = NWARPS_Q4_1_AMPERE;
  4726. } else if (compute_capability >= MIN_CC_DP4A) {
  4727. mmq_x = MMQ_X_Q4_1_PASCAL;
  4728. mmq_y = MMQ_Y_Q4_1_PASCAL;
  4729. nwarps = NWARPS_Q4_1_PASCAL;
  4730. } else {
  4731. GGML_ASSERT(false);
  4732. }
  4733. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4734. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4735. const dim3 block_nums(block_num_x, block_num_y, 1);
  4736. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4737. if (nrows_x % mmq_y == 0) {
  4738. const bool need_check = false;
  4739. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4740. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4741. } else {
  4742. const bool need_check = true;
  4743. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4744. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4745. }
  4746. }
  4747. static void ggml_mul_mat_q5_0_q8_1_cuda(
  4748. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4749. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4750. int id;
  4751. CUDA_CHECK(cudaGetDevice(&id));
  4752. const int compute_capability = g_compute_capabilities[id];
  4753. int mmq_x, mmq_y, nwarps;
  4754. if (compute_capability >= CC_RDNA2) {
  4755. mmq_x = MMQ_X_Q5_0_RDNA2;
  4756. mmq_y = MMQ_Y_Q5_0_RDNA2;
  4757. nwarps = NWARPS_Q5_0_RDNA2;
  4758. } else if (compute_capability >= CC_OFFSET_AMD) {
  4759. mmq_x = MMQ_X_Q5_0_RDNA1;
  4760. mmq_y = MMQ_Y_Q5_0_RDNA1;
  4761. nwarps = NWARPS_Q5_0_RDNA1;
  4762. } else if (compute_capability >= CC_VOLTA) {
  4763. mmq_x = MMQ_X_Q5_0_AMPERE;
  4764. mmq_y = MMQ_Y_Q5_0_AMPERE;
  4765. nwarps = NWARPS_Q5_0_AMPERE;
  4766. } else if (compute_capability >= MIN_CC_DP4A) {
  4767. mmq_x = MMQ_X_Q5_0_PASCAL;
  4768. mmq_y = MMQ_Y_Q5_0_PASCAL;
  4769. nwarps = NWARPS_Q5_0_PASCAL;
  4770. } else {
  4771. GGML_ASSERT(false);
  4772. }
  4773. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4774. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4775. const dim3 block_nums(block_num_x, block_num_y, 1);
  4776. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4777. if (nrows_x % mmq_y == 0) {
  4778. const bool need_check = false;
  4779. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4780. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4781. } else {
  4782. const bool need_check = true;
  4783. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4784. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4785. }
  4786. }
  4787. static void ggml_mul_mat_q5_1_q8_1_cuda(
  4788. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4789. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4790. int id;
  4791. CUDA_CHECK(cudaGetDevice(&id));
  4792. const int compute_capability = g_compute_capabilities[id];
  4793. int mmq_x, mmq_y, nwarps;
  4794. if (compute_capability >= CC_RDNA2) {
  4795. mmq_x = MMQ_X_Q5_1_RDNA2;
  4796. mmq_y = MMQ_Y_Q5_1_RDNA2;
  4797. nwarps = NWARPS_Q5_1_RDNA2;
  4798. } else if (compute_capability >= CC_OFFSET_AMD) {
  4799. mmq_x = MMQ_X_Q5_1_RDNA1;
  4800. mmq_y = MMQ_Y_Q5_1_RDNA1;
  4801. nwarps = NWARPS_Q5_1_RDNA1;
  4802. } else if (compute_capability >= CC_VOLTA) {
  4803. mmq_x = MMQ_X_Q5_1_AMPERE;
  4804. mmq_y = MMQ_Y_Q5_1_AMPERE;
  4805. nwarps = NWARPS_Q5_1_AMPERE;
  4806. } else if (compute_capability >= MIN_CC_DP4A) {
  4807. mmq_x = MMQ_X_Q5_1_PASCAL;
  4808. mmq_y = MMQ_Y_Q5_1_PASCAL;
  4809. nwarps = NWARPS_Q5_1_PASCAL;
  4810. } else {
  4811. GGML_ASSERT(false);
  4812. }
  4813. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4814. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4815. const dim3 block_nums(block_num_x, block_num_y, 1);
  4816. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4817. if (nrows_x % mmq_y == 0) {
  4818. const bool need_check = false;
  4819. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4820. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4821. } else {
  4822. const bool need_check = true;
  4823. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  4824. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4825. }
  4826. }
  4827. static void ggml_mul_mat_q8_0_q8_1_cuda(
  4828. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4829. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4830. int id;
  4831. CUDA_CHECK(cudaGetDevice(&id));
  4832. const int compute_capability = g_compute_capabilities[id];
  4833. int mmq_x, mmq_y, nwarps;
  4834. if (compute_capability >= CC_RDNA2) {
  4835. mmq_x = MMQ_X_Q8_0_RDNA2;
  4836. mmq_y = MMQ_Y_Q8_0_RDNA2;
  4837. nwarps = NWARPS_Q8_0_RDNA2;
  4838. } else if (compute_capability >= CC_OFFSET_AMD) {
  4839. mmq_x = MMQ_X_Q8_0_RDNA1;
  4840. mmq_y = MMQ_Y_Q8_0_RDNA1;
  4841. nwarps = NWARPS_Q8_0_RDNA1;
  4842. } else if (compute_capability >= CC_VOLTA) {
  4843. mmq_x = MMQ_X_Q8_0_AMPERE;
  4844. mmq_y = MMQ_Y_Q8_0_AMPERE;
  4845. nwarps = NWARPS_Q8_0_AMPERE;
  4846. } else if (compute_capability >= MIN_CC_DP4A) {
  4847. mmq_x = MMQ_X_Q8_0_PASCAL;
  4848. mmq_y = MMQ_Y_Q8_0_PASCAL;
  4849. nwarps = NWARPS_Q8_0_PASCAL;
  4850. } else {
  4851. GGML_ASSERT(false);
  4852. }
  4853. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4854. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4855. const dim3 block_nums(block_num_x, block_num_y, 1);
  4856. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4857. if (nrows_x % mmq_y == 0) {
  4858. const bool need_check = false;
  4859. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4860. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4861. } else {
  4862. const bool need_check = true;
  4863. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  4864. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4865. }
  4866. }
  4867. static void ggml_mul_mat_q2_K_q8_1_cuda(
  4868. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4869. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4870. int id;
  4871. CUDA_CHECK(cudaGetDevice(&id));
  4872. const int compute_capability = g_compute_capabilities[id];
  4873. int mmq_x, mmq_y, nwarps;
  4874. if (compute_capability >= CC_RDNA2) {
  4875. mmq_x = MMQ_X_Q2_K_RDNA2;
  4876. mmq_y = MMQ_Y_Q2_K_RDNA2;
  4877. nwarps = NWARPS_Q2_K_RDNA2;
  4878. } else if (compute_capability >= CC_OFFSET_AMD) {
  4879. mmq_x = MMQ_X_Q2_K_RDNA1;
  4880. mmq_y = MMQ_Y_Q2_K_RDNA1;
  4881. nwarps = NWARPS_Q2_K_RDNA1;
  4882. } else if (compute_capability >= CC_VOLTA) {
  4883. mmq_x = MMQ_X_Q2_K_AMPERE;
  4884. mmq_y = MMQ_Y_Q2_K_AMPERE;
  4885. nwarps = NWARPS_Q2_K_AMPERE;
  4886. } else if (compute_capability >= MIN_CC_DP4A) {
  4887. mmq_x = MMQ_X_Q2_K_PASCAL;
  4888. mmq_y = MMQ_Y_Q2_K_PASCAL;
  4889. nwarps = NWARPS_Q2_K_PASCAL;
  4890. } else {
  4891. GGML_ASSERT(false);
  4892. }
  4893. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4894. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4895. const dim3 block_nums(block_num_x, block_num_y, 1);
  4896. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4897. if (nrows_x % mmq_y == 0) {
  4898. const bool need_check = false;
  4899. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4900. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4901. } else {
  4902. const bool need_check = true;
  4903. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4904. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4905. }
  4906. }
  4907. static void ggml_mul_mat_q3_K_q8_1_cuda(
  4908. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4909. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4910. #if QK_K == 256
  4911. int id;
  4912. CUDA_CHECK(cudaGetDevice(&id));
  4913. const int compute_capability = g_compute_capabilities[id];
  4914. int mmq_x, mmq_y, nwarps;
  4915. if (compute_capability >= CC_RDNA2) {
  4916. mmq_x = MMQ_X_Q3_K_RDNA2;
  4917. mmq_y = MMQ_Y_Q3_K_RDNA2;
  4918. nwarps = NWARPS_Q3_K_RDNA2;
  4919. } else if (compute_capability >= CC_OFFSET_AMD) {
  4920. mmq_x = MMQ_X_Q3_K_RDNA1;
  4921. mmq_y = MMQ_Y_Q3_K_RDNA1;
  4922. nwarps = NWARPS_Q3_K_RDNA1;
  4923. } else if (compute_capability >= CC_VOLTA) {
  4924. mmq_x = MMQ_X_Q3_K_AMPERE;
  4925. mmq_y = MMQ_Y_Q3_K_AMPERE;
  4926. nwarps = NWARPS_Q3_K_AMPERE;
  4927. } else if (compute_capability >= MIN_CC_DP4A) {
  4928. mmq_x = MMQ_X_Q3_K_PASCAL;
  4929. mmq_y = MMQ_Y_Q3_K_PASCAL;
  4930. nwarps = NWARPS_Q3_K_PASCAL;
  4931. } else {
  4932. GGML_ASSERT(false);
  4933. }
  4934. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4935. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4936. const dim3 block_nums(block_num_x, block_num_y, 1);
  4937. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4938. if (nrows_x % mmq_y == 0) {
  4939. const bool need_check = false;
  4940. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4941. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4942. } else {
  4943. const bool need_check = true;
  4944. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4945. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4946. }
  4947. #endif
  4948. }
  4949. static void ggml_mul_mat_q4_K_q8_1_cuda(
  4950. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4951. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4952. int id;
  4953. CUDA_CHECK(cudaGetDevice(&id));
  4954. const int compute_capability = g_compute_capabilities[id];
  4955. int mmq_x, mmq_y, nwarps;
  4956. if (compute_capability >= CC_RDNA2) {
  4957. mmq_x = MMQ_X_Q4_K_RDNA2;
  4958. mmq_y = MMQ_Y_Q4_K_RDNA2;
  4959. nwarps = NWARPS_Q4_K_RDNA2;
  4960. } else if (compute_capability >= CC_OFFSET_AMD) {
  4961. mmq_x = MMQ_X_Q4_K_RDNA1;
  4962. mmq_y = MMQ_Y_Q4_K_RDNA1;
  4963. nwarps = NWARPS_Q4_K_RDNA1;
  4964. } else if (compute_capability >= CC_VOLTA) {
  4965. mmq_x = MMQ_X_Q4_K_AMPERE;
  4966. mmq_y = MMQ_Y_Q4_K_AMPERE;
  4967. nwarps = NWARPS_Q4_K_AMPERE;
  4968. } else if (compute_capability >= MIN_CC_DP4A) {
  4969. mmq_x = MMQ_X_Q4_K_PASCAL;
  4970. mmq_y = MMQ_Y_Q4_K_PASCAL;
  4971. nwarps = NWARPS_Q4_K_PASCAL;
  4972. } else {
  4973. GGML_ASSERT(false);
  4974. }
  4975. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  4976. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  4977. const dim3 block_nums(block_num_x, block_num_y, 1);
  4978. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  4979. if (nrows_x % mmq_y == 0) {
  4980. const bool need_check = false;
  4981. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4982. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4983. } else {
  4984. const bool need_check = true;
  4985. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  4986. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4987. }
  4988. }
  4989. static void ggml_mul_mat_q5_K_q8_1_cuda(
  4990. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  4991. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  4992. int id;
  4993. CUDA_CHECK(cudaGetDevice(&id));
  4994. const int compute_capability = g_compute_capabilities[id];
  4995. int mmq_x, mmq_y, nwarps;
  4996. if (compute_capability >= CC_RDNA2) {
  4997. mmq_x = MMQ_X_Q5_K_RDNA2;
  4998. mmq_y = MMQ_Y_Q5_K_RDNA2;
  4999. nwarps = NWARPS_Q5_K_RDNA2;
  5000. } else if (compute_capability >= CC_OFFSET_AMD) {
  5001. mmq_x = MMQ_X_Q5_K_RDNA1;
  5002. mmq_y = MMQ_Y_Q5_K_RDNA1;
  5003. nwarps = NWARPS_Q5_K_RDNA1;
  5004. } else if (compute_capability >= CC_VOLTA) {
  5005. mmq_x = MMQ_X_Q5_K_AMPERE;
  5006. mmq_y = MMQ_Y_Q5_K_AMPERE;
  5007. nwarps = NWARPS_Q5_K_AMPERE;
  5008. } else if (compute_capability >= MIN_CC_DP4A) {
  5009. mmq_x = MMQ_X_Q5_K_PASCAL;
  5010. mmq_y = MMQ_Y_Q5_K_PASCAL;
  5011. nwarps = NWARPS_Q5_K_PASCAL;
  5012. } else {
  5013. GGML_ASSERT(false);
  5014. }
  5015. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5016. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5017. const dim3 block_nums(block_num_x, block_num_y, 1);
  5018. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5019. if (nrows_x % mmq_y == 0) {
  5020. const bool need_check = false;
  5021. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5022. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5023. } else {
  5024. const bool need_check = true;
  5025. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5026. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5027. }
  5028. }
  5029. static void ggml_mul_mat_q6_K_q8_1_cuda(
  5030. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5031. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5032. int id;
  5033. CUDA_CHECK(cudaGetDevice(&id));
  5034. const int compute_capability = g_compute_capabilities[id];
  5035. int mmq_x, mmq_y, nwarps;
  5036. if (compute_capability >= CC_RDNA2) {
  5037. mmq_x = MMQ_X_Q6_K_RDNA2;
  5038. mmq_y = MMQ_Y_Q6_K_RDNA2;
  5039. nwarps = NWARPS_Q6_K_RDNA2;
  5040. } else if (compute_capability >= CC_OFFSET_AMD) {
  5041. mmq_x = MMQ_X_Q6_K_RDNA1;
  5042. mmq_y = MMQ_Y_Q6_K_RDNA1;
  5043. nwarps = NWARPS_Q6_K_RDNA1;
  5044. } else if (compute_capability >= CC_VOLTA) {
  5045. mmq_x = MMQ_X_Q6_K_AMPERE;
  5046. mmq_y = MMQ_Y_Q6_K_AMPERE;
  5047. nwarps = NWARPS_Q6_K_AMPERE;
  5048. } else if (compute_capability >= MIN_CC_DP4A) {
  5049. mmq_x = MMQ_X_Q6_K_PASCAL;
  5050. mmq_y = MMQ_Y_Q6_K_PASCAL;
  5051. nwarps = NWARPS_Q6_K_PASCAL;
  5052. } else {
  5053. GGML_ASSERT(false);
  5054. }
  5055. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5056. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5057. const dim3 block_nums(block_num_x, block_num_y, 1);
  5058. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5059. if (nrows_x % mmq_y == 0) {
  5060. const bool need_check = false;
  5061. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5062. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5063. } else {
  5064. const bool need_check = true;
  5065. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5066. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5067. }
  5068. }
  5069. static void ggml_mul_mat_p021_f16_f32_cuda(
  5070. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  5071. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  5072. const dim3 block_nums(1, nrows_x, nchannels_y);
  5073. const dim3 block_dims(WARP_SIZE, 1, 1);
  5074. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  5075. }
  5076. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  5077. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  5078. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  5079. const dim3 block_nums(1, nrows_x, nchannels_y);
  5080. const dim3 block_dims(WARP_SIZE, 1, 1);
  5081. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  5082. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  5083. }
  5084. static void ggml_cpy_f32_f32_cuda(
  5085. const char * cx, char * cdst, const int ne,
  5086. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5087. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5088. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5089. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5090. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5091. }
  5092. static void ggml_cpy_f32_f16_cuda(
  5093. const char * cx, char * cdst, const int ne,
  5094. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5095. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5096. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5097. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5098. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5099. }
  5100. static void ggml_cpy_f32_q8_0_cuda(
  5101. const char * cx, char * cdst, const int ne,
  5102. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5103. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5104. GGML_ASSERT(ne % QK8_0 == 0);
  5105. const int num_blocks = ne / QK8_0;
  5106. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  5107. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5108. }
  5109. static void ggml_cpy_f32_q4_0_cuda(
  5110. const char * cx, char * cdst, const int ne,
  5111. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5112. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5113. GGML_ASSERT(ne % QK4_0 == 0);
  5114. const int num_blocks = ne / QK4_0;
  5115. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  5116. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5117. }
  5118. static void ggml_cpy_f32_q4_1_cuda(
  5119. const char * cx, char * cdst, const int ne,
  5120. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5121. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5122. GGML_ASSERT(ne % QK4_1 == 0);
  5123. const int num_blocks = ne / QK4_1;
  5124. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  5125. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5126. }
  5127. static void ggml_cpy_f16_f16_cuda(
  5128. const char * cx, char * cdst, const int ne,
  5129. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  5130. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12, cudaStream_t stream) {
  5131. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  5132. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  5133. (cx, cdst, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12);
  5134. }
  5135. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  5136. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  5137. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  5138. }
  5139. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  5140. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  5141. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  5142. }
  5143. template<typename T>
  5144. static void rope_cuda(
  5145. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5146. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5147. ) {
  5148. GGML_ASSERT(ncols % 2 == 0);
  5149. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5150. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5151. const dim3 block_nums(nrows, num_blocks_x, 1);
  5152. if (pos == nullptr) {
  5153. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  5154. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5155. );
  5156. } else {
  5157. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  5158. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  5159. );
  5160. }
  5161. }
  5162. template<typename T>
  5163. static void rope_neox_cuda(
  5164. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5165. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  5166. ) {
  5167. GGML_ASSERT(ncols % 2 == 0);
  5168. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  5169. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  5170. const dim3 block_nums(nrows, num_blocks_x, 1);
  5171. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  5172. const float inv_ndims = -1.0f / n_dims;
  5173. if (pos == nullptr) {
  5174. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  5175. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5176. theta_scale, inv_ndims
  5177. );
  5178. } else {
  5179. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  5180. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  5181. theta_scale, inv_ndims
  5182. );
  5183. }
  5184. }
  5185. static void rope_glm_f32_cuda(
  5186. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  5187. float freq_base, int n_ctx, cudaStream_t stream
  5188. ) {
  5189. GGML_ASSERT(ncols % 4 == 0);
  5190. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  5191. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  5192. const dim3 block_nums(num_blocks_x, nrows, 1);
  5193. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  5194. }
  5195. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  5196. const int k_rows, const int n_heads_log2_floor, const float m0,
  5197. const float m1, cudaStream_t stream) {
  5198. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  5199. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  5200. const dim3 block_nums(num_blocks_x, nrows, 1);
  5201. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  5202. }
  5203. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  5204. const dim3 block_dims(WARP_SIZE, 1, 1);
  5205. const dim3 block_nums(1, nrows, 1);
  5206. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5207. }
  5208. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  5209. // bitonic sort requires ncols to be power of 2
  5210. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  5211. const dim3 block_dims(ncols, 1, 1);
  5212. const dim3 block_nums(1, nrows, 1);
  5213. if (order == GGML_SORT_ASC) {
  5214. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5215. } else if (order == GGML_SORT_DESC) {
  5216. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  5217. } else {
  5218. GGML_ASSERT(false);
  5219. }
  5220. }
  5221. 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) {
  5222. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  5223. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  5224. const dim3 block_nums(nrows_x, block_num_x, 1);
  5225. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  5226. }
  5227. static void soft_max_f32_cuda(const float * x, const float * y, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, cudaStream_t stream) {
  5228. int nth = WARP_SIZE;
  5229. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  5230. const dim3 block_dims(nth, 1, 1);
  5231. const dim3 block_nums(nrows_x, 1, 1);
  5232. soft_max_f32<<<block_nums, block_dims, 0, stream>>>(x, y, dst, ncols_x, nrows_y, scale);
  5233. }
  5234. static void im2col_f32_f16_cuda(const float* x, half* dst,
  5235. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  5236. int offset_delta,
  5237. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  5238. const int parallel_elements = OW * KW * KH;
  5239. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  5240. dim3 block_nums(num_blocks, OH, IC);
  5241. im2col_f32_f16<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, offset_delta, IW, IH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  5242. }
  5243. // buffer pool for cuda
  5244. #define MAX_CUDA_BUFFERS 256
  5245. struct scoped_spin_lock {
  5246. std::atomic_flag& lock;
  5247. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  5248. while (lock.test_and_set(std::memory_order_acquire)) {
  5249. ; // spin
  5250. }
  5251. }
  5252. ~scoped_spin_lock() {
  5253. lock.clear(std::memory_order_release);
  5254. }
  5255. scoped_spin_lock(const scoped_spin_lock&) = delete;
  5256. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  5257. };
  5258. struct cuda_buffer {
  5259. void * ptr = nullptr;
  5260. size_t size = 0;
  5261. };
  5262. static cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  5263. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  5264. static void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  5265. scoped_spin_lock lock(g_cuda_pool_lock);
  5266. int id;
  5267. CUDA_CHECK(cudaGetDevice(&id));
  5268. #ifdef DEBUG_CUDA_MALLOC
  5269. int nnz = 0;
  5270. size_t max_size = 0, tot_size = 0;
  5271. #endif
  5272. size_t best_diff = 1ull << 36;
  5273. int ibest = -1;
  5274. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5275. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5276. if (b.ptr != nullptr) {
  5277. #ifdef DEBUG_CUDA_MALLOC
  5278. ++nnz;
  5279. tot_size += b.size;
  5280. if (b.size > max_size) max_size = b.size;
  5281. #endif
  5282. if (b.size >= size) {
  5283. size_t diff = b.size - size;
  5284. if (diff < best_diff) {
  5285. best_diff = diff;
  5286. ibest = i;
  5287. if (!best_diff) {
  5288. void * ptr = b.ptr;
  5289. *actual_size = b.size;
  5290. b.ptr = nullptr;
  5291. b.size = 0;
  5292. return ptr;
  5293. }
  5294. }
  5295. }
  5296. }
  5297. }
  5298. if (ibest >= 0) {
  5299. cuda_buffer& b = g_cuda_buffer_pool[id][ibest];
  5300. void * ptr = b.ptr;
  5301. *actual_size = b.size;
  5302. b.ptr = nullptr;
  5303. b.size = 0;
  5304. return ptr;
  5305. }
  5306. #ifdef DEBUG_CUDA_MALLOC
  5307. fprintf(stderr, "%s: %d buffers, max_size = %u MB, tot_size = %u MB, requested %u MB\n", __func__, nnz,
  5308. (uint32_t)(max_size/1024/1024), (uint32_t)(tot_size/1024/1024), (uint32_t)(size/1024/1024));
  5309. #endif
  5310. void * ptr;
  5311. size_t look_ahead_size = (size_t) (1.05 * size);
  5312. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  5313. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  5314. *actual_size = look_ahead_size;
  5315. return ptr;
  5316. }
  5317. static void ggml_cuda_pool_free(void * ptr, size_t size) {
  5318. scoped_spin_lock lock(g_cuda_pool_lock);
  5319. int id;
  5320. CUDA_CHECK(cudaGetDevice(&id));
  5321. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  5322. cuda_buffer& b = g_cuda_buffer_pool[id][i];
  5323. if (b.ptr == nullptr) {
  5324. b.ptr = ptr;
  5325. b.size = size;
  5326. return;
  5327. }
  5328. }
  5329. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  5330. CUDA_CHECK(cudaFree(ptr));
  5331. }
  5332. static bool g_cublas_loaded = false;
  5333. bool ggml_cublas_loaded(void) {
  5334. return g_cublas_loaded;
  5335. }
  5336. void ggml_init_cublas() {
  5337. static bool initialized = false;
  5338. if (!initialized) {
  5339. #ifdef __HIP_PLATFORM_AMD__
  5340. // Workaround for a rocBLAS bug when using multiple graphics cards:
  5341. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  5342. rocblas_initialize();
  5343. CUDA_CHECK(cudaDeviceSynchronize());
  5344. #endif
  5345. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  5346. initialized = true;
  5347. g_cublas_loaded = false;
  5348. return;
  5349. }
  5350. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  5351. int64_t total_vram = 0;
  5352. #if defined(GGML_CUDA_FORCE_MMQ)
  5353. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  5354. #else
  5355. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  5356. #endif
  5357. #if defined(CUDA_USE_TENSOR_CORES)
  5358. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  5359. #else
  5360. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  5361. #endif
  5362. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  5363. for (int id = 0; id < g_device_count; ++id) {
  5364. cudaDeviceProp prop;
  5365. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  5366. fprintf(stderr, " Device %d: %s, compute capability %d.%d\n", id, prop.name, prop.major, prop.minor);
  5367. g_tensor_split[id] = total_vram;
  5368. total_vram += prop.totalGlobalMem;
  5369. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5370. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  5371. #else
  5372. g_compute_capabilities[id] = 100*prop.major + 10*prop.minor;
  5373. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5374. }
  5375. for (int id = 0; id < g_device_count; ++id) {
  5376. g_tensor_split[id] /= total_vram;
  5377. }
  5378. for (int id = 0; id < g_device_count; ++id) {
  5379. CUDA_CHECK(ggml_cuda_set_device(id));
  5380. // create cuda streams
  5381. for (int is = 0; is < MAX_STREAMS; ++is) {
  5382. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  5383. }
  5384. // create cublas handle
  5385. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  5386. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  5387. }
  5388. // configure logging to stdout
  5389. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  5390. initialized = true;
  5391. g_cublas_loaded = true;
  5392. }
  5393. }
  5394. void ggml_cuda_set_tensor_split(const float * tensor_split) {
  5395. if (tensor_split == nullptr) {
  5396. return;
  5397. }
  5398. bool all_zero = true;
  5399. for (int i = 0; i < g_device_count; ++i) {
  5400. if (tensor_split[i] != 0.0f) {
  5401. all_zero = false;
  5402. break;
  5403. }
  5404. }
  5405. if (all_zero) {
  5406. return;
  5407. }
  5408. float split_sum = 0.0f;
  5409. for (int i = 0; i < g_device_count; ++i) {
  5410. g_tensor_split[i] = split_sum;
  5411. split_sum += tensor_split[i];
  5412. }
  5413. for (int i = 0; i < g_device_count; ++i) {
  5414. g_tensor_split[i] /= split_sum;
  5415. }
  5416. }
  5417. void * ggml_cuda_host_malloc(size_t size) {
  5418. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  5419. return nullptr;
  5420. }
  5421. void * ptr = nullptr;
  5422. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  5423. if (err != cudaSuccess) {
  5424. // The allocation error can be bypassed. A null ptr will assigned out of this function.
  5425. // This can fixed the OOM error in WSL.
  5426. cudaGetLastError();
  5427. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  5428. size/1024.0/1024.0, cudaGetErrorString(err));
  5429. return nullptr;
  5430. }
  5431. return ptr;
  5432. }
  5433. void ggml_cuda_host_free(void * ptr) {
  5434. CUDA_CHECK(cudaFreeHost(ptr));
  5435. }
  5436. static cudaError_t ggml_cuda_cpy_tensor_2d(
  5437. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  5438. cudaMemcpyKind kind;
  5439. char * src_ptr;
  5440. if (src->backend == GGML_BACKEND_CPU) {
  5441. kind = cudaMemcpyHostToDevice;
  5442. src_ptr = (char *) src->data;
  5443. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  5444. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  5445. kind = cudaMemcpyDeviceToDevice;
  5446. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  5447. int id;
  5448. CUDA_CHECK(cudaGetDevice(&id));
  5449. src_ptr = (char *) extra->data_device[id];
  5450. } else {
  5451. GGML_ASSERT(false);
  5452. }
  5453. char * dst_ptr = (char *) dst;
  5454. const int64_t ne0 = src->ne[0];
  5455. const int64_t nb0 = src->nb[0];
  5456. const int64_t nb1 = src->nb[1];
  5457. const int64_t nb2 = src->nb[2];
  5458. const int64_t nb3 = src->nb[3];
  5459. const enum ggml_type type = src->type;
  5460. const int64_t ts = ggml_type_size(type);
  5461. const int64_t bs = ggml_blck_size(type);
  5462. int64_t i1_diff = i1_high - i1_low;
  5463. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  5464. if (nb0 == ts && nb1 == ts*ne0/bs) {
  5465. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  5466. } else if (nb0 == ts) {
  5467. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  5468. } else {
  5469. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  5470. const void * rx = (const void *) ((const char *) x + i1*nb1);
  5471. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  5472. // pretend the row is a matrix with cols=1
  5473. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  5474. if (r != cudaSuccess) return r;
  5475. }
  5476. return cudaSuccess;
  5477. }
  5478. }
  5479. static void ggml_cuda_op_get_rows(
  5480. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5481. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & stream) {
  5482. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  5483. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5484. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  5485. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  5486. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  5487. const int32_t * src1_i32 = (const int32_t *) src1_d;
  5488. switch (src0->type) {
  5489. case GGML_TYPE_F16:
  5490. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  5491. break;
  5492. case GGML_TYPE_F32:
  5493. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5494. break;
  5495. case GGML_TYPE_Q4_0:
  5496. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5497. break;
  5498. case GGML_TYPE_Q4_1:
  5499. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5500. break;
  5501. case GGML_TYPE_Q5_0:
  5502. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5503. break;
  5504. case GGML_TYPE_Q5_1:
  5505. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5506. break;
  5507. case GGML_TYPE_Q8_0:
  5508. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  5509. break;
  5510. default:
  5511. // TODO: k-quants
  5512. GGML_ASSERT(false);
  5513. break;
  5514. }
  5515. }
  5516. template<class op>
  5517. inline void ggml_cuda_op_bin_bcast(
  5518. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5519. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5520. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5521. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  5522. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5523. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  5524. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  5525. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  5526. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  5527. } else {
  5528. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  5529. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  5530. GGML_ASSERT(false);
  5531. }
  5532. }
  5533. static void ggml_cuda_op_repeat(
  5534. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5535. const float * src0_d, const float * src1_d, float * dst_d, const cudaStream_t & main_stream) {
  5536. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  5537. (void) src1;
  5538. (void) src1_d;
  5539. }
  5540. inline void ggml_cuda_op_add(
  5541. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5542. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5543. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5544. }
  5545. inline void ggml_cuda_op_acc(
  5546. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5547. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5548. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5549. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5550. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5551. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  5552. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  5553. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  5554. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  5555. int offset = dst->op_params[3] / 4; // offset in bytes
  5556. acc_f32_cuda(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  5557. (void) dst;
  5558. }
  5559. inline void ggml_cuda_op_mul(
  5560. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5561. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5562. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5563. }
  5564. inline void ggml_cuda_op_div(
  5565. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5566. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5567. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  5568. }
  5569. inline void ggml_cuda_op_gelu(
  5570. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5571. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5572. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5573. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5574. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5575. (void) src1;
  5576. (void) dst;
  5577. (void) src1_dd;
  5578. }
  5579. inline void ggml_cuda_op_silu(
  5580. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5581. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5582. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5583. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5584. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5585. (void) src1;
  5586. (void) dst;
  5587. (void) src1_dd;
  5588. }
  5589. inline void ggml_cuda_op_gelu_quick(
  5590. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5591. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5592. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5593. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5594. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5595. (void) src1;
  5596. (void) dst;
  5597. (void) src1_dd;
  5598. }
  5599. inline void ggml_cuda_op_tanh(
  5600. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5601. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5602. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5603. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5604. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5605. (void) src1;
  5606. (void) dst;
  5607. (void) src1_dd;
  5608. }
  5609. inline void ggml_cuda_op_relu(
  5610. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5611. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5612. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5613. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5614. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5615. (void) src1;
  5616. (void) dst;
  5617. (void) src1_dd;
  5618. }
  5619. inline void ggml_cuda_op_leaky_relu(
  5620. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5621. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5622. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5623. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5624. float negative_slope;
  5625. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5626. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  5627. (void) src1;
  5628. (void) dst;
  5629. (void) src1_dd;
  5630. }
  5631. inline void ggml_cuda_op_sqr(
  5632. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5633. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5634. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5635. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5636. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  5637. (void) src1;
  5638. (void) dst;
  5639. (void) src1_dd;
  5640. }
  5641. inline void ggml_cuda_op_norm(
  5642. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5643. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5644. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5645. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5646. const int64_t ne00 = src0->ne[0];
  5647. const int64_t nrows = ggml_nrows(src0);
  5648. float eps;
  5649. memcpy(&eps, dst->op_params, sizeof(float));
  5650. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5651. (void) src1;
  5652. (void) dst;
  5653. (void) src1_dd;
  5654. }
  5655. inline void ggml_cuda_op_group_norm(
  5656. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5657. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5658. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5659. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5660. int num_groups = dst->op_params[0];
  5661. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  5662. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  5663. (void) src1;
  5664. (void) dst;
  5665. (void) src1_dd;
  5666. }
  5667. inline void ggml_cuda_op_concat(
  5668. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5669. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5670. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5671. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  5672. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5673. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  5674. concat_f32_cuda(src0_dd + i3 * (src0->nb[3] / 4), src1_dd + i3 * (src1->nb[3] / 4), dst_dd + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], main_stream);
  5675. }
  5676. (void) src1;
  5677. (void) dst;
  5678. }
  5679. inline void ggml_cuda_op_upscale(
  5680. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5681. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5682. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5683. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5684. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5685. const int scale_factor = dst->op_params[0];
  5686. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  5687. (void) src1;
  5688. (void) dst;
  5689. }
  5690. inline void ggml_cuda_op_pad(
  5691. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5692. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5693. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5694. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  5695. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  5696. pad_f32_cuda(src0_dd, dst_dd,
  5697. src0->ne[0], src0->ne[1], src0->ne[2],
  5698. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  5699. (void) src1;
  5700. (void) dst;
  5701. }
  5702. inline void ggml_cuda_op_rms_norm(
  5703. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5704. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  5705. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  5706. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  5707. const int64_t ne00 = src0->ne[0];
  5708. const int64_t nrows = ggml_nrows(src0);
  5709. float eps;
  5710. memcpy(&eps, dst->op_params, sizeof(float));
  5711. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  5712. (void) src1;
  5713. (void) dst;
  5714. (void) src1_dd;
  5715. }
  5716. inline void ggml_cuda_op_mul_mat_q(
  5717. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5718. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5719. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5720. const int64_t ne00 = src0->ne[0];
  5721. const int64_t ne10 = src1->ne[0];
  5722. GGML_ASSERT(ne10 % QK8_1 == 0);
  5723. const int64_t ne0 = dst->ne[0];
  5724. const int64_t row_diff = row_high - row_low;
  5725. int id;
  5726. CUDA_CHECK(cudaGetDevice(&id));
  5727. // the main device has a larger memory buffer to hold the results from all GPUs
  5728. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  5729. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5730. switch (src0->type) {
  5731. case GGML_TYPE_Q4_0:
  5732. 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);
  5733. break;
  5734. case GGML_TYPE_Q4_1:
  5735. 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);
  5736. break;
  5737. case GGML_TYPE_Q5_0:
  5738. 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);
  5739. break;
  5740. case GGML_TYPE_Q5_1:
  5741. 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);
  5742. break;
  5743. case GGML_TYPE_Q8_0:
  5744. 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);
  5745. break;
  5746. case GGML_TYPE_Q2_K:
  5747. 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);
  5748. break;
  5749. case GGML_TYPE_Q3_K:
  5750. 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);
  5751. break;
  5752. case GGML_TYPE_Q4_K:
  5753. 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);
  5754. break;
  5755. case GGML_TYPE_Q5_K:
  5756. 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);
  5757. break;
  5758. case GGML_TYPE_Q6_K:
  5759. 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);
  5760. break;
  5761. default:
  5762. GGML_ASSERT(false);
  5763. break;
  5764. }
  5765. (void) src1;
  5766. (void) dst;
  5767. (void) src1_ddf_i;
  5768. }
  5769. static int64_t get_row_rounding(ggml_type type) {
  5770. int64_t min_compute_capability = INT_MAX;
  5771. int64_t max_compute_capability = INT_MIN;
  5772. for (int64_t id = 0; id < g_device_count; ++id) {
  5773. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  5774. if (min_compute_capability > g_compute_capabilities[id]) {
  5775. min_compute_capability = g_compute_capabilities[id];
  5776. }
  5777. if (max_compute_capability < g_compute_capabilities[id]) {
  5778. max_compute_capability = g_compute_capabilities[id];
  5779. }
  5780. }
  5781. }
  5782. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5783. switch(type) {
  5784. case GGML_TYPE_Q4_0:
  5785. case GGML_TYPE_Q4_1:
  5786. case GGML_TYPE_Q5_0:
  5787. case GGML_TYPE_Q5_1:
  5788. case GGML_TYPE_Q8_0:
  5789. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5790. case GGML_TYPE_F16:
  5791. case GGML_TYPE_F32:
  5792. return 1;
  5793. case GGML_TYPE_Q2_K:
  5794. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  5795. case GGML_TYPE_Q3_K:
  5796. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  5797. case GGML_TYPE_Q4_K:
  5798. case GGML_TYPE_Q5_K:
  5799. case GGML_TYPE_Q6_K:
  5800. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  5801. default:
  5802. GGML_ASSERT(false);
  5803. }
  5804. #else
  5805. switch(type) {
  5806. case GGML_TYPE_Q4_0:
  5807. case GGML_TYPE_Q4_1:
  5808. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5809. case GGML_TYPE_Q5_0:
  5810. case GGML_TYPE_Q5_1:
  5811. case GGML_TYPE_Q8_0:
  5812. return 64;
  5813. case GGML_TYPE_F16:
  5814. case GGML_TYPE_F32:
  5815. return 1;
  5816. case GGML_TYPE_Q2_K:
  5817. case GGML_TYPE_Q3_K:
  5818. case GGML_TYPE_Q4_K:
  5819. case GGML_TYPE_Q5_K:
  5820. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  5821. case GGML_TYPE_Q6_K:
  5822. return 64;
  5823. default:
  5824. GGML_ASSERT(false);
  5825. }
  5826. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  5827. }
  5828. inline void ggml_cuda_op_mul_mat_vec_q(
  5829. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5830. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5831. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5832. GGML_ASSERT(ggml_nrows(src1) == 1);
  5833. const int64_t ne00 = src0->ne[0];
  5834. const int64_t row_diff = row_high - row_low;
  5835. switch (src0->type) {
  5836. case GGML_TYPE_Q4_0:
  5837. mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5838. break;
  5839. case GGML_TYPE_Q4_1:
  5840. mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5841. break;
  5842. case GGML_TYPE_Q5_0:
  5843. mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5844. break;
  5845. case GGML_TYPE_Q5_1:
  5846. mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5847. break;
  5848. case GGML_TYPE_Q8_0:
  5849. mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5850. break;
  5851. case GGML_TYPE_Q2_K:
  5852. mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5853. break;
  5854. case GGML_TYPE_Q3_K:
  5855. mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5856. break;
  5857. case GGML_TYPE_Q4_K:
  5858. mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5859. break;
  5860. case GGML_TYPE_Q5_K:
  5861. mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5862. break;
  5863. case GGML_TYPE_Q6_K:
  5864. mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  5865. break;
  5866. default:
  5867. GGML_ASSERT(false);
  5868. break;
  5869. }
  5870. (void) src1;
  5871. (void) dst;
  5872. (void) src1_ddf_i;
  5873. (void) src1_ncols;
  5874. (void) src1_padded_row_size;
  5875. }
  5876. inline void ggml_cuda_op_dequantize_mul_mat_vec(
  5877. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5878. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5879. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5880. const int64_t ne00 = src0->ne[0];
  5881. const int64_t row_diff = row_high - row_low;
  5882. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  5883. #ifdef GGML_CUDA_F16
  5884. size_t ash;
  5885. dfloat * src1_dfloat = nullptr; // dfloat == half
  5886. bool src1_convert_f16 =
  5887. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  5888. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  5889. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  5890. if (src1_convert_f16) {
  5891. src1_dfloat = (half *) ggml_cuda_pool_malloc(ne00*sizeof(half), &ash);
  5892. ggml_cpy_f32_f16_cuda((const char *) src1_ddf_i, (char *) src1_dfloat, ne00,
  5893. ne00, 1, sizeof(float), 0, 0,
  5894. ne00, 1, sizeof(half), 0, 0, stream);
  5895. }
  5896. #else
  5897. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  5898. #endif // GGML_CUDA_F16
  5899. switch (src0->type) {
  5900. case GGML_TYPE_Q4_0:
  5901. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5902. break;
  5903. case GGML_TYPE_Q4_1:
  5904. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5905. break;
  5906. case GGML_TYPE_Q5_0:
  5907. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5908. break;
  5909. case GGML_TYPE_Q5_1:
  5910. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5911. break;
  5912. case GGML_TYPE_Q8_0:
  5913. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5914. break;
  5915. case GGML_TYPE_Q2_K:
  5916. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5917. break;
  5918. case GGML_TYPE_Q3_K:
  5919. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5920. break;
  5921. case GGML_TYPE_Q4_K:
  5922. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5923. break;
  5924. case GGML_TYPE_Q5_K:
  5925. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5926. break;
  5927. case GGML_TYPE_Q6_K:
  5928. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  5929. break;
  5930. case GGML_TYPE_F16:
  5931. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  5932. break;
  5933. default:
  5934. GGML_ASSERT(false);
  5935. break;
  5936. }
  5937. #ifdef GGML_CUDA_F16
  5938. if (src1_convert_f16) {
  5939. ggml_cuda_pool_free(src1_dfloat, ash);
  5940. }
  5941. #endif // GGML_CUDA_F16
  5942. (void) src1;
  5943. (void) dst;
  5944. (void) src1_ddq_i;
  5945. (void) src1_ncols;
  5946. (void) src1_padded_row_size;
  5947. }
  5948. inline void ggml_cuda_op_mul_mat_cublas(
  5949. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  5950. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  5951. const int64_t src1_padded_row_size, const cudaStream_t & stream) {
  5952. GGML_ASSERT(src0_dd_i != nullptr);
  5953. GGML_ASSERT(src1_ddf_i != nullptr);
  5954. GGML_ASSERT(dst_dd_i != nullptr);
  5955. const int64_t ne00 = src0->ne[0];
  5956. const int64_t ne10 = src1->ne[0];
  5957. const int64_t ne0 = dst->ne[0];
  5958. const int64_t row_diff = row_high - row_low;
  5959. int id;
  5960. CUDA_CHECK(cudaGetDevice(&id));
  5961. // the main device has a larger memory buffer to hold the results from all GPUs
  5962. // ldc == nrows of the matrix that cuBLAS writes into
  5963. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  5964. const int compute_capability = g_compute_capabilities[id];
  5965. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1]) {
  5966. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  5967. half * src0_as_f16 = nullptr;
  5968. size_t src0_as = 0;
  5969. if (src0->type != GGML_TYPE_F16) {
  5970. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  5971. GGML_ASSERT(to_fp16_cuda != nullptr);
  5972. size_t ne = row_diff*ne00;
  5973. src0_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src0_as);
  5974. to_fp16_cuda(src0_dd_i, src0_as_f16, ne, stream);
  5975. }
  5976. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16;
  5977. half * src1_as_f16 = nullptr;
  5978. size_t src1_as = 0;
  5979. if (src1->type != GGML_TYPE_F16) {
  5980. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  5981. GGML_ASSERT(to_fp16_cuda != nullptr);
  5982. size_t ne = src1_ncols*ne10;
  5983. src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &src1_as);
  5984. to_fp16_cuda(src1_ddf_i, src1_as_f16, ne, stream);
  5985. }
  5986. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16;
  5987. size_t dst_as = 0;
  5988. half * dst_f16 = (half *) ggml_cuda_pool_malloc(row_diff*src1_ncols * sizeof(half), &dst_as);
  5989. const half alpha_f16 = 1.0f;
  5990. const half beta_f16 = 0.0f;
  5991. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  5992. CUBLAS_CHECK(
  5993. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  5994. row_diff, src1_ncols, ne10,
  5995. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  5996. src1_ptr, CUDA_R_16F, ne10,
  5997. &beta_f16, dst_f16, CUDA_R_16F, ldc,
  5998. CUBLAS_COMPUTE_16F,
  5999. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6000. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6001. to_fp32_cuda(dst_f16, dst_dd_i, row_diff*src1_ncols, stream);
  6002. ggml_cuda_pool_free(dst_f16, dst_as);
  6003. if (src0_as != 0) {
  6004. ggml_cuda_pool_free(src0_as_f16, src0_as);
  6005. }
  6006. if (src1_as != 0) {
  6007. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6008. }
  6009. }
  6010. else {
  6011. float * src0_ddq_as_f32 = nullptr;
  6012. size_t src0_as = 0;
  6013. if (src0->type != GGML_TYPE_F32) {
  6014. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  6015. GGML_ASSERT(to_fp32_cuda != nullptr);
  6016. src0_ddq_as_f32 = (float *) ggml_cuda_pool_malloc(row_diff*ne00 * sizeof(float), &src0_as); // NOLINT
  6017. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32, row_diff*ne00, stream);
  6018. }
  6019. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32;
  6020. const float alpha = 1.0f;
  6021. const float beta = 0.0f;
  6022. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  6023. CUBLAS_CHECK(
  6024. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6025. row_diff, src1_ncols, ne10,
  6026. &alpha, src0_ddf_i, ne00,
  6027. src1_ddf_i, ne10,
  6028. &beta, dst_dd_i, ldc));
  6029. if (src0_as != 0) {
  6030. ggml_cuda_pool_free(src0_ddq_as_f32, src0_as);
  6031. }
  6032. }
  6033. (void) dst;
  6034. (void) src1_ddq_i;
  6035. (void) src1_padded_row_size;
  6036. }
  6037. inline void ggml_cuda_op_rope(
  6038. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6039. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6040. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  6041. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  6042. GGML_ASSERT(src0->type == dst->type);
  6043. const int64_t ne00 = src0->ne[0];
  6044. const int64_t ne01 = src0->ne[1];
  6045. const int64_t ne2 = dst->ne[2];
  6046. const int64_t nrows = ggml_nrows(src0);
  6047. //const int n_past = ((int32_t *) dst->op_params)[0];
  6048. const int n_dims = ((int32_t *) dst->op_params)[1];
  6049. const int mode = ((int32_t *) dst->op_params)[2];
  6050. const int n_ctx = ((int32_t *) dst->op_params)[3];
  6051. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  6052. // RoPE alteration for extended context
  6053. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  6054. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  6055. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  6056. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  6057. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  6058. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  6059. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  6060. const int32_t * pos = nullptr;
  6061. if ((mode & 1) == 0) {
  6062. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6063. GGML_ASSERT(src1->ne[0] == ne2);
  6064. pos = (const int32_t *) src1_dd;
  6065. }
  6066. const bool is_neox = mode & 2;
  6067. const bool is_glm = mode & 4;
  6068. rope_corr_dims corr_dims;
  6069. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  6070. // compute
  6071. if (is_glm) {
  6072. GGML_ASSERT(false);
  6073. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  6074. } else if (is_neox) {
  6075. if (src0->type == GGML_TYPE_F32) {
  6076. rope_neox_cuda(
  6077. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6078. attn_factor, corr_dims, main_stream
  6079. );
  6080. } else if (src0->type == GGML_TYPE_F16) {
  6081. rope_neox_cuda(
  6082. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6083. attn_factor, corr_dims, main_stream
  6084. );
  6085. } else {
  6086. GGML_ASSERT(false);
  6087. }
  6088. } else {
  6089. if (src0->type == GGML_TYPE_F32) {
  6090. rope_cuda(
  6091. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6092. attn_factor, corr_dims, main_stream
  6093. );
  6094. } else if (src0->type == GGML_TYPE_F16) {
  6095. rope_cuda(
  6096. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  6097. attn_factor, corr_dims, main_stream
  6098. );
  6099. } else {
  6100. GGML_ASSERT(false);
  6101. }
  6102. }
  6103. (void) src1;
  6104. (void) dst;
  6105. (void) src1_dd;
  6106. }
  6107. inline void ggml_cuda_op_alibi(
  6108. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6109. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6110. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6111. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6112. const int64_t ne00 = src0->ne[0];
  6113. const int64_t ne01 = src0->ne[1];
  6114. const int64_t ne02 = src0->ne[2];
  6115. const int64_t nrows = ggml_nrows(src0);
  6116. //const int n_past = ((int32_t *) dst->op_params)[0];
  6117. const int n_head = ((int32_t *) dst->op_params)[1];
  6118. float max_bias;
  6119. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  6120. //GGML_ASSERT(ne01 + n_past == ne00);
  6121. GGML_ASSERT(n_head == ne02);
  6122. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  6123. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  6124. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  6125. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  6126. (void) src1;
  6127. (void) src1_dd;
  6128. }
  6129. inline void ggml_cuda_op_im2col(
  6130. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6131. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6132. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6133. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6134. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  6135. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  6136. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  6137. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  6138. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  6139. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  6140. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  6141. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  6142. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  6143. const int64_t IH = is_2D ? src1->ne[1] : 1;
  6144. const int64_t IW = src1->ne[0];
  6145. const int64_t KH = is_2D ? src0->ne[1] : 1;
  6146. const int64_t KW = src0->ne[0];
  6147. const int64_t OH = is_2D ? dst->ne[2] : 1;
  6148. const int64_t OW = dst->ne[1];
  6149. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  6150. im2col_f32_f16_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  6151. (void) src0;
  6152. (void) src0_dd;
  6153. }
  6154. inline void ggml_cuda_op_sum_rows(
  6155. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6156. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6157. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6158. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6159. const int64_t ncols = src0->ne[0];
  6160. const int64_t nrows = ggml_nrows(src0);
  6161. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  6162. (void) src1;
  6163. (void) dst;
  6164. (void) src1_dd;
  6165. }
  6166. inline void ggml_cuda_op_argsort(
  6167. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6168. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6169. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6170. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  6171. const int64_t ncols = src0->ne[0];
  6172. const int64_t nrows = ggml_nrows(src0);
  6173. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  6174. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  6175. (void) src1;
  6176. (void) dst;
  6177. (void) src1_dd;
  6178. }
  6179. inline void ggml_cuda_op_diag_mask_inf(
  6180. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6181. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6182. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6183. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6184. const int64_t ne00 = src0->ne[0];
  6185. const int64_t ne01 = src0->ne[1];
  6186. const int nrows0 = ggml_nrows(src0);
  6187. const int n_past = ((int32_t *) dst->op_params)[0];
  6188. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  6189. (void) src1;
  6190. (void) dst;
  6191. (void) src1_dd;
  6192. }
  6193. inline void ggml_cuda_op_soft_max(
  6194. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6195. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6196. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6197. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6198. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  6199. const int64_t ne00 = src0->ne[0];
  6200. const int64_t nrows_x = ggml_nrows(src0);
  6201. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  6202. float scale = 1.0f;
  6203. memcpy(&scale, dst->op_params, sizeof(float));
  6204. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  6205. (void) dst;
  6206. }
  6207. inline void ggml_cuda_op_scale(
  6208. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6209. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6210. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6211. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6212. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6213. float scale;
  6214. // HACK: support for ggml backend interface
  6215. if (src1->backend == GGML_BACKEND_CPU) {
  6216. scale = ((float *) src1->data)[0];
  6217. } else {
  6218. // TODO: pass pointer to kernel instead of copying to host
  6219. CUDA_CHECK(cudaMemcpy(&scale, src1->data, sizeof(float), cudaMemcpyDeviceToHost));
  6220. }
  6221. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  6222. CUDA_CHECK(cudaGetLastError());
  6223. (void) src1;
  6224. (void) dst;
  6225. (void) src1_dd;
  6226. }
  6227. inline void ggml_cuda_op_clamp(
  6228. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6229. const float * src0_dd, const float * src1_dd, float * dst_dd, const cudaStream_t & main_stream) {
  6230. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6231. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6232. float min;
  6233. float max;
  6234. memcpy(&min, dst->op_params, sizeof(float));
  6235. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  6236. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  6237. CUDA_CHECK(cudaGetLastError());
  6238. (void) src1;
  6239. (void) dst;
  6240. (void) src1_dd;
  6241. }
  6242. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  6243. const int64_t nrows0 = ggml_nrows(src0);
  6244. const bool use_src1 = src1 != nullptr;
  6245. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  6246. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  6247. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  6248. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6249. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  6250. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6251. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6252. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  6253. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  6254. const bool src1_stays_on_host = use_src1 && dst->op == GGML_OP_SCALE;
  6255. // dd = data device
  6256. float * src0_ddf = nullptr;
  6257. float * src1_ddf = nullptr;
  6258. float * dst_ddf = nullptr;
  6259. // as = actual size
  6260. size_t src0_asf = 0;
  6261. size_t src1_asf = 0;
  6262. size_t dst_asf = 0;
  6263. ggml_cuda_set_device(g_main_device);
  6264. const cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6265. if (src0_on_device) {
  6266. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  6267. } else {
  6268. src0_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_asf);
  6269. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  6270. }
  6271. if (use_src1 && !src1_stays_on_host) {
  6272. if (src1_on_device) {
  6273. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6274. } else {
  6275. src1_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf);
  6276. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  6277. }
  6278. }
  6279. if (dst_on_device) {
  6280. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6281. } else {
  6282. dst_ddf = (float *) ggml_cuda_pool_malloc(ggml_nbytes(dst), &dst_asf);
  6283. }
  6284. // do the computation
  6285. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  6286. CUDA_CHECK(cudaGetLastError());
  6287. // copy dst to host if necessary
  6288. if (!dst_on_device) {
  6289. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  6290. }
  6291. if (src0_asf > 0) {
  6292. ggml_cuda_pool_free(src0_ddf, src0_asf);
  6293. }
  6294. if (src1_asf > 0) {
  6295. ggml_cuda_pool_free(src1_ddf, src1_asf);
  6296. }
  6297. if (dst_asf > 0) {
  6298. ggml_cuda_pool_free(dst_ddf, dst_asf);
  6299. }
  6300. if (dst->backend == GGML_BACKEND_CPU) {
  6301. CUDA_CHECK(cudaDeviceSynchronize());
  6302. }
  6303. }
  6304. static void ggml_cuda_set_peer_access(const int n_tokens) {
  6305. static bool peer_access_enabled = false;
  6306. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  6307. if (peer_access_enabled == enable_peer_access) {
  6308. return;
  6309. }
  6310. #ifdef NDEBUG
  6311. for (int id = 0; id < g_device_count; ++id) {
  6312. CUDA_CHECK(ggml_cuda_set_device(id));
  6313. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  6314. if (id == id_other) {
  6315. continue;
  6316. }
  6317. if (id != g_main_device && id_other != g_main_device) {
  6318. continue;
  6319. }
  6320. int can_access_peer;
  6321. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  6322. if (can_access_peer) {
  6323. if (enable_peer_access) {
  6324. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  6325. } else {
  6326. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  6327. }
  6328. }
  6329. }
  6330. }
  6331. #endif // NDEBUG
  6332. peer_access_enabled = enable_peer_access;
  6333. }
  6334. static void ggml_cuda_op_mul_mat(
  6335. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  6336. const bool convert_src1_to_q8_1) {
  6337. const int64_t ne00 = src0->ne[0];
  6338. const int64_t ne01 = src0->ne[1];
  6339. const int64_t ne02 = src0->ne[2];
  6340. const int64_t ne03 = src0->ne[3];
  6341. const int64_t nrows0 = ggml_nrows(src0);
  6342. const int64_t ne10 = src1->ne[0];
  6343. const int64_t ne11 = src1->ne[1];
  6344. const int64_t ne12 = src1->ne[2];
  6345. const int64_t ne13 = src1->ne[3];
  6346. const int64_t nrows1 = ggml_nrows(src1);
  6347. GGML_ASSERT(ne03 == ne13);
  6348. const int64_t ne0 = dst->ne[0];
  6349. const int64_t ne1 = dst->ne[1];
  6350. const int nb2 = dst->nb[2];
  6351. const int nb3 = dst->nb[3];
  6352. ggml_cuda_set_peer_access(ne11);
  6353. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  6354. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  6355. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  6356. const int64_t i02_divisor = ne12 / ne02;
  6357. const size_t src0_ts = ggml_type_size(src0->type);
  6358. const size_t src0_bs = ggml_blck_size(src0->type);
  6359. const size_t q8_1_ts = sizeof(block_q8_1);
  6360. const size_t q8_1_bs = QK8_1;
  6361. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6362. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6363. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6364. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  6365. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  6366. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  6367. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  6368. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6369. GGML_ASSERT(!(split && ne02 > 1));
  6370. GGML_ASSERT(!(split && ne03 > 1));
  6371. GGML_ASSERT(!(split && ne02 < ne12));
  6372. // dd = data device
  6373. char * src0_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6374. float * src1_ddf[GGML_CUDA_MAX_DEVICES] = {nullptr}; // float
  6375. char * src1_ddq[GGML_CUDA_MAX_DEVICES] = {nullptr}; // q8_1
  6376. float * dst_dd[GGML_CUDA_MAX_DEVICES] = {nullptr};
  6377. // as = actual size
  6378. size_t src0_as[GGML_CUDA_MAX_DEVICES] = {0};
  6379. size_t src1_asf[GGML_CUDA_MAX_DEVICES] = {0};
  6380. size_t src1_asq[GGML_CUDA_MAX_DEVICES] = {0};
  6381. size_t dst_as[GGML_CUDA_MAX_DEVICES] = {0};
  6382. int64_t row_low[GGML_CUDA_MAX_DEVICES];
  6383. int64_t row_high[GGML_CUDA_MAX_DEVICES];
  6384. int used_devices = 0;
  6385. for (int64_t id = 0; id < g_device_count; ++id) {
  6386. // by default, use all rows
  6387. row_low[id] = 0;
  6388. row_high[id] = ne01;
  6389. // for multi GPU, get the row boundaries from tensor split
  6390. // and round to mul_mat_q tile sizes
  6391. if (split) {
  6392. const int64_t rounding = get_row_rounding(src0->type);
  6393. if (id != 0) {
  6394. row_low[id] = ne01*g_tensor_split[id];
  6395. row_low[id] -= row_low[id] % rounding;
  6396. }
  6397. if (id != g_device_count - 1) {
  6398. row_high[id] = ne01*g_tensor_split[id + 1];
  6399. row_high[id] -= row_high[id] % rounding;
  6400. }
  6401. }
  6402. }
  6403. for (int64_t id = 0; id < g_device_count; ++id) {
  6404. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6405. continue;
  6406. }
  6407. used_devices++;
  6408. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6409. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6410. ggml_cuda_set_device(id);
  6411. const cudaStream_t stream = g_cudaStreams[id][0];
  6412. if (src0_on_device && src0_is_contiguous) {
  6413. src0_dd[id] = (char *) src0_extra->data_device[id];
  6414. } else {
  6415. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  6416. src0_dd[id] = (char *) ggml_cuda_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  6417. }
  6418. if (src1_on_device && src1_is_contiguous) {
  6419. src1_ddf[id] = (float *) src1_extra->data_device[id];
  6420. } else {
  6421. src1_ddf[id] = (float *) ggml_cuda_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  6422. }
  6423. if (convert_src1_to_q8_1) {
  6424. src1_ddq[id] = (char *) ggml_cuda_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  6425. if (src1_on_device && src1_is_contiguous) {
  6426. quantize_row_q8_1_cuda(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  6427. CUDA_CHECK(cudaGetLastError());
  6428. }
  6429. }
  6430. if (dst_on_device) {
  6431. dst_dd[id] = (float *) dst_extra->data_device[id];
  6432. } else {
  6433. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  6434. dst_dd[id] = (float *) ggml_cuda_pool_malloc(size_dst_ddf, &dst_as[id]);
  6435. }
  6436. }
  6437. // if multiple devices are used they need to wait for the main device
  6438. // here an event is recorded that signals that the main device has finished calculating the input data
  6439. if (split && used_devices > 1) {
  6440. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6441. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  6442. }
  6443. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  6444. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  6445. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  6446. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  6447. for (int64_t id = 0; id < g_device_count; ++id) {
  6448. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6449. continue;
  6450. }
  6451. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  6452. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  6453. const int64_t row_diff = row_high[id] - row_low[id];
  6454. ggml_cuda_set_device(id);
  6455. const cudaStream_t stream = g_cudaStreams[id][is];
  6456. // wait for main GPU data if necessary
  6457. if (split && (id != g_main_device || is != 0)) {
  6458. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  6459. }
  6460. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  6461. const int64_t i03 = i0 / ne12;
  6462. const int64_t i02 = i0 % ne12;
  6463. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  6464. // for split tensors the data begins at i0 == i0_offset_low
  6465. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  6466. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  6467. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  6468. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  6469. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  6470. // in that case an offset on dst_ddf_i is needed
  6471. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  6472. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  6473. }
  6474. // copy src0, src1 to device if necessary
  6475. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  6476. if (id != g_main_device) {
  6477. if (convert_src1_to_q8_1) {
  6478. char * src1_ddq_i_source = src1_ddq[g_main_device] + src1_ddq_i_offset;
  6479. CUDA_CHECK(cudaMemcpyAsync(src1_ddq_i, src1_ddq_i_source, src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs,
  6480. cudaMemcpyDeviceToDevice, stream));
  6481. } else {
  6482. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  6483. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  6484. CUDA_CHECK(cudaMemcpyAsync(src1_ddf_i, src1_ddf_i_source, src1_ncols*ne10*sizeof(float),
  6485. cudaMemcpyDeviceToDevice, stream));
  6486. }
  6487. }
  6488. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  6489. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  6490. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  6491. } else {
  6492. GGML_ASSERT(false);
  6493. }
  6494. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  6495. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  6496. CUDA_CHECK(cudaGetLastError());
  6497. }
  6498. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  6499. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  6500. }
  6501. // do the computation
  6502. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  6503. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  6504. CUDA_CHECK(cudaGetLastError());
  6505. // copy dst to host or other device if necessary
  6506. if (!dst_on_device) {
  6507. void * dst_off_device;
  6508. cudaMemcpyKind kind;
  6509. if (dst->backend == GGML_BACKEND_CPU) {
  6510. dst_off_device = dst->data;
  6511. kind = cudaMemcpyDeviceToHost;
  6512. } else if (dst->backend == GGML_BACKEND_GPU) {
  6513. dst_off_device = dst_extra->data_device[g_main_device];
  6514. kind = cudaMemcpyDeviceToDevice;
  6515. } else {
  6516. GGML_ASSERT(false);
  6517. }
  6518. if (split) {
  6519. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  6520. // dst is NOT transposed.
  6521. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  6522. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  6523. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  6524. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6525. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6526. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  6527. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float), dst_dd_i, row_diff*sizeof(float),
  6528. row_diff*sizeof(float), src1_ncols, kind, stream));
  6529. } else {
  6530. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  6531. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  6532. dhf_dst_i += src1_col_0*ne0;
  6533. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  6534. }
  6535. }
  6536. // add event for the main device to wait on until other device is done
  6537. if (split && (id != g_main_device || is != 0)) {
  6538. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  6539. }
  6540. }
  6541. }
  6542. }
  6543. for (int64_t id = 0; id < g_device_count; ++id) {
  6544. if ((!split && id != g_main_device) || row_low[id] == row_high[id]) {
  6545. continue;
  6546. }
  6547. CUDA_CHECK(ggml_cuda_set_device(id));
  6548. // free buffers again when done
  6549. if (src0_as[id] > 0) {
  6550. ggml_cuda_pool_free(src0_dd[id], src0_as[id]);
  6551. }
  6552. if (src1_asf[id] > 0) {
  6553. ggml_cuda_pool_free(src1_ddf[id], src1_asf[id]);
  6554. }
  6555. if (src1_asq[id] > 0) {
  6556. ggml_cuda_pool_free(src1_ddq[id], src1_asq[id]);
  6557. }
  6558. if (dst_as[id] > 0) {
  6559. ggml_cuda_pool_free(dst_dd[id], dst_as[id]);
  6560. }
  6561. }
  6562. // main device waits for all other devices to be finished
  6563. if (split && g_device_count > 1) {
  6564. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  6565. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  6566. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6567. for (int64_t id = 0; id < g_device_count; ++id) {
  6568. if (row_low[id] == row_high[id]) {
  6569. continue;
  6570. }
  6571. for (int64_t is = 0; is < is_max; ++is) {
  6572. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  6573. }
  6574. }
  6575. }
  6576. if (dst->backend == GGML_BACKEND_CPU) {
  6577. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6578. CUDA_CHECK(cudaDeviceSynchronize());
  6579. }
  6580. }
  6581. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6582. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  6583. }
  6584. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6585. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  6586. }
  6587. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6588. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  6589. }
  6590. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6591. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  6592. }
  6593. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6594. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  6595. }
  6596. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6597. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  6598. }
  6599. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6600. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  6601. }
  6602. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6603. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  6604. }
  6605. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6606. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  6607. }
  6608. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6609. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  6610. }
  6611. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6612. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  6613. }
  6614. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6615. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  6616. }
  6617. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6618. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  6619. }
  6620. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6621. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  6622. }
  6623. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6624. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  6625. }
  6626. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6627. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  6628. }
  6629. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6630. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  6631. }
  6632. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6633. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  6634. }
  6635. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6636. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  6637. }
  6638. bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  6639. if (!g_cublas_loaded) return false;
  6640. const int64_t ne10 = src1->ne[0];
  6641. const int64_t ne0 = dst->ne[0];
  6642. const int64_t ne1 = dst->ne[1];
  6643. // TODO: find the optimal values for these
  6644. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  6645. src1->type == GGML_TYPE_F32 &&
  6646. dst->type == GGML_TYPE_F32 &&
  6647. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  6648. }
  6649. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6650. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  6651. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6652. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  6653. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  6654. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6655. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6656. const int64_t ne00 = src0->ne[0];
  6657. const int64_t ne01 = src0->ne[1];
  6658. const int64_t ne02 = src0->ne[2];
  6659. const int64_t ne12 = src1->ne[2];
  6660. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6661. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6662. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6663. void * src0_ddq = src0_extra->data_device[g_main_device];
  6664. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6665. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6666. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6667. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6668. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  6669. }
  6670. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  6671. GGML_ASSERT(!ggml_is_transposed(src0));
  6672. GGML_ASSERT(!ggml_is_transposed(src1));
  6673. GGML_ASSERT(!ggml_is_permuted(src0));
  6674. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6675. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6676. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6677. const int64_t ne00 = src0->ne[0];
  6678. const int64_t ne01 = src0->ne[1];
  6679. const int64_t ne02 = src0->ne[2];
  6680. const int64_t nb01 = src0->nb[1];
  6681. const int64_t nb02 = src0->nb[2];
  6682. const int64_t ne12 = src1->ne[2];
  6683. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6684. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6685. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6686. void * src0_ddq = src0_extra->data_device[g_main_device];
  6687. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6688. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6689. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6690. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6691. const int64_t row_stride_x = nb01 / sizeof(half);
  6692. const int64_t channel_stride_x = nb02 / sizeof(half);
  6693. 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);
  6694. }
  6695. static __global__ void k_compute_batched_ptrs(
  6696. const half * src0_as_f16, const half * src1_as_f16, half * dst_f16,
  6697. const void ** ptrs_src, void ** ptrs_dst,
  6698. int ne12, int ne13,
  6699. int ne23,
  6700. int nb02, int nb03,
  6701. int nb12, int nb13,
  6702. int nb2, int nb3,
  6703. int r2, int r3) {
  6704. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6705. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6706. if (i13 >= ne13 || i12 >= ne12) {
  6707. return;
  6708. }
  6709. int i03 = i13 / r3;
  6710. int i02 = i12 / r2;
  6711. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  6712. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  6713. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  6714. }
  6715. static void ggml_cuda_mul_mat_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6716. GGML_ASSERT(!ggml_is_transposed(src0));
  6717. GGML_ASSERT(!ggml_is_transposed(src1));
  6718. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  6719. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  6720. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6721. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  6722. const int64_t ne01 = src0->ne[1];
  6723. const int64_t ne02 = src0->ne[2];
  6724. const int64_t ne03 = src0->ne[3];
  6725. const int64_t nb01 = src0->nb[1];
  6726. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  6727. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  6728. const int64_t ne10 = src1->ne[0];
  6729. const int64_t ne11 = src1->ne[1];
  6730. const int64_t ne12 = src1->ne[2];
  6731. const int64_t ne13 = src1->ne[3];
  6732. const int64_t nb11 = src1->nb[1];
  6733. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6734. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6735. const int64_t ne1 = ggml_nelements(src1);
  6736. const int64_t ne = ggml_nelements(dst);
  6737. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6738. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6739. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6740. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6741. void * src0_ddq = src0_extra->data_device[g_main_device];
  6742. half * src0_as_f16 = (half *) src0_ddq;
  6743. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6744. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6745. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6746. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6747. // convert src1 to fp16
  6748. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6749. GGML_ASSERT(to_fp16_cuda != nullptr);
  6750. size_t src1_as = 0;
  6751. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  6752. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  6753. size_t dst_as = 0;
  6754. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  6755. GGML_ASSERT(ne12 % ne02 == 0);
  6756. GGML_ASSERT(ne13 % ne03 == 0);
  6757. // broadcast factors
  6758. const int64_t r2 = ne12/ne02;
  6759. const int64_t r3 = ne13/ne03;
  6760. const half alpha_f16 = 1.0f;
  6761. const half beta_f16 = 0.0f;
  6762. #if 0
  6763. // use cublasGemmEx
  6764. {
  6765. for (int i13 = 0; i13 < ne13; ++i13) {
  6766. for (int i12 = 0; i12 < ne12; ++i12) {
  6767. int i03 = i13 / r3;
  6768. int i02 = i12 / r2;
  6769. CUBLAS_CHECK(
  6770. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  6771. ne01, ne11, ne10,
  6772. &alpha_f16, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  6773. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  6774. &beta_f16, ( char *) dst_f16 + i12* dst->nb[2]/2 + i13* dst->nb[3]/2, CUDA_R_16F, ne01,
  6775. CUBLAS_COMPUTE_16F,
  6776. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6777. }
  6778. }
  6779. }
  6780. #else
  6781. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  6782. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  6783. // use cublasGemmStridedBatchedEx
  6784. CUBLAS_CHECK(
  6785. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6786. ne01, ne11, ne10,
  6787. &alpha_f16, (const char *) src0_as_f16, CUDA_R_16F, nb01/sizeof(half), src0->nb[2]/sizeof(half), // strideA
  6788. (const char *) src1_as_f16, CUDA_R_16F, nb11/sizeof(float), src1->nb[2]/sizeof(float), // strideB
  6789. &beta_f16, ( char *) dst_f16, CUDA_R_16F, ne01, dst->nb[2]/sizeof(float), // strideC
  6790. ne12*ne13,
  6791. CUBLAS_COMPUTE_16F,
  6792. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6793. } else {
  6794. // use cublasGemmBatchedEx
  6795. const int ne23 = ne12*ne13;
  6796. const void ** ptrs_src = nullptr;
  6797. void ** ptrs_dst = nullptr;
  6798. size_t ptrs_src_s = 0;
  6799. size_t ptrs_dst_s = 0;
  6800. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  6801. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  6802. dim3 block_dims(ne13, ne12);
  6803. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  6804. src0_as_f16, src1_as_f16, dst_f16,
  6805. ptrs_src, ptrs_dst,
  6806. ne12, ne13,
  6807. ne23,
  6808. nb02, nb03,
  6809. nb12, nb13,
  6810. dst->nb[2], dst->nb[3],
  6811. r2, r3);
  6812. CUDA_CHECK(cudaGetLastError());
  6813. CUBLAS_CHECK(
  6814. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  6815. ne01, ne11, ne10,
  6816. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, nb01/sizeof(half),
  6817. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, nb11/sizeof(float),
  6818. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  6819. ne23,
  6820. CUBLAS_COMPUTE_16F,
  6821. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  6822. if (ptrs_src_s != 0) {
  6823. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  6824. }
  6825. if (ptrs_dst_s != 0) {
  6826. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  6827. }
  6828. }
  6829. #endif
  6830. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  6831. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  6832. ggml_cuda_pool_free(src1_as_f16, src1_as);
  6833. ggml_cuda_pool_free(dst_f16, dst_as);
  6834. }
  6835. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  6836. const bool all_on_device =
  6837. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  6838. (src1->backend == GGML_BACKEND_GPU) &&
  6839. ( dst->backend == GGML_BACKEND_GPU);
  6840. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  6841. int64_t min_compute_capability = INT_MAX;
  6842. for (int64_t id = 0; id < g_device_count; ++id) {
  6843. if (min_compute_capability > g_compute_capabilities[id] && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  6844. min_compute_capability = g_compute_capabilities[id];
  6845. }
  6846. }
  6847. #ifdef CUDA_USE_TENSOR_CORES
  6848. const bool use_tensor_cores = true;
  6849. #else
  6850. const bool use_tensor_cores = false;
  6851. #endif
  6852. // debug helpers
  6853. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  6854. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  6855. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  6856. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  6857. //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);
  6858. //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);
  6859. if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  6860. // KQ single-batch
  6861. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  6862. } else if (!split && all_on_device && !use_tensor_cores && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  6863. // KQV single-batch
  6864. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  6865. } else if (!split && all_on_device && use_tensor_cores && src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  6866. // KQ + KQV multi-batch
  6867. ggml_cuda_mul_mat_mat_batched_cublas(src0, src1, dst);
  6868. } else if (src0->type == GGML_TYPE_F32) {
  6869. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6870. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  6871. if (src1->ne[1] == 1 && src0->ne[0] % GGML_CUDA_DMMV_X == 0) {
  6872. #ifdef GGML_CUDA_FORCE_DMMV
  6873. const bool use_mul_mat_vec_q = false;
  6874. #else
  6875. const bool use_mul_mat_vec_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  6876. #endif // GGML_CUDA_FORCE_DMMV
  6877. if (use_mul_mat_vec_q) {
  6878. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  6879. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  6880. } else {
  6881. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  6882. }
  6883. } else {
  6884. bool use_mul_mat_q = min_compute_capability >= MIN_CC_DP4A && ggml_is_quantized(src0->type);
  6885. // when tensor cores are available, use them for large batch size
  6886. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  6887. if (use_tensor_cores && min_compute_capability >= CC_VOLTA && src1->ne[1] > MMQ_MAX_BATCH_SIZE) {
  6888. use_mul_mat_q = false;
  6889. }
  6890. if (use_mul_mat_q) {
  6891. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  6892. } else {
  6893. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  6894. }
  6895. }
  6896. } else {
  6897. GGML_ASSERT(false);
  6898. }
  6899. }
  6900. #if 0
  6901. template<typename ... Srcs>
  6902. static __global__ void k_compute_batched_ptrs_id(
  6903. const void ** ptrs_src, void ** ptrs_dst,
  6904. int ne12, int ne13,
  6905. int ne23,
  6906. int nb02, int nb03,
  6907. int nb12, int nb13,
  6908. int nb2, int nb3,
  6909. int r2, int r3,
  6910. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  6911. const half * src1_f16, half * dst_f16,
  6912. const int32_t * ids, const int id,
  6913. Srcs... src0s) {
  6914. int i = ids[id];
  6915. half * src0_f16;
  6916. const void * srcs_ar[] = { (const half *) src0s... };
  6917. if (src0_type == GGML_TYPE_F16) {
  6918. src0_f16 = (half *) srcs_ar[i];
  6919. } else {
  6920. src0_f16 = src0_as_f16;
  6921. if (threadIdx.x == 0 && threadIdx.y == 0) {
  6922. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  6923. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  6924. }
  6925. }
  6926. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  6927. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  6928. if (i13 >= ne13 || i12 >= ne12) {
  6929. return;
  6930. }
  6931. int i03 = i13 / r3;
  6932. int i02 = i12 / r2;
  6933. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  6934. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  6935. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  6936. }
  6937. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  6938. const struct ggml_tensor * ids = dst->src[0];
  6939. const struct ggml_tensor * src1 = dst->src[1];
  6940. const struct ggml_tensor * src00 = dst->src[2];
  6941. const int id = dst->op_params[0];
  6942. GGML_ASSERT(!ggml_is_transposed(src00));
  6943. GGML_ASSERT(!ggml_is_transposed(src1));
  6944. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  6945. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6946. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  6947. const int64_t ne01 = src00->ne[1];
  6948. const int64_t ne02 = src00->ne[2];
  6949. const int64_t ne03 = src00->ne[3];
  6950. //const int64_t nb01 = src00->nb[1];
  6951. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  6952. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  6953. const int64_t ne10 = src1->ne[0];
  6954. const int64_t ne11 = src1->ne[1];
  6955. const int64_t ne12 = src1->ne[2];
  6956. const int64_t ne13 = src1->ne[3];
  6957. //const int64_t nb11 = src1->nb[1];
  6958. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  6959. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  6960. const int64_t ne1 = ggml_nelements(src1);
  6961. const int64_t ne = ggml_nelements(dst);
  6962. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  6963. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  6964. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  6965. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  6966. //void * src0_ddq = src0_extra->data_device[g_main_device];
  6967. //half * src0_as_f16 = (half *) src0_ddq;
  6968. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  6969. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  6970. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  6971. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  6972. // convert src1 to fp16
  6973. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  6974. GGML_ASSERT(to_fp16_cuda != nullptr);
  6975. size_t src1_as = 0;
  6976. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  6977. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  6978. size_t dst_as = 0;
  6979. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  6980. GGML_ASSERT(ne12 % ne02 == 0);
  6981. GGML_ASSERT(ne13 % ne03 == 0);
  6982. // broadcast factors
  6983. const int64_t r2 = ne12/ne02;
  6984. const int64_t r3 = ne13/ne03;
  6985. const half alpha_f16 = 1.0f;
  6986. const half beta_f16 = 0.0f;
  6987. // use cublasGemmBatchedEx
  6988. const int ne23 = ne12*ne13;
  6989. const void ** ptrs_src = nullptr;
  6990. void ** ptrs_dst = nullptr;
  6991. size_t ptrs_src_s = 0;
  6992. size_t ptrs_dst_s = 0;
  6993. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  6994. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  6995. int64_t src0_ne = ggml_nelements(src00);
  6996. half * src0_as_f16 = nullptr;
  6997. size_t src0_as = 0;
  6998. if (src00->type != GGML_TYPE_F16) {
  6999. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  7000. }
  7001. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  7002. dim3 block_dims(ne13, ne12);
  7003. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  7004. ptrs_src, ptrs_dst,
  7005. ne12, ne13,
  7006. ne23,
  7007. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  7008. nb12, nb13,
  7009. dst->nb[2], dst->nb[3],
  7010. r2, r3,
  7011. src00->type, src0_as_f16, src0_ne,
  7012. src1_as_f16, dst_f16,
  7013. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  7014. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  7015. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  7016. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  7017. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  7018. );
  7019. CUDA_CHECK(cudaGetLastError());
  7020. CUBLAS_CHECK(
  7021. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  7022. ne01, ne11, ne10,
  7023. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  7024. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  7025. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  7026. ne23,
  7027. CUBLAS_COMPUTE_16F,
  7028. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7029. if (src0_as != 0) {
  7030. ggml_cuda_pool_free(src0_as_f16, src0_as);
  7031. }
  7032. if (ptrs_src_s != 0) {
  7033. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  7034. }
  7035. if (ptrs_dst_s != 0) {
  7036. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  7037. }
  7038. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7039. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  7040. ggml_cuda_pool_free(src1_as_f16, src1_as);
  7041. ggml_cuda_pool_free(dst_f16, dst_as);
  7042. }
  7043. #endif
  7044. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7045. #if 0
  7046. ggml_cuda_mul_mat_id_cublas(dst);
  7047. // TODO: mmq/mmv support
  7048. #endif
  7049. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  7050. const struct ggml_tensor * ids = src0;
  7051. const int32_t id = ((int32_t *) dst->op_params)[0];
  7052. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  7053. std::vector<char> ids_host(ggml_nbytes(ids));
  7054. if (ids->backend == GGML_BACKEND_GPU) {
  7055. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  7056. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  7057. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7058. } else {
  7059. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  7060. }
  7061. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  7062. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  7063. ggml_tensor_extra_gpu src1_row_extra;
  7064. ggml_tensor_extra_gpu dst_row_extra;
  7065. ggml_tensor src1_row = *src1;
  7066. ggml_tensor dst_row = *dst;
  7067. src1_row.ne[1] = 1;
  7068. dst_row.ne[1] = 1;
  7069. src1_row.nb[2] = src1_row.nb[1];
  7070. dst_row.nb[2] = dst_row.nb[1];
  7071. src1_row.nb[3] = src1_row.nb[1];
  7072. dst_row.nb[3] = dst_row.nb[1];
  7073. src1_row.extra = &src1_row_extra;
  7074. dst_row.extra = &dst_row_extra;
  7075. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  7076. //int32_t row_id;
  7077. //CUDA_CHECK(cudaMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), cudaMemcpyDeviceToHost, g_cudaStreams[g_main_device][0]));
  7078. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  7079. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  7080. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  7081. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  7082. src1_row_extra.data_device[g_main_device] = (char *) src1_extra->data_device[g_main_device] + i01*src1->nb[1];
  7083. src1_row.data = (char *) src1->data + i01*src1->nb[1];
  7084. dst_row_extra.data_device[g_main_device] = (char *) dst_extra->data_device[g_main_device] + i01*dst->nb[1];
  7085. dst_row.data = (char *) dst->data + i01*dst->nb[1];
  7086. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  7087. }
  7088. }
  7089. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7090. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  7091. }
  7092. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7093. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  7094. }
  7095. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7096. const int64_t ne = ggml_nelements(src0);
  7097. GGML_ASSERT(ne == ggml_nelements(src1));
  7098. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  7099. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  7100. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  7101. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  7102. const int64_t ne00 = src0->ne[0];
  7103. const int64_t ne01 = src0->ne[1];
  7104. GGML_ASSERT(src0->ne[3] == 1);
  7105. const int64_t nb00 = src0->nb[0];
  7106. const int64_t nb01 = src0->nb[1];
  7107. const int64_t nb02 = src0->nb[2];
  7108. const int64_t ne10 = src1->ne[0];
  7109. const int64_t ne11 = src1->ne[1];
  7110. GGML_ASSERT(src1->ne[3] == 1);
  7111. const int64_t nb10 = src1->nb[0];
  7112. const int64_t nb11 = src1->nb[1];
  7113. const int64_t nb12 = src1->nb[2];
  7114. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7115. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7116. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7117. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7118. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7119. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  7120. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  7121. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7122. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  7123. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7124. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  7125. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7126. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  7127. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7128. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  7129. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7130. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  7131. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  7132. } else {
  7133. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  7134. ggml_type_name(src0->type), ggml_type_name(src1->type));
  7135. GGML_ASSERT(false);
  7136. }
  7137. (void) dst;
  7138. }
  7139. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7140. // TODO: why do we pass dst as src1 here?
  7141. ggml_cuda_cpy(src0, dst, nullptr);
  7142. (void) src1;
  7143. }
  7144. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7145. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  7146. }
  7147. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7148. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  7149. }
  7150. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7151. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  7152. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  7153. }
  7154. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7155. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  7156. }
  7157. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7158. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  7159. }
  7160. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7161. GGML_ASSERT(ggml_is_contiguous(src0));
  7162. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  7163. }
  7164. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7165. GGML_ASSERT(ggml_is_contiguous(src0));
  7166. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  7167. }
  7168. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7169. (void) src0;
  7170. (void) src1;
  7171. (void) dst;
  7172. }
  7173. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  7174. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  7175. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  7176. }
  7177. void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor) {
  7178. const int64_t nrows = ggml_nrows(tensor);
  7179. const int64_t ne0 = tensor->ne[0];
  7180. const size_t nb1 = tensor->nb[1];
  7181. ggml_backend_type backend = tensor->backend;
  7182. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  7183. memset(extra, 0, sizeof(*extra));
  7184. for (int64_t id = 0; id < g_device_count; ++id) {
  7185. if (backend == GGML_BACKEND_GPU && id != g_main_device) {
  7186. continue;
  7187. }
  7188. ggml_cuda_set_device(id);
  7189. int64_t row_low, row_high;
  7190. if (backend == GGML_BACKEND_GPU) {
  7191. row_low = 0;
  7192. row_high = nrows;
  7193. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  7194. const int64_t rounding = get_row_rounding(tensor->type);
  7195. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  7196. row_low -= row_low % rounding;
  7197. if (id == g_device_count - 1) {
  7198. row_high = nrows;
  7199. } else {
  7200. row_high = nrows*g_tensor_split[id + 1];
  7201. row_high -= row_high % rounding;
  7202. }
  7203. } else {
  7204. GGML_ASSERT(false);
  7205. }
  7206. if (row_low == row_high) {
  7207. continue;
  7208. }
  7209. int64_t nrows_split = row_high - row_low;
  7210. const size_t offset_split = row_low*nb1;
  7211. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7212. const size_t original_size = size;
  7213. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  7214. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7215. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7216. }
  7217. char * buf;
  7218. CUDA_CHECK(cudaMalloc(&buf, size));
  7219. char * buf_host = (char*)data + offset_split;
  7220. // set padding to 0 to avoid possible NaN values
  7221. if (size > original_size) {
  7222. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  7223. }
  7224. CUDA_CHECK(cudaMemcpy(buf, buf_host, original_size, cudaMemcpyHostToDevice));
  7225. extra->data_device[id] = buf;
  7226. if (backend == GGML_BACKEND_GPU_SPLIT) {
  7227. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7228. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  7229. }
  7230. }
  7231. }
  7232. tensor->extra = extra;
  7233. }
  7234. void ggml_cuda_free_data(struct ggml_tensor * tensor) {
  7235. if (!tensor || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  7236. return;
  7237. }
  7238. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7239. for (int64_t id = 0; id < g_device_count; ++id) {
  7240. if (extra->data_device[id] != nullptr) {
  7241. CUDA_CHECK(ggml_cuda_set_device(id));
  7242. CUDA_CHECK(cudaFree(extra->data_device[id]));
  7243. }
  7244. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  7245. if (extra->events[id][is] != nullptr) {
  7246. CUDA_CHECK(ggml_cuda_set_device(id));
  7247. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  7248. }
  7249. }
  7250. }
  7251. delete extra;
  7252. }
  7253. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  7254. static size_t g_temp_tensor_extra_index = 0;
  7255. static ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7256. if (g_temp_tensor_extras == nullptr) {
  7257. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7258. }
  7259. size_t alloc_index = g_temp_tensor_extra_index;
  7260. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7261. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  7262. memset(extra, 0, sizeof(*extra));
  7263. return extra;
  7264. }
  7265. static void ggml_cuda_assign_buffers_impl(struct ggml_tensor * tensor, bool scratch, bool force_inplace, bool no_alloc) {
  7266. if (scratch && g_scratch_size == 0) {
  7267. return;
  7268. }
  7269. tensor->backend = GGML_BACKEND_GPU;
  7270. // recursively assign CUDA buffers until a compute tensor is found
  7271. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  7272. const ggml_op src0_op = tensor->src[0]->op;
  7273. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  7274. ggml_cuda_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  7275. }
  7276. }
  7277. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  7278. ggml_cuda_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  7279. }
  7280. if (scratch && no_alloc) {
  7281. return;
  7282. }
  7283. ggml_tensor_extra_gpu * extra;
  7284. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7285. tensor->op == GGML_OP_VIEW ||
  7286. force_inplace;
  7287. const size_t size = ggml_nbytes(tensor);
  7288. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7289. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7290. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7291. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7292. size_t offset = 0;
  7293. if (tensor->op == GGML_OP_VIEW) {
  7294. memcpy(&offset, tensor->op_params, sizeof(size_t));
  7295. }
  7296. extra = ggml_cuda_alloc_temp_tensor_extra();
  7297. extra->data_device[g_main_device] = src0_ddc + offset;
  7298. } else if (tensor->op == GGML_OP_CPY) {
  7299. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  7300. void * src1_ddv = src1_extra->data_device[g_main_device];
  7301. extra = ggml_cuda_alloc_temp_tensor_extra();
  7302. extra->data_device[g_main_device] = src1_ddv;
  7303. } else if (scratch) {
  7304. GGML_ASSERT(size <= g_scratch_size);
  7305. if (g_scratch_offset + size > g_scratch_size) {
  7306. g_scratch_offset = 0;
  7307. }
  7308. char * data = (char *) g_scratch_buffer;
  7309. if (data == nullptr) {
  7310. CUDA_CHECK(cudaMalloc(&data, g_scratch_size));
  7311. g_scratch_buffer = data;
  7312. }
  7313. extra = ggml_cuda_alloc_temp_tensor_extra();
  7314. extra->data_device[g_main_device] = data + g_scratch_offset;
  7315. g_scratch_offset += size;
  7316. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  7317. } else { // allocate new buffers outside of scratch
  7318. void * data;
  7319. CUDA_CHECK(cudaMalloc(&data, size));
  7320. CUDA_CHECK(cudaMemset(data, 0, size));
  7321. extra = new ggml_tensor_extra_gpu;
  7322. memset(extra, 0, sizeof(*extra));
  7323. extra->data_device[g_main_device] = data;
  7324. }
  7325. tensor->extra = extra;
  7326. }
  7327. void ggml_cuda_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset) {
  7328. if (g_scratch_size == 0) {
  7329. return;
  7330. }
  7331. if (g_scratch_buffer == nullptr) {
  7332. ggml_cuda_set_device(g_main_device);
  7333. CUDA_CHECK(cudaMalloc(&g_scratch_buffer, g_scratch_size));
  7334. }
  7335. ggml_tensor_extra_gpu * extra = ggml_cuda_alloc_temp_tensor_extra();
  7336. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  7337. tensor->op == GGML_OP_VIEW;
  7338. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  7339. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  7340. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  7341. size_t view_offset = 0;
  7342. if (tensor->op == GGML_OP_VIEW) {
  7343. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  7344. }
  7345. extra->data_device[g_main_device] = src0_ddc + view_offset;
  7346. } else {
  7347. extra->data_device[g_main_device] = (char *) g_scratch_buffer + offset;
  7348. }
  7349. tensor->extra = extra;
  7350. }
  7351. void ggml_cuda_copy_to_device(struct ggml_tensor * tensor) {
  7352. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7353. GGML_ASSERT(ggml_is_contiguous(tensor));
  7354. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  7355. CUDA_CHECK(ggml_cuda_set_device(g_main_device));
  7356. CUDA_CHECK(cudaMemcpy(extra->data_device[g_main_device], tensor->data, ggml_nbytes(tensor), cudaMemcpyHostToDevice));
  7357. }
  7358. void ggml_cuda_assign_buffers(struct ggml_tensor * tensor) {
  7359. ggml_cuda_assign_buffers_impl(tensor, true, false, false);
  7360. }
  7361. void ggml_cuda_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  7362. ggml_cuda_assign_buffers_impl(tensor, true, false, true);
  7363. }
  7364. void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  7365. ggml_cuda_assign_buffers_impl(tensor, false, false, false);
  7366. }
  7367. void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  7368. ggml_cuda_assign_buffers_impl(tensor, false, true, false);
  7369. }
  7370. void ggml_cuda_set_main_device(const int main_device) {
  7371. if (main_device >= g_device_count) {
  7372. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  7373. main_device, g_device_count, g_main_device);
  7374. return;
  7375. }
  7376. if (g_main_device != main_device && g_device_count > 1) {
  7377. g_main_device = main_device;
  7378. cudaDeviceProp prop;
  7379. CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  7380. fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  7381. }
  7382. }
  7383. void ggml_cuda_set_scratch_size(const size_t scratch_size) {
  7384. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  7385. // it still won't always work as expected, but it's better than nothing
  7386. if (scratch_size > g_scratch_size) {
  7387. ggml_cuda_free_scratch();
  7388. }
  7389. g_scratch_size = std::max(g_scratch_size, scratch_size);
  7390. }
  7391. void ggml_cuda_free_scratch() {
  7392. if (g_scratch_buffer == nullptr) {
  7393. return;
  7394. }
  7395. CUDA_CHECK(cudaFree(g_scratch_buffer));
  7396. g_scratch_buffer = nullptr;
  7397. }
  7398. bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  7399. if (!g_cublas_loaded) return false;
  7400. ggml_cuda_func_t func;
  7401. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  7402. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  7403. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  7404. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT) {
  7405. return false;
  7406. }
  7407. if (tensor->op == GGML_OP_MUL_MAT) {
  7408. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  7409. #ifndef NDEBUG
  7410. fprintf(stderr, "%s: cannot compute %s: src0->ne[3] = " PRId64 ", src1->ne[3] = " PRId64 " - fallback to CPU\n", __func__, tensor->name, tensor->src[0]->ne[3], tensor->src[1]->ne[3]);
  7411. #endif
  7412. return false;
  7413. }
  7414. }
  7415. switch (tensor->op) {
  7416. case GGML_OP_REPEAT:
  7417. func = ggml_cuda_repeat;
  7418. break;
  7419. case GGML_OP_GET_ROWS:
  7420. func = ggml_cuda_get_rows;
  7421. break;
  7422. case GGML_OP_DUP:
  7423. func = ggml_cuda_dup;
  7424. break;
  7425. case GGML_OP_ADD:
  7426. func = ggml_cuda_add;
  7427. break;
  7428. case GGML_OP_ACC:
  7429. func = ggml_cuda_acc;
  7430. break;
  7431. case GGML_OP_MUL:
  7432. func = ggml_cuda_mul;
  7433. break;
  7434. case GGML_OP_DIV:
  7435. func = ggml_cuda_div;
  7436. break;
  7437. case GGML_OP_UNARY:
  7438. switch (ggml_get_unary_op(tensor)) {
  7439. case GGML_UNARY_OP_GELU:
  7440. func = ggml_cuda_gelu;
  7441. break;
  7442. case GGML_UNARY_OP_SILU:
  7443. func = ggml_cuda_silu;
  7444. break;
  7445. case GGML_UNARY_OP_GELU_QUICK:
  7446. func = ggml_cuda_gelu_quick;
  7447. break;
  7448. case GGML_UNARY_OP_TANH:
  7449. func = ggml_cuda_tanh;
  7450. break;
  7451. case GGML_UNARY_OP_RELU:
  7452. func = ggml_cuda_relu;
  7453. break;
  7454. default:
  7455. return false;
  7456. }
  7457. break;
  7458. case GGML_OP_NORM:
  7459. func = ggml_cuda_norm;
  7460. break;
  7461. case GGML_OP_GROUP_NORM:
  7462. func = ggml_cuda_group_norm;
  7463. break;
  7464. case GGML_OP_CONCAT:
  7465. func = ggml_cuda_concat;
  7466. break;
  7467. case GGML_OP_UPSCALE:
  7468. func = ggml_cuda_upscale;
  7469. break;
  7470. case GGML_OP_PAD:
  7471. func = ggml_cuda_pad;
  7472. break;
  7473. case GGML_OP_LEAKY_RELU:
  7474. func = ggml_cuda_leaky_relu;
  7475. break;
  7476. case GGML_OP_RMS_NORM:
  7477. func = ggml_cuda_rms_norm;
  7478. break;
  7479. case GGML_OP_MUL_MAT:
  7480. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  7481. return false;
  7482. }
  7483. func = ggml_cuda_mul_mat;
  7484. break;
  7485. case GGML_OP_MUL_MAT_ID:
  7486. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  7487. return false;
  7488. }
  7489. func = ggml_cuda_mul_mat_id;
  7490. break;
  7491. case GGML_OP_SCALE:
  7492. func = ggml_cuda_scale;
  7493. break;
  7494. case GGML_OP_SQR:
  7495. func = ggml_cuda_sqr;
  7496. break;
  7497. case GGML_OP_CLAMP:
  7498. func = ggml_cuda_clamp;
  7499. break;
  7500. case GGML_OP_CPY:
  7501. func = ggml_cuda_cpy;
  7502. break;
  7503. case GGML_OP_CONT:
  7504. func = ggml_cuda_dup;
  7505. break;
  7506. case GGML_OP_NONE:
  7507. case GGML_OP_RESHAPE:
  7508. case GGML_OP_VIEW:
  7509. case GGML_OP_PERMUTE:
  7510. case GGML_OP_TRANSPOSE:
  7511. func = ggml_cuda_nop;
  7512. break;
  7513. case GGML_OP_DIAG_MASK_INF:
  7514. func = ggml_cuda_diag_mask_inf;
  7515. break;
  7516. case GGML_OP_SOFT_MAX:
  7517. func = ggml_cuda_soft_max;
  7518. break;
  7519. case GGML_OP_ROPE:
  7520. func = ggml_cuda_rope;
  7521. break;
  7522. case GGML_OP_ALIBI:
  7523. func = ggml_cuda_alibi;
  7524. break;
  7525. case GGML_OP_IM2COL:
  7526. func = ggml_cuda_im2col;
  7527. break;
  7528. case GGML_OP_SUM_ROWS:
  7529. func = ggml_cuda_sum_rows;
  7530. break;
  7531. case GGML_OP_ARGSORT:
  7532. func = ggml_cuda_argsort;
  7533. break;
  7534. default:
  7535. return false;
  7536. }
  7537. if (params->ith != 0) {
  7538. return true;
  7539. }
  7540. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  7541. return true;
  7542. }
  7543. func(tensor->src[0], tensor->src[1], tensor);
  7544. return true;
  7545. }
  7546. int ggml_cuda_get_device_count() {
  7547. int device_count;
  7548. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  7549. return 0;
  7550. }
  7551. return device_count;
  7552. }
  7553. void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  7554. cudaDeviceProp prop;
  7555. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  7556. snprintf(description, description_size, "%s", prop.name);
  7557. }
  7558. ////////////////////////////////////////////////////////////////////////////////
  7559. // backend interface
  7560. #define UNUSED GGML_UNUSED
  7561. // cuda buffer
  7562. struct ggml_backend_buffer_context_cuda {
  7563. int device;
  7564. void * dev_ptr = nullptr;
  7565. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  7566. size_t temp_tensor_extra_index = 0;
  7567. ggml_backend_buffer_context_cuda(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  7568. ~ggml_backend_buffer_context_cuda() {
  7569. delete[] temp_tensor_extras;
  7570. }
  7571. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  7572. if (temp_tensor_extras == nullptr) {
  7573. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  7574. }
  7575. size_t alloc_index = temp_tensor_extra_index;
  7576. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  7577. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  7578. memset(extra, 0, sizeof(*extra));
  7579. return extra;
  7580. }
  7581. };
  7582. static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7583. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7584. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  7585. delete ctx;
  7586. }
  7587. static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  7588. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7589. return ctx->dev_ptr;
  7590. }
  7591. static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  7592. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7593. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  7594. assert(tensor->view_src->buffer->buft == buffer->buft); // TODO
  7595. tensor->backend = tensor->view_src->backend;
  7596. tensor->extra = tensor->view_src->extra;
  7597. return;
  7598. }
  7599. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  7600. extra->data_device[ctx->device] = tensor->data;
  7601. tensor->backend = GGML_BACKEND_GPU;
  7602. tensor->extra = extra;
  7603. if (ggml_is_quantized(tensor->type)) {
  7604. // initialize padding to 0 to avoid possible NaN values
  7605. int64_t row_low = 0;
  7606. int64_t row_high = ggml_nrows(tensor);
  7607. int64_t nrows_split = row_high - row_low;
  7608. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  7609. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  7610. if (padded_size > original_size && tensor->view_src == nullptr) {
  7611. CUDA_CHECK(cudaMemsetAsync((char *)tensor->data + original_size, 0, padded_size - original_size, g_cudaStreams[ctx->device][0]));
  7612. }
  7613. }
  7614. UNUSED(buffer);
  7615. }
  7616. static void ggml_backend_cuda_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7617. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  7618. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  7619. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7620. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  7621. UNUSED(buffer);
  7622. }
  7623. static void ggml_backend_cuda_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7624. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  7625. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  7626. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7627. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  7628. UNUSED(buffer);
  7629. }
  7630. static struct ggml_backend_buffer_i cuda_backend_buffer_interface = {
  7631. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  7632. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  7633. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  7634. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  7635. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  7636. /* .cpy_tensor_from = */ NULL,
  7637. /* .cpy_tensor_to = */ NULL,
  7638. };
  7639. // cuda buffer type
  7640. static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7641. int device = (int) (intptr_t) buft->context;
  7642. ggml_cuda_set_device(device);
  7643. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  7644. void * dev_ptr;
  7645. CUDA_CHECK(cudaMalloc(&dev_ptr, size));
  7646. ggml_backend_buffer_context_cuda * ctx = new ggml_backend_buffer_context_cuda(device, dev_ptr);
  7647. return ggml_backend_buffer_init(buft, cuda_backend_buffer_interface, ctx, size);
  7648. }
  7649. static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  7650. return 128;
  7651. UNUSED(buft);
  7652. }
  7653. static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, ggml_tensor * tensor) {
  7654. int64_t row_low = 0;
  7655. int64_t row_high = ggml_nrows(tensor);
  7656. int64_t nrows_split = row_high - row_low;
  7657. size_t size = ggml_nbytes_split(tensor, nrows_split);
  7658. int64_t ne0 = tensor->ne[0];
  7659. if (ggml_is_quantized(tensor->type)) {
  7660. if (ne0 % MATRIX_ROW_PADDING != 0) {
  7661. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  7662. }
  7663. }
  7664. return size;
  7665. UNUSED(buft);
  7666. }
  7667. static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  7668. return ggml_backend_is_cuda(backend);
  7669. UNUSED(buft);
  7670. }
  7671. static ggml_backend_buffer_type_i cuda_backend_buffer_type_interface = {
  7672. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  7673. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  7674. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  7675. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  7676. };
  7677. ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  7678. static struct ggml_backend_buffer_type ggml_backend_buffer_type_cuda[GGML_CUDA_MAX_DEVICES];
  7679. static bool ggml_backend_buffer_type_cuda_initialized = false;
  7680. if (!ggml_backend_buffer_type_cuda_initialized) {
  7681. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  7682. ggml_backend_buffer_type_cuda[i] = {
  7683. /* .iface = */ cuda_backend_buffer_type_interface,
  7684. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  7685. };
  7686. }
  7687. ggml_backend_buffer_type_cuda_initialized = true;
  7688. }
  7689. return &ggml_backend_buffer_type_cuda[device];
  7690. }
  7691. // host buffer type
  7692. static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  7693. ggml_backend_buffer_context_cuda * ctx = (ggml_backend_buffer_context_cuda *)buffer->context;
  7694. CUDA_CHECK(cudaFreeHost(ctx->dev_ptr));
  7695. delete ctx;
  7696. }
  7697. static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  7698. void * ptr;
  7699. CUDA_CHECK(cudaMallocHost(&ptr, size));
  7700. // FIXME: this is a hack to avoid having to implement a new buffer type
  7701. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  7702. buffer->buft = buft;
  7703. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  7704. return buffer;
  7705. UNUSED(buft);
  7706. }
  7707. struct ggml_backend_buffer_type_i cuda_backend_host_buffer_type_interface = {
  7708. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  7709. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  7710. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  7711. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  7712. };
  7713. ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  7714. static struct ggml_backend_buffer_type ggml_backend_buffer_type_cuda_host = {
  7715. /* .iface = */ cuda_backend_host_buffer_type_interface,
  7716. /* .context = */ nullptr,
  7717. };
  7718. return &ggml_backend_buffer_type_cuda_host;
  7719. }
  7720. // backend
  7721. struct ggml_backend_context_cuda {
  7722. int device;
  7723. };
  7724. static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  7725. return GGML_CUDA_NAME;
  7726. UNUSED(backend);
  7727. }
  7728. static void ggml_backend_cuda_free(ggml_backend_t backend) {
  7729. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7730. delete cuda_ctx;
  7731. delete backend;
  7732. }
  7733. static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  7734. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7735. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  7736. }
  7737. static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  7738. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7739. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7740. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  7741. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  7742. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7743. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  7744. }
  7745. static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  7746. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7747. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  7748. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  7749. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  7750. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  7751. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  7752. }
  7753. static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  7754. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7755. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  7756. UNUSED(backend);
  7757. }
  7758. static ggml_backend_graph_plan_t ggml_backend_cuda_graph_plan_create(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7759. GGML_ASSERT(!"not implemented");
  7760. return nullptr;
  7761. UNUSED(backend);
  7762. UNUSED(cgraph);
  7763. }
  7764. static void ggml_backend_cuda_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7765. GGML_ASSERT(!"not implemented");
  7766. UNUSED(backend);
  7767. UNUSED(plan);
  7768. }
  7769. static void ggml_backend_cuda_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  7770. GGML_ASSERT(!"not implemented");
  7771. UNUSED(backend);
  7772. UNUSED(plan);
  7773. }
  7774. static void ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  7775. ggml_backend_context_cuda * cuda_ctx = (ggml_backend_context_cuda *)backend->context;
  7776. ggml_cuda_set_main_device(cuda_ctx->device);
  7777. ggml_compute_params params = {};
  7778. params.type = GGML_TASK_COMPUTE;
  7779. params.ith = 0;
  7780. for (int i = 0; i < cgraph->n_nodes; i++) {
  7781. ggml_tensor * node = cgraph->nodes[i];
  7782. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  7783. continue;
  7784. assert(node->backend == GGML_BACKEND_GPU);
  7785. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7786. assert(node->extra != nullptr);
  7787. for (int j = 0; j < GGML_MAX_SRC; j++) {
  7788. if (node->src[j] != nullptr) {
  7789. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  7790. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  7791. assert(node->src[j]->extra != nullptr);
  7792. }
  7793. }
  7794. bool ok = ggml_cuda_compute_forward(&params, node);
  7795. if (!ok) {
  7796. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  7797. }
  7798. GGML_ASSERT(ok);
  7799. #if 0
  7800. if (node->type == GGML_TYPE_F32) {
  7801. cudaDeviceSynchronize();
  7802. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  7803. cudaMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), cudaMemcpyDeviceToHost);
  7804. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  7805. ggml_type_name(node->src[0]->type),
  7806. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  7807. node->src[0]->name,
  7808. node->src[1] ? node->src[1]->name : "none");
  7809. double sum = 0.0;
  7810. double sq_sum = 0.0;
  7811. for (int i = 0; i < ggml_nelements(node); i++) {
  7812. printf("%f ", tmp[i]);
  7813. sum += tmp[i];
  7814. sq_sum += tmp[i]*tmp[i];
  7815. }
  7816. printf("\n");
  7817. printf("sum: %f, ", sum);
  7818. printf("sq_sum: %f\n", sq_sum);
  7819. }
  7820. #endif
  7821. }
  7822. UNUSED(backend);
  7823. }
  7824. static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  7825. switch (op->op) {
  7826. case GGML_OP_UNARY:
  7827. switch (ggml_get_unary_op(op)) {
  7828. case GGML_UNARY_OP_GELU:
  7829. case GGML_UNARY_OP_SILU:
  7830. case GGML_UNARY_OP_RELU:
  7831. case GGML_UNARY_OP_GELU_QUICK:
  7832. case GGML_UNARY_OP_TANH:
  7833. return true;
  7834. default:
  7835. return false;
  7836. }
  7837. break;
  7838. case GGML_OP_MUL_MAT:
  7839. case GGML_OP_MUL_MAT_ID:
  7840. {
  7841. struct ggml_tensor * a;
  7842. struct ggml_tensor * b;
  7843. if (op->op == GGML_OP_MUL_MAT) {
  7844. a = op->src[0];
  7845. b = op->src[1];
  7846. } else {
  7847. a = op->src[2];
  7848. b = op->src[1];
  7849. }
  7850. if (a->ne[3] != b->ne[3]) {
  7851. return false;
  7852. }
  7853. return true;
  7854. } break;
  7855. case GGML_OP_GET_ROWS:
  7856. {
  7857. switch (op->src[0]->type) {
  7858. case GGML_TYPE_F16:
  7859. case GGML_TYPE_F32:
  7860. case GGML_TYPE_Q4_0:
  7861. case GGML_TYPE_Q4_1:
  7862. case GGML_TYPE_Q5_0:
  7863. case GGML_TYPE_Q5_1:
  7864. case GGML_TYPE_Q8_0:
  7865. return true;
  7866. default:
  7867. return false;
  7868. }
  7869. } break;
  7870. case GGML_OP_CPY:
  7871. {
  7872. ggml_type src0_type = op->src[0]->type;
  7873. ggml_type src1_type = op->src[1]->type;
  7874. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  7875. return true;
  7876. }
  7877. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  7878. return true;
  7879. }
  7880. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  7881. return true;
  7882. }
  7883. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  7884. return true;
  7885. }
  7886. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  7887. return true;
  7888. }
  7889. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  7890. return true;
  7891. }
  7892. return false;
  7893. } break;
  7894. case GGML_OP_NONE:
  7895. case GGML_OP_RESHAPE:
  7896. case GGML_OP_VIEW:
  7897. case GGML_OP_PERMUTE:
  7898. case GGML_OP_TRANSPOSE:
  7899. case GGML_OP_NORM:
  7900. case GGML_OP_REPEAT:
  7901. case GGML_OP_DUP:
  7902. case GGML_OP_ADD:
  7903. case GGML_OP_MUL:
  7904. case GGML_OP_DIV:
  7905. case GGML_OP_RMS_NORM:
  7906. case GGML_OP_SCALE:
  7907. case GGML_OP_SQR:
  7908. case GGML_OP_CLAMP:
  7909. case GGML_OP_CONT:
  7910. case GGML_OP_DIAG_MASK_INF:
  7911. case GGML_OP_SOFT_MAX:
  7912. case GGML_OP_ROPE:
  7913. case GGML_OP_ALIBI:
  7914. case GGML_OP_IM2COL:
  7915. case GGML_OP_SUM_ROWS:
  7916. case GGML_OP_ARGSORT:
  7917. case GGML_OP_ACC:
  7918. case GGML_OP_CONCAT:
  7919. case GGML_OP_GROUP_NORM:
  7920. case GGML_OP_UPSCALE:
  7921. case GGML_OP_PAD:
  7922. case GGML_OP_LEAKY_RELU:
  7923. return true;
  7924. default:
  7925. return false;
  7926. }
  7927. UNUSED(backend);
  7928. }
  7929. static ggml_backend_i cuda_backend_i = {
  7930. /* .get_name = */ ggml_backend_cuda_name,
  7931. /* .free = */ ggml_backend_cuda_free,
  7932. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  7933. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  7934. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  7935. /* .cpy_tensor_from_async = */ NULL,
  7936. /* .cpy_tensor_to_async = */ NULL,
  7937. /* .synchronize = */ ggml_backend_cuda_synchronize,
  7938. /* .graph_plan_create = */ ggml_backend_cuda_graph_plan_create,
  7939. /* .graph_plan_free = */ ggml_backend_cuda_graph_plan_free,
  7940. /* .graph_plan_compute = */ ggml_backend_cuda_graph_plan_compute,
  7941. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  7942. /* .supports_op = */ ggml_backend_cuda_supports_op,
  7943. };
  7944. ggml_backend_t ggml_backend_cuda_init(int device) {
  7945. ggml_init_cublas(); // TODO: remove from ggml.c
  7946. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  7947. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  7948. return nullptr;
  7949. }
  7950. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  7951. ggml_cuda_set_main_device(device);
  7952. ggml_backend_context_cuda * ctx = new ggml_backend_context_cuda {
  7953. /* .device = */ device
  7954. };
  7955. ggml_backend_t cuda_backend = new ggml_backend {
  7956. /* .interface = */ cuda_backend_i,
  7957. /* .context = */ ctx
  7958. };
  7959. return cuda_backend;
  7960. }
  7961. bool ggml_backend_is_cuda(ggml_backend_t backend) {
  7962. return backend->iface.get_name == ggml_backend_cuda_name;
  7963. }
  7964. static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  7965. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  7966. return cuda_backend;
  7967. UNUSED(params);
  7968. }
  7969. extern "C" int ggml_backend_cuda_reg_devices();
  7970. int ggml_backend_cuda_reg_devices() {
  7971. int device_count = ggml_cuda_get_device_count();
  7972. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  7973. for (int i = 0; i < device_count; i++) {
  7974. char name[128];
  7975. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  7976. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  7977. }
  7978. return device_count;
  7979. }