1
0

ggml-cuda.cu 445 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606
  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 <string>
  12. #include <vector>
  13. #include <map>
  14. #include <array>
  15. // stringize macro for converting __CUDA_ARCH_LIST__ (list of integers) to string
  16. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  17. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  18. #if defined(GGML_USE_HIPBLAS)
  19. #include <hip/hip_runtime.h>
  20. #include <hipblas/hipblas.h>
  21. #include <hip/hip_fp16.h>
  22. #ifdef __HIP_PLATFORM_AMD__
  23. // for rocblas_initialize()
  24. #include "rocblas/rocblas.h"
  25. #endif // __HIP_PLATFORM_AMD__
  26. #define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
  27. #define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
  28. #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
  29. #define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
  30. #define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
  31. #define CUBLAS_OP_N HIPBLAS_OP_N
  32. #define CUBLAS_OP_T HIPBLAS_OP_T
  33. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  34. #define CUBLAS_TF32_TENSOR_OP_MATH 0
  35. #define CUDA_R_16F HIPBLAS_R_16F
  36. #define CUDA_R_32F HIPBLAS_R_32F
  37. #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
  38. #define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
  39. #define cublasCreate hipblasCreate
  40. #define cublasGemmEx hipblasGemmEx
  41. #define cublasGemmBatchedEx hipblasGemmBatchedEx
  42. #define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
  43. #define cublasHandle_t hipblasHandle_t
  44. #define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
  45. #define cublasSetStream hipblasSetStream
  46. #define cublasSgemm hipblasSgemm
  47. #define cublasStatus_t hipblasStatus_t
  48. #define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
  49. #define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
  50. #define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
  51. #define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
  52. #define cudaDeviceProp hipDeviceProp_t
  53. #define cudaDeviceSynchronize hipDeviceSynchronize
  54. #define cudaError_t hipError_t
  55. #define cudaEventCreateWithFlags hipEventCreateWithFlags
  56. #define cudaEventDisableTiming hipEventDisableTiming
  57. #define cudaEventRecord hipEventRecord
  58. #define cudaEvent_t hipEvent_t
  59. #define cudaEventDestroy hipEventDestroy
  60. #define cudaFree hipFree
  61. #define cudaFreeHost hipHostFree
  62. #define cudaGetDevice hipGetDevice
  63. #define cudaGetDeviceCount hipGetDeviceCount
  64. #define cudaGetDeviceProperties hipGetDeviceProperties
  65. #define cudaGetErrorString hipGetErrorString
  66. #define cudaGetLastError hipGetLastError
  67. #ifdef GGML_HIP_UMA
  68. #define cudaMalloc hipMallocManaged
  69. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
  70. #else
  71. #define cudaMalloc hipMalloc
  72. #define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
  73. #endif
  74. #define cudaMemcpy hipMemcpy
  75. #define cudaMemcpyAsync hipMemcpyAsync
  76. #define cudaMemcpyPeerAsync hipMemcpyPeerAsync
  77. #define cudaMemcpy2DAsync hipMemcpy2DAsync
  78. #define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
  79. #define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
  80. #define cudaMemcpyHostToDevice hipMemcpyHostToDevice
  81. #define cudaMemcpyKind hipMemcpyKind
  82. #define cudaMemset hipMemset
  83. #define cudaMemsetAsync hipMemsetAsync
  84. #define cudaMemGetInfo hipMemGetInfo
  85. #define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
  86. #define cudaSetDevice hipSetDevice
  87. #define cudaStreamCreateWithFlags hipStreamCreateWithFlags
  88. #define cudaStreamFireAndForget hipStreamFireAndForget
  89. #define cudaStreamNonBlocking hipStreamNonBlocking
  90. #define cudaStreamSynchronize hipStreamSynchronize
  91. #define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
  92. #define cudaStream_t hipStream_t
  93. #define cudaSuccess hipSuccess
  94. #define __trap abort
  95. #define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
  96. #define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
  97. #define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
  98. #define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
  99. #define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
  100. #define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
  101. #define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
  102. #define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
  103. #define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
  104. #else
  105. #include <cuda_runtime.h>
  106. #include <cuda.h>
  107. #include <cublas_v2.h>
  108. #include <cuda_fp16.h>
  109. #if CUDART_VERSION < 11020
  110. #define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
  111. #define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
  112. #define CUBLAS_COMPUTE_16F CUDA_R_16F
  113. #define CUBLAS_COMPUTE_32F CUDA_R_32F
  114. #define cublasComputeType_t cudaDataType_t
  115. #endif // CUDART_VERSION < 11020
  116. #endif // defined(GGML_USE_HIPBLAS)
  117. // ggml-cuda need half type so keep ggml headers include at last
  118. #include "ggml-cuda.h"
  119. #include "ggml.h"
  120. #include "ggml-backend-impl.h"
  121. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  122. #define CC_PASCAL 600
  123. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  124. #define CC_VOLTA 700
  125. #define CC_OFFSET_AMD 1000000
  126. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  127. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  128. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  129. #define GGML_CUDA_MAX_NODES 8192
  130. // define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
  131. // on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
  132. // for large computational tasks. the drawback is that this requires some extra amount of VRAM:
  133. // - 7B quantum model: +100-200 MB
  134. // - 13B quantum model: +200-400 MB
  135. //
  136. //#define GGML_CUDA_FORCE_MMQ
  137. // TODO: improve this to be correct for more hardware
  138. // for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
  139. #if !defined(GGML_CUDA_FORCE_MMQ)
  140. #define CUDA_USE_TENSOR_CORES
  141. #endif
  142. #define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
  143. #define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
  144. #if defined(GGML_USE_HIPBLAS)
  145. #define __CUDA_ARCH__ 1300
  146. #if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
  147. defined(__gfx1150__) || defined(__gfx1151__)
  148. #define RDNA3
  149. #endif
  150. #if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
  151. defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
  152. #define RDNA2
  153. #endif
  154. #ifndef __has_builtin
  155. #define __has_builtin(x) 0
  156. #endif
  157. typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
  158. static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
  159. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  160. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  161. #if __has_builtin(__builtin_elementwise_sub_sat)
  162. const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
  163. return reinterpret_cast<const int &>(c);
  164. #else
  165. int8x4_t c;
  166. int16_t tmp;
  167. #pragma unroll
  168. for (int i = 0; i < 4; i++) {
  169. tmp = va[i] - vb[i];
  170. if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
  171. if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
  172. c[i] = tmp;
  173. }
  174. return reinterpret_cast<int &>(c);
  175. #endif // __has_builtin(__builtin_elementwise_sub_sat)
  176. }
  177. static __device__ __forceinline__ int __vsub4(const int a, const int b) {
  178. return __vsubss4(a, b);
  179. }
  180. static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
  181. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
  182. c = __builtin_amdgcn_sdot4(a, b, c, false);
  183. #elif defined(RDNA3)
  184. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  185. #elif defined(__gfx1010__) || defined(__gfx900__)
  186. int tmp1;
  187. int tmp2;
  188. asm("\n \
  189. 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 \
  190. 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 \
  191. v_add3_u32 %0, %1, %2, %0 \n \
  192. 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 \
  193. 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 \
  194. v_add3_u32 %0, %1, %2, %0 \n \
  195. "
  196. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  197. : "v"(a), "v"(b)
  198. );
  199. #else
  200. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  201. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  202. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  203. #endif
  204. return c;
  205. }
  206. #endif // defined(GGML_USE_HIPBLAS)
  207. #if defined(_MSC_VER)
  208. #pragma warning(disable: 4244 4267) // possible loss of data
  209. #endif
  210. static_assert(sizeof(half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  211. [[noreturn]]
  212. static void ggml_cuda_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  213. int id = -1; // in case cudaGetDevice fails
  214. cudaGetDevice(&id);
  215. fprintf(stderr, "CUDA error: %s\n", msg);
  216. fprintf(stderr, " current device: %d, in function %s at %s:%d\n", id, func, file, line);
  217. fprintf(stderr, " %s\n", stmt);
  218. // abort with GGML_ASSERT to get a stack trace
  219. GGML_ASSERT(!"CUDA error");
  220. }
  221. #define CUDA_CHECK_GEN(err, success, error_fn) \
  222. do { \
  223. auto err_ = (err); \
  224. if (err_ != (success)) { \
  225. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  226. } \
  227. } while (0)
  228. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  229. #if CUDART_VERSION >= 12000
  230. static const char * cublas_get_error_str(const cublasStatus_t err) {
  231. return cublasGetStatusString(err);
  232. }
  233. #else
  234. static const char * cublas_get_error_str(const cublasStatus_t err) {
  235. switch (err) {
  236. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  237. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  238. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  239. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  240. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  241. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  242. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  243. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  244. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  245. default: return "unknown error";
  246. }
  247. }
  248. #endif // CUDART_VERSION >= 12000
  249. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  250. #if !defined(GGML_USE_HIPBLAS)
  251. static const char * cu_get_error_str(CUresult err) {
  252. const char * err_str;
  253. cuGetErrorString(err, &err_str);
  254. return err_str;
  255. }
  256. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  257. #endif
  258. #if CUDART_VERSION >= 11100
  259. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  260. #else
  261. #define GGML_CUDA_ASSUME(x)
  262. #endif // CUDART_VERSION >= 11100
  263. #ifdef GGML_CUDA_F16
  264. typedef half dfloat; // dequantize float
  265. typedef half2 dfloat2;
  266. #else
  267. typedef float dfloat; // dequantize float
  268. typedef float2 dfloat2;
  269. #endif //GGML_CUDA_F16
  270. static __device__ __forceinline__ int get_int_from_int8(const int8_t * x8, const int & i32) {
  271. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  272. int x32 = 0;
  273. x32 |= x16[0] << 0;
  274. x32 |= x16[1] << 16;
  275. return x32;
  276. }
  277. static __device__ __forceinline__ int get_int_from_uint8(const uint8_t * x8, const int & i32) {
  278. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  279. int x32 = 0;
  280. x32 |= x16[0] << 0;
  281. x32 |= x16[1] << 16;
  282. return x32;
  283. }
  284. static __device__ __forceinline__ int get_int_from_int8_aligned(const int8_t * x8, const int & i32) {
  285. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  286. }
  287. static __device__ __forceinline__ int get_int_from_uint8_aligned(const uint8_t * x8, const int & i32) {
  288. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  289. }
  290. template<typename T>
  291. using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int k, cudaStream_t stream);
  292. typedef to_t_cuda_t<float> to_fp32_cuda_t;
  293. typedef to_t_cuda_t<half> to_fp16_cuda_t;
  294. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  295. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  296. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  297. typedef void (*ggml_cuda_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  298. typedef void (*ggml_cuda_op_mul_mat_t)(
  299. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  300. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  301. const int64_t src1_padded_row_size, cudaStream_t stream);
  302. typedef void (*ggml_cuda_op_flatten_t)(
  303. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  304. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream);
  305. // QK = number of values after dequantization
  306. // QR = QK / number of values before dequantization
  307. // QI = number of 32 bit integers before dequantization
  308. #define QK4_0 32
  309. #define QR4_0 2
  310. #define QI4_0 (QK4_0 / (4 * QR4_0))
  311. typedef struct {
  312. half d; // delta
  313. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  314. } block_q4_0;
  315. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  316. #define QK4_1 32
  317. #define QR4_1 2
  318. #define QI4_1 (QK4_1 / (4 * QR4_1))
  319. typedef struct {
  320. half2 dm; // dm.x = delta, dm.y = min
  321. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  322. } block_q4_1;
  323. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  324. #define QK5_0 32
  325. #define QR5_0 2
  326. #define QI5_0 (QK5_0 / (4 * QR5_0))
  327. typedef struct {
  328. half d; // delta
  329. uint8_t qh[4]; // 5-th bit of quants
  330. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  331. } block_q5_0;
  332. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  333. #define QK5_1 32
  334. #define QR5_1 2
  335. #define QI5_1 (QK5_1 / (4 * QR5_1))
  336. typedef struct {
  337. half2 dm; // dm.x = delta, dm.y = min
  338. uint8_t qh[4]; // 5-th bit of quants
  339. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  340. } block_q5_1;
  341. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  342. #define QK8_0 32
  343. #define QR8_0 1
  344. #define QI8_0 (QK8_0 / (4 * QR8_0))
  345. typedef struct {
  346. half d; // delta
  347. int8_t qs[QK8_0]; // quants
  348. } block_q8_0;
  349. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  350. #define QK8_1 32
  351. #define QR8_1 1
  352. #define QI8_1 (QK8_1 / (4 * QR8_1))
  353. typedef struct {
  354. half2 ds; // ds.x = delta, ds.y = sum
  355. int8_t qs[QK8_0]; // quants
  356. } block_q8_1;
  357. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  358. typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  359. typedef void (*allocate_tiles_cuda_t)(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc);
  360. typedef void (*load_tiles_cuda_t)(
  361. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  362. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row);
  363. typedef float (*vec_dot_q_mul_mat_cuda_t)(
  364. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  365. const int * __restrict__ y_qs, const half2 * __restrict__ y_ms, const int & i, const int & j, const int & k);
  366. //================================= k-quants
  367. #ifdef GGML_QKK_64
  368. #define QK_K 64
  369. #define K_SCALE_SIZE 4
  370. #else
  371. #define QK_K 256
  372. #define K_SCALE_SIZE 12
  373. #endif
  374. #define QR2_K 4
  375. #define QI2_K (QK_K / (4*QR2_K))
  376. typedef struct {
  377. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  378. uint8_t qs[QK_K/4]; // quants
  379. half2 dm; // super-block scale for quantized scales/mins
  380. } block_q2_K;
  381. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  382. #define QR3_K 4
  383. #define QI3_K (QK_K / (4*QR3_K))
  384. typedef struct {
  385. uint8_t hmask[QK_K/8]; // quants - high bit
  386. uint8_t qs[QK_K/4]; // quants - low 2 bits
  387. #ifdef GGML_QKK_64
  388. uint8_t scales[2]; // scales, quantized with 8 bits
  389. #else
  390. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  391. #endif
  392. half d; // super-block scale
  393. } block_q3_K;
  394. //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");
  395. #define QR4_K 2
  396. #define QI4_K (QK_K / (4*QR4_K))
  397. #ifdef GGML_QKK_64
  398. typedef struct {
  399. half dm[2]; // super-block scales/mins
  400. uint8_t scales[2]; // 4-bit block scales/mins
  401. uint8_t qs[QK_K/2]; // 4--bit quants
  402. } block_q4_K;
  403. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  404. #else
  405. typedef struct {
  406. half2 dm; // super-block scale for quantized scales/mins
  407. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  408. uint8_t qs[QK_K/2]; // 4--bit quants
  409. } block_q4_K;
  410. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  411. #endif
  412. #define QR5_K 2
  413. #define QI5_K (QK_K / (4*QR5_K))
  414. #ifdef GGML_QKK_64
  415. typedef struct {
  416. half d; // super-block scale
  417. int8_t scales[QK_K/16]; // block scales
  418. uint8_t qh[QK_K/8]; // quants, high bit
  419. uint8_t qs[QK_K/2]; // quants, low 4 bits
  420. } block_q5_K;
  421. 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");
  422. #else
  423. typedef struct {
  424. half2 dm; // super-block scale for quantized scales/mins
  425. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  426. uint8_t qh[QK_K/8]; // quants, high bit
  427. uint8_t qs[QK_K/2]; // quants, low 4 bits
  428. } block_q5_K;
  429. 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");
  430. #endif
  431. #define QR6_K 2
  432. #define QI6_K (QK_K / (4*QR6_K))
  433. typedef struct {
  434. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  435. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  436. int8_t scales[QK_K/16]; // scales
  437. half d; // delta
  438. } block_q6_K;
  439. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  440. #define QR2_XXS 8
  441. #define QI2_XXS (QK_K / (4*QR2_XXS))
  442. typedef struct {
  443. half d;
  444. uint16_t qs[QK_K/8];
  445. } block_iq2_xxs;
  446. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  447. #define QR2_XS 8
  448. #define QI2_XS (QK_K / (4*QR2_XS))
  449. typedef struct {
  450. half d;
  451. uint16_t qs[QK_K/8];
  452. uint8_t scales[QK_K/32];
  453. } block_iq2_xs;
  454. static_assert(sizeof(block_iq2_xs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
  455. #define QR3_XXS 8
  456. #define QI3_XXS (QK_K / (4*QR3_XXS))
  457. typedef struct {
  458. half d;
  459. uint8_t qs[3*(QK_K/8)];
  460. } block_iq3_xxs;
  461. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  462. #define QR1_S 8
  463. #define QI1_S (QK_K / (4*QR1_S))
  464. typedef struct {
  465. half d;
  466. uint8_t qs[QK_K/8];
  467. uint8_t scales[QK_K/16];
  468. } block_iq1_s;
  469. static_assert(sizeof(block_iq1_s) == sizeof(ggml_fp16_t) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
  470. #define WARP_SIZE 32
  471. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  472. #define CUDA_GELU_BLOCK_SIZE 256
  473. #define CUDA_SILU_BLOCK_SIZE 256
  474. #define CUDA_TANH_BLOCK_SIZE 256
  475. #define CUDA_RELU_BLOCK_SIZE 256
  476. #define CUDA_HARDSIGMOID_BLOCK_SIZE 256
  477. #define CUDA_HARDSWISH_BLOCK_SIZE 256
  478. #define CUDA_SQR_BLOCK_SIZE 256
  479. #define CUDA_CPY_BLOCK_SIZE 32
  480. #define CUDA_SCALE_BLOCK_SIZE 256
  481. #define CUDA_CLAMP_BLOCK_SIZE 256
  482. #define CUDA_ROPE_BLOCK_SIZE 256
  483. #define CUDA_SOFT_MAX_BLOCK_SIZE 1024
  484. #define CUDA_ALIBI_BLOCK_SIZE 32
  485. #define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
  486. #define CUDA_QUANTIZE_BLOCK_SIZE 256
  487. #define CUDA_DEQUANTIZE_BLOCK_SIZE 256
  488. #define CUDA_GET_ROWS_BLOCK_SIZE 256
  489. #define CUDA_UPSCALE_BLOCK_SIZE 256
  490. #define CUDA_CONCAT_BLOCK_SIZE 256
  491. #define CUDA_PAD_BLOCK_SIZE 256
  492. #define CUDA_ACC_BLOCK_SIZE 256
  493. #define CUDA_IM2COL_BLOCK_SIZE 256
  494. #define CUDA_POOL2D_BLOCK_SIZE 256
  495. #define CUDA_Q8_0_NE_ALIGN 2048
  496. // dmmv = dequantize_mul_mat_vec
  497. #ifndef GGML_CUDA_DMMV_X
  498. #define GGML_CUDA_DMMV_X 32
  499. #endif
  500. #ifndef GGML_CUDA_MMV_Y
  501. #define GGML_CUDA_MMV_Y 1
  502. #endif
  503. #ifndef K_QUANTS_PER_ITERATION
  504. #define K_QUANTS_PER_ITERATION 2
  505. #else
  506. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  507. #endif
  508. #ifndef GGML_CUDA_PEER_MAX_BATCH_SIZE
  509. #define GGML_CUDA_PEER_MAX_BATCH_SIZE 128
  510. #endif // GGML_CUDA_PEER_MAX_BATCH_SIZE
  511. #define MUL_MAT_SRC1_COL_STRIDE 128
  512. #define MAX_STREAMS 8
  513. static cudaStream_t g_cudaStreams[GGML_CUDA_MAX_DEVICES][MAX_STREAMS] = { { nullptr } };
  514. struct ggml_tensor_extra_gpu {
  515. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  516. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][MAX_STREAMS]; // events for synchronizing multiple GPUs
  517. };
  518. // this is faster on Windows
  519. // probably because the Windows CUDA libraries forget to make this check before invoking the drivers
  520. static void ggml_cuda_set_device(const int device) {
  521. int current_device;
  522. CUDA_CHECK(cudaGetDevice(&current_device));
  523. if (device == current_device) {
  524. return;
  525. }
  526. CUDA_CHECK(cudaSetDevice(device));
  527. }
  528. static int g_device_count = -1;
  529. static int g_main_device = 0;
  530. static std::array<float, GGML_CUDA_MAX_DEVICES> g_default_tensor_split = {};
  531. struct cuda_device_capabilities {
  532. int cc; // compute capability
  533. size_t smpb; // max. shared memory per block
  534. bool vmm; // virtual memory support
  535. size_t vmm_granularity; // granularity of virtual memory
  536. };
  537. static cuda_device_capabilities g_device_caps[GGML_CUDA_MAX_DEVICES] = { {0, 0, false, 0} };
  538. static cublasHandle_t g_cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  539. [[noreturn]]
  540. static __device__ void no_device_code(
  541. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  542. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  543. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  544. file_name, line, function_name, arch);
  545. (void) arch_list;
  546. #else
  547. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  548. file_name, line, function_name, arch, arch_list);
  549. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  550. __trap();
  551. (void) no_device_code; // suppress unused function warning
  552. }
  553. #ifdef __CUDA_ARCH__
  554. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  555. #else
  556. #define NO_DEVICE_CODE GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
  557. #endif // __CUDA_ARCH__
  558. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  559. #pragma unroll
  560. for (int mask = 16; mask > 0; mask >>= 1) {
  561. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  562. }
  563. return x;
  564. }
  565. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  566. #pragma unroll
  567. for (int mask = 16; mask > 0; mask >>= 1) {
  568. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  569. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  570. }
  571. return a;
  572. }
  573. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  574. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  575. #pragma unroll
  576. for (int mask = 16; mask > 0; mask >>= 1) {
  577. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  578. }
  579. return a;
  580. #else
  581. (void) a;
  582. NO_DEVICE_CODE;
  583. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  584. }
  585. static __device__ __forceinline__ float warp_reduce_max(float x) {
  586. #pragma unroll
  587. for (int mask = 16; mask > 0; mask >>= 1) {
  588. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  589. }
  590. return x;
  591. }
  592. static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  593. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  594. #pragma unroll
  595. for (int mask = 16; mask > 0; mask >>= 1) {
  596. x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  597. }
  598. return x;
  599. #else
  600. (void) x;
  601. NO_DEVICE_CODE;
  602. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
  603. }
  604. static __device__ __forceinline__ float op_repeat(const float a, const float b) {
  605. return b;
  606. GGML_UNUSED(a);
  607. }
  608. static __device__ __forceinline__ float op_add(const float a, const float b) {
  609. return a + b;
  610. }
  611. static __device__ __forceinline__ float op_mul(const float a, const float b) {
  612. return a * b;
  613. }
  614. static __device__ __forceinline__ float op_div(const float a, const float b) {
  615. return a / b;
  616. }
  617. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  618. static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  619. int ne0, int ne1, int ne2, int ne3,
  620. int ne10, int ne11, int ne12, int ne13,
  621. /*int s0, */ int s1, int s2, int s3,
  622. /*int s10,*/ int s11, int s12, int s13) {
  623. const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
  624. const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
  625. const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
  626. const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
  627. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  628. return;
  629. }
  630. const int i11 = i1 % ne11;
  631. const int i12 = i2 % ne12;
  632. const int i13 = i3 % ne13;
  633. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  634. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  635. const size_t i_dst = i_src0;
  636. const src0_t * src0_row = src0 + i_src0;
  637. const src1_t * src1_row = src1 + i_src1;
  638. dst_t * dst_row = dst + i_dst;
  639. for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
  640. const int i10 = i0 % ne10;
  641. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  642. }
  643. }
  644. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  645. static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  646. int ne0, int ne1, int ne2, int ne3,
  647. int ne10, int ne11, int ne12, int ne13,
  648. /*int s0, */ int s1, int s2, int s3,
  649. /*int s10,*/ int s11, int s12, int s13) {
  650. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  651. const int i3 = i/(ne2*ne1*ne0);
  652. const int i2 = (i/(ne1*ne0)) % ne2;
  653. const int i1 = (i/ne0) % ne1;
  654. const int i0 = i % ne0;
  655. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  656. return;
  657. }
  658. const int i11 = i1 % ne11;
  659. const int i12 = i2 % ne12;
  660. const int i13 = i3 % ne13;
  661. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  662. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  663. const size_t i_dst = i_src0;
  664. const src0_t * src0_row = src0 + i_src0;
  665. const src1_t * src1_row = src1 + i_src1;
  666. dst_t * dst_row = dst + i_dst;
  667. const int i10 = i0 % ne10;
  668. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  669. }
  670. static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
  671. const int ne10, const int ne11, const int ne12,
  672. const int nb1, const int nb2, int offset) {
  673. const int i = blockDim.x * blockIdx.x + threadIdx.x;
  674. if (i >= ne) {
  675. return;
  676. }
  677. int src1_idx = i - offset;
  678. int oz = src1_idx / nb2;
  679. int oy = (src1_idx - (oz * nb2)) / nb1;
  680. int ox = src1_idx % nb1;
  681. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  682. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  683. } else {
  684. dst[i] = x[i];
  685. }
  686. }
  687. static __global__ void gelu_f32(const float * x, float * dst, const int k) {
  688. const float GELU_COEF_A = 0.044715f;
  689. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  690. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  691. if (i >= k) {
  692. return;
  693. }
  694. float xi = x[i];
  695. dst[i] = 0.5f*xi*(1.0f + tanhf(SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi)));
  696. }
  697. static __global__ void silu_f32(const float * x, float * dst, const int k) {
  698. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  699. if (i >= k) {
  700. return;
  701. }
  702. dst[i] = x[i] / (1.0f + expf(-x[i]));
  703. }
  704. static __global__ void gelu_quick_f32(const float * x, float * dst, int k) {
  705. const float GELU_QUICK_COEF = -1.702f;
  706. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  707. if (i >= k) {
  708. return;
  709. }
  710. dst[i] = x[i] * (1.0f / (1.0f + expf(GELU_QUICK_COEF * x[i])));
  711. }
  712. static __global__ void tanh_f32(const float * x, float * dst, int k) {
  713. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  714. if (i >= k) {
  715. return;
  716. }
  717. dst[i] = tanhf(x[i]);
  718. }
  719. static __global__ void relu_f32(const float * x, float * dst, const int k) {
  720. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  721. if (i >= k) {
  722. return;
  723. }
  724. dst[i] = fmaxf(x[i], 0);
  725. }
  726. static __global__ void hardsigmoid_f32(const float * x, float * dst, const int k) {
  727. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  728. if (i >= k) {
  729. return;
  730. }
  731. dst[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  732. }
  733. static __global__ void hardswish_f32(const float * x, float * dst, const int k) {
  734. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  735. if (i >= k) {
  736. return;
  737. }
  738. dst[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f));
  739. }
  740. static __global__ void leaky_relu_f32(const float * x, float * dst, const int k, const float negative_slope) {
  741. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  742. if (i >= k) {
  743. return;
  744. }
  745. dst[i] = fmaxf(x[i], 0) + fminf(x[i], 0.0f) * negative_slope;
  746. }
  747. static __global__ void sqr_f32(const float * x, float * dst, const int k) {
  748. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  749. if (i >= k) {
  750. return;
  751. }
  752. dst[i] = x[i] * x[i];
  753. }
  754. template <int block_size>
  755. static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  756. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  757. const int tid = threadIdx.x;
  758. float2 mean_var = make_float2(0.f, 0.f);
  759. for (int col = tid; col < ncols; col += block_size) {
  760. const float xi = x[row*ncols + col];
  761. mean_var.x += xi;
  762. mean_var.y += xi * xi;
  763. }
  764. // sum up partial sums
  765. mean_var = warp_reduce_sum(mean_var);
  766. if (block_size > WARP_SIZE) {
  767. __shared__ float2 s_sum[32];
  768. int warp_id = threadIdx.x / WARP_SIZE;
  769. int lane_id = threadIdx.x % WARP_SIZE;
  770. if (lane_id == 0) {
  771. s_sum[warp_id] = mean_var;
  772. }
  773. __syncthreads();
  774. mean_var = s_sum[lane_id];
  775. mean_var = warp_reduce_sum(mean_var);
  776. }
  777. const float mean = mean_var.x / ncols;
  778. const float var = mean_var.y / ncols - mean * mean;
  779. const float inv_std = rsqrtf(var + eps);
  780. for (int col = tid; col < ncols; col += block_size) {
  781. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  782. }
  783. }
  784. static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
  785. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  786. if (nidx >= ne0) {
  787. return;
  788. }
  789. // operation
  790. int offset_dst =
  791. nidx +
  792. blockIdx.y * ne0 +
  793. blockIdx.z * ne0 * gridDim.y;
  794. if (blockIdx.z < ne02) { // src0
  795. int offset_src =
  796. nidx +
  797. blockIdx.y * ne0 +
  798. blockIdx.z * ne0 * gridDim.y;
  799. dst[offset_dst] = x[offset_src];
  800. } else {
  801. int offset_src =
  802. nidx +
  803. blockIdx.y * ne0 +
  804. (blockIdx.z - ne02) * ne0 * gridDim.y;
  805. dst[offset_dst] = y[offset_src];
  806. }
  807. }
  808. static __global__ void upscale_f32(const float * x, float * dst, const int ne00, const int nb02, const int scale_factor) {
  809. int ne0 = ne00 * scale_factor;
  810. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  811. if (nidx >= ne0) {
  812. return;
  813. }
  814. // operation
  815. int i00 = nidx / scale_factor;
  816. int i01 = blockIdx.y / scale_factor;
  817. int offset_src =
  818. i00 +
  819. i01 * ne00 +
  820. blockIdx.z * nb02;
  821. int offset_dst =
  822. nidx +
  823. blockIdx.y * ne0 +
  824. blockIdx.z * ne0 * gridDim.y;
  825. dst[offset_dst] = x[offset_src];
  826. }
  827. static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02) {
  828. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  829. if (nidx >= ne0) {
  830. return;
  831. }
  832. // operation
  833. int offset_dst =
  834. nidx +
  835. blockIdx.y * ne0 +
  836. blockIdx.z * ne0 * gridDim.y;
  837. if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02) {
  838. int offset_src =
  839. nidx +
  840. blockIdx.y * ne00 +
  841. blockIdx.z * ne00 * ne01;
  842. dst[offset_dst] = x[offset_src];
  843. } else {
  844. dst[offset_dst] = 0.0f;
  845. }
  846. }
  847. template <int block_size>
  848. static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
  849. int start = blockIdx.x * group_size;
  850. int end = start + group_size;
  851. start += threadIdx.x;
  852. if (end >= ne_elements) {
  853. end = ne_elements;
  854. }
  855. float tmp = 0.0f; // partial sum for thread in warp
  856. for (int j = start; j < end; j += block_size) {
  857. tmp += x[j];
  858. }
  859. tmp = warp_reduce_sum(tmp);
  860. if (block_size > WARP_SIZE) {
  861. __shared__ float s_sum[32];
  862. int warp_id = threadIdx.x / WARP_SIZE;
  863. int lane_id = threadIdx.x % WARP_SIZE;
  864. if (lane_id == 0) {
  865. s_sum[warp_id] = tmp;
  866. }
  867. __syncthreads();
  868. tmp = s_sum[lane_id];
  869. tmp = warp_reduce_sum(tmp);
  870. }
  871. float mean = tmp / group_size;
  872. tmp = 0.0f;
  873. for (int j = start; j < end; j += block_size) {
  874. float xi = x[j] - mean;
  875. dst[j] = xi;
  876. tmp += xi * xi;
  877. }
  878. tmp = warp_reduce_sum(tmp);
  879. if (block_size > WARP_SIZE) {
  880. __shared__ float s_sum[32];
  881. int warp_id = threadIdx.x / WARP_SIZE;
  882. int lane_id = threadIdx.x % WARP_SIZE;
  883. if (lane_id == 0) {
  884. s_sum[warp_id] = tmp;
  885. }
  886. __syncthreads();
  887. tmp = s_sum[lane_id];
  888. tmp = warp_reduce_sum(tmp);
  889. }
  890. float variance = tmp / group_size;
  891. float scale = rsqrtf(variance + eps);
  892. for (int j = start; j < end; j += block_size) {
  893. dst[j] *= scale;
  894. }
  895. }
  896. template <int block_size>
  897. static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
  898. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  899. const int tid = threadIdx.x;
  900. float tmp = 0.0f; // partial sum for thread in warp
  901. for (int col = tid; col < ncols; col += block_size) {
  902. const float xi = x[row*ncols + col];
  903. tmp += xi * xi;
  904. }
  905. // sum up partial sums
  906. tmp = warp_reduce_sum(tmp);
  907. if (block_size > WARP_SIZE) {
  908. __shared__ float s_sum[32];
  909. int warp_id = threadIdx.x / WARP_SIZE;
  910. int lane_id = threadIdx.x % WARP_SIZE;
  911. if (lane_id == 0) {
  912. s_sum[warp_id] = tmp;
  913. }
  914. __syncthreads();
  915. tmp = s_sum[lane_id];
  916. tmp = warp_reduce_sum(tmp);
  917. }
  918. const float mean = tmp / ncols;
  919. const float scale = rsqrtf(mean + eps);
  920. for (int col = tid; col < ncols; col += block_size) {
  921. dst[row*ncols + col] = scale * x[row*ncols + col];
  922. }
  923. }
  924. static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  925. const block_q4_0 * x = (const block_q4_0 *) vx;
  926. const dfloat d = x[ib].d;
  927. const int vui = x[ib].qs[iqs];
  928. v.x = vui & 0xF;
  929. v.y = vui >> 4;
  930. #ifdef GGML_CUDA_F16
  931. v = __hsub2(v, {8.0f, 8.0f});
  932. v = __hmul2(v, {d, d});
  933. #else
  934. v.x = (v.x - 8.0f) * d;
  935. v.y = (v.y - 8.0f) * d;
  936. #endif // GGML_CUDA_F16
  937. }
  938. static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  939. const block_q4_1 * x = (const block_q4_1 *) vx;
  940. const dfloat d = __low2half(x[ib].dm);
  941. const dfloat m = __high2half(x[ib].dm);
  942. const int vui = x[ib].qs[iqs];
  943. v.x = vui & 0xF;
  944. v.y = vui >> 4;
  945. #ifdef GGML_CUDA_F16
  946. v = __hmul2(v, {d, d});
  947. v = __hadd2(v, {m, m});
  948. #else
  949. v.x = (v.x * d) + m;
  950. v.y = (v.y * d) + m;
  951. #endif // GGML_CUDA_F16
  952. }
  953. static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  954. const block_q5_0 * x = (const block_q5_0 *) vx;
  955. const dfloat d = x[ib].d;
  956. uint32_t qh;
  957. memcpy(&qh, x[ib].qh, sizeof(qh));
  958. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  959. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  960. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  961. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  962. #ifdef GGML_CUDA_F16
  963. v = __hsub2(v, {16.0f, 16.0f});
  964. v = __hmul2(v, {d, d});
  965. #else
  966. v.x = (v.x - 16.0f) * d;
  967. v.y = (v.y - 16.0f) * d;
  968. #endif // GGML_CUDA_F16
  969. }
  970. static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int ib, const int iqs, dfloat2 & v){
  971. const block_q5_1 * x = (const block_q5_1 *) vx;
  972. const dfloat d = __low2half(x[ib].dm);
  973. const dfloat m = __high2half(x[ib].dm);
  974. uint32_t qh;
  975. memcpy(&qh, x[ib].qh, sizeof(qh));
  976. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  977. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  978. v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
  979. v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
  980. #ifdef GGML_CUDA_F16
  981. v = __hmul2(v, {d, d});
  982. v = __hadd2(v, {m, m});
  983. #else
  984. v.x = (v.x * d) + m;
  985. v.y = (v.y * d) + m;
  986. #endif // GGML_CUDA_F16
  987. }
  988. static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int ib, const int iqs, dfloat2 & v){
  989. const block_q8_0 * x = (const block_q8_0 *) vx;
  990. const dfloat d = x[ib].d;
  991. v.x = x[ib].qs[iqs + 0];
  992. v.y = x[ib].qs[iqs + 1];
  993. #ifdef GGML_CUDA_F16
  994. v = __hmul2(v, {d, d});
  995. #else
  996. v.x *= d;
  997. v.y *= d;
  998. #endif // GGML_CUDA_F16
  999. }
  1000. template<typename dst_t>
  1001. static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1002. const int i = blockIdx.x;
  1003. // assume 32 threads
  1004. const int tid = threadIdx.x;
  1005. const int il = tid/8;
  1006. const int ir = tid%8;
  1007. const int ib = 8*i + ir;
  1008. if (ib >= nb32) {
  1009. return;
  1010. }
  1011. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1012. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  1013. const float d = __half2float(x->d);
  1014. const float dm = -8*d;
  1015. const uint8_t * q = x->qs + 4*il;
  1016. for (int l = 0; l < 4; ++l) {
  1017. y[l+ 0] = d * (q[l] & 0xF) + dm;
  1018. y[l+16] = d * (q[l] >> 4) + dm;
  1019. }
  1020. }
  1021. template<typename dst_t>
  1022. static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
  1023. const int i = blockIdx.x;
  1024. // assume 32 threads
  1025. const int tid = threadIdx.x;
  1026. const int il = tid/8;
  1027. const int ir = tid%8;
  1028. const int ib = 8*i + ir;
  1029. if (ib >= nb32) {
  1030. return;
  1031. }
  1032. dst_t * y = yy + 256*i + 32*ir + 4*il;
  1033. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  1034. const float2 d = __half22float2(x->dm);
  1035. const uint8_t * q = x->qs + 4*il;
  1036. for (int l = 0; l < 4; ++l) {
  1037. y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
  1038. y[l+16] = d.x * (q[l] >> 4) + d.y;
  1039. }
  1040. }
  1041. //================================== k-quants
  1042. template<typename dst_t>
  1043. static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1044. const int i = blockIdx.x;
  1045. const block_q2_K * x = (const block_q2_K *) vx;
  1046. const int tid = threadIdx.x;
  1047. #if QK_K == 256
  1048. const int n = tid/32;
  1049. const int l = tid - 32*n;
  1050. const int is = 8*n + l/16;
  1051. const uint8_t q = x[i].qs[32*n + l];
  1052. dst_t * y = yy + i*QK_K + 128*n;
  1053. float dall = __low2half(x[i].dm);
  1054. float dmin = __high2half(x[i].dm);
  1055. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1056. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1057. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  1058. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  1059. #else
  1060. const int is = tid/16; // 0 or 1
  1061. const int il = tid%16; // 0...15
  1062. const uint8_t q = x[i].qs[il] >> (2*is);
  1063. dst_t * y = yy + i*QK_K + 16*is + il;
  1064. float dall = __low2half(x[i].dm);
  1065. float dmin = __high2half(x[i].dm);
  1066. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  1067. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  1068. #endif
  1069. }
  1070. template<typename dst_t>
  1071. static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1072. const int i = blockIdx.x;
  1073. const block_q3_K * x = (const block_q3_K *) vx;
  1074. #if QK_K == 256
  1075. const int r = threadIdx.x/4;
  1076. const int tid = r/2;
  1077. const int is0 = r%2;
  1078. const int l0 = 16*is0 + 4*(threadIdx.x%4);
  1079. const int n = tid / 4;
  1080. const int j = tid - 4*n;
  1081. uint8_t m = 1 << (4*n + j);
  1082. int is = 8*n + 2*j + is0;
  1083. int shift = 2*j;
  1084. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  1085. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  1086. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  1087. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  1088. float d_all = x[i].d;
  1089. float dl = d_all * (us - 32);
  1090. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  1091. const uint8_t * q = x[i].qs + 32*n;
  1092. const uint8_t * hm = x[i].hmask;
  1093. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  1094. #else
  1095. const int tid = threadIdx.x;
  1096. const int is = tid/16; // 0 or 1
  1097. const int il = tid%16; // 0...15
  1098. const int im = il/8; // 0...1
  1099. const int in = il%8; // 0...7
  1100. dst_t * y = yy + i*QK_K + 16*is + il;
  1101. const uint8_t q = x[i].qs[il] >> (2*is);
  1102. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  1103. const float d = (float)x[i].d;
  1104. if (is == 0) {
  1105. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1106. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1107. } else {
  1108. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  1109. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  1110. }
  1111. #endif
  1112. }
  1113. #if QK_K == 256
  1114. static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  1115. if (j < 4) {
  1116. d = q[j] & 63; m = q[j + 4] & 63;
  1117. } else {
  1118. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1119. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1120. }
  1121. }
  1122. #endif
  1123. template<typename dst_t>
  1124. static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1125. const block_q4_K * x = (const block_q4_K *) vx;
  1126. const int i = blockIdx.x;
  1127. #if QK_K == 256
  1128. // assume 32 threads
  1129. const int tid = threadIdx.x;
  1130. const int il = tid/8;
  1131. const int ir = tid%8;
  1132. const int is = 2*il;
  1133. const int n = 4;
  1134. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  1135. const float dall = __low2half(x[i].dm);
  1136. const float dmin = __high2half(x[i].dm);
  1137. const uint8_t * q = x[i].qs + 32*il + n*ir;
  1138. uint8_t sc, m;
  1139. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1140. const float d1 = dall * sc; const float m1 = dmin * m;
  1141. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1142. const float d2 = dall * sc; const float m2 = dmin * m;
  1143. for (int l = 0; l < n; ++l) {
  1144. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  1145. y[l +32] = d2 * (q[l] >> 4) - m2;
  1146. }
  1147. #else
  1148. const int tid = threadIdx.x;
  1149. const uint8_t * q = x[i].qs;
  1150. dst_t * y = yy + i*QK_K;
  1151. const float d = (float)x[i].dm[0];
  1152. const float m = (float)x[i].dm[1];
  1153. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  1154. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  1155. #endif
  1156. }
  1157. template<typename dst_t>
  1158. static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1159. const block_q5_K * x = (const block_q5_K *) vx;
  1160. const int i = blockIdx.x;
  1161. #if QK_K == 256
  1162. // assume 64 threads - this is very slightly better than the one below
  1163. const int tid = threadIdx.x;
  1164. const int il = tid/16; // il is in 0...3
  1165. const int ir = tid%16; // ir is in 0...15
  1166. const int is = 2*il; // is is in 0...6
  1167. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  1168. const float dall = __low2half(x[i].dm);
  1169. const float dmin = __high2half(x[i].dm);
  1170. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  1171. const uint8_t * qh = x[i].qh + 2*ir;
  1172. uint8_t sc, m;
  1173. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  1174. const float d1 = dall * sc; const float m1 = dmin * m;
  1175. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  1176. const float d2 = dall * sc; const float m2 = dmin * m;
  1177. uint8_t hm = 1 << (2*il);
  1178. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  1179. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  1180. hm <<= 1;
  1181. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  1182. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  1183. #else
  1184. const int tid = threadIdx.x;
  1185. const uint8_t q = x[i].qs[tid];
  1186. const int im = tid/8; // 0...3
  1187. const int in = tid%8; // 0...7
  1188. const int is = tid/16; // 0 or 1
  1189. const uint8_t h = x[i].qh[in] >> im;
  1190. const float d = x[i].d;
  1191. dst_t * y = yy + i*QK_K + tid;
  1192. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  1193. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  1194. #endif
  1195. }
  1196. template<typename dst_t>
  1197. static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1198. const block_q6_K * x = (const block_q6_K *) vx;
  1199. const int i = blockIdx.x;
  1200. #if QK_K == 256
  1201. // assume 64 threads - this is very slightly better than the one below
  1202. const int tid = threadIdx.x;
  1203. const int ip = tid/32; // ip is 0 or 1
  1204. const int il = tid - 32*ip; // 0...32
  1205. const int is = 8*ip + il/16;
  1206. dst_t * y = yy + i*QK_K + 128*ip + il;
  1207. const float d = x[i].d;
  1208. const uint8_t * ql = x[i].ql + 64*ip + il;
  1209. const uint8_t qh = x[i].qh[32*ip + il];
  1210. const int8_t * sc = x[i].scales + is;
  1211. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1212. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  1213. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1214. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  1215. #else
  1216. // assume 32 threads
  1217. const int tid = threadIdx.x;
  1218. const int ip = tid/16; // 0 or 1
  1219. const int il = tid - 16*ip; // 0...15
  1220. dst_t * y = yy + i*QK_K + 16*ip + il;
  1221. const float d = x[i].d;
  1222. const uint8_t ql = x[i].ql[16*ip + il];
  1223. const uint8_t qh = x[i].qh[il] >> (2*ip);
  1224. const int8_t * sc = x[i].scales;
  1225. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  1226. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  1227. #endif
  1228. }
  1229. static const __device__ uint64_t iq2xxs_grid[256] = {
  1230. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1231. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  1232. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  1233. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  1234. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  1235. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  1236. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  1237. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  1238. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  1239. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  1240. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  1241. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  1242. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  1243. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  1244. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  1245. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  1246. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  1247. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  1248. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  1249. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  1250. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  1251. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  1252. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  1253. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  1254. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  1255. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  1256. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  1257. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  1258. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  1259. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  1260. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  1261. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  1262. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  1263. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  1264. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  1265. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  1266. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  1267. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  1268. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  1269. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  1270. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  1271. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  1272. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  1273. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  1274. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  1275. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  1276. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  1277. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  1278. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  1279. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  1280. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  1281. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  1282. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  1283. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  1284. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  1285. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  1286. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  1287. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  1288. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  1289. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  1290. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  1291. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  1292. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  1293. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  1294. };
  1295. static const __device__ uint64_t iq2xs_grid[512] = {
  1296. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  1297. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  1298. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  1299. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  1300. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  1301. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  1302. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  1303. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  1304. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  1305. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  1306. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  1307. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  1308. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  1309. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  1310. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  1311. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  1312. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  1313. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  1314. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  1315. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  1316. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  1317. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  1318. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  1319. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  1320. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  1321. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  1322. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  1323. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  1324. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  1325. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  1326. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  1327. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  1328. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  1329. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  1330. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  1331. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  1332. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  1333. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  1334. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  1335. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  1336. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  1337. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  1338. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  1339. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  1340. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  1341. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  1342. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  1343. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  1344. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  1345. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  1346. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  1347. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  1348. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  1349. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  1350. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  1351. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  1352. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  1353. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  1354. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  1355. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  1356. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  1357. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  1358. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  1359. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  1360. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  1361. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  1362. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  1363. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  1364. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  1365. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  1366. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  1367. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  1368. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  1369. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  1370. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  1371. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  1372. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  1373. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  1374. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  1375. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  1376. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  1377. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  1378. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  1379. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  1380. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  1381. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  1382. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  1383. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  1384. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  1385. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  1386. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  1387. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  1388. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  1389. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  1390. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  1391. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  1392. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  1393. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  1394. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  1395. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  1396. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  1397. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  1398. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  1399. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  1400. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  1401. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  1402. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  1403. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  1404. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  1405. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  1406. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  1407. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  1408. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  1409. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  1410. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  1411. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  1412. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  1413. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  1414. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  1415. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  1416. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  1417. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  1418. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  1419. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  1420. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  1421. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  1422. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  1423. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  1424. };
  1425. static const __device__ uint32_t iq3xxs_grid[256] = {
  1426. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
  1427. 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
  1428. 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  1429. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
  1430. 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
  1431. 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  1432. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
  1433. 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
  1434. 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  1435. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
  1436. 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
  1437. 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  1438. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
  1439. 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
  1440. 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  1441. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
  1442. 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
  1443. 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  1444. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
  1445. 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
  1446. 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  1447. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
  1448. 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
  1449. 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  1450. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
  1451. 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
  1452. 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  1453. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
  1454. 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
  1455. 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  1456. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
  1457. 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  1458. };
  1459. static const __device__ uint64_t iq1s_grid[512] = {
  1460. 0xffffffffffff0101, 0xffffffffff01ff00, 0xffffffffff010100, 0xffffffff00000000,
  1461. 0xffffffff01ff00ff, 0xffffffff01ff0001, 0xffffffff0101ffff, 0xffffffff0101ff01,
  1462. 0xffffff00ff000000, 0xffffff000000ff00, 0xffffff00000000ff, 0xffffff0000000100,
  1463. 0xffffff0000010000, 0xffffff0001000000, 0xffffff01ffff00ff, 0xffffff01ff01ff00,
  1464. 0xffffff01ff010100, 0xffffff0100000001, 0xffffff0101ffff00, 0xffffff0101ff0101,
  1465. 0xffffff0101010100, 0xffff00ffff00ff01, 0xffff00ffff0000ff, 0xffff00ff00ff0100,
  1466. 0xffff00ff0100ff00, 0xffff00ff010001ff, 0xffff0000ff0101ff, 0xffff000000ffff00,
  1467. 0xffff000000000000, 0xffff00000001ff01, 0xffff000001000101, 0xffff0000010100ff,
  1468. 0xffff0001ffff0100, 0xffff00010000ff00, 0xffff000100010101, 0xffff000101000000,
  1469. 0xffff01ffffff0000, 0xffff01ffff01ffff, 0xffff01ffff010100, 0xffff01ff00000000,
  1470. 0xffff01ff01ffffff, 0xffff01ff01ff0001, 0xffff01ff0101ffff, 0xffff01ff01010001,
  1471. 0xffff0100ffffff01, 0xffff01000000ffff, 0xffff010000000100, 0xffff010001ff01ff,
  1472. 0xffff010001000000, 0xffff0101ff000000, 0xffff0101000101ff, 0xffff010101ffff01,
  1473. 0xffff01010101ff00, 0xff00ffffff000000, 0xff00ffff00ffff00, 0xff00ffff00000001,
  1474. 0xff00ffff000001ff, 0xff00ffff01010000, 0xff00ff00ffff0000, 0xff00ff00ff00ff00,
  1475. 0xff00ff00ff0000ff, 0xff00ff00ff000100, 0xff00ff00ff010001, 0xff00ff0000ff0001,
  1476. 0xff00ff000000ffff, 0xff00ff0000000000, 0xff00ff000001ff00, 0xff00ff0000010100,
  1477. 0xff00ff0001ff0000, 0xff00ff000100ff00, 0xff00ff0001000100, 0xff00ff01ff000000,
  1478. 0xff00ff0100ff0000, 0xff00ff01000001ff, 0xff00ff0101010001, 0xff0000ff00000000,
  1479. 0xff0000ff0001ff00, 0xff0000ff00010100, 0xff000000ffff0101, 0xff000000ff000000,
  1480. 0xff000000ff01ff00, 0xff00000000ff0000, 0xff0000000000ff00, 0xff000000000000ff,
  1481. 0xff00000000000000, 0xff00000000000001, 0xff00000000000100, 0xff0000000001ffff,
  1482. 0xff00000000010000, 0xff00000001000000, 0xff00000001010100, 0xff000001ff00ff01,
  1483. 0xff000001ff0100ff, 0xff00000100000000, 0xff0000010001ff00, 0xff00000101ff0100,
  1484. 0xff0000010100ff00, 0xff0001ff00ff00ff, 0xff0001ff00000101, 0xff0001ff000100ff,
  1485. 0xff0001ff01000000, 0xff000100ff0001ff, 0xff0001000000ff01, 0xff00010000000000,
  1486. 0xff00010000010001, 0xff00010000010100, 0xff00010001ffff00, 0xff00010001ff0101,
  1487. 0xff00010001010000, 0xff000101ffffffff, 0xff000101ff000101, 0xff00010101ff00ff,
  1488. 0xff00010101000001, 0xff000101010100ff, 0xff01ffffff000101, 0xff01ffffff01ffff,
  1489. 0xff01ffffff01ff01, 0xff01ffffff0101ff, 0xff01ffff00000000, 0xff01ffff01ff0001,
  1490. 0xff01ffff0101ff01, 0xff01ff00ff000000, 0xff01ff0000ff0100, 0xff01ff000000ff01,
  1491. 0xff01ff0000010000, 0xff01ff00010000ff, 0xff01ff01ff01ff00, 0xff01ff0100000101,
  1492. 0xff0100ffffff0000, 0xff0100ffff010000, 0xff0100ff01ff00ff, 0xff0100ff01000100,
  1493. 0xff0100ff010100ff, 0xff010000ffffff01, 0xff01000000000000, 0xff0100000101ff00,
  1494. 0xff010001ffff00ff, 0xff010001ff000100, 0xff01000100ffff00, 0xff01000100010001,
  1495. 0xff01000101ff0001, 0xff010001010001ff, 0xff0101ffffffffff, 0xff0101ffff01ffff,
  1496. 0xff0101ffff010101, 0xff0101ff0000ff00, 0xff0101ff01010001, 0xff010100ff000000,
  1497. 0xff010100ff01ff01, 0xff01010000ff0001, 0xff01010000000100, 0xff01010001000000,
  1498. 0xff0101010100ffff, 0x00ffffff0000ff01, 0x00ffffff000000ff, 0x00ffffff00000100,
  1499. 0x00ffffff00010000, 0x00ffff00ffff0001, 0x00ffff00ff0000ff, 0x00ffff00ff000100,
  1500. 0x00ffff0000000000, 0x00ffff0001000100, 0x00ffff0001010001, 0x00ffff01ff00ff01,
  1501. 0x00ffff0100ff0100, 0x00ffff010000ff00, 0x00ffff01000100ff, 0x00ffff0101ff00ff,
  1502. 0x00ffff010101ff00, 0x00ff00ffffffffff, 0x00ff00ffffff01ff, 0x00ff00ffff000101,
  1503. 0x00ff00ff00000000, 0x00ff00ff000101ff, 0x00ff00ff01010101, 0x00ff0000ff000000,
  1504. 0x00ff0000ff01ffff, 0x00ff000000ff0000, 0x00ff00000000ff00, 0x00ff0000000000ff,
  1505. 0x00ff000000000000, 0x00ff000000000001, 0x00ff000000000100, 0x00ff000000010000,
  1506. 0x00ff000001ffff01, 0x00ff000001000000, 0x00ff0001ff000101, 0x00ff000100ffffff,
  1507. 0x00ff000100000000, 0x00ff0001010001ff, 0x00ff01ffff000000, 0x00ff01ff0001ff00,
  1508. 0x00ff01ff01ff0100, 0x00ff0100ff01ff01, 0x00ff010000ff00ff, 0x00ff010000ff0101,
  1509. 0x00ff010000000000, 0x00ff010000010101, 0x00ff01000100ff00, 0x00ff010001010000,
  1510. 0x00ff0101ffffff00, 0x00ff01010000ff01, 0x00ff010100000100, 0x00ff010101ff0000,
  1511. 0x0000ffffffff0100, 0x0000ffffff00ff00, 0x0000ffffff0000ff, 0x0000ffffff010000,
  1512. 0x0000ffff00000000, 0x0000ffff00010101, 0x0000ffff01ffff01, 0x0000ffff01000100,
  1513. 0x0000ff00ff000000, 0x0000ff00ff01ff00, 0x0000ff00ff0101ff, 0x0000ff0000ff0000,
  1514. 0x0000ff000000ff00, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
  1515. 0x0000ff0000000100, 0x0000ff0000010000, 0x0000ff0001ffffff, 0x0000ff0001ff01ff,
  1516. 0x0000ff0001000000, 0x0000ff000101ffff, 0x0000ff01ffff0101, 0x0000ff01ff010000,
  1517. 0x0000ff0100000000, 0x0000ff0101000101, 0x000000ffffff0001, 0x000000ffff000000,
  1518. 0x000000ff00ff0000, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
  1519. 0x000000ff00000001, 0x000000ff00000100, 0x000000ff00010000, 0x000000ff01000000,
  1520. 0x000000ff0101ff00, 0x00000000ffff0000, 0x00000000ff00ff00, 0x00000000ff0000ff,
  1521. 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff010000,
  1522. 0x0000000000ffff00, 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001,
  1523. 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
  1524. 0x00000000000000ff, 0x0000000000000001, 0x00000000000001ff, 0x0000000000000100,
  1525. 0x0000000000000101, 0x000000000001ff00, 0x00000000000100ff, 0x0000000000010000,
  1526. 0x0000000000010001, 0x0000000000010100, 0x0000000001ff0000, 0x000000000100ff00,
  1527. 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001, 0x0000000001000100,
  1528. 0x0000000001010000, 0x00000001ffff01ff, 0x00000001ff000000, 0x0000000100ff0000,
  1529. 0x000000010000ff00, 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001,
  1530. 0x0000000100000100, 0x0000000100010000, 0x0000000101000000, 0x000001ffff00ff00,
  1531. 0x000001ffff010001, 0x000001ffff0101ff, 0x000001ff00ffff01, 0x000001ff0000ffff,
  1532. 0x000001ff00000000, 0x000001ff010000ff, 0x000001ff01010100, 0x00000100ffff0100,
  1533. 0x00000100ff000000, 0x0000010000ff0000, 0x000001000000ff00, 0x00000100000000ff,
  1534. 0x0000010000000000, 0x0000010000000001, 0x0000010000000100, 0x0000010000010000,
  1535. 0x0000010001000000, 0x000001000101ff01, 0x00000101ffff0001, 0x00000101ff01ffff,
  1536. 0x0000010100000000, 0x0000010101010100, 0x0001ffffff000000, 0x0001ffff00ffffff,
  1537. 0x0001ffff00000100, 0x0001ffff0001ff00, 0x0001ffff01000000, 0x0001ff00ffffff00,
  1538. 0x0001ff00ffff01ff, 0x0001ff00ff010000, 0x0001ff0000000000, 0x0001ff0000010001,
  1539. 0x0001ff0001ff0000, 0x0001ff0001010100, 0x0001ff01ff0000ff, 0x0001ff01ff000001,
  1540. 0x0001ff0100ffffff, 0x0001ff010001ffff, 0x0001ff01000101ff, 0x0001ff010100ff01,
  1541. 0x000100ffff00ffff, 0x000100ffff00ff01, 0x000100ffff000100, 0x000100ff00000000,
  1542. 0x000100ff000101ff, 0x000100ff01ff0101, 0x000100ff0100ffff, 0x000100ff01010101,
  1543. 0x00010000ff000000, 0x00010000ff010100, 0x0001000000ff0000, 0x000100000000ff00,
  1544. 0x00010000000000ff, 0x0001000000000000, 0x0001000000000001, 0x0001000000000100,
  1545. 0x0001000000010000, 0x0001000001ffff01, 0x0001000001000000, 0x0001000100ff0101,
  1546. 0x0001000100000000, 0x00010001010100ff, 0x000101ffffff01ff, 0x000101ffffff0101,
  1547. 0x000101ff00010000, 0x000101ff01ff0000, 0x000101ff0100ff01, 0x00010100ffff0000,
  1548. 0x0001010000000000, 0x000101000001ffff, 0x0001010000010101, 0x00010100010001ff,
  1549. 0x00010101ff00ff00, 0x00010101ff010001, 0x0001010100ffffff, 0x0001010100ff01ff,
  1550. 0x00010101000101ff, 0x0001010101ff0000, 0x000101010100ff01, 0x0001010101000101,
  1551. 0x01ffffffffff0101, 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff,
  1552. 0x01ffffffff010101, 0x01ffffff00000000, 0x01ffffff01ff01ff, 0x01ffffff01000101,
  1553. 0x01ffffff0101ff01, 0x01ffffff010100ff, 0x01ffff000000ff00, 0x01ffff0000000001,
  1554. 0x01ffff00000001ff, 0x01ffff0000010000, 0x01ffff0001ff0000, 0x01ffff01ffffffff,
  1555. 0x01ffff01ffff01ff, 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff0101ff,
  1556. 0x01ffff010100ffff, 0x01ff00ffffff0000, 0x01ff00ffff010000, 0x01ff00ff00ffff01,
  1557. 0x01ff0000ff0000ff, 0x01ff000000000000, 0x01ff00000001ff01, 0x01ff000001ffffff,
  1558. 0x01ff000001010100, 0x01ff0001ffffff01, 0x01ff0001ff010001, 0x01ff000101ff0100,
  1559. 0x01ff000101000001, 0x01ff0001010100ff, 0x01ff01ffff00ffff, 0x01ff01ff00010001,
  1560. 0x01ff01ff01000000, 0x01ff01ff010101ff, 0x01ff0100ff000001, 0x01ff010000ffff00,
  1561. 0x01ff010000000100, 0x01ff010001ff01ff, 0x01ff01000101ffff, 0x01ff0101ffff00ff,
  1562. 0x01ff0101ffff0101, 0x01ff0101ff0101ff, 0x01ff010100010000, 0x0100ffff00ff00ff,
  1563. 0x0100ffff00ff0001, 0x0100ffff00000100, 0x0100ffff0100ff00, 0x0100ff00ffff0000,
  1564. 0x0100ff00ff00ffff, 0x0100ff00ff00ff01, 0x0100ff00ff000100, 0x0100ff00ff010000,
  1565. 0x0100ff0000000000, 0x0100ff00000100ff, 0x0100ff0001ff0101, 0x0100ff0001010101,
  1566. 0x0100ff0100ff00ff, 0x0100ff0100ff0001, 0x0100ff0100000100, 0x0100ff0100010001,
  1567. 0x0100ff0101000000, 0x010000ffff00ff00, 0x010000ff0000ffff, 0x010000ff00000000,
  1568. 0x010000ff010001ff, 0x010000ff01010001, 0x01000000ffffff00, 0x01000000ffff0101,
  1569. 0x01000000ff000000, 0x01000000ff0100ff, 0x01000000ff010101, 0x0100000000ff0000,
  1570. 0x010000000000ff00, 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001,
  1571. 0x0100000000000100, 0x0100000000010000, 0x0100000001000000, 0x0100000100000000,
  1572. 0x01000001000101ff, 0x0100000101ffff01, 0x010001ffff000101, 0x010001ff00ff0100,
  1573. 0x010001ff0000ff00, 0x010001ff000100ff, 0x010001ff01ffffff, 0x01000100ffff0000,
  1574. 0x01000100ff0001ff, 0x0100010000000000, 0x010001000001ff00, 0x0100010001ff0000,
  1575. 0x01000100010000ff, 0x0100010001000101, 0x01000101ff00ff01, 0x0100010100ff0100,
  1576. 0x010001010000ffff, 0x0100010101010001, 0x0101ffffffff0101, 0x0101ffffff0001ff,
  1577. 0x0101ffffff01ffff, 0x0101ffffff010101, 0x0101ffff00000000, 0x0101ffff0101ffff,
  1578. 0x0101ffff010101ff, 0x0101ff00ff000000, 0x0101ff0000ff0100, 0x0101ff000000ff00,
  1579. 0x0101ff0000010000, 0x0101ff00010000ff, 0x0101ff0001000001, 0x0101ff01ff010101,
  1580. 0x0101ff0100000000, 0x0101ff010101ff00, 0x010100ffffff0000, 0x010100ffff010000,
  1581. 0x010100ff00ff01ff, 0x010100ff000000ff, 0x010100ff00000101, 0x010100ff01ffff00,
  1582. 0x01010000ffffff01, 0x01010000ff000100, 0x01010000ff01ff01, 0x0101000000000000,
  1583. 0x01010000000100ff, 0x010100000101ff01, 0x01010001ffff0000, 0x01010001ff00ffff,
  1584. 0x01010001ff010000, 0x0101000101ffffff, 0x0101000101ff01ff, 0x0101000101010101,
  1585. 0x010101ffff01ffff, 0x010101ff00000000, 0x010101ff0001ff01, 0x010101ff0101ffff,
  1586. 0x010101ff010101ff, 0x01010100ffffffff, 0x01010100ff000001, 0x010101000000ff00,
  1587. 0x0101010001010000, 0x0101010100ff0001, 0x010101010001ff01, 0x010101010101ffff,
  1588. };
  1589. static const __device__ uint8_t ksigns_iq2xs[128] = {
  1590. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  1591. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  1592. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  1593. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  1594. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  1595. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  1596. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  1597. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  1598. };
  1599. //#if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  1600. static const __device__ uint64_t ksigns64[128] = {
  1601. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,
  1602. 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,
  1603. 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  1604. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,
  1605. 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,
  1606. 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  1607. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,
  1608. 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,
  1609. 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  1610. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,
  1611. 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,
  1612. 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  1613. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,
  1614. 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,
  1615. 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  1616. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,
  1617. 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,
  1618. 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  1619. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,
  1620. 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,
  1621. 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  1622. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,
  1623. 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,
  1624. 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  1625. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,
  1626. 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,
  1627. 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  1628. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,
  1629. 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,
  1630. 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  1631. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,
  1632. 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,
  1633. };
  1634. //#endif
  1635. static const __device__ uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  1636. inline bool ggml_cuda_supports_mmq(enum ggml_type type) {
  1637. switch (type) {
  1638. case GGML_TYPE_Q4_0:
  1639. case GGML_TYPE_Q4_1:
  1640. case GGML_TYPE_Q5_0:
  1641. case GGML_TYPE_Q5_1:
  1642. case GGML_TYPE_Q8_0:
  1643. case GGML_TYPE_Q2_K:
  1644. case GGML_TYPE_Q3_K:
  1645. case GGML_TYPE_Q4_K:
  1646. case GGML_TYPE_Q5_K:
  1647. case GGML_TYPE_Q6_K:
  1648. return true;
  1649. default:
  1650. return false;
  1651. }
  1652. }
  1653. template<typename dst_t>
  1654. static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1655. const int i = blockIdx.x;
  1656. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  1657. const int tid = threadIdx.x;
  1658. #if QK_K == 256
  1659. const int il = tid/8; // 0...3
  1660. const int ib = tid%8; // 0...7
  1661. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1662. const uint16_t * q2 = x[i].qs + 4*ib;
  1663. const uint8_t * aux8 = (const uint8_t *)q2;
  1664. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
  1665. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  1666. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  1667. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1668. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1669. #else
  1670. assert(false);
  1671. #endif
  1672. }
  1673. template<typename dst_t>
  1674. static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1675. const int i = blockIdx.x;
  1676. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  1677. const int tid = threadIdx.x;
  1678. #if QK_K == 256
  1679. const int il = tid/8; // 0...3
  1680. const int ib = tid%8; // 0...7
  1681. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1682. const uint16_t * q2 = x[i].qs + 4*ib;
  1683. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  1684. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  1685. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  1686. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1687. #else
  1688. assert(false);
  1689. #endif
  1690. }
  1691. template<typename dst_t>
  1692. static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1693. const int i = blockIdx.x;
  1694. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  1695. const int tid = threadIdx.x;
  1696. #if QK_K == 256
  1697. const int il = tid/8; // 0...3
  1698. const int ib = tid%8; // 0...7
  1699. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1700. const uint8_t * q3 = x[i].qs + 8*ib;
  1701. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  1702. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  1703. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  1704. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  1705. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  1706. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  1707. for (int j = 0; j < 4; ++j) {
  1708. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1709. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1710. }
  1711. #else
  1712. assert(false);
  1713. #endif
  1714. }
  1715. template<typename dst_t>
  1716. static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
  1717. const int i = blockIdx.x;
  1718. const block_iq1_s * x = (const block_iq1_s *) vx;
  1719. const int tid = threadIdx.x;
  1720. #if QK_K == 256
  1721. const int il = tid/8; // 0...3
  1722. const int ib = tid%8; // 0...7
  1723. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  1724. const int i8 = 4*ib+il;
  1725. uint8_t h = x[i].scales[i8/2] >> 4*(i8%2);
  1726. const int8_t * grid = (const int8_t *)(iq1s_grid + (x[i].qs[i8] | ((h & 8) << 5)));
  1727. const float d = (float)x[i].d * (2*(h & 7) + 1);
  1728. for (int j = 0; j < 8; ++j) y[j] = d * grid[j];
  1729. #else
  1730. assert(false);
  1731. #endif
  1732. }
  1733. 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) {
  1734. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  1735. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1736. if (row > nrows) return;
  1737. const int num_blocks_per_row = ncols / QK_K;
  1738. const int ib0 = row*num_blocks_per_row;
  1739. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  1740. float tmp = 0; // partial sum for thread in warp
  1741. #if QK_K == 256
  1742. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  1743. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1744. const int step = 16/K_QUANTS_PER_ITERATION;
  1745. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1746. const int in = tid - step*im; // 0...15 or 0...7
  1747. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  1748. const int q_offset = 32*im + l0;
  1749. const int s_offset = 8*im;
  1750. const int y_offset = 128*im + l0;
  1751. uint32_t aux[4];
  1752. const uint8_t * d = (const uint8_t *)aux;
  1753. const uint8_t * m = (const uint8_t *)(aux + 2);
  1754. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1755. const float * y = yy + i * QK_K + y_offset;
  1756. const uint8_t * q = x[i].qs + q_offset;
  1757. const float dall = __low2half(x[i].dm);
  1758. const float dmin = __high2half(x[i].dm);
  1759. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  1760. aux[0] = a[0] & 0x0f0f0f0f;
  1761. aux[1] = a[1] & 0x0f0f0f0f;
  1762. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  1763. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  1764. float sum1 = 0, sum2 = 0;
  1765. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1766. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  1767. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  1768. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  1769. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  1770. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  1771. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  1772. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  1773. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  1774. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  1775. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  1776. }
  1777. tmp += dall * sum1 - dmin * sum2;
  1778. }
  1779. #else
  1780. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1781. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1782. const int offset = tid * K_QUANTS_PER_ITERATION;
  1783. uint32_t uaux[2];
  1784. const uint8_t * d = (const uint8_t *)uaux;
  1785. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1786. const float * y = yy + i * QK_K + offset;
  1787. const uint8_t * q = x[i].qs + offset;
  1788. const uint32_t * s = (const uint32_t *)x[i].scales;
  1789. uaux[0] = s[0] & 0x0f0f0f0f;
  1790. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  1791. const float2 dall = __half22float2(x[i].dm);
  1792. float sum1 = 0, sum2 = 0;
  1793. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1794. const uint8_t ql = q[l];
  1795. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  1796. + y[l+16] * d[1] * ((ql >> 2) & 3)
  1797. + y[l+32] * d[2] * ((ql >> 4) & 3)
  1798. + y[l+48] * d[3] * ((ql >> 6) & 3);
  1799. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  1800. }
  1801. tmp += dall.x * sum1 - dall.y * sum2;
  1802. }
  1803. #endif
  1804. // sum up partial sums and write back result
  1805. #pragma unroll
  1806. for (int mask = 16; mask > 0; mask >>= 1) {
  1807. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1808. }
  1809. if (threadIdx.x == 0) {
  1810. dst[row] = tmp;
  1811. }
  1812. }
  1813. 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) {
  1814. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1815. if (row > nrows) return;
  1816. const int num_blocks_per_row = ncols / QK_K;
  1817. const int ib0 = row*num_blocks_per_row;
  1818. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  1819. float tmp = 0; // partial sum for thread in warp
  1820. #if QK_K == 256
  1821. const uint16_t kmask1 = 0x0303;
  1822. const uint16_t kmask2 = 0x0f0f;
  1823. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1824. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1825. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  1826. const int step = 16/K_QUANTS_PER_ITERATION;
  1827. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  1828. const int in = tid - step*im; // 0....15 or 0...7
  1829. const uint8_t m = 1 << (4*im);
  1830. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  1831. const int q_offset = 32*im + l0;
  1832. const int y_offset = 128*im + l0;
  1833. uint16_t utmp[4];
  1834. const int8_t * s = (const int8_t *)utmp;
  1835. const uint16_t s_shift = 4*im;
  1836. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1837. const float * y = yy + i * QK_K + y_offset;
  1838. const uint8_t * q = x[i].qs + q_offset;
  1839. const uint8_t * h = x[i].hmask + l0;
  1840. const uint16_t * a = (const uint16_t *)x[i].scales;
  1841. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  1842. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  1843. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  1844. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  1845. const float d = x[i].d;
  1846. float sum = 0;
  1847. for (int l = 0; l < n; ++l) {
  1848. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  1849. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  1850. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  1851. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  1852. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  1853. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  1854. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  1855. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  1856. }
  1857. tmp += d * sum;
  1858. }
  1859. #else
  1860. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  1861. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  1862. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  1863. const int in = offset/8; // 0 or 1
  1864. const int im = offset%8; // 0...7
  1865. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1866. const float * y = yy + i * QK_K + offset;
  1867. const uint8_t * q = x[i].qs + offset;
  1868. const uint8_t * s = x[i].scales;
  1869. const float dall = (float)x[i].d;
  1870. float sum = 0;
  1871. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  1872. const uint8_t hl = x[i].hmask[im+l] >> in;
  1873. const uint8_t ql = q[l];
  1874. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  1875. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  1876. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  1877. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  1878. }
  1879. tmp += sum;
  1880. }
  1881. #endif
  1882. // sum up partial sums and write back result
  1883. #pragma unroll
  1884. for (int mask = 16; mask > 0; mask >>= 1) {
  1885. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1886. }
  1887. if (threadIdx.x == 0) {
  1888. dst[row] = tmp;
  1889. }
  1890. }
  1891. 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) {
  1892. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  1893. if (row > nrows) return;
  1894. const int num_blocks_per_row = ncols / QK_K;
  1895. const int ib0 = row*num_blocks_per_row;
  1896. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  1897. #if QK_K == 256
  1898. const uint16_t kmask1 = 0x3f3f;
  1899. const uint16_t kmask2 = 0x0f0f;
  1900. const uint16_t kmask3 = 0xc0c0;
  1901. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  1902. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
  1903. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  1904. const int il = tid/step; // 0...3
  1905. const int ir = tid - step*il; // 0...7 or 0...3
  1906. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  1907. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  1908. const int in = il%2;
  1909. const int l0 = n*(2*ir + in);
  1910. const int q_offset = 32*im + l0;
  1911. const int y_offset = 64*im + l0;
  1912. uint16_t aux[4];
  1913. const uint8_t * sc = (const uint8_t *)aux;
  1914. #if K_QUANTS_PER_ITERATION == 2
  1915. uint32_t q32[4];
  1916. const uint8_t * q4 = (const uint8_t *)q32;
  1917. #else
  1918. uint16_t q16[4];
  1919. const uint8_t * q4 = (const uint8_t *)q16;
  1920. #endif
  1921. float tmp = 0; // partial sum for thread in warp
  1922. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  1923. const float * y1 = yy + i*QK_K + y_offset;
  1924. const float * y2 = y1 + 128;
  1925. const float dall = __low2half(x[i].dm);
  1926. const float dmin = __high2half(x[i].dm);
  1927. const uint16_t * a = (const uint16_t *)x[i].scales;
  1928. aux[0] = a[im+0] & kmask1;
  1929. aux[1] = a[im+2] & kmask1;
  1930. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  1931. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  1932. #if K_QUANTS_PER_ITERATION == 2
  1933. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  1934. const uint32_t * q2 = q1 + 16;
  1935. q32[0] = q1[0] & 0x0f0f0f0f;
  1936. q32[1] = q1[0] & 0xf0f0f0f0;
  1937. q32[2] = q2[0] & 0x0f0f0f0f;
  1938. q32[3] = q2[0] & 0xf0f0f0f0;
  1939. float4 s = {0.f, 0.f, 0.f, 0.f};
  1940. float smin = 0;
  1941. for (int l = 0; l < 4; ++l) {
  1942. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
  1943. s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
  1944. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1945. }
  1946. 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;
  1947. #else
  1948. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  1949. const uint16_t * q2 = q1 + 32;
  1950. q16[0] = q1[0] & 0x0f0f;
  1951. q16[1] = q1[0] & 0xf0f0;
  1952. q16[2] = q2[0] & 0x0f0f;
  1953. q16[3] = q2[0] & 0xf0f0;
  1954. float4 s = {0.f, 0.f, 0.f, 0.f};
  1955. float smin = 0;
  1956. for (int l = 0; l < 2; ++l) {
  1957. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  1958. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  1959. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  1960. }
  1961. 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;
  1962. #endif
  1963. }
  1964. #else
  1965. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  1966. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  1967. const int step = tid * K_QUANTS_PER_ITERATION;
  1968. uint16_t aux16[2];
  1969. const uint8_t * s = (const uint8_t *)aux16;
  1970. float tmp = 0;
  1971. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  1972. const uint8_t * q = x[i].qs + step;
  1973. const float * y = yy + i*QK_K + step;
  1974. const uint16_t * a = (const uint16_t *)x[i].scales;
  1975. aux16[0] = a[0] & 0x0f0f;
  1976. aux16[1] = (a[0] >> 4) & 0x0f0f;
  1977. const float d = (float)x[i].dm[0];
  1978. const float m = (float)x[i].dm[1];
  1979. float sum = 0.f;
  1980. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  1981. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  1982. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  1983. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  1984. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  1985. }
  1986. tmp += sum;
  1987. }
  1988. #endif
  1989. // sum up partial sums and write back result
  1990. #pragma unroll
  1991. for (int mask = 16; mask > 0; mask >>= 1) {
  1992. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  1993. }
  1994. if (tid == 0) {
  1995. dst[row] = tmp;
  1996. }
  1997. }
  1998. static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
  1999. const int row = blockIdx.x;
  2000. const int num_blocks_per_row = ncols / QK_K;
  2001. const int ib0 = row*num_blocks_per_row;
  2002. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  2003. float tmp = 0; // partial sum for thread in warp
  2004. #if QK_K == 256
  2005. const uint16_t kmask1 = 0x3f3f;
  2006. const uint16_t kmask2 = 0x0f0f;
  2007. const uint16_t kmask3 = 0xc0c0;
  2008. const int tid = threadIdx.x/2; // 0...15
  2009. const int ix = threadIdx.x%2;
  2010. const int il = tid/4; // 0...3
  2011. const int ir = tid - 4*il;// 0...3
  2012. const int n = 2;
  2013. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  2014. const int in = il%2;
  2015. const int l0 = n*(2*ir + in);
  2016. const int q_offset = 32*im + l0;
  2017. const int y_offset = 64*im + l0;
  2018. const uint8_t hm1 = 1 << (2*im);
  2019. const uint8_t hm2 = hm1 << 4;
  2020. uint16_t aux[4];
  2021. const uint8_t * sc = (const uint8_t *)aux;
  2022. uint16_t q16[8];
  2023. const uint8_t * q4 = (const uint8_t *)q16;
  2024. for (int i = ix; i < num_blocks_per_row; i += 2) {
  2025. const uint8_t * ql1 = x[i].qs + q_offset;
  2026. const uint8_t * qh = x[i].qh + l0;
  2027. const float * y1 = yy + i*QK_K + y_offset;
  2028. const float * y2 = y1 + 128;
  2029. const float dall = __low2half(x[i].dm);
  2030. const float dmin = __high2half(x[i].dm);
  2031. const uint16_t * a = (const uint16_t *)x[i].scales;
  2032. aux[0] = a[im+0] & kmask1;
  2033. aux[1] = a[im+2] & kmask1;
  2034. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  2035. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  2036. float4 sum = {0.f, 0.f, 0.f, 0.f};
  2037. float smin = 0;
  2038. const uint16_t * q1 = (const uint16_t *)ql1;
  2039. const uint16_t * q2 = q1 + 32;
  2040. q16[0] = q1[0] & 0x0f0f;
  2041. q16[1] = q1[8] & 0x0f0f;
  2042. q16[2] = (q1[0] >> 4) & 0x0f0f;
  2043. q16[3] = (q1[8] >> 4) & 0x0f0f;
  2044. q16[4] = q2[0] & 0x0f0f;
  2045. q16[5] = q2[8] & 0x0f0f;
  2046. q16[6] = (q2[0] >> 4) & 0x0f0f;
  2047. q16[7] = (q2[8] >> 4) & 0x0f0f;
  2048. for (int l = 0; l < n; ++l) {
  2049. sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  2050. + y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  2051. sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  2052. + y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  2053. sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  2054. + y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  2055. sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  2056. + y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  2057. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  2058. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  2059. }
  2060. tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  2061. }
  2062. #else
  2063. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  2064. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  2065. const int step = tid * K_QUANTS_PER_ITERATION;
  2066. const int im = step/8;
  2067. const int in = step%8;
  2068. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2069. const uint8_t * q = x[i].qs + step;
  2070. const int8_t * s = x[i].scales;
  2071. const float * y = yy + i*QK_K + step;
  2072. const float d = x[i].d;
  2073. float sum = 0.f;
  2074. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2075. const uint8_t h = x[i].qh[in+j] >> im;
  2076. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  2077. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  2078. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  2079. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  2080. }
  2081. tmp += sum;
  2082. }
  2083. #endif
  2084. // sum up partial sums and write back result
  2085. #pragma unroll
  2086. for (int mask = 16; mask > 0; mask >>= 1) {
  2087. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2088. }
  2089. if (threadIdx.x == 0) {
  2090. dst[row] = tmp;
  2091. }
  2092. }
  2093. 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) {
  2094. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  2095. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  2096. if (row > nrows) return;
  2097. const int num_blocks_per_row = ncols / QK_K;
  2098. const int ib0 = row*num_blocks_per_row;
  2099. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  2100. #if QK_K == 256
  2101. const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  2102. const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  2103. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  2104. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  2105. const int in = tid - step*im; // 0...15 or 0...7
  2106. #if K_QUANTS_PER_ITERATION == 1
  2107. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  2108. const int is = 0;
  2109. #else
  2110. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  2111. const int is = in / 4;
  2112. #endif
  2113. const int ql_offset = 64*im + l0;
  2114. const int qh_offset = 32*im + l0;
  2115. const int s_offset = 8*im + is;
  2116. const int y_offset = 128*im + l0;
  2117. float tmp = 0; // partial sum for thread in warp
  2118. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  2119. const float * y = yy + i * QK_K + y_offset;
  2120. const uint8_t * ql = x[i].ql + ql_offset;
  2121. const uint8_t * qh = x[i].qh + qh_offset;
  2122. const int8_t * s = x[i].scales + s_offset;
  2123. const float d = x[i].d;
  2124. #if K_QUANTS_PER_ITERATION == 1
  2125. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  2126. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  2127. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  2128. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  2129. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  2130. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  2131. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  2132. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  2133. tmp += sum;
  2134. #else
  2135. float sum = 0;
  2136. for (int l = 0; l < 4; ++l) {
  2137. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  2138. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  2139. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  2140. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  2141. }
  2142. tmp += sum;
  2143. #endif
  2144. }
  2145. #else
  2146. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  2147. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  2148. const int step = tid * K_QUANTS_PER_ITERATION;
  2149. float tmp = 0; // partial sum for thread in warp
  2150. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  2151. const float * y = yy + i * QK_K + step;
  2152. const uint8_t * ql = x[i].ql + step;
  2153. const uint8_t * qh = x[i].qh + step;
  2154. const int8_t * s = x[i].scales;
  2155. const float d = x[i+0].d;
  2156. float sum = 0;
  2157. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  2158. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  2159. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  2160. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  2161. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  2162. }
  2163. tmp += sum;
  2164. }
  2165. #endif
  2166. // sum up partial sums and write back result
  2167. #pragma unroll
  2168. for (int mask = 16; mask > 0; mask >>= 1) {
  2169. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  2170. }
  2171. if (tid == 0) {
  2172. dst[row] = tmp;
  2173. }
  2174. }
  2175. static __device__ void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  2176. const half * x = (const half *) vx;
  2177. // automatic half -> float type cast if dfloat == float
  2178. v.x = x[ib + iqs + 0];
  2179. v.y = x[ib + iqs + 1];
  2180. }
  2181. static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded) {
  2182. const int ix = blockDim.x*blockIdx.x + threadIdx.x;
  2183. if (ix >= kx_padded) {
  2184. return;
  2185. }
  2186. const int iy = blockDim.y*blockIdx.y + threadIdx.y;
  2187. const int i_padded = iy*kx_padded + ix;
  2188. block_q8_1 * y = (block_q8_1 *) vy;
  2189. const int ib = i_padded / QK8_1; // block index
  2190. const int iqs = i_padded % QK8_1; // quant index
  2191. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  2192. float amax = fabsf(xi);
  2193. float sum = xi;
  2194. #pragma unroll
  2195. for (int mask = 16; mask > 0; mask >>= 1) {
  2196. amax = fmaxf(amax, __shfl_xor_sync(0xffffffff, amax, mask, 32));
  2197. sum += __shfl_xor_sync(0xffffffff, sum, mask, 32);
  2198. }
  2199. const float d = amax / 127;
  2200. const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
  2201. y[ib].qs[iqs] = q;
  2202. if (iqs > 0) {
  2203. return;
  2204. }
  2205. reinterpret_cast<half&>(y[ib].ds.x) = d;
  2206. reinterpret_cast<half&>(y[ib].ds.y) = sum;
  2207. }
  2208. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2209. static __global__ void k_get_rows(
  2210. const void * src0, const int32_t * src1, dst_t * dst,
  2211. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2212. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2213. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2214. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2215. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2216. const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
  2217. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2218. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2219. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2220. if (i00 >= ne00) {
  2221. return;
  2222. }
  2223. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2224. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2225. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  2226. const int ib = i00/qk; // block index
  2227. const int iqs = (i00%qk)/qr; // quant index
  2228. const int iybs = i00 - i00%qk; // dst block start index
  2229. const int y_offset = qr == 1 ? 1 : qk/2;
  2230. // dequantize
  2231. dfloat2 v;
  2232. dequantize_kernel(src0_row, ib, iqs, v);
  2233. dst_row[iybs + iqs + 0] = v.x;
  2234. dst_row[iybs + iqs + y_offset] = v.y;
  2235. }
  2236. template<typename src0_t, typename dst_t>
  2237. static __global__ void k_get_rows_float(
  2238. const src0_t * src0, const int32_t * src1, dst_t * dst,
  2239. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  2240. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  2241. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  2242. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  2243. size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
  2244. const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
  2245. const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
  2246. const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
  2247. const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
  2248. if (i00 >= ne00) {
  2249. return;
  2250. }
  2251. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  2252. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  2253. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  2254. dst_row[i00] = src0_row[i00];
  2255. }
  2256. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  2257. static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2258. const int i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
  2259. if (i >= k) {
  2260. return;
  2261. }
  2262. const int ib = i/qk; // block index
  2263. const int iqs = (i%qk)/qr; // quant index
  2264. const int iybs = i - i%qk; // y block start index
  2265. const int y_offset = qr == 1 ? 1 : qk/2;
  2266. // dequantize
  2267. dfloat2 v;
  2268. dequantize_kernel(vx, ib, iqs, v);
  2269. y[iybs + iqs + 0] = v.x;
  2270. y[iybs + iqs + y_offset] = v.y;
  2271. }
  2272. template <typename src_t, typename dst_t>
  2273. static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k) {
  2274. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  2275. if (i >= k) {
  2276. return;
  2277. }
  2278. const src_t * x = (src_t *) vx;
  2279. y[i] = x[i];
  2280. }
  2281. template <bool need_check>
  2282. static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int k) {
  2283. #if __CUDA_ARCH__ >= CC_PASCAL
  2284. constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
  2285. const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
  2286. const int * x0 = ((int *) vx) + blockIdx.x * nint;
  2287. half2 * y2 = (half2 *) (y + i0);
  2288. __shared__ int vals[nint];
  2289. #pragma unroll
  2290. for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
  2291. if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
  2292. break;
  2293. }
  2294. const int ix = ix0 + threadIdx.x;
  2295. vals[ix] = x0[ix];
  2296. }
  2297. #pragma unroll
  2298. for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
  2299. if (need_check && i0 + iy + 2*threadIdx.x >= k) {
  2300. return;
  2301. }
  2302. const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
  2303. const half d = *b0;
  2304. const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
  2305. y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
  2306. }
  2307. #else
  2308. (void) vx; (void) y; (void) k;
  2309. NO_DEVICE_CODE;
  2310. #endif // __CUDA_ARCH__ >= CC_PASCAL
  2311. }
  2312. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  2313. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  2314. #define VDR_Q4_0_Q8_1_MMVQ 2
  2315. #define VDR_Q4_0_Q8_1_MMQ 4
  2316. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(
  2317. const int * v, const int * u, const float & d4, const half2 & ds8) {
  2318. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2319. int sumi = 0;
  2320. #pragma unroll
  2321. for (int i = 0; i < vdr; ++i) {
  2322. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2323. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2324. // SIMD dot product of quantized values
  2325. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2326. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2327. }
  2328. const float2 ds8f = __half22float2(ds8);
  2329. // second part effectively subtracts 8 from each quant value
  2330. return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);
  2331. #else
  2332. NO_DEVICE_CODE;
  2333. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2334. }
  2335. #define VDR_Q4_1_Q8_1_MMVQ 2
  2336. #define VDR_Q4_1_Q8_1_MMQ 4
  2337. template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(
  2338. const int * v, const int * u, const half2 & dm4, const half2 & ds8) {
  2339. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2340. int sumi = 0;
  2341. #pragma unroll
  2342. for (int i = 0; i < vdr; ++i) {
  2343. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  2344. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  2345. // SIMD dot product of quantized values
  2346. sumi = __dp4a(vi0, u[2*i+0], sumi);
  2347. sumi = __dp4a(vi1, u[2*i+1], sumi);
  2348. }
  2349. #ifdef GGML_CUDA_F16
  2350. const float2 tmp = __half22float2(__hmul2(dm4, ds8));
  2351. const float d4d8 = tmp.x;
  2352. const float m4s8 = tmp.y;
  2353. #else
  2354. const float2 dm4f = __half22float2(dm4);
  2355. const float2 ds8f = __half22float2(ds8);
  2356. const float d4d8 = dm4f.x * ds8f.x;
  2357. const float m4s8 = dm4f.y * ds8f.y;
  2358. #endif // GGML_CUDA_F16
  2359. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  2360. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  2361. #else
  2362. NO_DEVICE_CODE;
  2363. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2364. }
  2365. #define VDR_Q5_0_Q8_1_MMVQ 2
  2366. #define VDR_Q5_0_Q8_1_MMQ 4
  2367. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(
  2368. const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {
  2369. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2370. int sumi = 0;
  2371. #pragma unroll
  2372. for (int i = 0; i < vdr; ++i) {
  2373. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2374. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2375. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2376. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2377. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2378. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2379. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2380. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2381. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2382. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2383. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2384. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2385. }
  2386. const float2 ds8f = __half22float2(ds8);
  2387. // second part effectively subtracts 16 from each quant value
  2388. return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);
  2389. #else
  2390. NO_DEVICE_CODE;
  2391. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2392. }
  2393. #define VDR_Q5_1_Q8_1_MMVQ 2
  2394. #define VDR_Q5_1_Q8_1_MMQ 4
  2395. template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(
  2396. const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {
  2397. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2398. int sumi = 0;
  2399. #pragma unroll
  2400. for (int i = 0; i < vdr; ++i) {
  2401. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  2402. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  2403. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  2404. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  2405. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  2406. sumi = __dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values
  2407. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  2408. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  2409. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  2410. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  2411. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  2412. sumi = __dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values
  2413. }
  2414. #ifdef GGML_CUDA_F16
  2415. const float2 tmp = __half22float2(__hmul2(dm5, ds8));
  2416. const float d5d8 = tmp.x;
  2417. const float m5s8 = tmp.y;
  2418. #else
  2419. const float2 dm5f = __half22float2(dm5);
  2420. const float2 ds8f = __half22float2(ds8);
  2421. const float d5d8 = dm5f.x * ds8f.x;
  2422. const float m5s8 = dm5f.y * ds8f.y;
  2423. #endif // GGML_CUDA_F16
  2424. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  2425. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  2426. #else
  2427. NO_DEVICE_CODE;
  2428. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2429. }
  2430. #define VDR_Q8_0_Q8_1_MMVQ 2
  2431. #define VDR_Q8_0_Q8_1_MMQ 8
  2432. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_q8_1_impl(
  2433. const int * v, const int * u, const float & d8_0, const float & d8_1) {
  2434. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2435. int sumi = 0;
  2436. #pragma unroll
  2437. for (int i = 0; i < vdr; ++i) {
  2438. // SIMD dot product of quantized values
  2439. sumi = __dp4a(v[i], u[i], sumi);
  2440. }
  2441. return d8_0*d8_1 * sumi;
  2442. #else
  2443. NO_DEVICE_CODE;
  2444. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2445. }
  2446. template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(
  2447. const int * v, const int * u, const half2 & dm8, const half2 & ds8) {
  2448. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2449. int sumi = 0;
  2450. #pragma unroll
  2451. for (int i = 0; i < vdr; ++i) {
  2452. // SIMD dot product of quantized values
  2453. sumi = __dp4a(v[i], u[i], sumi);
  2454. }
  2455. #ifdef GGML_CUDA_F16
  2456. const float2 tmp = __half22float2(__hmul2(dm8, ds8));
  2457. const float d8d8 = tmp.x;
  2458. const float m8s8 = tmp.y;
  2459. #else
  2460. const float2 dm8f = __half22float2(dm8);
  2461. const float2 ds8f = __half22float2(ds8);
  2462. const float d8d8 = dm8f.x * ds8f.x;
  2463. const float m8s8 = dm8f.y * ds8f.y;
  2464. #endif // GGML_CUDA_F16
  2465. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  2466. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  2467. #else
  2468. NO_DEVICE_CODE;
  2469. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2470. }
  2471. #define VDR_Q2_K_Q8_1_MMVQ 1
  2472. #define VDR_Q2_K_Q8_1_MMQ 2
  2473. // contiguous v/x values
  2474. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  2475. const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2476. const half2 & dm2, const float * __restrict__ d8) {
  2477. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2478. float sumf_d = 0.0f;
  2479. float sumf_m = 0.0f;
  2480. #pragma unroll
  2481. for (int i = 0; i < QR2_K; ++i) {
  2482. const int sc = scales[2*i];
  2483. const int vi = (v >> (2*i)) & 0x03030303;
  2484. sumf_d += d8[i] * (__dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  2485. // fill int with 4x m
  2486. int m = sc >> 4;
  2487. m |= m << 8;
  2488. m |= m << 16;
  2489. sumf_m += d8[i] * __dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values
  2490. }
  2491. const float2 dm2f = __half22float2(dm2);
  2492. return dm2f.x*sumf_d - dm2f.y*sumf_m;
  2493. #else
  2494. NO_DEVICE_CODE;
  2495. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2496. }
  2497. // contiguous u/y values
  2498. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(
  2499. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2500. const half2 & dm2, const float & d8) {
  2501. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2502. int sumi_d = 0;
  2503. int sumi_m = 0;
  2504. #pragma unroll
  2505. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  2506. int sumi_d_sc = 0;
  2507. const int sc = scales[i0 / (QI8_1/2)];
  2508. // fill int with 4x m
  2509. int m = sc >> 4;
  2510. m |= m << 8;
  2511. m |= m << 16;
  2512. #pragma unroll
  2513. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2514. sumi_d_sc = __dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  2515. sumi_m = __dp4a(m, u[i], sumi_m); // multiply sum of q8_1 values with m
  2516. }
  2517. sumi_d += sumi_d_sc * (sc & 0xF);
  2518. }
  2519. const float2 dm2f = __half22float2(dm2);
  2520. return d8 * (dm2f.x*sumi_d - dm2f.y*sumi_m);
  2521. #else
  2522. NO_DEVICE_CODE;
  2523. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2524. }
  2525. #define VDR_Q3_K_Q8_1_MMVQ 1
  2526. #define VDR_Q3_K_Q8_1_MMQ 2
  2527. // contiguous v/x values
  2528. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  2529. const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,
  2530. const int & scale_offset, const float & d3, const float * __restrict__ d8) {
  2531. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2532. float sumf = 0.0f;
  2533. #pragma unroll
  2534. for (int i = 0; i < QR3_K; ++i) {
  2535. const int isc = scale_offset + 2*i;
  2536. const int isc_low = isc % (QK_K/32);
  2537. const int sc_shift_low = 4 * (isc / (QK_K/32));
  2538. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  2539. const int isc_high = isc % (QK_K/64);
  2540. const int sc_shift_high = 2 * (isc / (QK_K/64));
  2541. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  2542. const int sc = (sc_low | sc_high) - 32;
  2543. const int vil = (vl >> (2*i)) & 0x03030303;
  2544. const int vih = ((vh >> i) << 2) & 0x04040404;
  2545. const int vi = __vsubss4(vil, vih);
  2546. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2547. }
  2548. return d3 * sumf;
  2549. #else
  2550. NO_DEVICE_CODE;
  2551. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2552. }
  2553. // contiguous u/y values
  2554. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(
  2555. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2556. const float & d3, const float & d8) {
  2557. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2558. int sumi = 0;
  2559. #pragma unroll
  2560. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  2561. int sumi_sc = 0;
  2562. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  2563. sumi_sc = __dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  2564. }
  2565. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  2566. }
  2567. return d3*d8 * sumi;
  2568. #else
  2569. NO_DEVICE_CODE;
  2570. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2571. }
  2572. #define VDR_Q4_K_Q8_1_MMVQ 2
  2573. #define VDR_Q4_K_Q8_1_MMQ 8
  2574. // contiguous v/x values
  2575. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  2576. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2577. const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {
  2578. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2579. float sumf_d = 0.0f;
  2580. float sumf_m = 0.0f;
  2581. #pragma unroll
  2582. for (int i = 0; i < QR4_K; ++i) {
  2583. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  2584. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  2585. const int dot1 = __dp4a(v1i, u[2*i+1], __dp4a(v0i, u[2*i+0], 0)); // SIMD dot product
  2586. const int dot2 = __dp4a(0x01010101, u[2*i+1], __dp4a(0x01010101, u[2*i+0], 0)); // sum of u
  2587. sumf_d += d8[i] * (dot1 * sc[i]);
  2588. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  2589. }
  2590. const float2 dm4f = __half22float2(dm4);
  2591. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2592. #else
  2593. NO_DEVICE_CODE;
  2594. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2595. }
  2596. // contiguous u/y values
  2597. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(
  2598. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2599. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2600. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2601. float sumf_d = 0.0f;
  2602. float sumf_m = 0.0f;
  2603. #pragma unroll
  2604. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  2605. int sumi_d = 0;
  2606. #pragma unroll
  2607. for (int j = 0; j < QI8_1; ++j) {
  2608. sumi_d = __dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2609. }
  2610. const float2 ds8f = __half22float2(ds8[i]);
  2611. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2612. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2613. }
  2614. const float2 dm4f = __half22float2(dm4);
  2615. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2616. #else
  2617. NO_DEVICE_CODE;
  2618. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2619. }
  2620. #define VDR_Q5_K_Q8_1_MMVQ 2
  2621. #define VDR_Q5_K_Q8_1_MMQ 8
  2622. // contiguous v/x values
  2623. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  2624. const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2625. const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
  2626. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2627. float sumf_d = 0.0f;
  2628. float sumf_m = 0.0f;
  2629. #pragma unroll
  2630. for (int i = 0; i < QR5_K; ++i) {
  2631. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  2632. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  2633. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  2634. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  2635. const int v0i = vl0i | vh0i;
  2636. const int v1i = vl1i | vh1i;
  2637. const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product
  2638. const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u
  2639. sumf_d += d8[i] * (dot1 * sc[i]);
  2640. sumf_m += d8[i] * (dot2 * m[i]);
  2641. }
  2642. const float2 dm5f = __half22float2(dm5);
  2643. return dm5f.x*sumf_d - dm5f.y*sumf_m;
  2644. #else
  2645. NO_DEVICE_CODE;
  2646. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2647. }
  2648. // contiguous u/y values
  2649. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(
  2650. const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,
  2651. const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {
  2652. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2653. float sumf_d = 0.0f;
  2654. float sumf_m = 0.0f;
  2655. #pragma unroll
  2656. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  2657. int sumi_d = 0;
  2658. #pragma unroll
  2659. for (int j = 0; j < QI8_1; ++j) {
  2660. sumi_d = __dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product
  2661. }
  2662. const float2 ds8f = __half22float2(ds8[i]);
  2663. sumf_d += ds8f.x * (sc[i] * sumi_d);
  2664. sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val
  2665. }
  2666. const float2 dm4f = __half22float2(dm4);
  2667. return dm4f.x*sumf_d - dm4f.y*sumf_m;
  2668. #else
  2669. NO_DEVICE_CODE;
  2670. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2671. }
  2672. #define VDR_Q6_K_Q8_1_MMVQ 1
  2673. #define VDR_Q6_K_Q8_1_MMQ 8
  2674. // contiguous v/x values
  2675. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(
  2676. const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,
  2677. const float & d, const float * __restrict__ d8) {
  2678. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2679. float sumf = 0.0f;
  2680. #pragma unroll
  2681. for (int i = 0; i < QR6_K; ++i) {
  2682. const int sc = scales[4*i];
  2683. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  2684. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  2685. const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32
  2686. sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc); // SIMD dot product
  2687. }
  2688. return d*sumf;
  2689. #else
  2690. NO_DEVICE_CODE;
  2691. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2692. }
  2693. // contiguous u/y values
  2694. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(
  2695. const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,
  2696. const float & d6, const float * __restrict__ d8) {
  2697. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  2698. float sumf_d = 0.0f;
  2699. #pragma unroll
  2700. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  2701. int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  2702. #pragma unroll
  2703. for (int i = i0; i < i0 + 2; ++i) {
  2704. sumi_d.x = __dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product
  2705. sumi_d.x = __dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product
  2706. sumi_d.y = __dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product
  2707. sumi_d.y = __dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product
  2708. }
  2709. sumf_d += d8[i0/4] * (sc[i0/2+0]*sumi_d.x + sc[i0/2+1]*sumi_d.y);
  2710. }
  2711. return d6 * sumf_d;
  2712. #else
  2713. NO_DEVICE_CODE;
  2714. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  2715. }
  2716. static __device__ __forceinline__ float vec_dot_q4_0_q8_1(
  2717. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2718. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  2719. int v[VDR_Q4_0_Q8_1_MMVQ];
  2720. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  2721. #pragma unroll
  2722. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  2723. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  2724. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2725. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  2726. }
  2727. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  2728. }
  2729. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2730. (void)x_qh; (void)x_sc;
  2731. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  2732. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI4_0) + mmq_y/QI4_0];
  2733. *x_ql = tile_x_qs;
  2734. *x_dm = (half2 *) tile_x_d;
  2735. }
  2736. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(
  2737. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2738. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2739. (void)x_qh; (void)x_sc;
  2740. GGML_CUDA_ASSUME(i_offset >= 0);
  2741. GGML_CUDA_ASSUME(i_offset < nwarps);
  2742. GGML_CUDA_ASSUME(k >= 0);
  2743. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2744. const int kbx = k / QI4_0;
  2745. const int kqsx = k % QI4_0;
  2746. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  2747. float * x_dmf = (float *) x_dm;
  2748. #pragma unroll
  2749. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2750. int i = i0 + i_offset;
  2751. if (need_check) {
  2752. i = min(i, i_max);
  2753. }
  2754. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2755. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  2756. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  2757. }
  2758. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  2759. const int kbxd = k % blocks_per_tile_x_row;
  2760. #pragma unroll
  2761. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  2762. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  2763. if (need_check) {
  2764. i = min(i, i_max);
  2765. }
  2766. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2767. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  2768. }
  2769. }
  2770. static __device__ __forceinline__ float vec_dot_q4_0_q8_1_mul_mat(
  2771. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2772. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2773. (void)x_qh; (void)x_sc;
  2774. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2775. const float * x_dmf = (const float *) x_dm;
  2776. int u[2*VDR_Q4_0_Q8_1_MMQ];
  2777. #pragma unroll
  2778. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  2779. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2780. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  2781. }
  2782. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  2783. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  2784. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2785. }
  2786. static __device__ __forceinline__ float vec_dot_q4_1_q8_1(
  2787. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2788. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  2789. int v[VDR_Q4_1_Q8_1_MMVQ];
  2790. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  2791. #pragma unroll
  2792. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  2793. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  2794. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2795. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  2796. }
  2797. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  2798. }
  2799. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2800. (void)x_qh; (void)x_sc;
  2801. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + + mmq_y];
  2802. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_1) + mmq_y/QI4_1];
  2803. *x_ql = tile_x_qs;
  2804. *x_dm = tile_x_dm;
  2805. }
  2806. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(
  2807. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2808. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2809. (void)x_qh; (void)x_sc;
  2810. GGML_CUDA_ASSUME(i_offset >= 0);
  2811. GGML_CUDA_ASSUME(i_offset < nwarps);
  2812. GGML_CUDA_ASSUME(k >= 0);
  2813. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2814. const int kbx = k / QI4_1;
  2815. const int kqsx = k % QI4_1;
  2816. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  2817. #pragma unroll
  2818. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2819. int i = i0 + i_offset;
  2820. if (need_check) {
  2821. i = min(i, i_max);
  2822. }
  2823. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2824. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2825. }
  2826. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  2827. const int kbxd = k % blocks_per_tile_x_row;
  2828. #pragma unroll
  2829. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  2830. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  2831. if (need_check) {
  2832. i = min(i, i_max);
  2833. }
  2834. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  2835. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  2836. }
  2837. }
  2838. static __device__ __forceinline__ float vec_dot_q4_1_q8_1_mul_mat(
  2839. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2840. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2841. (void)x_qh; (void)x_sc;
  2842. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2843. int u[2*VDR_Q4_1_Q8_1_MMQ];
  2844. #pragma unroll
  2845. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  2846. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2847. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  2848. }
  2849. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  2850. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  2851. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  2852. }
  2853. static __device__ __forceinline__ float vec_dot_q5_0_q8_1(
  2854. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2855. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  2856. int vl[VDR_Q5_0_Q8_1_MMVQ];
  2857. int vh[VDR_Q5_0_Q8_1_MMVQ];
  2858. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  2859. #pragma unroll
  2860. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  2861. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  2862. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  2863. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2864. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  2865. }
  2866. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  2867. }
  2868. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2869. (void)x_qh; (void)x_sc;
  2870. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2871. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI5_0) + mmq_y/QI5_0];
  2872. *x_ql = tile_x_ql;
  2873. *x_dm = (half2 *) tile_x_d;
  2874. }
  2875. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(
  2876. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2877. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2878. (void)x_qh; (void)x_sc;
  2879. GGML_CUDA_ASSUME(i_offset >= 0);
  2880. GGML_CUDA_ASSUME(i_offset < nwarps);
  2881. GGML_CUDA_ASSUME(k >= 0);
  2882. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2883. const int kbx = k / QI5_0;
  2884. const int kqsx = k % QI5_0;
  2885. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  2886. #pragma unroll
  2887. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2888. int i = i0 + i_offset;
  2889. if (need_check) {
  2890. i = min(i, i_max);
  2891. }
  2892. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  2893. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  2894. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  2895. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2896. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2897. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2898. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2899. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2900. qs0 = __vsubss4(qs0, 0x10101010); // subtract 16
  2901. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2902. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2903. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2904. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2905. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2906. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2907. qs1 = __vsubss4(qs1, 0x10101010); // subtract 16
  2908. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2909. }
  2910. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  2911. const int kbxd = k % blocks_per_tile_x_row;
  2912. float * x_dmf = (float *) x_dm;
  2913. #pragma unroll
  2914. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  2915. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  2916. if (need_check) {
  2917. i = min(i, i_max);
  2918. }
  2919. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  2920. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  2921. }
  2922. }
  2923. static __device__ __forceinline__ float vec_dot_q5_0_q8_1_mul_mat(
  2924. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  2925. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  2926. (void)x_qh; (void)x_sc;
  2927. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  2928. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  2929. const float * x_dmf = (const float *) x_dm;
  2930. const float * y_df = (const float *) y_ds;
  2931. int u[2*VDR_Q5_0_Q8_1_MMQ];
  2932. #pragma unroll
  2933. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  2934. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  2935. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  2936. }
  2937. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  2938. (&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)]);
  2939. }
  2940. static __device__ __forceinline__ float vec_dot_q5_1_q8_1(
  2941. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  2942. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  2943. int vl[VDR_Q5_1_Q8_1_MMVQ];
  2944. int vh[VDR_Q5_1_Q8_1_MMVQ];
  2945. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  2946. #pragma unroll
  2947. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  2948. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  2949. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  2950. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  2951. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  2952. }
  2953. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  2954. }
  2955. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_1(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  2956. (void)x_qh; (void)x_sc;
  2957. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  2958. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_1) + mmq_y/QI5_1];
  2959. *x_ql = tile_x_ql;
  2960. *x_dm = tile_x_dm;
  2961. }
  2962. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(
  2963. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  2964. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  2965. (void)x_qh; (void)x_sc;
  2966. GGML_CUDA_ASSUME(i_offset >= 0);
  2967. GGML_CUDA_ASSUME(i_offset < nwarps);
  2968. GGML_CUDA_ASSUME(k >= 0);
  2969. GGML_CUDA_ASSUME(k < WARP_SIZE);
  2970. const int kbx = k / QI5_1;
  2971. const int kqsx = k % QI5_1;
  2972. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  2973. #pragma unroll
  2974. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  2975. int i = i0 + i_offset;
  2976. if (need_check) {
  2977. i = min(i, i_max);
  2978. }
  2979. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  2980. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  2981. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  2982. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  2983. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  2984. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  2985. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  2986. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  2987. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  2988. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  2989. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  2990. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  2991. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  2992. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  2993. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  2994. }
  2995. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  2996. const int kbxd = k % blocks_per_tile_x_row;
  2997. #pragma unroll
  2998. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  2999. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  3000. if (need_check) {
  3001. i = min(i, i_max);
  3002. }
  3003. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  3004. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  3005. }
  3006. }
  3007. static __device__ __forceinline__ float vec_dot_q5_1_q8_1_mul_mat(
  3008. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3009. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3010. (void)x_qh; (void)x_sc;
  3011. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  3012. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  3013. int u[2*VDR_Q5_1_Q8_1_MMQ];
  3014. #pragma unroll
  3015. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  3016. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  3017. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  3018. }
  3019. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  3020. (&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)]);
  3021. }
  3022. static __device__ __forceinline__ float vec_dot_q8_0_q8_1(
  3023. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3024. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  3025. int v[VDR_Q8_0_Q8_1_MMVQ];
  3026. int u[VDR_Q8_0_Q8_1_MMVQ];
  3027. #pragma unroll
  3028. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  3029. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  3030. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  3031. }
  3032. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));
  3033. }
  3034. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q8_0(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3035. (void)x_qh; (void)x_sc;
  3036. __shared__ int tile_x_qs[mmq_y * (WARP_SIZE) + mmq_y];
  3037. __shared__ float tile_x_d[mmq_y * (WARP_SIZE/QI8_0) + mmq_y/QI8_0];
  3038. *x_ql = tile_x_qs;
  3039. *x_dm = (half2 *) tile_x_d;
  3040. }
  3041. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(
  3042. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3043. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3044. (void)x_qh; (void)x_sc;
  3045. GGML_CUDA_ASSUME(i_offset >= 0);
  3046. GGML_CUDA_ASSUME(i_offset < nwarps);
  3047. GGML_CUDA_ASSUME(k >= 0);
  3048. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3049. const int kbx = k / QI8_0;
  3050. const int kqsx = k % QI8_0;
  3051. float * x_dmf = (float *) x_dm;
  3052. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  3053. #pragma unroll
  3054. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3055. int i = i0 + i_offset;
  3056. if (need_check) {
  3057. i = min(i, i_max);
  3058. }
  3059. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  3060. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  3061. }
  3062. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  3063. const int kbxd = k % blocks_per_tile_x_row;
  3064. #pragma unroll
  3065. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  3066. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  3067. if (need_check) {
  3068. i = min(i, i_max);
  3069. }
  3070. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  3071. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  3072. }
  3073. }
  3074. static __device__ __forceinline__ float vec_dot_q8_0_q8_1_mul_mat(
  3075. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3076. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3077. (void)x_qh; (void)x_sc;
  3078. const float * x_dmf = (const float *) x_dm;
  3079. const float * y_df = (const float *) y_ds;
  3080. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  3081. (&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],
  3082. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  3083. }
  3084. static __device__ __forceinline__ float vec_dot_q2_K_q8_1(
  3085. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3086. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  3087. const int bq8_offset = QR2_K * (iqs / QI8_1);
  3088. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3089. const uint8_t * scales = bq2_K->scales + scale_offset;
  3090. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  3091. int u[QR2_K];
  3092. float d8[QR2_K];
  3093. #pragma unroll
  3094. for (int i = 0; i < QR2_K; ++ i) {
  3095. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3096. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3097. }
  3098. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  3099. }
  3100. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q2_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3101. (void)x_qh;
  3102. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3103. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI2_K) + mmq_y/QI2_K];
  3104. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3105. *x_ql = tile_x_ql;
  3106. *x_dm = tile_x_dm;
  3107. *x_sc = tile_x_sc;
  3108. }
  3109. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(
  3110. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3111. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3112. (void)x_qh;
  3113. GGML_CUDA_ASSUME(i_offset >= 0);
  3114. GGML_CUDA_ASSUME(i_offset < nwarps);
  3115. GGML_CUDA_ASSUME(k >= 0);
  3116. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3117. const int kbx = k / QI2_K;
  3118. const int kqsx = k % QI2_K;
  3119. const block_q2_K * bx0 = (const block_q2_K *) vx;
  3120. #pragma unroll
  3121. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3122. int i = i0 + i_offset;
  3123. if (need_check) {
  3124. i = min(i, i_max);
  3125. }
  3126. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  3127. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3128. }
  3129. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  3130. const int kbxd = k % blocks_per_tile_x_row;
  3131. #pragma unroll
  3132. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  3133. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  3134. if (need_check) {
  3135. i = min(i, i_max);
  3136. }
  3137. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3138. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  3139. }
  3140. #pragma unroll
  3141. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3142. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3143. if (need_check) {
  3144. i = min(i, i_max);
  3145. }
  3146. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  3147. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  3148. }
  3149. }
  3150. static __device__ __forceinline__ float vec_dot_q2_K_q8_1_mul_mat(
  3151. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3152. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3153. (void)x_qh;
  3154. const int kbx = k / QI2_K;
  3155. const int ky = (k % QI2_K) * QR2_K;
  3156. const float * y_df = (const float *) y_ds;
  3157. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  3158. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  3159. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  3160. #pragma unroll
  3161. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  3162. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3163. }
  3164. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  3165. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  3166. 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]);
  3167. }
  3168. static __device__ __forceinline__ float vec_dot_q3_K_q8_1(
  3169. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3170. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  3171. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  3172. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  3173. const float d = bq3_K->d;
  3174. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  3175. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3176. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  3177. int u[QR3_K];
  3178. float d8[QR3_K];
  3179. #pragma unroll
  3180. for (int i = 0; i < QR3_K; ++i) {
  3181. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  3182. d8[i] = __low2half(bq8_1[bq8_offset + i].ds);
  3183. }
  3184. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  3185. }
  3186. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q3_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3187. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3188. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI3_K) + mmq_y/QI3_K];
  3189. __shared__ int tile_x_qh[mmq_y * (WARP_SIZE/2) + mmq_y/2];
  3190. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/4) + mmq_y/4];
  3191. *x_ql = tile_x_ql;
  3192. *x_dm = tile_x_dm;
  3193. *x_qh = tile_x_qh;
  3194. *x_sc = tile_x_sc;
  3195. }
  3196. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(
  3197. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3198. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3199. GGML_CUDA_ASSUME(i_offset >= 0);
  3200. GGML_CUDA_ASSUME(i_offset < nwarps);
  3201. GGML_CUDA_ASSUME(k >= 0);
  3202. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3203. const int kbx = k / QI3_K;
  3204. const int kqsx = k % QI3_K;
  3205. const block_q3_K * bx0 = (const block_q3_K *) vx;
  3206. #pragma unroll
  3207. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3208. int i = i0 + i_offset;
  3209. if (need_check) {
  3210. i = min(i, i_max);
  3211. }
  3212. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  3213. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  3214. }
  3215. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  3216. const int kbxd = k % blocks_per_tile_x_row;
  3217. float * x_dmf = (float *) x_dm;
  3218. #pragma unroll
  3219. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  3220. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  3221. if (need_check) {
  3222. i = min(i, i_max);
  3223. }
  3224. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3225. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  3226. }
  3227. #pragma unroll
  3228. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  3229. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  3230. if (need_check) {
  3231. i = min(i, i_max);
  3232. }
  3233. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  3234. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  3235. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  3236. }
  3237. #pragma unroll
  3238. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  3239. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  3240. if (need_check) {
  3241. i = min(i, i_max);
  3242. }
  3243. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  3244. const int ksc = k % (QI3_K/4);
  3245. const int ksc_low = ksc % (QI3_K/8);
  3246. const int shift_low = 4 * (ksc / (QI3_K/8));
  3247. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  3248. const int ksc_high = QI3_K/8;
  3249. const int shift_high = 2 * ksc;
  3250. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  3251. const int sc = __vsubss4(sc_low | sc_high, 0x20202020);
  3252. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  3253. }
  3254. }
  3255. static __device__ __forceinline__ float vec_dot_q3_K_q8_1_mul_mat(
  3256. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3257. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3258. const int kbx = k / QI3_K;
  3259. const int ky = (k % QI3_K) * QR3_K;
  3260. const float * x_dmf = (const float *) x_dm;
  3261. const float * y_df = (const float *) y_ds;
  3262. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  3263. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  3264. #pragma unroll
  3265. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  3266. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  3267. const int shift = 2 * ((ky % 32) / 8);
  3268. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  3269. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  3270. const int vlh = (vh << 2) & 0x04040404;
  3271. v[l] = __vsubss4(vll, vlh);
  3272. }
  3273. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  3274. 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]);
  3275. }
  3276. static __device__ __forceinline__ float vec_dot_q4_K_q8_1(
  3277. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3278. #ifndef GGML_QKK_64
  3279. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3280. int v[2];
  3281. int u[2*QR4_K];
  3282. float d8[QR4_K];
  3283. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  3284. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  3285. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  3286. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  3287. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  3288. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  3289. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3290. v[0] = q4[0];
  3291. v[1] = q4[4];
  3292. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  3293. uint16_t aux[2];
  3294. const int j = bq8_offset/2;
  3295. if (j < 2) {
  3296. aux[0] = scales[j+0] & 0x3f3f;
  3297. aux[1] = scales[j+2] & 0x3f3f;
  3298. } else {
  3299. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3300. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3301. }
  3302. const uint8_t * sc = (const uint8_t *)aux;
  3303. const uint8_t * m = sc + 2;
  3304. for (int i = 0; i < QR4_K; ++i) {
  3305. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3306. d8[i] = __low2half(bq8i->ds);
  3307. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3308. u[2*i+0] = q8[0];
  3309. u[2*i+1] = q8[4];
  3310. }
  3311. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  3312. #else
  3313. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3314. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  3315. float sumf_d = 0.0f;
  3316. float sumf_m = 0.0f;
  3317. uint16_t aux16[2];
  3318. const uint8_t * s = (const uint8_t *)aux16;
  3319. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  3320. aux16[0] = a[0] & 0x0f0f;
  3321. aux16[1] = (a[0] >> 4) & 0x0f0f;
  3322. const float dall = bq4_K->dm[0];
  3323. const float dmin = bq4_K->dm[1];
  3324. const float d8_1 = __low2float(bq8_1[0].ds);
  3325. const float d8_2 = __low2float(bq8_1[1].ds);
  3326. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3327. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3328. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3329. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3330. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  3331. const int v1 = q4[0];
  3332. const int v2 = q4[4];
  3333. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  3334. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  3335. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  3336. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  3337. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  3338. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  3339. return dall * sumf_d - dmin * sumf_m;
  3340. #else
  3341. NO_DEVICE_CODE;
  3342. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3343. #endif
  3344. }
  3345. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q4_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3346. (void)x_qh;
  3347. __shared__ int tile_x_ql[mmq_y * (WARP_SIZE) + mmq_y];
  3348. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI4_K) + mmq_y/QI4_K];
  3349. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3350. *x_ql = tile_x_ql;
  3351. *x_dm = tile_x_dm;
  3352. *x_sc = tile_x_sc;
  3353. }
  3354. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(
  3355. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3356. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3357. (void)x_qh;
  3358. GGML_CUDA_ASSUME(i_offset >= 0);
  3359. GGML_CUDA_ASSUME(i_offset < nwarps);
  3360. GGML_CUDA_ASSUME(k >= 0);
  3361. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3362. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  3363. const int kqsx = k % QI4_K; // == k if QK_K == 256
  3364. const block_q4_K * bx0 = (const block_q4_K *) vx;
  3365. #pragma unroll
  3366. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3367. int i = i0 + i_offset;
  3368. if (need_check) {
  3369. i = min(i, i_max);
  3370. }
  3371. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  3372. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3373. }
  3374. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  3375. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3376. #pragma unroll
  3377. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  3378. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  3379. if (need_check) {
  3380. i = min(i, i_max);
  3381. }
  3382. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3383. #if QK_K == 256
  3384. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  3385. #else
  3386. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  3387. #endif
  3388. }
  3389. #pragma unroll
  3390. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3391. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3392. if (need_check) {
  3393. i = min(i, i_max);
  3394. }
  3395. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  3396. const int * scales = (const int *) bxi->scales;
  3397. const int ksc = k % (WARP_SIZE/8);
  3398. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3399. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3400. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3401. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3402. }
  3403. }
  3404. static __device__ __forceinline__ float vec_dot_q4_K_q8_1_mul_mat(
  3405. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3406. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3407. (void)x_qh;
  3408. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  3409. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  3410. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  3411. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  3412. }
  3413. static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
  3414. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3415. #ifndef GGML_QKK_64
  3416. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3417. int vl[2];
  3418. int vh[2];
  3419. int u[2*QR5_K];
  3420. float d8[QR5_K];
  3421. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  3422. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  3423. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  3424. vl[0] = ql[0];
  3425. vl[1] = ql[4];
  3426. vh[0] = qh[0] >> bq8_offset;
  3427. vh[1] = qh[4] >> bq8_offset;
  3428. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  3429. uint16_t aux[2];
  3430. const int j = bq8_offset/2;
  3431. if (j < 2) {
  3432. aux[0] = scales[j+0] & 0x3f3f;
  3433. aux[1] = scales[j+2] & 0x3f3f;
  3434. } else {
  3435. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  3436. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  3437. }
  3438. const uint8_t * sc = (const uint8_t *)aux;
  3439. const uint8_t * m = sc + 2;
  3440. #pragma unroll
  3441. for (int i = 0; i < QR5_K; ++i) {
  3442. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  3443. d8[i] = __low2float(bq8i->ds);
  3444. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  3445. u[2*i+0] = q8[0];
  3446. u[2*i+1] = q8[4];
  3447. }
  3448. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  3449. #else
  3450. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3451. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  3452. const int8_t * s = bq5_K->scales;
  3453. const float d = bq5_K->d;
  3454. const float d8_1 = __low2half(bq8_1[0].ds);
  3455. const float d8_2 = __low2half(bq8_1[1].ds);
  3456. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  3457. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  3458. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  3459. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  3460. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  3461. const int vl1 = ql[0];
  3462. const int vl2 = ql[4];
  3463. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  3464. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  3465. const int in = step%8; // 0, 4, 0, 4
  3466. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  3467. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  3468. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  3469. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  3470. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  3471. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  3472. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  3473. return d * sumf_d;
  3474. #else
  3475. NO_DEVICE_CODE;
  3476. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  3477. #endif
  3478. }
  3479. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q5_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3480. (void)x_qh;
  3481. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3482. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI5_K) + mmq_y/QI5_K];
  3483. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3484. *x_ql = tile_x_ql;
  3485. *x_dm = tile_x_dm;
  3486. *x_sc = tile_x_sc;
  3487. }
  3488. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(
  3489. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3490. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3491. (void)x_qh;
  3492. GGML_CUDA_ASSUME(i_offset >= 0);
  3493. GGML_CUDA_ASSUME(i_offset < nwarps);
  3494. GGML_CUDA_ASSUME(k >= 0);
  3495. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3496. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  3497. const int kqsx = k % QI5_K; // == k if QK_K == 256
  3498. const block_q5_K * bx0 = (const block_q5_K *) vx;
  3499. #pragma unroll
  3500. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3501. int i = i0 + i_offset;
  3502. if (need_check) {
  3503. i = min(i, i_max);
  3504. }
  3505. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  3506. const int ky = QR5_K*kqsx;
  3507. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  3508. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3509. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3510. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  3511. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  3512. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  3513. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  3514. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  3515. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  3516. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  3517. }
  3518. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  3519. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3520. #pragma unroll
  3521. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  3522. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  3523. if (need_check) {
  3524. i = min(i, i_max);
  3525. }
  3526. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3527. #if QK_K == 256
  3528. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  3529. #endif
  3530. }
  3531. #pragma unroll
  3532. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3533. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3534. if (need_check) {
  3535. i = min(i, i_max);
  3536. }
  3537. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  3538. const int * scales = (const int *) bxi->scales;
  3539. const int ksc = k % (WARP_SIZE/8);
  3540. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  3541. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  3542. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  3543. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  3544. }
  3545. }
  3546. static __device__ __forceinline__ float vec_dot_q5_K_q8_1_mul_mat(
  3547. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3548. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3549. (void)x_qh;
  3550. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  3551. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  3552. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  3553. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  3554. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  3555. }
  3556. static __device__ __forceinline__ float vec_dot_q6_K_q8_1(
  3557. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3558. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  3559. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  3560. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  3561. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  3562. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  3563. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  3564. const int8_t * scales = bq6_K->scales + scale_offset;
  3565. int u[QR6_K];
  3566. float d8[QR6_K];
  3567. #pragma unroll
  3568. for (int i = 0; i < QR6_K; ++i) {
  3569. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  3570. d8[i] = __low2half(bq8_1[bq8_offset + 2*i].ds);
  3571. }
  3572. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  3573. }
  3574. template <int mmq_y> static __device__ __forceinline__ void allocate_tiles_q6_K(int ** x_ql, half2 ** x_dm, int ** x_qh, int ** x_sc) {
  3575. (void)x_qh;
  3576. __shared__ int tile_x_ql[mmq_y * (2*WARP_SIZE) + mmq_y];
  3577. __shared__ half2 tile_x_dm[mmq_y * (WARP_SIZE/QI6_K) + mmq_y/QI6_K];
  3578. __shared__ int tile_x_sc[mmq_y * (WARP_SIZE/8) + mmq_y/8];
  3579. *x_ql = tile_x_ql;
  3580. *x_dm = tile_x_dm;
  3581. *x_sc = tile_x_sc;
  3582. }
  3583. template <int mmq_y, int nwarps, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(
  3584. const void * __restrict__ vx, int * __restrict__ x_ql, half2 * __restrict__ x_dm, int * __restrict__ x_qh,
  3585. int * __restrict__ x_sc, const int & i_offset, const int & i_max, const int & k, const int & blocks_per_row) {
  3586. (void)x_qh;
  3587. GGML_CUDA_ASSUME(i_offset >= 0);
  3588. GGML_CUDA_ASSUME(i_offset < nwarps);
  3589. GGML_CUDA_ASSUME(k >= 0);
  3590. GGML_CUDA_ASSUME(k < WARP_SIZE);
  3591. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  3592. const int kqsx = k % QI6_K; // == k if QK_K == 256
  3593. const block_q6_K * bx0 = (const block_q6_K *) vx;
  3594. #pragma unroll
  3595. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  3596. int i = i0 + i_offset;
  3597. if (need_check) {
  3598. i = min(i, i_max);
  3599. }
  3600. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  3601. const int ky = QR6_K*kqsx;
  3602. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  3603. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  3604. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  3605. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  3606. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  3607. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  3608. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  3609. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  3610. x_ql[i * (2*WARP_SIZE + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);
  3611. x_ql[i * (2*WARP_SIZE + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);
  3612. }
  3613. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  3614. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  3615. float * x_dmf = (float *) x_dm;
  3616. #pragma unroll
  3617. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  3618. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  3619. if (need_check) {
  3620. i = min(i, i_max);
  3621. }
  3622. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  3623. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  3624. }
  3625. #pragma unroll
  3626. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  3627. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  3628. if (need_check) {
  3629. i = min(i, i_max);
  3630. }
  3631. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  3632. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  3633. }
  3634. }
  3635. static __device__ __forceinline__ float vec_dot_q6_K_q8_1_mul_mat(
  3636. const int * __restrict__ x_ql, const half2 * __restrict__ x_dm, const int * __restrict__ x_qh, const int * __restrict__ x_sc,
  3637. const int * __restrict__ y_qs, const half2 * __restrict__ y_ds, const int & i, const int & j, const int & k) {
  3638. (void)x_qh;
  3639. const float * x_dmf = (const float *) x_dm;
  3640. const float * y_df = (const float *) y_ds;
  3641. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  3642. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  3643. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  3644. 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]);
  3645. }
  3646. static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
  3647. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3648. #if QK_K == 256
  3649. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  3650. #if QR2_XXS == 8
  3651. const int ib32 = iqs;
  3652. const uint16_t * q2 = bq2->qs + 4*ib32;
  3653. const uint8_t * aux8 = (const uint8_t *)q2;
  3654. const int8_t * q8 = bq8_1[ib32].qs;
  3655. uint32_t aux32 = q2[2] | (q2[3] << 16);
  3656. int sumi = 0;
  3657. for (int l = 0; l < 4; ++l) {
  3658. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  3659. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  3660. for (int j = 0; j < 8; ++j) {
  3661. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  3662. }
  3663. q8 += 8;
  3664. aux32 >>= 7;
  3665. }
  3666. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3667. return d * sumi;
  3668. #else
  3669. // iqs is 0...15
  3670. const int ib32 = iqs/2;
  3671. const int il = iqs%2;
  3672. const uint16_t * q2 = bq2->qs + 4*ib32;
  3673. const uint8_t * aux8 = (const uint8_t *)q2;
  3674. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  3675. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  3676. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3677. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * __low2float(bq8_1[ib32].ds) * 0.25f;
  3678. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  3679. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  3680. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  3681. int sumi1 = 0, sumi2 = 0;
  3682. for (int j = 0; j < 8; ++j) {
  3683. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  3684. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  3685. }
  3686. return d * (sumi1 + sumi2);
  3687. #endif
  3688. #else
  3689. assert(false);
  3690. return 0.f;
  3691. #endif
  3692. }
  3693. static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
  3694. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3695. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3696. #if QK_K == 256
  3697. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  3698. const int ib32 = iqs;
  3699. const uint16_t * q2 = bq2->qs + 4*ib32;
  3700. const int8_t * q8 = bq8_1[ib32].qs;
  3701. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  3702. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  3703. int sumi1 = 0;
  3704. for (int l = 0; l < 2; ++l) {
  3705. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3706. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3707. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3708. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3709. sumi1 = __dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  3710. sumi1 = __dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  3711. q8 += 8;
  3712. }
  3713. int sumi2 = 0;
  3714. for (int l = 2; l < 4; ++l) {
  3715. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  3716. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  3717. const int grid_l = __vsub4(grid[0] ^ signs[0], signs[0]);
  3718. const int grid_h = __vsub4(grid[1] ^ signs[1], signs[1]);
  3719. sumi2 = __dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  3720. sumi2 = __dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  3721. q8 += 8;
  3722. }
  3723. const float d = (float)bq2->d * __low2float(bq8_1[ib32].ds) * 0.25f;
  3724. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  3725. #else
  3726. assert(false);
  3727. return 0.f;
  3728. #endif
  3729. #else
  3730. assert(false);
  3731. return 0.f;
  3732. #endif
  3733. }
  3734. static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
  3735. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3736. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3737. #if QK_K == 256
  3738. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  3739. const int ib32 = iqs;
  3740. const uint8_t * q3 = bq2->qs + 8*ib32;
  3741. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  3742. const int8_t * q8 = bq8_1[ib32].qs;
  3743. uint32_t aux32 = gas[0] | (gas[1] << 16);
  3744. int sumi = 0;
  3745. for (int l = 0; l < 4; ++l) {
  3746. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  3747. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  3748. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  3749. const int grid_l = __vsub4(grid1[0] ^ signs[0], signs[0]);
  3750. const int grid_h = __vsub4(grid2[0] ^ signs[1], signs[1]);
  3751. sumi = __dp4a(grid_l, *((int *)q8+0), sumi);
  3752. sumi = __dp4a(grid_h, *((int *)q8+1), sumi);
  3753. q8 += 8;
  3754. aux32 >>= 7;
  3755. }
  3756. const float d = (float)bq2->d * (0.5f + aux32) * __low2float(bq8_1[ib32].ds) * 0.5f;
  3757. return d * sumi;
  3758. #else
  3759. assert(false);
  3760. return 0.f;
  3761. #endif
  3762. #else
  3763. assert(false);
  3764. return 0.f;
  3765. #endif
  3766. }
  3767. static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(
  3768. const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs) {
  3769. #if QK_K == 256
  3770. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  3771. const int ib32 = iqs;
  3772. int sumi1 = 0, sumi2 = 0, sumi3 = 0, sumi4 = 0;
  3773. const uint8_t h1 = bq1->scales[2*ib32+0];
  3774. const uint8_t h2 = bq1->scales[2*ib32+1];
  3775. #if __CUDA_ARCH__ >= MIN_CC_DP4A // lowest compute capability for integer intrinsics
  3776. const int * q8 = (const int *)bq8_1[ib32].qs;
  3777. const int * grid1 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3778. const int * grid2 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3779. const int * grid3 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3780. const int * grid4 = (const int *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3781. for (int j = 0; j < 2; ++j) {
  3782. sumi1 = __dp4a(q8[j+0], grid1[j], sumi1);
  3783. sumi2 = __dp4a(q8[j+2], grid2[j], sumi2);
  3784. sumi3 = __dp4a(q8[j+4], grid3[j], sumi3);
  3785. sumi4 = __dp4a(q8[j+6], grid4[j], sumi4);
  3786. }
  3787. #else
  3788. const int8_t * q8 = bq8_1[ib32].qs;
  3789. const int8_t * grid1 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+0] | ((h1 & 0x08) << 5)));
  3790. const int8_t * grid2 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+1] | ((h1 & 0x80) << 1)));
  3791. const int8_t * grid3 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+2] | ((h2 & 0x08) << 5)));
  3792. const int8_t * grid4 = (const int8_t *)(iq1s_grid + (bq1->qs[4*ib32+3] | ((h2 & 0x80) << 1)));
  3793. for (int j = 0; j < 8; ++j) {
  3794. sumi1 += q8[j+ 0] * grid1[j];
  3795. sumi2 += q8[j+ 8] * grid2[j];
  3796. sumi3 += q8[j+16] * grid3[j];
  3797. sumi4 += q8[j+24] * grid4[j];
  3798. }
  3799. #endif
  3800. const float d = (float)bq1->d * __low2float(bq8_1[ib32].ds);
  3801. return d * (sumi1 * (2*(h1 & 7) + 1) + sumi2 * (2*((h1 >> 4) & 7) + 1) +
  3802. sumi3 * (2*(h2 & 7) + 1) + sumi4 * (2*((h2 >> 4) & 7) + 1));
  3803. #else
  3804. assert(false);
  3805. return 0.f;
  3806. #endif
  3807. }
  3808. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x, int mmq_y, int nwarps,
  3809. allocate_tiles_cuda_t allocate_tiles, load_tiles_cuda_t load_tiles, int vdr, vec_dot_q_mul_mat_cuda_t vec_dot>
  3810. static __device__ __forceinline__ void mul_mat_q(
  3811. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3812. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3813. const block_q_t * x = (const block_q_t *) vx;
  3814. const block_q8_1 * y = (const block_q8_1 *) vy;
  3815. const int blocks_per_row_x = ncols_x / qk;
  3816. const int blocks_per_col_y = nrows_y / QK8_1;
  3817. const int blocks_per_warp = WARP_SIZE / qi;
  3818. const int & ncols_dst = ncols_y;
  3819. const int row_dst_0 = blockIdx.x*mmq_y;
  3820. const int & row_x_0 = row_dst_0;
  3821. const int col_dst_0 = blockIdx.y*mmq_x;
  3822. const int & col_y_0 = col_dst_0;
  3823. int * tile_x_ql = nullptr;
  3824. half2 * tile_x_dm = nullptr;
  3825. int * tile_x_qh = nullptr;
  3826. int * tile_x_sc = nullptr;
  3827. allocate_tiles(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc);
  3828. __shared__ int tile_y_qs[mmq_x * WARP_SIZE];
  3829. __shared__ half2 tile_y_ds[mmq_x * WARP_SIZE/QI8_1];
  3830. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  3831. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  3832. load_tiles(x + row_x_0*blocks_per_row_x + ib0, tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  3833. threadIdx.y, nrows_x-row_x_0-1, threadIdx.x, blocks_per_row_x);
  3834. #pragma unroll
  3835. for (int ir = 0; ir < qr; ++ir) {
  3836. const int kqs = ir*WARP_SIZE + threadIdx.x;
  3837. const int kbxd = kqs / QI8_1;
  3838. #pragma unroll
  3839. for (int i = 0; i < mmq_x; i += nwarps) {
  3840. const int col_y_eff = min(col_y_0 + threadIdx.y + i, ncols_y-1); // to prevent out-of-bounds memory accesses
  3841. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  3842. const int index_y = (threadIdx.y + i) * WARP_SIZE + kqs % WARP_SIZE;
  3843. tile_y_qs[index_y] = get_int_from_int8_aligned(by0->qs, threadIdx.x % QI8_1);
  3844. }
  3845. #pragma unroll
  3846. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  3847. const int ids = (ids0 + threadIdx.y * QI8_1 + threadIdx.x / (WARP_SIZE/QI8_1)) % mmq_x;
  3848. const int kby = threadIdx.x % (WARP_SIZE/QI8_1);
  3849. const int col_y_eff = min(col_y_0 + ids, ncols_y-1);
  3850. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  3851. const half2 * dsi_src = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + ir*(WARP_SIZE/QI8_1) + kby].ds;
  3852. half2 * dsi_dst = &tile_y_ds[ids * (WARP_SIZE/QI8_1) + kby];
  3853. if (need_sum) {
  3854. *dsi_dst = *dsi_src;
  3855. } else {
  3856. float * dfi_dst = (float *) dsi_dst;
  3857. *dfi_dst = __low2half(*dsi_src);
  3858. }
  3859. }
  3860. __syncthreads();
  3861. // #pragma unroll // unrolling this loop causes too much register pressure
  3862. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  3863. #pragma unroll
  3864. for (int j = 0; j < mmq_x; j += nwarps) {
  3865. #pragma unroll
  3866. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3867. sum[i/WARP_SIZE][j/nwarps] += vec_dot(
  3868. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc, tile_y_qs, tile_y_ds,
  3869. threadIdx.x + i, threadIdx.y + j, k);
  3870. }
  3871. }
  3872. }
  3873. __syncthreads();
  3874. }
  3875. }
  3876. #pragma unroll
  3877. for (int j = 0; j < mmq_x; j += nwarps) {
  3878. const int col_dst = col_dst_0 + j + threadIdx.y;
  3879. if (col_dst >= ncols_dst) {
  3880. return;
  3881. }
  3882. #pragma unroll
  3883. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  3884. const int row_dst = row_dst_0 + threadIdx.x + i;
  3885. if (row_dst >= nrows_dst) {
  3886. continue;
  3887. }
  3888. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  3889. }
  3890. }
  3891. }
  3892. #define MMQ_X_Q4_0_RDNA2 64
  3893. #define MMQ_Y_Q4_0_RDNA2 128
  3894. #define NWARPS_Q4_0_RDNA2 8
  3895. #define MMQ_X_Q4_0_RDNA1 64
  3896. #define MMQ_Y_Q4_0_RDNA1 64
  3897. #define NWARPS_Q4_0_RDNA1 8
  3898. #if defined(CUDA_USE_TENSOR_CORES)
  3899. #define MMQ_X_Q4_0_AMPERE 4
  3900. #define MMQ_Y_Q4_0_AMPERE 32
  3901. #define NWARPS_Q4_0_AMPERE 4
  3902. #else
  3903. #define MMQ_X_Q4_0_AMPERE 64
  3904. #define MMQ_Y_Q4_0_AMPERE 128
  3905. #define NWARPS_Q4_0_AMPERE 4
  3906. #endif
  3907. #define MMQ_X_Q4_0_PASCAL 64
  3908. #define MMQ_Y_Q4_0_PASCAL 64
  3909. #define NWARPS_Q4_0_PASCAL 8
  3910. template <bool need_check> static __global__ void
  3911. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3912. #if defined(RDNA3) || defined(RDNA2)
  3913. __launch_bounds__(WARP_SIZE*NWARPS_Q4_0_RDNA2, 2)
  3914. #endif // defined(RDNA3) || defined(RDNA2)
  3915. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3916. mul_mat_q4_0(
  3917. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3918. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3919. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3920. #if defined(RDNA3) || defined(RDNA2)
  3921. const int mmq_x = MMQ_X_Q4_0_RDNA2;
  3922. const int mmq_y = MMQ_Y_Q4_0_RDNA2;
  3923. const int nwarps = NWARPS_Q4_0_RDNA2;
  3924. #else
  3925. const int mmq_x = MMQ_X_Q4_0_RDNA1;
  3926. const int mmq_y = MMQ_Y_Q4_0_RDNA1;
  3927. const int nwarps = NWARPS_Q4_0_RDNA1;
  3928. #endif // defined(RDNA3) || defined(RDNA2)
  3929. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3930. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3931. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3932. #elif __CUDA_ARCH__ >= CC_VOLTA
  3933. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  3934. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  3935. const int nwarps = NWARPS_Q4_0_AMPERE;
  3936. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3937. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3938. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3939. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  3940. const int mmq_x = MMQ_X_Q4_0_PASCAL;
  3941. const int mmq_y = MMQ_Y_Q4_0_PASCAL;
  3942. const int nwarps = NWARPS_Q4_0_PASCAL;
  3943. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps, allocate_tiles_q4_0<mmq_y>,
  3944. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ, vec_dot_q4_0_q8_1_mul_mat>
  3945. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3946. #else
  3947. (void) vec_dot_q4_0_q8_1_mul_mat;
  3948. NO_DEVICE_CODE;
  3949. #endif // __CUDA_ARCH__ >= CC_VOLTA
  3950. }
  3951. #define MMQ_X_Q4_1_RDNA2 64
  3952. #define MMQ_Y_Q4_1_RDNA2 128
  3953. #define NWARPS_Q4_1_RDNA2 8
  3954. #define MMQ_X_Q4_1_RDNA1 64
  3955. #define MMQ_Y_Q4_1_RDNA1 64
  3956. #define NWARPS_Q4_1_RDNA1 8
  3957. #if defined(CUDA_USE_TENSOR_CORES)
  3958. #define MMQ_X_Q4_1_AMPERE 4
  3959. #define MMQ_Y_Q4_1_AMPERE 32
  3960. #define NWARPS_Q4_1_AMPERE 4
  3961. #else
  3962. #define MMQ_X_Q4_1_AMPERE 64
  3963. #define MMQ_Y_Q4_1_AMPERE 128
  3964. #define NWARPS_Q4_1_AMPERE 4
  3965. #endif
  3966. #define MMQ_X_Q4_1_PASCAL 64
  3967. #define MMQ_Y_Q4_1_PASCAL 64
  3968. #define NWARPS_Q4_1_PASCAL 8
  3969. template <bool need_check> static __global__ void
  3970. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3971. #if defined(RDNA3) || defined(RDNA2)
  3972. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_RDNA2, 2)
  3973. #endif // defined(RDNA3) || defined(RDNA2)
  3974. #elif __CUDA_ARCH__ < CC_VOLTA
  3975. __launch_bounds__(WARP_SIZE*NWARPS_Q4_1_PASCAL, 2)
  3976. #endif // __CUDA_ARCH__ < CC_VOLTA
  3977. mul_mat_q4_1(
  3978. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  3979. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  3980. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  3981. #if defined(RDNA3) || defined(RDNA2)
  3982. const int mmq_x = MMQ_X_Q4_1_RDNA2;
  3983. const int mmq_y = MMQ_Y_Q4_1_RDNA2;
  3984. const int nwarps = NWARPS_Q4_1_RDNA2;
  3985. #else
  3986. const int mmq_x = MMQ_X_Q4_1_RDNA1;
  3987. const int mmq_y = MMQ_Y_Q4_1_RDNA1;
  3988. const int nwarps = NWARPS_Q4_1_RDNA1;
  3989. #endif // defined(RDNA3) || defined(RDNA2)
  3990. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3991. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3992. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  3993. #elif __CUDA_ARCH__ >= CC_VOLTA
  3994. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  3995. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  3996. const int nwarps = NWARPS_Q4_1_AMPERE;
  3997. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  3998. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  3999. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4000. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4001. const int mmq_x = MMQ_X_Q4_1_PASCAL;
  4002. const int mmq_y = MMQ_Y_Q4_1_PASCAL;
  4003. const int nwarps = NWARPS_Q4_1_PASCAL;
  4004. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps, allocate_tiles_q4_1<mmq_y>,
  4005. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ, vec_dot_q4_1_q8_1_mul_mat>
  4006. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4007. #else
  4008. (void) vec_dot_q4_1_q8_1_mul_mat;
  4009. NO_DEVICE_CODE;
  4010. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4011. }
  4012. #define MMQ_X_Q5_0_RDNA2 64
  4013. #define MMQ_Y_Q5_0_RDNA2 128
  4014. #define NWARPS_Q5_0_RDNA2 8
  4015. #define MMQ_X_Q5_0_RDNA1 64
  4016. #define MMQ_Y_Q5_0_RDNA1 64
  4017. #define NWARPS_Q5_0_RDNA1 8
  4018. #if defined(CUDA_USE_TENSOR_CORES)
  4019. #define MMQ_X_Q5_0_AMPERE 4
  4020. #define MMQ_Y_Q5_0_AMPERE 32
  4021. #define NWARPS_Q5_0_AMPERE 4
  4022. #else
  4023. #define MMQ_X_Q5_0_AMPERE 128
  4024. #define MMQ_Y_Q5_0_AMPERE 64
  4025. #define NWARPS_Q5_0_AMPERE 4
  4026. #endif
  4027. #define MMQ_X_Q5_0_PASCAL 64
  4028. #define MMQ_Y_Q5_0_PASCAL 64
  4029. #define NWARPS_Q5_0_PASCAL 8
  4030. template <bool need_check> static __global__ void
  4031. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4032. #if defined(RDNA3) || defined(RDNA2)
  4033. __launch_bounds__(WARP_SIZE*NWARPS_Q5_0_RDNA2, 2)
  4034. #endif // defined(RDNA3) || defined(RDNA2)
  4035. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4036. mul_mat_q5_0(
  4037. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4038. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4039. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4040. #if defined(RDNA3) || defined(RDNA2)
  4041. const int mmq_x = MMQ_X_Q5_0_RDNA2;
  4042. const int mmq_y = MMQ_Y_Q5_0_RDNA2;
  4043. const int nwarps = NWARPS_Q5_0_RDNA2;
  4044. #else
  4045. const int mmq_x = MMQ_X_Q5_0_RDNA1;
  4046. const int mmq_y = MMQ_Y_Q5_0_RDNA1;
  4047. const int nwarps = NWARPS_Q5_0_RDNA1;
  4048. #endif // defined(RDNA3) || defined(RDNA2)
  4049. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4050. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4051. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4052. #elif __CUDA_ARCH__ >= CC_VOLTA
  4053. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  4054. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  4055. const int nwarps = NWARPS_Q5_0_AMPERE;
  4056. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4057. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4058. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4059. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4060. const int mmq_x = MMQ_X_Q5_0_PASCAL;
  4061. const int mmq_y = MMQ_Y_Q5_0_PASCAL;
  4062. const int nwarps = NWARPS_Q5_0_PASCAL;
  4063. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps, allocate_tiles_q5_0<mmq_y>,
  4064. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ, vec_dot_q5_0_q8_1_mul_mat>
  4065. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4066. #else
  4067. (void) vec_dot_q5_0_q8_1_mul_mat;
  4068. NO_DEVICE_CODE;
  4069. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4070. }
  4071. #define MMQ_X_Q5_1_RDNA2 64
  4072. #define MMQ_Y_Q5_1_RDNA2 128
  4073. #define NWARPS_Q5_1_RDNA2 8
  4074. #define MMQ_X_Q5_1_RDNA1 64
  4075. #define MMQ_Y_Q5_1_RDNA1 64
  4076. #define NWARPS_Q5_1_RDNA1 8
  4077. #if defined(CUDA_USE_TENSOR_CORES)
  4078. #define MMQ_X_Q5_1_AMPERE 4
  4079. #define MMQ_Y_Q5_1_AMPERE 32
  4080. #define NWARPS_Q5_1_AMPERE 4
  4081. #else
  4082. #define MMQ_X_Q5_1_AMPERE 128
  4083. #define MMQ_Y_Q5_1_AMPERE 64
  4084. #define NWARPS_Q5_1_AMPERE 4
  4085. #endif
  4086. #define MMQ_X_Q5_1_PASCAL 64
  4087. #define MMQ_Y_Q5_1_PASCAL 64
  4088. #define NWARPS_Q5_1_PASCAL 8
  4089. template <bool need_check> static __global__ void
  4090. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4091. #if defined(RDNA3) || defined(RDNA2)
  4092. __launch_bounds__(WARP_SIZE*NWARPS_Q5_1_RDNA2, 2)
  4093. #endif // defined(RDNA3) || defined(RDNA2)
  4094. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4095. mul_mat_q5_1(
  4096. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4097. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4098. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4099. #if defined(RDNA3) || defined(RDNA2)
  4100. const int mmq_x = MMQ_X_Q5_1_RDNA2;
  4101. const int mmq_y = MMQ_Y_Q5_1_RDNA2;
  4102. const int nwarps = NWARPS_Q5_1_RDNA2;
  4103. #else
  4104. const int mmq_x = MMQ_X_Q5_1_RDNA1;
  4105. const int mmq_y = MMQ_Y_Q5_1_RDNA1;
  4106. const int nwarps = NWARPS_Q5_1_RDNA1;
  4107. #endif // defined(RDNA3) || defined(RDNA2)
  4108. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4109. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4110. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4111. #elif __CUDA_ARCH__ >= CC_VOLTA
  4112. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  4113. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  4114. const int nwarps = NWARPS_Q5_1_AMPERE;
  4115. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4116. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4117. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4118. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4119. const int mmq_x = MMQ_X_Q5_1_PASCAL;
  4120. const int mmq_y = MMQ_Y_Q5_1_PASCAL;
  4121. const int nwarps = NWARPS_Q5_1_PASCAL;
  4122. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps, allocate_tiles_q5_1<mmq_y>,
  4123. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ, vec_dot_q5_1_q8_1_mul_mat>
  4124. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4125. #else
  4126. (void) vec_dot_q5_1_q8_1_mul_mat;
  4127. NO_DEVICE_CODE;
  4128. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4129. }
  4130. #define MMQ_X_Q8_0_RDNA2 64
  4131. #define MMQ_Y_Q8_0_RDNA2 128
  4132. #define NWARPS_Q8_0_RDNA2 8
  4133. #define MMQ_X_Q8_0_RDNA1 64
  4134. #define MMQ_Y_Q8_0_RDNA1 64
  4135. #define NWARPS_Q8_0_RDNA1 8
  4136. #if defined(CUDA_USE_TENSOR_CORES)
  4137. #define MMQ_X_Q8_0_AMPERE 4
  4138. #define MMQ_Y_Q8_0_AMPERE 32
  4139. #define NWARPS_Q8_0_AMPERE 4
  4140. #else
  4141. #define MMQ_X_Q8_0_AMPERE 128
  4142. #define MMQ_Y_Q8_0_AMPERE 64
  4143. #define NWARPS_Q8_0_AMPERE 4
  4144. #endif
  4145. #define MMQ_X_Q8_0_PASCAL 64
  4146. #define MMQ_Y_Q8_0_PASCAL 64
  4147. #define NWARPS_Q8_0_PASCAL 8
  4148. template <bool need_check> static __global__ void
  4149. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4150. #if defined(RDNA3) || defined(RDNA2)
  4151. __launch_bounds__(WARP_SIZE*NWARPS_Q8_0_RDNA2, 2)
  4152. #endif // defined(RDNA3) || defined(RDNA2)
  4153. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4154. mul_mat_q8_0(
  4155. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4156. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4157. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4158. #if defined(RDNA3) || defined(RDNA2)
  4159. const int mmq_x = MMQ_X_Q8_0_RDNA2;
  4160. const int mmq_y = MMQ_Y_Q8_0_RDNA2;
  4161. const int nwarps = NWARPS_Q8_0_RDNA2;
  4162. #else
  4163. const int mmq_x = MMQ_X_Q8_0_RDNA1;
  4164. const int mmq_y = MMQ_Y_Q8_0_RDNA1;
  4165. const int nwarps = NWARPS_Q8_0_RDNA1;
  4166. #endif // defined(RDNA3) || defined(RDNA2)
  4167. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4168. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4169. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4170. #elif __CUDA_ARCH__ >= CC_VOLTA
  4171. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  4172. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  4173. const int nwarps = NWARPS_Q8_0_AMPERE;
  4174. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4175. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4176. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4177. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4178. const int mmq_x = MMQ_X_Q8_0_PASCAL;
  4179. const int mmq_y = MMQ_Y_Q8_0_PASCAL;
  4180. const int nwarps = NWARPS_Q8_0_PASCAL;
  4181. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps, allocate_tiles_q8_0<mmq_y>,
  4182. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ, vec_dot_q8_0_q8_1_mul_mat>
  4183. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4184. #else
  4185. (void) vec_dot_q8_0_q8_1_mul_mat;
  4186. NO_DEVICE_CODE;
  4187. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4188. }
  4189. #define MMQ_X_Q2_K_RDNA2 64
  4190. #define MMQ_Y_Q2_K_RDNA2 128
  4191. #define NWARPS_Q2_K_RDNA2 8
  4192. #define MMQ_X_Q2_K_RDNA1 128
  4193. #define MMQ_Y_Q2_K_RDNA1 32
  4194. #define NWARPS_Q2_K_RDNA1 8
  4195. #if defined(CUDA_USE_TENSOR_CORES)
  4196. #define MMQ_X_Q2_K_AMPERE 4
  4197. #define MMQ_Y_Q2_K_AMPERE 32
  4198. #define NWARPS_Q2_K_AMPERE 4
  4199. #else
  4200. #define MMQ_X_Q2_K_AMPERE 64
  4201. #define MMQ_Y_Q2_K_AMPERE 128
  4202. #define NWARPS_Q2_K_AMPERE 4
  4203. #endif
  4204. #define MMQ_X_Q2_K_PASCAL 64
  4205. #define MMQ_Y_Q2_K_PASCAL 64
  4206. #define NWARPS_Q2_K_PASCAL 8
  4207. template <bool need_check> static __global__ void
  4208. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4209. #if defined(RDNA3) || defined(RDNA2)
  4210. __launch_bounds__(WARP_SIZE*NWARPS_Q2_K_RDNA2, 2)
  4211. #endif // defined(RDNA3) || defined(RDNA2)
  4212. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4213. mul_mat_q2_K(
  4214. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4215. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4216. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4217. #if defined(RDNA3) || defined(RDNA2)
  4218. const int mmq_x = MMQ_X_Q2_K_RDNA2;
  4219. const int mmq_y = MMQ_Y_Q2_K_RDNA2;
  4220. const int nwarps = NWARPS_Q2_K_RDNA2;
  4221. #else
  4222. const int mmq_x = MMQ_X_Q2_K_RDNA1;
  4223. const int mmq_y = MMQ_Y_Q2_K_RDNA1;
  4224. const int nwarps = NWARPS_Q2_K_RDNA1;
  4225. #endif // defined(RDNA3) || defined(RDNA2)
  4226. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4227. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4228. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4229. #elif __CUDA_ARCH__ >= CC_VOLTA
  4230. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  4231. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  4232. const int nwarps = NWARPS_Q2_K_AMPERE;
  4233. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4234. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4235. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4236. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4237. const int mmq_x = MMQ_X_Q2_K_PASCAL;
  4238. const int mmq_y = MMQ_Y_Q2_K_PASCAL;
  4239. const int nwarps = NWARPS_Q2_K_PASCAL;
  4240. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps, allocate_tiles_q2_K<mmq_y>,
  4241. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ, vec_dot_q2_K_q8_1_mul_mat>
  4242. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4243. #else
  4244. (void) vec_dot_q2_K_q8_1_mul_mat;
  4245. NO_DEVICE_CODE;
  4246. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4247. }
  4248. #define MMQ_X_Q3_K_RDNA2 128
  4249. #define MMQ_Y_Q3_K_RDNA2 64
  4250. #define NWARPS_Q3_K_RDNA2 8
  4251. #define MMQ_X_Q3_K_RDNA1 32
  4252. #define MMQ_Y_Q3_K_RDNA1 128
  4253. #define NWARPS_Q3_K_RDNA1 8
  4254. #if defined(CUDA_USE_TENSOR_CORES)
  4255. #define MMQ_X_Q3_K_AMPERE 4
  4256. #define MMQ_Y_Q3_K_AMPERE 32
  4257. #define NWARPS_Q3_K_AMPERE 4
  4258. #else
  4259. #define MMQ_X_Q3_K_AMPERE 128
  4260. #define MMQ_Y_Q3_K_AMPERE 128
  4261. #define NWARPS_Q3_K_AMPERE 4
  4262. #endif
  4263. #define MMQ_X_Q3_K_PASCAL 64
  4264. #define MMQ_Y_Q3_K_PASCAL 64
  4265. #define NWARPS_Q3_K_PASCAL 8
  4266. template <bool need_check> static __global__ void
  4267. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4268. #if defined(RDNA3) || defined(RDNA2)
  4269. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_RDNA2, 2)
  4270. #endif // defined(RDNA3) || defined(RDNA2)
  4271. #elif __CUDA_ARCH__ < CC_VOLTA
  4272. __launch_bounds__(WARP_SIZE*NWARPS_Q3_K_PASCAL, 2)
  4273. #endif // __CUDA_ARCH__ < CC_VOLTA
  4274. mul_mat_q3_K(
  4275. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4276. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4277. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4278. #if defined(RDNA3) || defined(RDNA2)
  4279. const int mmq_x = MMQ_X_Q3_K_RDNA2;
  4280. const int mmq_y = MMQ_Y_Q3_K_RDNA2;
  4281. const int nwarps = NWARPS_Q3_K_RDNA2;
  4282. #else
  4283. const int mmq_x = MMQ_X_Q3_K_RDNA1;
  4284. const int mmq_y = MMQ_Y_Q3_K_RDNA1;
  4285. const int nwarps = NWARPS_Q3_K_RDNA1;
  4286. #endif // defined(RDNA3) || defined(RDNA2)
  4287. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4288. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4289. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4290. #elif __CUDA_ARCH__ >= CC_VOLTA
  4291. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  4292. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  4293. const int nwarps = NWARPS_Q3_K_AMPERE;
  4294. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4295. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4296. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4297. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4298. const int mmq_x = MMQ_X_Q3_K_PASCAL;
  4299. const int mmq_y = MMQ_Y_Q3_K_PASCAL;
  4300. const int nwarps = NWARPS_Q3_K_PASCAL;
  4301. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps, allocate_tiles_q3_K<mmq_y>,
  4302. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ, vec_dot_q3_K_q8_1_mul_mat>
  4303. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4304. #else
  4305. (void) vec_dot_q3_K_q8_1_mul_mat;
  4306. NO_DEVICE_CODE;
  4307. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4308. }
  4309. #define MMQ_X_Q4_K_RDNA2 64
  4310. #define MMQ_Y_Q4_K_RDNA2 128
  4311. #define NWARPS_Q4_K_RDNA2 8
  4312. #define MMQ_X_Q4_K_RDNA1 32
  4313. #define MMQ_Y_Q4_K_RDNA1 64
  4314. #define NWARPS_Q4_K_RDNA1 8
  4315. #if defined(CUDA_USE_TENSOR_CORES)
  4316. #define MMQ_X_Q4_K_AMPERE 4
  4317. #define MMQ_Y_Q4_K_AMPERE 32
  4318. #define NWARPS_Q4_K_AMPERE 4
  4319. #else
  4320. #define MMQ_X_Q4_K_AMPERE 64
  4321. #define MMQ_Y_Q4_K_AMPERE 128
  4322. #define NWARPS_Q4_K_AMPERE 4
  4323. #endif
  4324. #define MMQ_X_Q4_K_PASCAL 64
  4325. #define MMQ_Y_Q4_K_PASCAL 64
  4326. #define NWARPS_Q4_K_PASCAL 8
  4327. template <bool need_check> static __global__ void
  4328. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4329. #if defined(RDNA3) || defined(RDNA2)
  4330. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_RDNA2, 2)
  4331. #endif // defined(RDNA3) || defined(RDNA2)
  4332. #elif __CUDA_ARCH__ < CC_VOLTA
  4333. __launch_bounds__(WARP_SIZE*NWARPS_Q4_K_PASCAL, 2)
  4334. #endif // __CUDA_ARCH__ < CC_VOLTA
  4335. mul_mat_q4_K(
  4336. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4337. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4338. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4339. #if defined(RDNA3) || defined(RDNA2)
  4340. const int mmq_x = MMQ_X_Q4_K_RDNA2;
  4341. const int mmq_y = MMQ_Y_Q4_K_RDNA2;
  4342. const int nwarps = NWARPS_Q4_K_RDNA2;
  4343. #else
  4344. const int mmq_x = MMQ_X_Q4_K_RDNA1;
  4345. const int mmq_y = MMQ_Y_Q4_K_RDNA1;
  4346. const int nwarps = NWARPS_Q4_K_RDNA1;
  4347. #endif // defined(RDNA3) || defined(RDNA2)
  4348. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4349. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4350. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4351. #elif __CUDA_ARCH__ >= CC_VOLTA
  4352. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  4353. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  4354. const int nwarps = NWARPS_Q4_K_AMPERE;
  4355. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4356. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4357. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4358. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4359. const int mmq_x = MMQ_X_Q4_K_PASCAL;
  4360. const int mmq_y = MMQ_Y_Q4_K_PASCAL;
  4361. const int nwarps = NWARPS_Q4_K_PASCAL;
  4362. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps, allocate_tiles_q4_K<mmq_y>,
  4363. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ, vec_dot_q4_K_q8_1_mul_mat>
  4364. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4365. #else
  4366. (void) vec_dot_q4_K_q8_1_mul_mat;
  4367. NO_DEVICE_CODE;
  4368. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4369. }
  4370. #define MMQ_X_Q5_K_RDNA2 64
  4371. #define MMQ_Y_Q5_K_RDNA2 128
  4372. #define NWARPS_Q5_K_RDNA2 8
  4373. #define MMQ_X_Q5_K_RDNA1 32
  4374. #define MMQ_Y_Q5_K_RDNA1 64
  4375. #define NWARPS_Q5_K_RDNA1 8
  4376. #if defined(CUDA_USE_TENSOR_CORES)
  4377. #define MMQ_X_Q5_K_AMPERE 4
  4378. #define MMQ_Y_Q5_K_AMPERE 32
  4379. #define NWARPS_Q5_K_AMPERE 4
  4380. #else
  4381. #define MMQ_X_Q5_K_AMPERE 64
  4382. #define MMQ_Y_Q5_K_AMPERE 128
  4383. #define NWARPS_Q5_K_AMPERE 4
  4384. #endif
  4385. #define MMQ_X_Q5_K_PASCAL 64
  4386. #define MMQ_Y_Q5_K_PASCAL 64
  4387. #define NWARPS_Q5_K_PASCAL 8
  4388. template <bool need_check> static __global__ void
  4389. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4390. #if defined(RDNA3) || defined(RDNA2)
  4391. __launch_bounds__(WARP_SIZE*NWARPS_Q5_K_RDNA2, 2)
  4392. #endif // defined(RDNA3) || defined(RDNA2)
  4393. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4394. mul_mat_q5_K(
  4395. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4396. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4397. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4398. #if defined(RDNA3) || defined(RDNA2)
  4399. const int mmq_x = MMQ_X_Q5_K_RDNA2;
  4400. const int mmq_y = MMQ_Y_Q5_K_RDNA2;
  4401. const int nwarps = NWARPS_Q5_K_RDNA2;
  4402. #else
  4403. const int mmq_x = MMQ_X_Q5_K_RDNA1;
  4404. const int mmq_y = MMQ_Y_Q5_K_RDNA1;
  4405. const int nwarps = NWARPS_Q5_K_RDNA1;
  4406. #endif // defined(RDNA3) || defined(RDNA2)
  4407. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4408. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4409. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4410. #elif __CUDA_ARCH__ >= CC_VOLTA
  4411. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  4412. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  4413. const int nwarps = NWARPS_Q5_K_AMPERE;
  4414. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4415. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4416. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4417. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4418. const int mmq_x = MMQ_X_Q5_K_PASCAL;
  4419. const int mmq_y = MMQ_Y_Q5_K_PASCAL;
  4420. const int nwarps = NWARPS_Q5_K_PASCAL;
  4421. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps, allocate_tiles_q5_K<mmq_y>,
  4422. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ, vec_dot_q5_K_q8_1_mul_mat>
  4423. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4424. #else
  4425. (void) vec_dot_q5_K_q8_1_mul_mat;
  4426. NO_DEVICE_CODE;
  4427. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4428. }
  4429. #define MMQ_X_Q6_K_RDNA2 64
  4430. #define MMQ_Y_Q6_K_RDNA2 128
  4431. #define NWARPS_Q6_K_RDNA2 8
  4432. #define MMQ_X_Q6_K_RDNA1 32
  4433. #define MMQ_Y_Q6_K_RDNA1 64
  4434. #define NWARPS_Q6_K_RDNA1 8
  4435. #if defined(CUDA_USE_TENSOR_CORES)
  4436. #define MMQ_X_Q6_K_AMPERE 4
  4437. #define MMQ_Y_Q6_K_AMPERE 32
  4438. #define NWARPS_Q6_K_AMPERE 4
  4439. #else
  4440. #define MMQ_X_Q6_K_AMPERE 64
  4441. #define MMQ_Y_Q6_K_AMPERE 64
  4442. #define NWARPS_Q6_K_AMPERE 4
  4443. #endif
  4444. #define MMQ_X_Q6_K_PASCAL 64
  4445. #define MMQ_Y_Q6_K_PASCAL 64
  4446. #define NWARPS_Q6_K_PASCAL 8
  4447. template <bool need_check> static __global__ void
  4448. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4449. #if defined(RDNA3) || defined(RDNA2)
  4450. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_RDNA2, 2)
  4451. #endif // defined(RDNA3) || defined(RDNA2)
  4452. #elif __CUDA_ARCH__ < CC_VOLTA
  4453. __launch_bounds__(WARP_SIZE*NWARPS_Q6_K_PASCAL, 2)
  4454. #endif // __CUDA_ARCH__ < CC_VOLTA
  4455. mul_mat_q6_K(
  4456. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4457. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst) {
  4458. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  4459. #if defined(RDNA3) || defined(RDNA2)
  4460. const int mmq_x = MMQ_X_Q6_K_RDNA2;
  4461. const int mmq_y = MMQ_Y_Q6_K_RDNA2;
  4462. const int nwarps = NWARPS_Q6_K_RDNA2;
  4463. #else
  4464. const int mmq_x = MMQ_X_Q6_K_RDNA1;
  4465. const int mmq_y = MMQ_Y_Q6_K_RDNA1;
  4466. const int nwarps = NWARPS_Q6_K_RDNA1;
  4467. #endif // defined(RDNA3) || defined(RDNA2)
  4468. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4469. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4470. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4471. #elif __CUDA_ARCH__ >= CC_VOLTA
  4472. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  4473. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  4474. const int nwarps = NWARPS_Q6_K_AMPERE;
  4475. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4476. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4477. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4478. #elif __CUDA_ARCH__ >= MIN_CC_DP4A
  4479. const int mmq_x = MMQ_X_Q6_K_PASCAL;
  4480. const int mmq_y = MMQ_Y_Q6_K_PASCAL;
  4481. const int nwarps = NWARPS_Q6_K_PASCAL;
  4482. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps, allocate_tiles_q6_K<mmq_y>,
  4483. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ, vec_dot_q6_K_q8_1_mul_mat>
  4484. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  4485. #else
  4486. (void) vec_dot_q6_K_q8_1_mul_mat;
  4487. NO_DEVICE_CODE;
  4488. #endif // __CUDA_ARCH__ >= CC_VOLTA
  4489. }
  4490. template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
  4491. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4492. // tell the compiler to use as many registers as it wants, see nwarps definition below
  4493. __launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
  4494. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  4495. static __global__ void mul_mat_vec_q(
  4496. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  4497. const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
  4498. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
  4499. constexpr int nwarps = 1;
  4500. constexpr int rows_per_cuda_block = 1;
  4501. #else
  4502. constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
  4503. constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
  4504. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
  4505. const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
  4506. const int row0 = rows_per_cuda_block*blockIdx.x;
  4507. const int blocks_per_row_x = ncols_x / qk;
  4508. const int blocks_per_col_y = nrows_y / QK8_1;
  4509. constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
  4510. // partial sum for each thread
  4511. float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
  4512. const block_q_t * x = (const block_q_t *) vx;
  4513. const block_q8_1 * y = (const block_q8_1 *) vy;
  4514. for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
  4515. const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
  4516. // x block quant index when casting the quants to int
  4517. const int kqs = vdr * (tid % (qi/vdr));
  4518. #pragma unroll
  4519. for (int j = 0; j < ncols_y; ++j) {
  4520. #pragma unroll
  4521. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4522. tmp[j][i] += vec_dot_q_cuda(
  4523. &x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
  4524. }
  4525. }
  4526. }
  4527. __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
  4528. if (threadIdx.y > 0) {
  4529. #pragma unroll
  4530. for (int j = 0; j < ncols_y; ++j) {
  4531. #pragma unroll
  4532. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4533. tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
  4534. }
  4535. }
  4536. }
  4537. __syncthreads();
  4538. if (threadIdx.y > 0) {
  4539. return;
  4540. }
  4541. // sum up partial sums and write back result
  4542. #pragma unroll
  4543. for (int j = 0; j < ncols_y; ++j) {
  4544. #pragma unroll
  4545. for (int i = 0; i < rows_per_cuda_block; ++i) {
  4546. #pragma unroll
  4547. for (int l = 0; l < nwarps-1; ++l) {
  4548. tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
  4549. }
  4550. tmp[j][i] = warp_reduce_sum(tmp[j][i]);
  4551. }
  4552. if (threadIdx.x < rows_per_cuda_block) {
  4553. dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
  4554. }
  4555. }
  4556. }
  4557. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  4558. static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
  4559. // qk = quantized weights per x block
  4560. // qr = number of quantized weights per data value in x block
  4561. const int row = blockIdx.x*blockDim.y + threadIdx.y;
  4562. if (row >= nrows) {
  4563. return;
  4564. }
  4565. const int tid = threadIdx.x;
  4566. const int iter_stride = 2*GGML_CUDA_DMMV_X;
  4567. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  4568. const int y_offset = qr == 1 ? 1 : qk/2;
  4569. // partial sum for each thread
  4570. #ifdef GGML_CUDA_F16
  4571. half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  4572. #else
  4573. float tmp = 0.0f;
  4574. #endif // GGML_CUDA_F16
  4575. for (int i = 0; i < ncols; i += iter_stride) {
  4576. const int col = i + vals_per_iter*tid;
  4577. const int ib = (row*ncols + col)/qk; // x block index
  4578. const int iqs = (col%qk)/qr; // x quant index
  4579. const int iybs = col - col%qk; // y block start index
  4580. // processing >2 values per i iter is faster for fast GPUs
  4581. #pragma unroll
  4582. for (int j = 0; j < vals_per_iter; j += 2) {
  4583. // process 2 vals per j iter
  4584. // dequantize
  4585. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  4586. dfloat2 v;
  4587. dequantize_kernel(vx, ib, iqs + j/qr, v);
  4588. // matrix multiplication
  4589. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  4590. #ifdef GGML_CUDA_F16
  4591. tmp += __hmul2(v, {
  4592. y[iybs + iqs + j/qr + 0],
  4593. y[iybs + iqs + j/qr + y_offset]
  4594. });
  4595. #else
  4596. tmp += v.x * y[iybs + iqs + j/qr + 0];
  4597. tmp += v.y * y[iybs + iqs + j/qr + y_offset];
  4598. #endif // GGML_CUDA_F16
  4599. }
  4600. }
  4601. // sum up partial sums and write back result
  4602. #pragma unroll
  4603. for (int mask = 16; mask > 0; mask >>= 1) {
  4604. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4605. }
  4606. if (tid == 0) {
  4607. #ifdef GGML_CUDA_F16
  4608. dst[row] = tmp.x + tmp.y;
  4609. #else
  4610. dst[row] = tmp;
  4611. #endif // GGML_CUDA_F16
  4612. }
  4613. }
  4614. static __global__ void mul_mat_p021_f16_f32(
  4615. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  4616. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y) {
  4617. const half * x = (const half *) vx;
  4618. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4619. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4620. const int channel_x = channel / (nchannels_y / nchannels_x);
  4621. const int nrows_y = ncols_x;
  4622. const int nrows_dst = nrows_x;
  4623. const int row_dst = row_x;
  4624. float tmp = 0.0f;
  4625. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4626. const int col_x = col_x0 + threadIdx.x;
  4627. if (col_x >= ncols_x) {
  4628. break;
  4629. }
  4630. // x is transposed and permuted
  4631. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  4632. const float xi = __half2float(x[ix]);
  4633. const int row_y = col_x;
  4634. // y is not transposed but permuted
  4635. const int iy = channel*nrows_y + row_y;
  4636. tmp += xi * y[iy];
  4637. }
  4638. // dst is not transposed and not permuted
  4639. const int idst = channel*nrows_dst + row_dst;
  4640. // sum up partial sums and write back result
  4641. #pragma unroll
  4642. for (int mask = 16; mask > 0; mask >>= 1) {
  4643. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4644. }
  4645. if (threadIdx.x == 0) {
  4646. dst[idst] = tmp;
  4647. }
  4648. }
  4649. static __global__ void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  4650. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  4651. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor) {
  4652. const half * x = (const half *) vx;
  4653. const int row_x = blockDim.y*blockIdx.y + threadIdx.y;
  4654. const int channel = blockDim.z*blockIdx.z + threadIdx.z;
  4655. const int channel_x = channel / channel_x_divisor;
  4656. const int nrows_y = ncols_x;
  4657. const int nrows_dst = nrows_x;
  4658. const int row_dst = row_x;
  4659. const int idst = channel*nrows_dst + row_dst;
  4660. float tmp = 0.0f;
  4661. for (int col_x0 = 0; col_x0 < ncols_x; col_x0 += blockDim.x) {
  4662. const int col_x = col_x0 + threadIdx.x;
  4663. if (col_x >= ncols_x) {
  4664. break;
  4665. }
  4666. const int row_y = col_x;
  4667. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  4668. const int iy = channel*nrows_y + row_y;
  4669. const float xi = __half2float(x[ix]);
  4670. tmp += xi * y[iy];
  4671. }
  4672. // sum up partial sums and write back result
  4673. #pragma unroll
  4674. for (int mask = 16; mask > 0; mask >>= 1) {
  4675. tmp += __shfl_xor_sync(0xffffffff, tmp, mask, 32);
  4676. }
  4677. if (threadIdx.x == 0) {
  4678. dst[idst] = tmp;
  4679. }
  4680. }
  4681. static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  4682. const float * xi = (const float *) cxi;
  4683. float * dsti = (float *) cdsti;
  4684. *dsti = *xi;
  4685. }
  4686. static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  4687. const float * xi = (const float *) cxi;
  4688. half * dsti = (half *) cdsti;
  4689. *dsti = __float2half(*xi);
  4690. }
  4691. static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  4692. const half * xi = (const half *) cxi;
  4693. half * dsti = (half *) cdsti;
  4694. *dsti = *xi;
  4695. }
  4696. static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  4697. const half * xi = (const half *) cxi;
  4698. float * dsti = (float *) cdsti;
  4699. *dsti = *xi;
  4700. }
  4701. template <cpy_kernel_t cpy_1>
  4702. static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  4703. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4704. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4705. const int nb12, const int nb13) {
  4706. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4707. if (i >= ne) {
  4708. return;
  4709. }
  4710. // determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  4711. // then combine those indices with the corresponding byte offsets to get the total offsets
  4712. const int i03 = i/(ne00 * ne01 * ne02);
  4713. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4714. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4715. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4716. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4717. const int i13 = i/(ne10 * ne11 * ne12);
  4718. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4719. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4720. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4721. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  4722. cpy_1(cx + x_offset, cdst + dst_offset);
  4723. }
  4724. static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  4725. const float * xi = (const float *) cxi;
  4726. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  4727. float amax = 0.0f; // absolute max
  4728. for (int j = 0; j < QK8_0; j++) {
  4729. const float v = xi[j];
  4730. amax = fmaxf(amax, fabsf(v));
  4731. }
  4732. const float d = amax / ((1 << 7) - 1);
  4733. const float id = d ? 1.0f/d : 0.0f;
  4734. dsti->d = d;
  4735. for (int j = 0; j < QK8_0; ++j) {
  4736. const float x0 = xi[j]*id;
  4737. dsti->qs[j] = roundf(x0);
  4738. }
  4739. }
  4740. static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  4741. const float * xi = (const float *) cxi;
  4742. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  4743. float amax = 0.0f;
  4744. float vmax = 0.0f;
  4745. for (int j = 0; j < QK4_0; ++j) {
  4746. const float v = xi[j];
  4747. if (amax < fabsf(v)) {
  4748. amax = fabsf(v);
  4749. vmax = v;
  4750. }
  4751. }
  4752. const float d = vmax / -8;
  4753. const float id = d ? 1.0f/d : 0.0f;
  4754. dsti->d = d;
  4755. for (int j = 0; j < QK4_0/2; ++j) {
  4756. const float x0 = xi[0 + j]*id;
  4757. const float x1 = xi[QK4_0/2 + j]*id;
  4758. const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
  4759. const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
  4760. dsti->qs[j] = xi0;
  4761. dsti->qs[j] |= xi1 << 4;
  4762. }
  4763. }
  4764. static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  4765. const float * xi = (const float *) cxi;
  4766. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  4767. float vmin = FLT_MAX;
  4768. float vmax = -FLT_MAX;
  4769. for (int j = 0; j < QK4_1; ++j) {
  4770. const float v = xi[j];
  4771. if (v < vmin) vmin = v;
  4772. if (v > vmax) vmax = v;
  4773. }
  4774. const float d = (vmax - vmin) / ((1 << 4) - 1);
  4775. const float id = d ? 1.0f/d : 0.0f;
  4776. dsti->dm.x = d;
  4777. dsti->dm.y = vmin;
  4778. for (int j = 0; j < QK4_1/2; ++j) {
  4779. const float x0 = (xi[0 + j] - vmin)*id;
  4780. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  4781. const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
  4782. const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
  4783. dsti->qs[j] = xi0;
  4784. dsti->qs[j] |= xi1 << 4;
  4785. }
  4786. }
  4787. template <cpy_kernel_t cpy_blck, int qk>
  4788. static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
  4789. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  4790. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  4791. const int nb12, const int nb13) {
  4792. const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
  4793. if (i >= ne) {
  4794. return;
  4795. }
  4796. const int i03 = i/(ne00 * ne01 * ne02);
  4797. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  4798. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  4799. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  4800. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  4801. const int i13 = i/(ne10 * ne11 * ne12);
  4802. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  4803. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  4804. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  4805. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  4806. cpy_blck(cx + x_offset, cdst + dst_offset);
  4807. }
  4808. static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
  4809. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  4810. return 1.0f - min(1.0f, max(0.0f, y));
  4811. }
  4812. struct rope_corr_dims {
  4813. float v[4];
  4814. };
  4815. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  4816. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  4817. static __device__ void rope_yarn(
  4818. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  4819. float * cos_theta, float * sin_theta
  4820. ) {
  4821. // Get n-d rotational scaling corrected for extrapolation
  4822. float theta_interp = freq_scale * theta_extrap;
  4823. float theta = theta_interp;
  4824. if (ext_factor != 0.0f) {
  4825. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  4826. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  4827. // Get n-d magnitude scaling corrected for interpolation
  4828. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  4829. }
  4830. *cos_theta = cosf(theta) * mscale;
  4831. *sin_theta = sinf(theta) * mscale;
  4832. }
  4833. // rope == RoPE == rotary positional embedding
  4834. template<typename T, bool has_pos>
  4835. static __global__ void rope(
  4836. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4837. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  4838. ) {
  4839. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4840. if (col >= ncols) {
  4841. return;
  4842. }
  4843. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4844. const int i = row*ncols + col;
  4845. const int i2 = row/p_delta_rows;
  4846. const int p = has_pos ? pos[i2] : 0;
  4847. const float theta_base = p*powf(freq_base, -float(col)/ncols);
  4848. float cos_theta, sin_theta;
  4849. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4850. const float x0 = x[i + 0];
  4851. const float x1 = x[i + 1];
  4852. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4853. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  4854. }
  4855. template<typename T, bool has_pos>
  4856. static __global__ void rope_neox(
  4857. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  4858. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  4859. ) {
  4860. const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
  4861. if (col >= ncols) {
  4862. return;
  4863. }
  4864. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4865. const int ib = col / n_dims;
  4866. const int ic = col % n_dims;
  4867. if (ib > 0) {
  4868. const int i = row*ncols + ib*n_dims + ic;
  4869. dst[i + 0] = x[i + 0];
  4870. dst[i + 1] = x[i + 1];
  4871. return;
  4872. }
  4873. const int i = row*ncols + ib*n_dims + ic/2;
  4874. const int i2 = row/p_delta_rows;
  4875. float cur_rot = inv_ndims * ic - ib;
  4876. const int p = has_pos ? pos[i2] : 0;
  4877. const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
  4878. float cos_theta, sin_theta;
  4879. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  4880. const float x0 = x[i + 0];
  4881. const float x1 = x[i + n_dims/2];
  4882. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4883. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  4884. }
  4885. static __global__ void rope_glm_f32(
  4886. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  4887. int n_ctx
  4888. ) {
  4889. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4890. const int half_n_dims = ncols/4;
  4891. if (col >= half_n_dims) {
  4892. return;
  4893. }
  4894. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4895. const int i = row*ncols + col;
  4896. const int i2 = row/p_delta_rows;
  4897. const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
  4898. // FIXME: this is likely wrong
  4899. const int p = pos != nullptr ? pos[i2] : 0;
  4900. const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
  4901. const float sin_theta = sinf(theta);
  4902. const float cos_theta = cosf(theta);
  4903. const float x0 = x[i + 0];
  4904. const float x1 = x[i + half_n_dims];
  4905. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  4906. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  4907. const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
  4908. const float sin_block_theta = sinf(block_theta);
  4909. const float cos_block_theta = cosf(block_theta);
  4910. const float x2 = x[i + half_n_dims * 2];
  4911. const float x3 = x[i + half_n_dims * 3];
  4912. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  4913. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  4914. }
  4915. static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  4916. const int n_heads_log2_floor, const float m0, const float m1) {
  4917. const int col = blockDim.x*blockIdx.x + threadIdx.x;
  4918. if (col >= ncols) {
  4919. return;
  4920. }
  4921. const int row = blockDim.y*blockIdx.y + threadIdx.y;
  4922. const int i = row*ncols + col;
  4923. const int k = row/k_rows;
  4924. float m_k;
  4925. if (k < n_heads_log2_floor) {
  4926. m_k = powf(m0, k + 1);
  4927. } else {
  4928. m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
  4929. }
  4930. dst[i] = col * m_k + x[i];
  4931. }
  4932. static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
  4933. const int row = blockIdx.x;
  4934. const int col = threadIdx.x;
  4935. float sum = 0.0f;
  4936. for (int i = col; i < ncols; i += blockDim.x) {
  4937. sum += x[row * ncols + i];
  4938. }
  4939. sum = warp_reduce_sum(sum);
  4940. if (col == 0) {
  4941. dst[row] = sum;
  4942. }
  4943. }
  4944. template<typename T>
  4945. static inline __device__ void swap(T & a, T & b) {
  4946. T tmp = a;
  4947. a = b;
  4948. b = tmp;
  4949. }
  4950. template<ggml_sort_order order>
  4951. static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols) {
  4952. // bitonic sort
  4953. int col = threadIdx.x;
  4954. int row = blockIdx.y;
  4955. if (col >= ncols) return;
  4956. const float * x_row = x + row * ncols;
  4957. int * dst_row = dst + row * ncols;
  4958. // initialize indices
  4959. if (col < ncols) {
  4960. dst_row[col] = col;
  4961. }
  4962. __syncthreads();
  4963. for (int k = 2; k <= ncols; k *= 2) {
  4964. for (int j = k / 2; j > 0; j /= 2) {
  4965. int ixj = col ^ j;
  4966. if (ixj > col) {
  4967. if ((col & k) == 0) {
  4968. 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]]) {
  4969. swap(dst_row[col], dst_row[ixj]);
  4970. }
  4971. } else {
  4972. 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]]) {
  4973. swap(dst_row[col], dst_row[ixj]);
  4974. }
  4975. }
  4976. }
  4977. __syncthreads();
  4978. }
  4979. }
  4980. }
  4981. static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
  4982. const int col = blockDim.y*blockIdx.y + threadIdx.y;
  4983. const int row = blockDim.x*blockIdx.x + threadIdx.x;
  4984. if (col >= ncols) {
  4985. return;
  4986. }
  4987. const int i = row*ncols + col;
  4988. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  4989. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  4990. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  4991. }
  4992. template <bool vals_smem, int ncols_template, int block_size_template>
  4993. static __global__ void soft_max_f32(const float * x, const float * mask, const float * pos, float * dst, const int ncols_par, const int nrows_y, const float scale, const float max_bias, const float m0, const float m1, uint32_t n_head_log2) {
  4994. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  4995. const int tid = threadIdx.x;
  4996. const int rowx = blockIdx.x;
  4997. const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
  4998. const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
  4999. const int warp_id = threadIdx.x / WARP_SIZE;
  5000. const int lane_id = threadIdx.x % WARP_SIZE;
  5001. float slope = 0.0f;
  5002. // ALiBi
  5003. if (max_bias > 0.0f) {
  5004. const int h = rowx/nrows_y; // head index
  5005. const float base = h < n_head_log2 ? m0 : m1;
  5006. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  5007. slope = powf(base, exp);
  5008. }
  5009. extern __shared__ float data_soft_max_f32[];
  5010. float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
  5011. // shared memory buffer to cache values between iterations:
  5012. float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
  5013. float max_val = -INFINITY;
  5014. #pragma unroll
  5015. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5016. const int col = col0 + tid;
  5017. if (ncols_template == 0 && col >= ncols) {
  5018. break;
  5019. }
  5020. const int ix = rowx*ncols + col;
  5021. const int iy = rowy*ncols + col;
  5022. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + slope*pos[col];
  5023. vals[col] = val;
  5024. max_val = max(max_val, val);
  5025. }
  5026. // find the max value in the block
  5027. max_val = warp_reduce_max(max_val);
  5028. if (block_size > WARP_SIZE) {
  5029. if (warp_id == 0) {
  5030. buf_iw[lane_id] = -INFINITY;
  5031. }
  5032. __syncthreads();
  5033. if (lane_id == 0) {
  5034. buf_iw[warp_id] = max_val;
  5035. }
  5036. __syncthreads();
  5037. max_val = buf_iw[lane_id];
  5038. max_val = warp_reduce_max(max_val);
  5039. }
  5040. float tmp = 0.0f; // partial sum
  5041. #pragma unroll
  5042. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5043. const int col = col0 + tid;
  5044. if (ncols_template == 0 && col >= ncols) {
  5045. break;
  5046. }
  5047. const float val = expf(vals[col] - max_val);
  5048. tmp += val;
  5049. vals[col] = val;
  5050. }
  5051. // find the sum of exps in the block
  5052. tmp = warp_reduce_sum(tmp);
  5053. if (block_size > WARP_SIZE) {
  5054. if (warp_id == 0) {
  5055. buf_iw[lane_id] = 0.0f;
  5056. }
  5057. __syncthreads();
  5058. if (lane_id == 0) {
  5059. buf_iw[warp_id] = tmp;
  5060. }
  5061. __syncthreads();
  5062. tmp = buf_iw[lane_id];
  5063. tmp = warp_reduce_sum(tmp);
  5064. }
  5065. const float inv_sum = 1.0f / tmp;
  5066. #pragma unroll
  5067. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  5068. const int col = col0 + tid;
  5069. if (ncols_template == 0 && col >= ncols) {
  5070. return;
  5071. }
  5072. const int idst = rowx*ncols + col;
  5073. dst[idst] = vals[col] * inv_sum;
  5074. }
  5075. }
  5076. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  5077. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5078. if (i >= k) {
  5079. return;
  5080. }
  5081. dst[i] = scale * x[i];
  5082. }
  5083. static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
  5084. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  5085. if (i >= k) {
  5086. return;
  5087. }
  5088. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  5089. }
  5090. template <typename T>
  5091. static __global__ void im2col_kernel(
  5092. const float * x, T * dst, int batch_offset,
  5093. int offset_delta, int IC, int IW, int IH, int OH, int OW, int KW, int KH, int pelements, int CHW,
  5094. int s0, int s1, int p0, int p1, int d0, int d1) {
  5095. const int i = threadIdx.x + blockIdx.x * blockDim.x;
  5096. if (i >= pelements) {
  5097. return;
  5098. }
  5099. const int ksize = OW * (KH > 1 ? KW : 1);
  5100. const int kx = i / ksize;
  5101. const int kd = kx * ksize;
  5102. const int ky = (i - kd) / OW;
  5103. const int ix = i % OW;
  5104. const int oh = blockIdx.y;
  5105. const int batch = blockIdx.z / IC;
  5106. const int ic = blockIdx.z % IC;
  5107. const int64_t iiw = ix * s0 + kx * d0 - p0;
  5108. const int64_t iih = oh * s1 + ky * d1 - p1;
  5109. const int64_t offset_dst =
  5110. ((batch * OH + oh) * OW + ix) * CHW +
  5111. (ic * (KW * KH) + ky * KW + kx);
  5112. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  5113. dst[offset_dst] = 0.0f;
  5114. } else {
  5115. const int64_t offset_src = ic * offset_delta + batch * batch_offset;
  5116. dst[offset_dst] = x[offset_src + iih * IW + iiw];
  5117. }
  5118. }
  5119. template <typename Ti, typename To>
  5120. static __global__ void pool2d_nchw_kernel(
  5121. const int ih, const int iw, const int oh, const int ow,
  5122. const int kh, const int kw, const int sh, const int sw,
  5123. const int ph, const int pw, const int parallel_elements,
  5124. const Ti* src, To* dst, const enum ggml_op_pool op) {
  5125. int idx = threadIdx.x + blockIdx.x * blockDim.x;
  5126. if (idx >= parallel_elements) {
  5127. return;
  5128. }
  5129. const int I_HW = ih * iw;
  5130. const int O_HW = oh * ow;
  5131. const int nc = idx / O_HW;
  5132. const int cur_oh = idx % O_HW / ow;
  5133. const int cur_ow = idx % O_HW % ow;
  5134. const Ti* i_ptr = src + nc * I_HW;
  5135. To* o_ptr = dst + nc * O_HW;
  5136. const int start_h = cur_oh * sh - ph;
  5137. const int bh = max(0, start_h);
  5138. const int eh = min(ih, start_h + kh);
  5139. const int start_w = cur_ow * sw - pw;
  5140. const int bw = max(0, start_w);
  5141. const int ew = min(iw, start_w + kw);
  5142. const To scale = 1. / (kh * kw);
  5143. To res = 0;
  5144. switch (op) {
  5145. case GGML_OP_POOL_AVG: res = 0; break;
  5146. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  5147. }
  5148. for (int i = bh; i < eh; i += 1) {
  5149. for (int j = bw; j < ew; j += 1) {
  5150. #if __CUDA_ARCH__ >= 350
  5151. Ti cur = __ldg(i_ptr + i * iw + j);
  5152. #else
  5153. Ti cur = i_ptr[i * iw + j];
  5154. #endif
  5155. switch (op) {
  5156. case GGML_OP_POOL_AVG: res += cur * scale; break;
  5157. case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
  5158. }
  5159. }
  5160. }
  5161. o_ptr[cur_oh * ow + cur_ow] = res;
  5162. }
  5163. template<int qk, int qr, dequantize_kernel_t dq>
  5164. static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5165. const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5166. GGML_TENSOR_BINARY_OP_LOCALS
  5167. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5168. const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
  5169. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5170. // strides in elements
  5171. //const size_t s0 = nb0 / ggml_element_size(dst);
  5172. const size_t s1 = nb1 / ggml_element_size(dst);
  5173. const size_t s2 = nb2 / ggml_element_size(dst);
  5174. const size_t s3 = nb3 / ggml_element_size(dst);
  5175. const size_t s10 = nb10 / ggml_element_size(src1);
  5176. const size_t s11 = nb11 / ggml_element_size(src1);
  5177. const size_t s12 = nb12 / ggml_element_size(src1);
  5178. //const size_t s13 = nb13 / ggml_element_size(src1);
  5179. GGML_ASSERT(ne00 % 2 == 0);
  5180. k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
  5181. src0_dd, src1_dd, dst_dd,
  5182. ne00, /*ne01, ne02, ne03,*/
  5183. /*ne10, ne11,*/ ne12, /*ne13,*/
  5184. /* s0,*/ s1, s2, s3,
  5185. /* nb00,*/ nb01, nb02, nb03,
  5186. s10, s11, s12/*, s13*/);
  5187. (void) dst;
  5188. }
  5189. template<typename src0_t>
  5190. static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  5191. const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
  5192. GGML_TENSOR_BINARY_OP_LOCALS
  5193. const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
  5194. const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
  5195. const dim3 block_nums(block_num_x, ne10, ne11*ne12);
  5196. // strides in elements
  5197. //const size_t s0 = nb0 / ggml_element_size(dst);
  5198. const size_t s1 = nb1 / ggml_element_size(dst);
  5199. const size_t s2 = nb2 / ggml_element_size(dst);
  5200. const size_t s3 = nb3 / ggml_element_size(dst);
  5201. const size_t s10 = nb10 / ggml_element_size(src1);
  5202. const size_t s11 = nb11 / ggml_element_size(src1);
  5203. const size_t s12 = nb12 / ggml_element_size(src1);
  5204. //const size_t s13 = nb13 / ggml_element_size(src1);
  5205. k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
  5206. src0_dd, src1_dd, dst_dd,
  5207. ne00, /*ne01, ne02, ne03,*/
  5208. /*ne10, ne11,*/ ne12, /*ne13,*/
  5209. /* s0,*/ s1, s2, s3,
  5210. /* nb00,*/ nb01, nb02, nb03,
  5211. s10, s11, s12/*, s13*/);
  5212. (void) dst;
  5213. }
  5214. template<float (*bin_op)(const float, const float)>
  5215. struct bin_bcast_cuda {
  5216. template<typename src0_t, typename src1_t, typename dst_t>
  5217. void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
  5218. const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
  5219. cudaStream_t stream) {
  5220. GGML_TENSOR_BINARY_OP_LOCALS
  5221. int nr0 = ne10/ne0;
  5222. int nr1 = ne11/ne1;
  5223. int nr2 = ne12/ne2;
  5224. int nr3 = ne13/ne3;
  5225. int nr[4] = { nr0, nr1, nr2, nr3 };
  5226. // collapse dimensions until first broadcast dimension
  5227. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  5228. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  5229. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  5230. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  5231. auto collapse = [](int64_t cne[]) {
  5232. cne[0] *= cne[1];
  5233. cne[1] = cne[2];
  5234. cne[2] = cne[3];
  5235. cne[3] = 1;
  5236. };
  5237. auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
  5238. cnb[1] *= cne[1];
  5239. cnb[2] *= cne[2];
  5240. cnb[3] *= cne[3];
  5241. };
  5242. for (int i = 0; i < 4; i++) {
  5243. if (nr[i] != 1) {
  5244. break;
  5245. }
  5246. if (i > 0) {
  5247. collapse_nb(cnb0, cne0);
  5248. collapse_nb(cnb1, cne1);
  5249. collapse(cne0);
  5250. collapse(cne1);
  5251. }
  5252. }
  5253. {
  5254. int64_t ne0 = cne0[0];
  5255. int64_t ne1 = cne0[1];
  5256. int64_t ne2 = cne0[2];
  5257. int64_t ne3 = cne0[3];
  5258. int64_t ne10 = cne1[0];
  5259. int64_t ne11 = cne1[1];
  5260. int64_t ne12 = cne1[2];
  5261. int64_t ne13 = cne1[3];
  5262. size_t nb0 = cnb0[0];
  5263. size_t nb1 = cnb0[1];
  5264. size_t nb2 = cnb0[2];
  5265. size_t nb3 = cnb0[3];
  5266. size_t nb10 = cnb1[0];
  5267. size_t nb11 = cnb1[1];
  5268. size_t nb12 = cnb1[2];
  5269. size_t nb13 = cnb1[3];
  5270. size_t s0 = nb0 / sizeof(dst_t);
  5271. size_t s1 = nb1 / sizeof(dst_t);
  5272. size_t s2 = nb2 / sizeof(dst_t);
  5273. size_t s3 = nb3 / sizeof(dst_t);
  5274. size_t s10 = nb10 / sizeof(src1_t);
  5275. size_t s11 = nb11 / sizeof(src1_t);
  5276. size_t s12 = nb12 / sizeof(src1_t);
  5277. size_t s13 = nb13 / sizeof(src1_t);
  5278. GGML_ASSERT(s0 == 1);
  5279. GGML_ASSERT(s10 == 1);
  5280. const int block_size = 128;
  5281. int64_t hne0 = std::max(ne0/2LL, 1LL);
  5282. dim3 block_dims;
  5283. block_dims.x = std::min<unsigned int>(hne0, block_size);
  5284. block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
  5285. block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
  5286. dim3 block_nums(
  5287. (hne0 + block_dims.x - 1) / block_dims.x,
  5288. (ne1 + block_dims.y - 1) / block_dims.y,
  5289. (ne2*ne3 + block_dims.z - 1) / block_dims.z
  5290. );
  5291. if (block_nums.z > 65535) {
  5292. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  5293. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  5294. k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
  5295. src0_dd, src1_dd, dst_dd,
  5296. ne0, ne1, ne2, ne3,
  5297. ne10, ne11, ne12, ne13,
  5298. /* s0, */ s1, s2, s3,
  5299. /* s10, */ s11, s12, s13);
  5300. } else {
  5301. k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
  5302. src0_dd, src1_dd, dst_dd,
  5303. ne0, ne1, ne2, ne3,
  5304. ne10, ne11, ne12, ne13,
  5305. /* s0, */ s1, s2, s3,
  5306. /* s10, */ s11, s12, s13);
  5307. }
  5308. }
  5309. }
  5310. };
  5311. static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
  5312. const int ne10, const int ne11, const int ne12,
  5313. const int nb1, const int nb2, const int offset, cudaStream_t stream) {
  5314. int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
  5315. acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
  5316. }
  5317. static void gelu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5318. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5319. gelu_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5320. }
  5321. static void silu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5322. const int num_blocks = (k + CUDA_SILU_BLOCK_SIZE - 1) / CUDA_SILU_BLOCK_SIZE;
  5323. silu_f32<<<num_blocks, CUDA_SILU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5324. }
  5325. static void gelu_quick_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5326. const int num_blocks = (k + CUDA_GELU_BLOCK_SIZE - 1) / CUDA_GELU_BLOCK_SIZE;
  5327. gelu_quick_f32<<<num_blocks, CUDA_GELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5328. }
  5329. static void tanh_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5330. const int num_blocks = (k + CUDA_TANH_BLOCK_SIZE - 1) / CUDA_TANH_BLOCK_SIZE;
  5331. tanh_f32<<<num_blocks, CUDA_TANH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5332. }
  5333. static void relu_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5334. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5335. relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5336. }
  5337. static void hardsigmoid_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5338. const int num_blocks = (k + CUDA_HARDSIGMOID_BLOCK_SIZE - 1) / CUDA_HARDSIGMOID_BLOCK_SIZE;
  5339. hardsigmoid_f32<<<num_blocks, CUDA_HARDSIGMOID_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5340. }
  5341. static void hardswish_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5342. const int num_blocks = (k + CUDA_HARDSWISH_BLOCK_SIZE - 1) / CUDA_HARDSWISH_BLOCK_SIZE;
  5343. hardswish_f32<<<num_blocks, CUDA_HARDSWISH_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5344. }
  5345. static void leaky_relu_f32_cuda(const float * x, float * dst, const int k, const float negative_slope, cudaStream_t stream) {
  5346. const int num_blocks = (k + CUDA_RELU_BLOCK_SIZE - 1) / CUDA_RELU_BLOCK_SIZE;
  5347. leaky_relu_f32<<<num_blocks, CUDA_RELU_BLOCK_SIZE, 0, stream>>>(x, dst, k, negative_slope);
  5348. }
  5349. static void sqr_f32_cuda(const float * x, float * dst, const int k, cudaStream_t stream) {
  5350. const int num_blocks = (k + CUDA_SQR_BLOCK_SIZE - 1) / CUDA_SQR_BLOCK_SIZE;
  5351. sqr_f32<<<num_blocks, CUDA_SQR_BLOCK_SIZE, 0, stream>>>(x, dst, k);
  5352. }
  5353. static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5354. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5355. if (ncols < 1024) {
  5356. const dim3 block_dims(WARP_SIZE, 1, 1);
  5357. norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5358. } else {
  5359. const dim3 block_dims(1024, 1, 1);
  5360. norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5361. }
  5362. }
  5363. 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) {
  5364. static const float eps = 1e-6f;
  5365. if (group_size < 1024) {
  5366. const dim3 block_dims(WARP_SIZE, 1, 1);
  5367. group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5368. } else {
  5369. const dim3 block_dims(1024, 1, 1);
  5370. group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
  5371. }
  5372. }
  5373. 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) {
  5374. int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
  5375. dim3 gridDim(num_blocks, ne1, ne2);
  5376. concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
  5377. }
  5378. 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) {
  5379. int ne0 = (ne00 * scale_factor);
  5380. int num_blocks = (ne0 + CUDA_UPSCALE_BLOCK_SIZE - 1) / CUDA_UPSCALE_BLOCK_SIZE;
  5381. dim3 gridDim(num_blocks, (ne01 * scale_factor), ne02);
  5382. upscale_f32<<<gridDim, CUDA_UPSCALE_BLOCK_SIZE, 0, stream>>>(x, dst, ne00, ne00 * ne01, scale_factor);
  5383. }
  5384. static void pad_f32_cuda(const float * x, float * dst,
  5385. const int ne00, const int ne01, const int ne02,
  5386. const int ne0, const int ne1, const int ne2, cudaStream_t stream) {
  5387. int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
  5388. dim3 gridDim(num_blocks, ne1, ne2);
  5389. pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02);
  5390. }
  5391. static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
  5392. GGML_ASSERT(ncols % WARP_SIZE == 0);
  5393. if (ncols < 1024) {
  5394. const dim3 block_dims(WARP_SIZE, 1, 1);
  5395. rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5396. } else {
  5397. const dim3 block_dims(1024, 1, 1);
  5398. rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
  5399. }
  5400. }
  5401. 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) {
  5402. const int block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
  5403. const dim3 num_blocks(block_num_x, ky, 1);
  5404. const dim3 block_size(CUDA_DEQUANTIZE_BLOCK_SIZE, 1, 1);
  5405. quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
  5406. }
  5407. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5408. static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5409. const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
  5410. dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5411. }
  5412. static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int k, cudaStream_t stream) {
  5413. const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
  5414. if (k % CUDA_Q8_0_NE_ALIGN == 0) {
  5415. const bool need_check = false;
  5416. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5417. } else {
  5418. const bool need_check = true;
  5419. dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
  5420. }
  5421. }
  5422. template<typename dst_t>
  5423. static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5424. const int nb = k / QK_K;
  5425. #if QK_K == 256
  5426. dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
  5427. #else
  5428. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  5429. #endif
  5430. }
  5431. template<typename dst_t>
  5432. static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5433. const int nb = k / QK_K;
  5434. #if QK_K == 256
  5435. dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
  5436. #else
  5437. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  5438. #endif
  5439. }
  5440. template<typename dst_t>
  5441. static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5442. const int nb32 = k / 32;
  5443. const int nb = (k + 255) / 256;
  5444. dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5445. }
  5446. template<typename dst_t>
  5447. static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5448. const int nb32 = k / 32;
  5449. const int nb = (k + 255) / 256;
  5450. dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
  5451. }
  5452. template<typename dst_t>
  5453. static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5454. const int nb = k / QK_K;
  5455. dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
  5456. }
  5457. template<typename dst_t>
  5458. static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5459. const int nb = k / QK_K;
  5460. #if QK_K == 256
  5461. dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
  5462. #else
  5463. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  5464. #endif
  5465. }
  5466. template<typename dst_t>
  5467. static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5468. const int nb = k / QK_K;
  5469. #if QK_K == 256
  5470. dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
  5471. #else
  5472. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  5473. #endif
  5474. }
  5475. template<typename dst_t>
  5476. static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5477. const int nb = k / QK_K;
  5478. dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5479. }
  5480. template<typename dst_t>
  5481. static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5482. const int nb = k / QK_K;
  5483. dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
  5484. }
  5485. template<typename dst_t>
  5486. static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5487. const int nb = k / QK_K;
  5488. dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
  5489. }
  5490. template<typename dst_t>
  5491. static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int k, cudaStream_t stream) {
  5492. const int nb = k / QK_K;
  5493. dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
  5494. }
  5495. template <typename src_t, typename dst_t>
  5496. static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int k, cudaStream_t stream) {
  5497. const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
  5498. convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
  5499. }
  5500. static to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
  5501. int id;
  5502. switch (type) {
  5503. case GGML_TYPE_Q4_0:
  5504. return dequantize_row_q4_0_cuda;
  5505. case GGML_TYPE_Q4_1:
  5506. return dequantize_row_q4_1_cuda;
  5507. case GGML_TYPE_Q5_0:
  5508. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5509. case GGML_TYPE_Q5_1:
  5510. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5511. case GGML_TYPE_Q8_0:
  5512. CUDA_CHECK(cudaGetDevice(&id));
  5513. if (g_device_caps[id].cc >= CC_PASCAL) {
  5514. return dequantize_block_q8_0_f16_cuda;
  5515. }
  5516. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5517. case GGML_TYPE_Q2_K:
  5518. return dequantize_row_q2_K_cuda;
  5519. case GGML_TYPE_Q3_K:
  5520. return dequantize_row_q3_K_cuda;
  5521. case GGML_TYPE_Q4_K:
  5522. return dequantize_row_q4_K_cuda;
  5523. case GGML_TYPE_Q5_K:
  5524. return dequantize_row_q5_K_cuda;
  5525. case GGML_TYPE_Q6_K:
  5526. return dequantize_row_q6_K_cuda;
  5527. case GGML_TYPE_IQ2_XXS:
  5528. return dequantize_row_iq2_xxs_cuda;
  5529. case GGML_TYPE_IQ2_XS:
  5530. return dequantize_row_iq2_xs_cuda;
  5531. case GGML_TYPE_IQ3_XXS:
  5532. return dequantize_row_iq3_xxs_cuda;
  5533. case GGML_TYPE_IQ1_S:
  5534. return dequantize_row_iq1_s_cuda;
  5535. case GGML_TYPE_F32:
  5536. return convert_unary_cuda<float>;
  5537. default:
  5538. return nullptr;
  5539. }
  5540. }
  5541. static to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
  5542. switch (type) {
  5543. case GGML_TYPE_Q4_0:
  5544. return dequantize_row_q4_0_cuda;
  5545. case GGML_TYPE_Q4_1:
  5546. return dequantize_row_q4_1_cuda;
  5547. case GGML_TYPE_Q5_0:
  5548. return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
  5549. case GGML_TYPE_Q5_1:
  5550. return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
  5551. case GGML_TYPE_Q8_0:
  5552. return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
  5553. case GGML_TYPE_Q2_K:
  5554. return dequantize_row_q2_K_cuda;
  5555. case GGML_TYPE_Q3_K:
  5556. return dequantize_row_q3_K_cuda;
  5557. case GGML_TYPE_Q4_K:
  5558. return dequantize_row_q4_K_cuda;
  5559. case GGML_TYPE_Q5_K:
  5560. return dequantize_row_q5_K_cuda;
  5561. case GGML_TYPE_Q6_K:
  5562. return dequantize_row_q6_K_cuda;
  5563. case GGML_TYPE_IQ2_XXS:
  5564. return dequantize_row_iq2_xxs_cuda;
  5565. case GGML_TYPE_IQ2_XS:
  5566. return dequantize_row_iq2_xs_cuda;
  5567. case GGML_TYPE_IQ3_XXS:
  5568. return dequantize_row_iq3_xxs_cuda;
  5569. case GGML_TYPE_IQ1_S:
  5570. return dequantize_row_iq1_s_cuda;
  5571. case GGML_TYPE_F16:
  5572. return convert_unary_cuda<half>;
  5573. default:
  5574. return nullptr;
  5575. }
  5576. }
  5577. 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) {
  5578. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5579. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5580. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  5581. const dim3 block_nums(block_num_y, 1, 1);
  5582. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5583. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
  5584. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5585. }
  5586. 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) {
  5587. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5588. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5589. const dim3 block_nums(block_num_y, 1, 1);
  5590. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5591. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
  5592. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5593. }
  5594. 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) {
  5595. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5596. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5597. const dim3 block_nums(block_num_y, 1, 1);
  5598. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5599. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
  5600. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5601. }
  5602. 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) {
  5603. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5604. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5605. const dim3 block_nums(block_num_y, 1, 1);
  5606. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5607. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
  5608. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5609. }
  5610. 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) {
  5611. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5612. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5613. const dim3 block_nums(block_num_y, 1, 1);
  5614. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5615. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
  5616. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5617. }
  5618. 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) {
  5619. GGML_ASSERT(ncols % QK_K == 0);
  5620. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  5621. const int block_num_y = (nrows + ny - 1) / ny;
  5622. const dim3 block_nums(block_num_y, 1, 1);
  5623. const dim3 block_dims(32, ny, 1);
  5624. dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5625. }
  5626. 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) {
  5627. GGML_ASSERT(ncols % QK_K == 0);
  5628. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5629. const int block_num_y = (nrows + ny - 1) / ny;
  5630. const dim3 block_nums(block_num_y, 1, 1);
  5631. const dim3 block_dims(32, ny, 1);
  5632. dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5633. }
  5634. 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) {
  5635. GGML_ASSERT(ncols % QK_K == 0);
  5636. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5637. const int block_num_y = (nrows + ny - 1) / ny;
  5638. const dim3 block_nums(block_num_y, 1, 1);
  5639. const dim3 block_dims(32, ny, 1);
  5640. dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5641. }
  5642. 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) {
  5643. GGML_ASSERT(ncols % QK_K == 0);
  5644. const dim3 block_dims(32, 1, 1);
  5645. dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
  5646. }
  5647. 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) {
  5648. GGML_ASSERT(ncols % QK_K == 0);
  5649. const int ny = 2 / K_QUANTS_PER_ITERATION;
  5650. const int block_num_y = (nrows + ny - 1) / ny;
  5651. const dim3 block_nums(block_num_y, 1, 1);
  5652. const dim3 block_dims(32, ny, 1);
  5653. dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5654. }
  5655. 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) {
  5656. GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
  5657. const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
  5658. const dim3 block_nums(block_num_y, 1, 1);
  5659. const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
  5660. dequantize_mul_mat_vec<1, 1, convert_f16>
  5661. <<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
  5662. }
  5663. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
  5664. static void mul_mat_vec_q_cuda(
  5665. const void * vx, const void * vy, float * dst,
  5666. const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
  5667. GGML_ASSERT(ncols_x % qk == 0);
  5668. GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
  5669. int id;
  5670. CUDA_CHECK(cudaGetDevice(&id));
  5671. int64_t nwarps = 1;
  5672. int64_t rows_per_cuda_block = 1;
  5673. if (g_device_caps[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
  5674. switch(ncols_y) {
  5675. case 1:
  5676. nwarps = 4;
  5677. rows_per_cuda_block = 1;
  5678. break;
  5679. case 2:
  5680. case 3:
  5681. case 4:
  5682. nwarps = 4;
  5683. rows_per_cuda_block = 2;
  5684. break;
  5685. case 5:
  5686. case 6:
  5687. case 7:
  5688. case 8:
  5689. nwarps = 2;
  5690. rows_per_cuda_block = 2;
  5691. break;
  5692. default:
  5693. GGML_ASSERT(false);
  5694. break;
  5695. }
  5696. }
  5697. const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
  5698. const dim3 block_nums(nblocks, 1, 1);
  5699. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5700. switch (ncols_y) {
  5701. case 1:
  5702. mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
  5703. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5704. break;
  5705. case 2:
  5706. mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
  5707. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5708. break;
  5709. case 3:
  5710. mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
  5711. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5712. break;
  5713. case 4:
  5714. mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
  5715. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5716. break;
  5717. case 5:
  5718. mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
  5719. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5720. break;
  5721. case 6:
  5722. mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
  5723. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5724. break;
  5725. case 7:
  5726. mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
  5727. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5728. break;
  5729. case 8:
  5730. mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
  5731. <<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
  5732. break;
  5733. default:
  5734. GGML_ASSERT(false);
  5735. break;
  5736. }
  5737. }
  5738. static void ggml_mul_mat_q4_0_q8_1_cuda(
  5739. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5740. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5741. int id;
  5742. CUDA_CHECK(cudaGetDevice(&id));
  5743. const int compute_capability = g_device_caps[id].cc;
  5744. int mmq_x, mmq_y, nwarps;
  5745. if (compute_capability >= CC_RDNA2) {
  5746. mmq_x = MMQ_X_Q4_0_RDNA2;
  5747. mmq_y = MMQ_Y_Q4_0_RDNA2;
  5748. nwarps = NWARPS_Q4_0_RDNA2;
  5749. } else if (compute_capability >= CC_OFFSET_AMD) {
  5750. mmq_x = MMQ_X_Q4_0_RDNA1;
  5751. mmq_y = MMQ_Y_Q4_0_RDNA1;
  5752. nwarps = NWARPS_Q4_0_RDNA1;
  5753. } else if (compute_capability >= CC_VOLTA) {
  5754. mmq_x = MMQ_X_Q4_0_AMPERE;
  5755. mmq_y = MMQ_Y_Q4_0_AMPERE;
  5756. nwarps = NWARPS_Q4_0_AMPERE;
  5757. } else if (compute_capability >= MIN_CC_DP4A) {
  5758. mmq_x = MMQ_X_Q4_0_PASCAL;
  5759. mmq_y = MMQ_Y_Q4_0_PASCAL;
  5760. nwarps = NWARPS_Q4_0_PASCAL;
  5761. } else {
  5762. GGML_ASSERT(false);
  5763. }
  5764. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5765. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5766. const dim3 block_nums(block_num_x, block_num_y, 1);
  5767. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5768. if (nrows_x % mmq_y == 0) {
  5769. const bool need_check = false;
  5770. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5771. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5772. } else {
  5773. const bool need_check = true;
  5774. mul_mat_q4_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5775. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5776. }
  5777. }
  5778. static void ggml_mul_mat_q4_1_q8_1_cuda(
  5779. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5780. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5781. int id;
  5782. CUDA_CHECK(cudaGetDevice(&id));
  5783. const int compute_capability = g_device_caps[id].cc;
  5784. int mmq_x, mmq_y, nwarps;
  5785. if (compute_capability >= CC_RDNA2) {
  5786. mmq_x = MMQ_X_Q4_1_RDNA2;
  5787. mmq_y = MMQ_Y_Q4_1_RDNA2;
  5788. nwarps = NWARPS_Q4_1_RDNA2;
  5789. } else if (compute_capability >= CC_OFFSET_AMD) {
  5790. mmq_x = MMQ_X_Q4_1_RDNA1;
  5791. mmq_y = MMQ_Y_Q4_1_RDNA1;
  5792. nwarps = NWARPS_Q4_1_RDNA1;
  5793. } else if (compute_capability >= CC_VOLTA) {
  5794. mmq_x = MMQ_X_Q4_1_AMPERE;
  5795. mmq_y = MMQ_Y_Q4_1_AMPERE;
  5796. nwarps = NWARPS_Q4_1_AMPERE;
  5797. } else if (compute_capability >= MIN_CC_DP4A) {
  5798. mmq_x = MMQ_X_Q4_1_PASCAL;
  5799. mmq_y = MMQ_Y_Q4_1_PASCAL;
  5800. nwarps = NWARPS_Q4_1_PASCAL;
  5801. } else {
  5802. GGML_ASSERT(false);
  5803. }
  5804. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5805. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5806. const dim3 block_nums(block_num_x, block_num_y, 1);
  5807. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5808. if (nrows_x % mmq_y == 0) {
  5809. const bool need_check = false;
  5810. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5811. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5812. } else {
  5813. const bool need_check = true;
  5814. mul_mat_q4_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5815. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5816. }
  5817. }
  5818. static void ggml_mul_mat_q5_0_q8_1_cuda(
  5819. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5820. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5821. int id;
  5822. CUDA_CHECK(cudaGetDevice(&id));
  5823. const int compute_capability = g_device_caps[id].cc;
  5824. int mmq_x, mmq_y, nwarps;
  5825. if (compute_capability >= CC_RDNA2) {
  5826. mmq_x = MMQ_X_Q5_0_RDNA2;
  5827. mmq_y = MMQ_Y_Q5_0_RDNA2;
  5828. nwarps = NWARPS_Q5_0_RDNA2;
  5829. } else if (compute_capability >= CC_OFFSET_AMD) {
  5830. mmq_x = MMQ_X_Q5_0_RDNA1;
  5831. mmq_y = MMQ_Y_Q5_0_RDNA1;
  5832. nwarps = NWARPS_Q5_0_RDNA1;
  5833. } else if (compute_capability >= CC_VOLTA) {
  5834. mmq_x = MMQ_X_Q5_0_AMPERE;
  5835. mmq_y = MMQ_Y_Q5_0_AMPERE;
  5836. nwarps = NWARPS_Q5_0_AMPERE;
  5837. } else if (compute_capability >= MIN_CC_DP4A) {
  5838. mmq_x = MMQ_X_Q5_0_PASCAL;
  5839. mmq_y = MMQ_Y_Q5_0_PASCAL;
  5840. nwarps = NWARPS_Q5_0_PASCAL;
  5841. } else {
  5842. GGML_ASSERT(false);
  5843. }
  5844. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5845. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5846. const dim3 block_nums(block_num_x, block_num_y, 1);
  5847. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5848. if (nrows_x % mmq_y == 0) {
  5849. const bool need_check = false;
  5850. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5851. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5852. } else {
  5853. const bool need_check = true;
  5854. mul_mat_q5_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5855. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5856. }
  5857. }
  5858. static void ggml_mul_mat_q5_1_q8_1_cuda(
  5859. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5860. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5861. int id;
  5862. CUDA_CHECK(cudaGetDevice(&id));
  5863. const int compute_capability = g_device_caps[id].cc;
  5864. int mmq_x, mmq_y, nwarps;
  5865. if (compute_capability >= CC_RDNA2) {
  5866. mmq_x = MMQ_X_Q5_1_RDNA2;
  5867. mmq_y = MMQ_Y_Q5_1_RDNA2;
  5868. nwarps = NWARPS_Q5_1_RDNA2;
  5869. } else if (compute_capability >= CC_OFFSET_AMD) {
  5870. mmq_x = MMQ_X_Q5_1_RDNA1;
  5871. mmq_y = MMQ_Y_Q5_1_RDNA1;
  5872. nwarps = NWARPS_Q5_1_RDNA1;
  5873. } else if (compute_capability >= CC_VOLTA) {
  5874. mmq_x = MMQ_X_Q5_1_AMPERE;
  5875. mmq_y = MMQ_Y_Q5_1_AMPERE;
  5876. nwarps = NWARPS_Q5_1_AMPERE;
  5877. } else if (compute_capability >= MIN_CC_DP4A) {
  5878. mmq_x = MMQ_X_Q5_1_PASCAL;
  5879. mmq_y = MMQ_Y_Q5_1_PASCAL;
  5880. nwarps = NWARPS_Q5_1_PASCAL;
  5881. } else {
  5882. GGML_ASSERT(false);
  5883. }
  5884. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5885. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5886. const dim3 block_nums(block_num_x, block_num_y, 1);
  5887. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5888. if (nrows_x % mmq_y == 0) {
  5889. const bool need_check = false;
  5890. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5891. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5892. } else {
  5893. const bool need_check = true;
  5894. mul_mat_q5_1<need_check><<<block_nums, block_dims, 0, stream>>>
  5895. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5896. }
  5897. }
  5898. static void ggml_mul_mat_q8_0_q8_1_cuda(
  5899. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5900. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5901. int id;
  5902. CUDA_CHECK(cudaGetDevice(&id));
  5903. const int compute_capability = g_device_caps[id].cc;
  5904. int mmq_x, mmq_y, nwarps;
  5905. if (compute_capability >= CC_RDNA2) {
  5906. mmq_x = MMQ_X_Q8_0_RDNA2;
  5907. mmq_y = MMQ_Y_Q8_0_RDNA2;
  5908. nwarps = NWARPS_Q8_0_RDNA2;
  5909. } else if (compute_capability >= CC_OFFSET_AMD) {
  5910. mmq_x = MMQ_X_Q8_0_RDNA1;
  5911. mmq_y = MMQ_Y_Q8_0_RDNA1;
  5912. nwarps = NWARPS_Q8_0_RDNA1;
  5913. } else if (compute_capability >= CC_VOLTA) {
  5914. mmq_x = MMQ_X_Q8_0_AMPERE;
  5915. mmq_y = MMQ_Y_Q8_0_AMPERE;
  5916. nwarps = NWARPS_Q8_0_AMPERE;
  5917. } else if (compute_capability >= MIN_CC_DP4A) {
  5918. mmq_x = MMQ_X_Q8_0_PASCAL;
  5919. mmq_y = MMQ_Y_Q8_0_PASCAL;
  5920. nwarps = NWARPS_Q8_0_PASCAL;
  5921. } else {
  5922. GGML_ASSERT(false);
  5923. }
  5924. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5925. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5926. const dim3 block_nums(block_num_x, block_num_y, 1);
  5927. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5928. if (nrows_x % mmq_y == 0) {
  5929. const bool need_check = false;
  5930. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5931. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5932. } else {
  5933. const bool need_check = true;
  5934. mul_mat_q8_0<need_check><<<block_nums, block_dims, 0, stream>>>
  5935. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5936. }
  5937. }
  5938. static void ggml_mul_mat_q2_K_q8_1_cuda(
  5939. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5940. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5941. int id;
  5942. CUDA_CHECK(cudaGetDevice(&id));
  5943. const int compute_capability = g_device_caps[id].cc;
  5944. int mmq_x, mmq_y, nwarps;
  5945. if (compute_capability >= CC_RDNA2) {
  5946. mmq_x = MMQ_X_Q2_K_RDNA2;
  5947. mmq_y = MMQ_Y_Q2_K_RDNA2;
  5948. nwarps = NWARPS_Q2_K_RDNA2;
  5949. } else if (compute_capability >= CC_OFFSET_AMD) {
  5950. mmq_x = MMQ_X_Q2_K_RDNA1;
  5951. mmq_y = MMQ_Y_Q2_K_RDNA1;
  5952. nwarps = NWARPS_Q2_K_RDNA1;
  5953. } else if (compute_capability >= CC_VOLTA) {
  5954. mmq_x = MMQ_X_Q2_K_AMPERE;
  5955. mmq_y = MMQ_Y_Q2_K_AMPERE;
  5956. nwarps = NWARPS_Q2_K_AMPERE;
  5957. } else if (compute_capability >= MIN_CC_DP4A) {
  5958. mmq_x = MMQ_X_Q2_K_PASCAL;
  5959. mmq_y = MMQ_Y_Q2_K_PASCAL;
  5960. nwarps = NWARPS_Q2_K_PASCAL;
  5961. } else {
  5962. GGML_ASSERT(false);
  5963. }
  5964. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  5965. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  5966. const dim3 block_nums(block_num_x, block_num_y, 1);
  5967. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  5968. if (nrows_x % mmq_y == 0) {
  5969. const bool need_check = false;
  5970. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5971. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5972. } else {
  5973. const bool need_check = true;
  5974. mul_mat_q2_K<need_check><<<block_nums, block_dims, 0, stream>>>
  5975. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  5976. }
  5977. }
  5978. static void ggml_mul_mat_q3_K_q8_1_cuda(
  5979. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  5980. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  5981. #if QK_K == 256
  5982. int id;
  5983. CUDA_CHECK(cudaGetDevice(&id));
  5984. const int compute_capability = g_device_caps[id].cc;
  5985. int mmq_x, mmq_y, nwarps;
  5986. if (compute_capability >= CC_RDNA2) {
  5987. mmq_x = MMQ_X_Q3_K_RDNA2;
  5988. mmq_y = MMQ_Y_Q3_K_RDNA2;
  5989. nwarps = NWARPS_Q3_K_RDNA2;
  5990. } else if (compute_capability >= CC_OFFSET_AMD) {
  5991. mmq_x = MMQ_X_Q3_K_RDNA1;
  5992. mmq_y = MMQ_Y_Q3_K_RDNA1;
  5993. nwarps = NWARPS_Q3_K_RDNA1;
  5994. } else if (compute_capability >= CC_VOLTA) {
  5995. mmq_x = MMQ_X_Q3_K_AMPERE;
  5996. mmq_y = MMQ_Y_Q3_K_AMPERE;
  5997. nwarps = NWARPS_Q3_K_AMPERE;
  5998. } else if (compute_capability >= MIN_CC_DP4A) {
  5999. mmq_x = MMQ_X_Q3_K_PASCAL;
  6000. mmq_y = MMQ_Y_Q3_K_PASCAL;
  6001. nwarps = NWARPS_Q3_K_PASCAL;
  6002. } else {
  6003. GGML_ASSERT(false);
  6004. }
  6005. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6006. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6007. const dim3 block_nums(block_num_x, block_num_y, 1);
  6008. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6009. if (nrows_x % mmq_y == 0) {
  6010. const bool need_check = false;
  6011. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6012. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6013. } else {
  6014. const bool need_check = true;
  6015. mul_mat_q3_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6016. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6017. }
  6018. #endif
  6019. }
  6020. static void ggml_mul_mat_q4_K_q8_1_cuda(
  6021. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6022. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6023. int id;
  6024. CUDA_CHECK(cudaGetDevice(&id));
  6025. const int compute_capability = g_device_caps[id].cc;
  6026. int mmq_x, mmq_y, nwarps;
  6027. if (compute_capability >= CC_RDNA2) {
  6028. mmq_x = MMQ_X_Q4_K_RDNA2;
  6029. mmq_y = MMQ_Y_Q4_K_RDNA2;
  6030. nwarps = NWARPS_Q4_K_RDNA2;
  6031. } else if (compute_capability >= CC_OFFSET_AMD) {
  6032. mmq_x = MMQ_X_Q4_K_RDNA1;
  6033. mmq_y = MMQ_Y_Q4_K_RDNA1;
  6034. nwarps = NWARPS_Q4_K_RDNA1;
  6035. } else if (compute_capability >= CC_VOLTA) {
  6036. mmq_x = MMQ_X_Q4_K_AMPERE;
  6037. mmq_y = MMQ_Y_Q4_K_AMPERE;
  6038. nwarps = NWARPS_Q4_K_AMPERE;
  6039. } else if (compute_capability >= MIN_CC_DP4A) {
  6040. mmq_x = MMQ_X_Q4_K_PASCAL;
  6041. mmq_y = MMQ_Y_Q4_K_PASCAL;
  6042. nwarps = NWARPS_Q4_K_PASCAL;
  6043. } else {
  6044. GGML_ASSERT(false);
  6045. }
  6046. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6047. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6048. const dim3 block_nums(block_num_x, block_num_y, 1);
  6049. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6050. if (nrows_x % mmq_y == 0) {
  6051. const bool need_check = false;
  6052. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6053. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6054. } else {
  6055. const bool need_check = true;
  6056. mul_mat_q4_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6057. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6058. }
  6059. }
  6060. static void ggml_mul_mat_q5_K_q8_1_cuda(
  6061. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6062. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6063. int id;
  6064. CUDA_CHECK(cudaGetDevice(&id));
  6065. const int compute_capability = g_device_caps[id].cc;
  6066. int mmq_x, mmq_y, nwarps;
  6067. if (compute_capability >= CC_RDNA2) {
  6068. mmq_x = MMQ_X_Q5_K_RDNA2;
  6069. mmq_y = MMQ_Y_Q5_K_RDNA2;
  6070. nwarps = NWARPS_Q5_K_RDNA2;
  6071. } else if (compute_capability >= CC_OFFSET_AMD) {
  6072. mmq_x = MMQ_X_Q5_K_RDNA1;
  6073. mmq_y = MMQ_Y_Q5_K_RDNA1;
  6074. nwarps = NWARPS_Q5_K_RDNA1;
  6075. } else if (compute_capability >= CC_VOLTA) {
  6076. mmq_x = MMQ_X_Q5_K_AMPERE;
  6077. mmq_y = MMQ_Y_Q5_K_AMPERE;
  6078. nwarps = NWARPS_Q5_K_AMPERE;
  6079. } else if (compute_capability >= MIN_CC_DP4A) {
  6080. mmq_x = MMQ_X_Q5_K_PASCAL;
  6081. mmq_y = MMQ_Y_Q5_K_PASCAL;
  6082. nwarps = NWARPS_Q5_K_PASCAL;
  6083. } else {
  6084. GGML_ASSERT(false);
  6085. }
  6086. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6087. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6088. const dim3 block_nums(block_num_x, block_num_y, 1);
  6089. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6090. if (nrows_x % mmq_y == 0) {
  6091. const bool need_check = false;
  6092. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6093. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6094. } else {
  6095. const bool need_check = true;
  6096. mul_mat_q5_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6097. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6098. }
  6099. }
  6100. static void ggml_mul_mat_q6_K_q8_1_cuda(
  6101. const void * vx, const void * vy, float * dst, const int ncols_x, const int nrows_x,
  6102. const int ncols_y, const int nrows_y, const int nrows_dst, cudaStream_t stream) {
  6103. int id;
  6104. CUDA_CHECK(cudaGetDevice(&id));
  6105. const int compute_capability = g_device_caps[id].cc;
  6106. int mmq_x, mmq_y, nwarps;
  6107. if (compute_capability >= CC_RDNA2) {
  6108. mmq_x = MMQ_X_Q6_K_RDNA2;
  6109. mmq_y = MMQ_Y_Q6_K_RDNA2;
  6110. nwarps = NWARPS_Q6_K_RDNA2;
  6111. } else if (compute_capability >= CC_OFFSET_AMD) {
  6112. mmq_x = MMQ_X_Q6_K_RDNA1;
  6113. mmq_y = MMQ_Y_Q6_K_RDNA1;
  6114. nwarps = NWARPS_Q6_K_RDNA1;
  6115. } else if (compute_capability >= CC_VOLTA) {
  6116. mmq_x = MMQ_X_Q6_K_AMPERE;
  6117. mmq_y = MMQ_Y_Q6_K_AMPERE;
  6118. nwarps = NWARPS_Q6_K_AMPERE;
  6119. } else if (compute_capability >= MIN_CC_DP4A) {
  6120. mmq_x = MMQ_X_Q6_K_PASCAL;
  6121. mmq_y = MMQ_Y_Q6_K_PASCAL;
  6122. nwarps = NWARPS_Q6_K_PASCAL;
  6123. } else {
  6124. GGML_ASSERT(false);
  6125. }
  6126. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  6127. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  6128. const dim3 block_nums(block_num_x, block_num_y, 1);
  6129. const dim3 block_dims(WARP_SIZE, nwarps, 1);
  6130. if (nrows_x % mmq_y == 0) {
  6131. const bool need_check = false;
  6132. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6133. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6134. } else {
  6135. const bool need_check = true;
  6136. mul_mat_q6_K<need_check><<<block_nums, block_dims, 0, stream>>>
  6137. (vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst);
  6138. }
  6139. }
  6140. static void ggml_mul_mat_p021_f16_f32_cuda(
  6141. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x,
  6142. const int nchannels_x, const int nchannels_y, cudaStream_t stream) {
  6143. const dim3 block_nums(1, nrows_x, nchannels_y);
  6144. const dim3 block_dims(WARP_SIZE, 1, 1);
  6145. mul_mat_p021_f16_f32<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols_x, nrows_x, nchannels_x, nchannels_y);
  6146. }
  6147. static void ggml_mul_mat_vec_nc_f16_f32_cuda(
  6148. const void * vx, const float * y, float * dst, const int ncols_x, const int nrows_x, const int row_stride_x,
  6149. const int nchannels_x, const int nchannels_y, const int channel_stride_x, cudaStream_t stream) {
  6150. const dim3 block_nums(1, nrows_x, nchannels_y);
  6151. const dim3 block_dims(WARP_SIZE, 1, 1);
  6152. mul_mat_vec_nc_f16_f32<<<block_nums, block_dims, 0, stream>>>
  6153. (vx, y, dst, ncols_x, nrows_x, row_stride_x, channel_stride_x, nchannels_y/nchannels_x);
  6154. }
  6155. static void ggml_cpy_f16_f32_cuda(
  6156. const char * cx, char * cdst, const int ne,
  6157. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6158. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6159. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6160. cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6161. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6162. }
  6163. static void ggml_cpy_f32_f32_cuda(
  6164. const char * cx, char * cdst, const int ne,
  6165. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6166. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6167. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6168. cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6169. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6170. }
  6171. static void ggml_cpy_f32_f16_cuda(
  6172. const char * cx, char * cdst, const int ne,
  6173. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6174. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6175. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6176. cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6177. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6178. }
  6179. static void ggml_cpy_f32_q8_0_cuda(
  6180. const char * cx, char * cdst, const int ne,
  6181. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6182. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6183. GGML_ASSERT(ne % QK8_0 == 0);
  6184. const int num_blocks = ne / QK8_0;
  6185. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
  6186. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6187. }
  6188. static void ggml_cpy_f32_q4_0_cuda(
  6189. const char * cx, char * cdst, const int ne,
  6190. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6191. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6192. GGML_ASSERT(ne % QK4_0 == 0);
  6193. const int num_blocks = ne / QK4_0;
  6194. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
  6195. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6196. }
  6197. static void ggml_cpy_f32_q4_1_cuda(
  6198. const char * cx, char * cdst, const int ne,
  6199. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6200. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6201. GGML_ASSERT(ne % QK4_1 == 0);
  6202. const int num_blocks = ne / QK4_1;
  6203. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
  6204. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6205. }
  6206. static void ggml_cpy_f16_f16_cuda(
  6207. const char * cx, char * cdst, const int ne,
  6208. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6209. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
  6210. const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
  6211. cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
  6212. (cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
  6213. }
  6214. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  6215. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  6216. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  6217. }
  6218. static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
  6219. const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
  6220. clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
  6221. }
  6222. template<typename T>
  6223. static void rope_cuda(
  6224. const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6225. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6226. ) {
  6227. GGML_ASSERT(ncols % 2 == 0);
  6228. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6229. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6230. const dim3 block_nums(nrows, num_blocks_x, 1);
  6231. if (pos == nullptr) {
  6232. rope<T, false><<<block_nums, block_dims, 0, stream>>>(
  6233. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6234. );
  6235. } else {
  6236. rope<T, true><<<block_nums, block_dims, 0, stream>>>(
  6237. x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
  6238. );
  6239. }
  6240. }
  6241. template<typename T>
  6242. static void rope_neox_cuda(
  6243. const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6244. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
  6245. ) {
  6246. GGML_ASSERT(ncols % 2 == 0);
  6247. const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
  6248. const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
  6249. const dim3 block_nums(nrows, num_blocks_x, 1);
  6250. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  6251. const float inv_ndims = -1.0f / n_dims;
  6252. if (pos == nullptr) {
  6253. rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
  6254. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6255. theta_scale, inv_ndims
  6256. );
  6257. } else {
  6258. rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
  6259. x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
  6260. theta_scale, inv_ndims
  6261. );
  6262. }
  6263. }
  6264. static void rope_glm_f32_cuda(
  6265. const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
  6266. float freq_base, int n_ctx, cudaStream_t stream
  6267. ) {
  6268. GGML_ASSERT(ncols % 4 == 0);
  6269. const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
  6270. const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
  6271. const dim3 block_nums(num_blocks_x, nrows, 1);
  6272. rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
  6273. }
  6274. static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
  6275. const int k_rows, const int n_heads_log2_floor, const float m0,
  6276. const float m1, cudaStream_t stream) {
  6277. const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
  6278. const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
  6279. const dim3 block_nums(num_blocks_x, nrows, 1);
  6280. alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
  6281. }
  6282. static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
  6283. const dim3 block_dims(WARP_SIZE, 1, 1);
  6284. const dim3 block_nums(nrows, 1, 1);
  6285. k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6286. }
  6287. static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
  6288. // bitonic sort requires ncols to be power of 2
  6289. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  6290. const dim3 block_dims(ncols, 1, 1);
  6291. const dim3 block_nums(1, nrows, 1);
  6292. if (order == GGML_SORT_ASC) {
  6293. k_argsort_f32_i32<GGML_SORT_ASC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6294. } else if (order == GGML_SORT_DESC) {
  6295. k_argsort_f32_i32<GGML_SORT_DESC><<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
  6296. } else {
  6297. GGML_ASSERT(false);
  6298. }
  6299. }
  6300. 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) {
  6301. const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
  6302. const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
  6303. const dim3 block_nums(nrows_x, block_num_x, 1);
  6304. diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
  6305. }
  6306. static void soft_max_f32_cuda(const float * x, const float * mask, const float * pos, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, const float max_bias, cudaStream_t stream) {
  6307. int nth = WARP_SIZE;
  6308. while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  6309. const dim3 block_dims(nth, 1, 1);
  6310. const dim3 block_nums(nrows_x, 1, 1);
  6311. const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
  6312. static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  6313. const uint32_t n_head_kv = nrows_x/nrows_y;
  6314. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  6315. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  6316. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  6317. if (shmem < g_device_caps[g_main_device].smpb) {
  6318. switch (ncols_x) {
  6319. case 32:
  6320. soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6321. break;
  6322. case 64:
  6323. soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6324. break;
  6325. case 128:
  6326. soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6327. break;
  6328. case 256:
  6329. soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6330. break;
  6331. case 512:
  6332. soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6333. break;
  6334. case 1024:
  6335. soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6336. break;
  6337. case 2048:
  6338. soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6339. break;
  6340. case 4096:
  6341. soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6342. break;
  6343. default:
  6344. soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6345. break;
  6346. }
  6347. } else {
  6348. const size_t shmem_low = WARP_SIZE*sizeof(float);
  6349. soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
  6350. }
  6351. }
  6352. template <typename T>
  6353. static void im2col_cuda(const float* x, T* dst,
  6354. int IW, int IH, int OW, int OH, int KW, int KH, int IC,
  6355. int batch, int batch_offset, int offset_delta,
  6356. int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
  6357. const int parallel_elements = OW * KW * KH;
  6358. const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
  6359. dim3 block_nums(num_blocks, OH, batch * IC);
  6360. im2col_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
  6361. }
  6362. // buffer pool for cuda
  6363. #define MAX_CUDA_BUFFERS 256
  6364. struct scoped_spin_lock {
  6365. std::atomic_flag& lock;
  6366. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  6367. while (lock.test_and_set(std::memory_order_acquire)) {
  6368. ; // spin
  6369. }
  6370. }
  6371. ~scoped_spin_lock() {
  6372. lock.clear(std::memory_order_release);
  6373. }
  6374. scoped_spin_lock(const scoped_spin_lock&) = delete;
  6375. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  6376. };
  6377. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  6378. // #define DEBUG_CUDA_MALLOC
  6379. struct ggml_cuda_buffer {
  6380. void * ptr = nullptr;
  6381. size_t size = 0;
  6382. };
  6383. static ggml_cuda_buffer g_cuda_buffer_pool[GGML_CUDA_MAX_DEVICES][MAX_CUDA_BUFFERS];
  6384. static size_t g_cuda_pool_size[GGML_CUDA_MAX_DEVICES] = {0};
  6385. static void * ggml_cuda_pool_malloc_leg(int device, size_t size, size_t * actual_size) {
  6386. scoped_spin_lock lock(g_cuda_pool_lock);
  6387. #ifdef DEBUG_CUDA_MALLOC
  6388. int nnz = 0;
  6389. size_t max_size = 0;
  6390. #endif
  6391. size_t best_diff = 1ull << 36;
  6392. int ibest = -1;
  6393. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6394. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6395. if (b.ptr != nullptr) {
  6396. #ifdef DEBUG_CUDA_MALLOC
  6397. ++nnz;
  6398. if (b.size > max_size) max_size = b.size;
  6399. #endif
  6400. if (b.size >= size) {
  6401. size_t diff = b.size - size;
  6402. if (diff < best_diff) {
  6403. best_diff = diff;
  6404. ibest = i;
  6405. if (!best_diff) {
  6406. void * ptr = b.ptr;
  6407. *actual_size = b.size;
  6408. b.ptr = nullptr;
  6409. b.size = 0;
  6410. return ptr;
  6411. }
  6412. }
  6413. }
  6414. }
  6415. }
  6416. if (ibest >= 0) {
  6417. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][ibest];
  6418. void * ptr = b.ptr;
  6419. *actual_size = b.size;
  6420. b.ptr = nullptr;
  6421. b.size = 0;
  6422. return ptr;
  6423. }
  6424. void * ptr;
  6425. size_t look_ahead_size = (size_t) (1.05 * size);
  6426. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  6427. ggml_cuda_set_device(device);
  6428. CUDA_CHECK(cudaMalloc((void **) &ptr, look_ahead_size));
  6429. *actual_size = look_ahead_size;
  6430. g_cuda_pool_size[device] += look_ahead_size;
  6431. #ifdef DEBUG_CUDA_MALLOC
  6432. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  6433. (uint32_t)(max_size/1024/1024), (uint32_t)(g_cuda_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  6434. #endif
  6435. return ptr;
  6436. }
  6437. static void ggml_cuda_pool_free_leg(int device, void * ptr, size_t size) {
  6438. scoped_spin_lock lock(g_cuda_pool_lock);
  6439. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  6440. ggml_cuda_buffer& b = g_cuda_buffer_pool[device][i];
  6441. if (b.ptr == nullptr) {
  6442. b.ptr = ptr;
  6443. b.size = size;
  6444. return;
  6445. }
  6446. }
  6447. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  6448. ggml_cuda_set_device(device);
  6449. CUDA_CHECK(cudaFree(ptr));
  6450. g_cuda_pool_size[device] -= size;
  6451. }
  6452. #if !defined(GGML_USE_HIPBLAS)
  6453. // pool with virtual memory
  6454. static CUdeviceptr g_cuda_pool_addr[GGML_CUDA_MAX_DEVICES] = {0};
  6455. static size_t g_cuda_pool_used[GGML_CUDA_MAX_DEVICES] = {0};
  6456. static const size_t CUDA_POOL_VMM_MAX_SIZE = 1ull << 35; // 32 GB
  6457. static void * ggml_cuda_pool_malloc_vmm(int device, size_t size, size_t * actual_size) {
  6458. scoped_spin_lock lock(g_cuda_pool_lock);
  6459. // round up the allocation size to the alignment to ensure that all allocations are aligned for all data types
  6460. const size_t alignment = 128;
  6461. size = alignment * ((size + alignment - 1) / alignment);
  6462. size_t avail = g_cuda_pool_size[device] - g_cuda_pool_used[device];
  6463. if (size > avail) {
  6464. // round up to the next multiple of the granularity
  6465. size_t reserve_size = size - avail;
  6466. const size_t granularity = g_device_caps[device].vmm_granularity;
  6467. reserve_size = granularity * ((reserve_size + granularity - 1) / granularity);
  6468. GGML_ASSERT(g_cuda_pool_size[device] + reserve_size <= CUDA_POOL_VMM_MAX_SIZE);
  6469. // allocate more physical memory
  6470. CUmemAllocationProp prop = {};
  6471. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6472. prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6473. prop.location.id = device;
  6474. CUmemGenericAllocationHandle handle;
  6475. CU_CHECK(cuMemCreate(&handle, reserve_size, &prop, 0));
  6476. // reserve virtual address space (if not already reserved)
  6477. if (g_cuda_pool_addr[device] == 0) {
  6478. CU_CHECK(cuMemAddressReserve(&g_cuda_pool_addr[device], CUDA_POOL_VMM_MAX_SIZE, 0, 0, 0));
  6479. }
  6480. // map at the end of the pool
  6481. CU_CHECK(cuMemMap(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, 0, handle, 0));
  6482. // the memory allocation handle is no longer needed after mapping
  6483. CU_CHECK(cuMemRelease(handle));
  6484. // set access
  6485. CUmemAccessDesc access = {};
  6486. access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6487. access.location.id = device;
  6488. access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
  6489. CU_CHECK(cuMemSetAccess(g_cuda_pool_addr[device] + g_cuda_pool_size[device], reserve_size, &access, 1));
  6490. // add to the pool
  6491. g_cuda_pool_size[device] += reserve_size;
  6492. //printf("cuda pool[%d]: size increased to %llu MB (reserved %llu MB)\n",
  6493. // id, (unsigned long long) (g_cuda_pool_size[id]/1024/1024),
  6494. // (unsigned long long) (reserve_size/1024/1024));
  6495. }
  6496. GGML_ASSERT(g_cuda_pool_addr[device] != 0);
  6497. void * ptr = (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]);
  6498. *actual_size = size;
  6499. g_cuda_pool_used[device] += size;
  6500. #ifdef DEBUG_CUDA_MALLOC
  6501. printf("cuda pool[%d]: allocated %llu bytes at %llx [%s]\n", id, (unsigned long long) size, ptr);
  6502. #endif
  6503. return ptr;
  6504. }
  6505. static void ggml_cuda_pool_free_vmm(int device, void * ptr, size_t size) {
  6506. scoped_spin_lock lock(g_cuda_pool_lock);
  6507. #ifdef DEBUG_CUDA_MALLOC
  6508. printf("cuda pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  6509. #endif
  6510. g_cuda_pool_used[device] -= size;
  6511. // all deallocations must be in reverse order of the allocations
  6512. GGML_ASSERT(ptr == (void *) (g_cuda_pool_addr[device] + g_cuda_pool_used[device]));
  6513. }
  6514. static void * ggml_cuda_pool_malloc(int device, size_t size, size_t * actual_size) {
  6515. if (g_device_caps[device].vmm) {
  6516. return ggml_cuda_pool_malloc_vmm(device, size, actual_size);
  6517. } else {
  6518. return ggml_cuda_pool_malloc_leg(device, size, actual_size);
  6519. }
  6520. }
  6521. static void ggml_cuda_pool_free(int device, void * ptr, size_t size) {
  6522. if (g_device_caps[device].vmm) {
  6523. ggml_cuda_pool_free_vmm(device, ptr, size);
  6524. } else {
  6525. ggml_cuda_pool_free_leg(device, ptr, size);
  6526. }
  6527. }
  6528. #else
  6529. #define ggml_cuda_pool_malloc ggml_cuda_pool_malloc_leg
  6530. #define ggml_cuda_pool_free ggml_cuda_pool_free_leg
  6531. #endif // !defined(GGML_USE_HIPBLAS)
  6532. template<typename T>
  6533. struct cuda_pool_alloc {
  6534. int device = -1;
  6535. T * ptr = nullptr;
  6536. size_t actual_size = 0;
  6537. // size is in number of elements
  6538. T * alloc(size_t size) {
  6539. GGML_ASSERT(ptr == nullptr);
  6540. CUDA_CHECK(cudaGetDevice(&device));
  6541. ptr = (T *) ggml_cuda_pool_malloc(device, size * sizeof(T), &this->actual_size);
  6542. return ptr;
  6543. }
  6544. cuda_pool_alloc(size_t size) {
  6545. alloc(size);
  6546. }
  6547. ~cuda_pool_alloc() {
  6548. if (ptr != nullptr) {
  6549. ggml_cuda_pool_free(device, ptr, actual_size);
  6550. }
  6551. }
  6552. T * get() {
  6553. return ptr;
  6554. }
  6555. cuda_pool_alloc() = default;
  6556. cuda_pool_alloc(const cuda_pool_alloc &) = delete;
  6557. cuda_pool_alloc(cuda_pool_alloc &&) = delete;
  6558. cuda_pool_alloc& operator=(const cuda_pool_alloc &) = delete;
  6559. cuda_pool_alloc& operator=(cuda_pool_alloc &&) = delete;
  6560. };
  6561. static bool g_cublas_loaded = false;
  6562. GGML_CALL bool ggml_cublas_loaded(void) {
  6563. return g_cublas_loaded;
  6564. }
  6565. GGML_CALL void ggml_init_cublas() {
  6566. static bool initialized = false;
  6567. if (!initialized) {
  6568. #ifdef __HIP_PLATFORM_AMD__
  6569. // Workaround for a rocBLAS bug when using multiple graphics cards:
  6570. // https://github.com/ROCmSoftwarePlatform/rocBLAS/issues/1346
  6571. rocblas_initialize();
  6572. CUDA_CHECK(cudaDeviceSynchronize());
  6573. #endif
  6574. if (cudaGetDeviceCount(&g_device_count) != cudaSuccess) {
  6575. initialized = true;
  6576. g_cublas_loaded = false;
  6577. fprintf(stderr, "%s: no " GGML_CUDA_NAME " devices found, " GGML_CUDA_NAME " will be disabled\n", __func__);
  6578. return;
  6579. }
  6580. GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
  6581. int64_t total_vram = 0;
  6582. #if defined(GGML_CUDA_FORCE_MMQ)
  6583. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
  6584. #else
  6585. fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: no\n", __func__);
  6586. #endif
  6587. #if defined(CUDA_USE_TENSOR_CORES)
  6588. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: yes\n", __func__);
  6589. #else
  6590. fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
  6591. #endif
  6592. fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
  6593. for (int id = 0; id < g_device_count; ++id) {
  6594. int device_vmm = 0;
  6595. #if !defined(GGML_USE_HIPBLAS)
  6596. CUdevice device;
  6597. CU_CHECK(cuDeviceGet(&device, id));
  6598. CU_CHECK(cuDeviceGetAttribute(&device_vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, device));
  6599. if (device_vmm) {
  6600. CUmemAllocationProp alloc_prop = {};
  6601. alloc_prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
  6602. alloc_prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
  6603. alloc_prop.location.id = id;
  6604. CU_CHECK(cuMemGetAllocationGranularity(&g_device_caps[id].vmm_granularity, &alloc_prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
  6605. }
  6606. #endif // !defined(GGML_USE_HIPBLAS)
  6607. g_device_caps[id].vmm = !!device_vmm;
  6608. cudaDeviceProp prop;
  6609. CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
  6610. fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
  6611. g_default_tensor_split[id] = total_vram;
  6612. total_vram += prop.totalGlobalMem;
  6613. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6614. g_device_caps[id].cc = 100*prop.major + 10*prop.minor + CC_OFFSET_AMD;
  6615. #else
  6616. g_device_caps[id].cc = 100*prop.major + 10*prop.minor;
  6617. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  6618. g_device_caps[id].smpb = prop.sharedMemPerBlock;
  6619. }
  6620. for (int id = 0; id < g_device_count; ++id) {
  6621. g_default_tensor_split[id] /= total_vram;
  6622. }
  6623. for (int id = 0; id < g_device_count; ++id) {
  6624. ggml_cuda_set_device(id);
  6625. // create cuda streams
  6626. for (int is = 0; is < MAX_STREAMS; ++is) {
  6627. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStreams[id][is], cudaStreamNonBlocking));
  6628. }
  6629. // create cublas handle
  6630. CUBLAS_CHECK(cublasCreate(&g_cublas_handles[id]));
  6631. CUBLAS_CHECK(cublasSetMathMode(g_cublas_handles[id], CUBLAS_TF32_TENSOR_OP_MATH));
  6632. }
  6633. // configure logging to stdout
  6634. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, nullptr));
  6635. initialized = true;
  6636. g_cublas_loaded = true;
  6637. }
  6638. }
  6639. GGML_CALL void * ggml_cuda_host_malloc(size_t size) {
  6640. if (getenv("GGML_CUDA_NO_PINNED") != nullptr) {
  6641. return nullptr;
  6642. }
  6643. void * ptr = nullptr;
  6644. cudaError_t err = cudaMallocHost((void **) &ptr, size);
  6645. if (err != cudaSuccess) {
  6646. // clear the error
  6647. cudaGetLastError();
  6648. fprintf(stderr, "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  6649. size/1024.0/1024.0, cudaGetErrorString(err));
  6650. return nullptr;
  6651. }
  6652. return ptr;
  6653. }
  6654. GGML_CALL void ggml_cuda_host_free(void * ptr) {
  6655. CUDA_CHECK(cudaFreeHost(ptr));
  6656. }
  6657. static cudaError_t ggml_cuda_cpy_tensor_2d(
  6658. void * dst, const struct ggml_tensor * src, int64_t i3, int64_t i2, int64_t i1_low, int64_t i1_high, cudaStream_t stream) {
  6659. cudaMemcpyKind kind;
  6660. char * src_ptr;
  6661. if (src->backend == GGML_BACKEND_CPU) {
  6662. kind = cudaMemcpyHostToDevice;
  6663. src_ptr = (char *) src->data;
  6664. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  6665. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  6666. kind = cudaMemcpyDeviceToDevice;
  6667. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  6668. int id;
  6669. CUDA_CHECK(cudaGetDevice(&id));
  6670. src_ptr = (char *) extra->data_device[id];
  6671. } else {
  6672. GGML_ASSERT(false);
  6673. }
  6674. char * dst_ptr = (char *) dst;
  6675. const int64_t ne0 = src->ne[0];
  6676. const int64_t nb0 = src->nb[0];
  6677. const int64_t nb1 = src->nb[1];
  6678. const int64_t nb2 = src->nb[2];
  6679. const int64_t nb3 = src->nb[3];
  6680. const enum ggml_type type = src->type;
  6681. const int64_t ts = ggml_type_size(type);
  6682. const int64_t bs = ggml_blck_size(type);
  6683. int64_t i1_diff = i1_high - i1_low;
  6684. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  6685. if (nb0 == ts && nb1 == ts*ne0/bs) {
  6686. return cudaMemcpyAsync(dst_ptr, x, i1_diff*nb1, kind, stream);
  6687. } else if (nb0 == ts) {
  6688. return cudaMemcpy2DAsync(dst_ptr, ts*ne0/bs, x, nb1, ts*ne0/bs, i1_diff, kind, stream);
  6689. } else {
  6690. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  6691. const void * rx = (const void *) ((const char *) x + i1*nb1);
  6692. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  6693. // pretend the row is a matrix with cols=1
  6694. cudaError_t r = cudaMemcpy2DAsync(rd, ts/bs, rx, nb0, ts/bs, ne0, kind, stream);
  6695. if (r != cudaSuccess) return r;
  6696. }
  6697. return cudaSuccess;
  6698. }
  6699. }
  6700. static void ggml_cuda_op_get_rows(
  6701. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6702. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t stream) {
  6703. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  6704. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6705. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  6706. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  6707. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  6708. const int32_t * src1_i32 = (const int32_t *) src1_d;
  6709. switch (src0->type) {
  6710. case GGML_TYPE_F16:
  6711. get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
  6712. break;
  6713. case GGML_TYPE_F32:
  6714. get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6715. break;
  6716. case GGML_TYPE_Q4_0:
  6717. get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6718. break;
  6719. case GGML_TYPE_Q4_1:
  6720. get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6721. break;
  6722. case GGML_TYPE_Q5_0:
  6723. get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6724. break;
  6725. case GGML_TYPE_Q5_1:
  6726. get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6727. break;
  6728. case GGML_TYPE_Q8_0:
  6729. get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  6730. break;
  6731. default:
  6732. // TODO: k-quants
  6733. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  6734. GGML_ASSERT(false);
  6735. break;
  6736. }
  6737. }
  6738. template<class op>
  6739. static void ggml_cuda_op_bin_bcast(
  6740. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6741. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6742. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6743. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  6744. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6745. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  6746. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, (half *) dst_dd, main_stream);
  6747. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  6748. op()(src0, src1, dst, (const half *) src0_dd, src1_dd, dst_dd, main_stream);
  6749. } else {
  6750. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  6751. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  6752. GGML_ASSERT(false);
  6753. }
  6754. }
  6755. static void ggml_cuda_op_repeat(
  6756. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6757. const float * src0_d, const float * src1_d, float * dst_d, cudaStream_t main_stream) {
  6758. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  6759. (void) src1;
  6760. (void) src1_d;
  6761. }
  6762. static void ggml_cuda_op_add(
  6763. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6764. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6765. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6766. }
  6767. static void ggml_cuda_op_acc(
  6768. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6769. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6770. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6771. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6772. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6773. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  6774. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  6775. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  6776. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  6777. int offset = dst->op_params[3] / 4; // offset in bytes
  6778. 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);
  6779. (void) dst;
  6780. }
  6781. static void ggml_cuda_op_mul(
  6782. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6783. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6784. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6785. }
  6786. static void ggml_cuda_op_div(
  6787. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6788. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6789. ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  6790. }
  6791. static void ggml_cuda_op_gelu(
  6792. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6793. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6794. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6795. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6796. gelu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6797. (void) src1;
  6798. (void) dst;
  6799. (void) src1_dd;
  6800. }
  6801. static void ggml_cuda_op_silu(
  6802. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6803. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6804. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6805. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6806. silu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6807. (void) src1;
  6808. (void) dst;
  6809. (void) src1_dd;
  6810. }
  6811. static void ggml_cuda_op_gelu_quick(
  6812. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6813. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6814. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6815. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6816. gelu_quick_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6817. (void) src1;
  6818. (void) dst;
  6819. (void) src1_dd;
  6820. }
  6821. static void ggml_cuda_op_tanh(
  6822. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6823. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6824. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6825. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6826. tanh_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6827. (void) src1;
  6828. (void) dst;
  6829. (void) src1_dd;
  6830. }
  6831. static void ggml_cuda_op_relu(
  6832. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6833. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6834. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6835. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6836. relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6837. (void) src1;
  6838. (void) dst;
  6839. (void) src1_dd;
  6840. }
  6841. static void ggml_cuda_op_hardsigmoid(
  6842. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6843. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6844. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6845. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6846. hardsigmoid_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6847. (void) src1;
  6848. (void) dst;
  6849. (void) src1_dd;
  6850. }
  6851. static void ggml_cuda_op_hardswish(
  6852. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6853. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6854. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6855. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6856. hardswish_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6857. (void) src1;
  6858. (void) dst;
  6859. (void) src1_dd;
  6860. }
  6861. static void ggml_cuda_op_leaky_relu(
  6862. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6863. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6864. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6865. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6866. float negative_slope;
  6867. memcpy(&negative_slope, dst->op_params, sizeof(float));
  6868. leaky_relu_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  6869. (void) src1;
  6870. (void) dst;
  6871. (void) src1_dd;
  6872. }
  6873. static void ggml_cuda_op_sqr(
  6874. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6875. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6876. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6877. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6878. sqr_f32_cuda(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  6879. (void) src1;
  6880. (void) dst;
  6881. (void) src1_dd;
  6882. }
  6883. static void ggml_cuda_op_norm(
  6884. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6885. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6886. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6887. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6888. const int64_t ne00 = src0->ne[0];
  6889. const int64_t nrows = ggml_nrows(src0);
  6890. float eps;
  6891. memcpy(&eps, dst->op_params, sizeof(float));
  6892. norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6893. (void) src1;
  6894. (void) dst;
  6895. (void) src1_dd;
  6896. }
  6897. static void ggml_cuda_op_group_norm(
  6898. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6899. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6900. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6901. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6902. int num_groups = dst->op_params[0];
  6903. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  6904. group_norm_f32_cuda(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  6905. (void) src1;
  6906. (void) dst;
  6907. (void) src1_dd;
  6908. }
  6909. static void ggml_cuda_op_concat(
  6910. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6911. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6912. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6913. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6914. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6915. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  6916. 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);
  6917. }
  6918. (void) src1;
  6919. (void) dst;
  6920. }
  6921. static void ggml_cuda_op_upscale(
  6922. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6923. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6924. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6925. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6926. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6927. const int scale_factor = dst->op_params[0];
  6928. upscale_f32_cuda(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  6929. (void) src1;
  6930. (void) dst;
  6931. (void) src1_dd;
  6932. }
  6933. static void ggml_cuda_op_pad(
  6934. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6935. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6936. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6937. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6938. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  6939. pad_f32_cuda(src0_dd, dst_dd,
  6940. src0->ne[0], src0->ne[1], src0->ne[2],
  6941. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  6942. (void) src1;
  6943. (void) dst;
  6944. (void) src1_dd;
  6945. }
  6946. static void ggml_cuda_op_rms_norm(
  6947. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  6948. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  6949. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6950. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  6951. const int64_t ne00 = src0->ne[0];
  6952. const int64_t nrows = ggml_nrows(src0);
  6953. float eps;
  6954. memcpy(&eps, dst->op_params, sizeof(float));
  6955. rms_norm_f32_cuda(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  6956. (void) src1;
  6957. (void) dst;
  6958. (void) src1_dd;
  6959. }
  6960. static void ggml_cuda_op_mul_mat_q(
  6961. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  6962. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  6963. const int64_t src1_padded_row_size, cudaStream_t stream) {
  6964. const int64_t ne00 = src0->ne[0];
  6965. const int64_t ne10 = src1->ne[0];
  6966. GGML_ASSERT(ne10 % QK8_1 == 0);
  6967. const int64_t ne0 = dst->ne[0];
  6968. const int64_t row_diff = row_high - row_low;
  6969. int id;
  6970. CUDA_CHECK(cudaGetDevice(&id));
  6971. // the main device has a larger memory buffer to hold the results from all GPUs
  6972. // nrows_dst == nrows of the matrix that the kernel writes into
  6973. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  6974. switch (src0->type) {
  6975. case GGML_TYPE_Q4_0:
  6976. 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);
  6977. break;
  6978. case GGML_TYPE_Q4_1:
  6979. 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);
  6980. break;
  6981. case GGML_TYPE_Q5_0:
  6982. 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);
  6983. break;
  6984. case GGML_TYPE_Q5_1:
  6985. 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);
  6986. break;
  6987. case GGML_TYPE_Q8_0:
  6988. 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);
  6989. break;
  6990. case GGML_TYPE_Q2_K:
  6991. 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);
  6992. break;
  6993. case GGML_TYPE_Q3_K:
  6994. 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);
  6995. break;
  6996. case GGML_TYPE_Q4_K:
  6997. 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);
  6998. break;
  6999. case GGML_TYPE_Q5_K:
  7000. 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);
  7001. break;
  7002. case GGML_TYPE_Q6_K:
  7003. 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);
  7004. break;
  7005. default:
  7006. GGML_ASSERT(false);
  7007. break;
  7008. }
  7009. (void) src1;
  7010. (void) dst;
  7011. (void) src1_ddf_i;
  7012. }
  7013. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split) {
  7014. int64_t min_compute_capability = INT_MAX;
  7015. int64_t max_compute_capability = INT_MIN;
  7016. for (int id = 0; id < g_device_count; ++id) {
  7017. if (tensor_split[id] < (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  7018. if (min_compute_capability > g_device_caps[id].cc) {
  7019. min_compute_capability = g_device_caps[id].cc;
  7020. }
  7021. if (max_compute_capability < g_device_caps[id].cc) {
  7022. max_compute_capability = g_device_caps[id].cc;
  7023. }
  7024. }
  7025. }
  7026. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7027. switch(type) {
  7028. case GGML_TYPE_Q4_0:
  7029. case GGML_TYPE_Q4_1:
  7030. case GGML_TYPE_Q5_0:
  7031. case GGML_TYPE_Q5_1:
  7032. case GGML_TYPE_Q8_0:
  7033. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7034. case GGML_TYPE_F16:
  7035. case GGML_TYPE_F32:
  7036. return 1;
  7037. case GGML_TYPE_Q2_K:
  7038. return max_compute_capability >= CC_RDNA2 ? 128 : 32;
  7039. case GGML_TYPE_Q3_K:
  7040. return min_compute_capability < CC_RDNA2 ? 128 : 64;
  7041. case GGML_TYPE_Q4_K:
  7042. case GGML_TYPE_Q5_K:
  7043. case GGML_TYPE_Q6_K:
  7044. case GGML_TYPE_IQ2_XXS:
  7045. case GGML_TYPE_IQ2_XS:
  7046. case GGML_TYPE_IQ3_XXS:
  7047. case GGML_TYPE_IQ1_S:
  7048. return max_compute_capability >= CC_RDNA2 ? 128 : 64;
  7049. default:
  7050. GGML_ASSERT(false);
  7051. }
  7052. #else
  7053. switch(type) {
  7054. case GGML_TYPE_Q4_0:
  7055. case GGML_TYPE_Q4_1:
  7056. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7057. case GGML_TYPE_Q5_0:
  7058. case GGML_TYPE_Q5_1:
  7059. case GGML_TYPE_Q8_0:
  7060. return 64;
  7061. case GGML_TYPE_F16:
  7062. case GGML_TYPE_F32:
  7063. return 1;
  7064. case GGML_TYPE_Q2_K:
  7065. case GGML_TYPE_Q3_K:
  7066. case GGML_TYPE_Q4_K:
  7067. case GGML_TYPE_Q5_K:
  7068. case GGML_TYPE_IQ2_XXS:
  7069. case GGML_TYPE_IQ2_XS:
  7070. case GGML_TYPE_IQ3_XXS:
  7071. case GGML_TYPE_IQ1_S:
  7072. return max_compute_capability >= CC_VOLTA ? 128 : 64;
  7073. case GGML_TYPE_Q6_K:
  7074. return 64;
  7075. default:
  7076. GGML_ASSERT(false);
  7077. }
  7078. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  7079. }
  7080. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_CUDA_MAX_DEVICES> & tensor_split, int id) {
  7081. const int64_t nrows = ggml_nrows(tensor);
  7082. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  7083. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  7084. *row_low -= *row_low % rounding;
  7085. if (id == g_device_count - 1) {
  7086. *row_high = nrows;
  7087. } else {
  7088. *row_high = nrows*tensor_split[id + 1];
  7089. *row_high -= *row_high % rounding;
  7090. }
  7091. }
  7092. static void ggml_cuda_op_mul_mat_vec_q(
  7093. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7094. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7095. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7096. const int64_t ne00 = src0->ne[0];
  7097. const int64_t row_diff = row_high - row_low;
  7098. const int64_t ne10 = src1->ne[0];
  7099. GGML_ASSERT(ne10 % QK8_1 == 0);
  7100. const int64_t ne0 = dst->ne[0];
  7101. int id;
  7102. CUDA_CHECK(cudaGetDevice(&id));
  7103. // the main device has a larger memory buffer to hold the results from all GPUs
  7104. // nrows_dst == nrows of the matrix that the kernel writes into
  7105. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7106. switch (src0->type) {
  7107. case GGML_TYPE_Q4_0:
  7108. mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
  7109. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7110. break;
  7111. case GGML_TYPE_Q4_1:
  7112. mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
  7113. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7114. break;
  7115. case GGML_TYPE_Q5_0:
  7116. mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
  7117. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7118. break;
  7119. case GGML_TYPE_Q5_1:
  7120. mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
  7121. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7122. break;
  7123. case GGML_TYPE_Q8_0:
  7124. mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
  7125. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7126. break;
  7127. case GGML_TYPE_Q2_K:
  7128. mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
  7129. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7130. break;
  7131. case GGML_TYPE_Q3_K:
  7132. mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
  7133. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7134. break;
  7135. case GGML_TYPE_Q4_K:
  7136. mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
  7137. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7138. break;
  7139. case GGML_TYPE_Q5_K:
  7140. mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
  7141. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7142. break;
  7143. case GGML_TYPE_Q6_K:
  7144. mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
  7145. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7146. break;
  7147. case GGML_TYPE_IQ2_XXS:
  7148. mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
  7149. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7150. break;
  7151. case GGML_TYPE_IQ2_XS:
  7152. mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
  7153. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7154. break;
  7155. case GGML_TYPE_IQ3_XXS:
  7156. mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
  7157. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7158. break;
  7159. case GGML_TYPE_IQ1_S:
  7160. mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
  7161. (src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
  7162. break;
  7163. default:
  7164. GGML_ASSERT(false);
  7165. break;
  7166. }
  7167. (void) src1;
  7168. (void) dst;
  7169. (void) src1_ddf_i;
  7170. (void) src1_ncols;
  7171. (void) src1_padded_row_size;
  7172. }
  7173. static void ggml_cuda_op_dequantize_mul_mat_vec(
  7174. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7175. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7176. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7177. const int64_t ne00 = src0->ne[0];
  7178. const int64_t row_diff = row_high - row_low;
  7179. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7180. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  7181. #ifdef GGML_CUDA_F16
  7182. cuda_pool_alloc<half> src1_dfloat_a;
  7183. half * src1_dfloat = nullptr; // dfloat == half
  7184. bool src1_convert_f16 =
  7185. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  7186. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  7187. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  7188. if (src1_convert_f16) {
  7189. src1_dfloat = src1_dfloat_a.alloc(ne00);
  7190. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7191. GGML_ASSERT(to_fp16_cuda != nullptr);
  7192. to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
  7193. }
  7194. #else
  7195. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  7196. #endif // GGML_CUDA_F16
  7197. switch (src0->type) {
  7198. case GGML_TYPE_Q4_0:
  7199. dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7200. break;
  7201. case GGML_TYPE_Q4_1:
  7202. dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7203. break;
  7204. case GGML_TYPE_Q5_0:
  7205. dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7206. break;
  7207. case GGML_TYPE_Q5_1:
  7208. dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7209. break;
  7210. case GGML_TYPE_Q8_0:
  7211. dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7212. break;
  7213. case GGML_TYPE_Q2_K:
  7214. dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7215. break;
  7216. case GGML_TYPE_Q3_K:
  7217. dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7218. break;
  7219. case GGML_TYPE_Q4_K:
  7220. dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7221. break;
  7222. case GGML_TYPE_Q5_K:
  7223. dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7224. break;
  7225. case GGML_TYPE_Q6_K:
  7226. dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  7227. break;
  7228. case GGML_TYPE_F16:
  7229. convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  7230. break;
  7231. default:
  7232. GGML_ASSERT(false);
  7233. break;
  7234. }
  7235. (void) src1;
  7236. (void) dst;
  7237. (void) src1_ddq_i;
  7238. (void) src1_ncols;
  7239. (void) src1_padded_row_size;
  7240. }
  7241. static void ggml_cuda_op_mul_mat_cublas(
  7242. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
  7243. const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
  7244. const int64_t src1_padded_row_size, cudaStream_t stream) {
  7245. GGML_ASSERT(src0_dd_i != nullptr);
  7246. GGML_ASSERT(src1_ddf_i != nullptr);
  7247. GGML_ASSERT(dst_dd_i != nullptr);
  7248. const int64_t ne00 = src0->ne[0];
  7249. const int64_t ne10 = src1->ne[0];
  7250. const int64_t ne0 = dst->ne[0];
  7251. const int64_t row_diff = row_high - row_low;
  7252. int id;
  7253. CUDA_CHECK(cudaGetDevice(&id));
  7254. // the main device has a larger memory buffer to hold the results from all GPUs
  7255. // ldc == nrows of the matrix that cuBLAS writes into
  7256. int ldc = dst->backend == GGML_BACKEND_GPU && id == g_main_device ? ne0 : row_diff;
  7257. const int compute_capability = g_device_caps[id].cc;
  7258. if (compute_capability >= CC_VOLTA && (src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff == src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  7259. //printf("this branch\n");
  7260. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  7261. cuda_pool_alloc<half> src0_as_f16;
  7262. if (src0->type != GGML_TYPE_F16) {
  7263. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src0->type);
  7264. GGML_ASSERT(to_fp16_cuda != nullptr);
  7265. size_t ne = row_diff*ne00;
  7266. src0_as_f16.alloc(ne);
  7267. to_fp16_cuda(src0_dd_i, src0_as_f16.get(), ne, stream);
  7268. }
  7269. const half * src0_ptr = src0->type == GGML_TYPE_F16 ? (const half *) src0_dd_i : src0_as_f16.get();
  7270. cuda_pool_alloc<half> src1_as_f16;
  7271. if (src1->type != GGML_TYPE_F16) {
  7272. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  7273. GGML_ASSERT(to_fp16_cuda != nullptr);
  7274. size_t ne = src1_ncols*ne10;
  7275. src1_as_f16.alloc(ne);
  7276. to_fp16_cuda(src1_ddf_i, src1_as_f16.get(), ne, stream);
  7277. }
  7278. const half * src1_ptr = src1->type == GGML_TYPE_F16 ? (const half *) src1_ddf_i : src1_as_f16.get();
  7279. cuda_pool_alloc<half> dst_f16(row_diff*src1_ncols);
  7280. const half alpha_f16 = 1.0f;
  7281. const half beta_f16 = 0.0f;
  7282. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7283. CUBLAS_CHECK(
  7284. cublasGemmEx(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7285. row_diff, src1_ncols, ne10,
  7286. &alpha_f16, src0_ptr, CUDA_R_16F, ne00,
  7287. src1_ptr, CUDA_R_16F, ne10,
  7288. &beta_f16, dst_f16.get(), CUDA_R_16F, ldc,
  7289. CUBLAS_COMPUTE_16F,
  7290. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  7291. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  7292. to_fp32_cuda(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  7293. } else {
  7294. cuda_pool_alloc<float> src0_ddq_as_f32;
  7295. cuda_pool_alloc<float> src1_ddq_as_f32;
  7296. if (src0->type != GGML_TYPE_F32) {
  7297. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src0->type);
  7298. GGML_ASSERT(to_fp32_cuda != nullptr);
  7299. src0_ddq_as_f32.alloc(row_diff*ne00);
  7300. to_fp32_cuda(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  7301. }
  7302. if (src1->type != GGML_TYPE_F32) {
  7303. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(src1->type);
  7304. GGML_ASSERT(to_fp32_cuda != nullptr);
  7305. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  7306. to_fp32_cuda(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  7307. }
  7308. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  7309. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  7310. const float alpha = 1.0f;
  7311. const float beta = 0.0f;
  7312. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[id], stream));
  7313. CUBLAS_CHECK(
  7314. cublasSgemm(g_cublas_handles[id], CUBLAS_OP_T, CUBLAS_OP_N,
  7315. row_diff, src1_ncols, ne10,
  7316. &alpha, src0_ddf_i, ne00,
  7317. src1_ddf1_i, ne10,
  7318. &beta, dst_dd_i, ldc));
  7319. }
  7320. (void) dst;
  7321. (void) src1_ddq_i;
  7322. (void) src1_padded_row_size;
  7323. }
  7324. static void ggml_cuda_op_rope(
  7325. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7326. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7327. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  7328. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  7329. GGML_ASSERT(src0->type == dst->type);
  7330. const int64_t ne00 = src0->ne[0];
  7331. const int64_t ne01 = src0->ne[1];
  7332. const int64_t ne2 = dst->ne[2];
  7333. const int64_t nrows = ggml_nrows(src0);
  7334. //const int n_past = ((int32_t *) dst->op_params)[0];
  7335. const int n_dims = ((int32_t *) dst->op_params)[1];
  7336. const int mode = ((int32_t *) dst->op_params)[2];
  7337. const int n_ctx = ((int32_t *) dst->op_params)[3];
  7338. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  7339. // RoPE alteration for extended context
  7340. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7341. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7342. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7343. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7344. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7345. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7346. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7347. const int32_t * pos = nullptr;
  7348. if ((mode & 1) == 0) {
  7349. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  7350. GGML_ASSERT(src1->ne[0] == ne2);
  7351. pos = (const int32_t *) src1_dd;
  7352. }
  7353. const bool is_neox = mode & 2;
  7354. const bool is_glm = mode & 4;
  7355. rope_corr_dims corr_dims;
  7356. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  7357. // compute
  7358. if (is_glm) {
  7359. GGML_ASSERT(false);
  7360. rope_glm_f32_cuda(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  7361. } else if (is_neox) {
  7362. if (src0->type == GGML_TYPE_F32) {
  7363. rope_neox_cuda(
  7364. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7365. attn_factor, corr_dims, main_stream
  7366. );
  7367. } else if (src0->type == GGML_TYPE_F16) {
  7368. rope_neox_cuda(
  7369. (const half *)src0_dd, (half *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7370. attn_factor, corr_dims, main_stream
  7371. );
  7372. } else {
  7373. GGML_ASSERT(false);
  7374. }
  7375. } else {
  7376. if (src0->type == GGML_TYPE_F32) {
  7377. rope_cuda(
  7378. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7379. attn_factor, corr_dims, main_stream
  7380. );
  7381. } else if (src0->type == GGML_TYPE_F16) {
  7382. rope_cuda(
  7383. (const half *)src0_dd, (half *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  7384. attn_factor, corr_dims, main_stream
  7385. );
  7386. } else {
  7387. GGML_ASSERT(false);
  7388. }
  7389. }
  7390. (void) src1;
  7391. (void) dst;
  7392. (void) src1_dd;
  7393. }
  7394. static void ggml_cuda_op_alibi(
  7395. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7396. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7397. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7398. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7399. const int64_t ne00 = src0->ne[0];
  7400. const int64_t ne01 = src0->ne[1];
  7401. const int64_t ne02 = src0->ne[2];
  7402. const int64_t nrows = ggml_nrows(src0);
  7403. //const int n_past = ((int32_t *) dst->op_params)[0];
  7404. const int n_head = ((int32_t *) dst->op_params)[1];
  7405. float max_bias;
  7406. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  7407. //GGML_ASSERT(ne01 + n_past == ne00);
  7408. GGML_ASSERT(n_head == ne02);
  7409. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  7410. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  7411. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  7412. alibi_f32_cuda(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  7413. (void) src1;
  7414. (void) src1_dd;
  7415. }
  7416. static void ggml_cuda_op_pool2d(
  7417. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7418. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7419. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7420. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7421. const int32_t * opts = (const int32_t *)dst->op_params;
  7422. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  7423. const int k0 = opts[1];
  7424. const int k1 = opts[2];
  7425. const int s0 = opts[3];
  7426. const int s1 = opts[4];
  7427. const int p0 = opts[5];
  7428. const int p1 = opts[6];
  7429. const int64_t IH = src0->ne[1];
  7430. const int64_t IW = src0->ne[0];
  7431. const int64_t N = dst->ne[3];
  7432. const int64_t OC = dst->ne[2];
  7433. const int64_t OH = dst->ne[1];
  7434. const int64_t OW = dst->ne[0];
  7435. const int parallel_elements = N * OC * OH * OW;
  7436. const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
  7437. dim3 block_nums(num_blocks);
  7438. pool2d_nchw_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, main_stream>>>(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0, parallel_elements, src0_dd, dst_dd, op);
  7439. (void) src1;
  7440. (void) src1_dd;
  7441. }
  7442. static void ggml_cuda_op_im2col(
  7443. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7444. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7445. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7446. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7447. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  7448. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7449. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  7450. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  7451. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  7452. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  7453. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  7454. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  7455. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  7456. const int64_t IH = is_2D ? src1->ne[1] : 1;
  7457. const int64_t IW = src1->ne[0];
  7458. const int64_t KH = is_2D ? src0->ne[1] : 1;
  7459. const int64_t KW = src0->ne[0];
  7460. const int64_t OH = is_2D ? dst->ne[2] : 1;
  7461. const int64_t OW = dst->ne[1];
  7462. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  7463. const int64_t batch = src1->ne[3];
  7464. const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
  7465. if(dst->type == GGML_TYPE_F16) {
  7466. im2col_cuda(src1_dd, (half*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7467. } else {
  7468. im2col_cuda(src1_dd, (float*) dst_dd, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  7469. }
  7470. (void) src0;
  7471. (void) src0_dd;
  7472. }
  7473. static void ggml_cuda_op_sum_rows(
  7474. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7475. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7476. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7477. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7478. const int64_t ncols = src0->ne[0];
  7479. const int64_t nrows = ggml_nrows(src0);
  7480. sum_rows_f32_cuda(src0_dd, dst_dd, ncols, nrows, main_stream);
  7481. (void) src1;
  7482. (void) dst;
  7483. (void) src1_dd;
  7484. }
  7485. static void ggml_cuda_op_argsort(
  7486. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7487. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7488. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7489. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  7490. const int64_t ncols = src0->ne[0];
  7491. const int64_t nrows = ggml_nrows(src0);
  7492. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  7493. argsort_f32_i32_cuda(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  7494. (void) src1;
  7495. (void) dst;
  7496. (void) src1_dd;
  7497. }
  7498. static void ggml_cuda_op_diag_mask_inf(
  7499. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7500. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7501. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7502. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7503. const int64_t ne00 = src0->ne[0];
  7504. const int64_t ne01 = src0->ne[1];
  7505. const int nrows0 = ggml_nrows(src0);
  7506. const int n_past = ((int32_t *) dst->op_params)[0];
  7507. diag_mask_inf_f32_cuda(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  7508. (void) src1;
  7509. (void) dst;
  7510. (void) src1_dd;
  7511. }
  7512. static void ggml_cuda_op_soft_max(
  7513. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7514. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7515. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7516. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7517. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  7518. const int64_t ne00 = src0->ne[0];
  7519. const int64_t nrows_x = ggml_nrows(src0);
  7520. const int64_t nrows_y = src0->ne[1];
  7521. float scale = 1.0f;
  7522. float max_bias = 0.0f;
  7523. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7524. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7525. // positions tensor
  7526. float * src2_dd = dst_dd; // default to avoid null checks in the kernel
  7527. cuda_pool_alloc<float> src2_f;
  7528. ggml_tensor * src2 = dst->src[2];
  7529. const bool use_src2 = src2 != nullptr;
  7530. if (use_src2) {
  7531. const bool src2_on_device = use_src2 && src2->backend == GGML_BACKEND_GPU;
  7532. ggml_tensor_extra_gpu * src2_extra = use_src2 ? (ggml_tensor_extra_gpu *) src2->extra : nullptr;
  7533. if (src2_on_device) {
  7534. src2_dd = (float *) src2_extra->data_device[g_main_device];
  7535. } else {
  7536. src2_dd = src2_f.alloc(ggml_nelements(src2));
  7537. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  7538. }
  7539. }
  7540. soft_max_f32_cuda(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00, nrows_x, nrows_y, scale, max_bias, main_stream);
  7541. }
  7542. static void ggml_cuda_op_scale(
  7543. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7544. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7545. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7546. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7547. float scale;
  7548. memcpy(&scale, dst->op_params, sizeof(float));
  7549. scale_f32_cuda(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  7550. CUDA_CHECK(cudaGetLastError());
  7551. (void) src1;
  7552. (void) dst;
  7553. (void) src1_dd;
  7554. }
  7555. static void ggml_cuda_op_clamp(
  7556. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
  7557. const float * src0_dd, const float * src1_dd, float * dst_dd, cudaStream_t main_stream) {
  7558. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  7559. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7560. float min;
  7561. float max;
  7562. memcpy(&min, dst->op_params, sizeof(float));
  7563. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7564. clamp_f32_cuda(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  7565. CUDA_CHECK(cudaGetLastError());
  7566. (void) src1;
  7567. (void) dst;
  7568. (void) src1_dd;
  7569. }
  7570. static void ggml_cuda_op_flatten(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const ggml_cuda_op_flatten_t op) {
  7571. const int64_t nrows0 = ggml_nrows(src0);
  7572. const bool use_src1 = src1 != nullptr;
  7573. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  7574. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  7575. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  7576. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7577. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  7578. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7579. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7580. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  7581. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  7582. // dd = data device
  7583. float * src0_ddf = nullptr;
  7584. float * src1_ddf = nullptr;
  7585. float * dst_ddf = nullptr;
  7586. cuda_pool_alloc<float> src0_f;
  7587. cuda_pool_alloc<float> src1_f;
  7588. cuda_pool_alloc<float> dst_f;
  7589. ggml_cuda_set_device(g_main_device);
  7590. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  7591. if (src0_on_device) {
  7592. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  7593. } else {
  7594. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  7595. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  7596. }
  7597. if (use_src1) {
  7598. if (src1_on_device) {
  7599. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  7600. } else {
  7601. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  7602. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  7603. }
  7604. }
  7605. if (dst_on_device) {
  7606. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  7607. } else {
  7608. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  7609. }
  7610. // do the computation
  7611. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  7612. CUDA_CHECK(cudaGetLastError());
  7613. // copy dst to host if necessary
  7614. if (!dst_on_device) {
  7615. CUDA_CHECK(cudaMemcpyAsync(dst->data, dst_ddf, ggml_nbytes(dst), cudaMemcpyDeviceToHost, main_stream));
  7616. }
  7617. if (dst->backend == GGML_BACKEND_CPU) {
  7618. CUDA_CHECK(cudaDeviceSynchronize());
  7619. }
  7620. }
  7621. static void ggml_cuda_set_peer_access(const int n_tokens) {
  7622. static bool peer_access_enabled = false;
  7623. const bool enable_peer_access = n_tokens <= GGML_CUDA_PEER_MAX_BATCH_SIZE;
  7624. if (peer_access_enabled == enable_peer_access) {
  7625. return;
  7626. }
  7627. #ifdef NDEBUG
  7628. for (int id = 0; id < g_device_count; ++id) {
  7629. ggml_cuda_set_device(id);
  7630. CUDA_CHECK(cudaDeviceSynchronize());
  7631. }
  7632. for (int id = 0; id < g_device_count; ++id) {
  7633. ggml_cuda_set_device(id);
  7634. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  7635. if (id == id_other) {
  7636. continue;
  7637. }
  7638. if (id != g_main_device && id_other != g_main_device) {
  7639. continue;
  7640. }
  7641. int can_access_peer;
  7642. CUDA_CHECK(cudaDeviceCanAccessPeer(&can_access_peer, id, id_other));
  7643. if (can_access_peer) {
  7644. if (enable_peer_access) {
  7645. CUDA_CHECK(cudaDeviceEnablePeerAccess(id_other, 0));
  7646. } else {
  7647. CUDA_CHECK(cudaDeviceDisablePeerAccess(id_other));
  7648. }
  7649. }
  7650. }
  7651. }
  7652. #endif // NDEBUG
  7653. peer_access_enabled = enable_peer_access;
  7654. }
  7655. // FIXME: move this somewhere else
  7656. struct ggml_backend_cuda_split_buffer_type_context {
  7657. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7658. };
  7659. static void ggml_cuda_op_mul_mat(
  7660. const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, ggml_cuda_op_mul_mat_t op,
  7661. const bool convert_src1_to_q8_1) {
  7662. const int64_t ne00 = src0->ne[0];
  7663. const int64_t ne01 = src0->ne[1];
  7664. const int64_t ne02 = src0->ne[2];
  7665. const int64_t ne03 = src0->ne[3];
  7666. const int64_t ne10 = src1->ne[0];
  7667. const int64_t ne11 = src1->ne[1];
  7668. const int64_t ne12 = src1->ne[2];
  7669. const int64_t ne13 = src1->ne[3];
  7670. const int64_t nrows1 = ggml_nrows(src1);
  7671. GGML_ASSERT(ne03 == ne13);
  7672. const int64_t ne0 = dst->ne[0];
  7673. const int64_t ne1 = dst->ne[1];
  7674. const int nb2 = dst->nb[2];
  7675. const int nb3 = dst->nb[3];
  7676. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  7677. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  7678. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  7679. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  7680. const int64_t i02_divisor = ne12 / ne02;
  7681. const size_t src0_ts = ggml_type_size(src0->type);
  7682. const size_t src0_bs = ggml_blck_size(src0->type);
  7683. const size_t q8_1_ts = sizeof(block_q8_1);
  7684. const size_t q8_1_bs = QK8_1;
  7685. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  7686. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  7687. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  7688. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  7689. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  7690. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  7691. const int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  7692. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  7693. GGML_ASSERT(!(split && ne02 > 1));
  7694. GGML_ASSERT(!(split && ne03 > 1));
  7695. GGML_ASSERT(!(split && ne02 < ne12));
  7696. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split;
  7697. if (split) {
  7698. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_GPU_SPLIT check
  7699. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  7700. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  7701. tensor_split = buft_ctx->tensor_split;
  7702. }
  7703. struct dev_data {
  7704. cuda_pool_alloc<char> src0_dd_alloc;
  7705. cuda_pool_alloc<float> src1_ddf_alloc;
  7706. cuda_pool_alloc<char> src1_ddq_alloc;
  7707. cuda_pool_alloc<float> dst_dd_alloc;
  7708. char * src0_dd = nullptr;
  7709. float * src1_ddf = nullptr; // float
  7710. char * src1_ddq = nullptr; // q8_1
  7711. float * dst_dd = nullptr;
  7712. int64_t row_low;
  7713. int64_t row_high;
  7714. };
  7715. dev_data dev[GGML_CUDA_MAX_DEVICES];
  7716. int used_devices = 0;
  7717. for (int id = 0; id < g_device_count; ++id) {
  7718. // by default, use all rows
  7719. dev[id].row_low = 0;
  7720. dev[id].row_high = ne01;
  7721. // for multi GPU, get the row boundaries from tensor split
  7722. // and round to mul_mat_q tile sizes
  7723. if (split) {
  7724. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  7725. if (id != 0) {
  7726. dev[id].row_low = ne01*tensor_split[id];
  7727. if (dev[id].row_low < ne01) {
  7728. dev[id].row_low -= dev[id].row_low % rounding;
  7729. }
  7730. }
  7731. if (id != g_device_count - 1) {
  7732. dev[id].row_high = ne01*tensor_split[id + 1];
  7733. if (dev[id].row_high < ne01) {
  7734. dev[id].row_high -= dev[id].row_high % rounding;
  7735. }
  7736. }
  7737. }
  7738. }
  7739. for (int id = 0; id < g_device_count; ++id) {
  7740. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7741. continue;
  7742. }
  7743. used_devices++;
  7744. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7745. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7746. ggml_cuda_set_device(id);
  7747. cudaStream_t stream = g_cudaStreams[id][0];
  7748. if (src0_on_device && src0_is_contiguous) {
  7749. dev[id].src0_dd = (char *) src0_extra->data_device[id];
  7750. } else {
  7751. dev[id].src0_dd = dev[id].src0_dd_alloc.alloc(ggml_nbytes(src0));
  7752. }
  7753. if (src1_on_device && src1_is_contiguous) {
  7754. dev[id].src1_ddf = (float *) src1_extra->data_device[id];
  7755. } else {
  7756. dev[id].src1_ddf = dev[id].src1_ddf_alloc.alloc(ggml_nelements(src1));
  7757. }
  7758. if (convert_src1_to_q8_1) {
  7759. dev[id].src1_ddq = dev[id].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  7760. if (src1_on_device && src1_is_contiguous) {
  7761. quantize_row_q8_1_cuda(dev[id].src1_ddf, dev[id].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  7762. CUDA_CHECK(cudaGetLastError());
  7763. }
  7764. }
  7765. if (dst_on_device) {
  7766. dev[id].dst_dd = (float *) dst_extra->data_device[id];
  7767. } else {
  7768. const size_t size_dst_ddf = split ? (dev[id].row_high - dev[id].row_low)*ne1 : ggml_nelements(dst);
  7769. dev[id].dst_dd = dev[id].dst_dd_alloc.alloc(size_dst_ddf);
  7770. }
  7771. }
  7772. // if multiple devices are used they need to wait for the main device
  7773. // here an event is recorded that signals that the main device has finished calculating the input data
  7774. if (split && used_devices > 1) {
  7775. ggml_cuda_set_device(g_main_device);
  7776. CUDA_CHECK(cudaEventRecord(src0_extra->events[g_main_device][0], g_cudaStreams[g_main_device][0]));
  7777. }
  7778. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  7779. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  7780. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  7781. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  7782. for (int id = 0; id < g_device_count; ++id) {
  7783. if ((!split && id != g_main_device) || dev[id].row_low == dev[id].row_high) {
  7784. continue;
  7785. }
  7786. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device;
  7787. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device;
  7788. const int64_t row_diff = dev[id].row_high - dev[id].row_low;
  7789. ggml_cuda_set_device(id);
  7790. cudaStream_t stream = g_cudaStreams[id][is];
  7791. // wait for main GPU data if necessary
  7792. if (split && (id != g_main_device || is != 0)) {
  7793. CUDA_CHECK(cudaStreamWaitEvent(stream, src0_extra->events[g_main_device][0], 0));
  7794. }
  7795. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  7796. const int64_t i03 = i0 / ne12;
  7797. const int64_t i02 = i0 % ne12;
  7798. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  7799. // for split tensors the data begins at i0 == i0_offset_low
  7800. char * src0_dd_i = dev[id].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  7801. float * src1_ddf_i = dev[id].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  7802. char * src1_ddq_i = dev[id].src1_ddq + src1_ddq_i_offset;
  7803. float * dst_dd_i = dev[id].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  7804. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  7805. // in that case an offset on dst_ddf_i is needed
  7806. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device) {
  7807. dst_dd_i += dev[id].row_low; // offset is 0 if no tensor split
  7808. }
  7809. // copy src0, src1 to device if necessary
  7810. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  7811. if (id != g_main_device) {
  7812. if (convert_src1_to_q8_1) {
  7813. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  7814. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddq_i, id, src1_ddq_i_source, g_main_device,
  7815. src1_ncols*src1_padded_col_size*q8_1_ts/q8_1_bs, stream));
  7816. } else {
  7817. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  7818. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  7819. CUDA_CHECK(cudaMemcpyPeerAsync(src1_ddf_i, id, src1_ddf_i_source, g_main_device,
  7820. src1_ncols*ne10*sizeof(float), stream));
  7821. }
  7822. }
  7823. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  7824. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(
  7825. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  7826. } else {
  7827. GGML_ASSERT(false);
  7828. }
  7829. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  7830. quantize_row_q8_1_cuda(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  7831. CUDA_CHECK(cudaGetLastError());
  7832. }
  7833. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  7834. CUDA_CHECK(ggml_cuda_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[id].row_low, dev[id].row_high, stream));
  7835. }
  7836. // do the computation
  7837. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  7838. dev[id].row_low, dev[id].row_high, src1_ncols, src1_padded_col_size, stream);
  7839. CUDA_CHECK(cudaGetLastError());
  7840. // copy dst to host or other device if necessary
  7841. if (!dst_on_device) {
  7842. void * dst_off_device;
  7843. cudaMemcpyKind kind;
  7844. if (dst->backend == GGML_BACKEND_CPU) {
  7845. dst_off_device = dst->data;
  7846. kind = cudaMemcpyDeviceToHost;
  7847. } else if (dst->backend == GGML_BACKEND_GPU) {
  7848. dst_off_device = dst_extra->data_device[g_main_device];
  7849. kind = cudaMemcpyDeviceToDevice;
  7850. } else {
  7851. GGML_ASSERT(false);
  7852. }
  7853. if (split) {
  7854. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  7855. // dst is NOT transposed.
  7856. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  7857. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  7858. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  7859. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7860. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7861. dhf_dst_i += src1_col_0*ne0 + dev[id].row_low;
  7862. #if !defined(GGML_USE_HIPBLAS)
  7863. if (kind == cudaMemcpyDeviceToDevice) {
  7864. // cudaMemcpy2DAsync may fail with copies between vmm pools of different devices
  7865. cudaMemcpy3DPeerParms p = {};
  7866. p.dstDevice = g_main_device;
  7867. p.dstPtr = make_cudaPitchedPtr(dhf_dst_i, ne0*sizeof(float), row_diff, src1_ncols);
  7868. p.srcDevice = id;
  7869. p.srcPtr = make_cudaPitchedPtr(dst_dd_i, row_diff*sizeof(float), row_diff, src1_ncols);
  7870. p.extent = make_cudaExtent(row_diff*sizeof(float), src1_ncols, 1);
  7871. CUDA_CHECK(cudaMemcpy3DPeerAsync(&p, stream));
  7872. } else
  7873. #endif
  7874. {
  7875. CUDA_CHECK(cudaMemcpy2DAsync(dhf_dst_i, ne0*sizeof(float),
  7876. dst_dd_i, row_diff*sizeof(float),
  7877. row_diff*sizeof(float), src1_ncols,
  7878. kind, stream));
  7879. }
  7880. } else {
  7881. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  7882. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  7883. dhf_dst_i += src1_col_0*ne0;
  7884. CUDA_CHECK(cudaMemcpyAsync(dhf_dst_i, dst_dd_i, src1_ncols*ne0*sizeof(float), kind, stream));
  7885. }
  7886. }
  7887. // add event for the main device to wait on until other device is done
  7888. if (split && (id != g_main_device || is != 0)) {
  7889. CUDA_CHECK(cudaEventRecord(src0_extra->events[id][is], stream));
  7890. }
  7891. }
  7892. }
  7893. }
  7894. // main device waits for all other devices to be finished
  7895. if (split && g_device_count > 1) {
  7896. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  7897. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  7898. ggml_cuda_set_device(g_main_device);
  7899. for (int id = 0; id < g_device_count; ++id) {
  7900. if (dev[id].row_low == dev[id].row_high) {
  7901. continue;
  7902. }
  7903. for (int64_t is = 0; is < is_max; ++is) {
  7904. CUDA_CHECK(cudaStreamWaitEvent(g_cudaStreams[g_main_device][0], src0_extra->events[id][is], 0));
  7905. }
  7906. }
  7907. }
  7908. if (dst->backend == GGML_BACKEND_CPU) {
  7909. ggml_cuda_set_device(g_main_device);
  7910. CUDA_CHECK(cudaDeviceSynchronize());
  7911. }
  7912. }
  7913. static void ggml_cuda_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7914. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_repeat);
  7915. }
  7916. static void ggml_cuda_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7917. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_get_rows);
  7918. }
  7919. static void ggml_cuda_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7920. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_add);
  7921. }
  7922. static void ggml_cuda_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7923. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_acc);
  7924. }
  7925. static void ggml_cuda_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7926. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_mul);
  7927. }
  7928. static void ggml_cuda_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7929. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_div);
  7930. }
  7931. static void ggml_cuda_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7932. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu);
  7933. }
  7934. static void ggml_cuda_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7935. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_silu);
  7936. }
  7937. static void ggml_cuda_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7938. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_gelu_quick);
  7939. }
  7940. static void ggml_cuda_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7941. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_tanh);
  7942. }
  7943. static void ggml_cuda_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7944. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_relu);
  7945. }
  7946. static void ggml_cuda_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7947. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardsigmoid);
  7948. }
  7949. static void ggml_cuda_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7950. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_hardswish);
  7951. }
  7952. static void ggml_cuda_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7953. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_leaky_relu);
  7954. }
  7955. static void ggml_cuda_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7956. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sqr);
  7957. }
  7958. static void ggml_cuda_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7959. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_norm);
  7960. }
  7961. static void ggml_cuda_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7962. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_group_norm);
  7963. }
  7964. static void ggml_cuda_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7965. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_concat);
  7966. }
  7967. static void ggml_cuda_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7968. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_upscale);
  7969. }
  7970. static void ggml_cuda_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7971. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pad);
  7972. }
  7973. static void ggml_cuda_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  7974. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rms_norm);
  7975. }
  7976. GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  7977. if (!g_cublas_loaded) return false;
  7978. const int64_t ne10 = src1->ne[0];
  7979. const int64_t ne0 = dst->ne[0];
  7980. const int64_t ne1 = dst->ne[1];
  7981. // TODO: find the optimal values for these
  7982. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  7983. src1->type == GGML_TYPE_F32 &&
  7984. dst->type == GGML_TYPE_F32 &&
  7985. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  7986. }
  7987. static void ggml_cuda_mul_mat_vec_p021(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  7988. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  7989. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  7990. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  7991. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  7992. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7993. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7994. const int64_t ne00 = src0->ne[0];
  7995. const int64_t ne01 = src0->ne[1];
  7996. const int64_t ne02 = src0->ne[2];
  7997. const int64_t ne12 = src1->ne[2];
  7998. ggml_cuda_set_device(g_main_device);
  7999. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8000. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8001. void * src0_ddq = src0_extra->data_device[g_main_device];
  8002. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8003. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8004. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8005. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8006. ggml_mul_mat_p021_f16_f32_cuda(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  8007. }
  8008. static void ggml_cuda_mul_mat_vec_nc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst){
  8009. GGML_ASSERT(!ggml_is_transposed(src0));
  8010. GGML_ASSERT(!ggml_is_transposed(src1));
  8011. GGML_ASSERT(!ggml_is_permuted(src0));
  8012. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8013. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8014. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8015. const int64_t ne00 = src0->ne[0];
  8016. const int64_t ne01 = src0->ne[1];
  8017. const int64_t ne02 = src0->ne[2];
  8018. const int64_t nb01 = src0->nb[1];
  8019. const int64_t nb02 = src0->nb[2];
  8020. const int64_t ne12 = src1->ne[2];
  8021. ggml_cuda_set_device(g_main_device);
  8022. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8023. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8024. void * src0_ddq = src0_extra->data_device[g_main_device];
  8025. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8026. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8027. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8028. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8029. const int64_t row_stride_x = nb01 / sizeof(half);
  8030. const int64_t channel_stride_x = nb02 / sizeof(half);
  8031. 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);
  8032. }
  8033. static __global__ void k_compute_batched_ptrs(
  8034. const half * src0_as_f16, const half * src1_as_f16, char * dst,
  8035. const void ** ptrs_src, void ** ptrs_dst,
  8036. int64_t ne12, int64_t ne13,
  8037. int64_t ne23,
  8038. size_t nb02, size_t nb03,
  8039. size_t nb12, size_t nb13,
  8040. size_t nbd2, size_t nbd3,
  8041. int64_t r2, int64_t r3) {
  8042. int64_t i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8043. int64_t i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8044. if (i13 >= ne13 || i12 >= ne12) {
  8045. return;
  8046. }
  8047. int64_t i03 = i13 / r3;
  8048. int64_t i02 = i12 / r2;
  8049. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  8050. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  8051. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  8052. }
  8053. static void ggml_cuda_mul_mat_batched_cublas(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8054. GGML_ASSERT(!ggml_is_transposed(src0));
  8055. GGML_ASSERT(!ggml_is_transposed(src1));
  8056. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  8057. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8058. GGML_TENSOR_BINARY_OP_LOCALS
  8059. const int64_t ne_dst = ggml_nelements(dst);
  8060. ggml_cuda_set_device(g_main_device);
  8061. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8062. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8063. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8064. void * src0_ddq = src0_extra->data_device[g_main_device];
  8065. half * src0_f16 = (half *) src0_ddq;
  8066. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8067. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8068. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8069. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8070. // convert src1 to fp16
  8071. cuda_pool_alloc<half> src1_f16_alloc;
  8072. if (src1->type != GGML_TYPE_F16) {
  8073. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8074. const int64_t ne_src1 = ggml_nelements(src1);
  8075. src1_f16_alloc.alloc(ne_src1);
  8076. GGML_ASSERT(to_fp16_cuda != nullptr);
  8077. to_fp16_cuda(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  8078. }
  8079. half * src1_f16 = src1->type == GGML_TYPE_F16 ? (half *) src1_ddf : src1_f16_alloc.get();
  8080. cuda_pool_alloc<half> dst_f16;
  8081. char * dst_t;
  8082. cublasComputeType_t cu_compute_type = CUBLAS_COMPUTE_16F;
  8083. cudaDataType_t cu_data_type = CUDA_R_16F;
  8084. // dst strides
  8085. size_t nbd2 = dst->nb[2];
  8086. size_t nbd3 = dst->nb[3];
  8087. const half alpha_f16 = 1.0f;
  8088. const half beta_f16 = 0.0f;
  8089. const float alpha_f32 = 1.0f;
  8090. const float beta_f32 = 0.0f;
  8091. const void * alpha = &alpha_f16;
  8092. const void * beta = &beta_f16;
  8093. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8094. dst_t = (char *) dst_f16.alloc(ne_dst);
  8095. nbd2 /= sizeof(float) / sizeof(half);
  8096. nbd3 /= sizeof(float) / sizeof(half);
  8097. } else {
  8098. dst_t = (char *) dst_ddf;
  8099. cu_compute_type = CUBLAS_COMPUTE_32F;
  8100. cu_data_type = CUDA_R_32F;
  8101. alpha = &alpha_f32;
  8102. beta = &beta_f32;
  8103. }
  8104. GGML_ASSERT(ne12 % ne02 == 0);
  8105. GGML_ASSERT(ne13 % ne03 == 0);
  8106. // broadcast factors
  8107. const int64_t r2 = ne12/ne02;
  8108. const int64_t r3 = ne13/ne03;
  8109. #if 0
  8110. // use cublasGemmEx
  8111. {
  8112. for (int i13 = 0; i13 < ne13; ++i13) {
  8113. for (int i12 = 0; i12 < ne12; ++i12) {
  8114. int i03 = i13 / r3;
  8115. int i02 = i12 / r2;
  8116. CUBLAS_CHECK(
  8117. cublasGemmEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8118. ne01, ne11, ne10,
  8119. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , CUDA_R_16F, nb01/sizeof(half),
  8120. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, CUDA_R_16F, nb11/sizeof(float),
  8121. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  8122. cu_compute_type,
  8123. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8124. }
  8125. }
  8126. }
  8127. #else
  8128. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  8129. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  8130. // use cublasGemmStridedBatchedEx
  8131. CUBLAS_CHECK(
  8132. cublasGemmStridedBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8133. ne01, ne11, ne10,
  8134. alpha, (const char *) src0_f16, CUDA_R_16F, nb01/nb00, nb02/nb00, // strideA
  8135. (const char *) src1_f16, CUDA_R_16F, nb11/nb10, nb12/nb10, // strideB
  8136. beta, ( char *) dst_t, cu_data_type, ne01, nb2/nb0, // strideC
  8137. ne12*ne13,
  8138. cu_compute_type,
  8139. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8140. } else {
  8141. // use cublasGemmBatchedEx
  8142. const int ne23 = ne12*ne13;
  8143. cuda_pool_alloc<const void *> ptrs_src(2*ne23);
  8144. cuda_pool_alloc< void *> ptrs_dst(1*ne23);
  8145. dim3 block_dims(ne13, ne12);
  8146. k_compute_batched_ptrs<<<1, block_dims, 0, main_stream>>>(
  8147. src0_f16, src1_f16, dst_t,
  8148. ptrs_src.get(), ptrs_dst.get(),
  8149. ne12, ne13,
  8150. ne23,
  8151. nb02, nb03,
  8152. src1->type == GGML_TYPE_F16 ? nb12 : nb12/2,
  8153. src1->type == GGML_TYPE_F16 ? nb13 : nb13/2,
  8154. nbd2, nbd3,
  8155. r2, r3);
  8156. CUDA_CHECK(cudaGetLastError());
  8157. CUBLAS_CHECK(
  8158. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8159. ne01, ne11, ne10,
  8160. alpha, (const void **) (ptrs_src.get() + 0*ne23), CUDA_R_16F, nb01/nb00,
  8161. (const void **) (ptrs_src.get() + 1*ne23), CUDA_R_16F, nb11/nb10,
  8162. beta, ( void **) (ptrs_dst.get() + 0*ne23), cu_data_type, ne01,
  8163. ne23,
  8164. cu_compute_type,
  8165. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8166. }
  8167. #endif
  8168. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  8169. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8170. to_fp32_cuda(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  8171. }
  8172. }
  8173. static void ggml_cuda_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8174. const bool all_on_device =
  8175. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  8176. (src1->backend == GGML_BACKEND_GPU) &&
  8177. ( dst->backend == GGML_BACKEND_GPU);
  8178. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  8179. int64_t min_compute_capability = INT_MAX;
  8180. bool any_pascal_with_slow_fp16 = false;
  8181. if (split) {
  8182. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *) src0->buffer->buft->context;
  8183. auto & tensor_split = buft_ctx->tensor_split;
  8184. for (int id = 0; id < g_device_count; ++id) {
  8185. // skip devices that are not going to do any work:
  8186. if (tensor_split[id] >= (id + 1 < g_device_count ? tensor_split[id + 1] : 1.0f)) {
  8187. continue;
  8188. }
  8189. if (min_compute_capability > g_device_caps[id].cc) {
  8190. min_compute_capability = g_device_caps[id].cc;
  8191. }
  8192. if (g_device_caps[id].cc == 610) {
  8193. any_pascal_with_slow_fp16 = true;
  8194. }
  8195. }
  8196. } else {
  8197. min_compute_capability = g_device_caps[g_main_device].cc;
  8198. any_pascal_with_slow_fp16 = g_device_caps[g_main_device].cc == 610;
  8199. }
  8200. // check data types and tensor shapes for custom matrix multiplication kernels:
  8201. bool use_dequantize_mul_mat_vec = (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16)
  8202. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8203. && src0->ne[0] % GGML_CUDA_DMMV_X == 0 && src1->ne[1] == 1;
  8204. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  8205. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  8206. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  8207. bool use_mul_mat_q = ggml_cuda_supports_mmq(src0->type)
  8208. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  8209. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8210. const bool fp16_performance_good = min_compute_capability >= CC_RDNA1;
  8211. #ifdef CUDA_USE_TENSOR_CORES
  8212. use_mul_mat_q = use_mul_mat_q && min_compute_capability < CC_RDNA3;
  8213. #endif // CUDA_USE_TENSOR_CORES
  8214. #else
  8215. // fp16 performance is good on Volta or newer and on P100 (compute capability 6.0)
  8216. const bool fp16_performance_good = min_compute_capability >= CC_PASCAL && !any_pascal_with_slow_fp16;
  8217. // mmvq and mmq need the __dp4a instruction which on NVIDIA is only available for CC >= 6.1
  8218. use_mul_mat_vec_q = use_mul_mat_vec_q && min_compute_capability >= MIN_CC_DP4A;
  8219. use_mul_mat_q = use_mul_mat_q && min_compute_capability >= MIN_CC_DP4A;
  8220. #ifdef CUDA_USE_TENSOR_CORES
  8221. // when tensor cores are available, use them for large batch size
  8222. // ref: https://github.com/ggerganov/llama.cpp/pull/3776
  8223. use_mul_mat_q = use_mul_mat_q && (!fp16_performance_good || src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  8224. #endif // CUDA_USE_TENSOR_CORES
  8225. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  8226. // if mmvq is available it's a better choice than dmmv:
  8227. #ifndef GGML_CUDA_FORCE_DMMV
  8228. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  8229. #endif // GGML_CUDA_FORCE_DMMV
  8230. // debug helpers
  8231. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  8232. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  8233. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  8234. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  8235. //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);
  8236. //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);
  8237. if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  8238. // KQ single-batch
  8239. ggml_cuda_mul_mat_vec_p021(src0, src1, dst);
  8240. } else if (!split && all_on_device && !fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  8241. // KQV single-batch
  8242. ggml_cuda_mul_mat_vec_nc(src0, src1, dst);
  8243. } else if (!split && all_on_device && fp16_performance_good && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  8244. // KQ + KQV multi-batch
  8245. ggml_cuda_mul_mat_batched_cublas(src0, src1, dst);
  8246. } else if (use_dequantize_mul_mat_vec) {
  8247. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_dequantize_mul_mat_vec, false);
  8248. } else if (use_mul_mat_vec_q) {
  8249. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_vec_q, true);
  8250. } else if (use_mul_mat_q) {
  8251. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_q, true);
  8252. } else {
  8253. ggml_cuda_op_mul_mat(src0, src1, dst, ggml_cuda_op_mul_mat_cublas, false);
  8254. }
  8255. }
  8256. #if 0
  8257. template<typename ... Srcs>
  8258. static __global__ void k_compute_batched_ptrs_id(
  8259. const void ** ptrs_src, void ** ptrs_dst,
  8260. int ne12, int ne13,
  8261. int ne23,
  8262. int nb02, int nb03,
  8263. int nb12, int nb13,
  8264. int nb2, int nb3,
  8265. int r2, int r3,
  8266. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  8267. const half * src1_f16, half * dst_f16,
  8268. const int32_t * ids, const int id,
  8269. Srcs... src0s) {
  8270. int i = ids[id];
  8271. half * src0_f16;
  8272. const void * srcs_ar[] = { (const half *) src0s... };
  8273. if (src0_type == GGML_TYPE_F16) {
  8274. src0_f16 = (half *) srcs_ar[i];
  8275. } else {
  8276. src0_f16 = src0_as_f16;
  8277. if (threadIdx.x == 0 && threadIdx.y == 0) {
  8278. const to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(src0_type);
  8279. to_fp16(srcs_ar[i], src0_f16, src0_ne, cudaStreamFireAndForget);
  8280. }
  8281. }
  8282. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  8283. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  8284. if (i13 >= ne13 || i12 >= ne12) {
  8285. return;
  8286. }
  8287. int i03 = i13 / r3;
  8288. int i02 = i12 / r2;
  8289. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  8290. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  8291. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  8292. }
  8293. static void ggml_cuda_mul_mat_id_cublas(ggml_tensor * dst) {
  8294. const struct ggml_tensor * ids = dst->src[0];
  8295. const struct ggml_tensor * src1 = dst->src[1];
  8296. const struct ggml_tensor * src00 = dst->src[2];
  8297. const int id = dst->op_params[0];
  8298. GGML_ASSERT(!ggml_is_transposed(src00));
  8299. GGML_ASSERT(!ggml_is_transposed(src1));
  8300. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  8301. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8302. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  8303. const int64_t ne01 = src00->ne[1];
  8304. const int64_t ne02 = src00->ne[2];
  8305. const int64_t ne03 = src00->ne[3];
  8306. //const int64_t nb01 = src00->nb[1];
  8307. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  8308. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  8309. const int64_t ne10 = src1->ne[0];
  8310. const int64_t ne11 = src1->ne[1];
  8311. const int64_t ne12 = src1->ne[2];
  8312. const int64_t ne13 = src1->ne[3];
  8313. //const int64_t nb11 = src1->nb[1];
  8314. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  8315. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  8316. const int64_t ne1 = ggml_nelements(src1);
  8317. const int64_t ne = ggml_nelements(dst);
  8318. ggml_cuda_set_device(g_main_device);
  8319. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8320. CUBLAS_CHECK(cublasSetStream(g_cublas_handles[g_main_device], main_stream));
  8321. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8322. //void * src0_ddq = src0_extra->data_device[g_main_device];
  8323. //half * src0_as_f16 = (half *) src0_ddq;
  8324. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8325. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  8326. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  8327. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  8328. // convert src1 to fp16
  8329. const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
  8330. GGML_ASSERT(to_fp16_cuda != nullptr);
  8331. size_t src1_as = 0;
  8332. half * src1_as_f16 = (half *) ggml_cuda_pool_malloc(ne1 * sizeof(half), &src1_as);
  8333. to_fp16_cuda(src1_ddf, src1_as_f16, ne1, main_stream);
  8334. size_t dst_as = 0;
  8335. half * dst_f16 = (half *) ggml_cuda_pool_malloc(ne * sizeof(half), &dst_as);
  8336. GGML_ASSERT(ne12 % ne02 == 0);
  8337. GGML_ASSERT(ne13 % ne03 == 0);
  8338. // broadcast factors
  8339. const int64_t r2 = ne12/ne02;
  8340. const int64_t r3 = ne13/ne03;
  8341. const half alpha_f16 = 1.0f;
  8342. const half beta_f16 = 0.0f;
  8343. // use cublasGemmBatchedEx
  8344. const int ne23 = ne12*ne13;
  8345. const void ** ptrs_src = nullptr;
  8346. void ** ptrs_dst = nullptr;
  8347. size_t ptrs_src_s = 0;
  8348. size_t ptrs_dst_s = 0;
  8349. ptrs_src = (const void **) ggml_cuda_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  8350. ptrs_dst = ( void **) ggml_cuda_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  8351. int64_t src0_ne = ggml_nelements(src00);
  8352. half * src0_as_f16 = nullptr;
  8353. size_t src0_as = 0;
  8354. if (src00->type != GGML_TYPE_F16) {
  8355. src0_as_f16 = (half *) ggml_cuda_pool_malloc(src0_ne * sizeof(half), &src0_as);
  8356. }
  8357. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  8358. dim3 block_dims(ne13, ne12);
  8359. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  8360. ptrs_src, ptrs_dst,
  8361. ne12, ne13,
  8362. ne23,
  8363. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  8364. nb12, nb13,
  8365. dst->nb[2], dst->nb[3],
  8366. r2, r3,
  8367. src00->type, src0_as_f16, src0_ne,
  8368. src1_as_f16, dst_f16,
  8369. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  8370. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  8371. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  8372. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  8373. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  8374. );
  8375. CUDA_CHECK(cudaGetLastError());
  8376. CUBLAS_CHECK(
  8377. cublasGemmBatchedEx(g_cublas_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  8378. ne01, ne11, ne10,
  8379. &alpha_f16, (const void **) (ptrs_src + 0*ne23), CUDA_R_16F, ne00,
  8380. (const void **) (ptrs_src + 1*ne23), CUDA_R_16F, ne10,
  8381. &beta_f16, ( void **) (ptrs_dst + 0*ne23), CUDA_R_16F, ne01,
  8382. ne23,
  8383. CUBLAS_COMPUTE_16F,
  8384. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  8385. if (src0_as != 0) {
  8386. ggml_cuda_pool_free(src0_as_f16, src0_as);
  8387. }
  8388. if (ptrs_src_s != 0) {
  8389. ggml_cuda_pool_free(ptrs_src, ptrs_src_s);
  8390. }
  8391. if (ptrs_dst_s != 0) {
  8392. ggml_cuda_pool_free(ptrs_dst, ptrs_dst_s);
  8393. }
  8394. const to_fp32_cuda_t to_fp32_cuda = ggml_get_to_fp32_cuda(GGML_TYPE_F16);
  8395. to_fp32_cuda(dst_f16, dst_ddf, ne, main_stream);
  8396. ggml_cuda_pool_free(src1_as_f16, src1_as);
  8397. ggml_cuda_pool_free(dst_f16, dst_as);
  8398. }
  8399. #endif
  8400. static void ggml_cuda_mul_mat_id(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8401. #if 0
  8402. ggml_cuda_mul_mat_id_cublas(dst);
  8403. // TODO: mmq/mmv support
  8404. #endif
  8405. const size_t nb11 = src1->nb[1];
  8406. const size_t nb1 = dst->nb[1];
  8407. const struct ggml_tensor * ids = src0;
  8408. const int32_t id = ((int32_t *) dst->op_params)[0];
  8409. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  8410. std::vector<char> ids_host(ggml_nbytes(ids));
  8411. cudaStream_t stream = g_cudaStreams[g_main_device][0];
  8412. if (ids->backend == GGML_BACKEND_GPU) {
  8413. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  8414. CUDA_CHECK(cudaMemcpyAsync(ids_host.data(), ids_dev, ggml_nbytes(ids), cudaMemcpyDeviceToHost, stream));
  8415. CUDA_CHECK(cudaStreamSynchronize(stream));
  8416. } else {
  8417. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  8418. }
  8419. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  8420. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  8421. ggml_tensor_extra_gpu src1_row_extra;
  8422. ggml_tensor_extra_gpu dst_row_extra;
  8423. ggml_tensor src1_row = *src1;
  8424. ggml_tensor dst_row = *dst;
  8425. src1_row.backend = GGML_BACKEND_GPU;
  8426. dst_row.backend = GGML_BACKEND_GPU;
  8427. src1_row.extra = &src1_row_extra;
  8428. dst_row.extra = &dst_row_extra;
  8429. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  8430. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  8431. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  8432. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  8433. if (src1->ne[1] == 1) {
  8434. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8435. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  8436. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8437. //int32_t row_id;
  8438. //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]));
  8439. //CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[g_main_device][0]));
  8440. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8441. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8442. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8443. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  8444. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  8445. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  8446. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  8447. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8448. }
  8449. } else {
  8450. cuda_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  8451. cuda_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  8452. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  8453. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  8454. const cudaMemcpyKind src1_kind = src1->backend == GGML_BACKEND_CPU ?
  8455. cudaMemcpyHostToDevice : cudaMemcpyDeviceToDevice;
  8456. const cudaMemcpyKind dst_kind = dst->backend == GGML_BACKEND_CPU ?
  8457. cudaMemcpyDeviceToHost : cudaMemcpyDeviceToDevice;
  8458. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  8459. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  8460. int64_t num_src1_rows = 0;
  8461. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8462. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8463. if (row_id_i != row_id) {
  8464. continue;
  8465. }
  8466. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8467. CUDA_CHECK(cudaMemcpyAsync(src1_contiguous.get() + num_src1_rows*nb11, src1_original + i01*nb11,
  8468. nb11, src1_kind, stream));
  8469. num_src1_rows++;
  8470. }
  8471. if (num_src1_rows == 0) {
  8472. continue;
  8473. }
  8474. src1_row.ne[1] = num_src1_rows;
  8475. dst_row.ne[1] = num_src1_rows;
  8476. src1_row.nb[1] = nb11;
  8477. src1_row.nb[2] = num_src1_rows*nb11;
  8478. src1_row.nb[3] = num_src1_rows*nb11;
  8479. dst_row.nb[1] = nb1;
  8480. dst_row.nb[2] = num_src1_rows*nb1;
  8481. dst_row.nb[3] = num_src1_rows*nb1;
  8482. ggml_cuda_mul_mat(src0_row, &src1_row, &dst_row);
  8483. num_src1_rows = 0;
  8484. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  8485. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  8486. if (row_id_i != row_id) {
  8487. continue;
  8488. }
  8489. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  8490. CUDA_CHECK(cudaMemcpyAsync(dst_original + i01*nb1, dst_contiguous.get() + num_src1_rows*nb1,
  8491. nb1, dst_kind, stream));
  8492. num_src1_rows++;
  8493. }
  8494. }
  8495. }
  8496. if (dst->backend == GGML_BACKEND_CPU) {
  8497. CUDA_CHECK(cudaStreamSynchronize(stream));
  8498. }
  8499. }
  8500. static void ggml_cuda_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8501. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_scale);
  8502. }
  8503. static void ggml_cuda_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8504. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_clamp);
  8505. }
  8506. static void ggml_cuda_cpy(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8507. const int64_t ne = ggml_nelements(src0);
  8508. GGML_ASSERT(ne == ggml_nelements(src1));
  8509. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  8510. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  8511. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  8512. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  8513. const int64_t ne00 = src0->ne[0];
  8514. const int64_t ne01 = src0->ne[1];
  8515. const int64_t ne02 = src0->ne[2];
  8516. //GGML_ASSERT(src0->ne[3] == 1);
  8517. const int64_t nb00 = src0->nb[0];
  8518. const int64_t nb01 = src0->nb[1];
  8519. const int64_t nb02 = src0->nb[2];
  8520. const int64_t nb03 = src0->nb[3];
  8521. const int64_t ne10 = src1->ne[0];
  8522. const int64_t ne11 = src1->ne[1];
  8523. const int64_t ne12 = src1->ne[2];
  8524. //GGML_ASSERT(src1->ne[3] == 1);
  8525. const int64_t nb10 = src1->nb[0];
  8526. const int64_t nb11 = src1->nb[1];
  8527. const int64_t nb12 = src1->nb[2];
  8528. const int64_t nb13 = src1->nb[3];
  8529. ggml_cuda_set_device(g_main_device);
  8530. cudaStream_t main_stream = g_cudaStreams[g_main_device][0];
  8531. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  8532. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  8533. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  8534. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  8535. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  8536. ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8537. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  8538. ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8539. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  8540. ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8541. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  8542. ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8543. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  8544. ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8545. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  8546. ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8547. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  8548. ggml_cpy_f16_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  8549. } else {
  8550. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  8551. ggml_type_name(src0->type), ggml_type_name(src1->type));
  8552. GGML_ASSERT(false);
  8553. }
  8554. (void) dst;
  8555. }
  8556. static void ggml_cuda_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8557. // TODO: why do we pass dst as src1 here?
  8558. ggml_cuda_cpy(src0, dst, nullptr);
  8559. (void) src1;
  8560. }
  8561. static void ggml_cuda_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8562. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_diag_mask_inf);
  8563. }
  8564. static void ggml_cuda_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8565. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_soft_max);
  8566. }
  8567. static void ggml_cuda_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8568. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  8569. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_rope);
  8570. }
  8571. static void ggml_cuda_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8572. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_alibi);
  8573. }
  8574. static void ggml_cuda_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8575. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_pool2d);
  8576. }
  8577. static void ggml_cuda_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8578. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_im2col);
  8579. }
  8580. static void ggml_cuda_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8581. GGML_ASSERT(ggml_is_contiguous(src0));
  8582. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_sum_rows);
  8583. }
  8584. static void ggml_cuda_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8585. GGML_ASSERT(ggml_is_contiguous(src0));
  8586. ggml_cuda_op_flatten(src0, src1, dst, ggml_cuda_op_argsort);
  8587. }
  8588. static void ggml_cuda_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  8589. (void) src0;
  8590. (void) src1;
  8591. (void) dst;
  8592. }
  8593. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  8594. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  8595. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  8596. }
  8597. GGML_CALL static void ggml_cuda_set_main_device(const int main_device) {
  8598. if (main_device >= g_device_count) {
  8599. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  8600. main_device, g_device_count, g_main_device);
  8601. return;
  8602. }
  8603. if (g_main_device != main_device && g_device_count > 1) {
  8604. g_main_device = main_device;
  8605. //cudaDeviceProp prop;
  8606. //CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
  8607. //fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
  8608. }
  8609. }
  8610. GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  8611. if (!g_cublas_loaded) return false;
  8612. ggml_cuda_func_t func;
  8613. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  8614. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  8615. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  8616. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  8617. return false;
  8618. }
  8619. if (tensor->op == GGML_OP_MUL_MAT) {
  8620. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  8621. #ifndef NDEBUG
  8622. 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]);
  8623. #endif
  8624. return false;
  8625. }
  8626. }
  8627. switch (tensor->op) {
  8628. case GGML_OP_REPEAT:
  8629. func = ggml_cuda_repeat;
  8630. break;
  8631. case GGML_OP_GET_ROWS:
  8632. func = ggml_cuda_get_rows;
  8633. break;
  8634. case GGML_OP_DUP:
  8635. func = ggml_cuda_dup;
  8636. break;
  8637. case GGML_OP_ADD:
  8638. func = ggml_cuda_add;
  8639. break;
  8640. case GGML_OP_ACC:
  8641. func = ggml_cuda_acc;
  8642. break;
  8643. case GGML_OP_MUL:
  8644. func = ggml_cuda_mul;
  8645. break;
  8646. case GGML_OP_DIV:
  8647. func = ggml_cuda_div;
  8648. break;
  8649. case GGML_OP_UNARY:
  8650. switch (ggml_get_unary_op(tensor)) {
  8651. case GGML_UNARY_OP_GELU:
  8652. func = ggml_cuda_gelu;
  8653. break;
  8654. case GGML_UNARY_OP_SILU:
  8655. func = ggml_cuda_silu;
  8656. break;
  8657. case GGML_UNARY_OP_GELU_QUICK:
  8658. func = ggml_cuda_gelu_quick;
  8659. break;
  8660. case GGML_UNARY_OP_TANH:
  8661. func = ggml_cuda_tanh;
  8662. break;
  8663. case GGML_UNARY_OP_RELU:
  8664. func = ggml_cuda_relu;
  8665. break;
  8666. case GGML_UNARY_OP_HARDSIGMOID:
  8667. func = ggml_cuda_hardsigmoid;
  8668. break;
  8669. case GGML_UNARY_OP_HARDSWISH:
  8670. func = ggml_cuda_hardswish;
  8671. break;
  8672. default:
  8673. return false;
  8674. }
  8675. break;
  8676. case GGML_OP_NORM:
  8677. func = ggml_cuda_norm;
  8678. break;
  8679. case GGML_OP_GROUP_NORM:
  8680. func = ggml_cuda_group_norm;
  8681. break;
  8682. case GGML_OP_CONCAT:
  8683. func = ggml_cuda_concat;
  8684. break;
  8685. case GGML_OP_UPSCALE:
  8686. func = ggml_cuda_upscale;
  8687. break;
  8688. case GGML_OP_PAD:
  8689. func = ggml_cuda_pad;
  8690. break;
  8691. case GGML_OP_LEAKY_RELU:
  8692. func = ggml_cuda_leaky_relu;
  8693. break;
  8694. case GGML_OP_RMS_NORM:
  8695. func = ggml_cuda_rms_norm;
  8696. break;
  8697. case GGML_OP_MUL_MAT:
  8698. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  8699. return false;
  8700. }
  8701. func = ggml_cuda_mul_mat;
  8702. break;
  8703. case GGML_OP_MUL_MAT_ID:
  8704. if (!any_on_device && !ggml_cuda_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  8705. return false;
  8706. }
  8707. func = ggml_cuda_mul_mat_id;
  8708. break;
  8709. case GGML_OP_SCALE:
  8710. func = ggml_cuda_scale;
  8711. break;
  8712. case GGML_OP_SQR:
  8713. func = ggml_cuda_sqr;
  8714. break;
  8715. case GGML_OP_CLAMP:
  8716. func = ggml_cuda_clamp;
  8717. break;
  8718. case GGML_OP_CPY:
  8719. func = ggml_cuda_cpy;
  8720. break;
  8721. case GGML_OP_CONT:
  8722. func = ggml_cuda_dup;
  8723. break;
  8724. case GGML_OP_NONE:
  8725. case GGML_OP_RESHAPE:
  8726. case GGML_OP_VIEW:
  8727. case GGML_OP_PERMUTE:
  8728. case GGML_OP_TRANSPOSE:
  8729. func = ggml_cuda_nop;
  8730. break;
  8731. case GGML_OP_DIAG_MASK_INF:
  8732. func = ggml_cuda_diag_mask_inf;
  8733. break;
  8734. case GGML_OP_SOFT_MAX:
  8735. func = ggml_cuda_soft_max;
  8736. break;
  8737. case GGML_OP_ROPE:
  8738. func = ggml_cuda_rope;
  8739. break;
  8740. case GGML_OP_ALIBI:
  8741. func = ggml_cuda_alibi;
  8742. break;
  8743. case GGML_OP_IM2COL:
  8744. func = ggml_cuda_im2col;
  8745. break;
  8746. case GGML_OP_POOL_2D:
  8747. func = ggml_cuda_pool2d;
  8748. break;
  8749. case GGML_OP_SUM_ROWS:
  8750. func = ggml_cuda_sum_rows;
  8751. break;
  8752. case GGML_OP_ARGSORT:
  8753. func = ggml_cuda_argsort;
  8754. break;
  8755. default:
  8756. return false;
  8757. }
  8758. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  8759. ggml_cuda_set_peer_access(tensor->src[1]->ne[1]);
  8760. }
  8761. if (params->ith != 0) {
  8762. return true;
  8763. }
  8764. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  8765. return true;
  8766. }
  8767. func(tensor->src[0], tensor->src[1], tensor);
  8768. return true;
  8769. }
  8770. GGML_CALL int ggml_cuda_get_device_count() {
  8771. int device_count;
  8772. if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
  8773. return 0;
  8774. }
  8775. return device_count;
  8776. }
  8777. GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
  8778. cudaDeviceProp prop;
  8779. CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
  8780. snprintf(description, description_size, "%s", prop.name);
  8781. }
  8782. ////////////////////////////////////////////////////////////////////////////////
  8783. // backend interface
  8784. #define UNUSED GGML_UNUSED
  8785. struct ggml_backend_cuda_context {
  8786. int device;
  8787. std::string name;
  8788. };
  8789. // cuda buffer
  8790. struct ggml_backend_cuda_buffer_context {
  8791. int device;
  8792. void * dev_ptr = nullptr;
  8793. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  8794. size_t temp_tensor_extra_index = 0;
  8795. std::string name;
  8796. ggml_backend_cuda_buffer_context(int device, void * dev_ptr) :
  8797. device(device), dev_ptr(dev_ptr),
  8798. name(GGML_CUDA_NAME + std::to_string(device)) {
  8799. }
  8800. ~ggml_backend_cuda_buffer_context() {
  8801. delete[] temp_tensor_extras;
  8802. }
  8803. ggml_tensor_extra_gpu * ggml_cuda_alloc_temp_tensor_extra() {
  8804. // TODO: remove GGML_CUDA_MAX_NODES, allocate dynamically and reuse in backend_buffer_reset
  8805. if (temp_tensor_extras == nullptr) {
  8806. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_CUDA_MAX_NODES];
  8807. }
  8808. size_t alloc_index = temp_tensor_extra_index;
  8809. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_CUDA_MAX_NODES;
  8810. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  8811. memset(extra, 0, sizeof(*extra));
  8812. return extra;
  8813. }
  8814. };
  8815. GGML_CALL static const char * ggml_backend_cuda_buffer_get_name(ggml_backend_buffer_t buffer) {
  8816. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8817. return ctx->name.c_str();
  8818. }
  8819. GGML_CALL static bool ggml_backend_buffer_is_cuda(ggml_backend_buffer_t buffer) {
  8820. return buffer->iface.get_name == ggml_backend_cuda_buffer_get_name;
  8821. }
  8822. GGML_CALL static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  8823. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8824. CUDA_CHECK(cudaFree(ctx->dev_ptr));
  8825. delete ctx;
  8826. }
  8827. GGML_CALL static void * ggml_backend_cuda_buffer_get_base(ggml_backend_buffer_t buffer) {
  8828. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8829. return ctx->dev_ptr;
  8830. }
  8831. GGML_CALL static void ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  8832. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8833. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  8834. assert(tensor->view_src->buffer->buft == buffer->buft);
  8835. tensor->backend = tensor->view_src->backend;
  8836. tensor->extra = tensor->view_src->extra;
  8837. return;
  8838. }
  8839. ggml_tensor_extra_gpu * extra = ctx->ggml_cuda_alloc_temp_tensor_extra();
  8840. extra->data_device[ctx->device] = tensor->data;
  8841. tensor->backend = GGML_BACKEND_GPU;
  8842. tensor->extra = extra;
  8843. if (ggml_is_quantized(tensor->type)) {
  8844. // initialize padding to 0 to avoid possible NaN values
  8845. size_t original_size = ggml_nbytes(tensor);
  8846. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  8847. if (padded_size > original_size && tensor->view_src == nullptr) {
  8848. CUDA_CHECK(cudaMemset((char *)tensor->data + original_size, 0, padded_size - original_size));
  8849. }
  8850. }
  8851. }
  8852. GGML_CALL 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) {
  8853. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8854. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8855. ggml_cuda_set_device(ctx->device);
  8856. CUDA_CHECK(cudaDeviceSynchronize());
  8857. CUDA_CHECK(cudaMemcpy((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice));
  8858. CUDA_CHECK(cudaDeviceSynchronize());
  8859. }
  8860. GGML_CALL 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) {
  8861. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  8862. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8863. ggml_cuda_set_device(ctx->device);
  8864. CUDA_CHECK(cudaDeviceSynchronize());
  8865. CUDA_CHECK(cudaMemcpy(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost));
  8866. CUDA_CHECK(cudaDeviceSynchronize());
  8867. }
  8868. GGML_CALL static bool ggml_backend_cuda_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
  8869. if (ggml_backend_buffer_is_cuda(src->buffer)) {
  8870. ggml_backend_cuda_buffer_context * src_ctx = (ggml_backend_cuda_buffer_context *)src->buffer->context;
  8871. ggml_backend_cuda_buffer_context * dst_ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8872. ggml_cuda_set_device(src_ctx->device);
  8873. CUDA_CHECK(cudaDeviceSynchronize());
  8874. ggml_cuda_set_device(dst_ctx->device);
  8875. CUDA_CHECK(cudaDeviceSynchronize());
  8876. CUDA_CHECK(cudaMemcpy((char *)dst->data, (const char *)src->data, ggml_nbytes(src), cudaMemcpyDeviceToDevice));
  8877. CUDA_CHECK(cudaDeviceSynchronize());
  8878. return true;
  8879. }
  8880. return false;
  8881. }
  8882. GGML_CALL static void ggml_backend_cuda_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  8883. ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
  8884. ggml_cuda_set_device(ctx->device);
  8885. CUDA_CHECK(cudaDeviceSynchronize());
  8886. CUDA_CHECK(cudaMemset(ctx->dev_ptr, value, buffer->size));
  8887. CUDA_CHECK(cudaDeviceSynchronize());
  8888. }
  8889. static ggml_backend_buffer_i ggml_backend_cuda_buffer_interface = {
  8890. /* .get_name = */ ggml_backend_cuda_buffer_get_name,
  8891. /* .free_buffer = */ ggml_backend_cuda_buffer_free_buffer,
  8892. /* .get_base = */ ggml_backend_cuda_buffer_get_base,
  8893. /* .init_tensor = */ ggml_backend_cuda_buffer_init_tensor,
  8894. /* .set_tensor = */ ggml_backend_cuda_buffer_set_tensor,
  8895. /* .get_tensor = */ ggml_backend_cuda_buffer_get_tensor,
  8896. /* .cpy_tensor = */ ggml_backend_cuda_buffer_cpy_tensor,
  8897. /* .clear = */ ggml_backend_cuda_buffer_clear,
  8898. /* .reset = */ NULL,
  8899. };
  8900. // cuda buffer type
  8901. struct ggml_backend_cuda_buffer_type_context {
  8902. int device;
  8903. std::string name;
  8904. };
  8905. GGML_CALL static const char * ggml_backend_cuda_buffer_type_name(ggml_backend_buffer_type_t buft) {
  8906. ggml_backend_cuda_buffer_type_context * ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8907. return ctx->name.c_str();
  8908. }
  8909. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  8910. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8911. ggml_cuda_set_device(buft_ctx->device);
  8912. size = std::max(size, (size_t)1); // cudaMalloc returns null for size 0
  8913. void * dev_ptr;
  8914. cudaError_t err = cudaMalloc(&dev_ptr, size);
  8915. if (err != cudaSuccess) {
  8916. fprintf(stderr, "%s: allocating %.2f MiB on device %d: cudaMalloc failed: %s\n", __func__, size/1024.0/1024.0, buft_ctx->device, cudaGetErrorString(err));
  8917. return nullptr;
  8918. }
  8919. ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
  8920. return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
  8921. }
  8922. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  8923. return 128;
  8924. UNUSED(buft);
  8925. }
  8926. GGML_CALL static size_t ggml_backend_cuda_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  8927. size_t size = ggml_nbytes(tensor);
  8928. int64_t ne0 = tensor->ne[0];
  8929. if (ggml_is_quantized(tensor->type)) {
  8930. if (ne0 % MATRIX_ROW_PADDING != 0) {
  8931. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  8932. }
  8933. }
  8934. return size;
  8935. UNUSED(buft);
  8936. }
  8937. GGML_CALL static bool ggml_backend_cuda_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  8938. if (!ggml_backend_is_cuda(backend)) {
  8939. return false;
  8940. }
  8941. ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buft->context;
  8942. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  8943. return buft_ctx->device == cuda_ctx->device;
  8944. }
  8945. static ggml_backend_buffer_type_i ggml_backend_cuda_buffer_type_interface = {
  8946. /* .get_name = */ ggml_backend_cuda_buffer_type_name,
  8947. /* .alloc_buffer = */ ggml_backend_cuda_buffer_type_alloc_buffer,
  8948. /* .get_alignment = */ ggml_backend_cuda_buffer_type_get_alignment,
  8949. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  8950. /* .get_alloc_size = */ ggml_backend_cuda_buffer_type_get_alloc_size,
  8951. /* .supports_backend = */ ggml_backend_cuda_buffer_type_supports_backend,
  8952. /* .is_host = */ NULL,
  8953. };
  8954. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
  8955. // FIXME: this is not thread safe
  8956. if (device >= ggml_backend_cuda_get_device_count()) {
  8957. return nullptr;
  8958. }
  8959. static ggml_backend_buffer_type ggml_backend_cuda_buffer_types[GGML_CUDA_MAX_DEVICES];
  8960. static bool ggml_backend_cuda_buffer_type_initialized = false;
  8961. if (!ggml_backend_cuda_buffer_type_initialized) {
  8962. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; i++) {
  8963. ggml_backend_cuda_buffer_types[i] = {
  8964. /* .iface = */ ggml_backend_cuda_buffer_type_interface,
  8965. /* .context = */ new ggml_backend_cuda_buffer_type_context{i, GGML_CUDA_NAME + std::to_string(i)},
  8966. };
  8967. }
  8968. ggml_backend_cuda_buffer_type_initialized = true;
  8969. }
  8970. return &ggml_backend_cuda_buffer_types[device];
  8971. }
  8972. // cuda split buffer
  8973. struct ggml_backend_cuda_split_buffer_context {
  8974. ~ggml_backend_cuda_split_buffer_context() {
  8975. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  8976. for (int id = 0; id < g_device_count; ++id) {
  8977. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  8978. if (extra->events[id][is] != nullptr) {
  8979. CUDA_CHECK(cudaEventDestroy(extra->events[id][is]));
  8980. }
  8981. }
  8982. if (extra->data_device[id] != nullptr) {
  8983. CUDA_CHECK(cudaFree(extra->data_device[id]));
  8984. }
  8985. }
  8986. delete extra;
  8987. }
  8988. }
  8989. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  8990. };
  8991. GGML_CALL static const char * ggml_backend_cuda_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  8992. return GGML_CUDA_NAME "_Split";
  8993. UNUSED(buffer);
  8994. }
  8995. // unused at the moment
  8996. //static bool ggml_backend_buffer_is_cuda_split(ggml_backend_buffer_t buffer) {
  8997. // return buffer->iface.get_name == ggml_backend_cuda_split_buffer_get_name;
  8998. //}
  8999. GGML_CALL static void ggml_backend_cuda_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9000. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9001. delete ctx;
  9002. }
  9003. GGML_CALL static void * ggml_backend_cuda_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  9004. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  9005. return (void *)0x1000;
  9006. UNUSED(buffer);
  9007. }
  9008. GGML_CALL static void ggml_backend_cuda_split_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
  9009. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  9010. ggml_backend_cuda_split_buffer_context * ctx = (ggml_backend_cuda_split_buffer_context *)buffer->context;
  9011. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9012. const int64_t ne0 = tensor->ne[0];
  9013. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  9014. ctx->tensor_extras.push_back(extra);
  9015. for (int id = 0; id < g_device_count; ++id) {
  9016. int64_t row_low, row_high;
  9017. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9018. int64_t nrows_split = row_high - row_low;
  9019. if (nrows_split == 0) {
  9020. continue;
  9021. }
  9022. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9023. const size_t original_size = size;
  9024. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9025. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9026. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9027. }
  9028. // FIXME: do not crash if cudaMalloc fails
  9029. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  9030. ggml_cuda_set_device(id);
  9031. char * buf;
  9032. CUDA_CHECK(cudaMalloc(&buf, size));
  9033. // set padding to 0 to avoid possible NaN values
  9034. if (size > original_size) {
  9035. CUDA_CHECK(cudaMemset(buf + original_size, 0, size - original_size));
  9036. }
  9037. extra->data_device[id] = buf;
  9038. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  9039. CUDA_CHECK(cudaEventCreateWithFlags(&extra->events[id][is], cudaEventDisableTiming));
  9040. }
  9041. }
  9042. tensor->backend = GGML_BACKEND_GPU_SPLIT;
  9043. tensor->extra = extra;
  9044. }
  9045. GGML_CALL static void ggml_backend_cuda_split_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9046. // split tensors must always be set in their entirety at once
  9047. GGML_ASSERT(offset == 0);
  9048. GGML_ASSERT(size == ggml_nbytes(tensor));
  9049. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9050. const int64_t ne0 = tensor->ne[0];
  9051. const size_t nb1 = tensor->nb[1];
  9052. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9053. for (int id = 0; id < g_device_count; ++id) {
  9054. int64_t row_low, row_high;
  9055. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9056. int64_t nrows_split = row_high - row_low;
  9057. if (nrows_split == 0) {
  9058. continue;
  9059. }
  9060. const size_t offset_split = row_low*nb1;
  9061. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9062. const size_t original_size = size;
  9063. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9064. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9065. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9066. }
  9067. const char * buf_host = (const char *)data + offset_split;
  9068. CUDA_CHECK(cudaMemcpy(extra->data_device[id], buf_host, original_size, cudaMemcpyHostToDevice));
  9069. }
  9070. }
  9071. GGML_CALL static void ggml_backend_cuda_split_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9072. // split tensors must always be set in their entirety at once
  9073. GGML_ASSERT(offset == 0);
  9074. GGML_ASSERT(size == ggml_nbytes(tensor));
  9075. ggml_backend_cuda_split_buffer_type_context * buft_ctx = (ggml_backend_cuda_split_buffer_type_context *)buffer->buft->context;
  9076. const int64_t ne0 = tensor->ne[0];
  9077. const size_t nb1 = tensor->nb[1];
  9078. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  9079. for (int id = 0; id < g_device_count; ++id) {
  9080. int64_t row_low, row_high;
  9081. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, id);
  9082. int64_t nrows_split = row_high - row_low;
  9083. if (nrows_split == 0) {
  9084. continue;
  9085. }
  9086. const size_t offset_split = row_low*nb1;
  9087. size_t size = ggml_nbytes_split(tensor, nrows_split);
  9088. const size_t original_size = size;
  9089. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9090. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9091. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9092. }
  9093. char * buf_host = (char *)data + offset_split;
  9094. CUDA_CHECK(cudaMemcpy(buf_host, extra->data_device[id], original_size, cudaMemcpyDeviceToHost));
  9095. }
  9096. }
  9097. GGML_CALL static void ggml_backend_cuda_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  9098. UNUSED(buffer);
  9099. UNUSED(value);
  9100. }
  9101. static struct ggml_backend_buffer_i ggml_backend_cuda_split_buffer_interface = {
  9102. /* .get_name = */ ggml_backend_cuda_split_buffer_get_name,
  9103. /* .free_buffer = */ ggml_backend_cuda_split_buffer_free_buffer,
  9104. /* .get_base = */ ggml_backend_cuda_split_buffer_get_base,
  9105. /* .init_tensor = */ ggml_backend_cuda_split_buffer_init_tensor,
  9106. /* .set_tensor = */ ggml_backend_cuda_split_buffer_set_tensor,
  9107. /* .get_tensor = */ ggml_backend_cuda_split_buffer_get_tensor,
  9108. /* .cpy_tensor = */ NULL,
  9109. /* .clear = */ ggml_backend_cuda_split_buffer_clear,
  9110. /* .reset = */ NULL,
  9111. };
  9112. // cuda split buffer type
  9113. GGML_CALL static const char * ggml_backend_cuda_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9114. return GGML_CUDA_NAME "_Split";
  9115. UNUSED(buft);
  9116. }
  9117. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9118. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  9119. // instead, we allocate them for each tensor separately in init_tensor
  9120. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  9121. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  9122. ggml_backend_cuda_split_buffer_context * ctx = new ggml_backend_cuda_split_buffer_context();
  9123. return ggml_backend_buffer_init(buft, ggml_backend_cuda_split_buffer_interface, ctx, size);
  9124. }
  9125. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  9126. return 128;
  9127. UNUSED(buft);
  9128. }
  9129. GGML_CALL static size_t ggml_backend_cuda_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  9130. ggml_backend_cuda_split_buffer_type_context * ctx = (ggml_backend_cuda_split_buffer_type_context *)buft->context;
  9131. size_t total_size = 0;
  9132. const int64_t ne0 = tensor->ne[0];
  9133. for (int id = 0; id < g_device_count; ++id) {
  9134. int64_t row_low, row_high;
  9135. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, id);
  9136. int64_t nrows_split = row_high - row_low;
  9137. if (nrows_split == 0) {
  9138. continue;
  9139. }
  9140. total_size += ggml_nbytes_split(tensor, nrows_split);
  9141. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  9142. if (ne0 % MATRIX_ROW_PADDING != 0) {
  9143. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  9144. }
  9145. }
  9146. return total_size;
  9147. }
  9148. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  9149. return ggml_backend_is_cuda(backend);
  9150. UNUSED(buft);
  9151. }
  9152. GGML_CALL static bool ggml_backend_cuda_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  9153. return false;
  9154. UNUSED(buft);
  9155. }
  9156. static ggml_backend_buffer_type_i ggml_backend_cuda_split_buffer_type_interface = {
  9157. /* .get_name = */ ggml_backend_cuda_split_buffer_type_name,
  9158. /* .alloc_buffer = */ ggml_backend_cuda_split_buffer_type_alloc_buffer,
  9159. /* .get_alignment = */ ggml_backend_cuda_split_buffer_type_get_alignment,
  9160. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9161. /* .get_alloc_size = */ ggml_backend_cuda_split_buffer_type_get_alloc_size,
  9162. /* .supports_backend = */ ggml_backend_cuda_split_buffer_type_supports_backend,
  9163. /* .is_host = */ ggml_backend_cuda_split_buffer_type_is_host,
  9164. };
  9165. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split) {
  9166. // FIXME: this is not thread safe
  9167. static std::map<std::array<float, GGML_CUDA_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  9168. std::array<float, GGML_CUDA_MAX_DEVICES> tensor_split_arr = {};
  9169. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_CUDA_MAX_DEVICES, [](float x) { return x == 0.0f; });
  9170. if (all_zero) {
  9171. tensor_split_arr = g_default_tensor_split;
  9172. } else {
  9173. float split_sum = 0.0f;
  9174. for (int i = 0; i < g_device_count; ++i) {
  9175. tensor_split_arr[i] = split_sum;
  9176. split_sum += tensor_split[i];
  9177. }
  9178. for (int i = 0; i < g_device_count; ++i) {
  9179. tensor_split_arr[i] /= split_sum;
  9180. }
  9181. }
  9182. auto it = buft_map.find(tensor_split_arr);
  9183. if (it != buft_map.end()) {
  9184. return &it->second;
  9185. }
  9186. struct ggml_backend_buffer_type buft {
  9187. /* .iface = */ ggml_backend_cuda_split_buffer_type_interface,
  9188. /* .context = */ new ggml_backend_cuda_split_buffer_type_context{tensor_split_arr},
  9189. };
  9190. auto result = buft_map.emplace(tensor_split_arr, buft);
  9191. return &result.first->second;
  9192. }
  9193. // host buffer type
  9194. GGML_CALL static const char * ggml_backend_cuda_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  9195. return GGML_CUDA_NAME "_Host";
  9196. UNUSED(buft);
  9197. }
  9198. GGML_CALL static const char * ggml_backend_cuda_host_buffer_name(ggml_backend_buffer_t buffer) {
  9199. return GGML_CUDA_NAME "_Host";
  9200. UNUSED(buffer);
  9201. }
  9202. GGML_CALL static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  9203. ggml_cuda_host_free(buffer->context);
  9204. }
  9205. GGML_CALL static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  9206. void * ptr = ggml_cuda_host_malloc(size);
  9207. if (ptr == nullptr) {
  9208. // fallback to cpu buffer
  9209. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  9210. }
  9211. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  9212. buffer->buft = buft;
  9213. buffer->iface.get_name = ggml_backend_cuda_host_buffer_name;
  9214. buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
  9215. return buffer;
  9216. }
  9217. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
  9218. static struct ggml_backend_buffer_type ggml_backend_cuda_buffer_type_host = {
  9219. /* .iface = */ {
  9220. /* .get_name = */ ggml_backend_cuda_host_buffer_type_name,
  9221. /* .alloc_buffer = */ ggml_backend_cuda_host_buffer_type_alloc_buffer,
  9222. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  9223. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  9224. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  9225. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  9226. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  9227. },
  9228. /* .context = */ nullptr,
  9229. };
  9230. return &ggml_backend_cuda_buffer_type_host;
  9231. }
  9232. // backend
  9233. GGML_CALL static const char * ggml_backend_cuda_name(ggml_backend_t backend) {
  9234. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9235. return cuda_ctx->name.c_str();
  9236. }
  9237. GGML_CALL static void ggml_backend_cuda_free(ggml_backend_t backend) {
  9238. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9239. delete cuda_ctx;
  9240. delete backend;
  9241. }
  9242. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cuda_get_default_buffer_type(ggml_backend_t backend) {
  9243. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9244. return ggml_backend_cuda_buffer_type(cuda_ctx->device);
  9245. }
  9246. GGML_CALL static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  9247. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9248. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9249. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9250. CUDA_CHECK(cudaMemcpyAsync((char *)tensor->data + offset, data, size, cudaMemcpyHostToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9251. }
  9252. GGML_CALL static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  9253. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9254. GGML_ASSERT(tensor->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
  9255. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  9256. CUDA_CHECK(cudaMemcpyAsync(data, (const char *)tensor->data + offset, size, cudaMemcpyDeviceToHost, g_cudaStreams[cuda_ctx->device][0]));
  9257. }
  9258. GGML_CALL static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst) {
  9259. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9260. if (dst->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && ggml_backend_buffer_is_cuda(src->buffer)) {
  9261. CUDA_CHECK(cudaMemcpyAsync(dst->data, src->data, ggml_nbytes(dst), cudaMemcpyDeviceToDevice, g_cudaStreams[cuda_ctx->device][0]));
  9262. return true;
  9263. }
  9264. return false;
  9265. }
  9266. GGML_CALL static void ggml_backend_cuda_synchronize(ggml_backend_t backend) {
  9267. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9268. CUDA_CHECK(cudaStreamSynchronize(g_cudaStreams[cuda_ctx->device][0]));
  9269. UNUSED(backend);
  9270. }
  9271. GGML_CALL static bool ggml_backend_cuda_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  9272. ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
  9273. ggml_cuda_set_main_device(cuda_ctx->device);
  9274. ggml_compute_params params = {};
  9275. params.type = GGML_TASK_COMPUTE;
  9276. params.ith = 0;
  9277. for (int i = 0; i < cgraph->n_nodes; i++) {
  9278. ggml_tensor * node = cgraph->nodes[i];
  9279. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  9280. continue;
  9281. }
  9282. #ifndef NDEBUG
  9283. assert(node->backend == GGML_BACKEND_GPU || node->backend == GGML_BACKEND_GPU_SPLIT);
  9284. assert(node->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9285. assert(node->extra != nullptr);
  9286. for (int j = 0; j < GGML_MAX_SRC; j++) {
  9287. if (node->src[j] != nullptr) {
  9288. assert(node->src[j]->backend == GGML_BACKEND_GPU || node->src[j]->backend == GGML_BACKEND_GPU_SPLIT);
  9289. assert(node->src[j]->buffer->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device));
  9290. assert(node->src[j]->extra != nullptr);
  9291. }
  9292. }
  9293. #endif
  9294. bool ok = ggml_cuda_compute_forward(&params, node);
  9295. if (!ok) {
  9296. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  9297. }
  9298. GGML_ASSERT(ok);
  9299. }
  9300. return true;
  9301. }
  9302. GGML_CALL static bool ggml_backend_cuda_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  9303. switch (op->op) {
  9304. case GGML_OP_UNARY:
  9305. switch (ggml_get_unary_op(op)) {
  9306. case GGML_UNARY_OP_GELU:
  9307. case GGML_UNARY_OP_SILU:
  9308. case GGML_UNARY_OP_RELU:
  9309. case GGML_UNARY_OP_HARDSIGMOID:
  9310. case GGML_UNARY_OP_HARDSWISH:
  9311. case GGML_UNARY_OP_GELU_QUICK:
  9312. case GGML_UNARY_OP_TANH:
  9313. return true;
  9314. default:
  9315. return false;
  9316. }
  9317. break;
  9318. case GGML_OP_MUL_MAT:
  9319. case GGML_OP_MUL_MAT_ID:
  9320. {
  9321. struct ggml_tensor * a;
  9322. struct ggml_tensor * b;
  9323. if (op->op == GGML_OP_MUL_MAT) {
  9324. a = op->src[0];
  9325. b = op->src[1];
  9326. } else {
  9327. a = op->src[2];
  9328. b = op->src[1];
  9329. }
  9330. if (a->ne[3] != b->ne[3]) {
  9331. return false;
  9332. }
  9333. ggml_type a_type = a->type;
  9334. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ1_S) {
  9335. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  9336. return false;
  9337. }
  9338. }
  9339. return true;
  9340. } break;
  9341. case GGML_OP_GET_ROWS:
  9342. {
  9343. switch (op->src[0]->type) {
  9344. case GGML_TYPE_F16:
  9345. case GGML_TYPE_F32:
  9346. case GGML_TYPE_Q4_0:
  9347. case GGML_TYPE_Q4_1:
  9348. case GGML_TYPE_Q5_0:
  9349. case GGML_TYPE_Q5_1:
  9350. case GGML_TYPE_Q8_0:
  9351. return true;
  9352. default:
  9353. return false;
  9354. }
  9355. } break;
  9356. case GGML_OP_CPY:
  9357. {
  9358. ggml_type src0_type = op->src[0]->type;
  9359. ggml_type src1_type = op->src[1]->type;
  9360. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  9361. return true;
  9362. }
  9363. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  9364. return true;
  9365. }
  9366. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  9367. return true;
  9368. }
  9369. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  9370. return true;
  9371. }
  9372. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  9373. return true;
  9374. }
  9375. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  9376. return true;
  9377. }
  9378. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  9379. return true;
  9380. }
  9381. return false;
  9382. } break;
  9383. case GGML_OP_DUP:
  9384. case GGML_OP_REPEAT:
  9385. case GGML_OP_CONCAT:
  9386. {
  9387. ggml_type src0_type = op->src[0]->type;
  9388. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  9389. } break;
  9390. case GGML_OP_NONE:
  9391. case GGML_OP_RESHAPE:
  9392. case GGML_OP_VIEW:
  9393. case GGML_OP_PERMUTE:
  9394. case GGML_OP_TRANSPOSE:
  9395. case GGML_OP_NORM:
  9396. case GGML_OP_ADD:
  9397. case GGML_OP_MUL:
  9398. case GGML_OP_DIV:
  9399. case GGML_OP_RMS_NORM:
  9400. case GGML_OP_SCALE:
  9401. case GGML_OP_SQR:
  9402. case GGML_OP_CLAMP:
  9403. case GGML_OP_CONT:
  9404. case GGML_OP_DIAG_MASK_INF:
  9405. case GGML_OP_SOFT_MAX:
  9406. case GGML_OP_ROPE:
  9407. case GGML_OP_ALIBI:
  9408. case GGML_OP_IM2COL:
  9409. case GGML_OP_POOL_2D:
  9410. case GGML_OP_SUM_ROWS:
  9411. case GGML_OP_ARGSORT:
  9412. case GGML_OP_ACC:
  9413. case GGML_OP_GROUP_NORM:
  9414. case GGML_OP_UPSCALE:
  9415. case GGML_OP_PAD:
  9416. case GGML_OP_LEAKY_RELU:
  9417. return true;
  9418. default:
  9419. return false;
  9420. }
  9421. UNUSED(backend);
  9422. }
  9423. static ggml_backend_i ggml_backend_cuda_interface = {
  9424. /* .get_name = */ ggml_backend_cuda_name,
  9425. /* .free = */ ggml_backend_cuda_free,
  9426. /* .get_default_buffer_type = */ ggml_backend_cuda_get_default_buffer_type,
  9427. /* .set_tensor_async = */ ggml_backend_cuda_set_tensor_async,
  9428. /* .get_tensor_async = */ ggml_backend_cuda_get_tensor_async,
  9429. /* .cpy_tensor_async = */ ggml_backend_cuda_cpy_tensor_async,
  9430. /* .synchronize = */ ggml_backend_cuda_synchronize,
  9431. /* .graph_plan_create = */ NULL,
  9432. /* .graph_plan_free = */ NULL,
  9433. /* .graph_plan_compute = */ NULL,
  9434. /* .graph_compute = */ ggml_backend_cuda_graph_compute,
  9435. /* .supports_op = */ ggml_backend_cuda_supports_op,
  9436. };
  9437. GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
  9438. ggml_init_cublas(); // TODO: remove from ggml.c
  9439. if (device < 0 || device >= ggml_cuda_get_device_count()) {
  9440. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  9441. return nullptr;
  9442. }
  9443. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  9444. ggml_cuda_set_main_device(device);
  9445. ggml_backend_cuda_context * ctx = new ggml_backend_cuda_context {
  9446. /* .device = */ device,
  9447. /* .name = */ GGML_CUDA_NAME + std::to_string(device),
  9448. };
  9449. ggml_backend_t cuda_backend = new ggml_backend {
  9450. /* .interface = */ ggml_backend_cuda_interface,
  9451. /* .context = */ ctx
  9452. };
  9453. return cuda_backend;
  9454. }
  9455. GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
  9456. return backend && backend->iface.get_name == ggml_backend_cuda_name;
  9457. }
  9458. GGML_CALL int ggml_backend_cuda_get_device_count() {
  9459. return ggml_cuda_get_device_count();
  9460. }
  9461. GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size) {
  9462. ggml_cuda_get_device_description(device, description, description_size);
  9463. }
  9464. GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total) {
  9465. ggml_cuda_set_device(device);
  9466. CUDA_CHECK(cudaMemGetInfo(free, total));
  9467. }
  9468. // backend registry
  9469. GGML_CALL static ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
  9470. ggml_backend_t cuda_backend = ggml_backend_cuda_init((int) (intptr_t) user_data);
  9471. return cuda_backend;
  9472. UNUSED(params);
  9473. }
  9474. extern "C" GGML_CALL int ggml_backend_cuda_reg_devices();
  9475. GGML_CALL int ggml_backend_cuda_reg_devices() {
  9476. int device_count = ggml_cuda_get_device_count();
  9477. //int device_count = 1; // DEBUG: some tools require delaying CUDA initialization
  9478. for (int i = 0; i < device_count; i++) {
  9479. char name[128];
  9480. snprintf(name, sizeof(name), "%s%d", GGML_CUDA_NAME, i);
  9481. ggml_backend_register(name, ggml_backend_reg_cuda_init, ggml_backend_cuda_buffer_type(i), (void *) (intptr_t) i);
  9482. }
  9483. return device_count;
  9484. }